./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
41 / 76
53.9%
Branches:
12 / 30
40.0%
Functions:
6 / 8
75.0%

kfsw-services/src/fwu/fwu_lite_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 <csp/csp.h>
8 #include <csp/crypto/csp_hmac.h>
9 #include <kfsw/comms/csp.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/util.h>
12
13 #include <kfsw/services/fwu_lite.h>
14 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FWU
15 #include <kfsw/services/log.h>
16
17 #define SOCKET_OPTIONS \
18 (CSP_SO_CRC32REQ | KFSW_CSP_AUTHENTICATED_SOCKET | \
19 (IS_ENABLED(CONFIG_KFSW_FWU_LITE_RDP) ? CSP_SO_RDPREQ : 0))
20
21 #define WIRE_BUFFER_SIZE KFSW_FWU_LITE_WIRE_SIZE
22
23 /* Pinned libcsp appends RDP(5), HMAC(4), CSP CRC(4), KISS CRC(4).
24 * CSP IDs use packet headroom. KISS escapes on output, at most
25 * 2 * (payload + these trailers + CSP2 ID(6)) + 3 serial bytes.
26 */
27 BUILD_ASSERT(WIRE_BUFFER_SIZE + 5U + CSP_HMAC_LENGTH + 4U + 4U <= CSP_BUFFER_SIZE,
28 "One block message must fit a CSP buffer; reduce KFSW_FWU_LITE_BLOCK_SIZE");
29
30 static K_THREAD_STACK_DEFINE(server_stack, CONFIG_KFSW_FWU_LITE_STACK_SIZE);
31 static struct k_thread server_thread;
32 static bool server_running;
33 static csp_socket_t server_socket;
34 K_MUTEX_DEFINE(server_start_lock);
35
36 ✗ static void serve_packet(csp_conn_t *connection, csp_packet_t *packet)
37 {
38 struct kfsw_fwu_lite_message request;
39 struct kfsw_fwu_lite_message reply;
40 uint8_t receipt_data[KFSW_FWU_LITE_RECEIPT_SIZE];
41 csp_packet_t *response;
42 ✗ size_t encoded = 0U;
43
44
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (packet->length > CSP_BUFFER_SIZE ||
45 ✗ kfsw_fwu_lite_decode(packet->data, packet->length, &request) != 0) {
46 /* Drop undecodable packets; no valid request ID is available. */
47 ✗ csp_buffer_free(packet);
48 ✗ return;
49 }
50
51 ✗ (void)kfsw_fwu_lite_handle_peer((uint16_t)csp_conn_src(connection), &request, &reply,
52 receipt_data);
53
54 ✗ response = csp_buffer_get(0);
55
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (response == NULL) {
56 ✗ csp_buffer_free(packet);
57 ✗ return;
58 }
59
60
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_fwu_lite_encode(&reply, response->data, CSP_BUFFER_SIZE, &encoded) != 0) {
61 ✗ csp_buffer_free(response);
62 ✗ csp_buffer_free(packet);
63 ✗ return;
64 }
65
66 ✗ response->length = (uint16_t)encoded;
67 ✗ csp_buffer_free(packet);
68 /* csp_send() frees the packet, even when sending fails. */
69 ✗ csp_send(connection, response);
70 }
71
72 /* Read on every use, so a change applies to the next block. */
73 static uint32_t fwu_lite_timeout_ms = CONFIG_KFSW_FWU_LITE_TIMEOUT_MS;
74 static uint8_t fwu_lite_retries = CONFIG_KFSW_FWU_LITE_BLOCK_RETRIES;
75
76 3108 uint32_t kfsw_fwu_lite_get_timeout_ms(void)
77 {
78 3108 return fwu_lite_timeout_ms;
79 }
80
81 222 int kfsw_fwu_lite_check_timeout_ms(uint32_t timeout_ms)
82 {
83
2/4
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 222 times.
222 if ((timeout_ms < KFSW_FWU_LITE_TIMEOUT_MIN_MS) ||
84 (timeout_ms > KFSW_FWU_LITE_TIMEOUT_MAX_MS)) {
85 ✗ return -ERANGE;
86 }
87 222 return 0;
88 }
89
90 222 int kfsw_fwu_lite_set_timeout_ms(uint32_t timeout_ms)
91 {
92 222 int result = kfsw_fwu_lite_check_timeout_ms(timeout_ms);
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
222 if (result != 0) {
95 ✗ return result;
96 }
97 222 fwu_lite_timeout_ms = timeout_ms;
98 222 return 0;
99 }
100
101 100 uint8_t kfsw_fwu_lite_get_retries(void)
102 {
103 100 return fwu_lite_retries;
104 }
105
106 222 int kfsw_fwu_lite_set_retries(uint8_t retries)
107 {
108 /* Zero retries would give up on the first lost block. */
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
222 if (retries == 0U) {
110 ✗ return -EINVAL;
111 }
112 222 fwu_lite_retries = retries;
113 222 return 0;
114 }
115
116 ✗ static void server_entry(void *first, void *second, void *third)
117 {
118 csp_conn_t *connection;
119
120 ARG_UNUSED(first);
121 ARG_UNUSED(second);
122 ARG_UNUSED(third);
123
124 ✗ kfsw_log_info("Firmware upload server on CSP port %d", CONFIG_KFSW_FWU_LITE_CSP_PORT);
125
126 while (true) {
127 ✗ connection = csp_accept(&server_socket, CSP_MAX_TIMEOUT);
128
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
129 ✗ continue;
130 }
131
132 ✗ while (true) {
133 /* Same timeout the sender uses; a block and its reply can take more than
134 * a second on a slow link.
135 */
136 ✗ csp_packet_t *packet = csp_read(connection, kfsw_fwu_lite_get_timeout_ms());
137
138
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
139 ✗ break;
140 }
141 ✗ serve_packet(connection, packet);
142 }
143 ✗ csp_close(connection);
144 }
145 }
146
147 8 int kfsw_fwu_lite_server_start(void)
148 {
149 struct kfsw_csp_info info;
150 int result;
151
152 8 k_mutex_lock(&server_start_lock, K_FOREVER);
153
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (server_running) {
154 2 result = -EALREADY;
155 2 goto out;
156 }
157 6 kfsw_csp_get_info(&info);
158
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
6 if (!info.initialized || !info.router_running) {
159 2 result = -EACCES;
160 2 goto out;
161 }
162 4 memset(&server_socket, 0, sizeof(server_socket));
163 4 server_socket.opts = SOCKET_OPTIONS;
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (csp_listen(&server_socket, 1U) != CSP_ERR_NONE) {
165 ✗ (void)csp_socket_close(&server_socket);
166 ✗ result = -EIO;
167 ✗ goto out;
168 }
169
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 if (csp_bind(&server_socket, CONFIG_KFSW_FWU_LITE_CSP_PORT) != CSP_ERR_NONE) {
170 2 (void)csp_socket_close(&server_socket);
171 2 result = -EADDRINUSE;
172 2 goto out;
173 }
174
175 2 (void)k_thread_create(&server_thread, server_stack, K_THREAD_STACK_SIZEOF(server_stack),
176 server_entry, NULL, NULL, NULL, CONFIG_KFSW_FWU_LITE_PRIORITY, 0,
177 2 K_FOREVER);
178 2 k_thread_name_set(&server_thread, "kfsw_fwu_lite");
179 2 server_running = true;
180 2 k_thread_start(&server_thread);
181 2 result = 0;
182 8 out:
183 8 k_mutex_unlock(&server_start_lock);
184 8 return result;
185 }
186