./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
69 / 138
50.0%
Branches:
51 / 118
43.2%
Functions:
3 / 4
75.0%

kfsw-services/src/command/command_protocol.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include <zephyr/sys/byteorder.h>
7
8 #include "command_internal.h"
9
10 /* Wire codec. Every length is checked against the buffer before use. */
11
12 4 int kfsw_command_protocol_encode(uint8_t *buffer, size_t capacity,
13 const struct kfsw_command_message *message, size_t *encoded_size)
14 {
15 size_t size;
16 size_t header;
17 uint8_t version;
18
19
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 if ((buffer == NULL) || (message == NULL) || (encoded_size == NULL) ||
20
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (message->payload_size > KFSW_COMMAND_MAX_PAYLOAD_SIZE) ||
21
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 ((message->payload_size != 0U) && (message->payload == NULL)) ||
22
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (message->arg_count > KFSW_COMMAND_MAX_ARGS)) {
23 ✗ return -EINVAL;
24 }
25
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 version = message->version ? message->version : KFSW_COMMAND_PROTOCOL_VERSION;
26
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if ((version != 1U) && (version != 2U)) {
27 ✗ return -EPROTONOSUPPORT;
28 }
29
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 header = (version == 2U) ? KFSW_COMMAND_RETRY_HEADER_SIZE : KFSW_COMMAND_HEADER_SIZE;
30 4 size = header + message->payload_size;
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (size > capacity) {
32 ✗ return -EMSGSIZE;
33 }
34
35 4 memset(buffer, 0, header);
36 4 buffer[0] = version;
37
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (version == 2U) {
38 2 sys_put_be64(message->token, &buffer[12]);
39 }
40 4 buffer[1] = message->opcode;
41 4 buffer[2] = message->status;
42 4 buffer[3] = message->arg_count;
43 4 sys_put_be16(message->command_id, &buffer[4]);
44 4 sys_put_be16(message->request_id, &buffer[6]);
45 4 sys_put_be16(message->payload_size, &buffer[8]);
46
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (message->payload_size != 0U) {
47 4 memcpy(&buffer[header], message->payload, message->payload_size);
48 }
49 4 *encoded_size = size;
50 4 return 0;
51 }
52
53 10 int kfsw_command_protocol_decode(const uint8_t *buffer, size_t size,
54 struct kfsw_command_message *message)
55 {
56 size_t header;
57
58
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
10 if ((buffer == NULL) || (message == NULL) || (size < KFSW_COMMAND_HEADER_SIZE)) {
59 ✗ return -EMSGSIZE;
60 }
61
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
10 if ((buffer[0] != 1U) && (buffer[0] != 2U)) {
62 ✗ return -EPROTONOSUPPORT;
63 }
64
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 header = (buffer[0] == 2U) ? KFSW_COMMAND_RETRY_HEADER_SIZE : KFSW_COMMAND_HEADER_SIZE;
65
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (size < header) {
66 2 return -EMSGSIZE;
67 }
68
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 if ((buffer[10] != 0U) || (buffer[11] != 0U)) {
69 2 return -EBADMSG;
70 }
71
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
12 if ((buffer[1] < KFSW_COMMAND_OP_REQUEST) ||
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 (buffer[1] > ((buffer[0] == 2U) ? KFSW_COMMAND_OP_EXECUTE : KFSW_COMMAND_OP_RESULT))) {
73 2 return -ENOTSUP;
74 }
75
76 4 memset(message, 0, sizeof(*message));
77 4 message->version = buffer[0];
78
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 message->token = (buffer[0] == 2U) ? sys_get_be64(&buffer[12]) : 0U;
79 4 message->opcode = buffer[1];
80 4 message->status = buffer[2];
81 4 message->arg_count = buffer[3];
82 4 message->command_id = sys_get_be16(&buffer[4]);
83 4 message->request_id = sys_get_be16(&buffer[6]);
84 4 message->payload_size = sys_get_be16(&buffer[8]);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (message->arg_count > KFSW_COMMAND_MAX_ARGS) {
87 ✗ return -E2BIG;
88 }
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (message->payload_size > KFSW_COMMAND_MAX_PAYLOAD_SIZE) {
90 ✗ return -EMSGSIZE;
91 }
92 /* The declared payload size must match what arrived. */
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if ((size_t)(header + message->payload_size) != size) {
94 ✗ return -EMSGSIZE;
95 }
96 4 message->payload = &buffer[header];
97 4 return 0;
98 }
99
100 ✗ int kfsw_command_encode_args(const struct kfsw_command_arg *args, size_t arg_count,
101 uint8_t *payload, size_t capacity, uint16_t *payload_size)
102 {
103 ✗ size_t offset = 0U;
104
105
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if ((payload == NULL) || (payload_size == NULL) || ((arg_count != 0U) && (args == NULL))) {
106 ✗ return -EINVAL;
107 }
108
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (arg_count > KFSW_COMMAND_MAX_ARGS) {
109 ✗ return -E2BIG;
110 }
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (size_t index = 0U; index < arg_count; index++) {
112 size_t value_size;
113
114
0/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ switch (args[index].type) {
115 ✗ case KFSW_COMMAND_TYPE_U32:
116 case KFSW_COMMAND_TYPE_I32:
117 ✗ value_size = sizeof(uint32_t);
118 ✗ break;
119 ✗ case KFSW_COMMAND_TYPE_TEXT:
120
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (args[index].value.text == NULL) {
121 ✗ return -EINVAL;
122 }
123 ✗ value_size = 0U;
124
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ while ((value_size <= KFSW_COMMAND_MAX_TEXT_SIZE) &&
125
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (args[index].value.text[value_size] != '\0')) {
126 ✗ value_size++;
127 }
128
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (value_size > KFSW_COMMAND_MAX_TEXT_SIZE) {
129 ✗ return -ENAMETOOLONG;
130 }
131 ✗ break;
132 ✗ default:
133 ✗ return -ENOTSUP;
134 }
135
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((offset + 3U + value_size) > capacity) {
136 ✗ return -EMSGSIZE;
137 }
138 ✗ payload[offset] = (uint8_t)args[index].type;
139 ✗ sys_put_be16((uint16_t)value_size, &payload[offset + 1U]);
140 ✗ offset += 3U;
141
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (args[index].type == KFSW_COMMAND_TYPE_TEXT) {
142 ✗ memcpy(&payload[offset], args[index].value.text, value_size);
143
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (args[index].type == KFSW_COMMAND_TYPE_U32) {
144 ✗ sys_put_be32(args[index].value.u32, &payload[offset]);
145 } else {
146 ✗ sys_put_be32((uint32_t)args[index].value.i32, &payload[offset]);
147 }
148 ✗ offset += value_size;
149 }
150 ✗ *payload_size = (uint16_t)offset;
151 ✗ return 0;
152 }
153
154 34 int kfsw_command_decode_args(const struct kfsw_command_message *message,
155 struct kfsw_command_arg *args, size_t max_args,
156 char text_storage[][KFSW_COMMAND_MAX_TEXT_SIZE + 1U])
157 {
158 34 size_t offset = 0U;
159 34 size_t text_index = 0U;
160
161
3/6
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 34 times.
34 if ((message == NULL) || (args == NULL) || (text_storage == NULL)) {
162 ✗ return -EINVAL;
163 }
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (message->arg_count > max_args) {
165 ✗ return -E2BIG;
166 }
167
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 34 times.
68 for (size_t index = 0U; index < message->arg_count; index++) {
168 uint8_t type;
169 uint16_t value_size;
170
171 /* Each entry needs its type byte and length before its value. */
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if ((offset + 3U) > message->payload_size) {
173 ✗ return -EMSGSIZE;
174 }
175 34 type = message->payload[offset];
176 34 value_size = sys_get_be16(&message->payload[offset + 1U]);
177 34 offset += 3U;
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if ((offset + value_size) > message->payload_size) {
179 ✗ return -EMSGSIZE;
180 }
181
182
1/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34 switch (type) {
183 34 case KFSW_COMMAND_TYPE_U32:
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (value_size != sizeof(uint32_t)) {
185 ✗ return -EBADMSG;
186 }
187 34 args[index].type = KFSW_COMMAND_TYPE_U32;
188 34 args[index].value.u32 = sys_get_be32(&message->payload[offset]);
189 34 break;
190 ✗ case KFSW_COMMAND_TYPE_I32:
191
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (value_size != sizeof(int32_t)) {
192 ✗ return -EBADMSG;
193 }
194 ✗ args[index].type = KFSW_COMMAND_TYPE_I32;
195 ✗ args[index].value.i32 = (int32_t)sys_get_be32(&message->payload[offset]);
196 ✗ break;
197 ✗ case KFSW_COMMAND_TYPE_TEXT:
198
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (value_size > KFSW_COMMAND_MAX_TEXT_SIZE) {
199 ✗ return -EMSGSIZE;
200 }
201
0/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (memchr(&message->payload[offset], '\0', value_size) != NULL) {
202 ✗ return -EBADMSG;
203 }
204 ✗ memcpy(text_storage[text_index], &message->payload[offset], value_size);
205 ✗ text_storage[text_index][value_size] = '\0';
206 ✗ args[index].type = KFSW_COMMAND_TYPE_TEXT;
207 ✗ args[index].value.text = text_storage[text_index];
208 ✗ text_index++;
209 ✗ break;
210 ✗ default:
211 ✗ return -ENOTSUP;
212 }
213 34 offset += value_size;
214 }
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (offset != message->payload_size) {
216 ✗ return -EMSGSIZE;
217 }
218 34 return (int)message->arg_count;
219 }
220