#pragma once #include // 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; };