./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
39 / 73
53.4%
Branches:
23 / 40
57.5%
Functions:
1 / 4
25.0%

kfsw-services/src/hk/hk_csp.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/byteorder.h>
8 #include <zephyr/sys/util.h>
9
10 #include <csp/csp.h>
11
12 #include <kfsw/comms/csp.h>
13 #include <kfsw/services/hk.h>
14 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HK
15 #include <kfsw/services/log.h>
16
17 #include "hk_internal.h"
18
19 #if CONFIG_KFSW_HK_CSP
20
21 /* Request: version, report, count, then the age to start from.
22 * Report 255 adds an eight-bit class mask and selects across RAM reports.
23 * Reply: one packet per sample, each a complete frame.
24 */
25 #define KFSW_HK_REQUEST_SIZE 5U
26 #define KFSW_HK_POLL_MS 100U
27
28 BUILD_ASSERT(CONFIG_KFSW_HK_SAMPLE_BYTES <= CSP_BUFFER_SIZE,
29 "a housekeeping sample must fit one CSP buffer");
30
31 static csp_socket_t hk_socket;
32 static bool running;
33 static K_MUTEX_DEFINE(start_lock);
34
35 /*
36 * One packet per sample, so a lost packet costs one sample.
37 */
38 30 void kfsw_hk_serve_request(csp_conn_t *connection, csp_packet_t *request)
39 {
40 static struct kfsw_hk_sample sample;
41 uint8_t report;
42 uint8_t wanted;
43 30 uint8_t class_mask = KFSW_HK_CLASS_MASK_ALL;
44 uint16_t first_age;
45 30 uint16_t depth = 0U;
46 30 uint16_t sent = 0U;
47
48
3/4
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 28 times.
30 if (!kfsw_hk_is_ready() || request->length < KFSW_HK_REQUEST_SIZE) {
49 2 csp_buffer_free(request);
50 8 return;
51 }
52
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26 times.
28 if (request->data[0] != KFSW_HK_PROTOCOL_VERSION) {
53 2 kfsw_log_warning("HK: request version %u is not %u", request->data[0],
54 KFSW_HK_PROTOCOL_VERSION);
55 2 csp_buffer_free(request);
56 2 return;
57 }
58
59 26 report = request->data[1];
60 26 wanted = request->data[2];
61 26 first_age = sys_get_be16(&request->data[3]);
62
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
26 if (report == KFSW_HK_REPORT_ALL) {
63
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (request->length != KFSW_HK_REQUEST_SIZE + 1U) {
64 2 csp_buffer_free(request);
65 2 return;
66 }
67 4 class_mask = request->data[5];
68 }
69 24 csp_buffer_free(request);
70
71
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (report == KFSW_HK_REPORT_ALL) {
72 4 depth = CONFIG_KFSW_HK_REPORTS * CONFIG_KFSW_HK_HISTORY;
73
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18 times.
20 } else if (kfsw_hk_depth(report, &depth) != 0) {
74 2 return;
75 }
76
77
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 6 times.
46 for (uint16_t offset = 0U; (offset < wanted) && ((first_age + offset) < depth); offset++) {
78 csp_packet_t *reply;
79
80 32 int result = report == KFSW_HK_REPORT_ALL
81 10 ? kfsw_hk_get_selected(class_mask,
82 10 (uint16_t)(first_age + offset), &sample)
83
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 22 times.
32 : kfsw_hk_get(report, (uint16_t)(first_age + offset), &sample);
84
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
32 if (result != 0) {
85 4 break;
86 }
87
88 28 reply = csp_buffer_get(sample.length);
89
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (reply == NULL) {
90 /* No buffer: skip this sample instead of waiting. */
91 4 kfsw_log_warning("HK: no buffer for report %u, %u sent", report, sent);
92 4 break;
93 }
94 24 memcpy(reply->data, sample.data, sample.length);
95 24 reply->length = sample.length;
96 24 csp_send(connection, reply);
97 24 sent++;
98 }
99 }
100
101 ✗ static void hk_server(void *arg1, void *arg2, void *arg3)
102 {
103 ARG_UNUSED(arg1);
104 ARG_UNUSED(arg2);
105 ARG_UNUSED(arg3);
106
107 ✗ while (true) {
108 ✗ csp_conn_t *connection = csp_accept(&hk_socket, KFSW_HK_POLL_MS);
109 csp_packet_t *request;
110
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
112 ✗ continue;
113 }
114 ✗ request = csp_read(connection, KFSW_HK_POLL_MS);
115
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (request != NULL) {
116 ✗ kfsw_hk_serve_request(connection, request);
117 }
118 ✗ (void)csp_close(connection);
119 }
120 }
121
122 K_THREAD_DEFINE(kfsw_hk_server_thread, CONFIG_KFSW_HK_SERVER_STACK_SIZE, hk_server, NULL, NULL,
123 NULL, CONFIG_KFSW_HK_SERVER_PRIORITY, 0, SYS_FOREVER_MS);
124
125 /* Bound here so a port in use is reported to the caller. */
126 ✗ static int server_start(void)
127 {
128 struct kfsw_csp_info csp_info;
129 int result;
130
131
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (running) {
132 ✗ return 0;
133 }
134 ✗ kfsw_csp_get_info(&csp_info);
135
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ if (!kfsw_hk_is_ready() || !csp_info.initialized || !csp_info.router_running) {
136 ✗ return -EACCES;
137 }
138
139 ✗ memset(&hk_socket, 0, sizeof(hk_socket));
140 ✗ hk_socket.opts = CSP_SO_CRC32REQ;
141 ✗ result = csp_listen(&hk_socket, 1U);
142
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != CSP_ERR_NONE) {
143 ✗ (void)csp_socket_close(&hk_socket);
144 ✗ return -EIO;
145 }
146 ✗ result = csp_bind(&hk_socket, CONFIG_KFSW_HK_CSP_PORT);
147
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != CSP_ERR_NONE) {
148 ✗ (void)csp_socket_close(&hk_socket);
149 ✗ return -EADDRINUSE;
150 }
151
152 ✗ running = true;
153 ✗ k_thread_start(kfsw_hk_server_thread);
154 ✗ kfsw_log_info("HK: serving on CSP port %d", CONFIG_KFSW_HK_CSP_PORT);
155 ✗ return 0;
156 }
157
158 ✗ int kfsw_hk_server_start(void)
159 {
160 int result;
161
162 ✗ k_mutex_lock(&start_lock, K_FOREVER);
163 ✗ result = server_start();
164 ✗ k_mutex_unlock(&start_lock);
165 ✗ return result;
166 }
167
168 #endif /* CONFIG_KFSW_HK_CSP */
169