Fix OTA resume and radio state recovery

Preserve resumed nRF52 flash pages and keep filtered OTA catalogs incomplete until full pages arrive. Bound stuck radio preparation, report companion setting failures, retain safe rollback, and codify conservative advert penalties with regression tests.
This commit is contained in:
mikecarper committed 2026-09-15 12:17:54 -07:00
1 parent 62ce8d3320
commit e4e0de8a79
22 files changed
+1175 -59

No files matched your search

+183
View File
@@ -0,0 +1,183 @@
#include <helpers/ota/OtaManager.h>
#include <helpers/ota/OtaByteIO.h>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace mesh::ota;
static bool send(void*, const uint8_t*, uint16_t, bool) { return true; }
static void advert(OtaManager& manager, uint32_t source, uint8_t count = 1) {
AdvMsg message{};
wr_u32le(message.seeder_id, source);
wr_u32le(message.set_digest, 123);
message.n_motas = count;
uint8_t wire[24];
manager.on_message(wire, encode_adv(wire, sizeof(wire), message));
}
static void have(OtaManager& manager, uint32_t source, uint32_t mid) {
uint8_t row[OTA_HAVE_ROW_BYTES] = {};
wr_u32le(row, mid);
wr_u32le(row + 4, 1234);
row[12] = CODEC_FULL; row[13] = MFLAG_FULL;
row[14] = 1;
HaveMsg message{};
wr_u32le(message.seeder_id, source);
wr_u32le(message.set_digest, 123);
message.frag_total = 1; message.n_rows = 1; message.rows = row;
uint8_t wire[64];
manager.on_message(wire, encode_have(wire, sizeof(wire), message));
}
static const OtaManager::CatRow* row(OtaManager& manager, uint32_t mid) {
for (unsigned i = 0; i < manager.catalogCount(); ++i) {
const auto* entry = manager.catalogRow(i);
if (rd_u32le(entry->mid) == mid) return entry;
}
return nullptr;
}
using Messages = std::vector<std::vector<uint8_t>>;
static bool capture(void* context, const uint8_t* message, uint16_t length, bool) {
static_cast<Messages*>(context)->emplace_back(message, message + length);
return true;
}
class MixedCatalogSource : public MotaSource {
uint8_t _count;
public:
explicit MixedCatalogSource(uint8_t count) : _count(count) {}
uint8_t count() override { return _count; }
bool describe(uint8_t index, MotaDesc& out) override {
if (index >= _count) return false;
wr_u32le(out.mid, 100u + index);
// Three pages: fully matching, partially matching, and empty for target 1.
out.target_id = _count == 2 ? index + 1u
: index < 10 ? 1u : index < 20 ? 1u + index % 2u : 2u;
out.fw_version = 1; out.codec_id = CODEC_FULL; out.flags = MFLAG_FULL;
out.block_size_log2 = 7; out.block_count = 1;
out.leaves_off = 8 + MOTA_MFL; out.payload_off = out.leaves_off + 4;
out.payload_size = 128; out.total_size = out.payload_off + out.payload_size + 5;
return true;
}
bool read(uint8_t, uint32_t, uint8_t*, uint32_t) override { return false; }
};
static void filtered_catalog_remains_queryable(int scenario, bool automatic) {
Messages from_server, from_client;
OtaManager server, client;
server.begin(0, capture, &from_server);
client.begin(0, capture, &from_client);
client.set_archive_interest(automatic);
uint8_t sid[4] = {1, 2, 3, 4}; server.set_seeder_id(sid);
MixedCatalogSource source(scenario == 4 ? 2 : 24);
assert(server.add_source(&source));
server.announce();
assert(from_server.size() == 1);
AdvMsg adv{};
assert(decode_adv(from_server[0].data(), from_server[0].size(), adv));
if (scenario != 8) client.on_message(from_server[0].data(), from_server[0].size());
from_server.clear();
QueryMsg query{};
memcpy(query.seeder_id, sid, 4); memcpy(query.set_digest, adv.set_digest, 4);
query.filter_target = scenario == 6 ? 99 : 1;
uint8_t wire[256];
server.on_message(wire, encode_query(wire, sizeof(wire), query));
Messages filtered = from_server;
if (scenario == 7) {
// Legacy filtered servers packed only the matching rows into fewer pages.
filtered.pop_back();
for (auto& message : filtered) {
HaveMsg legacy{};
assert(decode_have(message.data(), message.size(), legacy));
legacy.frag_total = 2;
const uint16_t length = encode_have(wire, sizeof(wire), legacy);
message.assign(wire, wire + length);
}
}
for (const auto& message : filtered) client.on_message(message.data(), message.size());
assert(client.catalogCount() == (scenario == 4 ? 1 : scenario == 6 ? 0 : 15));
if (scenario == 8) {
// Passive rows arriving before a beacon must not confer completeness on its new digest.
client.on_message(wire, encode_adv(wire, sizeof(wire), adv));
assert(client.catalogCount() == 0);
}
if (automatic) {
client.set_clock(5000);
client.loop();
} else {
client.queryAll();
}
assert(from_client.size() == 1);
QueryMsg recovery{};
assert(decode_query(from_client[0].data(), from_client[0].size(), recovery));
assert(recovery.filter_target == 0);
assert(recovery.want_fragments == (scenario == 4 ? 1u : scenario == 5 ? 6u : scenario == 8 ? 0u : 7u));
from_server.clear();
server.on_message(from_client[0].data(), from_client[0].size());
for (const auto& message : from_server) client.on_message(message.data(), message.size());
assert(client.catalogCount() == source.count());
from_client.clear();
// Late filtered packets must not downgrade already-complete pages either.
for (const auto& message : filtered) client.on_message(message.data(), message.size());
client.queryAll();
client.set_clock(5000 + OTA_CATALOG_RETRY_MS + 1);
client.loop();
assert(from_client.empty());
}
int main(int argc, char** argv) {
assert(argc == 3);
const int scenario = atoi(argv[1]);
const bool rollover = atoi(argv[2]) != 0;
if (scenario >= 4) {
filtered_catalog_remains_queryable(scenario, rollover);
return 0;
}
const uint32_t old = rollover ? 0xFFFF0000u : 1000u;
const uint32_t recent = rollover ? 0u : 100000u;
OtaManager manager;
manager.begin(1234, send, nullptr);
if (scenario == 0) {
for (unsigned i = 1; i <= OTA_MAX_SOURCES; ++i) {
manager.set_clock(old + i * 100);
advert(manager, i); have(manager, i, i);
}
manager.set_clock(recent);
advert(manager, 1); // freshest source is no longer the eviction victim
manager.set_clock(recent + 10);
advert(manager, OTA_MAX_SOURCES + 1);
assert(row(manager, 1));
assert(!row(manager, 2));
assert(manager.sourceCount() == OTA_MAX_SOURCES);
} else if (scenario == 1) {
for (unsigned i = 1; i <= OTA_MAX_CATALOG; ++i) {
manager.set_clock(old + i * 100);
have(manager, 1, i);
}
manager.set_clock(recent);
have(manager, 1, 1);
manager.set_clock(recent + 10);
have(manager, 1, OTA_MAX_CATALOG + 1);
assert(row(manager, 1));
assert(!row(manager, 2));
assert(manager.catalogCount() == OTA_MAX_CATALOG);
} else {
manager.set_clock(old);
have(manager, 1, 1);
manager.set_clock(recent);
have(manager, 2, 1);
if (scenario == 2) {
assert(row(manager, 1)->last_ms == recent);
} else {
manager.set_clock(recent + 10);
have(manager, 3, 1);
manager.set_clock(recent + 20);
advert(manager, 3, 0); // withdrawal recomputes newest surviving source
assert(row(manager, 1)->last_ms == recent);
assert(row(manager, 1)->n_seeders == 2);
}
}
}
@@ -286,6 +286,90 @@ static void dirty_flash_partial_checkpoint_finishes(bool reboot) {
CHECK(std::memcmp(active.data(), bytes.data(), total) == 0);
}
static void resumed_partial_payload_pages_preserve_committed_prefixes() {
using namespace mesh::ota;
const uint32_t total = 4u * MOTA_NRF52_FLASH_PAGE;
for (uint32_t prefix : {MOTA_NRF52_FLASH_PAGE + 1u,
MOTA_NRF52_FLASH_PAGE + 1024u,
2u * MOTA_NRF52_FLASH_PAGE,
2u * MOTA_NRF52_FLASH_PAGE + 227u}) {
reset_target_memory();
auto bytes = patterned_container(total);
std::memcpy(bytes.data() + total - 5u, MOTA_TRAILER, 5u);
{
OtaStoreFlashNrf52 original;
CHECK(original.begin(total));
CHECK(original.write(0, bytes.data(), prefix));
CHECK(original.write(total - 5u, MOTA_TRAILER, 5u));
original.checkpoint();
}
OtaStoreFlashNrf52 resumed;
CHECK(resumed.reopen());
CHECK(resumed.write(prefix, bytes.data() + prefix, total - prefix - 5u));
CHECK(resumed.finalize());
std::vector<uint8_t> output(total);
CHECK(resumed.read(0, output.data(), total));
CHECK(output == bytes);
CHECK(std::memcmp(resumed.data(), bytes.data(), total) == 0);
}
}
static void resumed_out_of_order_writes_survive_another_checkpoint() {
using namespace mesh::ota;
reset_target_memory();
const uint32_t total = 4u * MOTA_NRF52_FLASH_PAGE;
auto bytes = patterned_container(total);
std::memcpy(bytes.data() + total - 5u, MOTA_TRAILER, 5u);
{
OtaStoreFlashNrf52 original;
CHECK(original.begin(total));
CHECK(original.write(0, bytes.data(), total));
original.checkpoint();
}
const uint32_t offsets[] = {MOTA_NRF52_FLASH_PAGE + 11u,
3u * MOTA_NRF52_FLASH_PAGE + 17u,
2u * MOTA_NRF52_FLASH_PAGE + 25u,
MOTA_NRF52_FLASH_PAGE + 53u};
{
OtaStoreFlashNrf52 resumed;
CHECK(resumed.reopen());
for (uint32_t offset : offsets) {
for (unsigned i = 0; i < 13; ++i) bytes[offset + i] ^= 0x5A;
CHECK(resumed.write(offset, bytes.data() + offset, 13));
}
resumed.checkpoint();
}
OtaStoreFlashNrf52 resumed_again;
CHECK(resumed_again.reopen());
// Cross a page boundary after the second restart, preserving bytes on both sides.
const uint32_t offset = 2u * MOTA_NRF52_FLASH_PAGE - 7u;
for (unsigned i = 0; i < 19; ++i) bytes[offset + i] ^= 0xA5;
CHECK(resumed_again.write(offset, bytes.data() + offset, 19));
CHECK(resumed_again.finalize());
std::vector<uint8_t> output(total);
CHECK(resumed_again.read(0, output.data(), total));
CHECK(output == bytes);
CHECK(std::memcmp(resumed_again.data(), bytes.data(), total) == 0);
}
static void fresh_begin_after_reopen_does_not_retain_old_payload_bytes() {
using namespace mesh::ota;
reset_target_memory();
const uint32_t total = 3u * MOTA_NRF52_FLASH_PAGE;
const auto bytes = patterned_container(total);
OtaStoreFlashNrf52 store;
CHECK(store.begin(total));
CHECK(store.write(0, bytes.data(), total));
store.checkpoint();
CHECK(store.reopen());
CHECK(store.begin(total));
const uint32_t offset = MOTA_NRF52_FLASH_PAGE + 128u;
CHECK(store.write(offset, bytes.data() + offset, 16));
uint8_t preceding[128];
CHECK(store.read(MOTA_NRF52_FLASH_PAGE, preceding, sizeof(preceding)));
for (uint8_t byte : preceding) CHECK(byte == 0xFF);
}
int main() {
map_target_region(FLASH_MAP_START, FLASH_MAP_SIZE);
map_target_region(RAM_MAP_START, RAM_MAP_SIZE);
@@ -303,6 +387,12 @@ int main() {
std::puts("PASS: dirty flash partial checkpoint resumes and finishes");
dirty_flash_partial_checkpoint_finishes(false);
std::puts("PASS: dirty flash partial checkpoint continues without reboot");
std::puts("7 OtaStoreFlashNrf52 hybrid lifecycle checks passed");
resumed_partial_payload_pages_preserve_committed_prefixes();
std::puts("PASS: resumed payload pages preserve checkpointed prefixes");
resumed_out_of_order_writes_survive_another_checkpoint();
std::puts("PASS: resumed out-of-order writes survive repeated checkpoints");
fresh_begin_after_reopen_does_not_retain_old_payload_bytes();
std::puts("PASS: fresh captures after reopen discard stale payload bytes");
std::puts("10 OtaStoreFlashNrf52 hybrid lifecycle checks passed");
return 0;
}
+257 -2
View File
@@ -6,19 +6,29 @@ class DualProfileTestRadio : public RetryCodingRateRadio {
int busy_profile = -1;
bool fail_next_send = false;
bool hold_prepare_busy = false;
bool receive_mode = true, recovery_succeeds = true, clear_prepare_on_recovery = false;
bool carrier = false, carrier_on_receive = false;
unsigned carrier_services = 0, recoveries = 0;
bool isCarrierWaveActive() const override { return carrier; }
bool isInRecvMode() const override { return !carrier && !sending; }
bool isInRecvMode() const override { return receive_mode && !carrier && !sending; }
void loop() override { ++carrier_services; }
bool recoverRadio(bool) override { ++recoveries; return true; }
bool recoverRadio(bool) override {
++recoveries;
if (recovery_succeeds && clear_prepare_on_recovery) {
hold_prepare_busy = false;
receive_mode = true;
}
return recovery_succeeds;
}
uint32_t estimated_airtime = 10;
uint32_t secondary_airtime = 0;
float receive_score = 0;
float packetScore(float, int) override { return receive_score; }
TraceTestClock* sensing_clock = nullptr;
uint32_t sensing_delay = 0;
bool distinguish_airtime = false;
uint32_t getProfileAirtime(uint8_t profile, int bytes, uint8_t cr = 0) override {
if (profile == 1 && secondary_airtime) return secondary_airtime;
return distinguish_airtime ? (profile ? 70 : 900)
: mesh::Radio::getProfileAirtime(profile, bytes, cr);
}
@@ -79,6 +89,7 @@ class DualProfileTestMesh : public RetryCodingRateMesh {
bool cross_filter_allows = true;
float ota_speed = 1.0f;
bool suppress_tx = false;
bool enqueue_on_send_fail = false;
unsigned send_failures = 0;
unsigned send_completions = 0;
void onSendComplete(mesh::Packet* packet) override {
@@ -88,11 +99,24 @@ class DualProfileTestMesh : public RetryCodingRateMesh {
unsigned tx_failure_logs = 0;
void logTxFail(mesh::Packet*, int) override { ++tx_failure_logs; }
unsigned direct_successes = 0, direct_failures = 0;
unsigned flood_successes = 0, flood_failures = 0;
void onDirectRetrySucceeded(const uint8_t*, uint8_t, int8_t) override { ++direct_successes; }
void onDirectRetryFailed(const uint8_t*, uint8_t) override { ++direct_failures; }
void onFloodRetryEvent(const char* event, const mesh::Packet*, uint32_t, uint8_t) override {
if (strcmp(event, "good") == 0) ++flood_successes;
if (strcmp(event, "failure") == 0) ++flood_failures;
}
void onSendFail(mesh::Packet* packet) override {
++send_failures;
RetryCodingRateMesh::onSendFail(packet);
if (enqueue_on_send_fail) {
enqueue_on_send_fail = false;
auto* replacement = obtainNewPacket();
if (replacement) {
*replacement = makeFloodPacket(PAYLOAD_TYPE_GRP_TXT);
sendPacket(replacement, 0);
}
}
}
bool allowPacketTransmit(const mesh::Packet* packet) const override {
return !suppress_tx && RetryCodingRateMesh::allowPacketTransmit(packet);
@@ -268,6 +292,157 @@ TEST_F(DualProfileTest, RadioFaultRetryRetainsBoundedBusyEscapeAndSingleRetryLim
EXPECT_TRUE(radio.transmissions.empty());
}
TEST_F(DualProfileTest, StuckRadioRetryPreparationReleasesPacketAndRecoversQueue) {
node.flood_attempts = 0;
radio.config.cross = mesh::RadioCrossMode::Off;
radio.fail_next_send = true;
ASSERT_NE(nullptr, queue());
tick(); ASSERT_TRUE(node.hasOutbound());
ASSERT_NE(nullptr, queue()); // later work must not inherit the stuck packet
radio.hold_prepare_busy = true;
radio.receive_mode = false;
radio.clear_prepare_on_recovery = true;
tick(500); // arm only after the first BUSY preparation
tick(8000);
EXPECT_TRUE(node.hasOutbound());
EXPECT_EQ(radio.recoveries, 0u);
tick(20);
EXPECT_FALSE(node.hasOutbound());
EXPECT_EQ(node.send_failures, 1u);
EXPECT_EQ(node.tx_failure_logs, 1u);
EXPECT_EQ(radio.recoveries, 1u);
EXPECT_EQ(manager.getOutboundTotal(), 1);
EXPECT_EQ(manager.getFreeCount(), 39);
tick(); ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
EXPECT_EQ(manager.getFreeCount(), 40);
}
TEST_F(DualProfileTest, FailedBusyRecoveryDoesNotKeepRetryOwnershipOrDisableWatchdogs) {
node.flood_attempts = 0;
radio.config.cross = mesh::RadioCrossMode::Off;
radio.fail_next_send = true;
ASSERT_NE(nullptr, queue());
tick(); ASSERT_TRUE(node.hasOutbound());
radio.hold_prepare_busy = true;
radio.receive_mode = false;
radio.recovery_succeeds = false;
tick(500);
tick(9000);
EXPECT_FALSE(node.hasOutbound());
EXPECT_EQ(node.send_failures, 1u);
EXPECT_EQ(manager.getFreeCount(), 40);
EXPECT_EQ(radio.recoveries, 1u);
tick(9000); // the ordinary non-RX watchdog is no longer suppressed
EXPECT_EQ(radio.recoveries, 2u);
ASSERT_NE(nullptr, queue());
radio.recovery_succeeds = radio.clear_prepare_on_recovery = true;
tick(9000);
EXPECT_EQ(radio.recoveries, 3u);
ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
EXPECT_EQ(manager.getFreeCount(), 40);
}
TEST_F(DualProfileTest, BusyTimeoutPreservesPacketsEnqueuedByFailureCallback) {
node.flood_attempts = 0;
radio.config.cross = mesh::RadioCrossMode::Off;
radio.fail_next_send = true;
ASSERT_NE(nullptr, queue());
tick(); ASSERT_TRUE(node.hasOutbound());
node.enqueue_on_send_fail = true;
radio.hold_prepare_busy = true;
radio.clear_prepare_on_recovery = true;
tick(500);
tick(9000);
EXPECT_FALSE(node.hasOutbound());
EXPECT_EQ(node.send_failures, 1u);
EXPECT_EQ(manager.getOutboundTotal(), 1);
EXPECT_EQ(manager.getFreeCount(), 39);
tick(); ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
EXPECT_EQ(node.send_completions, 1u);
EXPECT_EQ(manager.getFreeCount(), 40);
}
TEST_F(DualProfileTest, InvalidPrepareAirtimeDoesNotCreateUnboundedDeadline) {
node.flood_attempts = 0;
radio.config.cross = mesh::RadioCrossMode::Off;
radio.fail_next_send = true;
ASSERT_NE(nullptr, queue());
tick(); ASSERT_TRUE(node.hasOutbound());
radio.estimated_airtime = UINT32_MAX;
radio.secondary_airtime = 20000;
radio.hold_prepare_busy = true;
tick(500);
tick(30000); // invalid primary must not erase the secondary's valid RX grace
EXPECT_TRUE(node.hasOutbound());
EXPECT_EQ(node.send_failures, 0u);
tick(8001);
EXPECT_FALSE(node.hasOutbound());
EXPECT_EQ(node.send_failures, 1u);
EXPECT_EQ(radio.recoveries, 1u);
EXPECT_EQ(manager.getFreeCount(), 40);
}
TEST_F(DualProfileTest, FinitePrepareBusyAndCadBackoffHaveSeparateAllowances) {
node.flood_attempts = 0;
radio.config.cross = mesh::RadioCrossMode::Off;
radio.fail_next_send = true;
ASSERT_NE(nullptr, queue());
tick();
radio.hold_prepare_busy = true;
tick(500);
tick(5000);
radio.hold_prepare_busy = false;
radio.busy_profile = 0;
tick(1000); // successful preparation clears the first BUSY allowance
radio.hold_prepare_busy = true;
tick(500);
tick(7000); // exceeds the original deadline, but not the new allowance
EXPECT_TRUE(node.hasOutbound());
EXPECT_EQ(node.send_failures, 0u);
EXPECT_EQ(radio.recoveries, 0u);
radio.hold_prepare_busy = false;
radio.busy_profile = -1;
tick(500); ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
EXPECT_EQ(manager.getFreeCount(), 40);
}
TEST_F(DualProfileTest, PrepareBusyAllowanceCoversLongFramesAndMillisWrap) {
TraceTestClock wrap_clock;
wrap_clock.now = UINT32_MAX - 1000UL;
DualProfileTestRadio wrap_radio;
StaticPoolPacketManager wrap_manager(40);
DualProfileTestMesh wrap_node(wrap_radio, wrap_clock, rng, rtc, wrap_manager, tables);
wrap_node.begin();
wrap_node.flood_attempts = 0;
wrap_radio.config.cross = mesh::RadioCrossMode::Off;
wrap_radio.estimated_airtime = 20000;
wrap_radio.fail_next_send = true;
auto* packet = wrap_node.obtainNewPacket();
ASSERT_NE(packet, nullptr);
*packet = makeFloodPacket(PAYLOAD_TYPE_GRP_TXT);
ASSERT_TRUE(wrap_node.sendFlood(packet));
auto advance = [&](uint32_t ms) {
wrap_clock.now = uint32_t(wrap_clock.now + ms);
wrap_node.loop();
};
advance(1); ASSERT_TRUE(wrap_node.hasOutbound());
wrap_radio.hold_prepare_busy = true;
advance(500);
advance(30000); // a slow full frame must not hit an eight-second fixed timeout
EXPECT_TRUE(wrap_node.hasOutbound());
EXPECT_EQ(wrap_radio.recoveries, 0u);
EXPECT_EQ(wrap_node.send_failures, 0u);
advance(8001); // 1.5 * 20 seconds + eight seconds, crossing millis wrap
EXPECT_FALSE(wrap_node.hasOutbound());
EXPECT_EQ(wrap_radio.recoveries, 1u);
EXPECT_EQ(wrap_node.send_failures, 1u);
EXPECT_EQ(wrap_manager.getFreeCount(), 40);
}
TEST_F(DualProfileTest, EchoDuringRadioFaultBackoffCancelsDirectAndFloodRetry) {
radio.config.cross = mesh::RadioCrossMode::Off;
radio.receive_score = 1; // strong echo is processed without a score delay
@@ -999,6 +1174,86 @@ TEST_F(DualProfileTest, ExpiredSecondaryPacketsAreDiscarded) {
EXPECT_EQ(40, manager.getFreeCount());
}
TEST_F(DualProfileTest, FinalFloodEchoWaitOnlyExpiresWhenItsOwnProfileChanges) {
node.flood_attempts = 1;
for (uint8_t origin : {0, 1}) for (unsigned change = 0; change < 3; ++change) {
SCOPED_TRACE(testing::Message() << "origin=" << unsigned(origin) << " change=" << change);
radio.config.cross = mesh::RadioCrossMode::Off;
tick();
auto* packet = node.obtainNewPacket(); ASSERT_NE(nullptr, packet);
*packet = makeFloodPacket(PAYLOAD_TYPE_GRP_TXT);
packet->radio_profile = origin;
packet->tx_radio = origin ? mesh::RADIO_TX_SECONDARY : mesh::RADIO_TX_PRIMARY;
ASSERT_TRUE(node.sendFlood(packet));
tick(); ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
ASSERT_EQ(manager.getOutboundTotal(), 1);
mesh::Packet echo = *manager.getOutboundByIdx(0);
echo.radio_bound = false; echo.radio_local = false;
echo.setPathHashCount(echo.getPathHashCount() + 1);
tick(10000); ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
ASSERT_EQ(manager.getOutboundTotal(), 0);
ASSERT_EQ(manager.getFreeCount(), 40); // final echo waits own metadata only
const auto successes = node.flood_successes;
if (change == 1) {
radio.config.cross = mesh::RadioCrossMode::On;
} else if ((change == 2 ? origin : origin ^ 1) == 0) {
auto primary = radio.config.primary; primary.freq += 1;
radio.config.setPrimary(primary, false);
} else {
auto secondary = radio.config.secondary; secondary.params.freq += 1;
radio.config.setSecondary(secondary, false);
}
tick();
node.receivePacket(&echo);
EXPECT_EQ(node.flood_successes, successes + (change == 2 ? 0 : 1));
EXPECT_EQ(node.flood_failures, 0u); // profile changes are not link failures
EXPECT_EQ(manager.getFreeCount(), 40);
}
}
TEST_F(DualProfileTest, ExpiredFinalFloodEchoCannotReserveKeyOnReplacementChannel) {
node.flood_attempts = 1;
radio.config.cross = mesh::RadioCrossMode::Off;
for (uint8_t origin : {0, 1}) {
SCOPED_TRACE(testing::Message() << "origin=" << unsigned(origin));
auto send = [&] {
auto* packet = node.obtainNewPacket();
EXPECT_NE(nullptr, packet);
if (!packet) return;
*packet = makeFloodPacket(PAYLOAD_TYPE_GRP_TXT);
packet->radio_profile = origin;
packet->tx_radio = origin ? mesh::RADIO_TX_SECONDARY : mesh::RADIO_TX_PRIMARY;
EXPECT_TRUE(node.sendFlood(packet));
};
send();
tick(); radio.complete = true; tick();
tick(10000); radio.complete = true; tick();
ASSERT_EQ(manager.getOutboundTotal(), 0);
ASSERT_EQ(manager.getFreeCount(), 40);
if (origin) {
const auto previous = radio.config.secondary;
radio.config.setSecondary({}, false);
tick();
radio.config.setSecondary(previous, false);
} else {
auto primary = radio.config.primary; primary.freq += 1;
radio.config.setPrimary(primary, false);
}
tick();
send(); // exact same logical packet, sent in a new radio session
tick(); ASSERT_TRUE(radio.sending);
radio.complete = true; tick();
EXPECT_EQ(manager.getOutboundTotal(), 1); // fresh channel owns its own retry
node.cancelAllFloodRetries();
tick(10000);
EXPECT_EQ(node.flood_failures, 0u);
EXPECT_EQ(manager.getFreeCount(), 40);
}
}
TEST_F(DualProfileTest, EchoOnOneProfileDoesNotCancelOtherProfileRetry) {
ASSERT_NE(nullptr, queue());
tick(); radio.complete = true; tick();