./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
488 / 536
91.0%
Branches:
194 / 254
76.4%
Functions:
49 / 49
100.0%

kfsw-services/src/hk/hk.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/byteorder.h>
8 #include <zephyr/sys/util.h>
9
10 #if CONFIG_KFSW_CSP
11 #include <csp/csp_iflist.h>
12 #include <kfsw/comms/csp.h>
13 #endif
14 #include <kfsw/services/hk.h>
15 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HK
16 #include <kfsw/services/log.h>
17 #include <kfsw/services/parameter.h>
18
19 #include "hk_internal.h"
20
21 static struct kfsw_hk_report reports[CONFIG_KFSW_HK_REPORTS];
22 static struct kfsw_hk_stats stats;
23 static uint64_t collected_order;
24 static bool initialized;
25 static bool enabled = true;
26 static bool restoring;
27 static uint64_t config_revision;
28 #if CONFIG_KFSW_HK_PERSISTENCE
29 static bool save_blocked;
30 #endif
31 static K_MUTEX_DEFINE(config_lock);
32 static K_MUTEX_DEFINE(init_lock);
33
34 K_MUTEX_DEFINE(hk_lock);
35 static K_MUTEX_DEFINE(collection_lock);
36 static K_MUTEX_DEFINE(storage_lock);
37 static uint64_t generations[CONFIG_KFSW_HK_REPORTS];
38 static uint64_t schedule_revisions[CONFIG_KFSW_HK_REPORTS];
39
40 5344 void kfsw_hk_storage_lock(void)
41 {
42 5344 k_mutex_lock(&storage_lock, K_FOREVER);
43 5344 }
44
45 5344 void kfsw_hk_storage_unlock(void)
46 {
47 5344 k_mutex_unlock(&storage_lock);
48 5344 }
49
50 19918 void kfsw_hk_lock(void)
51 {
52 19918 k_mutex_lock(&hk_lock, K_FOREVER);
53 19918 }
54
55 19918 void kfsw_hk_unlock(void)
56 {
57 19918 k_mutex_unlock(&hk_lock);
58 19918 }
59
60 134 void kfsw_hk_count_overwritten(void)
61 {
62 134 stats.overwritten++;
63 134 }
64
65 7392 struct kfsw_hk_report *kfsw_hk_report_at(uint8_t report)
66 {
67
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7386 times.
7392 if (report >= ARRAY_SIZE(reports)) {
68 6 return NULL;
69 }
70 7386 return &reports[report];
71 }
72
73 /*
74 * Wire width of a value, from its declaration, so every sample of a report has
75 * the same layout. Shorter strings are padded.
76 */
77 872 static size_t entry_width(enum kfsw_param_type type, uint16_t array_size)
78 {
79
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 428 times.
✓ Branch 2 taken 434 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
872 switch (type) {
80 2 case KFSW_PARAM_U8:
81 case KFSW_PARAM_I8:
82 case KFSW_PARAM_X8:
83 2 return 1U;
84 428 case KFSW_PARAM_U16:
85 case KFSW_PARAM_I16:
86 case KFSW_PARAM_X16:
87 428 return 2U;
88 434 case KFSW_PARAM_U32:
89 case KFSW_PARAM_I32:
90 case KFSW_PARAM_X32:
91 case KFSW_PARAM_FLOAT:
92 434 return 4U;
93 ✗ case KFSW_PARAM_U64:
94 case KFSW_PARAM_I64:
95 case KFSW_PARAM_X64:
96 case KFSW_PARAM_DOUBLE:
97 ✗ return 8U;
98 8 case KFSW_PARAM_STRING:
99 case KFSW_PARAM_DATA:
100 8 return array_size;
101 ✗ default:
102 ✗ return 0U;
103 }
104 }
105
106 /* Big-endian, like every other K-FSW protocol. */
107 784 void kfsw_hk_write_value(uint8_t *out, size_t width, const struct kfsw_param_value *value)
108 {
109 784 memset(out, 0, width);
110
111
3/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 406 times.
✓ Branch 2 taken 376 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
784 switch (value->type) {
112 2 case KFSW_PARAM_U8:
113 case KFSW_PARAM_I8:
114 case KFSW_PARAM_X8:
115 2 out[0] = value->scalar.u8;
116 2 break;
117 406 case KFSW_PARAM_U16:
118 case KFSW_PARAM_I16:
119 case KFSW_PARAM_X16:
120 406 sys_put_be16(value->scalar.u16, out);
121 406 break;
122 376 case KFSW_PARAM_U32:
123 case KFSW_PARAM_I32:
124 case KFSW_PARAM_X32:
125 376 sys_put_be32(value->scalar.u32, out);
126 376 break;
127 ✗ case KFSW_PARAM_FLOAT: {
128 uint32_t raw;
129
130 ✗ memcpy(&raw, &value->scalar.f32, sizeof(raw));
131 ✗ sys_put_be32(raw, out);
132 ✗ break;
133 }
134 ✗ case KFSW_PARAM_U64:
135 case KFSW_PARAM_I64:
136 case KFSW_PARAM_X64:
137 ✗ sys_put_be64(value->scalar.u64, out);
138 ✗ break;
139 ✗ case KFSW_PARAM_DOUBLE: {
140 uint64_t raw;
141
142 ✗ memcpy(&raw, &value->scalar.f64, sizeof(raw));
143 ✗ sys_put_be64(raw, out);
144 ✗ break;
145 }
146 ✗ case KFSW_PARAM_STRING:
147 case KFSW_PARAM_DATA:
148 ✗ memcpy(out, value->bytes, MIN(width, value->size));
149 ✗ break;
150 ✗ default:
151 ✗ break;
152 }
153 784 }
154
155 /*
156 * Declared width of one entry, without reading the value.
157 */
158 struct width_search {
159 uint16_t wanted;
160 size_t width;
161 bool found;
162 };
163
164 1574 static bool match_width(const struct kfsw_param_info *info, void *context)
165 {
166 1574 struct width_search *search = context;
167
168
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 872 times.
1574 if (info->id != search->wanted) {
169 702 return true;
170 }
171 872 search->width = entry_width(info->type, info->array_size);
172 872 search->found = true;
173 872 return false;
174 }
175
176 886 static int entry_declared_width(const struct kfsw_hk_entry *entry, size_t *width)
177 {
178 886 struct width_search search = {.wanted = entry->param_id};
179 int result;
180
181
2/2
✓ Branch 0 taken 828 times.
✓ Branch 1 taken 58 times.
886 if (entry->node == KFSW_HK_NODE_LOCAL) {
182 828 result = kfsw_param_visit(match_width, &search);
183 } else {
184 #if CONFIG_KFSW_PARAM_CSP
185 /* Refused when the report is defined, not at the first sample. */
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (entry->node >= KFSW_CSP_BROADCAST_ADDRESS) {
187 ✗ return -EINVAL;
188 }
189 58 result = kfsw_param_remote_visit_until(entry->node, match_width, &search,
190 58 k_uptime_get() +
191 CONFIG_KFSW_PARAM_LIST_TIMEOUT_MS);
192 #else
193 ✗ return -ENOTSUP;
194 #endif
195 }
196
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 884 times.
886 if (result != 0) {
197 2 return result;
198 }
199
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 872 times.
884 if (!search.found) {
200 12 return -ENOENT;
201 }
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 872 times.
872 if (search.width == 0U) {
203 ✗ return -ENOTSUP;
204 }
205 872 *width = search.width;
206 872 return 0;
207 }
208
209 476 int kfsw_hk_prepare_definition(uint8_t report, const struct kfsw_hk_entry *entries, size_t count,
210 struct kfsw_hk_definition *definition)
211 {
212 476 size_t payload = 0U;
213 int result;
214
215
4/8
✓ Branch 0 taken 476 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 476 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 476 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 476 times.
✗ Branch 7 not taken.
476 if (report >= CONFIG_KFSW_HK_REPORTS || entries == NULL || definition == NULL ||
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 count == 0U || count > CONFIG_KFSW_HK_ENTRIES) {
217 ✗ return -EINVAL;
218 }
219 /* Check every entry before storing anything, and keep the widths. */
220
221
2/2
✓ Branch 0 taken 886 times.
✓ Branch 1 taken 462 times.
1348 for (size_t index = 0U; index < count; index++) {
222 886 size_t width = 0U;
223
224 886 result = entry_declared_width(&entries[index], &width);
225
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 872 times.
886 if (result != 0) {
226 14 kfsw_log_warning("HK: report %u entry %u (node %u id 0x%04x): %d", report,
227 (unsigned int)index, entries[index].node,
228 entries[index].param_id, result);
229 14 return result;
230 }
231 872 definition->offsets[index] = (uint16_t)(KFSW_HK_HEADER_SIZE + payload);
232 872 definition->widths[index] = (uint16_t)width;
233 872 payload += width;
234 }
235
236
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 460 times.
462 if ((payload + KFSW_HK_HEADER_SIZE) > CONFIG_KFSW_HK_SAMPLE_BYTES) {
237 2 kfsw_log_warning("HK: report %u needs %u bytes, one sample holds %u", report,
238 (unsigned int)(payload + KFSW_HK_HEADER_SIZE),
239 (unsigned int)CONFIG_KFSW_HK_SAMPLE_BYTES);
240 2 return -EMSGSIZE;
241 }
242
243 460 memcpy(definition->entries, entries, count * sizeof(entries[0]));
244 460 definition->retrieval_class = KFSW_HK_CLASS_DEFAULT;
245 460 definition->entry_count = count;
246 460 definition->payload_bytes = payload;
247 460 return 0;
248 }
249
250 static int clear_impl(uint8_t report);
251
252 /* An entry may name this node by its address; it is read locally all the same. */
253 356 static void name_local_entries(const struct kfsw_hk_entry *entries, size_t count,
254 struct kfsw_hk_entry *named)
255 {
256
4/4
✓ Branch 0 taken 146 times.
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 96 times.
✓ Branch 1 taken 260 times.
1034 for (size_t index = 0U; index < count; index++) {
257 678 named[index] = entries[index];
258 #if CONFIG_KFSW_CSP
259
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 530 times.
532 if (csp_iflist_get_by_addr(named[index].node) != NULL) {
260 2 named[index].node = KFSW_HK_NODE_LOCAL;
261 }
262 #endif
263 }
264 356 }
265
266 362 static int define_impl(uint8_t report, const struct kfsw_hk_entry *requested, size_t count,
267 uint8_t retrieval_class)
268 {
269 362 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
270 struct kfsw_hk_entry entries[CONFIG_KFSW_HK_ENTRIES];
271 struct kfsw_hk_definition definition;
272 int result;
273
274
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 358 times.
362 if (retrieval_class > KFSW_HK_CLASS_MAX) {
275 4 return -ERANGE;
276 }
277
3/4
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 356 times.
358 if (target == NULL || requested == NULL) {
278 2 return -EINVAL;
279 }
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 356 times.
356 if (count == 0U) {
281 ✗ return clear_impl(report);
282 }
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 356 times.
356 if (count > CONFIG_KFSW_HK_ENTRIES) {
284 ✗ return -E2BIG;
285 }
286 356 name_local_entries(requested, count, entries);
287 356 result = kfsw_hk_prepare_definition(report, entries, count, &definition);
288
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 348 times.
356 if (result != 0) {
289 8 return result;
290 }
291
292 348 kfsw_hk_storage_lock();
293 348 kfsw_hk_lock();
294 348 generations[report]++;
295 348 schedule_revisions[report]++;
296 348 memcpy(target->entries, entries, count * sizeof(entries[0]));
297 348 memcpy(target->widths, definition.widths, count * sizeof(definition.widths[0]));
298 348 memcpy(target->offsets, definition.offsets, count * sizeof(definition.offsets[0]));
299 348 target->retrieval_class = retrieval_class;
300 348 target->entry_count = (uint8_t)count;
301 348 target->payload_bytes = definition.payload_bytes;
302 348 target->defined = true;
303 348 target->next_uptime_ms = k_uptime_get() + target->period_ms;
304 /* A new definition discards the old samples. */
305 348 target->held = 0U;
306 348 target->next_slot = 0U;
307 348 stats.reports = 0U;
308
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 348 times.
1044 for (size_t index = 0U; index < ARRAY_SIZE(reports); index++) {
309
2/2
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 322 times.
696 if (reports[index].defined) {
310 374 stats.reports++;
311 }
312 }
313 348 kfsw_hk_unlock();
314
315 #if CONFIG_KFSW_HK_STORE
316 /* Same for the store: the old file no longer matches the layout. */
317 206 kfsw_hk_store_forget(report);
318 #endif
319 348 kfsw_hk_storage_unlock();
320 348 kfsw_hk_wake();
321 348 kfsw_log_info("HK: report %u defined, %u entries, %u bytes", report, (unsigned int)count,
322 (unsigned int)definition.payload_bytes);
323
324 348 return 0;
325 }
326
327 1024 static int clear_impl(uint8_t report)
328 {
329 1024 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1024 times.
1024 if (target == NULL) {
332 ✗ return -EINVAL;
333 }
334 1024 kfsw_hk_storage_lock();
335 #if CONFIG_KFSW_HK_STORE
336 728 kfsw_hk_store_forget(report);
337 #endif
338
339 1024 kfsw_hk_lock();
340 1024 generations[report]++;
341 1024 schedule_revisions[report]++;
342 1024 memset(target, 0, sizeof(*target));
343 1024 stats.reports = 0U;
344
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 1024 times.
3072 for (size_t index = 0U; index < ARRAY_SIZE(reports); index++) {
345
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2012 times.
2048 if (reports[index].defined) {
346 36 stats.reports++;
347 }
348 }
349 1024 kfsw_hk_unlock();
350 1024 kfsw_hk_storage_unlock();
351 1024 kfsw_hk_wake();
352
353 1024 return 0;
354 }
355
356 60 int kfsw_hk_get_definition(uint8_t report, struct kfsw_hk_entry *entries, size_t *count)
357 {
358 60 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
359 60 int result = 0;
360
361
3/6
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 60 times.
60 if ((target == NULL) || (entries == NULL) || (count == NULL)) {
362 ✗ return -EINVAL;
363 }
364
365 60 kfsw_hk_lock();
366
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 36 times.
60 if (!target->defined) {
367 24 result = -ENOENT;
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 } else if (*count < target->entry_count) {
369 ✗ result = -ENOSPC;
370 } else {
371 36 memcpy(entries, target->entries, target->entry_count * sizeof(entries[0]));
372 36 *count = target->entry_count;
373 }
374 60 kfsw_hk_unlock();
375 60 return result;
376 }
377
378 44 void kfsw_hk_set_enabled(bool value)
379 {
380 bool changed;
381
382 44 kfsw_hk_lock();
383 44 changed = (enabled != value);
384 44 enabled = value;
385
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 32 times.
44 if (changed) {
386
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 for (size_t index = 0; index < ARRAY_SIZE(reports); index++) {
387 24 schedule_revisions[index]++;
388 }
389 }
390 44 kfsw_hk_unlock();
391
392 /* Log only on a change. */
393
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 32 times.
44 if (changed) {
394 12 kfsw_hk_wake();
395
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 kfsw_log_info("HK: periodic collection %s", value ? "enabled" : "disabled");
396 }
397 44 }
398
399 592 bool kfsw_hk_enabled(void)
400 {
401 bool value;
402
403 592 kfsw_hk_lock();
404 592 value = enabled;
405 592 kfsw_hk_unlock();
406 592 return value;
407 }
408
409 /*
410 * Whether the wall clock is set. False without CSP.
411 */
412 980 bool kfsw_hk_clock_valid(void)
413 {
414 #if CONFIG_KFSW_CSP
415 742 struct kfsw_csp_clock clock = {0};
416
417 742 kfsw_csp_clock_get(&clock);
418 742 return kfsw_csp_clock_is_set(&clock);
419 #else
420 238 return false;
421 #endif
422 }
423
424 /*
425 * One collection. The timestamp is taken at the start. An entry that can't be
426 * read is zero-filled and flagged.
427 */
428 420 int kfsw_hk_collect_report(const struct kfsw_hk_definition *entry, struct kfsw_hk_sample *sample,
429 uint32_t *failures)
430 {
431 420 uint8_t flags = 0U;
432 #if CONFIG_KFSW_PARAM_CSP
433 14 int64_t remote_deadline = k_uptime_get() + CONFIG_KFSW_HK_REMOTE_BUDGET_MS;
434 #endif
435
436
3/6
✓ Branch 0 taken 420 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✓ Branch 3 taken 406 times.
420 if ((entry == NULL) || (sample == NULL)) {
437 ✗ return -EINVAL;
438 }
439
440 420 memset(sample, 0, sizeof(*sample));
441
442 /* Zero means the clock is not set; flag it. */
443 420 sample->seconds = 0U;
444
2/2
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 130 times.
420 if (kfsw_hk_clock_valid()) {
445 #if CONFIG_KFSW_CSP
446 76 struct kfsw_csp_clock clock = {0};
447
448 76 kfsw_csp_clock_get(&clock);
449 76 sample->seconds = (uint32_t)clock.seconds;
450 #endif
451 } else {
452 344 flags |= KFSW_HK_FLAG_CLOCK_UNSET;
453 }
454 420 sample->entry_count = entry->entry_count;
455 420 sample->length = (uint16_t)(KFSW_HK_HEADER_SIZE + entry->payload_bytes);
456
457 /* Local entries first, straight into their slots. */
458
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 420 times.
1230 for (size_t index = 0U; index < entry->entry_count; index++) {
459 810 const struct kfsw_hk_entry *definition = &entry->entries[index];
460 struct kfsw_param_value value;
461
462
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 752 times.
810 if (definition->node != KFSW_HK_NODE_LOCAL) {
463 58 continue;
464 }
465
1/2
✓ Branch 1 taken 752 times.
✗ Branch 2 not taken.
752 if (kfsw_param_get_by_id(definition->param_id, &value) == 0) {
466 752 kfsw_hk_write_value(&sample->data[entry->offsets[index]],
467 752 entry->widths[index], &value);
468 } else {
469 ✗ flags |= KFSW_HK_FLAG_INCOMPLETE;
470 ✗ (*failures)++;
471 }
472 }
473
474 #if CONFIG_KFSW_PARAM_CSP
475 /* Then one pass per remote node, since the descriptor cache holds one node. */
476
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 14 times.
72 for (size_t index = 0U; index < entry->entry_count; index++) {
477 58 uint16_t node = entry->entries[index].node;
478 58 bool seen = false;
479
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (node == KFSW_HK_NODE_LOCAL) {
481 ✗ continue;
482 }
483
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 14 times.
58 for (size_t earlier = 0U; earlier < index; earlier++) {
484
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (entry->entries[earlier].node == node) {
485 44 seen = true;
486 44 break;
487 }
488 }
489
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 14 times.
58 if (seen) {
490 44 continue;
491 }
492
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6 times.
14 if (kfsw_hk_collect_remote(entry, node, sample, failures, remote_deadline) != 0) {
493 8 flags |= KFSW_HK_FLAG_INCOMPLETE;
494 }
495 }
496 #else
497
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 406 times.
1158 for (size_t index = 0U; index < entry->entry_count; index++) {
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (entry->entries[index].node != KFSW_HK_NODE_LOCAL) {
499 ✗ flags |= KFSW_HK_FLAG_INCOMPLETE;
500 ✗ (*failures)++;
501 }
502 }
503 #endif
504
505 420 sample->flags = flags;
506 420 sample->sequence = entry->sequence;
507
508 420 sample->data[0] = KFSW_HK_PROTOCOL_VERSION;
509 420 sys_put_be16(sample->sequence, &sample->data[2]);
510 420 sys_put_be32(sample->seconds, &sample->data[4]);
511 420 sample->data[8] = sample->entry_count;
512 420 sample->data[9] = sample->flags;
513 420 return 0;
514 }
515
516 432 int kfsw_hk_collect(uint8_t report)
517 {
518 432 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
519 static struct kfsw_hk_sample scratch;
520 static struct kfsw_hk_definition definition;
521 uint64_t generation;
522 432 uint32_t failures = 0U;
523 uint16_t next_sequence;
524 int result;
525
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432 times.
432 if (target == NULL) {
527 ✗ return -EINVAL;
528 }
529
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 424 times.
432 if (!kfsw_hk_is_ready()) {
530 8 return -EACCES;
531 }
532 424 k_mutex_lock(&collection_lock, K_FOREVER);
533 424 kfsw_hk_lock();
534
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 420 times.
424 if (!target->defined) {
535 4 kfsw_hk_unlock();
536 4 k_mutex_unlock(&collection_lock);
537 4 return -ENOENT;
538 }
539 420 memcpy(definition.entries, target->entries, sizeof(definition.entries));
540 420 memcpy(definition.widths, target->widths, sizeof(definition.widths));
541 420 memcpy(definition.offsets, target->offsets, sizeof(definition.offsets));
542 420 definition.retrieval_class = target->retrieval_class;
543 420 definition.entry_count = target->entry_count;
544 420 definition.payload_bytes = target->payload_bytes;
545 420 definition.sequence = target->sequence;
546 420 generation = generations[report];
547 420 kfsw_hk_unlock();
548
549 420 result = kfsw_hk_collect_report(&definition, &scratch, &failures);
550 420 kfsw_hk_storage_lock();
551 420 kfsw_hk_lock();
552
3/4
✓ Branch 0 taken 420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 418 times.
420 if (!target->defined || (generation != generations[report])) {
553 2 result = -EAGAIN;
554 }
555
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 418 times.
420 if (result != 0) {
556 2 stats.failures++;
557 2 kfsw_hk_unlock();
558 2 kfsw_hk_storage_unlock();
559 2 k_mutex_unlock(&collection_lock);
560 2 return result;
561 }
562 418 scratch.data[1] = report;
563
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 284 times.
418 if (target->held == ARRAY_SIZE(target->ring)) {
564 134 kfsw_hk_count_overwritten();
565 } else {
566 284 target->held++;
567 }
568 418 target->collected_order[target->next_slot] = ++collected_order;
569 418 target->ring[target->next_slot] = scratch;
570 418 target->next_slot = (uint16_t)((target->next_slot + 1U) % ARRAY_SIZE(target->ring));
571 418 target->sequence++;
572 418 next_sequence = target->sequence;
573 418 stats.collections++;
574 418 stats.entries_failed += failures;
575 418 stats.last_seconds = scratch.seconds;
576 418 kfsw_hk_unlock();
577 #if CONFIG_KFSW_HK_STORE
578 /* Collection writes the ring; storage blocks definition changes. */
579
2/2
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 22 times.
210 if (kfsw_hk_store_interval(report) != 0U) {
580 188 int stored = kfsw_hk_store_flush(report, next_sequence);
581
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 if (stored != 0) {
583 ✗ kfsw_log_warning("HK: report %u could not be stored (%d)", report, stored);
584 }
585 }
586 #else
587 ARG_UNUSED(next_sequence);
588 #endif
589 418 kfsw_hk_storage_unlock();
590 418 k_mutex_unlock(&collection_lock);
591 418 return 0;
592 }
593
594 #if CONFIG_KFSW_HK_STORE
595 154 static int set_store_impl(uint8_t report, uint32_t interval_ms)
596 {
597 154 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
598 uint32_t period;
599 uint16_t record_size;
600 int result;
601
602
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 152 times.
154 if (target == NULL) {
603 2 return -EINVAL;
604 }
605 152 kfsw_hk_storage_lock();
606 152 kfsw_hk_lock();
607
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 88 times.
152 if (!target->defined) {
608 64 kfsw_hk_unlock();
609 64 kfsw_hk_storage_unlock();
610 64 return -ENOENT;
611 }
612 88 period = target->period_ms;
613 88 record_size = (uint16_t)(KFSW_HK_HEADER_SIZE + target->payload_bytes);
614 88 kfsw_hk_unlock();
615
616 /* Stopping keeps the file; kfsw_hk_store_clear() deletes it. */
617 88 result = kfsw_hk_store_configure(report, interval_ms, period, record_size);
618 88 kfsw_hk_storage_unlock();
619 88 return result;
620 }
621
622 342 static int clear_store_impl(uint8_t report)
623 {
624
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 340 times.
342 if (report >= CONFIG_KFSW_HK_REPORTS) {
625 2 return -EINVAL;
626 }
627 340 kfsw_hk_storage_lock();
628 340 kfsw_hk_store_forget(report);
629 340 kfsw_hk_storage_unlock();
630 340 return 0;
631 }
632
633 876 int kfsw_hk_get_store(uint8_t report, uint32_t *interval_ms)
634 {
635
2/4
✓ Branch 0 taken 876 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 876 times.
876 if ((report >= CONFIG_KFSW_HK_REPORTS) || (interval_ms == NULL)) {
636 ✗ return -EINVAL;
637 }
638 876 kfsw_hk_storage_lock();
639 876 *interval_ms = kfsw_hk_store_interval(report);
640 876 kfsw_hk_storage_unlock();
641 876 return 0;
642 }
643 #endif
644
645 310 int kfsw_hk_get(uint8_t report, uint16_t age, struct kfsw_hk_sample *sample)
646 {
647 310 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
648 310 int result = 0;
649
650
2/4
✓ Branch 0 taken 310 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 310 times.
310 if ((target == NULL) || (sample == NULL)) {
651 ✗ return -EINVAL;
652 }
653
654 310 kfsw_hk_lock();
655
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 302 times.
310 if (age >= target->held) {
656 8 result = -ENOENT;
657 } else {
658 /* The newest sample is one slot before next_slot; age counts backwards.
659 */
660 302 uint16_t slot =
661 302 (uint16_t)((target->next_slot + ARRAY_SIZE(target->ring) - 1U - age) %
662 ARRAY_SIZE(target->ring));
663
664 302 *sample = target->ring[slot];
665 }
666 310 kfsw_hk_unlock();
667 310 return result;
668 }
669
670 40 int kfsw_hk_depth(uint8_t report, uint16_t *depth)
671 {
672 40 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
673
674
3/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
40 if ((target == NULL) || (depth == NULL)) {
675 2 return -EINVAL;
676 }
677 38 kfsw_hk_lock();
678 38 *depth = target->held;
679 38 kfsw_hk_unlock();
680 38 return 0;
681 }
682
683 112 static int set_period_impl(uint8_t report, uint32_t period_ms)
684 {
685 112 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
686
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (target == NULL) {
688 ✗ return -EINVAL;
689 }
690
3/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 110 times.
112 if ((period_ms != 0U) && (period_ms < CONFIG_KFSW_HK_PERIOD_FLOOR_MS)) {
691 2 return -ERANGE;
692 }
693
694 110 kfsw_hk_lock();
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (!target->defined) {
696 ✗ kfsw_hk_unlock();
697 ✗ return -ENOENT;
698 }
699 110 schedule_revisions[report]++;
700 110 target->period_ms = period_ms;
701
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 target->next_uptime_ms = (period_ms == 0U) ? 0 : (k_uptime_get() + (int64_t)period_ms);
702 110 kfsw_hk_unlock();
703 110 kfsw_hk_wake();
704
705 110 return 0;
706 }
707
708 58 int kfsw_hk_get_period(uint8_t report, uint32_t *period_ms)
709 {
710 58 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
711
712
2/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
58 if ((target == NULL) || (period_ms == NULL)) {
713 ✗ return -EINVAL;
714 }
715 58 kfsw_hk_lock();
716 58 *period_ms = target->period_ms;
717 58 kfsw_hk_unlock();
718 58 return 0;
719 }
720
721 146 void kfsw_hk_get_stats(struct kfsw_hk_stats *out)
722 {
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (out == NULL) {
724 ✗ return;
725 }
726 146 kfsw_hk_lock();
727 146 *out = stats;
728 146 kfsw_hk_unlock();
729 /* Read outside the lock; it only reads the clock. */
730 146 out->clock_valid = kfsw_hk_clock_valid();
731 146 out->enabled = kfsw_hk_enabled();
732 #if CONFIG_KFSW_HK_BEACON
733 88 kfsw_hk_beacon_stats(&out->beacons_sent, &out->beacons_skipped);
734 #endif
735 }
736
737 38 int kfsw_hk_init(void)
738 {
739 38 k_mutex_lock(&init_lock, K_FOREVER);
740
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 12 times.
38 if (!initialized) {
741 #if CONFIG_KFSW_HK_PERSISTENCE
742 /* A rejected snapshot leaves diagnostics available with default settings. */
743 12 (void)kfsw_hk_persist_load();
744 #endif
745 26 kfsw_hk_lock();
746 26 initialized = true;
747 26 kfsw_hk_unlock();
748 }
749 38 k_mutex_unlock(&init_lock);
750 38 return 0;
751 }
752
753 132 int64_t kfsw_hk_next_due(int64_t due, uint32_t period, int64_t now)
754 {
755 /* Advance strictly past completion, without a catch-up burst. */
756 132 return due + ((now - due) / period + 1) * period;
757 }
758
759 218 bool kfsw_hk_schedule_take(uint8_t index, int64_t now, struct kfsw_hk_due *due)
760 {
761 218 struct kfsw_hk_report *report = kfsw_hk_report_at(index);
762 218 bool ready = false;
763
764 218 kfsw_hk_lock();
765
7/10
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 216 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 216 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 142 times.
✓ Branch 9 taken 74 times.
218 if (initialized && !restoring && enabled && report != NULL && report->defined &&
766
4/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 46 times.
142 report->period_ms != 0U && now >= report->next_uptime_ms) {
767 88 *due = (struct kfsw_hk_due){schedule_revisions[index], report->next_uptime_ms,
768 88 report->period_ms};
769 88 stats.scheduled_attempts++;
770 88 ready = true;
771 }
772 218 kfsw_hk_unlock();
773 218 return ready;
774 }
775
776 90 void kfsw_hk_schedule_finish(uint8_t index, const struct kfsw_hk_due *due, int64_t now)
777 {
778 90 struct kfsw_hk_report *report = kfsw_hk_report_at(index);
779 90 uint64_t missed = (uint64_t)(MAX(now, due->uptime_ms) - due->uptime_ms) / due->period_ms;
780
781 90 kfsw_hk_lock();
782
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 stats.missed_slots += (uint32_t)MIN(missed, UINT32_MAX - stats.missed_slots);
783
5/8
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 88 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 88 times.
✗ Branch 7 not taken.
90 if (enabled && report != NULL && report->defined && report->period_ms != 0U &&
784
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4 times.
88 schedule_revisions[index] == due->revision) {
785 84 report->next_uptime_ms =
786 84 kfsw_hk_next_due(due->uptime_ms, due->period_ms, MAX(now, due->uptime_ms));
787 }
788 90 kfsw_hk_unlock();
789 90 }
790
791 94 int64_t kfsw_hk_schedule_wait(int64_t now)
792 {
793 94 int64_t wait = 200;
794
795 94 kfsw_hk_lock();
796
2/4
✗ Branch 0 not taken.
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 94 times.
282 for (size_t index = 0; index < ARRAY_SIZE(reports); index++) {
797 188 struct kfsw_hk_report *report = &reports[index];
798
799
4/4
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 8 times.
188 if (report->defined && report->period_ms != 0U) {
800 106 wait = MIN(wait, MAX(0, report->next_uptime_ms - now));
801 }
802 #if CONFIG_KFSW_HK_BEACON
803
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ wait = MIN(wait, kfsw_hk_beacon_wait((uint8_t)index, now));
804 #endif
805 }
806 94 kfsw_hk_unlock();
807 94 return wait;
808 }
809
810 5760 bool kfsw_hk_is_ready(void)
811 {
812 bool ready;
813
814 5760 kfsw_hk_lock();
815
4/4
✓ Branch 0 taken 5758 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5726 times.
✓ Branch 3 taken 32 times.
5760 ready = initialized && !restoring;
816 5760 kfsw_hk_unlock();
817 5760 return ready;
818 }
819
820 2540 int kfsw_hk_config_begin(void)
821 {
822
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2530 times.
2540 if (!kfsw_hk_is_ready()) {
823 10 return -EACCES;
824 }
825 2530 k_mutex_lock(&config_lock, K_FOREVER);
826
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2530 times.
2530 if (!kfsw_hk_is_ready()) {
827 ✗ k_mutex_unlock(&config_lock);
828 ✗ return -EACCES;
829 }
830 2530 return 0;
831 }
832
833 2530 int kfsw_hk_config_end(int result)
834 {
835
2/2
✓ Branch 0 taken 2424 times.
✓ Branch 1 taken 106 times.
2530 if (result == 0) {
836 2424 kfsw_hk_lock();
837 2424 config_revision++;
838 2424 stats.settings_dirty = IS_ENABLED(CONFIG_KFSW_HK_PERSISTENCE);
839 2424 kfsw_hk_unlock();
840 }
841 2530 k_mutex_unlock(&config_lock);
842 #if CONFIG_KFSW_HK_PERSISTENCE
843
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 70 times.
1854 if (result == 0) {
844 1784 result = kfsw_hk_persist_save();
845
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1752 times.
1784 if (result != 0) {
846 32 return KFSW_HK_APPLIED_UNSAVED;
847 }
848 }
849 #endif
850 2498 return result;
851 }
852
853 330 int kfsw_hk_define(uint8_t report, const struct kfsw_hk_entry *entries, size_t count)
854 {
855 330 return kfsw_hk_define_class(report, entries, count, KFSW_HK_CLASS_DEFAULT);
856 }
857
858 362 int kfsw_hk_define_class(uint8_t report, const struct kfsw_hk_entry *entries, size_t count,
859 uint8_t retrieval_class)
860 {
861 362 int result = kfsw_hk_config_begin();
862
863 return result != 0
864 ? result
865
1/2
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
362 : kfsw_hk_config_end(define_impl(report, entries, count, retrieval_class));
866 }
867
868 1026 int kfsw_hk_clear(uint8_t report)
869 {
870 1026 int result = kfsw_hk_config_begin();
871
872
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 2 times.
1026 return result != 0 ? result : kfsw_hk_config_end(clear_impl(report));
873 }
874
875 120 int kfsw_hk_set_period(uint8_t report, uint32_t period_ms)
876 {
877 120 int result = kfsw_hk_config_begin();
878
879
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 8 times.
120 return result != 0 ? result : kfsw_hk_config_end(set_period_impl(report, period_ms));
880 }
881
882 #if CONFIG_KFSW_HK_STORE
883 154 int kfsw_hk_set_store(uint8_t report, uint32_t interval_ms)
884 {
885 154 int result = kfsw_hk_config_begin();
886
887
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 return result != 0 ? result : kfsw_hk_config_end(set_store_impl(report, interval_ms));
888 }
889
890 342 int kfsw_hk_clear_store(uint8_t report)
891 {
892 342 int result = kfsw_hk_config_begin();
893
894
1/2
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
342 return result != 0 ? result : kfsw_hk_config_end(clear_store_impl(report));
895 }
896 #endif
897
898 #if CONFIG_KFSW_HK_PERSISTENCE
899 /* The caller holds the HK state lock. The file mutex is taken first. */
900 2088 uint64_t kfsw_hk_config_revision(void)
901 {
902 2088 return config_revision;
903 }
904
905 2088 bool kfsw_hk_save_blocked(void)
906 {
907 2088 return save_blocked;
908 }
909
910 2088 void kfsw_hk_save_result(uint64_t revision, int result)
911 {
912 2088 kfsw_hk_lock();
913 2088 stats.last_save_error = result;
914
4/4
✓ Branch 0 taken 2056 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 2048 times.
✓ Branch 3 taken 8 times.
2088 if (result == 0 && config_revision == revision) {
915 2048 stats.settings_dirty = false;
916 }
917 2088 kfsw_hk_unlock();
918 2088 }
919
920 200 int kfsw_hk_save(void)
921 {
922
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 if (!kfsw_hk_is_ready()) {
923 ✗ return -EACCES;
924 }
925 200 kfsw_hk_lock();
926 200 save_blocked = false;
927 200 kfsw_hk_unlock();
928 200 return kfsw_hk_persist_save();
929 }
930
931 180 void kfsw_hk_restore_begin(void)
932 {
933 180 k_mutex_lock(&config_lock, K_FOREVER);
934 180 kfsw_hk_lock();
935 180 restoring = true;
936 180 kfsw_hk_unlock();
937 180 }
938
939 180 void kfsw_hk_restore_end(int result)
940 {
941 180 kfsw_hk_lock();
942
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 28 times.
180 stats.last_load_error = result == -ENOENT ? 0 : result;
943
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 28 times.
180 save_blocked = result != 0 && result != -ENOENT;
944 180 stats.settings_dirty = save_blocked;
945
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 84 times.
180 if (result == 0) {
946 96 config_revision++;
947 96 stats.settings_dirty = false;
948 }
949 180 restoring = false;
950 180 kfsw_hk_unlock();
951 180 k_mutex_unlock(&config_lock);
952 180 kfsw_hk_wake();
953 180 }
954
955 /* The snapshot is already checked. The caller holds the config lock. */
956 192 void kfsw_hk_restore_report(uint8_t index, const struct kfsw_hk_definition *definition,
957 uint32_t period_ms)
958 {
959 192 struct kfsw_hk_report *report = &reports[index];
960
961 192 kfsw_hk_lock();
962 192 generations[index]++;
963 192 schedule_revisions[index]++;
964 192 memset(report, 0, sizeof(*report));
965
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 88 times.
192 if (definition != NULL) {
966 104 memcpy(report->entries, definition->entries, sizeof(report->entries));
967 104 memcpy(report->widths, definition->widths, sizeof(report->widths));
968 104 memcpy(report->offsets, definition->offsets, sizeof(report->offsets));
969 104 report->retrieval_class = definition->retrieval_class;
970 104 report->entry_count = definition->entry_count;
971 104 report->payload_bytes = definition->payload_bytes;
972 104 report->defined = true;
973 104 report->period_ms = period_ms;
974 104 report->next_uptime_ms = k_uptime_get() + period_ms;
975 #if CONFIG_KFSW_HK_STORE
976 104 report->sequence = kfsw_hk_store_restore_sequence(index);
977 #endif
978 }
979 192 stats.reports = 0;
980
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 192 times.
576 for (size_t i = 0; i < ARRAY_SIZE(reports); i++) {
981 384 stats.reports += reports[i].defined;
982 }
983 192 kfsw_hk_unlock();
984 192 }
985 #endif
986