simple_repeater: radio silence watchdog -- re-arm RX, then reboot a deaf receiver (#6)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-03 20:03:01 +01:00
1 parent 10f0349207
commit 278c1b8c8a
8 files changed
+440

No files matched your search

+113
View File
@@ -2,6 +2,7 @@
#include <Mesh.h>
#include "MyMesh.h"
#include <helpers/RadioSilenceWatchdog.h>
// ===========================================================================
// CUSTOM (TeTeHacko): `blink [n]` -- make THIS board flash its LED on demand,
@@ -750,6 +751,116 @@ bool txPwrHandleCommand(const char* command, char* reply) {
return true;
}
// CUSTOM (TeTeHacko, tth/meshcore-tth#6): radio silence watchdog -- see
// RadioSilenceWatchdog.h for why it is staged and how it avoids a reboot loop.
// `set radio.silence <min>` (0 = off), `get radio.silence`, `get radio.reboots`.
// 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.
static RadioSilenceWatchdog radio_silence;
static FILESYSTEM* radio_silence_fs = NULL;
static uint32_t radio_silence_reboots = 0;
#define RADIO_SILENCE_FILE "/rsilence"
static uint32_t radioActivity() {
return radio_driver.getPacketsRecv() + radio_driver.getPacketsRecvErrors();
}
static void radioSilenceLoad(uint8_t* backoff) {
*backoff = 0;
if (!radio_silence_fs || !radio_silence_fs->exists(RADIO_SILENCE_FILE)) return;
#if defined(RP2040_PLATFORM)
File f = radio_silence_fs->open(RADIO_SILENCE_FILE, "r");
#else
File f = radio_silence_fs->open(RADIO_SILENCE_FILE);
#endif
if (!f) return;
if (f.read((uint8_t*)&radio_silence_reboots, sizeof(radio_silence_reboots)) != sizeof(radio_silence_reboots)) {
radio_silence_reboots = 0;
}
if (f.read(backoff, 1) != 1) *backoff = 0;
f.close();
}
static void radioSilenceSave(uint8_t backoff) {
if (!radio_silence_fs) return;
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
radio_silence_fs->remove(RADIO_SILENCE_FILE);
File f = radio_silence_fs->open(RADIO_SILENCE_FILE, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
File f = radio_silence_fs->open(RADIO_SILENCE_FILE, "w");
#else
File f = radio_silence_fs->open(RADIO_SILENCE_FILE, "w", true);
#endif
if (!f) return;
f.write((const uint8_t*)&radio_silence_reboots, sizeof(radio_silence_reboots));
f.write(&backoff, 1);
f.close();
}
static void radioSilenceBegin(FILESYSTEM* fs) {
radio_silence_fs = fs;
uint8_t backoff;
radioSilenceLoad(&backoff);
radio_silence.begin(millis(), radioActivity(), backoff);
radio_silence.setMinutes(the_mesh.getNodePrefs()->radio_silence_min);
Serial.printf("boot: radio silence watchdog %u min, reboots %lu, backoff x%u\n",
(unsigned) radio_silence.minutes(), (unsigned long) radio_silence_reboots,
(unsigned) (1u << radio_silence.backoff()));
}
static void radioSilenceLoop() {
switch (radio_silence.poll(millis(), radioActivity())) {
case RadioSilenceWatchdog::KICK_RX:
Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n",
(unsigned long) (radio_silence.quietMs(millis()) / 60000));
radio_driver.kickRx();
break;
case RadioSilenceWatchdog::REBOOT:
Serial.printf("radio silence: still nothing after %lu min, rebooting\n",
(unsigned long) (radio_silence.quietMs(millis()) / 60000));
radio_silence_reboots++;
radioSilenceSave(radio_silence.nextBackoff());
delay(100); // let the line out
board.reboot();
break;
default:
break;
}
if (radio_silence.takeBackoffCleared()) radioSilenceSave(0);
}
bool radioSilenceHandleCommand(const char* command, char* reply) {
if (strcmp(command, "get radio.reboots") == 0) {
sprintf(reply, "> %lu", (unsigned long) radio_silence_reboots);
return true;
}
if (strcmp(command, "get radio.silence") == 0) {
if (radio_silence.minutes() == 0) {
strcpy(reply, "> off");
} else {
sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u",
(unsigned) radio_silence.minutes(), (unsigned) (1u << radio_silence.backoff()),
(unsigned long) (radio_silence.quietMs(millis()) / 60000), (unsigned) radio_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;
}
the_mesh.getNodePrefs()->radio_silence_min = (uint16_t) m;
the_mesh.savePrefs();
radio_silence.setMinutes((uint16_t) m);
strcpy(reply, "OK");
return true;
}
return false;
}
#ifdef RADIO_DIAG
// CUSTOM (TeTeHacko): `radiostat` / `radiokick` -- see RadioLibWrappers.cpp.
// On the masts since 2. 11. 2026: mast-three came up deaf after a BLE OTA (recv 0,
@@ -1018,6 +1129,7 @@ void setup() {
sensors.begin();
the_mesh.begin(fs);
radioSilenceBegin(fs); // CUSTOM (TeTeHacko): needs the prefs loaded by begin()
#ifdef DISPLAY_CLASS
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
@@ -1258,6 +1370,7 @@ void loop() {
#endif
the_mesh.loop();
radioSilenceLoop(); // CUSTOM (TeTeHacko): deaf-receiver watchdog
sensors.loop();
#ifdef DISPLAY_CLASS
ui_task.loop();