Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / findfamily-tracker / src / main.c
main.c325 lines · 9.8 KiBRaw
| 1 | /* |
| 2 | * FindFamily UWB tracker — main state machine. |
| 3 | * |
| 4 | * On boot: load persisted provisioning. Provisioned and time-synced -> beacon. |
| 5 | * Otherwise -> pairing mode, so a brand-new board is bindable straight out of the box |
| 6 | * without needing the button. |
| 7 | * |
| 8 | * Button (the kit's Button 1): |
| 9 | * long press (>= 2 s) -> pairing mode for CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S, then back |
| 10 | * to beaconing. Note a bound tracker cannot be re-provisioned: |
| 11 | * on_provision_write refuses writes once provisioned, which is |
| 12 | * the anti-theft lock. Nothing unbinds a tracker today. |
| 13 | * short press -> toggle the beacon off/on ("mute"), for taking it on a plane |
| 14 | * or debugging whether a sighting really came from this board |
| 15 | * |
| 16 | * The beacon id and battery percent are refreshed once a second and logged, so the |
| 17 | * board can be verified on the bench against an independently computed epoch id |
| 18 | * without involving the phone at all. |
| 19 | */ |
| 20 | #include "ff_tracker.h" |
| 21 | |
| 22 | #include <zephyr/kernel.h> |
| 23 | #include <zephyr/device.h> |
| 24 | #include <zephyr/drivers/gpio.h> |
| 25 | #include <zephyr/drivers/watchdog.h> |
| 26 | #include <zephyr/logging/log.h> |
| 27 | #include <psa/crypto.h> |
| 28 | |
| 29 | LOG_MODULE_REGISTER(ff_main, LOG_LEVEL_INF); |
| 30 | |
| 31 | #define LONG_PRESS_MS 2000 |
| 32 | #define TICK_MS 1000 |
| 33 | |
| 34 | static struct ff_tracker_state tracker_state; |
| 35 | |
| 36 | /* ---- Button ------------------------------------------------------------ */ |
| 37 | |
| 38 | static const struct gpio_dt_spec button = |
| 39 | GPIO_DT_SPEC_GET_OR(DT_ALIAS(sw0), gpios, { 0 }); |
| 40 | static struct gpio_callback button_cb; |
| 41 | |
| 42 | static int64_t press_started_ms; |
| 43 | static atomic_t pending_short_press; |
| 44 | static atomic_t pending_long_press; |
| 45 | |
| 46 | static void button_changed(const struct device *dev, struct gpio_callback *cb, |
| 47 | uint32_t pins) |
| 48 | { |
| 49 | ARG_UNUSED(dev); |
| 50 | ARG_UNUSED(cb); |
| 51 | ARG_UNUSED(pins); |
| 52 | |
| 53 | if (gpio_pin_get_dt(&button) == 1) { |
| 54 | press_started_ms = k_uptime_get(); |
| 55 | return; |
| 56 | } |
| 57 | if (press_started_ms == 0) { |
| 58 | return; |
| 59 | } |
| 60 | if (k_uptime_get() - press_started_ms >= LONG_PRESS_MS) { |
| 61 | atomic_set(&pending_long_press, 1); |
| 62 | } else { |
| 63 | atomic_set(&pending_short_press, 1); |
| 64 | } |
| 65 | press_started_ms = 0; |
| 66 | } |
| 67 | |
| 68 | static int button_init(void) |
| 69 | { |
| 70 | int rc; |
| 71 | |
| 72 | if (!gpio_is_ready_dt(&button)) { |
| 73 | LOG_WRN("no sw0 button on this board; pairing mode is boot-only"); |
| 74 | return -ENODEV; |
| 75 | } |
| 76 | rc = gpio_pin_configure_dt(&button, GPIO_INPUT); |
| 77 | if (rc) { |
| 78 | return rc; |
| 79 | } |
| 80 | rc = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_BOTH); |
| 81 | if (rc) { |
| 82 | return rc; |
| 83 | } |
| 84 | gpio_init_callback(&button_cb, button_changed, BIT(button.pin)); |
| 85 | return gpio_add_callback(button.port, &button_cb); |
| 86 | } |
| 87 | |
| 88 | /* ---- Battery ---------------------------------------------------------- */ |
| 89 | |
| 90 | /* |
| 91 | * The DWM3001CDK is USB-powered on the bench, so there is no meaningful cell voltage |
| 92 | * to read through the kit's SAADC wiring. Report a constant until a battery-backed |
| 93 | * carrier exists; the advertisement byte and the phone-side parsing are exercised |
| 94 | * either way. Sampling VDD here instead would just report the regulator, which reads |
| 95 | * as a permanently full battery and is more misleading than an obvious placeholder. |
| 96 | */ |
| 97 | static uint8_t battery_percent(void) |
| 98 | { |
| 99 | return 100; |
| 100 | } |
| 101 | |
| 102 | /* ---- Main ------------------------------------------------------------- */ |
| 103 | |
| 104 | /* |
| 105 | * Hardware watchdog. |
| 106 | * |
| 107 | * The UWB stack has wedged the firmware more than once, and a hung tracker advertises |
| 108 | * nothing at all — so crowd-finding silently dies with it, and it stays dead until someone |
| 109 | * physically resets the board. That happened for an hour. This bounds the damage to a few |
| 110 | * seconds: the main loop feeds the watchdog on every pass, so if anything stops it running, |
| 111 | * the device reboots and comes back advertising from its persisted state. |
| 112 | * |
| 113 | * The timeout is deliberately several times the loop period, so a slow pass never trips it. |
| 114 | */ |
| 115 | #define WDT_TIMEOUT_MS 8000 |
| 116 | |
| 117 | static const struct device *wdt; |
| 118 | static int wdt_channel = -1; |
| 119 | |
| 120 | static void watchdog_init(void) |
| 121 | { |
| 122 | struct wdt_timeout_cfg cfg = { |
| 123 | .window = { .min = 0U, .max = WDT_TIMEOUT_MS }, |
| 124 | /* No callback: by the time it fires the firmware is untrustworthy, and a |
| 125 | * handler that tries to log could itself hang. Just reset. */ |
| 126 | .callback = NULL, |
| 127 | .flags = WDT_FLAG_RESET_SOC, |
| 128 | }; |
| 129 | int rc; |
| 130 | |
| 131 | wdt = DEVICE_DT_GET_OR_NULL(DT_ALIAS(watchdog0)); |
| 132 | if (wdt == NULL || !device_is_ready(wdt)) { |
| 133 | LOG_WRN("no watchdog available; a hang will not self-recover"); |
| 134 | wdt = NULL; |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | wdt_channel = wdt_install_timeout(wdt, &cfg); |
| 139 | if (wdt_channel < 0) { |
| 140 | LOG_WRN("wdt_install_timeout: %d", wdt_channel); |
| 141 | wdt = NULL; |
| 142 | return; |
| 143 | } |
| 144 | /* |
| 145 | * WDT_OPT_PAUSE_HALTED_BY_DBG so a debugger breakpoint doesn't reboot the board |
| 146 | * mid-inspection. The nRF watchdog cannot be stopped once started, which is exactly |
| 147 | * the property wanted here. |
| 148 | */ |
| 149 | rc = wdt_setup(wdt, WDT_OPT_PAUSE_HALTED_BY_DBG); |
| 150 | if (rc) { |
| 151 | LOG_WRN("wdt_setup: %d", rc); |
| 152 | wdt = NULL; |
| 153 | return; |
| 154 | } |
| 155 | LOG_INF("watchdog armed (%d ms)", WDT_TIMEOUT_MS); |
| 156 | } |
| 157 | |
| 158 | static void watchdog_feed(void) |
| 159 | { |
| 160 | if (wdt != NULL && wdt_channel >= 0) { |
| 161 | (void)wdt_feed(wdt, wdt_channel); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | int main(void) |
| 166 | { |
| 167 | psa_status_t status; |
| 168 | uint64_t last_persisted_epoch = 0; |
| 169 | /* Non-zero while a long press has put a provisioned tracker into pairing mode. */ |
| 170 | int64_t pairing_deadline_ms = 0; |
| 171 | int rc; |
| 172 | |
| 173 | LOG_INF("FindFamily tracker starting"); |
| 174 | |
| 175 | /* |
| 176 | * Before anything that can fail out of main(). Every `return 0` below leaves a live |
| 177 | * board with no advertising and no thread to recover it, which is precisely what |
| 178 | * the watchdog exists to bound: armed first, such a board reboots and retries |
| 179 | * instead of going quiet until someone unplugs it. |
| 180 | */ |
| 181 | watchdog_init(); |
| 182 | |
| 183 | status = psa_crypto_init(); |
| 184 | if (status != PSA_SUCCESS) { |
| 185 | LOG_ERR("psa_crypto_init: %d", (int)status); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | rc = ff_store_init(); |
| 190 | if (rc) { |
| 191 | LOG_ERR("store init failed: %d", rc); |
| 192 | return 0; |
| 193 | } |
| 194 | rc = ff_store_load(&tracker_state); |
| 195 | if (rc) { |
| 196 | LOG_ERR("store load failed: %d", rc); |
| 197 | } |
| 198 | |
| 199 | rc = ff_ble_init(&tracker_state); |
| 200 | if (rc) { |
| 201 | LOG_ERR("ble init failed: %d", rc); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | (void)button_init(); |
| 206 | |
| 207 | #ifdef CONFIG_FF_TRACKER_UWB |
| 208 | /* Crypto, NVS and bt_enable() are behind us; don't make the UWB bring-up share |
| 209 | * their slice of the watchdog window. */ |
| 210 | watchdog_feed(); |
| 211 | rc = ff_uwb_init(); |
| 212 | if (rc) { |
| 213 | LOG_ERR("uwb init failed: %d", rc); |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | if (tracker_state.provisioned) { |
| 218 | LOG_INF("provisioned as userId=%llu", tracker_state.tracker_user_id); |
| 219 | if (ff_ble_set_mode(FF_BLE_MODE_BEACON) != 0) { |
| 220 | /* |
| 221 | * Provisioned but unusable — almost always a missing time base, |
| 222 | * without which no epoch id would fall in the owner's search |
| 223 | * window. Nothing can currently repair that over the air |
| 224 | * (on_provision_write refuses writes once provisioned), so pairing |
| 225 | * mode here only makes the tracker visible to the owner's app |
| 226 | * rather than silent. |
| 227 | */ |
| 228 | (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); |
| 229 | } |
| 230 | } else { |
| 231 | LOG_INF("unprovisioned; entering pairing mode"); |
| 232 | (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); |
| 233 | } |
| 234 | |
| 235 | while (true) { |
| 236 | watchdog_feed(); |
| 237 | |
| 238 | if (ff_ble_take_provision_event()) { |
| 239 | LOG_INF("provisioning accepted: switching to beacon mode"); |
| 240 | if (ff_ble_set_mode(FF_BLE_MODE_BEACON) != 0) { |
| 241 | /* Expected while the phone is still connected: a connectable |
| 242 | * advertisement needs a free connection slot. The disconnect |
| 243 | * handler asks us to retry. */ |
| 244 | LOG_INF("beacon will start once the phone disconnects"); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (ff_ble_take_readvertise_event()) { |
| 249 | LOG_INF("connection ended: re-applying advertising"); |
| 250 | (void)ff_ble_readvertise(); |
| 251 | } |
| 252 | |
| 253 | { |
| 254 | uint8_t uwb_params[FF_UWB_PARAMS_LEN]; |
| 255 | |
| 256 | if (ff_ble_take_uwb_params(uwb_params)) { |
| 257 | (void)ff_uwb_on_params(uwb_params, sizeof(uwb_params), |
| 258 | tracker_state.secret); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (atomic_cas(&pending_long_press, 1, 0)) { |
| 263 | LOG_INF("long press: entering pairing mode"); |
| 264 | (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); |
| 265 | pairing_deadline_ms = 0; |
| 266 | if (tracker_state.provisioned) { |
| 267 | /* |
| 268 | * Bound trackers go back to beaconing on their own. Leaving |
| 269 | * the unprovisioned service on air indefinitely both stops |
| 270 | * crowd-finding and widens the window in which anything |
| 271 | * nearby could try to claim the tracker. An unprovisioned |
| 272 | * board has nothing to go back to, so it keeps advertising |
| 273 | * until it is bound. |
| 274 | * |
| 275 | * Measured from the press, not from when the advertisement |
| 276 | * starts: if it could not start yet — the connection slot was |
| 277 | * busy — ff_ble_readvertise() starts it later, and the window |
| 278 | * still expires rather than running unbounded. |
| 279 | */ |
| 280 | pairing_deadline_ms = |
| 281 | k_uptime_get() + |
| 282 | CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S * MSEC_PER_SEC; |
| 283 | } |
| 284 | } |
| 285 | if (pairing_deadline_ms != 0 && ff_ble_mode() == FF_BLE_MODE_PAIRING && |
| 286 | k_uptime_get() >= pairing_deadline_ms) { |
| 287 | pairing_deadline_ms = 0; |
| 288 | LOG_INF("pairing window over: back to beaconing"); |
| 289 | if (ff_ble_set_mode(FF_BLE_MODE_BEACON) != 0) { |
| 290 | /* Same fallback as at boot: beacon mode needs a time base, |
| 291 | * and a tracker advertising nothing at all is worse than one |
| 292 | * still offering to bind. */ |
| 293 | (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); |
| 294 | } |
| 295 | } |
| 296 | if (atomic_cas(&pending_short_press, 1, 0)) { |
| 297 | if (ff_ble_mode() == FF_BLE_MODE_BEACON) { |
| 298 | LOG_INF("short press: muting beacon"); |
| 299 | (void)ff_ble_set_mode(FF_BLE_MODE_IDLE); |
| 300 | } else if (tracker_state.provisioned) { |
| 301 | LOG_INF("short press: unmuting beacon"); |
| 302 | (void)ff_ble_set_mode(FF_BLE_MODE_BEACON); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (tracker_state.provisioned) { |
| 307 | uint64_t epoch = ff_current_epoch(&tracker_state); |
| 308 | |
| 309 | (void)ff_ble_refresh_beacon(battery_percent()); |
| 310 | |
| 311 | /* |
| 312 | * Re-persist the time base once per epoch. k_uptime_get() restarts |
| 313 | * at 0 on reset, so without this a reset would lose the time base |
| 314 | * entirely; with it, the worst case is one epoch of drift. |
| 315 | */ |
| 316 | if (epoch != 0U && epoch != last_persisted_epoch) { |
| 317 | (void)ff_store_save_time(ff_now_unix(&tracker_state)); |
| 318 | last_persisted_epoch = epoch; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | k_msleep(TICK_MS); |
| 323 | } |
| 324 | return 0; |
| 325 | } |