mirror of https://github.com/meshcore-dev/MeshCore
10 changed files with 68 additions and 133 deletions
@ -1,16 +0,0 @@ |
|||||
#pragma once |
|
||||
|
|
||||
#include <Mesh.h> |
|
||||
|
|
||||
namespace mesh { |
|
||||
|
|
||||
/**
|
|
||||
* An abstraction of the data tables needed to be maintained, for the routing engine. |
|
||||
*/ |
|
||||
class MeshTables { |
|
||||
public: |
|
||||
virtual bool hasForwarded(const uint8_t* packet_hash) const = 0; |
|
||||
virtual void setHasForwarded(const uint8_t* packet_hash) = 0; |
|
||||
}; |
|
||||
|
|
||||
} |
|
||||
@ -1,56 +1,48 @@ |
|||||
#pragma once |
#pragma once |
||||
|
|
||||
#include <MeshTables.h> |
#include <Mesh.h> |
||||
|
|
||||
#ifdef ESP32 |
#ifdef ESP32 |
||||
#include <FS.h> |
#include <FS.h> |
||||
#endif |
#endif |
||||
|
|
||||
#define MAX_PACKET_HASHES 64 |
#define MAX_PACKET_HASHES 128 |
||||
|
|
||||
class SimpleMeshTables : public mesh::MeshTables { |
class SimpleMeshTables : public mesh::MeshTables { |
||||
uint8_t _fwd_hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE]; |
uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE]; |
||||
int _next_fwd_idx; |
int _next_idx; |
||||
|
|
||||
int lookupHashIndex(const uint8_t* hash) const { |
|
||||
const uint8_t* sp = _fwd_hashes; |
|
||||
for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { |
|
||||
if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) return i; |
|
||||
} |
|
||||
return -1; |
|
||||
} |
|
||||
|
|
||||
public: |
public: |
||||
SimpleMeshTables() { |
SimpleMeshTables() { |
||||
memset(_fwd_hashes, 0, sizeof(_fwd_hashes)); |
memset(_hashes, 0, sizeof(_hashes)); |
||||
_next_fwd_idx = 0; |
_next_idx = 0; |
||||
} |
} |
||||
|
|
||||
#ifdef ESP32 |
#ifdef ESP32 |
||||
void restoreFrom(File f) { |
void restoreFrom(File f) { |
||||
f.read(_fwd_hashes, sizeof(_fwd_hashes)); |
f.read(_hashes, sizeof(_hashes)); |
||||
f.read((uint8_t *) &_next_fwd_idx, sizeof(_next_fwd_idx)); |
f.read((uint8_t *) &_next_idx, sizeof(_next_idx)); |
||||
} |
} |
||||
void saveTo(File f) { |
void saveTo(File f) { |
||||
f.write(_fwd_hashes, sizeof(_fwd_hashes)); |
f.write(_hashes, sizeof(_hashes)); |
||||
f.write((const uint8_t *) &_next_fwd_idx, sizeof(_next_fwd_idx)); |
f.write((const uint8_t *) &_next_idx, sizeof(_next_idx)); |
||||
} |
} |
||||
#endif |
#endif |
||||
|
|
||||
bool hasForwarded(const uint8_t* packet_hash) const override { |
bool hasSeen(const mesh::Packet* packet) override { |
||||
int i = lookupHashIndex(packet_hash); |
uint8_t hash[MAX_HASH_SIZE]; |
||||
return i >= 0; |
packet->calculatePacketHash(hash); |
||||
} |
|
||||
|
|
||||
void setHasForwarded(const uint8_t* packet_hash) override { |
|
||||
int i = lookupHashIndex(packet_hash); |
|
||||
if (i >= 0) { |
|
||||
// already in table
|
|
||||
} else { |
|
||||
memcpy(&_fwd_hashes[_next_fwd_idx*MAX_HASH_SIZE], packet_hash, MAX_HASH_SIZE); |
|
||||
|
|
||||
_next_fwd_idx = (_next_fwd_idx + 1) % MAX_PACKET_HASHES; // cyclic table
|
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; |
||||
} |
} |
||||
|
|
||||
|
memcpy(&_hashes[_next_idx*MAX_HASH_SIZE], hash, MAX_HASH_SIZE); |
||||
|
_next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; // cyclic table
|
||||
|
|
||||
|
return false; |
||||
} |
} |
||||
|
|
||||
|
|
||||
}; |
}; |
||||
|
|||||
@ -1,33 +0,0 @@ |
|||||
#pragma once |
|
||||
|
|
||||
#include <Packet.h> |
|
||||
#include <string.h> |
|
||||
|
|
||||
#define MAX_PACKET_HASHES 64 |
|
||||
|
|
||||
class SimpleSeenTable { |
|
||||
uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE]; |
|
||||
int _next_idx; |
|
||||
|
|
||||
public: |
|
||||
SimpleSeenTable() { |
|
||||
memset(_hashes, 0, sizeof(_hashes)); |
|
||||
_next_idx = 0; |
|
||||
} |
|
||||
|
|
||||
bool hasSeenPacket(const mesh::Packet* packet) { |
|
||||
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) return true; |
|
||||
} |
|
||||
|
|
||||
memcpy(&_hashes[_next_idx*MAX_HASH_SIZE], hash, MAX_HASH_SIZE); |
|
||||
_next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; // cyclic table
|
|
||||
|
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
}; |
|
||||
Loading…
Reference in new issue