./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
27 / 28
96.4%
Branches:
9 / 14
64.3%
Functions:
3 / 3
100.0%

k-fsw/app/src/csp_liveness.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4
5 #include <zephyr/kernel.h>
6
7 #include <kfsw/comms/csp.h>
8 #include <kfsw/services/health.h>
9 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HEALTH
10 #include <kfsw/services/log.h>
11
12 #include "csp_liveness.h"
13
14 /*
15 * The application loop reporting to health only proves the scheduler runs. A
16 * node whose router or connection pool has stopped serving still reports, and
17 * the watchdog keeps being fed while the ground cannot reach it.
18 *
19 * This asks the router to carry a packet to this node's own address. It needs no
20 * wire and no peer, and it fails when the machinery an operator depends on has
21 * stopped, including when another thread is holding a connection it will never
22 * release.
23 */
24
25 /* The probe is the only reason this file is compiled, so a composition that
26 * enables it without a router has nothing to probe. The unit suite replaces
27 * the router instead of bringing it up.
28 */
29 BUILD_ASSERT(IS_ENABLED(CONFIG_KFSW_CSP) || IS_ENABLED(CONFIG_ZTEST),
30 "KFSW_CSP_LIVENESS needs KFSW_CSP");
31
32 #define LIVENESS_PAYLOAD_SIZE 1U
33
34 static uint8_t liveness_handle;
35 static bool registered;
36 static uint32_t probes;
37 static uint32_t failures;
38
39 static void liveness_work_handler(struct k_work *work);
40 static K_WORK_DELAYABLE_DEFINE(liveness_work, liveness_work_handler);
41
42 18 static void liveness_work_handler(struct k_work *work)
43 {
44 struct kfsw_csp_info info;
45 uint32_t round_trip_us;
46 int result;
47
48 ARG_UNUSED(work);
49
50 18 kfsw_csp_get_info(&info);
51 18 result = kfsw_csp_ping(info.address, CONFIG_KFSW_CSP_LIVENESS_TIMEOUT_MS,
52 LIVENESS_PAYLOAD_SIZE, &round_trip_us);
53
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (probes < UINT32_MAX) {
54 18 probes++;
55 }
56
57
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (result == 0) {
58 8 (void)kfsw_health_report(liveness_handle);
59 } else {
60
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (failures < UINT32_MAX) {
61 10 failures++;
62 }
63 /* Not reporting is the point: health stops feeding the watchdog
64 * when the deadline passes.
65 */
66 10 kfsw_log_warning("CSP liveness probe failed (%d)", result);
67 }
68
69 18 (void)k_work_reschedule(&liveness_work, K_MSEC(CONFIG_KFSW_CSP_LIVENESS_PERIOD_MS));
70 18 }
71
72 4 int kfsw_csp_liveness_start(void)
73 {
74 int result;
75
76
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (registered) {
77 2 return -EALREADY;
78 }
79 result =
80 2 kfsw_health_register("csp", CONFIG_KFSW_CSP_LIVENESS_DEADLINE_MS, &liveness_handle);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
82 ✗ return result;
83 }
84 2 registered = true;
85
86 /* Reported once up front, so the first period cannot miss the deadline. */
87 2 (void)kfsw_health_report(liveness_handle);
88 2 (void)k_work_reschedule(&liveness_work, K_MSEC(CONFIG_KFSW_CSP_LIVENESS_PERIOD_MS));
89 2 return 0;
90 }
91
92 4 void kfsw_csp_liveness_get_counters(uint32_t *probe_count, uint32_t *failure_count)
93 {
94
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (probe_count != NULL) {
95 4 *probe_count = probes;
96 }
97
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (failure_count != NULL) {
98 4 *failure_count = failures;
99 }
100 4 }
101