diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 2ddf6b987..12c4382d5 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1230,6 +1230,13 @@ bool MyMesh::hasFloodRetryPrefixes() const { return false; } +static bool configuredFloodRetryPrefixMatches(const uint8_t* configured, + const uint8_t* observed, + uint8_t observed_len) { + return (configured[0] != 0 || configured[1] != 0 || configured[2] != 0) + && routeHashPrefixesOverlap(configured, FLOOD_RETRY_PREFIX_LEN, observed, observed_len); +} + bool MyMesh::floodRetryLastHopMatches(const mesh::Packet* packet) const { if (packet == NULL || packet->getPathHashCount() == 0) { return false; @@ -1243,8 +1250,7 @@ bool MyMesh::floodRetryLastHopMatches(const mesh::Packet* packet) const { const uint8_t* heard_prefix = &packet->path[(packet->getPathHashCount() - 1) * hash_size]; for (int i = 0; i < FLOOD_RETRY_PREFIX_SLOTS; i++) { const uint8_t* configured = _prefs.flood_retry_prefixes[i]; - if ((configured[0] != 0 || configured[1] != 0 || configured[2] != 0) - && memcmp(configured, heard_prefix, hash_size) == 0) { + if (configuredFloodRetryPrefixMatches(configured, heard_prefix, hash_size)) { return true; } } @@ -1266,8 +1272,7 @@ bool MyMesh::floodRetryPrefixMatches(const mesh::Packet* packet) const { for (int hop = 0; hop < packet->getPathHashCount(); hop++) { for (int i = 0; i < FLOOD_RETRY_PREFIX_SLOTS; i++) { const uint8_t* configured = _prefs.flood_retry_prefixes[i]; - if ((configured[0] != 0 || configured[1] != 0 || configured[2] != 0) - && memcmp(configured, path, hash_size) == 0) { + if (configuredFloodRetryPrefixMatches(configured, path, hash_size)) { return true; } } @@ -1283,8 +1288,7 @@ bool MyMesh::floodRetryPrefixIgnored(const uint8_t* prefix, uint8_t prefix_len) } for (int i = 0; i < FLOOD_RETRY_IGNORE_PREFIXES; i++) { const uint8_t* ignored = _prefs.flood_retry_ignore_prefixes[i]; - if ((ignored[0] != 0 || ignored[1] != 0 || ignored[2] != 0) - && memcmp(ignored, prefix, prefix_len) == 0) { + if (configuredFloodRetryPrefixMatches(ignored, prefix, prefix_len)) { return true; } } @@ -1352,8 +1356,7 @@ int MyMesh::floodRetryBucketForPrefix(const uint8_t* prefix, uint8_t prefix_len, for (int bucket = 0; bucket < FLOOD_RETRY_BRIDGE_BUCKETS; bucket++) { for (int i = 0; i < FLOOD_RETRY_BUCKET_PREFIXES; i++) { const uint8_t* configured = _prefs.flood_retry_bridge_buckets[bucket][i]; - if ((configured[0] != 0 || configured[1] != 0 || configured[2] != 0) - && memcmp(configured, prefix, prefix_len) == 0) { + if (configuredFloodRetryPrefixMatches(configured, prefix, prefix_len)) { return bucket; } } @@ -1734,9 +1737,6 @@ void MyMesh::onFloodRetryEvent(const char* event, const mesh::Packet* packet, ui } bool MyMesh::hasFloodRetryTargetPrefix(const mesh::Packet* packet) const { - if (_prefs.flood_retry_bridge_enabled) { - return false; - } return floodRetryPrefixMatches(packet); } @@ -1776,14 +1776,6 @@ bool MyMesh::isFloodRetryEchoTarget(const mesh::Packet* packet, uint8_t progress if (packet == NULL || !packet->isRouteFlood()) { return false; } - if (_prefs.flood_retry_bridge_enabled) { - FloodRetryBridgeState* state = floodRetryBridgeStateFor(packet, false); - if (state == NULL) { - return false; - } - state->heard_mask |= floodRetryBridgeHeardMask(packet, state->source_bucket, state->progress_marker) & state->target_mask; - return (state->heard_mask & state->target_mask) == state->target_mask; - } if (packet->getPathHashCount() == 0) { return false; } @@ -1795,10 +1787,21 @@ bool MyMesh::isFloodRetryEchoTarget(const mesh::Packet* packet, uint8_t progress if (floodRetryPrefixIgnored(heard_prefix, hash_size)) { return false; } - if (hasFloodRetryPrefixes()) { - return floodRetryLastHopMatches(packet); + // A configured target prefix is terminal in both normal and bridge modes. + // Bridge bucket completion remains the fallback when no target is configured + // or the newly heard hop is not one of the configured targets. + if (hasFloodRetryPrefixes() && floodRetryLastHopMatches(packet)) { + return true; } - return true; + if (_prefs.flood_retry_bridge_enabled) { + FloodRetryBridgeState* state = floodRetryBridgeStateFor(packet, false); + if (state == NULL) { + return false; + } + state->heard_mask |= floodRetryBridgeHeardMask(packet, state->source_bucket, state->progress_marker) & state->target_mask; + return (state->heard_mask & state->target_mask) == state->target_mask; + } + return !hasFloodRetryPrefixes(); } static void formatLocalSnrX4(char* dest, size_t dest_len, int16_t snr_x4) { diff --git a/src/helpers/SimpleMeshTables.h b/src/helpers/SimpleMeshTables.h index ad47f5c71..7c403fa1f 100644 --- a/src/helpers/SimpleMeshTables.h +++ b/src/helpers/SimpleMeshTables.h @@ -29,6 +29,16 @@ #define RECENT_REPEATER_STORAGE_SLOTS (MAX_RECENT_REPEATERS > 0 ? MAX_RECENT_REPEATERS : 1) #define MAX_ROUTE_HASH_BYTES 3 +inline bool routeHashPrefixesOverlap(const uint8_t* a, uint8_t a_len, + const uint8_t* b, uint8_t b_len) { + if (a == NULL || b == NULL || a_len == 0 || b_len == 0 + || a_len > MAX_ROUTE_HASH_BYTES || b_len > MAX_ROUTE_HASH_BYTES) { + return false; + } + uint8_t compare_len = a_len < b_len ? a_len : b_len; + return memcmp(a, b, compare_len) == 0; +} + class SimpleMeshTables : public mesh::MeshTables { public: struct RecentRepeaterInfo { @@ -60,11 +70,6 @@ private: _next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; } - bool prefixesOverlap(const uint8_t* a, uint8_t a_len, const uint8_t* b, uint8_t b_len) const { - uint8_t n = a_len < b_len ? a_len : b_len; - return n > 0 && memcmp(a, b, n) == 0; - } - int8_t weightedSnrX4RoundUp(int8_t curr_snr_x4, int8_t new_snr_x4) const { // Keep existing SNR heavier than a single new sample: 75% existing + 25% new. int32_t weighted_sum = ((int32_t)curr_snr_x4 * 3) + (int32_t)new_snr_x4; @@ -293,16 +298,11 @@ public: prefix_len = MAX_ROUTE_HASH_BYTES; } - for (int i = 0; i < MAX_RECENT_REPEATERS; i++) { - RecentRepeaterInfo& existing = _recent_repeaters[i]; - if (existing.prefix_len != prefix_len || memcmp(existing.prefix, prefix, prefix_len) != 0) { - continue; - } - int16_t lowered = (int16_t)existing.snr_x4 - (int16_t)amount_x4; - if (lowered < -128) { - lowered = -128; - } - existing.snr_x4 = (int8_t)lowered; + RecentRepeaterInfo* existing = const_cast(findRecentRepeaterByHash(prefix, prefix_len)); + if (existing != NULL) { + int16_t lowered = (int16_t)existing->snr_x4 - (int16_t)amount_x4; + if (lowered < -128) lowered = -128; + existing->snr_x4 = (int8_t)lowered; return true; } return false; @@ -373,7 +373,7 @@ public: if (info->prefix_len == hash_len && memcmp(info->prefix, hash, hash_len) == 0) { return info; } - if (prefixesOverlap(info->prefix, info->prefix_len, hash, hash_len)) { + if (routeHashPrefixesOverlap(info->prefix, info->prefix_len, hash, hash_len)) { if (best == NULL || info->prefix_len > best->prefix_len || (info->prefix_len == best->prefix_len && info->snr_x4 > best->snr_x4)) { best = info; diff --git a/test/test_mesh_tables/test_simple_mesh_tables.cpp b/test/test_mesh_tables/test_simple_mesh_tables.cpp index 46b477d94..a3bfcdd7c 100644 --- a/test/test_mesh_tables/test_simple_mesh_tables.cpp +++ b/test/test_mesh_tables/test_simple_mesh_tables.cpp @@ -1,4 +1,6 @@ #include +#define MESH_ENABLE_RECENT_REPEATERS 1 +#define MAX_RECENT_REPEATERS 8 #include "helpers/SimpleMeshTables.h" using namespace mesh; @@ -97,6 +99,33 @@ TEST(SimpleMeshTables, Clear_RemovesSeenPacket) { EXPECT_FALSE(t.wasSeen(&p)); } +TEST(RouteHashPrefixes, MatchesSharedOneTwoOrThreeBytes) { + const uint8_t configured[] = {0x86, 0x0c, 0xca}; + const uint8_t one_byte[] = {0x86}; + const uint8_t two_bytes[] = {0x86, 0x0c}; + const uint8_t three_bytes[] = {0x86, 0x0c, 0xca}; + const uint8_t mismatch[] = {0x86, 0x0d}; + + EXPECT_TRUE(routeHashPrefixesOverlap(configured, 3, one_byte, 1)); + EXPECT_TRUE(routeHashPrefixesOverlap(configured, 3, two_bytes, 2)); + EXPECT_TRUE(routeHashPrefixesOverlap(configured, 3, three_bytes, 3)); + EXPECT_TRUE(routeHashPrefixesOverlap(one_byte, 1, configured, 3)); + EXPECT_FALSE(routeHashPrefixesOverlap(configured, 3, mismatch, 2)); +} + +TEST(SimpleMeshTables, ShortFailurePrefixUpdatesOverlappingLongEntry) { + SimpleMeshTables t; + const uint8_t configured[] = {0x86, 0x0c, 0xca}; + const uint8_t observed[] = {0x86}; + + ASSERT_TRUE(t.setRecentRepeater(configured, 3, 12)); + ASSERT_TRUE(t.decrementRecentRepeaterSnrX4(observed, 1, 1)); + const auto* info = t.findRecentRepeaterByHash(configured, 3); + ASSERT_NE(nullptr, info); + EXPECT_EQ(11, info->snr_x4); + EXPECT_EQ(1, t.getRecentRepeaterCount()); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();