./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
41 / 52
78.8%
Branches:
14 / 19
73.7%
Functions:
9 / 14
64.3%

kfsw-services/src/health-parameters/health_table.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include <zephyr/sys/util.h>
7
8 #include <kfsw/services/health.h>
9 #include <kfsw/services/parameter.h>
10
11 #if CONFIG_KFSW_HEALTH_SLOTS
12 #include CONFIG_KFSW_HEALTH_SLOTS_HEADER
13 #endif
14
15 static char health_faulted_by[KFSW_HEALTH_NAME_SIZE];
16 static uint32_t health_faults;
17 static uint32_t health_feeds;
18 static uint16_t health_interval;
19 static uint8_t health_state;
20 static uint8_t health_feeding;
21 static uint8_t health_count;
22
23 2 static void sample_status(void)
24 {
25 struct kfsw_health_status status;
26
27
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (kfsw_health_get_status(&status) != 0) {
28 ✗ return;
29 }
30 2 health_faults = status.faults;
31 2 health_feeds = status.feeds;
32 2 health_state = status.state;
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 health_feeding = status.feeding ? 1U : 0U;
34 2 health_count = status.count;
35
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 for (size_t index = 0U; index < sizeof(health_faulted_by); index++) {
36 32 health_faulted_by[index] = status.faulted_by[index];
37 }
38 2 health_faulted_by[sizeof(health_faulted_by) - 1U] = '\0';
39 }
40
41 #define HEALTH_SAMPLE(field, type) \
42 static void sample_##field(void *value) \
43 { \
44 sample_status(); \
45 *(type *)value = health_##field; \
46 }
47
48 ✗ HEALTH_SAMPLE(faults, uint32_t)
49 ✗ HEALTH_SAMPLE(feeds, uint32_t)
50 2 HEALTH_SAMPLE(state, uint8_t)
51 ✗ HEALTH_SAMPLE(feeding, uint8_t)
52 ✗ HEALTH_SAMPLE(count, uint8_t)
53
54 ✗ static void sample_faulted_by(void *value)
55 {
56 ✗ sample_status();
57 ✗ ((char *)value)[0] = health_faulted_by[0];
58
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (size_t index = 0U; index < KFSW_HEALTH_NAME_SIZE; index++) {
59 ✗ ((char *)value)[index] = health_faulted_by[index];
60 }
61 ✗ }
62
63 2 static void sample_interval(void *value)
64 {
65 2 *(uint16_t *)value = (uint16_t)kfsw_health_get_interval_ms();
66 2 }
67
68 /* Refuse an interval the watchdog can't tolerate before it is stored. */
69 8 static int validate_interval(const union kfsw_param_scalar *value)
70 {
71 8 return kfsw_health_check_interval_ms(value->u16);
72 }
73
74 6 static void apply_interval(const union kfsw_param_scalar *value)
75 {
76 6 (void)kfsw_health_set_interval_ms(value->u16);
77 6 }
78
79 #if CONFIG_KFSW_HEALTH_SLOTS
80 /* The parameters the table carried before the slots, and two per slot. */
81 #define HEALTH_AGGREGATES 7U
82 #define HEALTH_PER_SLOT 2U
83 #define HEALTH_PARAMETERS_MAX (KFSW_PARAM_TABLE_WIRE_MAX / KFSW_PARAM_WIRE_BYTES)
84 #define HEALTH_SLOT_MAX ((HEALTH_PARAMETERS_MAX - HEALTH_AGGREGATES) / HEALTH_PER_SLOT)
85
86 /* A slot's offsets follow from its index alone, so adding or removing another
87 * slot never moves it.
88 */
89 #define HEALTH_SLOT_BASE 0x20U
90 #define HEALTH_SLOT_STRIDE 0x08U
91 #define HEALTH_SLOT_SINCE_OFFSET 0x04U
92 #define HEALTH_SLOT_OFFSET(index) (HEALTH_SLOT_BASE + ((index) * HEALTH_SLOT_STRIDE))
93
94 #define HEALTH_SLOT_COUNT_ONE(slot, index) +1U
95 #define HEALTH_SLOT_COUNT (0U KFSW_HEALTH_SLOTS(HEALTH_SLOT_COUNT_ONE))
96 #define HEALTH_SLOT_PARAMETERS (HEALTH_PER_SLOT * HEALTH_SLOT_COUNT)
97
98 BUILD_ASSERT(HEALTH_SLOT_COUNT <= HEALTH_SLOT_MAX,
99 "table 31 carries at most 14 health slots on a 4800 baud link");
100
101 #define HEALTH_SLOT_CHECKS(slot, index) \
102 BUILD_ASSERT(sizeof(#slot) <= KFSW_HEALTH_NAME_SIZE, "a health slot name is too long"); \
103 BUILD_ASSERT((index) < HEALTH_SLOT_MAX, "a health slot index is 0 to 13");
104
105 KFSW_HEALTH_SLOTS(HEALTH_SLOT_CHECKS)
106
107 /* Each index and each name appears once here, so a repeat is a build error. */
108 #define HEALTH_SLOT_IDS(slot, index) HEALTH_SLOT_INDEX_##index, HEALTH_SLOT_NAME_##slot,
109
110 enum health_slot_ids { KFSW_HEALTH_SLOTS(HEALTH_SLOT_IDS) };
111
112 struct health_slot {
113 const char *name;
114 uint32_t since_ms;
115 uint8_t state;
116 };
117
118 #define HEALTH_SLOT_STORAGE(slot, index) [index] = {.name = #slot},
119
120 static struct health_slot health_slots[HEALTH_SLOT_MAX] = {KFSW_HEALTH_SLOTS(HEALTH_SLOT_STORAGE)};
121
122 /* By name, so the order components registered in never decides the slot. */
123 34 static bool find_component(const char *name, struct kfsw_health_component *component)
124 {
125
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 14 times.
108 for (uint8_t index = 0U; index < KFSW_HEALTH_MAX_COMPONENTS; index++) {
126
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 26 times.
94 if (kfsw_health_get_component(index, component) != 0) {
127 68 continue;
128 }
129
2/3
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
26 if (strncmp(component->name, name, KFSW_HEALTH_NAME_SIZE) == 0) {
130 20 return true;
131 }
132 }
133 14 return false;
134 }
135
136 34 static void sample_slot(struct health_slot *slot)
137 {
138 struct kfsw_health_component component;
139
140
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 20 times.
34 if (!find_component(slot->name, &component)) {
141 14 slot->state = (uint8_t)KFSW_HEALTH_SLOT_ABSENT;
142 14 slot->since_ms = KFSW_HEALTH_SLOT_SINCE_ABSENT;
143 14 return;
144 }
145 20 slot->since_ms = component.since_report_ms;
146
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (component.overdue) {
147 4 slot->state = (uint8_t)KFSW_HEALTH_SLOT_OVERDUE;
148 } else {
149 16 slot->state = (uint8_t)KFSW_HEALTH_SLOT_OK;
150 }
151 }
152
153 /* The table samples into the value it publishes; the slot is the struct holding it. */
154 20 static void sample_slot_state(void *value)
155 {
156 20 sample_slot(CONTAINER_OF(value, struct health_slot, state));
157 20 }
158
159 14 static void sample_slot_since(void *value)
160 {
161 14 sample_slot(CONTAINER_OF(value, struct health_slot, since_ms));
162 14 }
163
164 #define HEALTH_SLOT_STATE(slot, index) \
165 { \
166 .offset = HEALTH_SLOT_OFFSET(index), \
167 .type = KFSW_PARAM_U8, \
168 .flags = KFSW_PARAM_FLAG_READ_ONLY, \
169 .name = "health_" #slot "_state", \
170 .description = "0 ok, 1 overdue, 2 absent", \
171 .value = &health_slots[index].state, \
172 .sample = sample_slot_state, \
173 }
174
175 #define HEALTH_SLOT_SINCE(slot, index) \
176 { \
177 .offset = HEALTH_SLOT_OFFSET(index) + HEALTH_SLOT_SINCE_OFFSET, \
178 .type = KFSW_PARAM_U32, \
179 .flags = KFSW_PARAM_FLAG_READ_ONLY, \
180 .name = "health_" #slot "_since_ms", \
181 .unit = "ms", \
182 .description = "Since it last reported; 4294967295 when absent", \
183 .value = &health_slots[index].since_ms, \
184 .sample = sample_slot_since, \
185 }
186
187 #define HEALTH_SLOT_ROWS(slot, index) \
188 HEALTH_SLOT_STATE(slot, index), HEALTH_SLOT_SINCE(slot, index),
189 #endif
190
191 static const struct kfsw_param_definition health_param_definitions[] = {
192 {
193 .offset = 0x00U,
194 .type = KFSW_PARAM_STRING,
195 .capacity = KFSW_HEALTH_NAME_SIZE,
196 .flags = KFSW_PARAM_FLAG_READ_ONLY,
197 .name = "health_faulted_by",
198 .description = "Component holding the system faulted, or empty",
199 .value = health_faulted_by,
200 .sample = sample_faulted_by,
201 },
202 {
203 .offset = 0x10U,
204 .type = KFSW_PARAM_U32,
205 .flags = KFSW_PARAM_FLAG_READ_ONLY,
206 .name = "health_faults",
207 .description = "Times the system was judged unwell since boot",
208 .value = &health_faults,
209 .sample = sample_faults,
210 },
211 {
212 .offset = 0x14U,
213 .type = KFSW_PARAM_U32,
214 .flags = KFSW_PARAM_FLAG_READ_ONLY,
215 .name = "health_feeds",
216 .description = "Watchdog feeds this service has issued",
217 .value = &health_feeds,
218 .sample = sample_feeds,
219 },
220 {
221 .offset = 0x18U,
222 .type = KFSW_PARAM_U16,
223 .flags = KFSW_PARAM_FLAG_CONFIGURATION,
224 .name = "health_interval_ms",
225 .unit = "ms",
226 .description = "How often deadlines are checked; refused above the feed interval",
227 .value = &health_interval,
228 .default_value = {.u16 = CONFIG_KFSW_HEALTH_INTERVAL_MS},
229 .validate = validate_interval,
230 .changed = apply_interval,
231 .sample = sample_interval,
232 },
233 {
234 .offset = 0x1aU,
235 .type = KFSW_PARAM_U8,
236 .flags = KFSW_PARAM_FLAG_READ_ONLY,
237 .name = "health_state",
238 .description = "ok, faulted or stopped",
239 .value = &health_state,
240 .sample = sample_state,
241 },
242 {
243 .offset = 0x1bU,
244 .type = KFSW_PARAM_U8,
245 .flags = KFSW_PARAM_FLAG_READ_ONLY,
246 .name = "health_feeding",
247 .description = "Whether health is the one feeding the watchdog",
248 .value = &health_feeding,
249 .sample = sample_feeding,
250 },
251 {
252 .offset = 0x1cU,
253 .type = KFSW_PARAM_U8,
254 .flags = KFSW_PARAM_FLAG_READ_ONLY,
255 .name = "health_components",
256 .description = "Components currently watched",
257 .value = &health_count,
258 .sample = sample_count,
259 },
260 #if CONFIG_KFSW_HEALTH_SLOTS
261 KFSW_HEALTH_SLOTS(HEALTH_SLOT_ROWS)
262 #endif
263 };
264
265 KFSW_PARAM_TABLE_FITS_THE_LINK(health_param_definitions);
266
267 #if CONFIG_KFSW_HEALTH_SLOTS
268 BUILD_ASSERT(ARRAY_SIZE(health_param_definitions) == HEALTH_AGGREGATES + HEALTH_SLOT_PARAMETERS,
269 "HEALTH_AGGREGATES must count the parameters ahead of the slots");
270 #endif
271
272 const struct kfsw_param_definition_set kfsw_health_param_definitions = {
273 .table = KFSW_HEALTH_PARAM_TABLE_ID,
274 .name = KFSW_HEALTH_PARAM_TABLE_NAME,
275 .description = "Health monitor and watchdog feeds",
276 .definitions = health_param_definitions,
277 .count = ARRAY_SIZE(health_param_definitions),
278 };
279