./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
285 / 329
86.6%
Branches:
141 / 195
72.3%
Functions:
22 / 22
100.0%

kfsw-services/src/fbo/fbo_schedule.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/crc.h>
11 #include <zephyr/sys/util.h>
12
13 #include <kfsw/platform/time.h>
14 #include <kfsw/services/command.h>
15 #include <kfsw/services/fbo.h>
16 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FBO
17 #include <kfsw/services/log.h>
18
19 #include "fbo_internal.h"
20
21 /*
22 * Time-tagged releases. The queue lives in RAM only: a reset clears it,
23 * because a queue that outlives a reset while its clock may have jumped fires
24 * at the wrong time with nobody watching.
25 *
26 * A relative entry is due against the monotonic clock, so it is right on a
27 * node that was never told the time and a wall clock step does not move it. An
28 * absolute entry is due against the wall clock and needs it to have been set.
29 */
30
31 /** Longest a relative entry may be put off: a week. */
32 #define KFSW_FBO_SCHEDULE_DELAY_MAX_S 604800
33 /** Matches the Kconfig range, so a value accepted here is one it could be built with. */
34 #define KFSW_FBO_SCHEDULE_LATENCY_MAX_S 3600U
35
36 struct slot {
37 struct kfsw_fbo_schedule_entry entry;
38 /** Relative entry: monotonic millisecond it is due. */
39 int64_t due_uptime_ms;
40 bool used;
41 };
42
43 static struct slot slots[CONFIG_KFSW_FBO_SCHEDULE_ENTRIES];
44 static K_MUTEX_DEFINE(queue_lock);
45 static K_SEM_DEFINE(wake, 0, 1);
46
47 static uint32_t latency_s = CONFIG_KFSW_FBO_SCHEDULE_LATENCY_S;
48 static uint32_t releases;
49 static uint32_t refusals;
50 static uint32_t overdues;
51 static uint32_t clock_steps;
52 static int last_error;
53
54 /* One add at a time under the queue lock, and one release at a time on the
55 * release thread, so each parse has its own storage instead of a stack copy.
56 */
57 static char add_text[KFSW_COMMAND_MAX_ARGS][KFSW_COMMAND_MAX_TEXT_SIZE + 1U];
58 static char release_text[KFSW_COMMAND_MAX_ARGS][KFSW_COMMAND_MAX_TEXT_SIZE + 1U];
59
60 118 static size_t tokenise(char *text, char **tokens, size_t max)
61 {
62 118 size_t count = 0U;
63 118 char *cursor = text;
64
65
3/4
✓ Branch 0 taken 242 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 118 times.
242 while ((count < max) && (*cursor != '\0')) {
66
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 124 times.
124 while ((*cursor == ' ') || (*cursor == '\t')) {
67 ✗ cursor++;
68 }
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (*cursor == '\0') {
70 ✗ break;
71 }
72 124 tokens[count++] = cursor;
73
5/6
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 616 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 616 times.
✗ Branch 5 not taken.
740 while ((*cursor != '\0') && (*cursor != ' ') && (*cursor != '\t')) {
74 616 cursor++;
75 }
76
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 118 times.
124 if (*cursor != '\0') {
77 6 *cursor = '\0';
78 6 cursor++;
79 }
80 }
81 118 return count;
82 }
83
84 /*
85 * Resolve an entry's text into a command and its arguments. Used at add, so a
86 * bad entry is refused while an operator is listening, and again at release,
87 * where the text is the record of what was asked for.
88 */
89 118 static int build(char *text, char storage[][KFSW_COMMAND_MAX_TEXT_SIZE + 1U],
90 struct kfsw_command_info *info, struct kfsw_command_arg *args, size_t *count)
91 {
92 char *tokens[KFSW_COMMAND_MAX_ARGS + 2U];
93 118 size_t words = tokenise(text, tokens, ARRAY_SIZE(tokens));
94 int outcome;
95
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (words == 0U) {
97 ✗ return -EINVAL;
98 }
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (words > (KFSW_COMMAND_MAX_ARGS + 1U)) {
100 ✗ return -E2BIG;
101 }
102 118 outcome = kfsw_command_find(tokens[0], info);
103
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 116 times.
118 if (outcome != 0) {
104 2 return outcome;
105 }
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if ((words - 1U) != info->arg_count) {
107 ✗ return -EINVAL;
108 }
109
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 116 times.
122 for (size_t index = 0U; index < info->arg_count; index++) {
110
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if (strlen(tokens[index + 1U]) > KFSW_COMMAND_MAX_TEXT_SIZE) {
111 ✗ return -EINVAL;
112 }
113 6 strcpy(storage[index], tokens[index + 1U]);
114 6 outcome = kfsw_command_parse_arg(storage[index], info->arg_types[index],
115 6 &args[index]);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (outcome != 0) {
117 ✗ return outcome;
118 }
119 }
120 116 *count = info->arg_count;
121 116 return 0;
122 }
123
124 /*
125 * CRC32 over what an upload asked for: the kind, the time, the node and the
126 * text. Two adds of the same content give the same hash, which is what makes
127 * an upload idempotent and a partial one visible.
128 */
129 106 static uint32_t content_hash(uint8_t kind, int64_t when, uint16_t node, const char *line)
130 {
131 uint8_t header[13];
132
133 106 header[0] = kind;
134 106 sys_put_be64((uint64_t)when, &header[1]);
135 106 sys_put_be16(node, &header[9]);
136 106 sys_put_be16(0U, &header[11]);
137 106 return crc32_ieee_update(crc32_ieee(header, sizeof(header)), (const uint8_t *)line,
138 strlen(line));
139 }
140
141 /* Hash of the queue content, slot order, so the ground can address it. */
142 110 static uint32_t queue_hash(void)
143 {
144 110 uint32_t hash = 0U;
145 uint8_t word[4];
146
147
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 110 times.
750 for (size_t index = 0U; index < ARRAY_SIZE(slots); index++) {
148
2/2
✓ Branch 0 taken 530 times.
✓ Branch 1 taken 110 times.
640 if (!slots[index].used) {
149 530 continue;
150 }
151 110 sys_put_be32(slots[index].entry.content_hash, word);
152 110 hash = crc32_ieee_update(hash, word, sizeof(word));
153 }
154 110 return hash;
155 }
156
157 442 static bool live(const struct slot *slot)
158 {
159
3/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
442 return slot->used && ((slot->entry.status == KFSW_FBO_ENTRY_SCHEDULED) ||
160
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (slot->entry.status == KFSW_FBO_ENTRY_RUNNING));
161 }
162
163 26 static uint32_t latency_floor_s(void)
164 {
165 #if CONFIG_KFSW_COMMAND_CSP
166 /* A release waits its turn behind a command in flight; a buffer shorter
167 * than that reply timeout would report an overdue that never happened.
168 */
169 26 return DIV_ROUND_UP(kfsw_command_get_timeout_ms(), 1000U);
170 #else
171 ✗ return 1U;
172 #endif
173 }
174
175 20 uint32_t kfsw_fbo_schedule_get_latency_s(void)
176 {
177 20 return latency_s;
178 }
179
180 26 int kfsw_fbo_schedule_check_latency_s(uint32_t seconds)
181 {
182
3/4
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
26 if ((seconds < latency_floor_s()) || (seconds > KFSW_FBO_SCHEDULE_LATENCY_MAX_S)) {
183 4 return -ERANGE;
184 }
185 22 return 0;
186 }
187
188 6 int kfsw_fbo_schedule_set_latency_s(uint32_t seconds)
189 {
190 6 int result = kfsw_fbo_schedule_check_latency_s(seconds);
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (result != 0) {
193 ✗ return result;
194 }
195 6 latency_s = seconds;
196 6 return 0;
197 }
198
199 112 int kfsw_fbo_schedule_add(enum kfsw_fbo_time_kind kind, int64_t when, uint16_t node,
200 const char *line, uint16_t *index)
201 {
202 struct kfsw_command_arg args[KFSW_COMMAND_MAX_ARGS];
203 struct kfsw_command_info info;
204 char text[KFSW_FBO_SCHEDULE_LINE_MAX];
205 112 size_t count = 0U;
206 112 size_t free_slot = ARRAY_SIZE(slots);
207 112 int64_t now_utc = 0;
208 uint32_t hash;
209 int outcome;
210
211
4/8
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 60 times.
172 if ((line == NULL) || (index == NULL) || (line[0] == '\0') ||
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
112 (memchr(line, '\0', sizeof(text)) == NULL)) {
213 ✗ return -EINVAL;
214 }
215
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
112 if ((kind != KFSW_FBO_TIME_RELATIVE) && (kind != KFSW_FBO_TIME_ABSOLUTE)) {
216 ✗ return -EINVAL;
217 }
218
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 4 times.
112 if (kind == KFSW_FBO_TIME_RELATIVE) {
219
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
108 if ((when <= 0) || (when > KFSW_FBO_SCHEDULE_DELAY_MAX_S)) {
220 ✗ return -EINVAL;
221 }
222 } else {
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (when <= 0) {
224 ✗ return -EINVAL;
225 }
226 /* The gate: with nothing having set the clock this power cycle,
227 * an absolute entry has no time to be due against.
228 */
229
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 if (kfsw_fbo_schedule_clock_seconds(&now_utc) != 0) {
230 2 refusals++;
231 2 kfsw_log_warning("FBO: absolute entry refused, the clock is not set");
232 2 return -ENODATA;
233 }
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((now_utc - when) > (int64_t)latency_s) {
235 ✗ refusals++;
236 ✗ return -ETIME;
237 }
238 }
239 #if !CONFIG_KFSW_COMMAND_CSP
240
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 108 times.
110 if (node != 0U) {
241 2 return -ENOTSUP;
242 }
243 #endif
244
245 108 strcpy(text, line);
246 108 k_mutex_lock(&queue_lock, K_FOREVER);
247 108 outcome = build(text, add_text, &info, args, &count);
248
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 106 times.
108 if (outcome != 0) {
249 2 refusals++;
250 2 k_mutex_unlock(&queue_lock);
251 2 return outcome;
252 }
253 106 hash = content_hash((uint8_t)kind, when, node, line);
254
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 68 times.
510 for (size_t slot = 0U; slot < ARRAY_SIZE(slots); slot++) {
255
4/4
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 384 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 20 times.
442 if (live(&slots[slot]) && (slots[slot].entry.content_hash == hash)) {
256 38 *index = (uint16_t)slot;
257 38 k_mutex_unlock(&queue_lock);
258 38 return -EEXIST;
259 }
260
4/4
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 318 times.
404 if (!slots[slot].used && (free_slot == ARRAY_SIZE(slots))) {
261 66 free_slot = slot;
262 }
263 }
264
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 66 times.
68 if (free_slot == ARRAY_SIZE(slots)) {
265 2 refusals++;
266 2 k_mutex_unlock(&queue_lock);
267 2 return -ENOSPC;
268 }
269
270 66 struct slot *target = &slots[free_slot];
271
272 66 memset(target, 0, sizeof(*target));
273 66 strcpy(target->entry.line, line);
274 66 target->entry.index = (uint16_t)free_slot;
275 66 target->entry.node = node;
276 66 target->entry.command_id = info.id;
277 66 target->entry.kind = (uint8_t)kind;
278 66 target->entry.status = KFSW_FBO_ENTRY_SCHEDULED;
279 66 target->entry.content_hash = hash;
280
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2 times.
66 if (kind == KFSW_FBO_TIME_RELATIVE) {
281 64 target->entry.delay_s = (uint32_t)when;
282 64 target->due_uptime_ms = (int64_t)kfsw_time_monotonic_ms() + (when * 1000);
283 } else {
284 2 target->entry.due_utc = when;
285 }
286 66 target->used = true;
287 66 *index = (uint16_t)free_slot;
288 66 k_mutex_unlock(&queue_lock);
289
290 66 k_sem_give(&wake);
291 66 kfsw_log_info("FBO: entry %u scheduled, %s, queue 0x%08x", *index, line, queue_hash());
292 66 return 0;
293 }
294
295 8 int kfsw_fbo_schedule_cancel(uint16_t index)
296 {
297 8 int outcome = 0;
298
299
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (index >= ARRAY_SIZE(slots)) {
300 2 return -ENOENT;
301 }
302 6 k_mutex_lock(&queue_lock, K_FOREVER);
303
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!slots[index].used) {
304 2 outcome = -ENOENT;
305
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } else if (slots[index].entry.status != KFSW_FBO_ENTRY_SCHEDULED) {
306 /* Never claim to have stopped something that already fired. */
307 2 outcome = -EALREADY;
308 } else {
309 2 slots[index].entry.status = KFSW_FBO_ENTRY_CANCELLED;
310 }
311 6 k_mutex_unlock(&queue_lock);
312 6 return outcome;
313 }
314
315 102 int kfsw_fbo_schedule_clear(void)
316 {
317 102 int dropped = 0;
318
319 102 k_mutex_lock(&queue_lock, K_FOREVER);
320
2/2
✓ Branch 0 taken 686 times.
✓ Branch 1 taken 102 times.
788 for (size_t index = 0U; index < ARRAY_SIZE(slots); index++) {
321
2/2
✓ Branch 0 taken 626 times.
✓ Branch 1 taken 60 times.
686 if (!slots[index].used) {
322 626 continue;
323 }
324 /* A release in flight keeps its slot until it reports. */
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (slots[index].entry.status == KFSW_FBO_ENTRY_RUNNING) {
326 ✗ continue;
327 }
328 60 memset(&slots[index], 0, sizeof(slots[index]));
329 60 dropped++;
330 }
331 102 k_mutex_unlock(&queue_lock);
332 102 return dropped;
333 }
334
335 /* Seconds still to wait, 0 once due or once the entry is no longer waiting. */
336 36 static uint32_t remaining(const struct slot *slot, int64_t mono_ms, bool utc_ok, int64_t utc)
337 {
338 int64_t left;
339
340
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
36 if (slot->entry.status != KFSW_FBO_ENTRY_SCHEDULED) {
341 6 return 0U;
342 }
343
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (slot->entry.kind == KFSW_FBO_TIME_RELATIVE) {
344 30 left = DIV_ROUND_UP(slot->due_uptime_ms - mono_ms, 1000);
345
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (utc_ok) {
346 ✗ left = slot->entry.due_utc - utc;
347 } else {
348 ✗ return 0U;
349 }
350
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 return (left > 0) ? (uint32_t)left : 0U;
351 }
352
353 10 int kfsw_fbo_schedule_get_entry(uint16_t index, struct kfsw_fbo_schedule_entry *entry)
354 {
355 10 int64_t utc = 0;
356 bool utc_ok;
357
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (entry == NULL) {
359 ✗ return -EINVAL;
360 }
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (index >= ARRAY_SIZE(slots)) {
362 ✗ return -ENOENT;
363 }
364 10 utc_ok = kfsw_fbo_schedule_clock_seconds(&utc) == 0;
365 10 k_mutex_lock(&queue_lock, K_FOREVER);
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!slots[index].used) {
367 ✗ k_mutex_unlock(&queue_lock);
368 ✗ return -ENOENT;
369 }
370 10 *entry = slots[index].entry;
371 10 entry->remaining_s =
372 10 remaining(&slots[index], (int64_t)kfsw_time_monotonic_ms(), utc_ok, utc);
373 10 k_mutex_unlock(&queue_lock);
374 10 return 0;
375 }
376
377 44 int kfsw_fbo_schedule_get_status(struct kfsw_fbo_schedule_status *status)
378 {
379 44 int64_t utc = 0;
380 int64_t mono;
381 bool utc_ok;
382
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (status == NULL) {
384 ✗ return -EINVAL;
385 }
386 44 utc_ok = kfsw_fbo_schedule_clock_seconds(&utc) == 0;
387 44 mono = (int64_t)kfsw_time_monotonic_ms();
388 44 memset(status, 0, sizeof(*status));
389 44 k_mutex_lock(&queue_lock, K_FOREVER);
390 44 status->capacity = (uint16_t)ARRAY_SIZE(slots);
391 44 status->latency_s = latency_s;
392 44 status->releases = releases;
393 44 status->refusals = refusals;
394 44 status->overdues = overdues;
395 44 status->clock_steps = clock_steps;
396 44 status->last_error = last_error;
397 44 status->clock_set = utc_ok;
398 44 status->hash = queue_hash();
399
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 44 times.
286 for (size_t index = 0U; index < ARRAY_SIZE(slots); index++) {
400 242 const struct slot *slot = &slots[index];
401
402
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 30 times.
242 if (!slot->used) {
403 212 continue;
404 }
405 30 status->entries++;
406
2/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
30 switch (slot->entry.status) {
407 28 case KFSW_FBO_ENTRY_SCHEDULED:
408 28 status->scheduled++;
409
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26 times.
28 if (slot->entry.kind == KFSW_FBO_TIME_ABSOLUTE) {
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((status->next_due_utc == 0) ||
411
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (slot->entry.due_utc < status->next_due_utc)) {
412 2 status->next_due_utc = slot->entry.due_utc;
413 }
414 } else {
415 26 uint32_t left = remaining(slot, mono, utc_ok, utc);
416
417
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
26 if ((status->next_due_s == 0U) || (left < status->next_due_s)) {
418 14 status->next_due_s = left;
419 }
420 }
421 28 break;
422 ✗ case KFSW_FBO_ENTRY_RUNNING:
423 ✗ status->running++;
424 ✗ break;
425 ✗ case KFSW_FBO_ENTRY_COMPLETED:
426 ✗ status->completed++;
427 ✗ break;
428 ✗ case KFSW_FBO_ENTRY_FAILED:
429 ✗ status->failed++;
430 ✗ break;
431 2 case KFSW_FBO_ENTRY_OVERDUE:
432 2 status->overdue++;
433 2 break;
434 ✗ default:
435 ✗ status->cancelled++;
436 ✗ break;
437 }
438 }
439 44 k_mutex_unlock(&queue_lock);
440 44 return 0;
441 }
442
443 /* Run one entry's command. The queue lock is not held: a handler may block. */
444 10 static void invoke(struct kfsw_fbo_schedule_entry *entry, int *outcome,
445 struct kfsw_command_result *result)
446 {
447 struct kfsw_command_arg args[KFSW_COMMAND_MAX_ARGS];
448 struct kfsw_command_info info;
449 char text[KFSW_FBO_SCHEDULE_LINE_MAX];
450 10 size_t count = 0U;
451
452 10 strcpy(text, entry->line);
453 10 *outcome = build(text, release_text, &info, args, &count);
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (*outcome != 0) {
455 10 return;
456 }
457
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (entry->node == 0U) {
458 10 *outcome = kfsw_command_invoke(info.name, args, count, result);
459 10 return;
460 }
461 #if CONFIG_KFSW_COMMAND_RETRY && CONFIG_KFSW_COMMAND_CSP
462 /* A ticket, so a link retry cannot execute the release twice. */
463 *outcome = kfsw_command_invoke_remote_retry(entry->node, info.name, args, count, result);
464 #elif CONFIG_KFSW_COMMAND_CSP
465 ✗ *outcome = kfsw_command_invoke_remote(entry->node, info.name, args, count, result);
466 #else
467 ✗ *outcome = -ENOTSUP;
468 #endif
469 }
470
471 10 static void release(size_t index, bool utc_ok, int64_t utc)
472 {
473 10 struct kfsw_command_result result = {0};
474 struct kfsw_fbo_schedule_entry entry;
475 bool failed;
476 int outcome;
477
478 10 k_mutex_lock(&queue_lock, K_FOREVER);
479 10 slots[index].entry.status = KFSW_FBO_ENTRY_RUNNING;
480
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 slots[index].entry.released_utc = utc_ok ? utc : 0;
481
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (releases < UINT32_MAX) {
482 10 releases++;
483 }
484 10 entry = slots[index].entry;
485 10 k_mutex_unlock(&queue_lock);
486
487 10 kfsw_fbo_note(KFSW_FBO_EVENT_RELEASED, KFSW_EVENT_INFO, (uint16_t)index);
488 10 invoke(&entry, &outcome, &result);
489
490 10 k_mutex_lock(&queue_lock, K_FOREVER);
491 10 slots[index].entry.result = outcome;
492 10 slots[index].entry.command_status = (uint8_t)result.status;
493
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 failed = (outcome != 0) || (result.status != KFSW_COMMAND_OK);
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (failed) {
495 ✗ slots[index].entry.status = KFSW_FBO_ENTRY_FAILED;
496 ✗ last_error = outcome;
497 } else {
498 10 slots[index].entry.status = KFSW_FBO_ENTRY_COMPLETED;
499 }
500 10 k_mutex_unlock(&queue_lock);
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (failed) {
502 ✗ kfsw_log_error("FBO: entry %u released %s and failed (%d)", (unsigned int)index,
503 entry.line, outcome);
504 } else {
505 10 kfsw_log_info("FBO: entry %u released %s", (unsigned int)index, entry.line);
506 }
507 10 }
508
509 /* Mark anything later than the buffer overdue, and report the earliest due. */
510 66 static bool due_now(const struct slot *slot, int64_t mono_ms, bool utc_ok, int64_t utc,
511 int64_t *lateness_s)
512 {
513
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2 times.
66 if (slot->entry.kind == KFSW_FBO_TIME_RELATIVE) {
514
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 if (mono_ms < slot->due_uptime_ms) {
515 48 return false;
516 }
517 16 *lateness_s = (mono_ms - slot->due_uptime_ms) / 1000;
518 16 return true;
519 }
520
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!utc_ok || (utc < slot->entry.due_utc)) {
521 ✗ return false;
522 }
523 2 *lateness_s = utc - slot->entry.due_utc;
524 2 return true;
525 }
526
527 102 static void sweep(bool utc_ok, int64_t utc)
528 {
529 102 int64_t mono = (int64_t)kfsw_time_monotonic_ms();
530 102 unsigned int released = 0U;
531
532
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 10 times.
112 while (released < CONFIG_KFSW_FBO_SCHEDULE_BURST) {
533 102 size_t candidate = ARRAY_SIZE(slots);
534 102 int64_t earliest = 0;
535
536 102 k_mutex_lock(&queue_lock, K_FOREVER);
537
2/2
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 102 times.
468 for (size_t index = 0U; index < ARRAY_SIZE(slots); index++) {
538 366 struct slot *slot = &slots[index];
539 366 int64_t lateness = 0;
540
541
4/4
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 212 times.
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 66 times.
366 if (!slot->used || (slot->entry.status != KFSW_FBO_ENTRY_SCHEDULED)) {
542 350 continue;
543 }
544
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 18 times.
66 if (!due_now(slot, mono, utc_ok, utc, &lateness)) {
545 48 continue;
546 }
547
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (lateness > (int64_t)latency_s) {
548 2 slot->entry.status = KFSW_FBO_ENTRY_OVERDUE;
549 2 slot->entry.result = -ETIME;
550
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (overdues < UINT32_MAX) {
551 2 overdues++;
552 }
553 2 kfsw_log_warning("FBO: entry %u is %lld s late and will not run",
554 (unsigned int)index, (long long)lateness);
555 2 kfsw_fbo_note(KFSW_FBO_EVENT_OVERDUE, KFSW_EVENT_ERROR,
556 2 (uint16_t)index);
557 2 continue;
558 }
559 /* Earliest due first, so release order follows time order. */
560
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
16 if ((candidate == ARRAY_SIZE(slots)) || (lateness > earliest)) {
561 10 candidate = index;
562 10 earliest = lateness;
563 }
564 }
565 102 k_mutex_unlock(&queue_lock);
566
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 10 times.
102 if (candidate == ARRAY_SIZE(slots)) {
567 92 return;
568 }
569 10 release(candidate, utc_ok, utc);
570 10 released++;
571 }
572 }
573
574 /*
575 * A step is the wall clock disagreeing with the monotonic clock. It is counted
576 * and reported; it changes no entry's status by itself, and relative entries
577 * cannot be affected by one at all.
578 */
579 102 static void check_step(bool utc_ok, int64_t utc, int64_t mono)
580 {
581 static int64_t last_utc;
582 static int64_t last_mono;
583 static bool have_last;
584 int64_t drift;
585
586
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 24 times.
102 if (!utc_ok) {
587 78 have_last = false;
588 78 return;
589 }
590
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
24 if (have_last) {
591 22 drift = ((utc - last_utc) * 1000) - (mono - last_mono);
592
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 18 times.
22 if ((drift > CONFIG_KFSW_FBO_SCHEDULE_STEP_MS) ||
593 (drift < -CONFIG_KFSW_FBO_SCHEDULE_STEP_MS)) {
594
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (clock_steps < UINT32_MAX) {
595 4 clock_steps++;
596 }
597 4 kfsw_log_warning("FBO: wall clock stepped %lld ms", (long long)drift);
598 4 kfsw_fbo_note(KFSW_FBO_EVENT_CLOCK_STEP, KFSW_EVENT_WARNING,
599 4 (uint16_t)clock_steps);
600 }
601 }
602 24 last_utc = utc;
603 24 last_mono = mono;
604 24 have_last = true;
605 }
606
607 6 static void schedule_thread(void *arg1, void *arg2, void *arg3)
608 {
609 ARG_UNUSED(arg1);
610 ARG_UNUSED(arg2);
611 ARG_UNUSED(arg3);
612
613 102 while (true) {
614 108 int64_t utc = 0;
615 bool utc_ok;
616
617 108 (void)k_sem_take(&wake, K_MSEC(CONFIG_KFSW_FBO_SCHEDULE_TICK_MS));
618 102 utc_ok = kfsw_fbo_schedule_clock_seconds(&utc) == 0;
619 102 check_step(utc_ok, utc, (int64_t)kfsw_time_monotonic_ms());
620 102 sweep(utc_ok, utc);
621 }
622 }
623
624 K_THREAD_DEFINE(kfsw_fbo_schedule_thread, CONFIG_KFSW_FBO_SCHEDULE_STACK_SIZE, schedule_thread,
625 NULL, NULL, NULL, CONFIG_KFSW_FBO_SCHEDULE_PRIORITY, 0, SYS_FOREVER_MS);
626
627 8 void kfsw_fbo_schedule_init(void)
628 {
629 8 k_thread_start(kfsw_fbo_schedule_thread);
630 8 }
631