/* * FindFamily UWB tracker — main state machine. * * On boot: load persisted provisioning. Provisioned and time-synced -> beacon. * Otherwise -> pairing mode, so a brand-new board is bindable straight out of the box * without needing the button. * * Button (the kit's Button 1): * long press (>= 2 s) -> pairing mode for CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S, then back * to beaconing. Note a bound tracker cannot be re-provisioned: * on_provision_write refuses writes once provisioned, which is * the anti-theft lock. Nothing unbinds a tracker today. * short press -> toggle the beacon off/on ("mute"), for taking it on a plane * or debugging whether a sighting really came from this board * * The beacon id and battery percent are refreshed once a second and logged, so the * board can be verified on the bench against an independently computed epoch id * without involving the phone at all. */ #include "ff_tracker.h" #include #include #include #include #include #include LOG_MODULE_REGISTER(ff_main, LOG_LEVEL_INF); #define LONG_PRESS_MS 2000 #define TICK_MS 1000 static struct ff_tracker_state tracker_state; /* ---- Button ------------------------------------------------------------ */ static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(DT_ALIAS(sw0), gpios, { 0 }); static struct gpio_callback button_cb; static int64_t press_started_ms; static atomic_t pending_short_press; static atomic_t pending_long_press; static void button_changed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { ARG_UNUSED(dev); ARG_UNUSED(cb); ARG_UNUSED(pins); if (gpio_pin_get_dt(&button) == 1) { press_started_ms = k_uptime_get(); return; } if (press_started_ms == 0) { return; } if (k_uptime_get() - press_started_ms >= LONG_PRESS_MS) { atomic_set(&pending_long_press, 1); } else { atomic_set(&pending_short_press, 1); } press_started_ms = 0; } static int button_init(void) { int rc; if (!gpio_is_ready_dt(&button)) { LOG_WRN("no sw0 button on this board; pairing mode is boot-only"); return -ENODEV; } rc = gpio_pin_configure_dt(&button, GPIO_INPUT); if (rc) { return rc; } rc = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_BOTH); if (rc) { return rc; } gpio_init_callback(&button_cb, button_changed, BIT(button.pin)); return gpio_add_callback(button.port, &button_cb); } /* ---- Battery ---------------------------------------------------------- */ /* * The DWM3001CDK is USB-powered on the bench, so there is no meaningful cell voltage * to read through the kit's SAADC wiring. Report a constant until a battery-backed * carrier exists; the advertisement byte and the phone-side parsing are exercised * either way. Sampling VDD here instead would just report the regulator, which reads * as a permanently full battery and is more misleading than an obvious placeholder. */ static uint8_t battery_percent(void) { return 100; } /* ---- Main ------------------------------------------------------------- */ /* * Hardware watchdog. * * The UWB stack has wedged the firmware more than once, and a hung tracker advertises * nothing at all — so crowd-finding silently dies with it, and it stays dead until someone * physically resets the board. That happened for an hour. This bounds the damage to a few * seconds: the main loop feeds the watchdog on every pass, so if anything stops it running, * the device reboots and comes back advertising from its persisted state. * * The timeout is deliberately several times the loop period, so a slow pass never trips it. */ #define WDT_TIMEOUT_MS 8000 static const struct device *wdt; static int wdt_channel = -1; static void watchdog_init(void) { struct wdt_timeout_cfg cfg = { .window = { .min = 0U, .max = WDT_TIMEOUT_MS }, /* No callback: by the time it fires the firmware is untrustworthy, and a * handler that tries to log could itself hang. Just reset. */ .callback = NULL, .flags = WDT_FLAG_RESET_SOC, }; int rc; wdt = DEVICE_DT_GET_OR_NULL(DT_ALIAS(watchdog0)); if (wdt == NULL || !device_is_ready(wdt)) { LOG_WRN("no watchdog available; a hang will not self-recover"); wdt = NULL; return; } wdt_channel = wdt_install_timeout(wdt, &cfg); if (wdt_channel < 0) { LOG_WRN("wdt_install_timeout: %d", wdt_channel); wdt = NULL; return; } /* * WDT_OPT_PAUSE_HALTED_BY_DBG so a debugger breakpoint doesn't reboot the board * mid-inspection. The nRF watchdog cannot be stopped once started, which is exactly * the property wanted here. */ rc = wdt_setup(wdt, WDT_OPT_PAUSE_HALTED_BY_DBG); if (rc) { LOG_WRN("wdt_setup: %d", rc); wdt = NULL; return; } LOG_INF("watchdog armed (%d ms)", WDT_TIMEOUT_MS); } static void watchdog_feed(void) { if (wdt != NULL && wdt_channel >= 0) { (void)wdt_feed(wdt, wdt_channel); } } int main(void) { psa_status_t status; uint64_t last_persisted_epoch = 0; /* Non-zero while a long press has put a provisioned tracker into pairing mode. */ int64_t pairing_deadline_ms = 0; int rc; LOG_INF("FindFamily tracker starting"); /* * Before anything that can fail out of main(). Every `return 0` below leaves a live * board with no advertising and no thread to recover it, which is precisely what * the watchdog exists to bound: armed first, such a board reboots and retries * instead of going quiet until someone unplugs it. */ watchdog_init(); status = psa_crypto_init(); if (status != PSA_SUCCESS) { LOG_ERR("psa_crypto_init: %d", (int)status); return 0; } rc = ff_store_init(); if (rc) { LOG_ERR("store init failed: %d", rc); return 0; } rc = ff_store_load(&tracker_state); if (rc) { LOG_ERR("store load failed: %d", rc); } rc = ff_ble_init(&tracker_state); if (rc) { LOG_ERR("ble init failed: %d", rc); return 0; } (void)button_init(); #ifdef CONFIG_FF_TRACKER_UWB /* Crypto, NVS and bt_enable() are behind us; don't make the UWB bring-up share * their slice of the watchdog window. */ watchdog_feed(); rc = ff_uwb_init(); if (rc) { LOG_ERR("uwb init failed: %d", rc); } #endif if (tracker_state.provisioned) { LOG_INF("provisioned as userId=%llu", tracker_state.tracker_user_id); if (ff_ble_set_mode(FF_BLE_MODE_BEACON) != 0) { /* * Provisioned but unusable — almost always a missing time base, * without which no epoch id would fall in the owner's search * window. Nothing can currently repair that over the air * (on_provision_write refuses writes once provisioned), so pairing * mode here only makes the tracker visible to the owner's app * rather than silent. */ (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); } } else { LOG_INF("unprovisioned; entering pairing mode"); (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); } while (true) { watchdog_feed(); if (ff_ble_take_provision_event()) { LOG_INF("provisioning accepted: switching to beacon mode"); if (ff_ble_set_mode(FF_BLE_MODE_BEACON) != 0) { /* Expected while the phone is still connected: a connectable * advertisement needs a free connection slot. The disconnect * handler asks us to retry. */ LOG_INF("beacon will start once the phone disconnects"); } } if (ff_ble_take_readvertise_event()) { LOG_INF("connection ended: re-applying advertising"); (void)ff_ble_readvertise(); } { uint8_t uwb_params[FF_UWB_PARAMS_LEN]; if (ff_ble_take_uwb_params(uwb_params)) { (void)ff_uwb_on_params(uwb_params, sizeof(uwb_params), tracker_state.secret); } } if (atomic_cas(&pending_long_press, 1, 0)) { LOG_INF("long press: entering pairing mode"); (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); pairing_deadline_ms = 0; if (tracker_state.provisioned) { /* * Bound trackers go back to beaconing on their own. Leaving * the unprovisioned service on air indefinitely both stops * crowd-finding and widens the window in which anything * nearby could try to claim the tracker. An unprovisioned * board has nothing to go back to, so it keeps advertising * until it is bound. * * Measured from the press, not from when the advertisement * starts: if it could not start yet — the connection slot was * busy — ff_ble_readvertise() starts it later, and the window * still expires rather than running unbounded. */ pairing_deadline_ms = k_uptime_get() + CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S * MSEC_PER_SEC; } } if (pairing_deadline_ms != 0 && ff_ble_mode() == FF_BLE_MODE_PAIRING && k_uptime_get() >= pairing_deadline_ms) { pairing_deadline_ms = 0; LOG_INF("pairing window over: back to beaconing"); if (ff_ble_set_mode(FF_BLE_MODE_BEACON) != 0) { /* Same fallback as at boot: beacon mode needs a time base, * and a tracker advertising nothing at all is worse than one * still offering to bind. */ (void)ff_ble_set_mode(FF_BLE_MODE_PAIRING); } } if (atomic_cas(&pending_short_press, 1, 0)) { if (ff_ble_mode() == FF_BLE_MODE_BEACON) { LOG_INF("short press: muting beacon"); (void)ff_ble_set_mode(FF_BLE_MODE_IDLE); } else if (tracker_state.provisioned) { LOG_INF("short press: unmuting beacon"); (void)ff_ble_set_mode(FF_BLE_MODE_BEACON); } } if (tracker_state.provisioned) { uint64_t epoch = ff_current_epoch(&tracker_state); (void)ff_ble_refresh_beacon(battery_percent()); /* * Re-persist the time base once per epoch. k_uptime_get() restarts * at 0 on reset, so without this a reset would lose the time base * entirely; with it, the worst case is one epoch of drift. */ if (epoch != 0U && epoch != last_persisted_epoch) { (void)ff_store_save_time(ff_now_unix(&tracker_state)); last_persisted_epoch = epoch; } } k_msleep(TICK_MS); } return 0; }