#pragma once #include #ifdef ESP32 #include #endif #define MAX_PACKET_HASHES (128+32) class SimpleMeshTables : public mesh::MeshTables { uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE]; int _next_idx; uint32_t _direct_dups, _flood_dups; public: SimpleMeshTables() { memset(_hashes, 0, sizeof(_hashes)); _next_idx = 0; _direct_dups = _flood_dups = 0; } #ifdef ESP32 void restoreFrom(File f) { f.read(_hashes, sizeof(_hashes)); f.read((uint8_t *) &_next_idx, sizeof(_next_idx)); } void saveTo(File f) { f.write(_hashes, sizeof(_hashes)); f.write((const uint8_t *) &_next_idx, sizeof(_next_idx)); } #endif bool wasSeen(const mesh::Packet* packet) override { uint8_t hash[MAX_HASH_SIZE]; packet->calculatePacketHash(hash); const uint8_t* sp = _hashes; for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) { if (packet->isRouteDirect()) { _direct_dups++; } else { _flood_dups++; } return true; } } return false; } void markSeen(const mesh::Packet* packet) override { uint8_t hash[MAX_HASH_SIZE]; packet->calculatePacketHash(hash); // CUSTOM (TeTeHacko): idempotent, the way the single hasSeen() this replaced // was -- that one inserted only when the hash was absent. 1.17 split it into // wasSeen() + markSeen(), and while every receive-side caller still gates on // !wasSeen(), the SEND paths (Mesh::sendFlood, sendDirect, sendZeroHop) call // this one unguarded. Every re-send of the same packet then occupies another // slot of a fixed 160-entry ring, which shortens the dedup window and lets a // late echo of our own flood read as new traffic -- and on a repeater that // means rebroadcasting it. // // Cost is one extra 160x8 byte scan per send, which is nothing against LoRa // airtime. Fix taken from ALLFATHER-BV/meshcomod e62be9b (MIT). const uint8_t* sp = _hashes; for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) return; // already recorded } memcpy(&_hashes[_next_idx * MAX_HASH_SIZE], hash, MAX_HASH_SIZE); _next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; } void clear(const mesh::Packet* packet) override { uint8_t hash[MAX_HASH_SIZE]; packet->calculatePacketHash(hash); uint8_t* sp = _hashes; for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) { memset(sp, 0, MAX_HASH_SIZE); break; } } } uint32_t getNumDirectDups() const { return _direct_dups; } uint32_t getNumFloodDups() const { return _flood_dups; } void resetStats() { _direct_dups = _flood_dups = 0; } };