Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / firmware / findfamily-tracker / src / tracker_store.c

tracker_store.c146 lines · 3.5 KiBRaw
1/*
2 * NVS persistence for the provisioning blob and the time base.
3 *
4 * A tracker that forgets its secret is a brick — it can only be recovered by
5 * re-binding — and one that forgets its time base beacons epoch ids outside the
6 * owner's ~2.25 h search window, which looks identical to being out of range. Both
7 * therefore live in flash rather than RAM.
8 */
9#include "ff_tracker.h"
10
11#include <zephyr/kernel.h>
12#include <zephyr/device.h>
13#include <zephyr/drivers/flash.h>
14#include <zephyr/kvss/nvs.h>
15#include <zephyr/logging/log.h>
16#include <zephyr/storage/flash_map.h>
17
18#include <string.h>
19
20LOG_MODULE_REGISTER(ff_store, LOG_LEVEL_INF);
21
22#define NVS_PARTITION storage_partition
23#define NVS_PARTITION_DEVICE PARTITION_DEVICE(NVS_PARTITION)
24#define NVS_PARTITION_OFFSET PARTITION_OFFSET(NVS_PARTITION)
25
26/* NVS ids. Append only — reusing an id would read back another field's bytes. */
27#define ID_USER_ID 1
28#define ID_SECRET 2
29#define ID_UNIX 3
30
31static struct nvs_fs fs;
32static bool initialized;
33
34int ff_store_init(void)
35{
36 struct flash_pages_info info;
37 const struct device *flash_dev = NVS_PARTITION_DEVICE;
38 int rc;
39
40 if (initialized) {
41 return 0;
42 }
43 if (!device_is_ready(flash_dev)) {
44 LOG_ERR("flash device not ready");
45 return -ENODEV;
46 }
47
48 fs.flash_device = flash_dev;
49 fs.offset = NVS_PARTITION_OFFSET;
50 rc = flash_get_page_info_by_offs(flash_dev, fs.offset, &info);
51 if (rc) {
52 LOG_ERR("flash_get_page_info_by_offs: %d", rc);
53 return rc;
54 }
55 fs.sector_size = info.size;
56 fs.sector_count = 3U;
57
58 rc = nvs_mount(&fs);
59 if (rc) {
60 LOG_ERR("nvs_mount: %d", rc);
61 return rc;
62 }
63 initialized = true;
64 return 0;
65}
66
67int ff_store_load(struct ff_tracker_state *out)
68{
69 ssize_t rc;
70
71 if (!initialized) {
72 return -EINVAL;
73 }
74 memset(out, 0, sizeof(*out));
75
76 rc = nvs_read(&fs, ID_SECRET, out->secret, FF_SECRET_LEN);
77 if (rc != FF_SECRET_LEN) {
78 LOG_INF("no provisioning stored (secret read = %d)", (int)rc);
79 return 0;
80 }
81 rc = nvs_read(&fs, ID_USER_ID, &out->tracker_user_id, sizeof(out->tracker_user_id));
82 if (rc != sizeof(out->tracker_user_id)) {
83 /* A secret without a user id can't be reported against, so treat the
84 * pair as absent rather than half-provisioned. */
85 LOG_WRN("secret present but user id missing; ignoring provisioning");
86 memset(out->secret, 0, FF_SECRET_LEN);
87 return 0;
88 }
89 out->provisioned = true;
90
91 rc = nvs_read(&fs, ID_UNIX, &out->unix_at_base, sizeof(out->unix_at_base));
92 if (rc == sizeof(out->unix_at_base)) {
93 out->uptime_at_base_ms = k_uptime_get();
94 } else {
95 out->unix_at_base = 0;
96 LOG_WRN("no time base stored; beacon stays off until re-provisioned");
97 }
98 return 0;
99}
100
101int ff_store_save_provisioning(uint64_t tracker_user_id, const uint8_t *secret,
102 uint64_t unix_seconds)
103{
104 ssize_t rc;
105
106 if (!initialized) {
107 return -EINVAL;
108 }
109 rc = nvs_write(&fs, ID_SECRET, secret, FF_SECRET_LEN);
110 if (rc < 0) {
111 return (int)rc;
112 }
113 rc = nvs_write(&fs, ID_USER_ID, &tracker_user_id, sizeof(tracker_user_id));
114 if (rc < 0) {
115 return (int)rc;
116 }
117 if (unix_seconds != 0U) {
118 rc = nvs_write(&fs, ID_UNIX, &unix_seconds, sizeof(unix_seconds));
119 if (rc < 0) {
120 return (int)rc;
121 }
122 }
123 return 0;
124}
125
126int ff_store_save_time(uint64_t unix_seconds)
127{
128 ssize_t rc;
129
130 if (!initialized) {
131 return -EINVAL;
132 }
133 rc = nvs_write(&fs, ID_UNIX, &unix_seconds, sizeof(unix_seconds));
134 return rc < 0 ? (int)rc : 0;
135}
136
137int ff_store_clear(void)
138{
139 if (!initialized) {
140 return -EINVAL;
141 }
142 (void)nvs_delete(&fs, ID_SECRET);
143 (void)nvs_delete(&fs, ID_USER_ID);
144 (void)nvs_delete(&fs, ID_UNIX);
145 return 0;
146}