./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
48 / 50
96.0%
Branches:
31 / 36
86.1%
Functions:
2 / 2
100.0%

kfsw-services/src/hk/hk_selection.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdint.h>
3
4 #include <zephyr/sys/util.h>
5
6 #include "hk_internal.h"
7
8 36 int kfsw_hk_get_class(uint8_t report, uint8_t *retrieval_class)
9 {
10 36 struct kfsw_hk_report *target = kfsw_hk_report_at(report);
11 36 int result = 0;
12
13
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if (target == NULL || retrieval_class == NULL) {
14 ✗ return -EINVAL;
15 }
16 36 kfsw_hk_lock();
17
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
36 if (!target->defined) {
18 8 result = -ENOENT;
19 } else {
20 28 *retrieval_class = target->retrieval_class;
21 }
22 36 kfsw_hk_unlock();
23 36 return result;
24 }
25
26 74 int kfsw_hk_get_selected(uint8_t class_mask, uint16_t index, struct kfsw_hk_sample *sample)
27 {
28 74 int result = -ENOENT;
29
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (sample == NULL) {
31 ✗ return -EINVAL;
32 }
33 74 kfsw_hk_lock();
34
2/2
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 32 times.
488 for (uint8_t cls = 0; cls <= KFSW_HK_CLASS_MAX; cls++) {
35 456 uint16_t ages[CONFIG_KFSW_HK_REPORTS] = {0};
36 456 uint16_t available = 0;
37
38
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 108 times.
456 if ((class_mask & (1U << cls)) == 0U) {
39 414 continue;
40 }
41
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 108 times.
324 for (uint8_t report = 0; report < CONFIG_KFSW_HK_REPORTS; report++) {
42 216 const struct kfsw_hk_report *target = kfsw_hk_report_at(report);
43
44
4/4
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 96 times.
216 if (target->defined && target->retrieval_class == cls) {
45 66 available += target->held;
46 }
47 }
48
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 42 times.
108 if (index >= available) {
49 66 index -= available;
50 66 continue;
51 }
52 /* Merge newest-first report rings; inspect only each ring's next sample.
53 * Collection order remains valid even when UTC steps backwards.
54 */
55
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 42 times.
86 for (uint16_t position = 0; position <= index; position++) {
56 44 uint64_t newest = 0;
57 44 uint8_t selected = 0;
58 44 const struct kfsw_hk_sample *chosen = NULL;
59
60
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 44 times.
132 for (uint8_t report = 0; report < CONFIG_KFSW_HK_REPORTS; report++) {
61 88 const struct kfsw_hk_report *target = kfsw_hk_report_at(report);
62 uint16_t slot;
63
64
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 20 times.
88 if (!target->defined || target->retrieval_class != cls ||
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 ages[report] >= target->held) {
66 40 continue;
67 }
68 48 slot = (uint16_t)((target->next_slot + ARRAY_SIZE(target->ring) -
69 48 1U - ages[report]) %
70 ARRAY_SIZE(target->ring));
71
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2 times.
48 if (target->collected_order[slot] > newest) {
72 46 newest = target->collected_order[slot];
73 46 chosen = &target->ring[slot];
74 46 selected = report;
75 }
76 }
77
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
44 if (position == index && chosen != NULL) {
78 42 *sample = *chosen;
79 42 result = 0;
80 }
81 44 ages[selected]++;
82 }
83 42 break;
84 }
85 74 kfsw_hk_unlock();
86 74 return result;
87 }
88