From 7159a00f05a8fa4ec51448a1ab9ef4430c1d37f1 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Wed, 4 Nov 2026 00:21:54 +0100 Subject: [PATCH] simple_sensor: dfu, radio silence watchdog and `get boot` via shared NodeMaintenance Co-Authored-By: Claude Opus 5.5 --- docs/cli_commands.md | 18 ++- examples/simple_sensor/main.cpp | 8 ++ src/helpers/NodeMaintenance.cpp | 211 ++++++++++++++++++++++++++++++++ src/helpers/NodeMaintenance.h | 69 +++++++++++ 4 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 src/helpers/NodeMaintenance.cpp create mode 100644 src/helpers/NodeMaintenance.h diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 1c156bc7..5f9b2f6c 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -718,7 +718,7 @@ so the icon and the Grafana percentage tell one story. --- -#### Radio silence watchdog (repeater only) +#### Radio silence watchdog (repeater and sensor) **Usage:** - `get radio.silence` - `set radio.silence ` @@ -735,6 +735,22 @@ To keep a quiet site out of a reboot loop, each such reboot doubles the threshol --- +#### Reboot into the bootloader (repeater and sensor, nRF52, local console only) +**Usage:** +- `dfu [uf2|serial|ota]` + +**Description:** Reboots straight into the Adafruit bootloader: `uf2` (default) brings up the UF2 drive, `serial` only the CDC port for `adafruit-nrfutil`, `ota` BLE DFU. Refused over the mesh, where it would leave the node waiting in the bootloader with no way back. + +--- + +#### Why the board last reset (repeater and sensor) +**Usage:** +- `get boot` + +**Description:** The reset reason latched at boot (`RESETREAS` on nRF52: `0x1` reset pin, `0x2` watchdog, `0x4` software reset, `0x8` lockup, `0` power-on) and, if the board crashed, the hardfault record (PC, LR, PSR). The same is printed once at boot, but on a build without a settle delay the console is not open yet at that moment. + +--- + #### Enable or disable Multi-Acks support **Usage:** - `get multi.acks` diff --git a/examples/simple_sensor/main.cpp b/examples/simple_sensor/main.cpp index 66991daa..7bac93ce 100644 --- a/examples/simple_sensor/main.cpp +++ b/examples/simple_sensor/main.cpp @@ -1,4 +1,8 @@ #include "SensorMesh.h" +#include + +// CUSTOM (TeTeHacko): dfu, radio silence watchdog, reset reason -- see NodeMaintenance.h. +static NodeMaintenance maint(board, radio_driver); #ifdef DISPLAY_CLASS #include "UITask.h" @@ -32,6 +36,7 @@ protected: } bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) override { + if (maint.handleCommand(sender_timestamp, command, reply)) return true; if (strcmp(command, "magic") == 0) { // example 'custom' command handling strcpy(reply, "**Magic now done**"); return true; // handled @@ -57,6 +62,7 @@ void setup() { delay(1000); board.begin(); + maint.printBootDiag(); #ifdef HAS_EXTERNAL_WATCHDOG external_watchdog.begin(); @@ -109,6 +115,7 @@ void setup() { sensors.begin(); the_mesh.begin(fs); + maint.begin(fs, the_mesh.getNodePrefs(), &the_mesh); #ifdef DISPLAY_CLASS ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); @@ -156,6 +163,7 @@ void loop() { board.loop(); // let the board feed its watchdog, run periodic housekeeping the_mesh.loop(); + maint.loop(); sensors.loop(); #ifdef DISPLAY_CLASS ui_task.loop(); diff --git a/src/helpers/NodeMaintenance.cpp b/src/helpers/NodeMaintenance.cpp new file mode 100644 index 00000000..8ed89ad7 --- /dev/null +++ b/src/helpers/NodeMaintenance.cpp @@ -0,0 +1,211 @@ +#include "NodeMaintenance.h" + +// CUSTOM (TeTeHacko): moved out of examples/simple_repeater/main.cpp, where the +// long history of each piece still sits next to the repeater's own setup -- the +// why of the dfu USB detach, of RESETREAS and of the hardfault record is there. + +#define RADIO_SILENCE_FILE "/rsilence" + +#if defined(NRF52_PLATFORM) +extern "C" { + void enterUf2Dfu(void); + void enterSerialDfu(void); + void enterOTADfu(void); + extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit: magic, pc, lr, psr +} +#endif + +void NodeMaintenance::printBootDiag() { +#if defined(NRF52_PLATFORM) + uint32_t rr = readResetReason(); + _reset_reason = rr; + // The build epoch belongs next to it: it is what the clock comes up at when + // nothing else sets it. + Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long) FIRMWARE_BUILD_EPOCH); + Serial.printf("boot: RESETREAS=0x%08lX%s%s%s%s%s\n", (unsigned long)rr, + rr == 0 ? " (none set = power-on/brownout: power was removed)" : "", + (rr & 0x01) ? " RESETPIN" : "", + (rr & 0x02) ? " WATCHDOG" : "", + (rr & 0x04) ? " SREQ(sw reset, e.g. `dfu`/`reboot`)" : "", + (rr & 0x08) ? " LOCKUP(!)" : ""); + + volatile uint32_t* hf = g_hardfault_record; + if (hf[0] == 0xFA010DEB) { + _hardfault[0] = hf[1]; _hardfault[1] = hf[2]; _hardfault[2] = hf[3]; + _hardfault_seen = true; + Serial.printf("!!! HARDFAULT before last reboot: PC=%08lX LR=%08lX PSR=%08lX\n", + (unsigned long)hf[1], (unsigned long)hf[2], (unsigned long)hf[3]); + hf[0] = 0; // printed once; `get boot` keeps it until the next reset + } +#endif +} + +// --- radio silence watchdog (#6), see RadioSilenceWatchdog.h ---------------- +// Reboots it caused and the threshold backoff survive power loss in a small file, +// written only right before such a reboot and once when the radio proves itself. + +void NodeMaintenance::silenceLoad(uint8_t* backoff) { + *backoff = 0; + if (!_fs || !_fs->exists(RADIO_SILENCE_FILE)) return; +#if defined(RP2040_PLATFORM) + File f = _fs->open(RADIO_SILENCE_FILE, "r"); +#else + File f = _fs->open(RADIO_SILENCE_FILE); +#endif + if (!f) return; + if (f.read((uint8_t*)&_silence_reboots, sizeof(_silence_reboots)) != sizeof(_silence_reboots)) { + _silence_reboots = 0; + } + if (f.read(backoff, 1) != 1) *backoff = 0; + f.close(); +} + +void NodeMaintenance::silenceSave(uint8_t backoff) { + if (!_fs) return; +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) + _fs->remove(RADIO_SILENCE_FILE); + File f = _fs->open(RADIO_SILENCE_FILE, FILE_O_WRITE); +#elif defined(RP2040_PLATFORM) + File f = _fs->open(RADIO_SILENCE_FILE, "w"); +#else + File f = _fs->open(RADIO_SILENCE_FILE, "w", true); +#endif + if (!f) return; + f.write((const uint8_t*)&_silence_reboots, sizeof(_silence_reboots)); + f.write(&backoff, 1); + f.close(); +} + +void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks) { + _fs = fs; + _prefs = prefs; + _callbacks = callbacks; + uint8_t backoff; + silenceLoad(&backoff); + _silence.begin(millis(), radioActivity(), backoff); + _silence.setMinutes(_prefs->radio_silence_min); + Serial.printf("boot: radio silence watchdog %u min, reboots %lu, backoff x%u\n", + (unsigned) _silence.minutes(), (unsigned long) _silence_reboots, + (unsigned) (1u << _silence.backoff())); +} + +void NodeMaintenance::loop() { + dfuTick(); + switch (_silence.poll(millis(), radioActivity())) { + case RadioSilenceWatchdog::KICK_RX: + Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n", + (unsigned long) (_silence.quietMs(millis()) / 60000)); + _radio.kickRx(); + break; + case RadioSilenceWatchdog::REBOOT: + Serial.printf("radio silence: still nothing after %lu min, rebooting\n", + (unsigned long) (_silence.quietMs(millis()) / 60000)); + _silence_reboots++; + silenceSave(_silence.nextBackoff()); + delay(100); // let the line out + _board.reboot(); + break; + default: + break; + } + if (_silence.takeBackoffCleared()) silenceSave(0); +} + +bool NodeMaintenance::silenceCommand(const char* command, char* reply) { + if (strcmp(command, "get radio.reboots") == 0) { + sprintf(reply, "> %lu", (unsigned long) _silence_reboots); + return true; + } + if (strcmp(command, "get radio.silence") == 0) { + if (_silence.minutes() == 0) { + strcpy(reply, "> off"); + } else { + sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u", + (unsigned) _silence.minutes(), (unsigned) (1u << _silence.backoff()), + (unsigned long) (_silence.quietMs(millis()) / 60000), (unsigned) _silence.stage()); + } + return true; + } + if (memcmp(command, "set radio.silence ", 18) == 0) { + const char* arg = &command[18]; + bool numeric = *arg != 0 && strspn(arg, "0123456789") == strlen(arg); + int m = strcmp(arg, "off") == 0 ? 0 : (numeric ? atoi(arg) : -1); + if (m < 0 || m > RadioSilenceWatchdog::MAX_MINUTES) { + sprintf(reply, "ERR - minutes 0..%u (0/off = disabled)", (unsigned) RadioSilenceWatchdog::MAX_MINUTES); + return true; + } + _prefs->radio_silence_min = (uint16_t) m; + _callbacks->savePrefs(); + _silence.setMinutes((uint16_t) m); + strcpy(reply, "OK"); + return true; + } + return false; +} + +// --- dfu [uf2|serial|ota] ------------------------------------------------------ +// The Adafruit bootloader picks its mode from GPREGRET at boot; the core's +// enter*Dfu() write the magic and reset. LOCAL ONLY: over the mesh this would +// strand a node in a bootloader that waits forever. + +// Deferred so the reply reaches the console first -- the reset never returns. +void NodeMaintenance::dfuTick() { +#if defined(NRF52_PLATFORM) + if (_dfu_at == 0 || (long)(millis() - _dfu_at) < 0) return; + _dfu_at = 0; + // Detach USB before the reset instead of letting the reset yank it out from + // under the host (see simple_repeater/main.cpp for the measurements). + NRF_USBD->USBPULLUP = 0; + delay(150); + if (_dfu_mode == 1) enterSerialDfu(); + else if (_dfu_mode == 2) enterOTADfu(); + else enterUf2Dfu(); +#endif +} + +bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char* command, char* reply) { + if (memcmp(command, "dfu", 3) != 0 || (command[3] != 0 && command[3] != ' ')) return false; +#if defined(NRF52_PLATFORM) + if (sender_timestamp != 0) { + strcpy(reply, "ERR: dfu is local-only (would strand the node in the bootloader)"); + return true; + } + const char* arg = (command[3] == ' ') ? &command[4] : ""; + while (*arg == ' ') arg++; + if (strcmp(arg, "serial") == 0) { + strcpy(reply, "OK - rebooting into serial DFU (CDC only, no drive)"); + } else if (strcmp(arg, "ota") == 0) { + strcpy(reply, "OK - rebooting into BLE OTA DFU"); + } else if (arg[0] == 0 || strcmp(arg, "uf2") == 0) { + strcpy(reply, "OK - rebooting into UF2 (drive appears in a few seconds)"); + } else { + strcpy(reply, "ERR: dfu [uf2|serial|ota]"); + return true; + } + _dfu_at = millis() + 700; + _dfu_mode = (strcmp(arg, "serial") == 0) ? 1 : (strcmp(arg, "ota") == 0 ? 2 : 0); +#else + strcpy(reply, "ERR: not an nRF52 board"); +#endif + return true; +} + +bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) { + if (dfuCommand(sender_timestamp, command, reply)) return true; + if (silenceCommand(command, reply)) return true; + if (strcmp(command, "get boot") == 0) { +#if defined(NRF52_PLATFORM) + if (_hardfault_seen) { + sprintf(reply, "> RESETREAS=0x%08lX, HARDFAULT before it: PC=%08lX LR=%08lX PSR=%08lX", + (unsigned long) _reset_reason, (unsigned long) _hardfault[0], + (unsigned long) _hardfault[1], (unsigned long) _hardfault[2]); + } else { + sprintf(reply, "> RESETREAS=0x%08lX, no hardfault", (unsigned long) _reset_reason); + } +#else + strcpy(reply, "> not recorded on this platform"); +#endif + return true; + } + return false; +} diff --git a/src/helpers/NodeMaintenance.h b/src/helpers/NodeMaintenance.h new file mode 100644 index 00000000..438fcfea --- /dev/null +++ b/src/helpers/NodeMaintenance.h @@ -0,0 +1,69 @@ +#pragma once + +// CUSTOM (TeTeHacko): upkeep that belongs to the BOARD, not to the node's role -- +// so every build with a text console gets it, not only the repeater. +// +// It grew in examples/simple_repeater/main.cpp, because every node of ours on the +// mesh is a repeater and each feature was written for the problem at hand on a +// mast. That left the sensor on the birdhouse (x4) without `dfu` (flashable only +// with the 1200-baud touch) and without the radio silence watchdog, although it +// was the first node seen deaf (31. 10. 2026). Living here, it costs a role one +// line per hook, and the upstream files it touches stay one added line each. +// +// dfu [uf2|serial|ota] reboot into the bootloader (local console only) +// set radio.silence radio silence watchdog, 0/off = disabled (#6) +// get radio.silence threshold, backoff, quiet time, stage +// get radio.reboots reboots done by the watchdog, survives power loss +// get boot why the board last reset, and the hardfault record +// +// Wiring (see simple_sensor/main.cpp): +// setup(): maint.printBootDiag() early, maint.begin(fs, prefs, cb) after the +// mesh has loaded its prefs +// loop(): maint.loop() +// commands: if (maint.handleCommand(sender_timestamp, command, reply)) return; + +#include +#include +#include +#include +#include + +class NodeMaintenance { +public: + NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio) { } + + // Prints (and latches for `get boot`) the reset reason and the hardfault record. + // The record is cleared once read, so call it once, as early as Serial is up. + void printBootDiag(); + + void begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks); + void loop(); + + // sender_timestamp == 0 means the local console (the same test CommonCLI uses + // for `erase`); `dfu` refuses anything else. + bool handleCommand(uint32_t sender_timestamp, const char* command, char* reply); + +private: + uint32_t radioActivity() const { return _radio.getPacketsRecv() + _radio.getPacketsRecvErrors(); } + void silenceLoad(uint8_t* backoff); + void silenceSave(uint8_t backoff); + bool dfuCommand(uint32_t sender_timestamp, const char* command, char* reply); + bool silenceCommand(const char* command, char* reply); + void dfuTick(); + + mesh::MainBoard& _board; + RadioLibWrapper& _radio; + FILESYSTEM* _fs = NULL; + NodePrefs* _prefs = NULL; + CommonCLICallbacks* _callbacks = NULL; + + RadioSilenceWatchdog _silence; + uint32_t _silence_reboots = 0; + + unsigned long _dfu_at = 0; // 0 = nothing armed + uint8_t _dfu_mode = 0; // 0=uf2 1=serial 2=ota + + uint32_t _reset_reason = 0; + uint32_t _hardfault[3] = {0, 0, 0}; // pc, lr, psr; pc 0 = none + bool _hardfault_seen = false; +};