From cc4aee2165257aa184ce3628034f1ec052b0dbe8 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:27:07 -0600 Subject: [PATCH] refactor: keep radio2 inert while disabled --- RADIO2.md | 27 +++++++++++ examples/simple_repeater/MyMesh.cpp | 66 +++++++++++++++++--------- examples/simple_repeater/PersistUtil.h | 1 + examples/simple_repeater/Radio2.h | 8 +++- test/test_radio2/test_radio2.cpp | 57 ++++++++++++++++++++++ 5 files changed, 136 insertions(+), 23 deletions(-) diff --git a/RADIO2.md b/RADIO2.md index 6532011e..8b57e0d4 100644 --- a/RADIO2.md +++ b/RADIO2.md @@ -190,6 +190,33 @@ There is one shared noise floor, as today. No per-profile floor. `radio2` status may show the current value. If `int.thresh` is enabled, the value is a blend while scanning; this is accepted. +### 4.9 Isolation when off + +radio2 is off by default. Once it is off and settled (`enabled=0`, no pending +apply, already back on primary), the feature is inert: + +- no profile controller is installed on the radio: `selectTxProfile()` is the + core's default no-op; +- `radio2.loop()` is not called (`needsService()` is false) — no retunes, no + timers, no lazy-save passes; +- `logRx`/`logTx` and the receive policy hook are skipped; receive, forwarding, + battery, filter, fleet and observer code take their original paths; +- `getSleepMs()` returns 0, so `main.cpp` takes exactly the same sleep/no-sleep + branch it took before the feature existed; +- no packet is duplicated and `radio2 advert both` is inert; +- `/radio2_cfg` is read only at boot (two `exists` checks plus a 22-byte read + when present) and written only after a radio2 CLI change. + +The residual cost while off is boolean checks, not actions: one +`needsService()` per main loop, one `isScanning()` per packet, one +profile-control pointer check per transmit attempt, and two local metadata bytes +per packet. No RF activity, no timer, no wakeup, no extra filesystem write. + +The only window that is not fully "off" is the deliberate deferred apply after +`radio2 off` (up to `RADIO2_APPLY_DELAY_MS`, then until a packet in progress +finishes): the radio may still be on the secondary preset for that moment and +is retuned back to primary before the scanner stops. + --- ## 5. Architecture diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 12c7026e..81ed971d 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -526,11 +526,13 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { } void MyMesh::logRx(mesh::Packet *pkt, int len, float score) { - // logRx() runs before onRecvPacket(): stamp and count the profile recvRaw() - // just read from, so the observer and later hooks see the origin. - const uint8_t rx = radio2.currentProfile(); - pkt->setRxProfile(rx); - radio2.noteRx(rx); + if (radio2.isScanning()) { + // logRx() runs before onRecvPacket(): stamp and count the profile recvRaw() + // just read from, so the observer and later hooks see the origin. + const uint8_t rx = radio2.currentProfile(); + pkt->setRxProfile(rx); + radio2.noteRx(rx); + } #ifdef WITH_MQTT_OBSERVER mqttObserver.capture(pkt, score); #endif @@ -562,7 +564,7 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) { } void MyMesh::logTx(mesh::Packet *pkt, int len) { - radio2.noteTx(pkt->getTxProfile()); + if (radio2.isScanning()) radio2.noteTx(pkt->getTxProfile()); #ifdef WITH_MQTT_OBSERVER mqttObserver.captureTx(pkt); #endif @@ -617,14 +619,20 @@ uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { } mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) { - // The scanner's current profile is the one recvRaw() just read from. - const uint8_t rx_profile = radio2.currentProfile(); - const Radio2Action rx_action = radio2.actionFor(rx_profile, pkt); - rx_forward_allowed = rx_action != RADIO2_ACTION_DROP; + rx_forward_allowed = true; // the off path must never inherit a stale drop + const bool radio2_rx = radio2.isScanning(); + uint8_t rx_profile = RADIO_PROFILE_PRIMARY; + Radio2Action rx_action = RADIO2_ACTION_SAME; + if (radio2_rx) { + // The scanner's current profile is the one recvRaw() just read from. + rx_profile = radio2.currentProfile(); + rx_action = radio2.actionFor(rx_profile, pkt); + rx_forward_allowed = rx_action != RADIO2_ACTION_DROP; - pkt->setRxProfile(rx_profile); - pkt->setTxProfile(radio2.targetFor(rx_action, rx_profile)); - if (rx_profile == RADIO_PROFILE_PRIMARY) radio2.notePrimaryPacket(); + pkt->setRxProfile(rx_profile); + pkt->setTxProfile(radio2.targetFor(rx_action, rx_profile)); + if (rx_profile == RADIO_PROFILE_PRIMARY) radio2.notePrimaryPacket(); + } if (pkt->getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD) { recv_pkt_region = region_map.findMatch(pkt, REGION_DENY_FLOOD); @@ -644,16 +652,21 @@ mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) { // the forwarding hook at all. filter.clearContentVerdict(); - // Replies created anywhere below (core ACK/path return, CLI, fleet) inherit - // the origin profile through obtainNewPacket(). - const uint8_t previous_default = getDefaultTxProfile(); - setDefaultTxProfile(radio2.replyProfile(rx_profile)); - mesh::DispatcherAction action = Mesh::onRecvPacket(pkt); - setDefaultTxProfile(previous_default); + mesh::DispatcherAction action; + if (radio2_rx) { + // Replies created anywhere below (core ACK/path return, CLI, fleet) inherit + // the origin profile through obtainNewPacket(). + const uint8_t previous_default = getDefaultTxProfile(); + setDefaultTxProfile(radio2.replyProfile(rx_profile)); + action = Mesh::onRecvPacket(pkt); + setDefaultTxProfile(previous_default); + } else { + action = Mesh::onRecvPacket(pkt); + } filter.clearContentVerdict(); - if (rx_action == RADIO2_ACTION_BOTH && + if (radio2_rx && rx_action == RADIO2_ACTION_BOTH && action != ACTION_RELEASE && action != ACTION_MANUAL_HOLD) { radio2QueueDuplicate(pkt, action, rx_profile); } @@ -1139,7 +1152,6 @@ void MyMesh::begin(FILESYSTEM *fs) { // load persisted prefs _cli.loadPrefs(_fs); radio2.begin(_fs); - radio_driver.setProfileControl(&radio2); #ifdef WITH_MQTT_OBSERVER mqttObserver.begin(_fs, &self_id, _prefs.node_name, board.getManufacturerName(), FIRMWARE_VERSION, _prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); @@ -1190,6 +1202,9 @@ void MyMesh::begin(FILESYSTEM *fs) { radio2.setPrimary(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); radio2.start(_prefs.rxps.enabled, _prefs.extra_sf[0] != 0); + // With radio2 off the radio layer keeps its upstream default behaviour: no + // profile controller is installed at all. + radio_driver.setProfileControl(radio2.isScanning() ? &radio2 : nullptr); radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain); MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s", @@ -1589,7 +1604,14 @@ void MyMesh::loop() { mesh::Mesh::loop(); - radio2.loop(_fs, _mgr->getOutboundTotal() == 0); + if (radio2.needsService()) { + radio2.loop(_fs, _mgr->getOutboundTotal() == 0); + // Keep the radio layer free of radio2 whenever it is not needed: with + // radio2 off and on primary, selectTxProfile() is the upstream default. + radio_driver.setProfileControl( + (radio2.isScanning() || radio2.currentProfile() != RADIO_PROFILE_PRIMARY) + ? &radio2 : nullptr); + } if (next_flood_advert && millisHasNowPassed(next_flood_advert)) { mesh::Packet *pkt = createSelfAdvert(); diff --git a/examples/simple_repeater/PersistUtil.h b/examples/simple_repeater/PersistUtil.h index 512ea91a..7b6a60ca 100644 --- a/examples/simple_repeater/PersistUtil.h +++ b/examples/simple_repeater/PersistUtil.h @@ -28,6 +28,7 @@ class LazySave { public: LazySave() : dirty(false), dirty_since(0) {} void markDirty() { dirty = true; dirty_since = millis(); } + bool isDirty() const { return dirty; } bool due() const { return dirty && (uint32_t)(millis() - dirty_since) >= LAZY_SAVE_DELAY_MS; } // The config reached the disk. Only call this after a confirmed write. diff --git a/examples/simple_repeater/Radio2.h b/examples/simple_repeater/Radio2.h index 7b2998cc..6ea0edad 100644 --- a/examples/simple_repeater/Radio2.h +++ b/examples/simple_repeater/Radio2.h @@ -99,6 +99,12 @@ public: bool isScanning() const { return active; } bool isPaused() const { return paused; } bool hasPendingApply() const { return pending_apply; } + // True only while the scheduler/save machinery has something to do. When + // radio2 is off and settled this is false, so MyMesh skips radio2.loop(). + bool needsService() const { + return active || paused || pending_apply || current != RADIO_PROFILE_PRIMARY || + save_flag.isDirty(); + } uint8_t currentProfile() const { return current; } Radio2Conflict conflict() const { return conflict_state; } @@ -106,7 +112,7 @@ public: Radio2Action actionFor(uint8_t origin, const mesh::Packet* pkt) const; uint8_t targetFor(Radio2Action action, uint8_t origin) const; uint8_t replyProfile(uint8_t origin) const; - bool advertBoth() const { return cfg.advert == RADIO2_ADVERT_BOTH && active; } + bool advertBoth() const { return cfg.advert == RADIO2_ADVERT_BOTH && active && !paused; } uint8_t getPolicyPrimary() const { return cfg.policy_primary; } uint8_t getPolicySecondary() const { return cfg.policy_secondary; } uint8_t getAdvert() const { return cfg.advert; } diff --git a/test/test_radio2/test_radio2.cpp b/test/test_radio2/test_radio2.cpp index 50e93429..4d8a4e6b 100644 --- a/test/test_radio2/test_radio2.cpp +++ b/test/test_radio2/test_radio2.cpp @@ -372,6 +372,63 @@ TEST(Radio2Timing, SleepDeadline) { // ---------------------------------------------------------------- scheduler +TEST(Radio2Isolation, DisabledIsInert) { + g_mock_millis = 0; + FakeDevice dev; + Radio2 r(dev); + setPrimary(r); + + EXPECT_FALSE(r.needsService()); + EXPECT_FALSE(r.start(false, false)); + EXPECT_FALSE(r.needsService()); + EXPECT_FALSE(r.isScanning()); + EXPECT_EQ(r.getSleepMs(), 0u); + + // A secondary-target packet falls back to primary without any retune. + EXPECT_TRUE(r.selectTxProfile(RADIO_PROFILE_SECONDARY)); + EXPECT_EQ(dev.retunes, 0); + + r.loop(&scheduler_fs, true); + EXPECT_EQ(dev.retunes, 0); +} + +TEST(Radio2Isolation, DisableSettlesBackToIdle) { + g_mock_millis = 0; + FakeDevice dev; + Radio2 r(dev); + setPrimary(r); + ASSERT_EQ(r.setPreset(915.0f, 125.0f, 10, 5), RADIO2_SET_OK); + g_mock_millis += RADIO2_APPLY_DELAY_MS; + r.loop(&scheduler_fs, true); + ASSERT_TRUE(r.isScanning()); + + r.setEnabled(false); + g_mock_millis += RADIO2_APPLY_DELAY_MS; + r.loop(&scheduler_fs, true); + EXPECT_FALSE(r.isScanning()); + EXPECT_EQ(r.currentProfile(), RADIO_PROFILE_PRIMARY); + + g_mock_millis += LAZY_SAVE_DELAY_MS + 1; // let the config write finish + r.loop(&scheduler_fs, true); + EXPECT_FALSE(r.needsService()); // nothing left to service while off +} + +TEST(Radio2Isolation, PausedDoesNotAdvertiseOnBoth) { + g_mock_millis = 0; + FakeDevice dev; + Radio2 r(dev); + setPrimary(r); + ASSERT_EQ(r.setPreset(915.0f, 125.0f, 10, 5), RADIO2_SET_OK); + g_mock_millis += RADIO2_APPLY_DELAY_MS; + r.loop(&scheduler_fs, true); + r.setAdvert(RADIO2_ADVERT_BOTH); + ASSERT_TRUE(r.advertBoth()); + + r.pauseForPrimaryChange(); + EXPECT_FALSE(r.advertBoth()); + EXPECT_TRUE(r.needsService()); +} + TEST(Radio2Scheduler, DisabledDoesNotStart) { g_mock_millis = 0; FakeDevice dev;