./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
0 / 72
0.0%
Branches:
0 / 66
0.0%
Functions:
0 / 5
0.0%

kfsw-services/src/remexec/remexec_protocol.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/sys/byteorder.h>
8 #include <zephyr/sys/util.h>
9
10 #include "remexec_internal.h"
11
12 /* Layout: version, opcode, be16 length, text. */
13
14 ✗ static bool known_opcode(uint8_t opcode)
15 {
16
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ return (opcode == (uint8_t)KFSW_REMEXEC_OP_LIST) ||
17 (opcode == (uint8_t)KFSW_REMEXEC_OP_RUN);
18 }
19
20 ✗ int kfsw_remexec_encode_request(uint8_t *buffer, size_t capacity, enum kfsw_remexec_opcode opcode,
21 const char *text, size_t *size)
22 {
23
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ size_t length = (text != NULL) ? strnlen(text, KFSW_REMEXEC_COMMAND_MAX + 1U) : 0U;
24
25
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ if ((buffer == NULL) || (size == NULL) || !known_opcode((uint8_t)opcode)) {
26 ✗ return -EINVAL;
27 }
28
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (length > KFSW_REMEXEC_COMMAND_MAX) {
29 ✗ return -ENAMETOOLONG;
30 }
31
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (capacity < (KFSW_REMEXEC_REQUEST_HEADER_SIZE + length)) {
32 ✗ return -ENOSPC;
33 }
34 ✗ buffer[0] = KFSW_REMEXEC_PROTOCOL_VERSION;
35 ✗ buffer[1] = (uint8_t)opcode;
36 ✗ sys_put_be16((uint16_t)length, &buffer[2]);
37
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (length != 0U) {
38 ✗ memcpy(&buffer[KFSW_REMEXEC_REQUEST_HEADER_SIZE], text, length);
39 }
40 ✗ *size = KFSW_REMEXEC_REQUEST_HEADER_SIZE + length;
41 ✗ return 0;
42 }
43
44 ✗ int kfsw_remexec_decode_request(const uint8_t *data, size_t size, enum kfsw_remexec_opcode *opcode,
45 char *text, size_t text_capacity)
46 {
47 uint16_t length;
48
49
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 ((data == NULL) || (opcode == NULL) || (text == NULL) || (text_capacity == 0U)) {
50 ✗ return -EINVAL;
51 }
52
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (size < KFSW_REMEXEC_REQUEST_HEADER_SIZE) {
53 ✗ return -EBADMSG;
54 }
55
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if ((data[0] != KFSW_REMEXEC_PROTOCOL_VERSION) || !known_opcode(data[1])) {
56 ✗ return -EBADMSG;
57 }
58 ✗ length = sys_get_be16(&data[2]);
59
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((size_t)length != (size - KFSW_REMEXEC_REQUEST_HEADER_SIZE)) {
60 ✗ return -EBADMSG;
61 }
62
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((size_t)length >= text_capacity) {
63 ✗ return -ENAMETOOLONG;
64 }
65 ✗ *opcode = (enum kfsw_remexec_opcode)data[1];
66 ✗ memcpy(text, &data[KFSW_REMEXEC_REQUEST_HEADER_SIZE], length);
67 ✗ text[length] = '\0';
68 ✗ return 0;
69 }
70
71 ✗ int kfsw_remexec_encode_reply(uint8_t *buffer, size_t capacity, enum kfsw_remexec_opcode opcode,
72 const struct kfsw_remexec_reply *reply, size_t *size)
73 {
74 size_t length;
75
76
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if ((buffer == NULL) || (reply == NULL) || (size == NULL) ||
77
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ !known_opcode((uint8_t)opcode)) {
78 ✗ return -EINVAL;
79 }
80 ✗ length = MIN((size_t)reply->size, KFSW_REMEXEC_OUTPUT_MAX);
81
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (capacity < (KFSW_REMEXEC_REPLY_HEADER_SIZE + length)) {
82 ✗ return -ENOSPC;
83 }
84 ✗ buffer[0] = KFSW_REMEXEC_PROTOCOL_VERSION;
85 ✗ buffer[1] = (uint8_t)opcode;
86 ✗ buffer[2] = (uint8_t)reply->status;
87
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ buffer[3] = reply->truncated ? KFSW_REMEXEC_FLAG_TRUNCATED : 0U;
88 ✗ sys_put_be32((uint32_t)reply->command_result, &buffer[4]);
89 ✗ sys_put_be32(reply->produced, &buffer[8]);
90 ✗ sys_put_be32(reply->dropped, &buffer[12]);
91 ✗ sys_put_be16((uint16_t)length, &buffer[16]);
92
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (length != 0U) {
93 ✗ memcpy(&buffer[KFSW_REMEXEC_REPLY_HEADER_SIZE], reply->text, length);
94 }
95 ✗ *size = KFSW_REMEXEC_REPLY_HEADER_SIZE + length;
96 ✗ return 0;
97 }
98
99 ✗ int kfsw_remexec_decode_reply(const uint8_t *data, size_t size, enum kfsw_remexec_opcode opcode,
100 struct kfsw_remexec_reply *reply)
101 {
102 uint16_t length;
103
104
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((data == NULL) || (reply == NULL)) {
105 ✗ return -EINVAL;
106 }
107
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (size < KFSW_REMEXEC_REPLY_HEADER_SIZE) {
108 ✗ return -EBADMSG;
109 }
110
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((data[0] != KFSW_REMEXEC_PROTOCOL_VERSION) || (data[1] != (uint8_t)opcode) ||
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (data[2] > (uint8_t)KFSW_REMEXEC_UNAVAILABLE)) {
112 ✗ return -EBADMSG;
113 }
114 ✗ length = sys_get_be16(&data[16]);
115
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (((size_t)length != (size - KFSW_REMEXEC_REPLY_HEADER_SIZE)) ||
116 ((size_t)length > KFSW_REMEXEC_OUTPUT_MAX)) {
117 ✗ return -EBADMSG;
118 }
119 ✗ reply->status = (enum kfsw_remexec_status)data[2];
120 ✗ reply->truncated = (data[3] & KFSW_REMEXEC_FLAG_TRUNCATED) != 0U;
121 ✗ reply->command_result = (int32_t)sys_get_be32(&data[4]);
122 ✗ reply->produced = sys_get_be32(&data[8]);
123 ✗ reply->dropped = sys_get_be32(&data[12]);
124 ✗ reply->size = length;
125 ✗ memcpy(reply->text, &data[KFSW_REMEXEC_REPLY_HEADER_SIZE], length);
126 ✗ reply->text[length] = '\0';
127 ✗ return 0;
128 }
129