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

@@ -0,0 +1,175 @@
#include <gtest/gtest.h>
#include "helpers/RadioSilenceWatchdog.h"
using A = RadioSilenceWatchdog;
static const uint32_t MIN = 60000;
// Polls every CHECK_MS from `from` to `to` with a constant activity counter and
// returns the first non-NONE action (and where it happened).
static A::Action runQuiet(A& wd, uint32_t from, uint32_t to, uint32_t activity, uint32_t* at = nullptr) {
for (uint32_t t = from; (uint32_t)(t - from) <= (uint32_t)(to - from); t += A::CHECK_MS) {
A::Action a = wd.poll(t, activity);
if (a != A::NONE) {
if (at) *at = t;
return a;
}
}
return A::NONE;
}
TEST(RadioSilenceWatchdog, OffNeverActs) {
A wd;
wd.begin(0, 0, 0);
wd.setMinutes(0);
EXPECT_EQ(runQuiet(wd, 0, 48 * 60 * MIN, 0), A::NONE);
}
TEST(RadioSilenceWatchdog, KicksAtThresholdThenReboots) {
A wd;
wd.begin(0, 0, 0);
wd.setMinutes(60);
uint32_t at = 0;
EXPECT_EQ(runQuiet(wd, 0, 61 * MIN, 0, &at), A::KICK_RX);
EXPECT_GE(at, 60 * MIN);
EXPECT_LT(at, 60 * MIN + A::CHECK_MS + 1);
// step = max(60/4 min, 5 min) = 15 min after the threshold
EXPECT_EQ(runQuiet(wd, at + A::CHECK_MS, at + 20 * MIN, 0, &at), A::REBOOT);
EXPECT_GE(at, 75 * MIN);
EXPECT_LT(at, 75 * MIN + A::CHECK_MS + 1);
}
TEST(RadioSilenceWatchdog, ShortThresholdUsesMinimumStep) {
A wd;
wd.begin(0, 0, 0);
wd.setMinutes(4);
uint32_t at = 0;
EXPECT_EQ(runQuiet(wd, 0, 5 * MIN, 0, &at), A::KICK_RX);
EXPECT_EQ(runQuiet(wd, at + A::CHECK_MS, at + 10 * MIN, 0, &at), A::REBOOT);
EXPECT_GE(at, 9 * MIN); // 4 min + MIN_STEP_MS (5 min)
}
TEST(RadioSilenceWatchdog, TrafficKeepsItHealthy) {
A wd;
wd.begin(0, 0, 0);
wd.setMinutes(30);
uint32_t rx = 0;
// one frame every 20 minutes, for two days
for (uint32_t t = A::CHECK_MS; t < 48 * 60 * MIN; t += A::CHECK_MS) {
if (t % (20 * MIN) == 0) rx++;
EXPECT_EQ(wd.poll(t, rx), A::NONE) << "t=" << t;
}
}
TEST(RadioSilenceWatchdog, KickThatWorksCancelsTheReboot) {
A wd;
wd.begin(0, 7, 0);
wd.setMinutes(10);
uint32_t at = 0;
ASSERT_EQ(runQuiet(wd, 0, 11 * MIN, 7, &at), A::KICK_RX);
// the re-armed receiver hears something
EXPECT_EQ(wd.poll(at + A::CHECK_MS, 8), A::NONE);
EXPECT_EQ(wd.stage(), 0);
// and the next silence starts over from the threshold, not from the step
uint32_t again = 0;
EXPECT_EQ(runQuiet(wd, at + 2 * A::CHECK_MS, at + 30 * MIN, 8, &again), A::KICK_RX);
EXPECT_GE(again - (at + A::CHECK_MS), 10 * MIN);
}
TEST(RadioSilenceWatchdog, ReceiveErrorsCountAsActivity) {
// the caller feeds recv + recv_errors; only the counter moving matters
A wd;
wd.begin(0, 100, 0);
wd.setMinutes(10);
uint32_t c = 100;
for (uint32_t t = A::CHECK_MS; t < 5 * 60 * MIN; t += A::CHECK_MS) {
if (t % (5 * MIN) == 0) c++;
EXPECT_EQ(wd.poll(t, c), A::NONE);
}
}
TEST(RadioSilenceWatchdog, BackoffDoublesThresholdAndCaps) {
A wd;
wd.begin(0, 0, 2);
wd.setMinutes(60);
EXPECT_EQ(wd.thresholdMs(), 4u * 60 * MIN);
EXPECT_EQ(wd.nextBackoff(), 3);
uint32_t at = 0;
EXPECT_EQ(runQuiet(wd, 0, 4 * 60 * MIN + MIN, 0, &at), A::KICK_RX);
EXPECT_GE(at, 4u * 60 * MIN);
A capped;
capped.begin(0, 0, 9);
EXPECT_EQ(capped.backoff(), A::MAX_BACKOFF);
EXPECT_EQ(capped.nextBackoff(), A::MAX_BACKOFF);
capped.setMinutes(A::MAX_MINUTES);
EXPECT_EQ(capped.thresholdMs(), (uint32_t)A::MAX_MINUTES * MIN * 16); // fits in 32 bits
}
TEST(RadioSilenceWatchdog, FirstFrameClearsBackoffOnce) {
A wd;
wd.begin(0, 0, 3);
wd.setMinutes(60);
EXPECT_FALSE(wd.takeBackoffCleared());
EXPECT_EQ(wd.poll(A::CHECK_MS, 1), A::NONE);
EXPECT_EQ(wd.backoff(), 0);
EXPECT_TRUE(wd.takeBackoffCleared());
EXPECT_FALSE(wd.takeBackoffCleared());
EXPECT_EQ(wd.thresholdMs(), 60u * MIN);
}
TEST(RadioSilenceWatchdog, NoClearWithoutBackoff) {
A wd;
wd.begin(0, 0, 0);
wd.setMinutes(60);
wd.poll(A::CHECK_MS, 1);
EXPECT_FALSE(wd.takeBackoffCleared());
}
TEST(RadioSilenceWatchdog, SurvivesMillisWrap) {
// traffic right up to the wrap, then a healthy receiver after it: no action
A wd;
uint32_t start = 0xFFFFFFFFu - 30 * MIN;
wd.begin(start, 0, 0);
wd.setMinutes(20);
uint32_t rx = 0;
for (uint32_t i = 1; i < 240; i++) {
uint32_t t = start + i * A::CHECK_MS; // wraps around zero half-way
if (i % 10 == 0) rx++;
EXPECT_EQ(wd.poll(t, rx), A::NONE) << "i=" << i;
}
}
TEST(RadioSilenceWatchdog, GenuineSilenceAcrossWrapStillDetected) {
A wd;
uint32_t start = 0xFFFFFFFFu - 5 * MIN;
wd.begin(start, 0, 0);
wd.setMinutes(20);
uint32_t at = 0;
EXPECT_EQ(runQuiet(wd, start, start + 21 * MIN, 0, &at), A::KICK_RX);
EXPECT_GE((uint32_t)(at - start), 20 * MIN);
}
TEST(RadioSilenceWatchdog, PacedToCheckInterval) {
A wd;
wd.begin(0, 0, 0);
wd.setMinutes(1);
// polled every millisecond the logic still runs only on CHECK_MS boundaries:
// one kick, exactly at the check that first sees the 1 min threshold
int actions = 0;
uint32_t kicked_at = 0;
for (uint32_t t = 1; t < 3 * MIN; t++) {
A::Action a = wd.poll(t, 0);
if (a != A::NONE) {
actions++;
if (a == A::KICK_RX) kicked_at = t;
}
}
EXPECT_EQ(kicked_at, 2 * A::CHECK_MS);
EXPECT_EQ(actions, 1); // the reboot step (5 min) is not due yet
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}