diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 64d00b7be..2165deaac 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -503,6 +503,21 @@ send text.flood checking ridge link --- +#### Enable or disable the RX inactivity watchdog (Repeater Only) +**Usage:** +- `get rx.watchdog` +- `set rx.watchdog on` +- `set rx.watchdog off` + +**Default:** `off` + +**Notes:** +- When enabled, the first check is due after a full 12-hour observation window. The repeater then checks roughly every 12 hours and reboots if it has not successfully received a radio packet during the preceding 12 hours. +- Enabling the watchdog starts a new 12-hour observation window. Rebooting also starts a new window, so a quiet mesh can reboot no more often than once every 12 hours. +- The check reuses the radio driver's existing last-receive timestamp. It does not poll, sample, or wake the radio or CPU. A due check waits for the next normal loop/wake, so its actual cadence can drift around the 12-hour target. With RX power saving enabled, packets received during normal listening windows count as activity; the watchdog does not alter the RX/sleep timing. + +--- + #### View this node's public key **Usage:** `get public.key` diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index a566fdad6..bd53042f1 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -104,6 +104,7 @@ #define LOW_BATTERY_STARTUP_DELAY (30ULL * 60ULL * 1000ULL) #define LOW_BATTERY_CHECK_INTERVAL (30UL * 60UL * 1000UL) #define LOW_BATTERY_ALERT_INTERVAL (12UL * 60UL * 60UL * 1000UL) +#define RX_INACTIVITY_WATCHDOG_INTERVAL (12UL * 60UL * 60UL * 1000UL) static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) { uint8_t shift = failure_count < 5 ? failure_count : 5; @@ -2320,6 +2321,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc uptime_millis = 0; next_local_advert = next_flood_advert = 0; next_battery_alert_check = 0; + next_rx_watchdog_check = 0; next_recent_repeater_sweep = 0; last_battery_alert_sent = 0; pending_battery_alert_packet = NULL; @@ -2771,6 +2773,32 @@ void MyMesh::checkBatteryAlert() { } } +void MyMesh::checkRxInactivityWatchdog() { + if (!_prefs.rx_watchdog_enabled) { + next_rx_watchdog_check = 0; + return; + } + + if (next_rx_watchdog_check == 0) { + next_rx_watchdog_check = futureMillis(RX_INACTIVITY_WATCHDOG_INTERVAL); + if (next_rx_watchdog_check == 0) next_rx_watchdog_check = 1; + return; + } + if (!millisHasNowPassed(next_rx_watchdog_check)) { + return; + } + + next_rx_watchdog_check = futureMillis(RX_INACTIVITY_WATCHDOG_INTERVAL); + if (next_rx_watchdog_check == 0) next_rx_watchdog_check = 1; + + const unsigned long now = millis(); + const unsigned long last_rx = _radio->getLastRecvMillis(); + if (last_rx == 0 || (uint32_t)(now - last_rx) >= RX_INACTIVITY_WATCHDOG_INTERVAL) { + MESH_DEBUG_PRINTLN("RX watchdog: no packet received in 12 hours, rebooting"); + _cli.getBoard()->reboot(); + } +} + bool MyMesh::applyRadioParams(float freq, float bw, uint8_t sf, uint8_t cr) { uint32_t rx_us = _prefs.rx_ps_rx_us; uint32_t sleep_us = _prefs.rx_ps_sleep_us; @@ -4241,6 +4269,23 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char * sprintf(reply, "> %u", (uint32_t)_prefs.battery_alert_low_percent); } else if (strcmp(command, "get battery.alert.critical") == 0) { sprintf(reply, "> %u", (uint32_t)_prefs.battery_alert_critical_percent); + } else if (strcmp(command, "get rx.watchdog") == 0) { + sprintf(reply, "> %s", _prefs.rx_watchdog_enabled ? "on" : "off"); + } else if (strncmp(command, "set rx.watchdog ", 16) == 0) { + const char* value = command + 16; + if (strcmp(value, "on") == 0) { + _prefs.rx_watchdog_enabled = 1; + next_rx_watchdog_check = 0; + savePrefs(); + strcpy(reply, "OK - RX watchdog enabled; first check in 12 hours"); + } else if (strcmp(value, "off") == 0) { + _prefs.rx_watchdog_enabled = 0; + next_rx_watchdog_check = 0; + savePrefs(); + strcpy(reply, "OK - RX watchdog disabled"); + } else { + strcpy(reply, "Err - usage: set rx.watchdog "); + } } else if (strncmp(command, "set battery.alert ", 18) == 0) { const char* value = command + 18; if (strncmp(value, "on", 2) == 0 && (value[2] == 0 || value[2] == ' ')) { @@ -4332,6 +4377,7 @@ void MyMesh::loop() { // Check radio FIRST to ensure we don't miss incoming packets // MQTT processing runs in a separate FreeRTOS task on Core 0, so we don't call bridge.loop() here mesh::Mesh::loop(); + checkRxInactivityWatchdog(); checkBatteryAlert(); expireRecentRepeatersIfDue(); diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 1077fa50d..ec68579f5 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -136,6 +136,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { uint64_t uptime_millis; unsigned long next_local_advert, next_flood_advert; unsigned long next_battery_alert_check; + unsigned long next_rx_watchdog_check; unsigned long next_recent_repeater_sweep; uint64_t last_battery_alert_sent; mesh::Packet* pending_battery_alert_packet; @@ -270,6 +271,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { bool getBatteryAlertScopeForRegion(const RegionEntry& region, TransportKey& scope); bool resolveBatteryAlertScope(TransportKey& scope); void checkBatteryAlert(); + void checkRxInactivityWatchdog(); void expireRecentRepeatersIfDue(); void printRecentRepeatersSerial(); diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index a6c007045..eff39cfbc 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -941,6 +941,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { #endif _prefs->telemetry_access = TELEMETRY_ACCESS_ALL; _prefs->flood_retry_group_max_path = FLOOD_RETRY_GROUP_MAX_PATH_DEFAULT; + _prefs->rx_watchdog_enabled = 0; // A remainder larger than the smallest legacy MQTT gap (864) means an old fork // file with the zero-filled gap; detect and recover it below. Anything smaller // (upstream/flex 5-byte tails, or the ~384-byte keymind retry tail) takes the @@ -1192,6 +1193,10 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { file.read((uint8_t *)&_prefs->flood_retry_group_max_path, sizeof(_prefs->flood_retry_group_max_path)); } + if (file.available() >= (int)sizeof(_prefs->rx_watchdog_enabled)) { + file.read((uint8_t *)&_prefs->rx_watchdog_enabled, + sizeof(_prefs->rx_watchdog_enabled)); + } } // sanitise bad pref values @@ -1283,6 +1288,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { if (_prefs->ota_signer_count > 4) _prefs->ota_signer_count = 0; // corrupt count -> drop keys #endif _prefs->rx_powersaving_enabled = constrain(_prefs->rx_powersaving_enabled, 0, 1); + _prefs->rx_watchdog_enabled = constrain(_prefs->rx_watchdog_enabled, 0, 1); _prefs->rx_ps_level = constrain(_prefs->rx_ps_level, 0, 10); if (_prefs->rx_ps_preamble != 16 && _prefs->rx_ps_preamble != 32) { _prefs->rx_ps_preamble = 0; // 0 = auto (derive from SF) @@ -1419,7 +1425,9 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) { file.write((uint8_t *)_prefs->battery_alert_region, sizeof(_prefs->battery_alert_region)); // 822 file.write((uint8_t *)&_prefs->flood_retry_group_max_path, sizeof(_prefs->flood_retry_group_max_path)); // 853 - // next: 854 + file.write((uint8_t *)&_prefs->rx_watchdog_enabled, + sizeof(_prefs->rx_watchdog_enabled)); // 854 + // next: 855 file.close(); } diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 9866ffb53..8c6f92e17 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -217,6 +217,7 @@ struct NodePrefs { // persisted to file uint8_t rx_ps_preamble; // 0 = auto (derive from SF); else 16 or 32 = explicit override for level calc char battery_alert_region[31]; // named scope for low-battery floods; empty = no alert scope uint8_t flood_retry_group_max_path; // PAYLOAD_TYPE_GRP_DATA retry path gate; 0xFF = use only the general gate + uint8_t rx_watchdog_enabled; // repeater RX-inactivity reboot watchdog (boolean) }; #ifdef WITH_MQTT_BRIDGE diff --git a/src/helpers/esp32/ESPNOWRadio.h b/src/helpers/esp32/ESPNOWRadio.h index b6aed64a7..758f4c68f 100644 --- a/src/helpers/esp32/ESPNOWRadio.h +++ b/src/helpers/esp32/ESPNOWRadio.h @@ -11,8 +11,9 @@ public: uint32_t getRngSeed(); - void setParams(float freq, float bw, uint8_t sf, uint8_t cr) { - // no-op + bool setParams(float freq, float bw, uint8_t sf, uint8_t cr, + const uint32_t* rx_ps_timings = nullptr) { + return true; } void powerOff() { /* no-op */ } diff --git a/variants/rak3x72/platformio.ini b/variants/rak3x72/platformio.ini index f96678608..8795c79d1 100644 --- a/variants/rak3x72/platformio.ini +++ b/variants/rak3x72/platformio.ini @@ -17,6 +17,7 @@ build_src_filter = ${stm32_base.build_src_filter} [env:RAK_3x72_repeater] extends = rak3x72 build_flags = ${rak3x72.build_flags} + -flto -D ADVERT_NAME='"RAK3x72 Repeater"' -D ADMIN_PASSWORD='"password"' -D MAX_NEIGHBOURS=50 diff --git a/variants/wio-e5-dev/platformio.ini b/variants/wio-e5-dev/platformio.ini index 393df69a3..61878ddac 100644 --- a/variants/wio-e5-dev/platformio.ini +++ b/variants/wio-e5-dev/platformio.ini @@ -18,6 +18,7 @@ build_src_filter = ${stm32_base.build_src_filter} [env:wio-e5_repeater] extends = lora_e5 build_flags = ${lora_e5.build_flags} + -flto -D LORA_TX_POWER=22 -D ADVERT_NAME='"WIO-E5 Repeater"' -D ADMIN_PASSWORD='"password"'