mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 10:54:13 +00:00
fix: address radio2 review findings
- Use the profile stamped at reception for delayed packets, so policy, replies and duplicate targets are not decided by a later retune. - Count primary activity at reception and TX on the profile actually used. - Keep a paused scanner on primary, report `off; paused`, and stop `both` from sending two copies on primary during a tempradio change. - Wake for a deferred radio2 apply and for a retune home, instead of taking the stock sleep path. - Send periodic adverts through sendSelfAdvertisement so `advert both` applies, and keep locally originated adverts on primary. - Let nRF52 sleepMs() return on a non-RTC interrupt, as its contract says. - Reject trailing tokens and extra comma fields in the radio2 preset.
This commit is contained in:
1 parent
ea49202c97
commit
3cd0fe194c
5 files changed
+126
-16
No files matched your search
@@ -528,10 +528,13 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
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.
|
||||
// just read from, so the observer and later hooks see the origin. The stamp
|
||||
// must be taken here, not when the packet is processed: a flood packet can
|
||||
// sit in the delayed-inbound queue across a scheduler retune.
|
||||
const uint8_t rx = radio2.currentProfile();
|
||||
pkt->setRxProfile(rx);
|
||||
radio2.noteRx(rx);
|
||||
if (rx == RADIO_PROFILE_PRIMARY) radio2.notePrimaryPacket();
|
||||
}
|
||||
#ifdef WITH_MQTT_OBSERVER
|
||||
mqttObserver.capture(pkt, score);
|
||||
@@ -564,7 +567,10 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
}
|
||||
|
||||
void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
if (radio2.isScanning()) radio2.noteTx(pkt->getTxProfile());
|
||||
// The scanner's current preset is the one selectTxProfile() just tuned for;
|
||||
// a fallback send (paused/inactive) goes out on primary whatever the packet
|
||||
// asked for.
|
||||
if (radio2.isScanning()) radio2.noteTx(radio2.currentProfile());
|
||||
#ifdef WITH_MQTT_OBSERVER
|
||||
mqttObserver.captureTx(pkt);
|
||||
#endif
|
||||
@@ -624,14 +630,17 @@ mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) {
|
||||
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();
|
||||
// The origin is the profile stamped at reception, not the scanner's
|
||||
// current preset: a delayed flood packet may be processed after a retune.
|
||||
rx_profile = pkt->getRxProfile();
|
||||
if (rx_profile != RADIO_PROFILE_PRIMARY && rx_profile != RADIO_PROFILE_SECONDARY) {
|
||||
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();
|
||||
}
|
||||
|
||||
if (pkt->getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD) {
|
||||
@@ -1262,6 +1271,10 @@ void MyMesh::sendSelfAdvertisement(int delay_millis, bool flood) {
|
||||
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!");
|
||||
return;
|
||||
}
|
||||
// A local advert follows `radio2 advert`, not the preset that triggered it
|
||||
// (a remote `advert` command arrives on one preset, but the node's own
|
||||
// advert is primary by default).
|
||||
pkt->setTxProfile(RADIO_PROFILE_PRIMARY);
|
||||
|
||||
// Copy before queueing the first packet: the queued packet stays valid, but
|
||||
// copying first keeps the two independent from the start.
|
||||
@@ -1614,15 +1627,12 @@ void MyMesh::loop() {
|
||||
}
|
||||
|
||||
if (next_flood_advert && millisHasNowPassed(next_flood_advert)) {
|
||||
mesh::Packet *pkt = createSelfAdvert();
|
||||
uint32_t delay_millis = 0;
|
||||
if (pkt) sendFloodScoped(default_scope, pkt, delay_millis, _prefs.path_hash_mode + 1);
|
||||
sendSelfAdvertisement(0, true);
|
||||
|
||||
updateFloodAdvertTimer(); // schedule next flood advert
|
||||
updateAdvertTimer(); // also schedule local advert (so they don't overlap)
|
||||
} else if (next_local_advert && millisHasNowPassed(next_local_advert)) {
|
||||
mesh::Packet *pkt = createSelfAdvert();
|
||||
if (pkt) sendZeroHop(pkt);
|
||||
sendSelfAdvertisement(0, false);
|
||||
|
||||
updateAdvertTimer(); // schedule next local advert
|
||||
}
|
||||
|
||||
@@ -285,6 +285,9 @@ bool Radio2::selectTxProfile(uint8_t profile) {
|
||||
// secondary-target packet falls back to it; while a retune back is pending,
|
||||
// wait so nothing is ever transmitted on the wrong preset.
|
||||
if (!active) return current == RADIO_PROFILE_PRIMARY;
|
||||
// A paused scanner only permits the primary preset: a queued secondary-target
|
||||
// packet must not retune the radio away from a temporary primary change.
|
||||
if (paused) return current == RADIO_PROFILE_PRIMARY;
|
||||
if (current == profile) return true;
|
||||
if (device.hasCompletedReceive() || device.isReceiveInProgress()) return false;
|
||||
if (!switchTo(profile, false)) return current == profile; // busy or failed: retry
|
||||
@@ -390,7 +393,19 @@ bool Radio2::applyPending() {
|
||||
}
|
||||
|
||||
uint32_t Radio2::getSleepMs() {
|
||||
if (!active || paused || pending_apply) return 0;
|
||||
// A deferred apply is a deadline like any other: without it the MCU may take
|
||||
// the stock (30 s) sleep and turn radio2 on/off far later than documented.
|
||||
if (pending_apply) {
|
||||
int32_t delta = (int32_t)(pending_apply_at_ms - millis());
|
||||
if (delta <= 0) return 1;
|
||||
return (uint32_t)delta < RADIO2_SLEEP_MAX_MS ? (uint32_t)delta : RADIO2_SLEEP_MAX_MS;
|
||||
}
|
||||
if (!active || paused) {
|
||||
// Nothing is being scanned, but a retune back to primary may still be owed
|
||||
// (e.g. a disable that landed while a receive was in progress). Wake for
|
||||
// it instead of taking the stock sleep with the radio off-home.
|
||||
return current != RADIO_PROFILE_PRIMARY ? RADIO2_SWITCH_RETRY_MS : 0;
|
||||
}
|
||||
if (!device.isInRecvMode() || device.hasCompletedReceive() ||
|
||||
device.isReceiveInProgress()) {
|
||||
return 1; // wake soon; never sleep through a packet
|
||||
@@ -673,8 +688,8 @@ static void radio2SetPreset(Radio2& r, const char* tuple, bool rxps_enabled,
|
||||
snprintf(reply, CLI_REPLY_MAX, "Err - radio2 preset must be <freq>,<bw>,<sf>,<cr>");
|
||||
return;
|
||||
}
|
||||
const char* parts[4];
|
||||
int n = mesh::Utils::parseTextParts(scratch, parts, 4);
|
||||
const char* parts[5];
|
||||
int n = mesh::Utils::parseTextParts(scratch, parts, 5);
|
||||
if (n != 4) {
|
||||
snprintf(reply, CLI_REPLY_MAX, "Err - radio2 preset must be <freq>,<bw>,<sf>,<cr>");
|
||||
return;
|
||||
@@ -778,7 +793,9 @@ void radio2CLI(Radio2& r, bool rxps_enabled, bool extra_sf_enabled,
|
||||
}
|
||||
|
||||
// Anything else is a preset tuple. nextToken() only split on spaces, so the
|
||||
// token still holds the whole comma list.
|
||||
// token still holds the whole comma list. Reject anything after it: a preset
|
||||
// command is a complete command line, not a prefix.
|
||||
if (!cliNoExtra(p, reply, RADIO2_USAGE)) return;
|
||||
radio2SetPreset(r, sub, rxps_enabled, extra_sf_enabled, reply);
|
||||
}
|
||||
|
||||
@@ -96,7 +96,10 @@ public:
|
||||
void resumeAfterPrimaryChange(bool rxps_enabled, bool extra_sf_enabled);
|
||||
|
||||
bool isEnabled() const { return cfg.enabled != 0; }
|
||||
bool isScanning() const { return active; }
|
||||
// True only while the scanner is actually time-sharing. A paused scanner
|
||||
// (temporary primary change) reports false, matching the `off; paused` status
|
||||
// and keeping policy/counters off the radio until it resumes.
|
||||
bool isScanning() const { return active && !paused; }
|
||||
bool isPaused() const { return paused; }
|
||||
bool hasPendingApply() const { return pending_apply; }
|
||||
// True only while the scheduler/save machinery has something to do. When
|
||||
|
||||
@@ -353,6 +353,9 @@ void NRF52Board::sleepMs(uint32_t ms) {
|
||||
__WFE();
|
||||
__WFE();
|
||||
}
|
||||
// A radio/other interrupt brought the CPU back: return now so the loop can
|
||||
// service it. The RTC deadline is re-armed by the next sleepMs() call.
|
||||
if (!rtc2_fired) break;
|
||||
}
|
||||
|
||||
NRF_RTC2->INTENCLR = RTC_INTENCLR_COMPARE0_Msk;
|
||||
|
||||
@@ -421,7 +421,7 @@ TEST(Radio2Timing, OverlapRejected) {
|
||||
EXPECT_EQ(r.getSecondaryProfile().freq, 0.0f);
|
||||
}
|
||||
|
||||
TEST(Radio2Timing, SleepDeadline) {
|
||||
TEST(Radio2Scheduler, SleepDeadline) {
|
||||
FakeDevice dev;
|
||||
Radio2 r(dev);
|
||||
setPrimary(r);
|
||||
@@ -435,10 +435,57 @@ TEST(Radio2Timing, SleepDeadline) {
|
||||
EXPECT_EQ(r.getSleepMs(), 1u);
|
||||
dev.completed = false;
|
||||
|
||||
// Turning it off is deferred too, so the deadline still matters until the
|
||||
// apply lands and the radio is back on primary.
|
||||
r.setEnabled(false);
|
||||
EXPECT_GT(r.getSleepMs(), 0u);
|
||||
g_mock_millis += RADIO2_APPLY_DELAY_MS;
|
||||
r.loop(&scheduler_fs, true);
|
||||
EXPECT_EQ(r.getSleepMs(), 0u);
|
||||
}
|
||||
|
||||
TEST(Radio2Scheduler, SleepCoversDeferredApply) {
|
||||
g_mock_millis = 0;
|
||||
FakeDevice dev;
|
||||
Radio2 r(dev);
|
||||
setPrimary(r);
|
||||
ASSERT_EQ(r.setPreset(915.0f, 125.0f, 10, 5), RADIO2_SET_OK);
|
||||
ASSERT_TRUE(r.hasPendingApply());
|
||||
|
||||
// The deferred apply is a wake deadline; the stock sleep would delay it.
|
||||
uint32_t ms = r.getSleepMs();
|
||||
EXPECT_GT(ms, 0u);
|
||||
EXPECT_LE(ms, RADIO2_SLEEP_MAX_MS);
|
||||
|
||||
g_mock_millis += RADIO2_APPLY_DELAY_MS;
|
||||
EXPECT_EQ(r.getSleepMs(), 1u);
|
||||
r.loop(&scheduler_fs, true);
|
||||
EXPECT_FALSE(r.hasPendingApply());
|
||||
}
|
||||
|
||||
TEST(Radio2Scheduler, SleepCoversRetuneHome) {
|
||||
g_mock_millis = 0;
|
||||
FakeDevice dev;
|
||||
Radio2 r(dev);
|
||||
setPrimary(r);
|
||||
r.setPreset(915.0f, 125.0f, 10, 5);
|
||||
ASSERT_TRUE(r.start(false, false));
|
||||
g_mock_millis += 200;
|
||||
r.loop(&scheduler_fs, true);
|
||||
ASSERT_EQ(r.currentProfile(), RADIO_PROFILE_SECONDARY);
|
||||
|
||||
// The disable lands while a receive blocks the retune home: the deadline to
|
||||
// get back to primary must survive the stock sleep path.
|
||||
dev.in_progress = true;
|
||||
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_SECONDARY);
|
||||
EXPECT_GT(r.getSleepMs(), 0u);
|
||||
EXPECT_LE(r.getSleepMs(), RADIO2_SWITCH_RETRY_MS);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- scheduler
|
||||
|
||||
TEST(Radio2Isolation, DisabledIsInert) {
|
||||
@@ -496,6 +543,30 @@ TEST(Radio2Isolation, PausedDoesNotAdvertiseOnBoth) {
|
||||
r.pauseForPrimaryChange();
|
||||
EXPECT_FALSE(r.advertBoth());
|
||||
EXPECT_TRUE(r.needsService());
|
||||
|
||||
char reply[CLI_REPLY_MAX];
|
||||
radio2CLI(r, false, false, "status", reply);
|
||||
EXPECT_STREQ(reply, "off; paused");
|
||||
}
|
||||
|
||||
TEST(Radio2Isolation, PausedSendsOnPrimary) {
|
||||
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.pauseForPrimaryChange();
|
||||
int retunes = dev.retunes;
|
||||
EXPECT_FALSE(r.isScanning());
|
||||
// A queued secondary-target packet must not retune away from the temporary
|
||||
// primary change; it falls back to the current primary.
|
||||
EXPECT_TRUE(r.selectTxProfile(RADIO_PROFILE_SECONDARY));
|
||||
EXPECT_EQ(r.currentProfile(), RADIO_PROFILE_PRIMARY);
|
||||
EXPECT_EQ(dev.retunes, retunes);
|
||||
}
|
||||
|
||||
TEST(Radio2Scheduler, DisabledDoesNotStart) {
|
||||
@@ -826,6 +897,12 @@ TEST(Radio2Cli, Errors) {
|
||||
radio2CLI(r, false, false, "status junk", reply);
|
||||
EXPECT_EQ(strncmp(reply, "Err - usage:", 12), 0);
|
||||
EXPECT_LE(strlen(reply), (size_t)CLI_REPLY_MAX - 1);
|
||||
|
||||
radio2CLI(r, false, false, "915,125,10,5 junk", reply);
|
||||
EXPECT_EQ(strncmp(reply, "Err - usage:", 12), 0);
|
||||
|
||||
radio2CLI(r, false, false, "915,125,10,5,6", reply);
|
||||
EXPECT_STREQ(reply, "Err - radio2 preset must be <freq>,<bw>,<sf>,<cr>");
|
||||
}
|
||||
|
||||
TEST(Radio2Cli, Timing) {
|
||||
|
||||
Reference in new issue
Block a user