./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
143 / 170
84.1%
Branches:
62 / 109
56.9%
Functions:
11 / 11
100.0%

kfsw-services/src/hk/hk_store.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include <zephyr/devicetree.h>
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/util.h>
11
12 #include <kfsw/platform/storage.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_STORE
20
21 /*
22 * Samples stored in a ring file with fixed-size records. The file stops growing
23 * once every slot is used. The newest record is found from the sequence numbers
24 * when the file is opened, so no header is rewritten on each write.
25 */
26 struct store_state {
27 uint32_t interval_ms;
28 uint16_t every_n;
29 uint16_t pending;
30 uint16_t record_size;
31 bool open;
32 };
33
34 static struct store_state stores[CONFIG_KFSW_HK_REPORTS];
35
36 1606 void kfsw_hk_store_path(uint8_t report, char *out, size_t size)
37 {
38 1606 (void)snprintf(out, size, KFSW_HK_STORE_DIRECTORY "/report%u.bin", report);
39 1606 }
40
41 /*
42 * Rated erase cycles of the storage flash, used in the interval warning.
43 */
44 #define KFSW_HK_STORE_PARTITION DT_CHOSEN(kfsw_storage_partition)
45 #define KFSW_HK_STORE_ERASE_BLOCK \
46 DT_PROP_OR(DT_GPARENT(KFSW_HK_STORE_PARTITION), erase_block_size, 4096)
47 #define KFSW_HK_STORE_PARTITION_BYTES DT_REG_SIZE(KFSW_HK_STORE_PARTITION)
48
49 #define KFSW_HK_STORE_ERASE_BUDGET \
50 ((uint32_t)(KFSW_HK_STORE_PARTITION_BYTES / KFSW_HK_STORE_ERASE_BLOCK) * \
51 (uint32_t)CONFIG_KFSW_HK_STORE_ERASE_CYCLES)
52
53 246 static int write_at(struct fs_file_t *file, off_t offset, const uint8_t *data, size_t size)
54 {
55 246 int result = fs_seek(file, offset, FS_SEEK_SET);
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (result != 0) {
58 ✗ return result;
59 }
60
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 246 times.
492 while (size > 0U) {
61 246 ssize_t written = fs_write(file, data, size);
62
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (written < 0) {
64 ✗ return (int)written;
65 }
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (written == 0) {
67 ✗ return -EIO;
68 }
69 246 data += written;
70 246 size -= (size_t)written;
71 }
72 246 return 0;
73 }
74
75 58 static int create_file(uint8_t report, uint16_t record_size)
76 {
77 58 uint8_t header[KFSW_HK_STORE_HEADER_SIZE] = {0};
78 char path[64];
79 struct fs_file_t file;
80 int result;
81 int close_result;
82
83 58 result = fs_mkdir(KFSW_HK_STORE_DIRECTORY);
84
2/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
58 if ((result != 0) && (result != -EEXIST)) {
85 ✗ return result;
86 }
87
88 /* codechecker_intentional [bugprone-not-null-terminated-result] a binary magic */
89 58 memcpy(header, KFSW_HK_STORE_MAGIC, KFSW_HK_STORE_MAGIC_SIZE);
90 58 header[4] = KFSW_HK_STORE_VERSION;
91 58 header[5] = report;
92 58 sys_put_be16(record_size, &header[6]);
93 58 sys_put_be32(CONFIG_KFSW_HK_STORE_CAPACITY, &header[8]);
94
95 58 kfsw_hk_store_path(report, path, sizeof(path));
96 58 fs_file_t_init(&file);
97 58 result = fs_open(&file, path, FS_O_CREATE | FS_O_WRITE | FS_O_TRUNC);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (result != 0) {
99 ✗ return result;
100 }
101 58 result = write_at(&file, 0, header, sizeof(header));
102 58 close_result = fs_close(&file);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 return (result != 0) ? result : close_result;
104 }
105
106 54 int kfsw_hk_store_bytes_needed(uint16_t record_size)
107 {
108 108 return (int)(KFSW_HK_STORE_HEADER_SIZE +
109 54 ((size_t)record_size * (size_t)CONFIG_KFSW_HK_STORE_CAPACITY));
110 }
111
112 88 int kfsw_hk_store_configure(uint8_t report, uint32_t interval_ms, uint32_t period_ms,
113 uint16_t record_size)
114 {
115 struct kfsw_storage_info storage;
116 uint32_t needed;
117 uint32_t every;
118 int result;
119
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (report >= ARRAY_SIZE(stores)) {
121 ✗ return -EINVAL;
122 }
123
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 52 times.
88 if (interval_ms == 0U) {
124 36 stores[report].interval_ms = 0U;
125 36 stores[report].open = false;
126 36 stores[report].pending = 0U;
127 36 return 0;
128 }
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
52 if (!kfsw_storage_is_ready()) {
130 ✗ return -ENODEV;
131 }
132
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 50 times.
52 if (interval_ms < CONFIG_KFSW_HK_STORE_FLOOR_MS) {
133 2 return -ERANGE;
134 }
135
136 /* Refuse a store that won't fit now, not when the filesystem fills. */
137 50 needed = (uint32_t)kfsw_hk_store_bytes_needed(record_size);
138 50 result = kfsw_storage_get_info(&storage);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (result != 0) {
140 ✗ return result;
141 }
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if ((uint64_t)needed > storage.free_bytes) {
143 ✗ kfsw_log_warning("HK: report %u needs %u bytes and %u are free", report, needed,
144 (uint32_t)storage.free_bytes);
145 ✗ return -ENOSPC;
146 }
147
148 50 result = create_file(report, record_size);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (result != 0) {
150 ✗ return result;
151 }
152
153 /* Batch as many collections as fit in the interval, capped at the ring depth. */
154
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 34 times.
50 every = (period_ms == 0U) ? 1U : (((uint64_t)interval_ms + period_ms - 1U) / period_ms);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (every == 0U) {
156 ✗ every = 1U;
157 }
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (every > CONFIG_KFSW_HK_HISTORY) {
159 ✗ kfsw_log_warning("HK: report %u stores every %u samples, not %u: the ring holds %u",
160 report, (unsigned int)CONFIG_KFSW_HK_HISTORY, (unsigned int)every,
161 (unsigned int)CONFIG_KFSW_HK_HISTORY);
162 ✗ every = CONFIG_KFSW_HK_HISTORY;
163 }
164
165 50 stores[report].interval_ms = interval_ms;
166 50 stores[report].every_n = (uint16_t)every;
167 50 stores[report].record_size = record_size;
168 50 stores[report].pending = 0U;
169 50 stores[report].open = true;
170
171
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (interval_ms < CONFIG_KFSW_HK_STORE_ADVISED_MS) {
172 50 uint32_t per_day = (uint32_t)(86400000UL / interval_ms);
173
174 50 kfsw_log_warning("HK: report %u stores every %u ms, about %u writes a day "
175 "against this partition's %u erase budget",
176 report, interval_ms, per_day, KFSW_HK_STORE_ERASE_BUDGET);
177 }
178 50 kfsw_log_info("HK: report %u stores every %u ms, %u samples a write", report, interval_ms,
179 (unsigned int)every);
180 50 return 0;
181 }
182
183 1100 uint32_t kfsw_hk_store_interval(uint8_t report)
184 {
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1100 times.
1100 if (report >= ARRAY_SIZE(stores)) {
186 ✗ return 0U;
187 }
188 1100 return stores[report].interval_ms;
189 }
190
191 1274 void kfsw_hk_store_forget(uint8_t report)
192 {
193 char path[64];
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1274 times.
1274 if (report >= ARRAY_SIZE(stores)) {
196 ✗ return;
197 }
198 1274 stores[report].open = false;
199 1274 stores[report].interval_ms = 0U;
200 1274 stores[report].pending = 0U;
201 1274 kfsw_hk_store_path(report, path, sizeof(path));
202 1274 (void)fs_unlink(path);
203 }
204
205 188 int kfsw_hk_store_flush(uint8_t report, uint16_t next_sequence)
206 {
207 struct fs_file_t file;
208 char path[64];
209 uint16_t first;
210 int result;
211 int close_result;
212
213
2/4
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 188 times.
188 if ((report >= ARRAY_SIZE(stores)) || !stores[report].open) {
214 ✗ return -EACCES;
215 }
216
217 188 stores[report].pending = MIN(stores[report].pending + 1U, CONFIG_KFSW_HK_HISTORY);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 if (stores[report].pending < stores[report].every_n) {
219 ✗ return 0;
220 }
221
222 188 kfsw_hk_store_path(report, path, sizeof(path));
223 188 fs_file_t_init(&file);
224 188 result = fs_open(&file, path, FS_O_WRITE);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 if (result != 0) {
226 ✗ return result;
227 }
228
229 /* Write each sample to the slot its sequence number selects, oldest first. */
230 188 first = (uint16_t)(next_sequence - stores[report].pending);
231
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 188 times.
376 for (uint16_t index = 0U; index < stores[report].pending; index++) {
232 188 uint16_t sequence = (uint16_t)(first + index);
233 188 uint16_t age = (uint16_t)(stores[report].pending - 1U - index);
234 struct kfsw_hk_sample sample;
235 188 off_t offset = (off_t)KFSW_HK_STORE_HEADER_SIZE +
236 188 ((off_t)(sequence % CONFIG_KFSW_HK_STORE_CAPACITY) *
237 188 (off_t)stores[report].record_size);
238
239 188 result = kfsw_hk_get(report, age, &sample);
240
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 if (result == 0) {
241 188 result = write_at(&file, offset, sample.data, stores[report].record_size);
242 }
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 if (result != 0) {
244 ✗ break;
245 }
246 }
247
248 188 close_result = fs_close(&file);
249
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 if (result == 0) {
250 188 result = close_result;
251 }
252 188 stores[report].pending = 0U;
253 188 return result;
254 }
255
256 #if CONFIG_KFSW_HK_PERSISTENCE
257 static struct store_state restored_stores[CONFIG_KFSW_HK_REPORTS];
258 static uint16_t restored_sequences[CONFIG_KFSW_HK_REPORTS];
259
260 192 int kfsw_hk_store_restore_prepare(uint8_t report, uint32_t interval_ms, uint32_t period_ms,
261 uint16_t record_size)
262 {
263 struct fs_file_t file;
264 uint8_t header[KFSW_HK_STORE_HEADER_SIZE];
265 char path[64];
266 int result;
267
268 192 memset(&restored_stores[report], 0, sizeof(restored_stores[report]));
269 192 restored_sequences[report] = 0;
270
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 16 times.
192 if (interval_ms == 0U) {
271 176 return 0;
272 }
273 16 kfsw_hk_store_path(report, path, sizeof(path));
274 16 fs_file_t_init(&file);
275 16 result = fs_open(&file, path, FS_O_READ);
276
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (result == -ENOENT) {
277 8 result = create_file(report, record_size);
278
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 } else if (result == 0) {
279 8 ssize_t read = fs_read(&file, header, sizeof(header));
280 8 bool have_sequence = false;
281 8 uint16_t newest = 0;
282
283 8 result = 0;
284
2/5
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 if (read != sizeof(header) || memcmp(header, KFSW_HK_STORE_MAGIC, 4) != 0 ||
285
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
16 header[4] != KFSW_HK_STORE_VERSION || header[5] != report ||
286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
16 sys_get_be16(&header[6]) != record_size ||
287 8 sys_get_be32(&header[8]) != CONFIG_KFSW_HK_STORE_CAPACITY) {
288 ✗ result = -EBADMSG;
289 }
290
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 for (uint32_t slot = 0; result == 0 && slot <= CONFIG_KFSW_HK_STORE_CAPACITY;
291 16 slot++) {
292 uint8_t sample[CONFIG_KFSW_HK_SAMPLE_BYTES];
293
294 24 read = fs_read(&file, sample, record_size);
295
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 if (read == 0) {
296 8 break;
297 }
298
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (read != record_size || slot == CONFIG_KFSW_HK_STORE_CAPACITY) {
299
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ result = read < 0 ? (int)read : -EBADMSG;
300 ✗ break;
301 }
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (sample[0] == 0U) {
303 ✗ continue;
304 } /* Unwritten sparse slot. */
305 16 uint16_t sequence = sys_get_be16(&sample[2]);
306
307
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (sample[0] != KFSW_HK_PROTOCOL_VERSION || sample[1] != report ||
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 sequence % CONFIG_KFSW_HK_STORE_CAPACITY != slot) {
309 ✗ result = -EBADMSG;
310 ✗ break;
311 }
312
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 if (!have_sequence || (int16_t)(sequence - newest) > 0) {
313 16 newest = sequence;
314 16 have_sequence = true;
315 }
316 }
317 8 int closed = fs_close(&file);
318
319
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (result == 0) {
320 8 result = closed;
321 }
322
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (have_sequence) {
323 8 restored_sequences[report] = newest + 1U;
324 }
325 }
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (result != 0) {
327 ✗ return result;
328 }
329 16 uint64_t every =
330
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 period_ms == 0U ? 1U : ((uint64_t)interval_ms + period_ms - 1U) / period_ms;
331
332 16 restored_stores[report] = (struct store_state){
333 .interval_ms = interval_ms,
334 16 .every_n = MIN(every, CONFIG_KFSW_HK_HISTORY),
335 .record_size = record_size,
336 .open = true,
337 };
338 16 return 0;
339 }
340
341 104 uint16_t kfsw_hk_store_restore_sequence(uint8_t report)
342 {
343 104 return restored_sequences[report];
344 }
345
346 96 void kfsw_hk_store_restore_commit(void)
347 {
348 96 memcpy(stores, restored_stores, sizeof(stores));
349 96 }
350 #endif
351
352 #endif /* CONFIG_KFSW_HK_STORE */
353