Files
MeshCore-Solo/src/helpers/SimpleMeshTables.h
T

49 lines
1.0 KiB
C++
Raw Normal View History

2025-01-13 14:07:48 +11:00
#pragma once
2025-01-14 06:43:03 +11:00
#include <Mesh.h>
2025-01-13 14:07:48 +11:00
#ifdef ESP32
#include <FS.h>
#endif
2025-01-14 06:43:03 +11:00
#define MAX_PACKET_HASHES 128
2025-01-13 14:07:48 +11:00
class SimpleMeshTables : public mesh::MeshTables {
2025-01-14 06:43:03 +11:00
uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE];
int _next_idx;
2025-01-13 14:07:48 +11:00
public:
SimpleMeshTables() {
2025-01-14 06:43:03 +11:00
memset(_hashes, 0, sizeof(_hashes));
_next_idx = 0;
2025-01-13 14:07:48 +11:00
}
#ifdef ESP32
void restoreFrom(File f) {
2025-01-14 06:43:03 +11:00
f.read(_hashes, sizeof(_hashes));
f.read((uint8_t *) &_next_idx, sizeof(_next_idx));
2025-01-13 14:07:48 +11:00
}
void saveTo(File f) {
2025-01-14 06:43:03 +11:00
f.write(_hashes, sizeof(_hashes));
f.write((const uint8_t *) &_next_idx, sizeof(_next_idx));
2025-01-13 14:07:48 +11:00
}
#endif
2025-01-14 06:43:03 +11:00
bool hasSeen(const mesh::Packet* packet) override {
uint8_t hash[MAX_HASH_SIZE];
packet->calculatePacketHash(hash);
2025-01-13 14:07:48 +11:00
2025-01-14 06:43:03 +11:00
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 true;
2025-01-13 14:07:48 +11:00
}
2025-01-14 06:43:03 +11:00
memcpy(&_hashes[_next_idx*MAX_HASH_SIZE], hash, MAX_HASH_SIZE);
_next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; // cyclic table
return false;
2025-01-13 14:07:48 +11:00
}
2025-01-14 06:43:03 +11:00
2025-01-13 14:07:48 +11:00
};