findfamily: keep a wedged UWB session from taking the tracker down
- Commit
456d6cc2fe093fe4b21d58ff4daeeeb8fb06b7fa- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-22 02:06 UTC (3 months ago)
- Parents
490df61
Diff
firmware/findfamily-tracker/prj.conf
diff --git a/firmware/findfamily-tracker/prj.conf b/firmware/findfamily-tracker/prj.confindex d5ffcbeb4e..49179a17a2 100644--- a/firmware/findfamily-tracker/prj.conf+++ b/firmware/findfamily-tracker/prj.conf@@ -58,6 +58,11 @@ CONFIG_SETTINGS_NVS=y # --- Board I/O ----------------------------------------------------------- CONFIG_GPIO=y +# Hardware watchdog, fed by the main loop. A hung tracker advertises nothing, so a hang+# takes crowd-finding down with it and stays down until someone resets the board by hand.+# This turns that into a few seconds of downtime. See watchdog_init() in src/main.c.+CONFIG_WATCHDOG=y+ # --- Logging over the on-board J-Link CDC UART --------------------------- CONFIG_LOG=y CONFIG_SERIAL=y
firmware/findfamily-tracker/src/main.c
diff --git a/firmware/findfamily-tracker/src/main.c b/firmware/findfamily-tracker/src/main.cindex f97f5b277a..412ef0026a 100644--- a/firmware/findfamily-tracker/src/main.c+++ b/firmware/findfamily-tracker/src/main.c@@ -20,6 +20,7 @@ #include <zephyr/kernel.h> #include <zephyr/device.h> #include <zephyr/drivers/gpio.h>+#include <zephyr/drivers/watchdog.h> #include <zephyr/logging/log.h> #include <psa/crypto.h> @@ -98,6 +99,67 @@ static uint8_t battery_percent(void) /* ---- 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;@@ -129,6 +191,7 @@ int main(void) } (void)button_init();+ watchdog_init(); #ifdef CONFIG_FF_TRACKER_UWB rc = ff_uwb_init();@@ -150,6 +213,8 @@ int main(void) } 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) {
firmware/findfamily-tracker/src/tracker_uwb.c
diff --git a/firmware/findfamily-tracker/src/tracker_uwb.c b/firmware/findfamily-tracker/src/tracker_uwb.cindex 54d880e3c5..c69216734a 100644--- a/firmware/findfamily-tracker/src/tracker_uwb.c+++ b/firmware/findfamily-tracker/src/tracker_uwb.c@@ -72,6 +72,26 @@ static K_THREAD_STACK_DEFINE(uwb_thread_stack, UWB_THREAD_STACK_SIZE); static struct k_thread uwb_thread; static atomic_t poll_running; +/*+ * Session setup runs on its own thread rather than on the caller's.+ *+ * The fira_helper_* and uwbmac_start calls block, and at least one of them can block+ * indefinitely when the stack is unhappy. Called straight from the main loop — which is+ * also what refreshes the beacon and drives the mode state machine — a stall there took+ * the whole tracker off the air, so crowd-finding died along with ranging. Keeping it on a+ * separate thread means the worst a wedged UWB session can do is lose UWB.+ */+#define UWB_SESSION_THREAD_STACK_SIZE 4096+#define UWB_SESSION_THREAD_PRIORITY 6++static K_THREAD_STACK_DEFINE(uwb_session_stack, UWB_SESSION_THREAD_STACK_SIZE);+static struct k_thread uwb_session_thread_data;+static K_SEM_DEFINE(session_request, 0, 1);+static struct ff_uwb_params requested_params;+static uint8_t requested_secret[FF_SECRET_LEN];++static void uwb_session_thread(void *a, void *b, void *c);+ /* * Board calibration hook, from the vendor's DWM3001CDK support (built by * firmware/qorvo-uwb). It loads this module's factory calibration — antenna delays,@@ -212,6 +232,11 @@ int ff_uwb_init(void) stack_ready = true; LOG_INF("Qorvo uwbstack up; DW3110 identified");++ k_thread_create(&uwb_session_thread_data, uwb_session_stack,+ UWB_SESSION_THREAD_STACK_SIZE, uwb_session_thread, NULL, NULL, NULL,+ UWB_SESSION_THREAD_PRIORITY, 0, K_NO_WAIT);+ k_thread_name_set(&uwb_session_thread_data, "ff_uwb_sess"); return 0; } @@ -441,19 +466,41 @@ void ff_uwb_stop(void) LOG_INF("FiRa session stopped"); } -int ff_uwb_on_params(const uint8_t *data, size_t len, const uint8_t *secret)+/*+ * Runs session setup off the main loop; see the note by uwb_session_stack. One request at a+ * time is enough — the phone writes params once per find, and a second write while a session+ * is starting is better dropped than queued behind a possibly-wedged one.+ */+static void uwb_session_thread(void *a, void *b, void *c) {- struct ff_uwb_params params;+ ARG_UNUSED(a);+ ARG_UNUSED(b);+ ARG_UNUSED(c);++ while (true) {+ k_sem_take(&session_request, K_FOREVER);+ (void)ff_uwb_start(&requested_params, requested_secret);+ }+} +int ff_uwb_on_params(const uint8_t *data, size_t len, const uint8_t *secret)+{ if (len != FF_UWB_PARAMS_LEN) { return -EINVAL; }- /* TrackerUwbGatt.encodeSessionParams: [2B addr][4B sessionId BE][1B ch][1B pre] */- params.peer_address[0] = data[0];- params.peer_address[1] = data[1];- params.session_id = sys_get_be32(&data[2]);- params.channel = data[6];- params.preamble_index = data[7];+ if (!stack_ready) {+ return -EAGAIN;+ } - return ff_uwb_start(¶ms, secret);+ /* TrackerUwbGatt.encodeSessionParams: [2B addr][4B sessionId BE][1B ch][1B pre] */+ requested_params.peer_address[0] = data[0];+ requested_params.peer_address[1] = data[1];+ requested_params.session_id = sys_get_be32(&data[2]);+ requested_params.channel = data[6];+ requested_params.preamble_index = data[7];+ memcpy(requested_secret, secret, sizeof(requested_secret));++ /* Hand off and return immediately: the caller is the main loop. */+ k_sem_give(&session_request);+ return 0; }