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:
1 parent
10f0349207
commit
278c1b8c8a
8 files changed
+440
No files matched your search
@@ -94,6 +94,9 @@ public:
|
||||
uint16_t fair_group_refill_s = 0, fair_sender_refill_s = 0, fair_advert_refill_s = 0;
|
||||
uint16_t fair_advert_hours = 0; // per-origin advert window, hours; 0 = off
|
||||
uint8_t fair_dry_run = 0; // count FairnessLimiter verdicts, forward anyway
|
||||
// Radio silence watchdog (RadioSilenceWatchdog.h, simple_repeater): minutes
|
||||
// without a received frame before the receiver is re-armed, then rebooted; 0 = off.
|
||||
uint16_t radio_silence_min = 0;
|
||||
|
||||
private:
|
||||
class RadioPrefs : public CommonRadioPrefs {
|
||||
@@ -117,6 +120,7 @@ private:
|
||||
def("agc_int", _parent->agc_reset_interval);
|
||||
def("hash_mode", _parent->path_hash_mode);
|
||||
def("multi_ack", _parent->multi_acks);
|
||||
def("rsilence", _parent->radio_silence_min);
|
||||
}
|
||||
public:
|
||||
RadioPrefs(NodePrefs* parent) : _parent(parent) { }
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// CUSTOM (TeTeHacko, tth/meshcore-tth#6): a receiver that went deaf while the
|
||||
// main loop keeps running. The hardware watchdog only catches a frozen loop, and
|
||||
// two of our nodes have been found deaf with the loop alive -- bench-sensor on
|
||||
// 31. 10. 2026 (recv frozen for minutes, a reboot revived it) and mast-three on
|
||||
// 2. 11. after a BLE OTA (recv 0, tx_timeout 4 for 17 minutes, a reboot fixed it).
|
||||
//
|
||||
// Silence is judged by whether the receive counter MOVES, never by comparing
|
||||
// timestamps: taking the largest of several millis() stamps aliased across the
|
||||
// 32-bit wrap and reset healthy radios for weeks in another fork (Dutch-MeshCore
|
||||
// d8ffae32). Received packets and receive errors both count -- a CRC error still
|
||||
// proves the front end hears something.
|
||||
//
|
||||
// Staged: after `minutes` without a received frame the receiver is re-armed
|
||||
// (clear IRQ, startReceive -- covers an interrupt that never reached the MCU);
|
||||
// if that does not bring anything in within a step, the node reboots. There is
|
||||
// no in-between full chip re-init: it would have to restore every parameter
|
||||
// begin() applies, and a radio silently left on its default frequency is worse
|
||||
// than the fault (chendo 9a9a716a hit exactly that). AGC resets already run on
|
||||
// their own interval where configured.
|
||||
//
|
||||
// A quiet site must not turn this into a reboot loop: every reboot by this
|
||||
// watchdog doubles the threshold for the next boot (up to x16), and the first
|
||||
// frame received after boot clears it. The caller persists `backoff()` across
|
||||
// the reboot and reports `takeBackoffCleared()` so it can rewrite it.
|
||||
//
|
||||
// Pure logic, no Arduino: driven from the repeater loop, tested on the host.
|
||||
class RadioSilenceWatchdog {
|
||||
public:
|
||||
enum Action : uint8_t { NONE = 0, KICK_RX, REBOOT };
|
||||
|
||||
static constexpr uint32_t CHECK_MS = 30000; // evaluate at most this often
|
||||
static constexpr uint32_t MIN_STEP_MS = 5 * 60000; // kick -> reboot, at least
|
||||
static constexpr uint16_t MAX_MINUTES = 1440; // 24 h
|
||||
static constexpr uint8_t MAX_BACKOFF = 4; // threshold x16 at most
|
||||
|
||||
void begin(uint32_t now, uint32_t activity, uint8_t backoff) {
|
||||
_last_activity = activity;
|
||||
_quiet_since = _last_check = now;
|
||||
_backoff = backoff > MAX_BACKOFF ? MAX_BACKOFF : backoff;
|
||||
_stage = 0;
|
||||
_backoff_cleared = false;
|
||||
}
|
||||
|
||||
void setMinutes(uint16_t minutes) {
|
||||
_minutes = minutes > MAX_MINUTES ? MAX_MINUTES : minutes;
|
||||
_stage = 0;
|
||||
}
|
||||
uint16_t minutes() const { return _minutes; }
|
||||
|
||||
// `activity`: any counter that moves when the radio receives something.
|
||||
Action poll(uint32_t now, uint32_t activity) {
|
||||
if ((uint32_t)(now - _last_check) < CHECK_MS) return NONE;
|
||||
_last_check = now;
|
||||
|
||||
if (activity != _last_activity) {
|
||||
_last_activity = activity;
|
||||
_quiet_since = now;
|
||||
_stage = 0;
|
||||
if (_backoff) {
|
||||
_backoff = 0;
|
||||
_backoff_cleared = true;
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
if (_minutes == 0) {
|
||||
_stage = 0;
|
||||
return NONE;
|
||||
}
|
||||
|
||||
uint32_t quiet = (uint32_t)(now - _quiet_since);
|
||||
uint32_t threshold = thresholdMs();
|
||||
if (_stage == 0 && quiet >= threshold) {
|
||||
_stage = 1;
|
||||
return KICK_RX;
|
||||
}
|
||||
if (_stage == 1 && quiet >= threshold + stepMs()) {
|
||||
_stage = 2; // stays here: the caller reboots
|
||||
return REBOOT;
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
|
||||
uint32_t thresholdMs() const { return ((uint32_t)_minutes * 60000UL) << _backoff; }
|
||||
uint32_t stepMs() const {
|
||||
uint32_t step = thresholdMs() / 4;
|
||||
return step < MIN_STEP_MS ? MIN_STEP_MS : step;
|
||||
}
|
||||
uint32_t quietMs(uint32_t now) const { return (uint32_t)(now - _quiet_since); }
|
||||
uint8_t stage() const { return _stage; }
|
||||
uint8_t backoff() const { return _backoff; }
|
||||
// Backoff for the boot after a reboot by this watchdog.
|
||||
uint8_t nextBackoff() const { return _backoff < MAX_BACKOFF ? _backoff + 1 : MAX_BACKOFF; }
|
||||
|
||||
// True once after the radio proved itself and the persisted backoff went stale.
|
||||
bool takeBackoffCleared() {
|
||||
bool c = _backoff_cleared;
|
||||
_backoff_cleared = false;
|
||||
return c;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t _last_activity = 0;
|
||||
uint32_t _quiet_since = 0;
|
||||
uint32_t _last_check = 0;
|
||||
uint16_t _minutes = 0;
|
||||
uint8_t _backoff = 0;
|
||||
uint8_t _stage = 0;
|
||||
bool _backoff_cleared = false;
|
||||
};
|
||||
@@ -89,6 +89,12 @@ void RadioLibWrapper::doResetAGC() {
|
||||
_radio->sleep(); // warm sleep to reset analog frontend
|
||||
}
|
||||
|
||||
void RadioLibWrapper::kickRx() {
|
||||
if (state == STATE_TX_WAIT) return; // mid-transmit: next check will try again
|
||||
_radio->clearIrqFlags(0xFFFFFFFF); // a latched IRQ keeps DIO1 high: no new edge
|
||||
idle(); // standby; Dispatcher re-arms RX on its next loop
|
||||
}
|
||||
|
||||
void RadioLibWrapper::resetAGC() {
|
||||
// make sure we're not mid-receive of packet!
|
||||
if ((state & STATE_INT_READY) != 0 || isReceivingPacket()) return;
|
||||
|
||||
@@ -84,6 +84,9 @@ public:
|
||||
void loop() override;
|
||||
|
||||
uint32_t getPacketsRecv() const { return n_recv; }
|
||||
// CUSTOM (TeTeHacko): first recovery step of the radio silence watchdog --
|
||||
// drop latched IRQs and re-arm RX. Leaves a transmit in progress alone.
|
||||
void kickRx();
|
||||
uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
|
||||
uint32_t getPacketsSent() const { return n_sent; }
|
||||
void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
|
||||
|
||||
Reference in new issue
Block a user