Make repeater RX watchdog MeshCore-aware and tune temporary radio
Use RX-only radio recovery and feed the repeater reboot watchdog after MeshCore parsing. Default to 24 hours, apply temporary watchdog and advert intervals in RAM, and report temporary durations as days, hours, and minutes. Add regression coverage and an ESP-NOW bridge setup guide.
This commit is contained in:
1 parent
91d636b8ff
commit
2b4a35adb7
19 files changed
+1265
-140
No files matched your search
+52
-64
@@ -49,9 +49,8 @@ void Dispatcher::setRadioAvailable(bool available) {
|
||||
agc_reset_armed = false;
|
||||
_radio->begin();
|
||||
prev_isrecv_mode = _radio->isInRecvMode();
|
||||
last_observed_radio_irq = _radio->getLastRadioInterruptMillis();
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
last_radio_activity_ms = now;
|
||||
rx_watchdog_window_start = now;
|
||||
#else
|
||||
radio_liveness.begin(now);
|
||||
nonrx_soft_recovery_attempted = false;
|
||||
@@ -62,6 +61,7 @@ void Dispatcher::setRadioAvailable(bool available) {
|
||||
void Dispatcher::begin() {
|
||||
n_sent_flood = n_sent_direct = 0;
|
||||
n_recv_flood = n_recv_direct = 0;
|
||||
last_meshcore_recv_millis = 0;
|
||||
_err_flags = 0;
|
||||
outbound_radio_retry_at = 0;
|
||||
outbound_radio_retry_pending = false;
|
||||
@@ -80,14 +80,12 @@ void Dispatcher::begin() {
|
||||
if (radio_available) {
|
||||
_radio->begin();
|
||||
prev_isrecv_mode = _radio->isInRecvMode();
|
||||
last_observed_radio_irq = _radio->getLastRadioInterruptMillis();
|
||||
} else {
|
||||
prev_isrecv_mode = false;
|
||||
last_observed_radio_irq = 0;
|
||||
MESH_DEBUG_PRINTLN("Dispatcher: starting without a radio transport");
|
||||
}
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
last_radio_activity_ms = now;
|
||||
rx_watchdog_window_start = now;
|
||||
#else
|
||||
radio_liveness.begin(now);
|
||||
nonrx_soft_recovery_attempted = false;
|
||||
@@ -309,16 +307,6 @@ void Dispatcher::loop() {
|
||||
_radio->loop();
|
||||
|
||||
const unsigned long now = _ms->getMillis();
|
||||
const unsigned long latest_irq = _radio->getLastRadioInterruptMillis();
|
||||
if (latest_irq != 0 && latest_irq != last_observed_radio_irq) {
|
||||
last_observed_radio_irq = latest_irq;
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
last_radio_activity_ms = now;
|
||||
#else
|
||||
radio_liveness.noteActivity(now);
|
||||
#endif
|
||||
}
|
||||
|
||||
// check for radio 'stuck' in mode other than Rx
|
||||
bool is_recv = _radio->isInRecvMode();
|
||||
if (is_recv != prev_isrecv_mode) {
|
||||
@@ -339,7 +327,7 @@ void Dispatcher::loop() {
|
||||
MESH_DEBUG_PRINTLN("Radio watchdog: radio outside RX for %lu ms; soft recovery",
|
||||
now - radio_nonrx_start);
|
||||
if (_radio->recoverRadio(false)) {
|
||||
last_radio_activity_ms = now;
|
||||
rx_watchdog_window_start = now;
|
||||
recovered_this_loop = true;
|
||||
}
|
||||
#else
|
||||
@@ -352,47 +340,6 @@ void Dispatcher::loop() {
|
||||
radio_nonrx_start = now; // bounded retry cadence if recovery is unsupported
|
||||
}
|
||||
|
||||
// Continuous-RX can fail while the MCU and main loop remain healthy, so a
|
||||
// CPU watchdog cannot detect it. Use packet/IRQ/TX activity as proof of
|
||||
// radio life and stage recovery from a harmless re-arm to a radio-only
|
||||
// hardware reset when the driver supports one. Observer configuration may
|
||||
// request a faster soft check.
|
||||
uint32_t soft_liveness_ms = RADIO_LIVENESS_SOFT_MS;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
const uint32_t configured_watchdog_ms = getRadioWatchdogMillis();
|
||||
if (configured_watchdog_ms > 0) soft_liveness_ms = configured_watchdog_ms;
|
||||
#endif
|
||||
if (is_recv && outbound == NULL && !recovered_this_loop) {
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
if (soft_liveness_ms > 0 && now - last_radio_activity_ms >= soft_liveness_ms) {
|
||||
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
|
||||
MESH_DEBUG_PRINTLN("Radio watchdog: no hardware activity for %lu ms, state=%d, soft recovery",
|
||||
now - last_radio_activity_ms, _radio->getRadioState());
|
||||
_radio->recoverRadio(false);
|
||||
// This radio has no independently resettable RF peripheral. Retry its
|
||||
// only safe recovery at the soft interval until IRQ/TX/RX proves life.
|
||||
last_radio_activity_ms = now;
|
||||
}
|
||||
#else
|
||||
uint32_t hard_liveness_ms = RADIO_LIVENESS_HARD_MS;
|
||||
if (soft_liveness_ms > hard_liveness_ms / 2) {
|
||||
hard_liveness_ms = soft_liveness_ms <= 0x7FFFFFFFUL
|
||||
? soft_liveness_ms * 2 : 0xFFFFFFFFUL;
|
||||
}
|
||||
const RadioRecoveryAction action = radio_liveness.poll(
|
||||
now, soft_liveness_ms, hard_liveness_ms);
|
||||
if (action != RadioRecoveryAction::NONE) {
|
||||
const bool hard = action == RadioRecoveryAction::HARD;
|
||||
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
|
||||
MESH_DEBUG_PRINTLN("Radio watchdog: no hardware activity for %lu ms, state=%d, %s recovery",
|
||||
now - radio_liveness.lastActivity(), _radio->getRadioState(),
|
||||
hard ? "hard" : "soft");
|
||||
const bool recovered = _radio->recoverRadio(hard);
|
||||
if (hard) radio_liveness.noteHardRecoveryResult(now, recovered);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (outbound) { // waiting for outbound send to complete, or for its one radio retry
|
||||
if (outbound_radio_retry_pending) {
|
||||
if (!millisHasNowPassed(outbound_radio_retry_at)) return;
|
||||
@@ -434,11 +381,6 @@ void Dispatcher::loop() {
|
||||
// otherwise a later CAD pause can trip the eight-second watchdog using
|
||||
// the start time of the whole healthy burst.
|
||||
radio_nonrx_start = _ms->getMillis();
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
last_radio_activity_ms = radio_nonrx_start; // TX success -> radio is alive
|
||||
#else
|
||||
radio_liveness.noteActivity(radio_nonrx_start); // TX success -> radio is alive
|
||||
#endif
|
||||
restoreOutboundTxOverrides();
|
||||
logTx(outbound, 2 + outbound->getPathByteLen() + outbound->payload_len);
|
||||
onSendComplete(outbound);
|
||||
@@ -458,6 +400,7 @@ void Dispatcher::loop() {
|
||||
restoreOutboundTxOverrides();
|
||||
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
|
||||
const bool recovered = _radio->recoverRadio(true);
|
||||
recovered_this_loop = true;
|
||||
if (!recovered) {
|
||||
MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): WARNING: hard radio recovery after TX timeout failed!",
|
||||
getLogDateTime());
|
||||
@@ -516,6 +459,48 @@ void Dispatcher::loop() {
|
||||
}
|
||||
}
|
||||
checkRecv();
|
||||
|
||||
// Count only successful recvRaw() delivery as RX activity. Check after
|
||||
// draining RX and completing TX, before starting the next queued transmit:
|
||||
// a packet arriving at the deadline prevents unnecessary recovery, and a
|
||||
// continuous TX queue cannot hide a deaf receiver. The driver defers any
|
||||
// recovery that would interrupt a pending packet or an active reception.
|
||||
if (_radio->isInRecvMode() && outbound == NULL && !recovered_this_loop) {
|
||||
const unsigned long now = _ms->getMillis();
|
||||
uint32_t soft_liveness_ms = RADIO_LIVENESS_SOFT_MS;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
const uint32_t configured_watchdog_ms = getRadioWatchdogMillis();
|
||||
if (configured_watchdog_ms > 0) soft_liveness_ms = configured_watchdog_ms;
|
||||
#endif
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
if (soft_liveness_ms > 0 && (uint32_t)(now - rx_watchdog_window_start) >= soft_liveness_ms) {
|
||||
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
|
||||
MESH_DEBUG_PRINTLN("Radio watchdog: no packet received in observation window for %lu ms, state=%d, soft recovery",
|
||||
(unsigned long)(uint32_t)(now - rx_watchdog_window_start), _radio->getRadioState());
|
||||
_radio->recoverRadio(false);
|
||||
// This radio has no independently resettable RF peripheral. Allow a
|
||||
// full observation interval between attempts on a quiet mesh.
|
||||
rx_watchdog_window_start = now;
|
||||
}
|
||||
#else
|
||||
uint32_t hard_liveness_ms = RADIO_LIVENESS_HARD_MS;
|
||||
if (soft_liveness_ms > hard_liveness_ms / 2) {
|
||||
hard_liveness_ms = soft_liveness_ms <= 0x7FFFFFFFUL
|
||||
? soft_liveness_ms * 2 : 0xFFFFFFFFUL;
|
||||
}
|
||||
const RadioRecoveryAction action = radio_liveness.poll(
|
||||
now, soft_liveness_ms, hard_liveness_ms);
|
||||
if (action != RadioRecoveryAction::NONE) {
|
||||
const bool hard = action == RadioRecoveryAction::HARD;
|
||||
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
|
||||
MESH_DEBUG_PRINTLN("Radio watchdog: no packet received in observation window for %lu ms, state=%d, %s recovery",
|
||||
(unsigned long)(uint32_t)(now - radio_liveness.windowStart()), _radio->getRadioState(),
|
||||
hard ? "hard" : "soft");
|
||||
const bool recovered = _radio->recoverRadio(hard);
|
||||
if (hard) radio_liveness.noteHardRecoveryResult(now, recovered);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
checkSend();
|
||||
releaseDroppedOutbound();
|
||||
}
|
||||
@@ -595,10 +580,12 @@ void Dispatcher::checkRecv() {
|
||||
uint8_t raw[MAX_TRANS_UNIT+1];
|
||||
int len = _radio->recvRaw(raw, MAX_TRANS_UNIT);
|
||||
if (len > 0) {
|
||||
// A successful driver read proves RX works even if application parsing
|
||||
// rejects the packet or the packet pool has no space. IRQs alone do not.
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
last_radio_activity_ms = _ms->getMillis();
|
||||
rx_watchdog_window_start = _ms->getMillis();
|
||||
#else
|
||||
radio_liveness.noteActivity(_ms->getMillis());
|
||||
radio_liveness.noteReceive(_ms->getMillis());
|
||||
#endif
|
||||
snr = _radio->getLastSNR();
|
||||
rssi = _radio->getLastRSSI();
|
||||
@@ -609,6 +596,7 @@ void Dispatcher::checkRecv() {
|
||||
MESH_DEBUG_PRINTLN("%s Dispatcher::checkRecv(): WARNING: received data, no unused packets available!", getLogDateTime());
|
||||
} else {
|
||||
if (tryParsePacket(pkt, raw, len)) {
|
||||
last_meshcore_recv_millis = _ms->getMillis();
|
||||
pkt->_snr = snr * 4.0f;
|
||||
pkt->_rssi = (int16_t)rssi;
|
||||
score = _radio->packetScore(snr, len);
|
||||
|
||||
+7
-4
@@ -239,9 +239,8 @@ class Dispatcher {
|
||||
Packet* outbound; // current outbound packet
|
||||
unsigned long outbound_expiry, outbound_start, total_air_time, rx_air_time;
|
||||
unsigned long outbound_radio_retry_at;
|
||||
unsigned long last_observed_radio_irq;
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
unsigned long last_radio_activity_ms;
|
||||
unsigned long rx_watchdog_window_start;
|
||||
#else
|
||||
RadioLivenessTracker radio_liveness;
|
||||
#endif
|
||||
@@ -262,6 +261,7 @@ class Dispatcher {
|
||||
#endif
|
||||
uint32_t n_sent_flood, n_sent_direct;
|
||||
uint32_t n_recv_flood, n_recv_direct;
|
||||
uint32_t last_meshcore_recv_millis;
|
||||
unsigned long tx_budget_ms;
|
||||
unsigned long last_budget_update;
|
||||
unsigned long duty_cycle_window_ms;
|
||||
@@ -289,6 +289,7 @@ protected:
|
||||
outbound_radio_retry_used = false;
|
||||
outbound_restore_cr = 0;
|
||||
total_air_time = rx_air_time = 0;
|
||||
last_meshcore_recv_millis = 0;
|
||||
next_tx_time = ms.getMillis();
|
||||
cad_busy_start = 0;
|
||||
next_floor_calib_time = next_agc_reset_time = 0;
|
||||
@@ -302,9 +303,8 @@ protected:
|
||||
tx_budget_ms = 0;
|
||||
last_budget_update = 0;
|
||||
duty_cycle_window_ms = 3600000;
|
||||
last_observed_radio_irq = 0;
|
||||
#ifdef RADIO_LIVENESS_SOFT_ONLY
|
||||
last_radio_activity_ms = 0;
|
||||
rx_watchdog_window_start = 0;
|
||||
#else
|
||||
nonrx_soft_recovery_attempted = false;
|
||||
#endif
|
||||
@@ -377,6 +377,9 @@ public:
|
||||
uint32_t getNumSentDirect() const { return n_sent_direct; }
|
||||
uint32_t getNumRecvFlood() const { return n_recv_flood; }
|
||||
uint32_t getNumRecvDirect() const { return n_recv_direct; }
|
||||
// Successful MeshCore packet parsing, before receive delays and routing filters.
|
||||
// Raw radio reads, local sends, and statistics resets do not update this clock.
|
||||
uint32_t getLastMeshCoreRecvMillis() const { return last_meshcore_recv_millis; }
|
||||
uint16_t getErrFlags() const { return _err_flags; } // Get error flags
|
||||
bool hasOutbound() const { return outbound != NULL; }
|
||||
bool isCurrentOutbound(const Packet* packet) const { return outbound == packet; }
|
||||
|
||||
@@ -975,7 +975,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;
|
||||
_prefs->rx_watchdog_enabled = mesh::RepeaterRadioTiming::DEFAULT_RX_WATCHDOG_ENABLED;
|
||||
_prefs->system_watchdog_enabled = 1;
|
||||
// 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
|
||||
@@ -2608,15 +2608,23 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
float bw = 0.0f;
|
||||
uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
|
||||
uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
|
||||
int temp_timeout_mins = num > 4 ? atoi(parts[4]) : 0;
|
||||
uint32_t temp_timeout_mins = 0;
|
||||
if (num == 5
|
||||
&& parseUint32Strict(parts[4], temp_timeout_mins)
|
||||
&& temp_timeout_mins > 0
|
||||
&& (uint64_t)getRTCClock()->getCurrentTime() + 2
|
||||
+ (uint64_t)temp_timeout_mins * 60 <= UINT32_MAX
|
||||
&& mesh::cli::parseDecimalStrict(parts[0], freq)
|
||||
&& mesh::cli::parseDecimalStrict(parts[1], bw)
|
||||
&& freq >= 150.0f && freq <= 2500.0f
|
||||
&& sf >= 5 && sf <= 12 && cr >= 5 && cr <= 8
|
||||
&& isValidLoRaBandwidth(bw) && temp_timeout_mins > 0) {
|
||||
&& isValidLoRaBandwidth(bw)) {
|
||||
_callbacks->applyTempRadioParams(freq, bw, sf, cr, temp_timeout_mins);
|
||||
sprintf(reply, "OK - temp params for %d mins", temp_timeout_mins);
|
||||
char duration[64];
|
||||
const uint32_t seconds = (uint32_t)temp_timeout_mins * 60UL;
|
||||
mesh::RepeaterRadioTiming::formatDuration(duration, sizeof(duration), seconds);
|
||||
snprintf(reply, 160, "OK - temp params for %s", duration);
|
||||
_callbacks->appendTempRadioTimingNote(reply, 160, seconds);
|
||||
appendRxPowerSavingAdjustmentNote(reply, _prefs, sf, bw);
|
||||
} else {
|
||||
strcpy(reply, "Error, invalid params");
|
||||
@@ -4463,9 +4471,13 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
} else if (configKeyEquals(config, "telemetry.access")) {
|
||||
sprintf(reply, "> %s", _prefs->telemetry_access == TELEMETRY_ACCESS_ACL ? "acl" : "all");
|
||||
} else if (configKeyEquals(config, "flood.advert.interval")) {
|
||||
sprintf(reply, "> %d", ((uint32_t) _prefs->flood_advert_interval));
|
||||
mesh::RepeaterRadioTiming timing;
|
||||
timing.setTempDuration(_callbacks->getTempRadioDurationSeconds());
|
||||
sprintf(reply, "> %lu", (unsigned long)timing.floodAdvertHours(_prefs->flood_advert_interval));
|
||||
} else if (configKeyEquals(config, "advert.interval")) {
|
||||
sprintf(reply, "> %d", ((uint32_t) _prefs->advert_interval) * 2);
|
||||
mesh::RepeaterRadioTiming timing;
|
||||
timing.setTempDuration(_callbacks->getTempRadioDurationSeconds());
|
||||
sprintf(reply, "> %lu", (unsigned long)timing.localAdvertMinutes((uint32_t)_prefs->advert_interval * 2));
|
||||
} else if (configKeyEquals(config, "guest.password")) {
|
||||
sprintf(reply, "> %s", _prefs->guest_password);
|
||||
} else if (sender_timestamp == 0 && configKeyEquals(config, "prv.key")) { // local connections only
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <helpers/bridges/ESPNowBridgeFormat.h>
|
||||
#include <helpers/CommonRadioPrefs.h>
|
||||
#include <helpers/DynamicConfigSerializer.h>
|
||||
#include <helpers/RepeaterRadioTiming.h>
|
||||
|
||||
#ifndef DEFAULT_CAD_ENABLED
|
||||
#define DEFAULT_CAD_ENABLED 0
|
||||
@@ -238,7 +239,7 @@ public:
|
||||
uint8_t rx_ps_preamble = 0; // 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 = 0; // PAYLOAD_TYPE_GRP_DATA retry path gate; 0xFF = use only the general gate
|
||||
uint8_t rx_watchdog_enabled = 0; // repeater RX-inactivity reboot watchdog (boolean)
|
||||
uint8_t rx_watchdog_enabled = mesh::RepeaterRadioTiming::DEFAULT_RX_WATCHDOG_ENABLED;
|
||||
uint8_t system_watchdog_enabled = 0; // nRF52 main-loop hardware watchdog (boolean; default on)
|
||||
|
||||
private:
|
||||
@@ -511,6 +512,8 @@ public:
|
||||
virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0;
|
||||
virtual void clearStats() = 0;
|
||||
virtual void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) = 0;
|
||||
virtual uint32_t getTempRadioDurationSeconds() const { return 0; }
|
||||
virtual void appendTempRadioTimingNote(char* reply, size_t size, uint32_t seconds) const { }
|
||||
// Cancel pending/active temporary-radio windows and restore the saved tuple
|
||||
// after the command reply has drained on the current channel.
|
||||
virtual bool scheduleNormalRadio() { return false; }
|
||||
|
||||
@@ -10,41 +10,40 @@ enum class RadioRecoveryAction : uint8_t {
|
||||
HARD,
|
||||
};
|
||||
|
||||
// Tracks proof that the radio hardware is alive independently of the MCU main
|
||||
// loop. A successful packet, IRQ, or TX resets the schedule. Silence first
|
||||
// requests a non-destructive RX/AGC re-arm, then a radio-only hardware reset.
|
||||
// Tracks successful reception independently of TX and interrupt activity, which
|
||||
// can continue while the receiver is deaf. RX silence first requests an RX/AGC
|
||||
// re-arm, then a radio-only hardware reset. A completed hard recovery starts a
|
||||
// fresh observation window to bound repeated recovery on a quiet mesh.
|
||||
class RadioLivenessTracker {
|
||||
static const uint32_t HARD_RETRY_MS = 30000UL;
|
||||
|
||||
uint32_t _last_activity;
|
||||
uint32_t _window_start;
|
||||
uint32_t _last_hard_attempt;
|
||||
uint8_t _stage;
|
||||
|
||||
public:
|
||||
RadioLivenessTracker() : _last_activity(0), _last_hard_attempt(0), _stage(0) {}
|
||||
RadioLivenessTracker() : _window_start(0), _last_hard_attempt(0), _stage(0) {}
|
||||
|
||||
void begin(uint32_t now) {
|
||||
_last_activity = now;
|
||||
_window_start = now;
|
||||
_last_hard_attempt = 0;
|
||||
_stage = 0;
|
||||
}
|
||||
|
||||
void noteActivity(uint32_t now) {
|
||||
_last_activity = now;
|
||||
_last_hard_attempt = 0;
|
||||
_stage = 0;
|
||||
void noteReceive(uint32_t now) {
|
||||
begin(now);
|
||||
}
|
||||
|
||||
// A completed hard recovery is a safe point from which to restart the
|
||||
// liveness window. Failed recoveries remain escalated and retry at a bounded
|
||||
// cadence, instead of being silently deferred for another full interval.
|
||||
void noteHardRecoveryResult(uint32_t now, bool success) {
|
||||
if (success) noteActivity(now);
|
||||
if (success) begin(now);
|
||||
}
|
||||
|
||||
RadioRecoveryAction poll(uint32_t now, uint32_t soft_after_ms,
|
||||
uint32_t hard_after_ms) {
|
||||
const uint32_t silent_for = now - _last_activity;
|
||||
const uint32_t silent_for = now - _window_start;
|
||||
if (hard_after_ms > 0 && silent_for >= hard_after_ms) {
|
||||
if (_stage < 2 || now - _last_hard_attempt >= HARD_RETRY_MS) {
|
||||
_stage = 2;
|
||||
@@ -60,7 +59,7 @@ public:
|
||||
return RadioRecoveryAction::NONE;
|
||||
}
|
||||
|
||||
uint32_t lastActivity() const { return _last_activity; }
|
||||
uint32_t windowStart() const { return _window_start; }
|
||||
uint8_t stage() const { return _stage; }
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
// Runtime overrides only. A zero duration selects the saved normal settings.
|
||||
class RepeaterRadioTiming {
|
||||
uint32_t _temp_duration_secs = 0;
|
||||
|
||||
public:
|
||||
static constexpr bool DEFAULT_RX_WATCHDOG_ENABLED = true;
|
||||
static constexpr uint32_t HOUR_SECS = 3600UL;
|
||||
static constexpr uint32_t HOUR_MS = HOUR_SECS * 1000UL;
|
||||
|
||||
void setTempDuration(uint32_t seconds) { _temp_duration_secs = seconds; }
|
||||
uint32_t tempDuration() const { return _temp_duration_secs; }
|
||||
bool isTemporary() const { return _temp_duration_secs != 0; }
|
||||
|
||||
uint32_t watchdogMillis(bool normal_enabled) const {
|
||||
if (isTemporary()) {
|
||||
return _temp_duration_secs >= 12UL * HOUR_SECS ? 12UL * HOUR_MS : 0;
|
||||
}
|
||||
return normal_enabled ? 24UL * HOUR_MS : 0;
|
||||
}
|
||||
|
||||
const char* watchdogLabel(bool normal_enabled) const {
|
||||
if (isTemporary()) {
|
||||
return watchdogMillis(normal_enabled) ? "12hours" : "off (temp<12hours)";
|
||||
}
|
||||
return normal_enabled ? "24hours" : "off";
|
||||
}
|
||||
|
||||
uint32_t localAdvertMinutes(uint32_t normal_minutes) const {
|
||||
return isTemporary() ? 60 : normal_minutes;
|
||||
}
|
||||
|
||||
uint32_t floodAdvertHours(uint32_t normal_hours) const {
|
||||
return _temp_duration_secs > 3UL * HOUR_SECS ? 3 : normal_hours;
|
||||
}
|
||||
|
||||
static void formatDuration(char* dest, size_t size, uint32_t seconds) {
|
||||
const uint32_t days = seconds / (24UL * HOUR_SECS);
|
||||
const uint32_t hours = (seconds / HOUR_SECS) % 24;
|
||||
const uint32_t minutes = (seconds / 60) % 60;
|
||||
if (seconds % 60) {
|
||||
snprintf(dest, size, "%lu mins %lu sec (%lud%luh%lum)",
|
||||
(unsigned long)(seconds / 60), (unsigned long)(seconds % 60),
|
||||
(unsigned long)days, (unsigned long)hours, (unsigned long)minutes);
|
||||
} else {
|
||||
snprintf(dest, size, "%lu mins (%lud%luh%lum)",
|
||||
(unsigned long)(seconds / 60),
|
||||
(unsigned long)days, (unsigned long)hours, (unsigned long)minutes);
|
||||
}
|
||||
}
|
||||
|
||||
static void appendTempWatchdogNote(char* reply, size_t size, uint32_t seconds) {
|
||||
const size_t used = strlen(reply);
|
||||
if (used >= size) return;
|
||||
RepeaterRadioTiming timing;
|
||||
timing.setTempDuration(seconds);
|
||||
snprintf(reply + used, size - used, "; rx.watchdog=%s", timing.watchdogLabel(true));
|
||||
}
|
||||
};
|
||||
|
||||
// A sliding no-RX deadline, rebased on activation and each successful receive.
|
||||
// No wall clock, periodic flash writes, or extra radio/CPU wakeups are needed.
|
||||
class RxInactivityWatchdog {
|
||||
uint32_t _interval = 0;
|
||||
uint32_t _window_start = 0;
|
||||
uint32_t _last_rx = 0;
|
||||
|
||||
public:
|
||||
void reset() { _interval = 0; }
|
||||
|
||||
bool expired(uint32_t now, uint32_t last_rx, uint32_t interval) {
|
||||
if (_interval != interval) {
|
||||
_interval = interval;
|
||||
_window_start = now;
|
||||
_last_rx = last_rx;
|
||||
return false;
|
||||
}
|
||||
if (interval == 0) return false;
|
||||
if (last_rx != _last_rx) {
|
||||
_last_rx = last_rx;
|
||||
_window_start = last_rx;
|
||||
}
|
||||
return (uint32_t)(now - _window_start) >= interval;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mesh
|
||||
Reference in new issue
Block a user