./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
709 / 1088
65.2%
Branches:
427 / 782
54.6%
Functions:
42 / 52
80.8%

kfsw-services/src/parameter/parameter_csp.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/kernel.h>
7 #include <zephyr/sys/atomic.h>
8 #include <zephyr/sys/util.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/crc.h>
11
12 #include <csp/csp.h>
13 #include <csp/csp_id.h>
14
15 #include <mpack/mpack.h>
16 #include <param/param.h>
17 #include <param/param_list.h>
18 #include <param/param_queue.h>
19 #include <param/param_serializer.h>
20 #include <param/param_server.h>
21
22 #include <kfsw/comms/csp.h>
23 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_PARAM
24 #include <kfsw/services/log.h>
25 #include <kfsw/services/parameter.h>
26
27 #include "parameter_internal.h"
28
29 /* Upstream's version 3 list wire structure; kept private to this adapter. */
30 #include "param_list.h"
31
32 #define KFSW_PARAM_PROTOCOL_VERSION 2
33 #define KFSW_PARAM_LIST_VERSION 3
34
35 /* v4: one indexed descriptor per CRC-protected request/reply. v3 remains
36 * available to legacy clients, including their RDP handshake-only requests. */
37 #define KFSW_PARAM_LIST_INDEXED_VERSION 4U
38 #define KFSW_PARAM_LIST_REPLY_HEADER 10U
39 #define KFSW_PARAM_LIST_REQUEST_SIZE 7U
40 /* Ask for one named descriptor instead of the whole list. A node that doesn't
41 * support it doesn't answer, and the caller falls back to the list. */
42 #define KFSW_PARAM_LIST_LOOKUP_VERSION 5U
43 #define KFSW_PARAM_LIST_LOOKUP_HEADER 2U
44 /* v6: the tables a node carries, with their names, instead of every descriptor
45 * in them. A node that does not support it does not answer, and the caller
46 * falls back to counting the list. */
47 #define KFSW_PARAM_TABLES_VERSION 6U
48 #define KFSW_PARAM_TABLES_HEADER 3U
49 /* v7: the descriptors of one table. A whole node does not fit the refresh
50 * budget at 4800 baud, which is what asking per table is for. */
51 #define KFSW_PARAM_LIST_TABLE_VERSION 7U
52 #define KFSW_PARAM_LIST_TABLE_REQUEST_SIZE 8U
53 #define KFSW_PARAM_TABLES_ENTRY_HEADER 5U
54 #define KFSW_PARAM_LIST_ITEM 0U
55 #define KFSW_PARAM_LIST_END 1U
56 #define KFSW_PARAM_LIST_CHANGED 2U
57 #define KFSW_PARAM_REMOTE_BATCH_MAX 16U
58 #define KFSW_PARAM_READ_MAX_ATTEMPTS 3U
59 #define KFSW_PARAM_REPLY_MAX_PACKETS 64U
60 #define KFSW_PARAM_HWID_ANY 1234U
61 #define KFSW_PARAM_LIST_SOCKET_OPTIONS CSP_SO_CRC32REQ
62
63 static param_t local_parameters[KFSW_PARAM_MAX_DEFINITIONS];
64 static bool local_parameters_registered;
65 static uint32_t local_list_crc;
66
67 static size_t scalar_size(enum kfsw_param_type type);
68 static int decode_value(mpack_reader_t *reader, const param_t *param,
69 struct kfsw_param_value *value);
70
71 406 static void parameter_changed_from_csp(const param_t *param, int offset)
72 {
73 ARG_UNUSED(offset);
74
75 406 kfsw_param_value_accepted(param->id);
76 406 }
77
78 122 static param_type_e to_libparam_type(enum kfsw_param_type type)
79 {
80
8/17
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 8 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 10 times.
✓ Branch 15 taken 8 times.
✗ Branch 16 not taken.
122 switch (type) {
81 20 case KFSW_PARAM_U8:
82 20 return PARAM_TYPE_UINT8;
83 12 case KFSW_PARAM_U16:
84 12 return PARAM_TYPE_UINT16;
85 54 case KFSW_PARAM_U32:
86 54 return PARAM_TYPE_UINT32;
87 ✗ case KFSW_PARAM_U64:
88 ✗ return PARAM_TYPE_UINT64;
89 8 case KFSW_PARAM_I8:
90 8 return PARAM_TYPE_INT8;
91 ✗ case KFSW_PARAM_I16:
92 ✗ return PARAM_TYPE_INT16;
93 ✗ case KFSW_PARAM_I32:
94 ✗ return PARAM_TYPE_INT32;
95 2 case KFSW_PARAM_I64:
96 2 return PARAM_TYPE_INT64;
97 ✗ case KFSW_PARAM_X8:
98 ✗ return PARAM_TYPE_XINT8;
99 ✗ case KFSW_PARAM_X16:
100 ✗ return PARAM_TYPE_XINT16;
101 ✗ case KFSW_PARAM_X32:
102 ✗ return PARAM_TYPE_XINT32;
103 ✗ case KFSW_PARAM_X64:
104 ✗ return PARAM_TYPE_XINT64;
105 8 case KFSW_PARAM_FLOAT:
106 8 return PARAM_TYPE_FLOAT;
107 ✗ case KFSW_PARAM_DOUBLE:
108 ✗ return PARAM_TYPE_DOUBLE;
109 10 case KFSW_PARAM_STRING:
110 10 return PARAM_TYPE_STRING;
111 8 case KFSW_PARAM_DATA:
112 8 return PARAM_TYPE_DATA;
113 ✗ case KFSW_PARAM_INVALID:
114 default:
115 ✗ return PARAM_TYPE_INVALID;
116 }
117 }
118
119 18 static int register_local_parameters(void)
120 {
121
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (local_parameters_registered) {
122 8 return 0;
123 }
124
125 10 memset(local_parameters, 0, sizeof(local_parameters));
126
2/2
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 10 times.
132 for (size_t index = kfsw_param_entry_count(); index > 0U; index--) {
127 122 const struct kfsw_param_entry *entry = kfsw_param_entry_at(index - 1U);
128 122 param_t *descriptor = &local_parameters[index - 1U];
129
130 122 descriptor->node = (uint16_t *)&node_self;
131 122 descriptor->id = entry->info.id;
132 122 descriptor->type = to_libparam_type(entry->info.type);
133 122 descriptor->name = (char *)entry->info.name;
134 122 descriptor->array_size = entry->info.array_size;
135 /* libparam's width for the type; a string has no scalar width. */
136 122 descriptor->array_step = param_typesize(descriptor->type);
137 122 descriptor->mask = entry->info.flags;
138 122 descriptor->unit = (char *)entry->info.unit;
139 122 descriptor->callback = parameter_changed_from_csp;
140 122 descriptor->addr = entry->definition->value;
141 122 descriptor->docstr = (char *)entry->info.description;
142
143
2/4
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 122 times.
122 if ((descriptor->type == PARAM_TYPE_INVALID) || (param_list_add(descriptor) != 0)) {
144 ✗ return -EEXIST;
145 }
146 }
147 10 local_parameters_registered = true;
148 10 return 0;
149 }
150
151 _Static_assert(sizeof(param_transfer3_t) + KFSW_PARAM_LIST_REPLY_HEADER <= CSP_BUFFER_SIZE,
152 "CSP buffers must fit an upstream parameter-list entry");
153
154 K_MUTEX_DEFINE(kfsw_param_remote_lock);
155
156 static bool server_started;
157 static K_MUTEX_DEFINE(server_start_lock);
158 static csp_socket_t list_socket;
159
160 1492 static enum kfsw_param_type from_libparam_type(param_type_e type)
161 {
162
6/17
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1120 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 72 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 96 times.
✓ Branch 15 taken 96 times.
✗ Branch 16 not taken.
1492 switch (type) {
163 84 case PARAM_TYPE_UINT8:
164 84 return KFSW_PARAM_U8;
165 ✗ case PARAM_TYPE_UINT16:
166 ✗ return KFSW_PARAM_U16;
167 1120 case PARAM_TYPE_UINT32:
168 1120 return KFSW_PARAM_U32;
169 ✗ case PARAM_TYPE_UINT64:
170 ✗ return KFSW_PARAM_U64;
171 24 case PARAM_TYPE_INT8:
172 24 return KFSW_PARAM_I8;
173 ✗ case PARAM_TYPE_INT16:
174 ✗ return KFSW_PARAM_I16;
175 ✗ case PARAM_TYPE_INT32:
176 ✗ return KFSW_PARAM_I32;
177 ✗ case PARAM_TYPE_INT64:
178 ✗ return KFSW_PARAM_I64;
179 ✗ case PARAM_TYPE_XINT8:
180 ✗ return KFSW_PARAM_X8;
181 ✗ case PARAM_TYPE_XINT16:
182 ✗ return KFSW_PARAM_X16;
183 ✗ case PARAM_TYPE_XINT32:
184 ✗ return KFSW_PARAM_X32;
185 ✗ case PARAM_TYPE_XINT64:
186 ✗ return KFSW_PARAM_X64;
187 72 case PARAM_TYPE_FLOAT:
188 72 return KFSW_PARAM_FLOAT;
189 ✗ case PARAM_TYPE_DOUBLE:
190 ✗ return KFSW_PARAM_DOUBLE;
191 96 case PARAM_TYPE_STRING:
192 96 return KFSW_PARAM_STRING;
193 96 case PARAM_TYPE_DATA:
194 96 return KFSW_PARAM_DATA;
195 ✗ case PARAM_TYPE_INVALID:
196 default:
197 ✗ return KFSW_PARAM_INVALID;
198 }
199 }
200
201 1356 static size_t scalar_size(enum kfsw_param_type type)
202 {
203
4/7
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 984 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 192 times.
1356 switch (type) {
204 108 case KFSW_PARAM_U8:
205 case KFSW_PARAM_I8:
206 case KFSW_PARAM_X8:
207 108 return sizeof(uint8_t);
208 ✗ case KFSW_PARAM_U16:
209 case KFSW_PARAM_I16:
210 case KFSW_PARAM_X16:
211 ✗ return sizeof(uint16_t);
212 984 case KFSW_PARAM_U32:
213 case KFSW_PARAM_I32:
214 case KFSW_PARAM_X32:
215 984 return sizeof(uint32_t);
216 ✗ case KFSW_PARAM_U64:
217 case KFSW_PARAM_I64:
218 case KFSW_PARAM_X64:
219 ✗ return sizeof(uint64_t);
220 72 case KFSW_PARAM_FLOAT:
221 72 return sizeof(float);
222 ✗ case KFSW_PARAM_DOUBLE:
223 ✗ return sizeof(double);
224 192 case KFSW_PARAM_STRING:
225 case KFSW_PARAM_DATA:
226 case KFSW_PARAM_INVALID:
227 default:
228 192 return 0U;
229 }
230 }
231
232 2 static int validate_scalar(const param_t *param, const struct kfsw_param_value *value)
233 {
234 enum kfsw_param_type type;
235 size_t size;
236
237
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((param == NULL) || (value == NULL)) {
238 ✗ return -EINVAL;
239 }
240
241 2 type = from_libparam_type((param_type_e)param->type);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (type == KFSW_PARAM_DATA) {
243 /* Arrays are written whole. */
244
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((value->type != type) || (value->size != param->array_size)) {
245 ✗ return -EMSGSIZE;
246 }
247 ✗ return 0;
248 }
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (type == KFSW_PARAM_STRING) {
250
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (value->type != type) {
251 ✗ return -EMSGSIZE;
252 }
253 /* size includes the terminator. */
254
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((value->size == 0U) || (value->size > param->array_size)) {
255 ✗ return -EMSGSIZE;
256 }
257 ✗ return 0;
258 }
259
260 2 size = scalar_size(type);
261
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((size == 0U) || (param->array_size != 1U)) {
262 ✗ return -ENOTSUP;
263 }
264
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((value->type != type) || (value->size != size)) {
265 ✗ return -EMSGSIZE;
266 }
267 2 return 0;
268 }
269
270 108 static int read_scalar(const param_t *param, struct kfsw_param_value *value)
271 {
272 enum kfsw_param_type type;
273 size_t size;
274
275
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
108 if ((param == NULL) || (value == NULL)) {
276 ✗ return -EINVAL;
277 }
278
279 108 type = from_libparam_type((param_type_e)param->type);
280
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (type == KFSW_PARAM_DATA) {
282
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((param->array_size == 0U) || (param->array_size > sizeof(value->bytes))) {
283 ✗ return -ENOTSUP;
284 }
285 ✗ memset(value, 0, sizeof(*value));
286 ✗ value->type = type;
287 ✗ param_get_data(param, value->bytes, (int)param->array_size);
288 ✗ value->size = param->array_size;
289 ✗ return 0;
290 }
291
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (type == KFSW_PARAM_STRING) {
293 size_t length;
294
295
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((param->array_size == 0U) || (param->array_size > sizeof(value->text))) {
296 ✗ return -ENOTSUP;
297 }
298 ✗ memset(value, 0, sizeof(*value));
299 ✗ value->type = type;
300 ✗ param_get_data(param, value->text, (int)param->array_size);
301 /* Terminate the copy; the remote value may not be. */
302 ✗ value->text[param->array_size - 1U] = '\0';
303 ✗ length = 0U;
304
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ while (value->text[length] != '\0') {
305 ✗ length++;
306 }
307 ✗ value->size = length + 1U;
308 ✗ return 0;
309 }
310
311 108 size = scalar_size(type);
312
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
108 if ((size == 0U) || (param->array_size != 1U)) {
313 ✗ return -ENOTSUP;
314 }
315
316 108 memset(value, 0, sizeof(*value));
317 108 value->type = type;
318 108 value->size = size;
319 108 param_get(param, 0U, &value->scalar);
320 108 return 0;
321 }
322
323 ✗ static void fill_info(const param_t *param, struct kfsw_param_info *info)
324 {
325 ✗ info->node = *param->node;
326 ✗ info->id = param->id;
327 /* The wire ID has the table in the high byte and the offset in the low byte.
328 * Table names are not sent, so remote listings show the number.
329 */
330 ✗ info->table = (uint8_t)(param->id >> 8);
331 ✗ info->offset = (uint8_t)(param->id & 0xFFU);
332 ✗ info->table_name = NULL;
333 ✗ info->array_size = param->array_size;
334 ✗ info->type = from_libparam_type((param_type_e)param->type);
335 ✗ info->flags = param->mask;
336 ✗ info->name = param->name;
337 ✗ info->unit = param->unit;
338 ✗ info->description = param->docstr;
339 ✗ info->read_only = (param->mask & PM_READONLY) != 0U;
340 ✗ }
341
342 774 static bool push_allowed(csp_packet_t *packet)
343 {
344 774 param_queue_t queue = {0};
345 mpack_reader_t reader;
346 size_t payload_length;
347 int version;
348 774 bool first = true;
349 774 size_t count = 0U;
350 774 const struct kfsw_param_entry *fallible = NULL;
351 774 union kfsw_param_scalar proposed = {0};
352 bool response;
353
354
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 772 times.
774 if (packet->length < 2U) {
355 2 return false;
356 }
357
358
4/4
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 380 times.
✓ Branch 2 taken 190 times.
✓ Branch 3 taken 24 times.
772 switch (packet->data[0]) {
359 178 case PARAM_PULL_RESPONSE:
360 case PARAM_PUSH_REQUEST:
361 178 version = 1;
362 178 break;
363 380 case PARAM_PULL_RESPONSE_V2:
364 case PARAM_PUSH_REQUEST_V2:
365 380 version = 2;
366 380 break;
367 190 case PARAM_PUSH_REQUEST_V2_HWID:
368
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 188 times.
190 if (packet->length < (2U + sizeof(uint32_t))) {
369 28 return false;
370 }
371 uint32_t hwid;
372 __attribute__((weak)) int serial_get(void);
373
374 188 memcpy(&hwid, &packet->data[packet->length - sizeof(hwid)], sizeof(hwid));
375
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 120 times.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 24 times.
188 if ((serial_get != NULL) && (hwid != (uint32_t)serial_get()) &&
376
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 42 times.
44 (hwid != KFSW_PARAM_HWID_ANY)) {
377 2 return false;
378 }
379 186 version = 2;
380 186 break;
381 24 default:
382 24 return true;
383 }
384
385 744 response =
386
4/4
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 724 times.
744 packet->data[0] == PARAM_PULL_RESPONSE || packet->data[0] == PARAM_PULL_RESPONSE_V2;
387 744 payload_length = packet->length - 2U;
388
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 558 times.
744 if (packet->data[0] == PARAM_PUSH_REQUEST_V2_HWID) {
389 186 payload_length -= sizeof(uint32_t);
390 }
391 744 param_queue_init(&queue, &packet->data[2], payload_length, payload_length,
392 PARAM_QUEUE_TYPE_SET, version);
393 744 mpack_reader_init_data(&reader, queue.buffer, queue.used);
394
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 362 times.
1568 while (reader.data < reader.end) {
395 1206 csp_timestamp_t timestamp = {0};
396 const param_t *param;
397 struct kfsw_param_value value;
398 1206 int offset = -1;
399 1206 int node = 0;
400 1206 int id = 0;
401
402
2/2
✓ Branch 0 taken 880 times.
✓ Branch 1 taken 326 times.
1206 if (version == 2) {
403 880 mpack_reader_t peek = reader;
404 880 uint16_t header = be16toh(mpack_expect_u16(&peek));
405
406 /* param_queue_apply() does not initialize last_node. */
407
3/4
✓ Branch 1 taken 880 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 566 times.
✓ Branch 4 taken 314 times.
880 if (mpack_reader_error(&peek) != mpack_ok ||
408
3/4
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 872 times.
✗ Branch 3 not taken.
880 (first && (header & BIT(14)) == 0U) || (header & BIT(10)) != 0U ||
409
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 872 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
872 ((header & BIT(11)) != 0U && (header & BIT(13)) == 0U)) {
410 8 return false;
411 }
412 }
413 1198 param_deserialize_id(&reader, &id, &node, &timestamp, &offset, &queue);
414 1198 first = false;
415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1198 times.
1198 if (mpack_reader_error(&reader) != mpack_ok) {
416 ✗ return false;
417 }
418
419 /* Match param_queue_apply(), including cached responses from a peer. */
420
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1172 times.
1198 param = param_list_find_id(node == 0 ? packet->id.src : node, id);
421
2/2
✓ Branch 0 taken 1188 times.
✓ Branch 1 taken 10 times.
1198 if ((param == NULL) ||
422
2/2
✓ Branch 0 taken 1184 times.
✓ Branch 1 taken 4 times.
1188 ((param->mask & (PM_READONLY | KFSW_PARAM_FLAG_LOCAL_ONLY)) != 0U) ||
423
3/4
✓ Branch 0 taken 1184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 1136 times.
1184 (offset < -1) || (offset > 0)) {
424 62 return false;
425 }
426
427 1136 bool array = mpack_peek_tag(&reader).type == mpack_type_array;
428
429
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1088 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 24 times.
1136 if (array && mpack_expect_array(&reader) != 1U) {
430 24 return false;
431 }
432
2/2
✓ Branch 1 taken 172 times.
✓ Branch 2 taken 940 times.
1112 if (decode_value(&reader, param, &value) != 0) {
433 172 return false;
434 }
435
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 916 times.
940 if (array) {
436 24 mpack_done_array(&reader);
437 }
438
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 940 times.
940 if (mpack_reader_error(&reader) != mpack_ok) {
439 ✗ return false;
440 }
441 940 count++;
442
2/2
✓ Branch 0 taken 922 times.
✓ Branch 1 taken 18 times.
940 if (*param->node == node_self) {
443 922 const struct kfsw_param_entry *entry = kfsw_param_find_id(param->id);
444
445
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 920 times.
922 if (response) {
446 2 return false;
447 }
448
2/2
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 806 times.
920 if (kfsw_param_validate_entry(entry, &value) != 0) {
449 114 kfsw_log_warning("Remote parameter '%s' refused", param->name);
450 114 return false;
451 }
452
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 736 times.
806 if (entry->definition->apply != NULL) {
453 70 fallible = entry;
454 70 proposed = value.scalar;
455 }
456 }
457 }
458
459
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 314 times.
362 if (fallible != NULL) {
460
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 30 times.
48 if (count != 1U) {
461 18 kfsw_log_warning("Remote parameter '%s' requires a standalone write",
462 fallible->info.name);
463 18 return false;
464 }
465 30 int result = fallible->definition->apply(&proposed);
466
467
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 14 times.
30 if (result != 0) {
468 16 kfsw_log_warning("Remote parameter '%s' application failed: %d",
469 fallible->info.name, result);
470 16 return false;
471 }
472 }
473 328 return true;
474 }
475
476 BUILD_ASSERT(CONFIG_KFSW_PARAM_VALUE_PRIORITY > CONFIG_KFSW_CSP_ROUTER_PRIORITY,
477 "PARAM value worker must run below the CSP router");
478 BUILD_ASSERT(CONFIG_KFSW_PARAM_VALUE_PRIORITY < CONFIG_NUM_PREEMPT_PRIORITIES,
479 "PARAM value worker priority is outside the preemptible range");
480 BUILD_ASSERT(CONFIG_KFSW_PARAM_VALUE_QUEUE_DEPTH + 4U <= CSP_BUFFER_COUNT,
481 "Reserve CSP buffers for the worker, its reply, routing and list traffic");
482
483 K_MSGQ_DEFINE(value_requests, sizeof(csp_packet_t *), CONFIG_KFSW_PARAM_VALUE_QUEUE_DEPTH,
484 sizeof(void *));
485 static atomic_t value_requests_dropped;
486
487 112 uint32_t kfsw_param_csp_dropped_requests(void)
488 {
489 112 return (uint32_t)atomic_get(&value_requests_dropped);
490 }
491
492 774 static void serve_values(csp_packet_t *packet)
493 {
494
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 774 times.
774 if (!kfsw_param_is_initialized()) {
495 ✗ csp_buffer_free(packet);
496 ✗ return;
497 }
498
499 774 kfsw_param_table_lock();
500
2/2
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 422 times.
774 if (push_allowed(packet)) {
501 /* PUSH must apply against the state its validators inspected. */
502
1/2
✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
352 if (packet->data[0] == PARAM_PULL_REQUEST ||
503
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 24 times.
352 packet->data[0] == PARAM_PULL_REQUEST_V2 ||
504
1/2
✓ Branch 0 taken 328 times.
✗ Branch 1 not taken.
328 packet->data[0] == PARAM_PULL_ALL_REQUEST ||
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
328 packet->data[0] == PARAM_PULL_ALL_REQUEST_V2) {
506 24 kfsw_param_sample_all();
507 }
508 352 param_serve(packet);
509 } else {
510 422 csp_buffer_free(packet);
511 }
512 774 kfsw_param_table_unlock();
513 }
514
515 798 static void param_server_callback(csp_packet_t *packet)
516 {
517
3/4
✓ Branch 1 taken 798 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 774 times.
1596 if (!kfsw_param_is_initialized() ||
518 798 (k_msgq_put(&value_requests, &packet, K_NO_WAIT) != 0)) {
519 24 atomic_inc(&value_requests_dropped);
520 24 csp_buffer_free(packet);
521 }
522 798 }
523
524 10 static void value_server(void *a, void *b, void *c)
525 {
526 ARG_UNUSED(a);
527 ARG_UNUSED(b);
528 ARG_UNUSED(c);
529 774 for (;;) {
530 csp_packet_t *packet;
531
532 784 k_msgq_get(&value_requests, &packet, K_FOREVER);
533 774 serve_values(packet);
534 }
535 }
536
537 K_THREAD_DEFINE(kfsw_param_value_thread, CONFIG_KFSW_PARAM_VALUE_STACK_SIZE, value_server, NULL,
538 NULL, NULL, CONFIG_KFSW_PARAM_VALUE_PRIORITY, 0, SYS_FOREVER_MS);
539
540 1332 static uint32_t remaining_ms(int64_t deadline)
541 {
542
3/4
✓ Branch 1 taken 1316 times.
✓ Branch 2 taken 16 times.
✓ Branch 4 taken 1316 times.
✗ Branch 5 not taken.
1332 return (uint32_t)CLAMP(deadline - k_uptime_get(), 0, UINT32_MAX);
543 }
544
545 /* Table zero walks every parameter; any other walks that table alone. */
546 100 static const param_t *list_entry_in(uint8_t table, uint16_t wanted, uint16_t *total)
547 {
548 100 const param_t *found = NULL;
549
550 100 *total = 0;
551
2/2
✓ Branch 1 taken 884 times.
✓ Branch 2 taken 100 times.
984 for (size_t i = 0; i < kfsw_param_entry_count(); i++) {
552 884 const param_t *param = &local_parameters[i];
553
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 884 times.
884 if ((param->mask & PM_HIDDEN) != 0U) {
555 ✗ continue;
556 }
557
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 884 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
884 if (table != 0U && (uint8_t)(param->id >> 8) != table) {
558 ✗ continue;
559 }
560
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 800 times.
884 if ((*total)++ == wanted) {
561 84 found = param;
562 }
563 }
564 100 return found;
565 }
566
567 /* Match the same entries list_entry_in() lists, so a lookup and a list agree.
568 */
569 ✗ static const param_t *local_entry_by_name(const char *name)
570 {
571
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ for (size_t i = 0; i < kfsw_param_entry_count(); i++) {
572 ✗ const param_t *param = &local_parameters[i];
573
574
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((param->mask & PM_HIDDEN) != 0U) {
575 ✗ continue;
576 }
577
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (strcmp(param->name, name) == 0) {
578 ✗ return param;
579 }
580 }
581 ✗ return NULL;
582 }
583
584 274 static size_t encode_descriptor(const param_t *param, uint8_t *data)
585 {
586 274 param_transfer3_t *wire = (param_transfer3_t *)data;
587
588 274 memset(wire, 0, sizeof(*wire));
589 274 wire->id = htobe16(param->id);
590 274 wire->type = param->type;
591 274 wire->size = param->array_size;
592 274 wire->mask = htobe32(param->mask);
593 274 strncpy(wire->name, param->name, sizeof(wire->name) - 1U);
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 if (param->vmem != NULL) {
595 ✗ wire->storage_type = param->vmem->type;
596 }
597
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 256 times.
274 if (param->unit != NULL) {
598 18 strncpy(wire->unit, param->unit, sizeof(wire->unit) - 1U);
599 }
600 /* The help text stays on the node. */
601 274 return offsetof(param_transfer3_t, help) + 1U;
602 }
603
604 /* The fingerprint covers the descriptors the request will carry, so a client
605 * can check what it received. Table zero covers the whole node.
606 */
607 18 static uint32_t list_fingerprint(uint8_t table)
608 {
609 param_transfer3_t wire;
610 18 uint32_t crc = 0;
611
612
2/2
✓ Branch 1 taken 190 times.
✓ Branch 2 taken 18 times.
208 for (size_t i = 0; i < kfsw_param_entry_count(); i++) {
613 190 const param_t *param = &local_parameters[i];
614
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 if ((param->mask & PM_HIDDEN) != 0U) {
616 ✗ continue;
617 }
618
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
190 if (table != 0U && (uint8_t)(param->id >> 8) != table) {
619 ✗ continue;
620 }
621 190 crc = crc32_ieee_update(crc, (uint8_t *)&wire,
622 encode_descriptor(param, (uint8_t *)&wire));
623 }
624 18 return crc;
625 }
626
627 8 static void serve_list(csp_conn_t *connection)
628 {
629 8 int64_t deadline = k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS;
630 uint16_t total;
631
632 76 for (uint16_t index = 0;; index++) {
633 76 const param_t *param = list_entry_in(0U, index, &total);
634 76 csp_packet_t *packet = NULL;
635
636
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 68 times.
76 if (param == NULL) {
637 8 return;
638 }
639 /* Retry this descriptor; allocation failure must not advance the index. */
640
3/4
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
✓ Branch 4 taken 68 times.
144 while (remaining_ms(deadline) != 0U && packet == NULL) {
641 76 packet = csp_buffer_get(CSP_BUFFER_SIZE);
642
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 68 times.
76 if (packet == NULL) {
643 8 k_sleep(K_MSEC(5));
644 }
645 }
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (packet == NULL) {
647 ✗ return;
648 }
649 68 packet->length = encode_descriptor(param, packet->data);
650 /* Metadata is fixed after registration. No PARAM lock across a send. */
651 68 csp_send(connection, packet);
652 68 k_yield();
653 }
654 }
655
656 24 static void serve_indexed_list(csp_conn_t *connection, const csp_packet_t *request)
657 {
658 24 bool by_table = request->data[0] == KFSW_PARAM_LIST_TABLE_VERSION;
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 uint8_t table = by_table ? request->data[1] : 0U;
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 const uint8_t *fields = by_table ? &request->data[2] : &request->data[1];
661 24 uint16_t index = sys_get_be16(fields);
662 uint16_t total;
663 24 uint32_t expected = sys_get_be32(&fields[2]);
664 24 const param_t *param = list_entry_in(table, index, &total);
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 uint32_t crc = by_table ? list_fingerprint(table) : local_list_crc;
666 24 csp_packet_t *reply = csp_buffer_get(CSP_BUFFER_SIZE);
667
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (reply == NULL) {
669 ✗ return;
670 }
671 24 reply->data[0] = request->data[0];
672
6/8
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 16 times.
24 reply->data[1] = (index != 0U && expected != crc) || index > total
673 ? KFSW_PARAM_LIST_CHANGED
674 : (param == NULL ? KFSW_PARAM_LIST_END : KFSW_PARAM_LIST_ITEM);
675 24 sys_put_be16(index, &reply->data[2]);
676 24 sys_put_be16(total, &reply->data[4]);
677 24 sys_put_be32(crc, &reply->data[6]);
678 24 reply->length = KFSW_PARAM_LIST_REPLY_HEADER;
679
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (reply->data[1] == KFSW_PARAM_LIST_ITEM) {
680 16 reply->length +=
681 16 encode_descriptor(param, &reply->data[KFSW_PARAM_LIST_REPLY_HEADER]);
682 }
683 24 csp_send(connection, reply);
684 }
685
686 struct table_reply {
687 csp_packet_t *packet;
688 uint8_t skip;
689 uint8_t seen;
690 uint8_t written;
691 bool full;
692 };
693
694 ✗ static bool append_table(const struct kfsw_param_table_info *info, void *context)
695 {
696 ✗ struct table_reply *reply = context;
697
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const char *description = info->description != NULL ? info->description : "";
698
699
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (reply->seen++ < reply->skip) {
700 ✗ return true;
701 }
702
703 ✗ size_t name_length = strnlen(info->name, UINT8_MAX);
704 ✗ size_t description_length = strnlen(description, UINT8_MAX);
705 ✗ size_t need = KFSW_PARAM_TABLES_ENTRY_HEADER + name_length + description_length;
706 uint8_t *at;
707
708
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((size_t)reply->packet->length + need > CSP_BUFFER_SIZE) {
709 ✗ reply->full = true;
710 ✗ return false;
711 }
712 ✗ at = &reply->packet->data[reply->packet->length];
713 ✗ at[0] = info->id;
714 ✗ sys_put_be16(info->count, &at[1]);
715 ✗ at[3] = (uint8_t)name_length;
716 ✗ at[4] = (uint8_t)description_length;
717 ✗ memcpy(&at[KFSW_PARAM_TABLES_ENTRY_HEADER], info->name, name_length);
718 ✗ memcpy(&at[KFSW_PARAM_TABLES_ENTRY_HEADER + name_length], description, description_length);
719 ✗ reply->packet->length += (uint16_t)need;
720 ✗ reply->written++;
721 ✗ return true;
722 }
723
724 ✗ static void serve_tables(csp_conn_t *connection, uint8_t from)
725 {
726 ✗ struct table_reply reply = {.packet = csp_buffer_get(CSP_BUFFER_SIZE), .skip = from};
727
728
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (reply.packet == NULL) {
729 ✗ return;
730 }
731 ✗ reply.packet->data[0] = KFSW_PARAM_TABLES_VERSION;
732 ✗ reply.packet->length = KFSW_PARAM_TABLES_HEADER;
733 ✗ (void)kfsw_param_visit_tables(append_table, &reply);
734 ✗ reply.packet->data[1] = reply.written;
735
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ reply.packet->data[2] = reply.full ? 1U : 0U;
736 ✗ csp_send(connection, reply.packet);
737 }
738
739 ✗ static void serve_lookup(csp_conn_t *connection, const csp_packet_t *request)
740 {
741 char name[KFSW_PARAM_NAME_MAX + 1U];
742 ✗ size_t length = request->length - 1U;
743 const param_t *param;
744 csp_packet_t *reply;
745
746
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((length == 0U) || (length > KFSW_PARAM_NAME_MAX)) {
747 ✗ return;
748 }
749 ✗ memcpy(name, &request->data[1], length);
750 ✗ name[length] = '\0';
751
752 ✗ param = local_entry_by_name(name);
753 ✗ reply = csp_buffer_get(CSP_BUFFER_SIZE);
754
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (reply == NULL) {
755 ✗ return;
756 }
757 ✗ reply->data[0] = KFSW_PARAM_LIST_LOOKUP_VERSION;
758
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ reply->data[1] = (param == NULL) ? KFSW_PARAM_LIST_END : KFSW_PARAM_LIST_ITEM;
759 ✗ reply->length = KFSW_PARAM_LIST_LOOKUP_HEADER;
760
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (param != NULL) {
761 ✗ reply->length +=
762 ✗ encode_descriptor(param, &reply->data[KFSW_PARAM_LIST_LOOKUP_HEADER]);
763 }
764 ✗ csp_send(connection, reply);
765 }
766
767 10 static void list_server(void *arg1, void *arg2, void *arg3)
768 {
769 ARG_UNUSED(arg1);
770 ARG_UNUSED(arg2);
771 ARG_UNUSED(arg3);
772
773 32 for (;;) {
774 42 csp_conn_t *connection = csp_accept(&list_socket, CSP_MAX_DELAY);
775
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (connection == NULL) {
777 ✗ continue;
778 }
779 32 if ((csp_conn_flags(connection) & CSP_FRDP) != 0 &&
780 !IS_ENABLED(CONFIG_KFSW_PARAM_LIST_RDP)) {
781 csp_close(connection);
782 continue;
783 }
784 32 csp_packet_t *request = csp_read(connection, 0);
785
786
3/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 8 times.
32 if (request != NULL && request->length > 1U &&
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 request->data[0] == KFSW_PARAM_LIST_LOOKUP_VERSION) {
788 ✗ serve_lookup(connection, request);
789
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 } else if (request != NULL &&
790
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 ((request->length == KFSW_PARAM_LIST_REQUEST_SIZE &&
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 request->data[0] == KFSW_PARAM_LIST_INDEXED_VERSION) ||
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 (request->length == KFSW_PARAM_LIST_TABLE_REQUEST_SIZE &&
793
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ request->data[0] == KFSW_PARAM_LIST_TABLE_VERSION))) {
794 24 serve_indexed_list(connection, request);
795
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 } else if (request != NULL && request->length == 2U &&
796
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ request->data[0] == KFSW_PARAM_TABLES_VERSION) {
797 ✗ serve_tables(connection, request->data[1]);
798
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 } else if (request == NULL ||
799
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 (request->length == 1U && request->data[0] == KFSW_PARAM_LIST_VERSION)) {
800 8 serve_list(connection);
801 }
802
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (request != NULL) {
803 32 csp_buffer_free(request);
804 }
805 32 csp_close(connection);
806 }
807 }
808
809 K_THREAD_DEFINE(kfsw_param_list_thread, CONFIG_KFSW_PARAM_LIST_STACK_SIZE, list_server, NULL, NULL,
810 NULL, CONFIG_KFSW_PARAM_LIST_PRIORITY, 0, SYS_FOREVER_MS);
811
812 22 static int server_start(void)
813 {
814 struct kfsw_csp_info csp_info;
815 int result;
816
817
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
22 if (!kfsw_param_is_initialized()) {
818 2 return -EACCES;
819 }
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (server_started) {
821 ✗ return 0;
822 }
823 if (CONFIG_KFSW_PARAM_PORT == CONFIG_KFSW_PARAM_LIST_PORT) {
824 return -EINVAL;
825 }
826
827 20 kfsw_csp_get_info(&csp_info);
828
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (!csp_info.initialized) {
829 2 return -EACCES;
830 }
831 18 result = register_local_parameters();
832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (result != 0) {
833 ✗ return result;
834 }
835
836 18 local_list_crc = list_fingerprint(0U);
837 18 memset(&list_socket, 0, sizeof(list_socket));
838 18 list_socket.opts = KFSW_PARAM_LIST_SOCKET_OPTIONS;
839 18 result = csp_listen(&list_socket, 4U);
840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (result != CSP_ERR_NONE) {
841 ✗ (void)csp_socket_close(&list_socket);
842 ✗ return -EIO;
843 }
844 18 result = csp_bind(&list_socket, CONFIG_KFSW_PARAM_LIST_PORT);
845
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (result != CSP_ERR_NONE) {
846 8 (void)csp_socket_close(&list_socket);
847 8 return -EADDRINUSE;
848 }
849 10 result = csp_bind_callback(param_server_callback, CONFIG_KFSW_PARAM_PORT);
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (result != CSP_ERR_NONE) {
851 ✗ (void)csp_socket_close(&list_socket);
852 ✗ return -EADDRINUSE;
853 }
854
855 10 k_thread_start(kfsw_param_list_thread);
856 10 k_thread_start(kfsw_param_value_thread);
857 10 server_started = true;
858 10 return 0;
859 }
860
861 22 int kfsw_param_server_start(void)
862 {
863 int result;
864
865 22 k_mutex_lock(&server_start_lock, K_FOREVER);
866 22 result = server_start();
867 22 k_mutex_unlock(&server_start_lock);
868 22 return result;
869 }
870
871 88 static int validate_remote_node(uint16_t node)
872 {
873 struct kfsw_csp_info csp_info;
874 88 const unsigned int host_bits = csp_id_get_host_bits();
875
876 88 kfsw_csp_get_info(&csp_info);
877
5/6
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 86 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 84 times.
88 if (!kfsw_param_is_initialized() || !csp_info.initialized || !csp_info.router_running) {
878 4 return -EACCES;
879 }
880
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
84 if ((node == csp_info.address) || (node >= (1UL << host_bits))) {
881 ✗ return -EINVAL;
882 }
883 84 return 0;
884 }
885
886 static atomic_t read_resends;
887
888 412 uint32_t kfsw_param_csp_read_resends(void)
889 {
890 412 return (uint32_t)atomic_get(&read_resends);
891 }
892
893 /* Writers hold the remote lock; readers must not take it under the table lock. */
894 308 static void count_read_resend(bool resend)
895 {
896 308 uint32_t count = kfsw_param_csp_read_resends();
897
898
3/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 274 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
308 if (resend && count != UINT32_MAX) {
899 34 atomic_set(&read_resends, (atomic_val_t)count + 1);
900 }
901 308 }
902
903 /* The libparam pool holds one remote node at a time. Its destructor resets the
904 * whole pool, so every descriptor is unlinked before reuse. */
905 static uint16_t cached_node;
906 static bool cache_complete;
907 static const param_t *cached[CONFIG_KFSW_PARAM_REMOTE_POOL_SIZE];
908 static size_t cached_count;
909 /* Descriptors linked into the shared list so far. */
910 static size_t cached_linked;
911
912 88 static void clear_remote_cache(void)
913 {
914
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 88 times.
188 for (size_t i = 0; i < cached_linked; i++) {
915 100 param_list_remove_specific(cached[i], 0, 0);
916 }
917
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 24 times.
88 if (cached_count != 0U) {
918 64 param_list_destroy(cached[0]);
919 }
920 88 cached_count = 0;
921 88 cached_linked = 0;
922 88 cache_complete = false;
923 88 cached_node = 0;
924 88 }
925
926 136 static int validate_descriptor(const uint8_t *data, size_t size)
927 {
928 136 const param_transfer3_t *wire = (const param_transfer3_t *)data;
929
930
2/4
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
136 if (size < offsetof(param_transfer3_t, help) + 1U || size > sizeof(*wire) ||
931
4/8
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 136 times.
✗ Branch 7 not taken.
272 wire->node != 0U || wire->size == 0U || wire->size == 255U ||
932
2/4
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
✗ Branch 4 not taken.
408 from_libparam_type(wire->type) == KFSW_PARAM_INVALID || wire->name[0] == '\0' ||
933
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
272 memchr(wire->name, '\0', sizeof(wire->name)) == NULL ||
934
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
272 strnlen(wire->name, sizeof(wire->name)) > KFSW_PARAM_NAME_MAX ||
935
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
272 memchr(wire->unit, '\0', sizeof(wire->unit)) == NULL || data[size - 1] != 0U) {
936 ✗ return -EBADMSG;
937 }
938 136 return 0;
939 }
940
941 /* Add one validated descriptor to the cache and the shared list.
942 */
943 4 static int stage_remote_descriptor(uint16_t node, param_transfer3_t *wire, const uint8_t *data,
944 int64_t deadline)
945 {
946 const param_t *param;
947
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (cached_count >= ARRAY_SIZE(cached)) {
949 ✗ return -ENOSPC;
950 }
951
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (kfsw_param_table_lock_until(deadline) != 0) {
952 ✗ return -ETIMEDOUT;
953 }
954
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (cached_node != node) {
955 4 clear_remote_cache();
956 4 cached_node = node;
957 }
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 for (size_t i = 0; i < cached_count; i++) {
959
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if ((cached[i]->id == sys_get_be16(data)) ||
960 ✗ (strcmp(cached[i]->name, wire->name) == 0)) {
961 ✗ kfsw_param_table_unlock();
962 ✗ return -EEXIST;
963 }
964 }
965 4 param = param_list_create_remote(sys_get_be16(data), node, wire->type,
966 4 sys_get_be32((uint8_t *)&wire->mask) | PM_REMOTE,
967 4 wire->size, wire->name, wire->unit, wire->help,
968 4 wire->storage_type);
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (param == NULL) {
970 ✗ kfsw_param_table_unlock();
971 ✗ return -ENOSPC;
972 }
973 4 cached[cached_count++] = param;
974 4 (void)param_list_add((param_t *)param);
975 4 cached_linked = cached_count;
976 4 kfsw_param_table_unlock();
977 4 return 0;
978 }
979
980 /* table zero asks for the whole node, anything else for that table alone. The
981 * cache is only complete when the whole node was asked for.
982 */
983 70 static int refresh_table(uint16_t node, uint8_t table, int64_t deadline, bool force, bool resend)
984 {
985 70 uint32_t crc = 0;
986 70 uint32_t received_crc = 0;
987 70 uint16_t total = 0;
988 70 int result = validate_remote_node(node);
989
990
2/4
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
70 if (result != 0 || node == 0U) {
991
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return result != 0 ? result : -EINVAL;
992 }
993
6/8
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
70 if (!force && table == 0U && cache_complete && cached_node == node) {
994 ✗ return 0;
995 }
996
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
70 if (kfsw_param_table_lock_until(deadline) != 0) {
997 ✗ return -ETIMEDOUT;
998 }
999 70 clear_remote_cache();
1000 70 cached_node = node;
1001 70 kfsw_param_table_unlock();
1002
1003
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 for (uint16_t index = 0; index <= CONFIG_KFSW_PARAM_REMOTE_POOL_SIZE; index++) {
1004 200 uint32_t remaining = remaining_ms(deadline);
1005 csp_conn_t *connection;
1006 csp_packet_t *packet;
1007
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (remaining == 0U) {
1009 ✗ result = -ETIMEDOUT;
1010 ✗ break;
1011 }
1012 200 connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_PARAM_LIST_PORT, 0,
1013 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
1014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (connection == NULL) {
1015 ✗ result = -ECONNREFUSED;
1016 ✗ break;
1017 }
1018 200 packet = csp_buffer_get(KFSW_PARAM_LIST_TABLE_REQUEST_SIZE);
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (packet == NULL) {
1020 ✗ csp_close(connection);
1021 ✗ result = -ENOMEM;
1022 ✗ break;
1023 }
1024
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (table == 0U) {
1025 200 packet->data[0] = KFSW_PARAM_LIST_INDEXED_VERSION;
1026 200 sys_put_be16(index, &packet->data[1]);
1027 200 sys_put_be32(crc, &packet->data[3]);
1028 200 packet->length = KFSW_PARAM_LIST_REQUEST_SIZE;
1029 } else {
1030 ✗ packet->data[0] = KFSW_PARAM_LIST_TABLE_VERSION;
1031 ✗ packet->data[1] = table;
1032 ✗ sys_put_be16(index, &packet->data[2]);
1033 ✗ sys_put_be32(crc, &packet->data[4]);
1034 ✗ packet->length = KFSW_PARAM_LIST_TABLE_REQUEST_SIZE;
1035 }
1036 200 count_read_resend(resend);
1037 200 csp_send(connection, packet);
1038 200 remaining = remaining_ms(deadline);
1039
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 packet = remaining == 0U ? NULL : csp_read(connection, remaining);
1040 200 csp_close(connection);
1041
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 194 times.
200 if (packet == NULL) {
1042 6 result = -ETIMEDOUT;
1043 6 break;
1044 }
1045 194 result = -EBADMSG;
1046
2/4
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 194 times.
388 if (packet->length < KFSW_PARAM_LIST_REPLY_HEADER ||
1047 194 packet->data[0] != (table == 0U ? KFSW_PARAM_LIST_INDEXED_VERSION
1048
1/2
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
194 : KFSW_PARAM_LIST_TABLE_VERSION)) {
1049 ✗ csp_buffer_free(packet);
1050 ✗ break;
1051 }
1052 194 uint16_t offered = sys_get_be16(&packet->data[4]);
1053 194 uint32_t fingerprint = sys_get_be32(&packet->data[6]);
1054 194 uint8_t status = packet->data[1];
1055
1056
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 126 times.
194 if (index == 0U) {
1057 68 total = offered;
1058 68 crc = fingerprint;
1059 }
1060
4/6
✓ Branch 1 taken 194 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 190 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 190 times.
✗ Branch 6 not taken.
194 if (sys_get_be16(&packet->data[2]) != index || offered != total ||
1061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 fingerprint != crc || total > CONFIG_KFSW_PARAM_REMOTE_POOL_SIZE) {
1062 4 csp_buffer_free(packet);
1063 4 break;
1064 }
1065
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
190 if (status == KFSW_PARAM_LIST_END && index == total &&
1066
3/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 4 times.
60 packet->length == KFSW_PARAM_LIST_REPLY_HEADER && received_crc == crc) {
1067 56 csp_buffer_free(packet);
1068
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if (kfsw_param_table_lock_until(deadline) != 0) {
1069 ✗ result = -ETIMEDOUT;
1070 ✗ break;
1071 }
1072
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 56 times.
166 for (size_t i = 0; i < cached_count; i++) {
1073 /* Node and ID/name uniqueness were checked before allocation. */
1074 110 (void)param_list_add((param_t *)cached[i]);
1075 }
1076 56 cached_linked = cached_count;
1077 56 cache_complete = table == 0U;
1078 56 kfsw_param_table_unlock();
1079 56 return 0;
1080 }
1081 134 uint8_t *data = &packet->data[KFSW_PARAM_LIST_REPLY_HEADER];
1082 134 size_t size = packet->length - KFSW_PARAM_LIST_REPLY_HEADER;
1083
1084
4/6
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 130 times.
264 if (status != KFSW_PARAM_LIST_ITEM || index >= total ||
1085 130 validate_descriptor(data, size) != 0) {
1086 4 csp_buffer_free(packet);
1087 4 break;
1088 }
1089 130 received_crc = crc32_ieee_update(received_crc, data, size);
1090 130 param_transfer3_t *wire = (param_transfer3_t *)data;
1091 130 bool duplicate = false;
1092
1093
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 130 times.
192 for (size_t i = 0; i < cached_count; i++) {
1094
2/4
✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 62 times.
124 duplicate |= cached[i]->id == sys_get_be16(data) ||
1095 62 strcmp(cached[i]->name, wire->name) == 0;
1096 }
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (duplicate) {
1098 ✗ result = -EBADMSG;
1099 } else {
1100 130 memset(data + size, 0, sizeof(*wire) - size);
1101 130 const param_t *param = param_list_create_remote(
1102 130 sys_get_be16(data), node, wire->type,
1103 130 sys_get_be32((uint8_t *)&wire->mask) | PM_REMOTE, wire->size,
1104 130 wire->name, wire->unit, wire->help, wire->storage_type);
1105
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 result = param == NULL ? -ENOSPC : 0;
1107
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (param != NULL) {
1108 130 cached[cached_count++] = param;
1109 }
1110 }
1111 130 csp_buffer_free(packet);
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (result != 0) {
1113 ✗ break;
1114 }
1115 }
1116 /* Staged descriptors were never linked into the shared table. */
1117 14 clear_remote_cache();
1118
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 return result == 0 ? -EBADMSG : result;
1119 }
1120
1121 ✗ int kfsw_param_remote_tables(uint16_t node, kfsw_param_table_visitor_t visitor, void *context)
1122 {
1123 ✗ int64_t deadline = k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS;
1124 ✗ uint8_t from = 0U;
1125 ✗ int result = validate_remote_node(node);
1126
1127
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (result != 0 || visitor == NULL) {
1128
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return result != 0 ? result : -EINVAL;
1129 }
1130
1131 /* One reply holds a handful of tables, so ask again from where it stopped
1132 * until the node says there are no more.
1133 */
1134 ✗ for (;;) {
1135 csp_conn_t *connection;
1136 csp_packet_t *packet;
1137 ✗ uint32_t remaining = remaining_ms(deadline);
1138 uint8_t count;
1139 bool more;
1140 size_t at;
1141
1142
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (remaining == 0U) {
1143 ✗ return -ETIMEDOUT;
1144 }
1145 ✗ connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_PARAM_LIST_PORT, 0,
1146 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
1147
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
1148 ✗ return -ECONNREFUSED;
1149 }
1150 ✗ packet = csp_buffer_get(2U);
1151
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
1152 ✗ csp_close(connection);
1153 ✗ return -ENOMEM;
1154 }
1155 ✗ packet->data[0] = KFSW_PARAM_TABLES_VERSION;
1156 ✗ packet->data[1] = from;
1157 ✗ packet->length = 2U;
1158 ✗ csp_send(connection, packet);
1159
1160 ✗ remaining = remaining_ms(deadline);
1161
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ packet = remaining == 0U ? NULL : csp_read(connection, remaining);
1162 ✗ csp_close(connection);
1163
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
1164 /* An older node says nothing rather than refusing. */
1165
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return from == 0U ? -ENOTSUP : -ETIMEDOUT;
1166 }
1167
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet->length < KFSW_PARAM_TABLES_HEADER ||
1168
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ packet->data[0] != KFSW_PARAM_TABLES_VERSION) {
1169 ✗ csp_buffer_free(packet);
1170 ✗ return -EBADMSG;
1171 }
1172 ✗ count = packet->data[1];
1173 ✗ more = packet->data[2] != 0U;
1174 ✗ at = KFSW_PARAM_TABLES_HEADER;
1175
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (uint8_t i = 0; i < count; i++) {
1176 struct kfsw_param_table_info info;
1177 char name[KFSW_PARAM_NAME_MAX + 1U];
1178 char description[KFSW_PARAM_DESCRIPTION_MAX + 1U];
1179 size_t name_length;
1180 size_t description_length;
1181
1182
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (at + KFSW_PARAM_TABLES_ENTRY_HEADER > packet->length) {
1183 ✗ csp_buffer_free(packet);
1184 ✗ return -EBADMSG;
1185 }
1186 ✗ name_length = packet->data[at + 3U];
1187 ✗ description_length = packet->data[at + 4U];
1188
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((name_length > KFSW_PARAM_NAME_MAX) ||
1189 ✗ (description_length >= sizeof(description)) ||
1190 ✗ (at + KFSW_PARAM_TABLES_ENTRY_HEADER + name_length +
1191 description_length >
1192
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ packet->length)) {
1193 ✗ csp_buffer_free(packet);
1194 ✗ return -EBADMSG;
1195 }
1196 ✗ memcpy(name, &packet->data[at + KFSW_PARAM_TABLES_ENTRY_HEADER],
1197 name_length);
1198 ✗ name[name_length] = '\0';
1199 ✗ memcpy(description,
1200 ✗ &packet->data[at + KFSW_PARAM_TABLES_ENTRY_HEADER + name_length],
1201 description_length);
1202 ✗ description[description_length] = '\0';
1203 ✗ info.id = packet->data[at];
1204 ✗ info.count = sys_get_be16(&packet->data[at + 1U]);
1205 ✗ info.name = name;
1206 ✗ info.description = description;
1207 ✗ at += KFSW_PARAM_TABLES_ENTRY_HEADER + name_length + description_length;
1208
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (!visitor(&info, context)) {
1209 ✗ csp_buffer_free(packet);
1210 ✗ return 0;
1211 }
1212 }
1213 ✗ csp_buffer_free(packet);
1214
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!more) {
1215 ✗ return 0;
1216 }
1217
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (count == 0U) {
1218 /* No progress and more to come would loop forever. */
1219 ✗ return -EBADMSG;
1220 }
1221 ✗ from += count;
1222 }
1223 }
1224
1225 64 int kfsw_param_remote_refresh(uint16_t node)
1226 {
1227 64 int64_t deadline = k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS;
1228
1/2
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
64 int result = k_mutex_lock(&kfsw_param_remote_lock, K_MSEC(remaining_ms(deadline)));
1229
1230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (result != 0) {
1231 ✗ return -ETIMEDOUT;
1232 }
1233 64 result = refresh_table(node, 0U, deadline, true, false);
1234 64 k_mutex_unlock(&kfsw_param_remote_lock);
1235 64 return result;
1236 }
1237
1238 1246 static int decode_value(mpack_reader_t *reader, const param_t *param,
1239 struct kfsw_param_value *value)
1240 {
1241 1246 memset(value, 0, sizeof(*value));
1242 1246 value->type = from_libparam_type(param->type);
1243 1246 value->size = scalar_size(value->type);
1244
3/4
✓ Branch 0 taken 1054 times.
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1054 times.
1246 if (value->size != 0U && param->array_size != 1U) {
1245 ✗ return -ENOTSUP;
1246 }
1247
6/13
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 874 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 72 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 96 times.
✓ Branch 11 taken 96 times.
✗ Branch 12 not taken.
1246 switch (value->type) {
1248 84 case KFSW_PARAM_U8:
1249 case KFSW_PARAM_X8:
1250 84 value->scalar.u8 = mpack_expect_u8(reader);
1251 84 break;
1252 ✗ case KFSW_PARAM_U16:
1253 case KFSW_PARAM_X16:
1254 ✗ value->scalar.u16 = mpack_expect_u16(reader);
1255 ✗ break;
1256 874 case KFSW_PARAM_U32:
1257 case KFSW_PARAM_X32:
1258 874 value->scalar.u32 = mpack_expect_u32(reader);
1259 874 break;
1260 ✗ case KFSW_PARAM_U64:
1261 case KFSW_PARAM_X64:
1262 ✗ value->scalar.u64 = mpack_expect_u64(reader);
1263 ✗ break;
1264 24 case KFSW_PARAM_I8:
1265 24 value->scalar.i8 = mpack_expect_i8(reader);
1266 24 break;
1267 ✗ case KFSW_PARAM_I16:
1268 ✗ value->scalar.i16 = mpack_expect_i16(reader);
1269 ✗ break;
1270 ✗ case KFSW_PARAM_I32:
1271 ✗ value->scalar.i32 = mpack_expect_i32(reader);
1272 ✗ break;
1273 ✗ case KFSW_PARAM_I64:
1274 ✗ value->scalar.i64 = mpack_expect_i64(reader);
1275 ✗ break;
1276 72 case KFSW_PARAM_FLOAT:
1277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 if (mpack_peek_tag(reader).type != mpack_type_float) {
1278 ✗ return -EBADMSG;
1279 }
1280 72 value->scalar.f32 = mpack_expect_float(reader);
1281 72 break;
1282 ✗ case KFSW_PARAM_DOUBLE:
1283
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (mpack_peek_tag(reader).type != mpack_type_double) {
1284 ✗ return -EBADMSG;
1285 }
1286 ✗ value->scalar.f64 = mpack_expect_double(reader);
1287 ✗ break;
1288 96 case KFSW_PARAM_STRING: {
1289 96 size_t size = mpack_expect_str(reader);
1290
1291
3/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72 times.
96 if (size >= param->array_size || size >= sizeof(value->text)) {
1292 24 return -EBADMSG;
1293 }
1294 72 mpack_read_bytes(reader, value->text, size);
1295 72 mpack_done_str(reader);
1296
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 48 times.
72 if (memchr(value->text, 0, size) != NULL) {
1297 24 return -EBADMSG;
1298 }
1299 48 value->size = size + 1;
1300 48 break;
1301 }
1302 96 case KFSW_PARAM_DATA: {
1303 96 size_t size = mpack_expect_bin(reader);
1304
1305
3/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72 times.
96 if (size != param->array_size || size > sizeof(value->bytes)) {
1306 24 return -EBADMSG;
1307 }
1308 72 mpack_read_bytes(reader, (char *)value->bytes, size);
1309 72 mpack_done_bin(reader);
1310 72 value->size = size;
1311 72 break;
1312 }
1313 ✗ default:
1314 ✗ return -ENOTSUP;
1315 }
1316
2/2
✓ Branch 1 taken 1064 times.
✓ Branch 2 taken 110 times.
1174 return mpack_reader_error(reader) == mpack_ok ? 0 : -EBADMSG;
1317 }
1318
1319 108 static void store_remote_value(const param_t *param, const struct kfsw_param_value *value)
1320 {
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (value->type == KFSW_PARAM_STRING) {
1322 ✗ param_set_string(param, value->text, value->size);
1323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 } else if (value->type == KFSW_PARAM_DATA) {
1324 ✗ param_set_data(param, value->bytes, value->size);
1325 } else {
1326 108 param_set(param, 0, (void *)&value->scalar);
1327 }
1328 108 }
1329
1330 88 static int decode_reply(uint16_t node, csp_packet_t *packet, const param_t *const *params,
1331 size_t count, struct kfsw_param_value *values, bool *seen)
1332 {
1333 88 param_queue_t queue = {0};
1334 mpack_reader_t reader;
1335
1336 88 param_queue_init(&queue, &packet->data[2], packet->length - 2, packet->length - 2,
1337 PARAM_QUEUE_TYPE_SET, KFSW_PARAM_PROTOCOL_VERSION);
1338 88 mpack_reader_init_data(&reader, queue.buffer, queue.used);
1339
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 70 times.
208 while (reader.data < reader.end) {
1340 struct kfsw_param_value value;
1341 138 csp_timestamp_t timestamp = {0};
1342 138 int offset = -1, id = 0, source = 0;
1343 /* Bit 10 is reserved. Check it before using the pinned ID decoder. */
1344 138 mpack_reader_t peek = reader;
1345 138 uint16_t header = be16toh(mpack_expect_u16(&peek));
1346
1347
2/4
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
138 if (mpack_reader_error(&peek) != mpack_ok || (header & BIT(10)) != 0U ||
1348
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
138 ((header & BIT(11)) != 0U && (header & BIT(13)) == 0U)) {
1349 18 return -EBADMSG;
1350 }
1351 138 param_deserialize_id(&reader, &id, &source, &timestamp, &offset, &queue);
1352
3/6
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 138 times.
✗ Branch 6 not taken.
138 if (mpack_reader_error(&reader) != mpack_ok || offset > 0 || offset < -1 ||
1353
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
138 (source != 0 && source != node)) {
1354 ✗ return -EBADMSG;
1355 }
1356 size_t index;
1357
1358
4/4
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 134 times.
190 for (index = 0; index < count && params[index]->id != id; index++) {
1359 }
1360
4/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 124 times.
138 if (index == count || decode_value(&reader, params[index], &value) != 0) {
1361 14 return -EBADMSG;
1362 }
1363
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 120 times.
428 for (; index < count; index++) {
1364
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 180 times.
308 if (params[index]->id != id) {
1365 128 continue;
1366 }
1367 /* codechecker_false_positive [bugprone-suspicious-memory-comparison]
1368 * decode_value() zeroes the whole value first.
1369 */
1370
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 156 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 20 times.
180 if (seen[index] && memcmp(&values[index], &value, sizeof(value)) != 0) {
1371 4 return -EBADMSG;
1372 }
1373 176 values[index] = value;
1374 176 seen[index] = true;
1375 }
1376 }
1377
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 return mpack_reader_error(&reader) == mpack_ok ? 0 : -EBADMSG;
1378 }
1379
1380 100 static int pull_remote_batch(uint16_t node, const param_t *const *params, size_t count,
1381 size_t *consumed, int64_t deadline, bool resend)
1382 {
1383 static struct kfsw_param_value staged[KFSW_PARAM_REMOTE_BATCH_MAX];
1384 100 bool seen[KFSW_PARAM_REMOTE_BATCH_MAX] = {0};
1385 100 bool end_seen = false;
1386 csp_conn_t *connection;
1387 csp_packet_t *packet;
1388 100 param_queue_t queue = {0};
1389 100 size_t added = 0;
1390 100 int result = -ETIMEDOUT;
1391
1392 100 *consumed = 0;
1393 100 packet = csp_buffer_get(PARAM_SERVER_MTU);
1394
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 98 times.
100 if (packet == NULL) {
1395 2 return -ENOMEM;
1396 }
1397 98 packet->data[0] = PARAM_PULL_REQUEST_V2;
1398 98 packet->data[1] = 0;
1399 98 param_queue_init(&queue, &packet->data[2], PARAM_SERVER_MTU - 2, 0, PARAM_QUEUE_TYPE_GET,
1400 KFSW_PARAM_PROTOCOL_VERSION);
1401
3/4
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 244 times.
✗ Branch 3 not taken.
342 while (added < count && added < ARRAY_SIZE(staged)) {
1402
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
244 if (param_queue_add(&queue, params[added], -1, NULL) != 0) {
1403 ✗ break;
1404 }
1405 244 added++;
1406 }
1407
2/4
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
98 if (added == 0 || remaining_ms(deadline) == 0) {
1408 ✗ csp_buffer_free(packet);
1409
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return added == 0 ? -EMSGSIZE : -ETIMEDOUT;
1410 }
1411 98 packet->length = queue.used + 2;
1412 98 connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_PARAM_PORT, 0,
1413 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
1414
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 94 times.
98 if (connection == NULL) {
1415 4 csp_buffer_free(packet);
1416 4 return -ECONNREFUSED;
1417 }
1418 94 count_read_resend(resend);
1419 94 csp_send(connection, packet);
1420
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 for (size_t packets = 0; packets < KFSW_PARAM_REPLY_MAX_PACKETS; packets++) {
1421
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 102 times.
128 uint32_t remaining = MIN(remaining_ms(deadline), CONFIG_KFSW_PARAM_TIMEOUT_MS);
1422
1423
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 4 times.
128 packet = remaining == 0 ? NULL : csp_read(connection, remaining);
1424
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 90 times.
128 if (packet == NULL) {
1425 38 result = -ETIMEDOUT;
1426 38 break;
1427 }
1428
3/4
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 2 times.
90 if (packet->length < 2U || packet->data[0] != PARAM_PULL_RESPONSE_V2 ||
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 (packet->data[1] & ~PARAM_FLAG_END) != 0U) {
1430 2 csp_buffer_free(packet);
1431 2 result = -EBADMSG;
1432 2 break;
1433 }
1434 88 result = decode_reply(node, packet, params, added, staged, seen);
1435 88 end_seen = (packet->data[1] & PARAM_FLAG_END) != 0U;
1436 88 csp_buffer_free(packet);
1437
4/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 36 times.
88 if (result != 0 || end_seen) {
1438 break;
1439 }
1440 }
1441 94 csp_close(connection);
1442
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
94 if (result == 0 && !end_seen) {
1443 ✗ result = -ETIMEDOUT;
1444 }
1445
4/4
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 118 times.
✓ Branch 3 taken 30 times.
212 for (size_t i = 0; result == 0 && i < added; i++) {
1446
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 112 times.
118 if (!seen[i]) {
1447 6 result = -EBADMSG;
1448 }
1449 }
1450
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 30 times.
94 if (result != 0) {
1451 64 return result;
1452 }
1453
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (kfsw_param_table_lock_until(deadline) != 0) {
1454 ✗ return -ETIMEDOUT;
1455 }
1456
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 30 times.
138 for (size_t i = 0; i < added; i++) {
1457 108 store_remote_value(params[i], &staged[i]);
1458 }
1459 30 kfsw_param_table_unlock();
1460 30 *consumed = added;
1461 30 return 0;
1462 }
1463
1464 2 static int push_remote(const param_t *param, uint16_t node, const struct kfsw_param_value *value,
1465 int64_t deadline)
1466 {
1467 csp_conn_t *connection;
1468 csp_packet_t *packet;
1469 param_queue_t queue;
1470 2 int result = -ETIMEDOUT;
1471
1472 2 packet = csp_buffer_get(PARAM_SERVER_MTU);
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (packet == NULL) {
1474 ✗ return -ENOMEM;
1475 }
1476 2 packet->data[0] = PARAM_PUSH_REQUEST_V2;
1477 2 packet->data[1] = 0U;
1478 2 param_queue_init(&queue, &packet->data[2], PARAM_SERVER_MTU - 2, 0, PARAM_QUEUE_TYPE_SET,
1479 KFSW_PARAM_PROTOCOL_VERSION);
1480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (param_queue_add(&queue, param, -1,
1481
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 (value->type == KFSW_PARAM_STRING || value->type == KFSW_PARAM_DATA)
1482 ? (void *)value->bytes
1483 : (void *)&value->scalar) != 0) {
1484 ✗ csp_buffer_free(packet);
1485 ✗ return -EMSGSIZE;
1486 }
1487 2 packet->length = queue.used + 2U;
1488
1489 2 connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_PARAM_PORT, 0,
1490 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
1491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (connection == NULL) {
1492 ✗ csp_buffer_free(packet);
1493 ✗ return -ECONNREFUSED;
1494 }
1495 2 csp_send(connection, packet);
1496
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 uint32_t remaining = MIN(remaining_ms(deadline), CONFIG_KFSW_PARAM_TIMEOUT_MS);
1497
1498
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 packet = remaining == 0 ? NULL : csp_read(connection, remaining);
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (packet != NULL) {
1500
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((packet->length >= 2U) && (packet->data[0] == PARAM_PUSH_RESPONSE) &&
1501
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ ((packet->data[1] & PARAM_FLAG_END) != 0U)) {
1502 ✗ result = 0;
1503 } else {
1504 ✗ result = -EBADMSG;
1505 }
1506 ✗ csp_buffer_free(packet);
1507 }
1508 2 (void)csp_close(connection);
1509 2 return result;
1510 }
1511
1512 /* Callers hold the remote lock until names, reads and callbacks finish. */
1513 /* Fetch one descriptor.
1514 *
1515 * Returns -ENOTSUP when the node doesn't answer the lookup, so the caller can
1516 * fall back to the list.
1517 */
1518 18 static int lookup_remote(uint16_t node, const char *name, int64_t deadline, bool resend)
1519 {
1520 18 size_t length = strlen(name);
1521 csp_conn_t *connection;
1522 csp_packet_t *packet;
1523 uint32_t remaining;
1524 int result;
1525
1526
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if ((length == 0U) || (length > KFSW_PARAM_NAME_MAX)) {
1527 ✗ return -EINVAL;
1528 }
1529 18 result = validate_remote_node(node);
1530
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
18 if ((result != 0) || (node == 0U)) {
1531
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return (result != 0) ? result : -EINVAL;
1532 }
1533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (cached_count >= ARRAY_SIZE(cached)) {
1534 ✗ return -ENOSPC;
1535 }
1536
1537 14 remaining = remaining_ms(deadline);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (remaining == 0U) {
1539 ✗ return -ETIMEDOUT;
1540 }
1541 14 connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_PARAM_LIST_PORT, 0,
1542 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
1543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (connection == NULL) {
1544 ✗ return -ECONNREFUSED;
1545 }
1546 14 packet = csp_buffer_get(KFSW_PARAM_LIST_LOOKUP_HEADER + KFSW_PARAM_NAME_MAX);
1547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (packet == NULL) {
1548 ✗ csp_close(connection);
1549 ✗ return -ENOMEM;
1550 }
1551 14 packet->data[0] = KFSW_PARAM_LIST_LOOKUP_VERSION;
1552 14 memcpy(&packet->data[1], name, length);
1553 14 packet->length = (uint16_t)(1U + length);
1554 14 count_read_resend(resend);
1555 14 csp_send(connection, packet);
1556
1557 /* Wait one transaction timeout, not the whole budget, so the fallback still has time. */
1558
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 remaining = MIN(remaining_ms(deadline), (uint32_t)CONFIG_KFSW_PARAM_TIMEOUT_MS);
1559
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 packet = (remaining == 0U) ? NULL : csp_read(connection, remaining);
1560 14 csp_close(connection);
1561
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 if (packet == NULL) {
1562 /* No answer: an older node without lookup support. */
1563 6 return -ENOTSUP;
1564 }
1565
1566 8 result = -EBADMSG;
1567
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if ((packet->length >= KFSW_PARAM_LIST_LOOKUP_HEADER) &&
1568
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 (packet->data[0] == KFSW_PARAM_LIST_LOOKUP_VERSION)) {
1569
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (packet->data[1] == KFSW_PARAM_LIST_END) {
1570 2 result = -ENOENT;
1571
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 } else if (packet->data[1] == KFSW_PARAM_LIST_ITEM) {
1572 6 uint8_t *data = &packet->data[KFSW_PARAM_LIST_LOOKUP_HEADER];
1573 6 size_t size = packet->length - KFSW_PARAM_LIST_LOOKUP_HEADER;
1574
1575 6 result = validate_descriptor(data, size);
1576
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (result == 0) {
1577 6 param_transfer3_t *wire = (param_transfer3_t *)data;
1578
1579
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if (strcmp(wire->name, name) != 0) {
1580 2 result = -EBADMSG;
1581 } else {
1582 4 memset(data + size, 0, sizeof(*wire) - size);
1583 result =
1584 4 stage_remote_descriptor(node, wire, data, deadline);
1585 }
1586 }
1587 }
1588 }
1589 8 csp_buffer_free(packet);
1590 8 return result;
1591 }
1592
1593 262 static int find_remote(uint16_t node, const char *name, const param_t **found, int64_t deadline,
1594 bool resend)
1595 {
1596 int result;
1597
1598 /* A descriptor already held answers without a packet at all. */
1599
2/2
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 18 times.
262 if (cached_node == node) {
1600
2/2
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 2 times.
368 for (size_t i = 0; i < cached_count; i++) {
1601
2/2
✓ Branch 1 taken 242 times.
✓ Branch 2 taken 124 times.
366 if (strcmp(cached[i]->name, name) == 0) {
1602 242 *found = cached[i];
1603 242 return 0;
1604 }
1605 }
1606
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (cache_complete) {
1607 2 return -ENOENT;
1608 }
1609 }
1610
1611 18 result = lookup_remote(node, name, deadline, resend);
1612
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
18 if (result == 0) {
1613 4 *found = cached[cached_count - 1U];
1614 4 return 0;
1615 }
1616
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 if (result != -ENOTSUP) {
1617 8 return result;
1618 }
1619
1620 /* The node does not serve lookups. Walk its list, as before. */
1621 6 result = refresh_table(node, 0U, deadline, false, resend);
1622
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (result != 0) {
1623 2 return result;
1624 }
1625
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (size_t i = 0; i < cached_count; i++) {
1626
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (strcmp(cached[i]->name, name) == 0) {
1627 4 *found = cached[i];
1628 4 return 0;
1629 }
1630 }
1631 ✗ return -ENOENT;
1632 }
1633
1634 108 static int get_many(uint16_t node, const char *const *names, size_t count,
1635 struct kfsw_param_value *values, int64_t deadline, bool resend)
1636 {
1637 108 size_t done = 0U;
1638
1639
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
108 if ((names == NULL) || (values == NULL)) {
1640 ✗ return -EINVAL;
1641 }
1642
1643
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 26 times.
138 while (done < count) {
1644 const param_t *window[KFSW_PARAM_REMOTE_BATCH_MAX];
1645 112 size_t window_count = MIN(count - done, ARRAY_SIZE(window));
1646 112 size_t pulled = 0U;
1647 size_t index;
1648 112 int result = 0;
1649
1650 /* Resolve every name before requesting anything. */
1651
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 100 times.
360 for (index = 0U; index < window_count; index++) {
1652 520 result = names[done + index] == NULL
1653 ? -EINVAL
1654
1/2
✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
260 : find_remote(node, names[done + index], &window[index],
1655 deadline, resend);
1656
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 248 times.
260 if (result != 0) {
1657 82 return result;
1658 }
1659 }
1660
1661
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 30 times.
130 while (pulled < window_count) {
1662 100 size_t consumed = 0U;
1663
1664 100 result = pull_remote_batch(node, &window[pulled], window_count - pulled,
1665 &consumed, deadline, resend);
1666
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 30 times.
100 if (result != 0) {
1667 70 return result;
1668 }
1669 30 pulled += consumed;
1670 }
1671
1672
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (kfsw_param_table_lock_until(deadline) != 0) {
1673 ✗ return -ETIMEDOUT;
1674 }
1675
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 30 times.
138 for (index = 0U; index < window_count; index++) {
1676 108 result = read_scalar(window[index], &values[done + index]);
1677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (result != 0) {
1678 ✗ break;
1679 }
1680 }
1681 30 kfsw_param_table_unlock();
1682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (result != 0) {
1683 ✗ return result;
1684 }
1685 30 done += window_count;
1686 }
1687 26 return 0;
1688 }
1689
1690 2 static int set_remote(uint16_t node, const char *name, const struct kfsw_param_value *value,
1691 int64_t deadline)
1692 {
1693 const param_t *param;
1694 int result;
1695
1696
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((name == NULL) || (value == NULL)) {
1697 ✗ return -EINVAL;
1698 }
1699 2 result = find_remote(node, name, &param, deadline, false);
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
1701 ✗ return result;
1702 }
1703
1704
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (kfsw_param_table_lock_until(deadline) != 0) {
1705 ✗ return -ETIMEDOUT;
1706 }
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (param == NULL) {
1708 ✗ result = -ENOENT;
1709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if ((param->mask & PM_READONLY) != 0U) {
1710 ✗ result = -EACCES;
1711 } else {
1712 2 result = validate_scalar(param, value);
1713 }
1714 2 kfsw_param_table_unlock();
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
1716 ✗ return result;
1717 }
1718
1719 2 result = push_remote(param, node, value, deadline);
1720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result == 0) {
1721
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_param_table_lock_until(deadline) != 0) {
1722 ✗ return -ETIMEDOUT;
1723 }
1724
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (value->type == KFSW_PARAM_STRING) {
1725 ✗ param_set_string(param, value->text, (int)value->size);
1726
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (value->type == KFSW_PARAM_DATA) {
1727 ✗ param_set_data(param, value->bytes, (int)value->size);
1728 } else {
1729 ✗ param_set(param, 0U, (void *)&value->scalar);
1730 }
1731 ✗ kfsw_param_table_unlock();
1732 }
1733 2 return result;
1734 }
1735
1736 ✗ static int visit_remote(uint16_t node, uint8_t table, kfsw_param_visitor_t visitor, void *context,
1737 int64_t deadline)
1738 {
1739 struct kfsw_param_info info;
1740 ✗ uint16_t previous_id = 0U;
1741 ✗ bool emitted = false;
1742 int result;
1743
1744
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (visitor == NULL) {
1745 ✗ return -EINVAL;
1746 }
1747 ✗ result = refresh_table(node, table, deadline, table != 0U, false);
1748
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != 0) {
1749 ✗ return result;
1750 }
1751
1752 /* Print in ID order. The cache is in registration order, so pick the next
1753 * smallest ID on each pass.
1754 */
1755
1756 ✗ for (;;) {
1757
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (remaining_ms(deadline) == 0) {
1758 ✗ return -ETIMEDOUT;
1759 }
1760 ✗ const param_t *next = NULL;
1761
1762
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (size_t i = 0; i < cached_count; i++) {
1763 ✗ const param_t *param = cached[i];
1764
1765
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (emitted && param->id <= previous_id) {
1766 ✗ continue;
1767 }
1768
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (next == NULL || param->id < next->id) {
1769 ✗ next = param;
1770 }
1771 }
1772
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (next == NULL) {
1773 ✗ break;
1774 }
1775
1776 ✗ previous_id = next->id;
1777 ✗ emitted = true;
1778 ✗ fill_info(next, &info);
1779
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (!visitor(&info, context)) {
1780 ✗ break;
1781 }
1782 }
1783
1784 ✗ return 0;
1785 }
1786
1787 88 int kfsw_param_remote_get_many_until(uint16_t node, const char *const *names, size_t count,
1788 struct kfsw_param_value *values, int64_t deadline)
1789 {
1790
4/4
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 80 times.
174 if (remaining_ms(deadline) == 0 ||
1791
1/2
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
86 k_mutex_lock(&kfsw_param_remote_lock, K_MSEC(remaining_ms(deadline))) != 0) {
1792 8 return -ETIMEDOUT;
1793 }
1794 80 int result = -ETIMEDOUT;
1795
1796
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 6 times.
120 for (unsigned int attempt = 0U; attempt < KFSW_PARAM_READ_MAX_ATTEMPTS; attempt++) {
1797
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 108 times.
114 if (remaining_ms(deadline) == 0U) {
1798 6 break;
1799 }
1800 108 result = get_many(node, names, count, values, deadline, attempt != 0U);
1801
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 40 times.
108 if (result != -ETIMEDOUT) {
1802 68 break;
1803 }
1804 }
1805
1806 80 k_mutex_unlock(&kfsw_param_remote_lock);
1807 80 return result;
1808 }
1809
1810 54 int kfsw_param_remote_get_many(uint16_t node, const char *const *names, size_t count,
1811 struct kfsw_param_value *values)
1812 {
1813 54 return kfsw_param_remote_get_many_until(node, names, count, values,
1814 54 k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS +
1815 CONFIG_KFSW_PARAM_TIMEOUT_MS);
1816 }
1817
1818 18 int kfsw_param_remote_get(uint16_t node, const char *name, struct kfsw_param_value *value)
1819 {
1820 18 return kfsw_param_remote_get_many(node, &name, 1, value);
1821 }
1822
1823 2 int kfsw_param_remote_set(uint16_t node, const char *name, const struct kfsw_param_value *value)
1824 {
1825 2 int64_t deadline =
1826 2 k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS + CONFIG_KFSW_PARAM_TIMEOUT_MS;
1827
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
2 if (k_mutex_lock(&kfsw_param_remote_lock, K_MSEC(remaining_ms(deadline))) != 0) {
1828 ✗ return -ETIMEDOUT;
1829 }
1830 2 int result = set_remote(node, name, value, deadline);
1831
1832 2 k_mutex_unlock(&kfsw_param_remote_lock);
1833 2 return result;
1834 }
1835
1836 ✗ int kfsw_param_remote_visit_until(uint16_t node, kfsw_param_visitor_t visitor, void *context,
1837 int64_t deadline)
1838 {
1839
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (remaining_ms(deadline) == 0 ||
1840
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ k_mutex_lock(&kfsw_param_remote_lock, K_MSEC(remaining_ms(deadline))) != 0) {
1841 ✗ return -ETIMEDOUT;
1842 }
1843 ✗ int result = visit_remote(node, 0U, visitor, context, deadline);
1844
1845 ✗ k_mutex_unlock(&kfsw_param_remote_lock);
1846 ✗ return result;
1847 }
1848
1849 ✗ int kfsw_param_remote_visit(uint16_t node, kfsw_param_visitor_t visitor, void *context)
1850 {
1851 ✗ return kfsw_param_remote_visit_until(node, visitor, context,
1852 ✗ k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS);
1853 }
1854
1855 ✗ int kfsw_param_remote_visit_table(uint16_t node, uint8_t table, kfsw_param_visitor_t visitor,
1856 void *context)
1857 {
1858 ✗ int64_t deadline = k_uptime_get() + CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS;
1859 int result;
1860
1861
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (table == 0U) {
1862 ✗ return -EINVAL;
1863 }
1864
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ if (k_mutex_lock(&kfsw_param_remote_lock, K_MSEC(remaining_ms(deadline))) != 0) {
1865 ✗ return -ETIMEDOUT;
1866 }
1867 ✗ result = visit_remote(node, table, visitor, context, deadline);
1868 ✗ k_mutex_unlock(&kfsw_param_remote_lock);
1869 ✗ return result;
1870 }
1871