./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
98 / 131
74.8%
Branches:
39 / 59
66.1%
Functions:
4 / 4
100.0%

kfsw-services/src/parameter/param_wire.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/sys/byteorder.h>
7
8 #include "param_wire.h"
9
10 _Static_assert(sizeof(float) == sizeof(uint32_t),
11 "parameter files require a 32-bit IEEE-compatible float");
12
13 1696 int kfsw_param_wire_type_of(const struct kfsw_param_entry *entry, uint8_t *type,
14 uint16_t *value_size)
15 {
16
6/9
✓ Branch 0 taken 424 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 528 times.
✓ Branch 4 taken 256 times.
✓ Branch 5 taken 216 times.
✓ Branch 6 taken 240 times.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
1696 switch (entry->info.type) {
17 424 case KFSW_PARAM_U8:
18 /* The hex types differ from their unsigned twins only in how the shell
19 * prints them, so they persist as the same width. Without this a
20 * persistent hex value fails to encode, and one entry that cannot be
21 * encoded used to take the whole snapshot with it.
22 */
23 case KFSW_PARAM_X8:
24 424 *type = KFSW_PARAM_WIRE_U8;
25 424 *value_size = sizeof(uint8_t);
26 424 return 0;
27 ✗ case KFSW_PARAM_U16:
28 case KFSW_PARAM_X16:
29 ✗ *type = KFSW_PARAM_WIRE_U16;
30 ✗ *value_size = sizeof(uint16_t);
31 ✗ return 0;
32 ✗ case KFSW_PARAM_I16:
33 ✗ *type = KFSW_PARAM_WIRE_I16;
34 ✗ *value_size = sizeof(int16_t);
35 ✗ return 0;
36 528 case KFSW_PARAM_U32:
37 case KFSW_PARAM_X32:
38 528 *type = KFSW_PARAM_WIRE_U32;
39 528 *value_size = sizeof(uint32_t);
40 528 return 0;
41 256 case KFSW_PARAM_I32:
42 256 *type = KFSW_PARAM_WIRE_I32;
43 256 *value_size = sizeof(int32_t);
44 256 return 0;
45 216 case KFSW_PARAM_FLOAT:
46 216 *type = KFSW_PARAM_WIRE_FLOAT;
47 216 *value_size = sizeof(float);
48 216 return 0;
49 240 case KFSW_PARAM_DATA:
50 240 *type = KFSW_PARAM_WIRE_DATA;
51 240 *value_size = entry->info.array_size;
52 240 return 0;
53 32 case KFSW_PARAM_STRING:
54 32 *type = KFSW_PARAM_WIRE_STRING;
55 /* The encoder uses the current length, including its terminator. */
56 32 *value_size = 0U;
57 32 return 0;
58 ✗ default:
59 ✗ return -ENOTSUP;
60 }
61 }
62
63 474 int kfsw_param_wire_encode(const struct kfsw_param_entry *entry, uint8_t *output,
64 size_t output_size, uint16_t *written)
65 {
66 474 struct kfsw_param_value value = {0};
67 uint16_t value_size;
68 uint8_t type;
69 uint32_t raw_value;
70 int result;
71
72 474 result = kfsw_param_wire_type_of(entry, &type, &value_size);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 474 times.
474 if (result != 0) {
74 ✗ return result;
75 }
76
77 474 result = kfsw_param_read_entry(entry, &value);
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 474 times.
474 if (result != 0) {
79 ✗ return result;
80 }
81
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 470 times.
474 if (type == KFSW_PARAM_WIRE_STRING) {
82 4 value_size = (uint16_t)value.size;
83 }
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 474 times.
474 if (output_size < value_size) {
85 ✗ return -ENOSPC;
86 }
87
6/9
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✓ Branch 4 taken 72 times.
✓ Branch 5 taken 64 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 68 times.
✗ Branch 8 not taken.
474 switch (type) {
88 128 case KFSW_PARAM_WIRE_U8:
89 128 output[0] = value.scalar.u8;
90 128 break;
91 ✗ case KFSW_PARAM_WIRE_U16:
92 ✗ sys_put_be16(value.scalar.u16, output);
93 ✗ break;
94 ✗ case KFSW_PARAM_WIRE_I16:
95 ✗ sys_put_be16((uint16_t)value.scalar.i16, output);
96 ✗ break;
97 138 case KFSW_PARAM_WIRE_U32:
98 138 sys_put_be32(value.scalar.u32, output);
99 138 break;
100 72 case KFSW_PARAM_WIRE_I32:
101 72 sys_put_be32((uint32_t)value.scalar.i32, output);
102 72 break;
103 64 case KFSW_PARAM_WIRE_FLOAT:
104 64 memcpy(&raw_value, &value.scalar.f32, sizeof(raw_value));
105 64 sys_put_be32(raw_value, output);
106 64 break;
107 4 case KFSW_PARAM_WIRE_STRING:
108 4 memcpy(output, value.text, value_size);
109 4 break;
110 68 case KFSW_PARAM_WIRE_DATA:
111 68 memcpy(output, value.bytes, value_size);
112 68 break;
113 ✗ default:
114 ✗ return -ENOTSUP;
115 }
116
117 474 *written = value_size;
118 474 return 0;
119 }
120
121 436 int kfsw_param_wire_decode(const struct kfsw_param_entry *entry, uint8_t type, const uint8_t *value,
122 uint16_t value_size, struct kfsw_param_value *decoded)
123 {
124 uint32_t raw_value;
125 uint16_t expected_size;
126 uint8_t expected_type;
127 int result;
128
129 436 *decoded = (struct kfsw_param_value){
130 436 .type = entry->info.type,
131 .size = value_size,
132 };
133
134 /* The code has to be the one this parameter's type is written as, and the
135 * bytes have to be the width that code means. Coercing a U32 into a U8
136 * would quietly deliver a different value than the file asked for.
137 */
138 436 result = kfsw_param_wire_type_of(entry, &expected_type, &expected_size);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 436 times.
436 if (result != 0) {
140 ✗ return result;
141 }
142
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 428 times.
436 if (type != expected_type) {
143 8 return -EBADMSG;
144 }
145
3/4
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 412 times.
428 if ((expected_size != 0U) && (value_size != expected_size)) {
146 ✗ return -EBADMSG;
147 }
148
149
6/9
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 68 times.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 56 times.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
428 switch (type) {
150 84 case KFSW_PARAM_WIRE_U8:
151 84 decoded->scalar.u8 = value[0];
152 84 break;
153 ✗ case KFSW_PARAM_WIRE_U16:
154 ✗ decoded->scalar.u16 = sys_get_be16(value);
155 ✗ break;
156 ✗ case KFSW_PARAM_WIRE_I16:
157 ✗ decoded->scalar.i16 = (int16_t)sys_get_be16(value);
158 ✗ break;
159 160 case KFSW_PARAM_WIRE_U32:
160 160 decoded->scalar.u32 = sys_get_be32(value);
161 160 break;
162 68 case KFSW_PARAM_WIRE_I32:
163 68 decoded->scalar.i32 = (int32_t)sys_get_be32(value);
164 68 break;
165 44 case KFSW_PARAM_WIRE_FLOAT:
166 44 raw_value = sys_get_be32(value);
167 44 memcpy(&decoded->scalar.f32, &raw_value, sizeof(decoded->scalar.f32));
168 44 break;
169 56 case KFSW_PARAM_WIRE_DATA:
170 /* An array must have the same length. */
171
3/6
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 56 times.
56 if ((value_size == 0U) || (value_size != entry->info.array_size) ||
172 (value_size > sizeof(decoded->bytes))) {
173 ✗ return -EBADMSG;
174 }
175 56 memcpy(decoded->bytes, value, value_size);
176 56 decoded->size = value_size;
177 56 break;
178 16 case KFSW_PARAM_WIRE_STRING:
179 /* A string without its terminator is corrupt. */
180
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if ((value_size == 0U) || (value_size > sizeof(decoded->text)) ||
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (value[value_size - 1U] != '\0')) {
182 ✗ return -EBADMSG;
183 }
184 16 memcpy(decoded->text, value, value_size);
185 16 decoded->size = value_size;
186 16 break;
187 ✗ default:
188 ✗ return -ENOTSUP;
189 }
190
191 428 return kfsw_param_validate_entry(entry, decoded);
192 }
193
194 1978 int kfsw_param_wire_apply(const struct kfsw_param_entry *entry,
195 const struct kfsw_param_value *decoded)
196 {
197
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 1866 times.
1978 if (decoded->type == KFSW_PARAM_STRING) {
198 112 kfsw_param_write_text_entry(entry, decoded->text);
199
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 1722 times.
1866 } else if (decoded->type == KFSW_PARAM_DATA) {
200 144 kfsw_param_write_data_entry(entry, decoded->bytes, decoded->size);
201 } else {
202 1722 return kfsw_param_write_entry(entry, &decoded->scalar);
203 }
204 256 return 0;
205 }
206