kfsw-services/src/hk/hk_persist.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/fs/fs.h> | ||
| 7 | #include <zephyr/kernel.h> | ||
| 8 | #include <zephyr/sys/byteorder.h> | ||
| 9 | #include <zephyr/sys/crc.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 | #include <kfsw/services/snapshot.h> | ||
| 17 | |||
| 18 | #if CONFIG_KFSW_HK_BEACON | ||
| 19 | #include <kfsw/comms/csp.h> | ||
| 20 | #endif | ||
| 21 | |||
| 22 | #include "hk_internal.h" | ||
| 23 | |||
| 24 | #if CONFIG_KFSW_HK_PERSISTENCE | ||
| 25 | |||
| 26 | /* | ||
| 27 | * Report definitions survive a reset; samples don't. The file has a magic, | ||
| 28 | * version and CRC, and is written through a temporary file. | ||
| 29 | */ | ||
| 30 | #define KFSW_HK_PERSIST_DIRECTORY KFSW_STORAGE_MOUNT_POINT "/hk" | ||
| 31 | #define KFSW_HK_PERSIST_PATH KFSW_HK_PERSIST_DIRECTORY "/reports.dat" | ||
| 32 | #define KFSW_HK_PERSIST_TEMP_PATH KFSW_HK_PERSIST_DIRECTORY "/reports.tmp" | ||
| 33 | #define KFSW_HK_PERSIST_MAGIC "KHKR" | ||
| 34 | #define KFSW_HK_PERSIST_MAGIC_SIZE 4U | ||
| 35 | #define KFSW_HK_PERSIST_VERSION 3U | ||
| 36 | #define KFSW_HK_PERSIST_HEADER_SIZE 12U | ||
| 37 | #define KFSW_HK_PERSIST_CRC_OFFSET 8U | ||
| 38 | |||
| 39 | /* magic 4, version 1, reserved 1, count 2, crc 4, then per report: | ||
| 40 | * id 1, entry count 1, period 4, store interval 4, beacon node 2, | ||
| 41 | * beacon interval 4, class 1 (three-bit value), then entries of node 2 and id 2. | ||
| 42 | * | ||
| 43 | * Version 2 added the store interval and the beacon fields. Version 1 files | ||
| 44 | * are still read. Version 3 adds class; older definitions default to class 4. | ||
| 45 | */ | ||
| 46 | #define KFSW_HK_PERSIST_REPORT_HEADER_V1 6U | ||
| 47 | #define KFSW_HK_PERSIST_REPORT_HEADER_V2 16U | ||
| 48 | #define KFSW_HK_PERSIST_REPORT_HEADER 17U | ||
| 49 | #define KFSW_HK_PERSIST_ENTRY_SIZE 4U | ||
| 50 | #define KFSW_HK_PERSIST_MAX_SIZE \ | ||
| 51 | (KFSW_HK_PERSIST_HEADER_SIZE + \ | ||
| 52 | (CONFIG_KFSW_HK_REPORTS * (KFSW_HK_PERSIST_REPORT_HEADER + \ | ||
| 53 | (CONFIG_KFSW_HK_ENTRIES * KFSW_HK_PERSIST_ENTRY_SIZE)))) | ||
| 54 | |||
| 55 | static uint8_t blob[KFSW_HK_PERSIST_MAX_SIZE]; | ||
| 56 | static K_MUTEX_DEFINE(file_lock); | ||
| 57 | |||
| 58 | struct restored_report { | ||
| 59 | struct kfsw_hk_definition definition; | ||
| 60 | uint32_t period_ms; | ||
| 61 | uint32_t store_ms; | ||
| 62 | uint32_t beacon_ms; | ||
| 63 | uint16_t beacon_node; | ||
| 64 | bool present; | ||
| 65 | }; | ||
| 66 | |||
| 67 | static struct restored_report restored[CONFIG_KFSW_HK_REPORTS]; | ||
| 68 | |||
| 69 | /* The store and beacon fields are always written, as zero when not built in. */ | ||
| 70 | 864 | static uint32_t store_interval_of(uint8_t report) | |
| 71 | { | ||
| 72 | #if CONFIG_KFSW_HK_STORE | ||
| 73 | 864 | uint32_t interval = 0U; | |
| 74 | |||
| 75 | 864 | (void)kfsw_hk_get_store(report, &interval); | |
| 76 | 864 | return interval; | |
| 77 | #else | ||
| 78 | ARG_UNUSED(report); | ||
| 79 | return 0U; | ||
| 80 | #endif | ||
| 81 | } | ||
| 82 | |||
| 83 | 864 | static uint16_t beacon_node_of(uint8_t report) | |
| 84 | { | ||
| 85 | #if CONFIG_KFSW_HK_BEACON | ||
| 86 | 752 | uint16_t node = 0U; | |
| 87 | 752 | uint32_t interval = 0U; | |
| 88 | |||
| 89 | 752 | (void)kfsw_hk_get_beacon(report, &node, &interval); | |
| 90 | 752 | return node; | |
| 91 | #else | ||
| 92 | ARG_UNUSED(report); | ||
| 93 | 112 | return 0U; | |
| 94 | #endif | ||
| 95 | } | ||
| 96 | |||
| 97 | 864 | static uint32_t beacon_interval_of(uint8_t report) | |
| 98 | { | ||
| 99 | #if CONFIG_KFSW_HK_BEACON | ||
| 100 | 752 | uint16_t node = 0U; | |
| 101 | 752 | uint32_t interval = 0U; | |
| 102 | |||
| 103 | 752 | (void)kfsw_hk_get_beacon(report, &node, &interval); | |
| 104 | 752 | return interval; | |
| 105 | #else | ||
| 106 | ARG_UNUSED(report); | ||
| 107 | 112 | return 0U; | |
| 108 | #endif | ||
| 109 | } | ||
| 110 | |||
| 111 | 2088 | static int save_snapshot(uint64_t *revision) | |
| 112 | { | ||
| 113 | 2088 | size_t offset = KFSW_HK_PERSIST_HEADER_SIZE; | |
| 114 | 2088 | uint16_t saved = 0U; | |
| 115 | int result; | ||
| 116 | |||
| 117 | 2088 | if (!kfsw_storage_is_ready()) { | |
| 118 | ✗ | return -ENODEV; | |
| 119 | } | ||
| 120 | |||
| 121 | 2088 | kfsw_hk_storage_lock(); | |
| 122 | 2088 | kfsw_hk_lock(); | |
| 123 | 2088 | *revision = kfsw_hk_config_revision(); | |
| 124 | 2088 | if (kfsw_hk_save_blocked()) { | |
| 125 | 8 | kfsw_hk_unlock(); | |
| 126 | 8 | kfsw_hk_storage_unlock(); | |
| 127 | 8 | return -EROFS; | |
| 128 | } | ||
| 129 | 6240 | for (uint8_t index = 0U; index < CONFIG_KFSW_HK_REPORTS; index++) { | |
| 130 | 4160 | const struct kfsw_hk_report *report = kfsw_hk_report_at(index); | |
| 131 | |||
| 132 | 4160 | if ((report == NULL) || !report->defined) { | |
| 133 | 3296 | continue; | |
| 134 | } | ||
| 135 | 864 | blob[offset++] = index; | |
| 136 | 864 | blob[offset++] = report->entry_count; | |
| 137 | 864 | sys_put_be32(report->period_ms, &blob[offset]); | |
| 138 | 864 | offset += 4U; | |
| 139 | 864 | sys_put_be32(store_interval_of(index), &blob[offset]); | |
| 140 | 864 | offset += 4U; | |
| 141 | 864 | sys_put_be16(beacon_node_of(index), &blob[offset]); | |
| 142 | 864 | offset += 2U; | |
| 143 | 864 | sys_put_be32(beacon_interval_of(index), &blob[offset]); | |
| 144 | 864 | offset += 4U; | |
| 145 | 864 | blob[offset++] = report->retrieval_class; | |
| 146 | 2560 | for (uint8_t entry = 0U; entry < report->entry_count; entry++) { | |
| 147 | 1696 | sys_put_be16(report->entries[entry].node, &blob[offset]); | |
| 148 | 1696 | sys_put_be16(report->entries[entry].param_id, &blob[offset + 2U]); | |
| 149 | 1696 | offset += KFSW_HK_PERSIST_ENTRY_SIZE; | |
| 150 | } | ||
| 151 | 864 | saved++; | |
| 152 | } | ||
| 153 | 2080 | kfsw_hk_unlock(); | |
| 154 | 2080 | kfsw_hk_storage_unlock(); | |
| 155 | |||
| 156 | 2080 | memcpy(blob, KFSW_HK_PERSIST_MAGIC, KFSW_HK_PERSIST_MAGIC_SIZE); | |
| 157 | 2080 | blob[4] = KFSW_HK_PERSIST_VERSION; | |
| 158 | 2080 | blob[5] = 0U; | |
| 159 | 2080 | sys_put_be16(saved, &blob[6]); | |
| 160 | 2080 | sys_put_be32(0U, &blob[KFSW_HK_PERSIST_CRC_OFFSET]); | |
| 161 | 2080 | sys_put_be32(crc32_ieee(blob, offset), &blob[KFSW_HK_PERSIST_CRC_OFFSET]); | |
| 162 | |||
| 163 | 2080 | result = fs_mkdir(KFSW_HK_PERSIST_DIRECTORY); | |
| 164 | 2080 | if ((result != 0) && (result != -EEXIST)) { | |
| 165 | ✗ | return result; | |
| 166 | } | ||
| 167 | |||
| 168 | 2080 | result = kfsw_snapshot_write(KFSW_HK_PERSIST_PATH, KFSW_HK_PERSIST_TEMP_PATH, blob, offset); | |
| 169 | 2080 | if (result != 0) { | |
| 170 | 24 | return result; | |
| 171 | } | ||
| 172 | |||
| 173 | 2056 | kfsw_log_info("HK: %u report definitions saved", saved); | |
| 174 | 2056 | return 0; | |
| 175 | } | ||
| 176 | |||
| 177 | 180 | static int load_snapshot(void) | |
| 178 | { | ||
| 179 | struct fs_file_t file; | ||
| 180 | struct fs_dirent info; | ||
| 181 | 180 | size_t offset = KFSW_HK_PERSIST_HEADER_SIZE; | |
| 182 | uint16_t expected; | ||
| 183 | uint32_t stored_crc; | ||
| 184 | uint32_t actual_crc; | ||
| 185 | uint8_t version; | ||
| 186 | size_t report_header; | ||
| 187 | ssize_t read; | ||
| 188 | int result; | ||
| 189 | |||
| 190 | 180 | if (!kfsw_storage_is_ready()) { | |
| 191 | ✗ | return -ENODEV; | |
| 192 | } | ||
| 193 | 180 | result = fs_stat(KFSW_HK_PERSIST_PATH, &info); | |
| 194 | 180 | if (result != 0) { | |
| 195 | 20 | return result; | |
| 196 | } | ||
| 197 | 160 | if ((info.size < KFSW_HK_PERSIST_HEADER_SIZE) || (info.size > sizeof(blob))) { | |
| 198 | ✗ | return -EFBIG; | |
| 199 | } | ||
| 200 | |||
| 201 | 160 | fs_file_t_init(&file); | |
| 202 | 160 | result = fs_open(&file, KFSW_HK_PERSIST_PATH, FS_O_READ); | |
| 203 | 160 | if (result != 0) { | |
| 204 | ✗ | return result; | |
| 205 | } | ||
| 206 | 160 | read = fs_read(&file, blob, info.size); | |
| 207 | 160 | (void)fs_close(&file); | |
| 208 | 160 | if (read != (ssize_t)info.size) { | |
| 209 | ✗ | return -EIO; | |
| 210 | } | ||
| 211 | |||
| 212 | 160 | if (memcmp(blob, KFSW_HK_PERSIST_MAGIC, KFSW_HK_PERSIST_MAGIC_SIZE) != 0) { | |
| 213 | 8 | return -EBADMSG; | |
| 214 | } | ||
| 215 | /* Older versions are read; newer ones are refused. */ | ||
| 216 | 152 | version = blob[4]; | |
| 217 | 152 | if ((version != KFSW_HK_PERSIST_VERSION) && (version != 1U) && (version != 2U)) { | |
| 218 | 16 | kfsw_log_warning("HK: saved definitions are version %u, not %u", version, | |
| 219 | KFSW_HK_PERSIST_VERSION); | ||
| 220 | 16 | return -EPROTONOSUPPORT; | |
| 221 | } | ||
| 222 | 136 | report_header = (version == 1U) ? KFSW_HK_PERSIST_REPORT_HEADER_V1 | |
| 223 | 196 | : (version == 2U) ? KFSW_HK_PERSIST_REPORT_HEADER_V2 | |
| 224 | 60 | : KFSW_HK_PERSIST_REPORT_HEADER; | |
| 225 | |||
| 226 | 136 | stored_crc = sys_get_be32(&blob[KFSW_HK_PERSIST_CRC_OFFSET]); | |
| 227 | 136 | sys_put_be32(0U, &blob[KFSW_HK_PERSIST_CRC_OFFSET]); | |
| 228 | 136 | actual_crc = crc32_ieee(blob, (size_t)info.size); | |
| 229 | 136 | if (actual_crc != stored_crc) { | |
| 230 | 16 | kfsw_log_warning("HK: saved definitions failed their checksum"); | |
| 231 | 16 | return -EBADMSG; | |
| 232 | } | ||
| 233 | |||
| 234 | 120 | expected = sys_get_be16(&blob[6]); | |
| 235 | 120 | if (expected > ARRAY_SIZE(restored) || blob[5] != 0U) { | |
| 236 | ✗ | return -EBADMSG; | |
| 237 | } | ||
| 238 | 120 | memset(restored, 0, sizeof(restored)); | |
| 239 | 232 | for (uint16_t index = 0; index < expected; index++) { | |
| 240 | struct kfsw_hk_entry entries[CONFIG_KFSW_HK_ENTRIES]; | ||
| 241 | uint8_t report; | ||
| 242 | uint8_t count; | ||
| 243 | |||
| 244 | 136 | if (offset + report_header > info.size) { | |
| 245 | 24 | return -EBADMSG; | |
| 246 | } | ||
| 247 | 136 | report = blob[offset]; | |
| 248 | 136 | count = blob[offset + 1]; | |
| 249 | 136 | if (report >= ARRAY_SIZE(restored) || restored[report].present || count == 0U || | |
| 250 | count > ARRAY_SIZE(entries)) { | ||
| 251 | 8 | return -EBADMSG; | |
| 252 | } | ||
| 253 | 128 | struct restored_report *item = &restored[report]; | |
| 254 | |||
| 255 | 128 | item->present = true; | |
| 256 | 128 | item->period_ms = sys_get_be32(&blob[offset + 2]); | |
| 257 | 128 | if (version >= 2U) { | |
| 258 | 112 | item->store_ms = sys_get_be32(&blob[offset + 6]); | |
| 259 | 112 | item->beacon_node = sys_get_be16(&blob[offset + 10]); | |
| 260 | 112 | item->beacon_ms = sys_get_be32(&blob[offset + 12]); | |
| 261 | } | ||
| 262 | 128 | if (item->period_ms != 0U && item->period_ms < CONFIG_KFSW_HK_PERIOD_FLOOR_MS) { | |
| 263 | ✗ | return -ERANGE; | |
| 264 | } | ||
| 265 | #if CONFIG_KFSW_HK_STORE | ||
| 266 | 128 | if (item->store_ms != 0U && item->store_ms < CONFIG_KFSW_HK_STORE_FLOOR_MS) { | |
| 267 | ✗ | return -ERANGE; | |
| 268 | } | ||
| 269 | #endif | ||
| 270 | #if CONFIG_KFSW_HK_BEACON | ||
| 271 | 128 | if (item->beacon_ms != 0U && | |
| 272 | 8 | (item->beacon_ms < CONFIG_KFSW_HK_BEACON_FLOOR_MS || item->beacon_node == 0U || | |
| 273 | 8 | item->beacon_node >= KFSW_CSP_BROADCAST_ADDRESS)) { | |
| 274 | ✗ | return -ERANGE; | |
| 275 | } | ||
| 276 | #endif | ||
| 277 | 128 | uint8_t retrieval_class = | |
| 278 | 104 | version >= 3U ? blob[offset + 16U] : KFSW_HK_CLASS_DEFAULT; | |
| 279 | 128 | if (retrieval_class > KFSW_HK_CLASS_MAX) { | |
| 280 | 8 | return -ERANGE; | |
| 281 | } | ||
| 282 | 120 | offset += report_header; | |
| 283 | 120 | if (offset + count * KFSW_HK_PERSIST_ENTRY_SIZE > info.size) { | |
| 284 | ✗ | return -EBADMSG; | |
| 285 | } | ||
| 286 | 336 | for (uint8_t entry = 0; entry < count; entry++) { | |
| 287 | 216 | entries[entry].node = sys_get_be16(&blob[offset]); | |
| 288 | 216 | entries[entry].param_id = sys_get_be16(&blob[offset + 2]); | |
| 289 | 216 | offset += KFSW_HK_PERSIST_ENTRY_SIZE; | |
| 290 | } | ||
| 291 | 120 | result = kfsw_hk_prepare_definition(report, entries, count, &item->definition); | |
| 292 | 120 | if (result != 0) { | |
| 293 | 8 | return result; | |
| 294 | } | ||
| 295 | 112 | item->definition.retrieval_class = retrieval_class; | |
| 296 | } | ||
| 297 | 96 | if (offset != info.size) { | |
| 298 | ✗ | return -EBADMSG; | |
| 299 | } | ||
| 300 | |||
| 301 | /* Prepare every store without replacing an existing sample file. */ | ||
| 302 | 96 | kfsw_hk_storage_lock(); | |
| 303 | #if CONFIG_KFSW_HK_STORE | ||
| 304 | 288 | for (size_t index = 0; index < ARRAY_SIZE(restored); index++) { | |
| 305 | 192 | const struct restored_report *item = &restored[index]; | |
| 306 | |||
| 307 | 192 | result = kfsw_hk_store_restore_prepare( | |
| 308 | 192 | (uint8_t)index, item->store_ms, item->period_ms, | |
| 309 | 192 | KFSW_HK_HEADER_SIZE + item->definition.payload_bytes); | |
| 310 | 192 | if (result != 0) { | |
| 311 | ✗ | kfsw_hk_storage_unlock(); | |
| 312 | ✗ | return result; | |
| 313 | } | ||
| 314 | } | ||
| 315 | #endif | ||
| 316 | 96 | kfsw_hk_lock(); | |
| 317 | 288 | for (size_t index = 0; index < ARRAY_SIZE(restored); index++) { | |
| 318 | 192 | const struct restored_report *item = &restored[index]; | |
| 319 | |||
| 320 | 192 | kfsw_hk_restore_report((uint8_t)index, item->present ? &item->definition : NULL, | |
| 321 | 192 | item->period_ms); | |
| 322 | #if CONFIG_KFSW_HK_BEACON | ||
| 323 | 192 | kfsw_hk_beacon_restore((uint8_t)index, item->beacon_node, item->beacon_ms); | |
| 324 | #endif | ||
| 325 | } | ||
| 326 | #if CONFIG_KFSW_HK_STORE | ||
| 327 | 96 | kfsw_hk_store_restore_commit(); | |
| 328 | #endif | ||
| 329 | 96 | kfsw_hk_unlock(); | |
| 330 | 96 | kfsw_hk_storage_unlock(); | |
| 331 | 96 | return 0; | |
| 332 | } | ||
| 333 | |||
| 334 | 2088 | int kfsw_hk_persist_save(void) | |
| 335 | { | ||
| 336 | 2088 | uint64_t revision = 0; | |
| 337 | int result; | ||
| 338 | |||
| 339 | 2088 | k_mutex_lock(&file_lock, K_FOREVER); | |
| 340 | 2088 | result = save_snapshot(&revision); | |
| 341 | 2088 | kfsw_hk_save_result(revision, result); | |
| 342 | 2088 | k_mutex_unlock(&file_lock); | |
| 343 | 2088 | return result; | |
| 344 | } | ||
| 345 | |||
| 346 | 180 | int kfsw_hk_persist_load(void) | |
| 347 | { | ||
| 348 | int result; | ||
| 349 | |||
| 350 | /* Changes release the config lock before waiting for the file lock. */ | ||
| 351 | 180 | kfsw_hk_restore_begin(); | |
| 352 | 180 | k_mutex_lock(&file_lock, K_FOREVER); | |
| 353 | 180 | result = load_snapshot(); | |
| 354 | 180 | kfsw_hk_restore_end(result); | |
| 355 | 180 | k_mutex_unlock(&file_lock); | |
| 356 | 180 | return result; | |
| 357 | } | ||
| 358 | |||
| 359 | #endif /* CONFIG_KFSW_HK_PERSISTENCE */ | ||
| 360 |