kfsw-services/src/hk/hk_remote.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/util.h> | ||
| 8 | |||
| 9 | #include <kfsw/services/hk.h> | ||
| 10 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HK | ||
| 11 | #include <kfsw/services/log.h> | ||
| 12 | #include <kfsw/services/parameter.h> | ||
| 13 | |||
| 14 | #include "hk_internal.h" | ||
| 15 | |||
| 16 | #if CONFIG_KFSW_PARAM_CSP | ||
| 17 | |||
| 18 | /* Values are requested one window at a time. */ | ||
| 19 | #define KFSW_HK_REMOTE_WINDOW 8U | ||
| 20 | |||
| 21 | struct name_search { | ||
| 22 | const struct kfsw_hk_definition *report; | ||
| 23 | uint16_t node; | ||
| 24 | /* Indices into the report, and the name each one resolved to. */ | ||
| 25 | uint8_t indices[CONFIG_KFSW_HK_ENTRIES]; | ||
| 26 | const char *names[CONFIG_KFSW_HK_ENTRIES]; | ||
| 27 | char name_storage[CONFIG_KFSW_HK_ENTRIES][KFSW_PARAM_NAME_MAX + 1]; | ||
| 28 | size_t count; | ||
| 29 | }; | ||
| 30 | |||
| 31 | /* | ||
| 32 | * Map IDs to names with the cached descriptors; no round trip is needed. | ||
| 33 | */ | ||
| 34 | 52 | static bool match_names(const struct kfsw_param_info *info, void *context) | |
| 35 | { | ||
| 36 | 52 | struct name_search *search = context; | |
| 37 | |||
| 38 | 666 | for (size_t index = 0U; index < search->report->entry_count; index++) { | |
| 39 | 614 | const struct kfsw_hk_entry *entry = &search->report->entries[index]; | |
| 40 | |||
| 41 | 614 | if ((entry->node != search->node) || (entry->param_id != info->id)) { | |
| 42 | 562 | continue; | |
| 43 | } | ||
| 44 | 52 | if (search->count >= ARRAY_SIZE(search->indices)) { | |
| 45 | ✗ | return false; | |
| 46 | } | ||
| 47 | 52 | search->indices[search->count] = (uint8_t)index; | |
| 48 | 52 | strncpy(search->name_storage[search->count], info->name, KFSW_PARAM_NAME_MAX); | |
| 49 | 52 | search->names[search->count] = search->name_storage[search->count]; | |
| 50 | 52 | search->count++; | |
| 51 | } | ||
| 52 | 52 | return true; | |
| 53 | } | ||
| 54 | |||
| 55 | 14 | int kfsw_hk_collect_remote(const struct kfsw_hk_definition *report, uint16_t node, | |
| 56 | struct kfsw_hk_sample *sample, uint32_t *failures, int64_t deadline) | ||
| 57 | { | ||
| 58 | static struct name_search search; | ||
| 59 | static struct kfsw_param_value values[KFSW_HK_REMOTE_WINDOW]; | ||
| 60 | 14 | size_t expected = 0U; | |
| 61 | 14 | int first_read_error = 0; | |
| 62 | int result; | ||
| 63 | |||
| 64 | 14 | if ((report == NULL) || (sample == NULL)) { | |
| 65 | ✗ | return -EINVAL; | |
| 66 | } | ||
| 67 | |||
| 68 | 72 | for (size_t index = 0U; index < report->entry_count; index++) { | |
| 69 | 58 | if (report->entries[index].node == node) { | |
| 70 | 58 | expected++; | |
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 14 | memset(&search, 0, sizeof(search)); | |
| 75 | 14 | search.report = report; | |
| 76 | 14 | search.node = node; | |
| 77 | |||
| 78 | 14 | result = kfsw_param_remote_visit_until(node, match_names, &search, deadline); | |
| 79 | 14 | if (result != 0) { | |
| 80 | /* No answer: the values stay zero and the sample is marked incomplete. */ | ||
| 81 | 2 | kfsw_log_warning("HK: node %u did not list its parameters (%d)", node, result); | |
| 82 | 6 | for (size_t index = 0U; index < expected; index++) { | |
| 83 | 4 | (*failures)++; | |
| 84 | } | ||
| 85 | 2 | return result; | |
| 86 | } | ||
| 87 | |||
| 88 | 12 | if (search.count != expected) { | |
| 89 | 2 | kfsw_log_warning("HK: node %u carries %u of the %u values asked of it", node, | |
| 90 | (unsigned int)search.count, (unsigned int)expected); | ||
| 91 | } | ||
| 92 | |||
| 93 | 28 | for (size_t base = 0U; base < search.count; base += KFSW_HK_REMOTE_WINDOW) { | |
| 94 | 16 | size_t span = MIN(search.count - base, (size_t)KFSW_HK_REMOTE_WINDOW); | |
| 95 | |||
| 96 | 16 | result = kfsw_param_remote_get_many_until(node, &search.names[base], span, values, | |
| 97 | deadline); | ||
| 98 | 16 | if (result != 0) { | |
| 99 | 4 | if (first_read_error == 0) { | |
| 100 | 4 | first_read_error = result; | |
| 101 | } | ||
| 102 | /* A failed window leaves its values zero. */ | ||
| 103 | 24 | for (size_t offset = 0U; offset < span; offset++) { | |
| 104 | 20 | (*failures)++; | |
| 105 | } | ||
| 106 | 4 | continue; | |
| 107 | } | ||
| 108 | 44 | for (size_t offset = 0U; offset < span; offset++) { | |
| 109 | 32 | uint8_t index = search.indices[base + offset]; | |
| 110 | |||
| 111 | 32 | kfsw_hk_write_value(&sample->data[report->offsets[index]], | |
| 112 | 32 | report->widths[index], &values[offset]); | |
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | 12 | if (first_read_error != 0) { | |
| 117 | 4 | return first_read_error; | |
| 118 | } | ||
| 119 | 8 | return (search.count == expected) ? 0 : -ENOENT; | |
| 120 | } | ||
| 121 | |||
| 122 | #endif /* CONFIG_KFSW_PARAM_CSP */ | ||
| 123 |