./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
0 / 168
0.0%
Branches:
0 / 108
0.0%
Functions:
0 / 9
0.0%

kfsw-services/src/command/command_csp.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/atomic.h>
9
10 #include <csp/csp.h>
11 #include <csp/csp_buffer.h>
12
13 #include <kfsw/comms/csp.h>
14
15 #include "command_internal.h"
16 #if CONFIG_KFSW_COMMAND_RETRY
17 #include "command_retry.h"
18 static struct kfsw_command_retry_cache retry_cache;
19 #endif
20
21 /* Remote commands: one request per connection, handlers run on this thread. */
22
23 #define KFSW_COMMAND_POLL_MS 100U
24
25 BUILD_ASSERT(KFSW_COMMAND_RETRY_HEADER_SIZE + KFSW_COMMAND_MAX_PAYLOAD_SIZE <= CSP_BUFFER_SIZE,
26 "One command message must fit in one CSP packet");
27
28 static csp_socket_t command_socket;
29 static atomic_t server_started;
30 static bool thread_started;
31
32 ✗ static void send_result(csp_conn_t *connection, const struct kfsw_command_message *request,
33 uint64_t token, const struct kfsw_command_result *result)
34 {
35 uint8_t buffer[KFSW_COMMAND_RETRY_HEADER_SIZE + KFSW_COMMAND_MAX_PAYLOAD_SIZE];
36 ✗ struct kfsw_command_message message = {
37 ✗ .version = request->version,
38 .token = token,
39
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ .opcode = (request->opcode == KFSW_COMMAND_OP_PREPARE) ? KFSW_COMMAND_OP_TICKET
40 : KFSW_COMMAND_OP_RESULT,
41 ✗ .status = (uint8_t)result->status,
42 ✗ .command_id = request->command_id,
43 ✗ .request_id = request->request_id,
44 };
45 csp_packet_t *packet;
46 size_t detail_size;
47 size_t encoded_size;
48
49 ✗ detail_size = strnlen(result->detail, sizeof(result->detail));
50 ✗ message.payload_size = (uint16_t)detail_size;
51 ✗ message.payload = (const uint8_t *)result->detail;
52
53
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_command_protocol_encode(buffer, sizeof(buffer), &message, &encoded_size) != 0) {
54 ✗ return;
55 }
56 ✗ packet = csp_buffer_get(encoded_size);
57
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
58 ✗ return;
59 }
60 ✗ memcpy(packet->data, buffer, encoded_size);
61 ✗ packet->length = encoded_size;
62 /* csp_send() frees the packet, even when sending fails. */
63 ✗ csp_send(connection, packet);
64 }
65
66 ✗ static void serve_request(csp_conn_t *connection, uint16_t source_node)
67 {
68 char text_storage[KFSW_COMMAND_MAX_ARGS][KFSW_COMMAND_MAX_TEXT_SIZE + 1U];
69 struct kfsw_command_arg args[KFSW_COMMAND_MAX_ARGS];
70 ✗ struct kfsw_command_message request = {0};
71 struct kfsw_command_result result;
72 ✗ struct kfsw_command_source source = {
73 .node = source_node,
74 .via_csp = true,
75 /* Link protection does not provide a command-level identity here. */
76 .authenticated = false,
77 };
78 csp_packet_t *packet;
79 int decoded;
80
81 ✗ packet = csp_read(connection, kfsw_command_get_timeout_ms());
82
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
83 ✗ return;
84 }
85
86 ✗ memset(&result, 0, sizeof(result));
87
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_command_protocol_decode(packet->data, packet->length, &request) != 0) {
88 ✗ result.status = KFSW_COMMAND_INVALID_ARGUMENT;
89 ✗ csp_buffer_free(packet);
90 ✗ memset(&request, 0, sizeof(request));
91 ✗ send_result(connection, &request, 0U, &result);
92 ✗ return;
93 }
94
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (request.version == 2U) {
95 ✗ uint64_t token = request.token;
96 #if CONFIG_KFSW_COMMAND_RETRY
97 uint64_t fresh = 0U;
98
99 if (request.opcode == KFSW_COMMAND_OP_PREPARE) {
100 (void)kfsw_command_retry_random(&fresh);
101 }
102 kfsw_command_retry_dispatch(&retry_cache, source_node, &request, fresh,
103 k_uptime_get(), &token, &result);
104 #else
105 ✗ result.status = KFSW_COMMAND_UNAVAILABLE;
106 #endif
107 ✗ csp_buffer_free(packet);
108 ✗ send_result(connection, &request, token, &result);
109 ✗ return;
110 }
111
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((request.opcode != KFSW_COMMAND_OP_REQUEST) || (request.status != 0U)) {
112 ✗ result.status = KFSW_COMMAND_INVALID_ARGUMENT;
113 ✗ csp_buffer_free(packet);
114 ✗ send_result(connection, &request, request.token, &result);
115 ✗ return;
116 }
117
118 ✗ decoded = kfsw_command_decode_args(&request, args, ARRAY_SIZE(args), text_storage);
119
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (decoded < 0) {
120 ✗ result.status = KFSW_COMMAND_INVALID_ARGUMENT;
121 ✗ csp_buffer_free(packet);
122 ✗ send_result(connection, &request, request.token, &result);
123 ✗ return;
124 }
125
126 ✗ (void)kfsw_command_invoke_id(request.command_id, args, (size_t)decoded, &source, &result);
127
128 /* Arguments were copied to text_storage, so the packet can be freed now. */
129 ✗ csp_buffer_free(packet);
130 ✗ send_result(connection, &request, request.token, &result);
131 }
132
133 ✗ static void command_server(void *arg1, void *arg2, void *arg3)
134 {
135 ARG_UNUSED(arg1);
136 ARG_UNUSED(arg2);
137 ARG_UNUSED(arg3);
138
139 ✗ for (;;) {
140 csp_conn_t *connection;
141
142
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (atomic_get(&server_started) == 0) {
143 ✗ k_sleep(K_MSEC(KFSW_COMMAND_POLL_MS));
144 ✗ continue;
145 }
146 ✗ connection = csp_accept(&command_socket, KFSW_COMMAND_POLL_MS);
147
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
148 ✗ continue;
149 }
150 ✗ serve_request(connection, csp_conn_src(connection));
151 ✗ (void)csp_close(connection);
152 }
153 }
154
155 K_THREAD_DEFINE(kfsw_command_server_thread, CONFIG_KFSW_COMMAND_SERVER_STACK_SIZE, command_server,
156 NULL, NULL, NULL, CONFIG_KFSW_COMMAND_SERVER_PRIORITY, 0, SYS_FOREVER_MS);
157
158 ✗ int kfsw_command_server_start(void)
159 {
160 struct kfsw_csp_info csp_info;
161 int result;
162
163
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (!kfsw_command_is_initialized()) {
164 ✗ return -EACCES;
165 }
166
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (atomic_get(&server_started) != 0) {
167 ✗ return 0;
168 }
169 ✗ kfsw_csp_get_info(&csp_info);
170
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!csp_info.initialized || !csp_info.router_running) {
171 ✗ return -EACCES;
172 }
173
174 ✗ memset(&command_socket, 0, sizeof(command_socket));
175 ✗ command_socket.opts = CSP_SO_CRC32REQ | KFSW_CSP_AUTHENTICATED_SOCKET;
176 ✗ result = csp_listen(&command_socket, 1U);
177
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != CSP_ERR_NONE) {
178 ✗ (void)csp_socket_close(&command_socket);
179 ✗ return -EIO;
180 }
181 ✗ result = csp_bind(&command_socket, CONFIG_KFSW_COMMAND_CSP_PORT);
182
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != CSP_ERR_NONE) {
183 ✗ (void)csp_socket_close(&command_socket);
184 ✗ return -EADDRINUSE;
185 }
186 ✗ atomic_set(&server_started, 1);
187
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!thread_started) {
188 ✗ k_thread_start(kfsw_command_server_thread);
189 ✗ thread_started = true;
190 }
191 ✗ return 0;
192 }
193
194 /* Serializes the single client workspace. */
195 K_MUTEX_DEFINE(command_client_lock);
196
197 ✗ static int receive_result(csp_conn_t *connection, const struct kfsw_command_message *request,
198 uint64_t *token, struct kfsw_command_result *result)
199 {
200 struct kfsw_command_message response;
201 csp_packet_t *packet;
202 size_t detail_size;
203
204 ✗ packet = csp_read(connection, kfsw_command_get_timeout_ms());
205
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
206 ✗ return -ETIMEDOUT;
207 }
208
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_command_protocol_decode(packet->data, packet->length, &response) != 0) {
209 ✗ csp_buffer_free(packet);
210 ✗ return -EBADMSG;
211 }
212
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ uint8_t opcode = (request->opcode == KFSW_COMMAND_OP_PREPARE) ? KFSW_COMMAND_OP_TICKET
213 : KFSW_COMMAND_OP_RESULT;
214
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ uint8_t version = request->version ? request->version : 1U;
215
216
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((response.version != version) || (response.opcode != opcode) ||
217
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (response.command_id != request->command_id) ||
218
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ (response.request_id != request->request_id) || (response.arg_count != 0U) ||
219
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ (response.status > KFSW_COMMAND_UNAVAILABLE) ||
220 ✗ ((version == 2U) &&
221
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ ((response.token == 0U) || ((request->opcode == KFSW_COMMAND_OP_EXECUTE) &&
222
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (response.token != request->token))))) {
223 ✗ csp_buffer_free(packet);
224 ✗ return -EBADMSG;
225 }
226
227 ✗ *token = response.token;
228 ✗ result->status = (enum kfsw_command_status)response.status;
229 ✗ detail_size = MIN((size_t)response.payload_size, sizeof(result->detail) - 1U);
230 ✗ memcpy(result->detail, response.payload, detail_size);
231 ✗ result->detail[detail_size] = '\0';
232 ✗ csp_buffer_free(packet);
233 ✗ return 0;
234 }
235
236 ✗ static int exchange(uint16_t node, const struct kfsw_command_message *request,
237 const uint8_t *buffer, size_t size, uint64_t *token,
238 struct kfsw_command_result *result)
239 {
240 ✗ csp_conn_t *connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_COMMAND_CSP_PORT,
241 kfsw_command_get_timeout_ms(),
242 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
243 csp_packet_t *packet;
244 int outcome;
245
246
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
247 ✗ return -ECONNREFUSED;
248 }
249 ✗ packet = csp_buffer_get(size);
250
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
251 ✗ (void)csp_close(connection);
252 ✗ return -ENOMEM;
253 }
254 ✗ memcpy(packet->data, buffer, size);
255 ✗ packet->length = size;
256 ✗ csp_send(connection, packet);
257 ✗ outcome = receive_result(connection, request, token, result);
258 ✗ (void)csp_close(connection);
259 ✗ return outcome;
260 }
261
262 ✗ static int invoke_remote(uint16_t node, const char *name, const struct kfsw_command_arg *args,
263 size_t arg_count, bool retry, struct kfsw_command_result *result)
264 {
265 static uint16_t next_request_id;
266 static uint8_t payload[KFSW_COMMAND_MAX_PAYLOAD_SIZE];
267 uint8_t buffer[KFSW_COMMAND_RETRY_HEADER_SIZE + KFSW_COMMAND_MAX_PAYLOAD_SIZE];
268 ✗ struct kfsw_command_message request = {.opcode = KFSW_COMMAND_OP_REQUEST};
269 struct kfsw_csp_info csp_info;
270 ✗ uint64_t token = 0U;
271 size_t encoded_size;
272 int outcome;
273
274
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if ((name == NULL) || (result == NULL) || (node == 0U) ||
275 (node >= KFSW_CSP_BROADCAST_ADDRESS)) {
276 ✗ return -EINVAL;
277 }
278 ✗ kfsw_csp_get_info(&csp_info);
279
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (node == csp_info.address) {
280 ✗ return kfsw_command_invoke(name, args, arg_count, result);
281 }
282
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!csp_info.initialized || !csp_info.router_running) {
283 ✗ return -EACCES;
284 }
285 ✗ memset(result, 0, sizeof(*result));
286 ✗ result->status = KFSW_COMMAND_UNAVAILABLE;
287 ✗ k_mutex_lock(&command_client_lock, K_FOREVER);
288 ✗ outcome = kfsw_command_lookup_id(name, &request.command_id);
289
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (outcome == 0) {
290 ✗ outcome = kfsw_command_encode_args(args, arg_count, payload, sizeof(payload),
291 &request.payload_size);
292 }
293
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (outcome != 0) {
294 ✗ goto done;
295 }
296 ✗ request.request_id = ++next_request_id;
297 ✗ request.arg_count = (uint8_t)arg_count;
298 ✗ request.payload = payload;
299
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (retry) {
300 #if CONFIG_KFSW_COMMAND_RETRY
301 outcome = kfsw_command_retry_random(&request.token);
302 #else
303 ✗ outcome = -ENOTSUP;
304 #endif
305
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (outcome != 0) {
306 ✗ goto done;
307 }
308 ✗ request.version = 2U;
309 ✗ request.opcode = KFSW_COMMAND_OP_PREPARE;
310 }
311
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (unsigned int phase = 0U; phase < (retry ? 2U : 1U); phase++) {
312 ✗ outcome = kfsw_command_protocol_encode(buffer, sizeof(buffer), &request,
313 &encoded_size);
314
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (outcome != 0) {
315 ✗ break;
316 }
317
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (unsigned int attempt = 0U; attempt < (retry ? 3U : 1U); attempt++) {
318 ✗ outcome = exchange(node, &request, buffer, encoded_size, &token, result);
319
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((outcome != -ETIMEDOUT) && (outcome != -ECONNREFUSED)) {
320 ✗ break;
321 }
322 }
323
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((outcome != 0) || (result->status != KFSW_COMMAND_OK)) {
324 break;
325 }
326 ✗ request.opcode = KFSW_COMMAND_OP_EXECUTE;
327 ✗ request.token = token;
328 }
329 ✗ done:
330 ✗ k_mutex_unlock(&command_client_lock);
331 ✗ return outcome;
332 }
333
334 ✗ int kfsw_command_invoke_remote(uint16_t node, const char *name, const struct kfsw_command_arg *args,
335 size_t arg_count, struct kfsw_command_result *result)
336 {
337 ✗ return invoke_remote(node, name, args, arg_count, false, result);
338 }
339
340 ✗ int kfsw_command_invoke_remote_retry(uint16_t node, const char *name,
341 const struct kfsw_command_arg *args, size_t arg_count,
342 struct kfsw_command_result *result)
343 {
344 ✗ return invoke_remote(node, name, args, arg_count, true, result);
345 }
346