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 | 36 | if (target == NULL || retrieval_class == NULL) { | |
| 14 | ✗ | return -EINVAL; | |
| 15 | } | ||
| 16 | 36 | kfsw_hk_lock(); | |
| 17 | 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 | 74 | if (sample == NULL) { | |
| 31 | ✗ | return -EINVAL; | |
| 32 | } | ||
| 33 | 74 | kfsw_hk_lock(); | |
| 34 | 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 | 456 | if ((class_mask & (1U << cls)) == 0U) { | |
| 39 | 414 | continue; | |
| 40 | } | ||
| 41 | 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 | 216 | if (target->defined && target->retrieval_class == cls) { | |
| 45 | 66 | available += target->held; | |
| 46 | } | ||
| 47 | } | ||
| 48 | 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 | 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 | 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 | 88 | if (!target->defined || target->retrieval_class != cls || | |
| 65 | 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 | 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 | 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 |