2020-04-17 18:52:20 +00:00
|
|
|
#include "FloodingRouter.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "mesh-pb-constants.h"
|
|
|
|
|
|
|
|
/// We clear our old flood record five minute after we see the last of it
|
|
|
|
#define FLOOD_EXPIRE_TIME (5 * 60 * 1000L)
|
|
|
|
|
2020-04-25 18:43:28 +00:00
|
|
|
FloodingRouter::FloodingRouter() : toResend(MAX_NUM_NODES)
|
2020-04-17 18:52:20 +00:00
|
|
|
{
|
|
|
|
recentBroadcasts.reserve(MAX_NUM_NODES); // Prealloc the worst case # of records - to prevent heap fragmentation
|
2020-04-25 18:43:28 +00:00
|
|
|
// setup our periodic task
|
2020-04-17 18:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet on a suitable interface. This routine will
|
|
|
|
* later free() the packet to pool. This routine is not allowed to stall.
|
|
|
|
* If the txmit queue is full it might return an error
|
|
|
|
*/
|
|
|
|
ErrorCode FloodingRouter::send(MeshPacket *p)
|
|
|
|
{
|
|
|
|
// We update our table of recent broadcasts, even for messages we send
|
|
|
|
wasSeenRecently(p);
|
|
|
|
|
|
|
|
return Router::send(p);
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:43:28 +00:00
|
|
|
// Return a delay in msec before sending the next packet
|
|
|
|
uint32_t getRandomDelay()
|
|
|
|
{
|
|
|
|
return random(200, 10 * 1000L); // between 200ms and 10s
|
|
|
|
}
|
|
|
|
|
2020-04-17 18:52:20 +00:00
|
|
|
/**
|
|
|
|
* Called from loop()
|
|
|
|
* Handle any packet that is received by an interface on this node.
|
|
|
|
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
|
|
|
|
*
|
|
|
|
* Note: this method will free the provided packet
|
|
|
|
*/
|
|
|
|
void FloodingRouter::handleReceived(MeshPacket *p)
|
|
|
|
{
|
|
|
|
if (wasSeenRecently(p)) {
|
|
|
|
DEBUG_MSG("Ignoring incoming floodmsg, because we've already seen it\n");
|
|
|
|
packetPool.release(p);
|
|
|
|
} else {
|
2020-04-17 19:41:01 +00:00
|
|
|
if (p->to == NODENUM_BROADCAST) {
|
|
|
|
if (p->id != 0) {
|
2020-04-25 18:43:28 +00:00
|
|
|
uint32_t delay = getRandomDelay();
|
|
|
|
|
|
|
|
DEBUG_MSG("Rebroadcasting received floodmsg to neighbors in %u msec, fr=0x%x,to=0x%x,id=%d\n", delay, p->from,
|
|
|
|
p->to, p->id);
|
2020-04-17 18:52:20 +00:00
|
|
|
|
2020-04-17 19:41:01 +00:00
|
|
|
MeshPacket *tosend = packetPool.allocCopy(*p);
|
2020-04-25 18:43:28 +00:00
|
|
|
toResend.enqueue(tosend);
|
|
|
|
setPeriod(delay); // This will work even if we were already waiting a random delay
|
2020-04-17 19:41:01 +00:00
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Ignoring a simple (0 hop) broadcast\n");
|
|
|
|
}
|
2020-04-17 18:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle the packet as normal
|
|
|
|
Router::handleReceived(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:43:28 +00:00
|
|
|
void FloodingRouter::doTask()
|
|
|
|
{
|
|
|
|
MeshPacket *p = toResend.dequeuePtr(0);
|
|
|
|
|
|
|
|
DEBUG_MSG("Sending delayed message!\n");
|
|
|
|
if (p) {
|
|
|
|
// Note: we are careful to resend using the original senders node id
|
|
|
|
// We are careful not to call our hooked version of send() - because we don't want to check this again
|
|
|
|
Router::send(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toResend.isEmpty())
|
|
|
|
disable(); // no more work right now
|
|
|
|
else {
|
|
|
|
setPeriod(getRandomDelay());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 18:52:20 +00:00
|
|
|
/**
|
|
|
|
* Update recentBroadcasts and return true if we have already seen this packet
|
|
|
|
*/
|
|
|
|
bool FloodingRouter::wasSeenRecently(const MeshPacket *p)
|
|
|
|
{
|
|
|
|
if (p->to != NODENUM_BROADCAST)
|
|
|
|
return false; // Not a broadcast, so we don't care
|
|
|
|
|
2020-04-17 19:41:01 +00:00
|
|
|
if (p->id == 0) {
|
|
|
|
DEBUG_MSG("Ignoring message with zero id\n");
|
2020-04-17 18:52:20 +00:00
|
|
|
return false; // Not a floodable message ID, so we don't care
|
2020-04-17 19:41:01 +00:00
|
|
|
}
|
2020-04-17 18:52:20 +00:00
|
|
|
|
|
|
|
uint32_t now = millis();
|
|
|
|
for (int i = 0; i < recentBroadcasts.size();) {
|
|
|
|
BroadcastRecord &r = recentBroadcasts[i];
|
|
|
|
|
|
|
|
if ((now - r.rxTimeMsec) >= FLOOD_EXPIRE_TIME) {
|
2020-04-17 19:41:01 +00:00
|
|
|
DEBUG_MSG("Deleting old broadcast record %d\n", i);
|
2020-04-17 18:52:20 +00:00
|
|
|
recentBroadcasts.erase(recentBroadcasts.begin() + i); // delete old record
|
|
|
|
} else {
|
|
|
|
if (r.id == p->id && r.sender == p->from) {
|
2020-04-17 19:41:01 +00:00
|
|
|
DEBUG_MSG("Found existing broadcast record for fr=0x%x,to=0x%x,id=%d\n", p->from, p->to, p->id);
|
|
|
|
|
2020-04-17 18:52:20 +00:00
|
|
|
// Update the time on this record to now
|
|
|
|
r.rxTimeMsec = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find an existing record, make one
|
|
|
|
BroadcastRecord r;
|
|
|
|
r.id = p->id;
|
|
|
|
r.sender = p->from;
|
|
|
|
r.rxTimeMsec = now;
|
|
|
|
recentBroadcasts.push_back(r);
|
2020-04-17 19:41:01 +00:00
|
|
|
DEBUG_MSG("Adding broadcast record for fr=0x%x,to=0x%x,id=%d\n", p->from, p->to, p->id);
|
2020-04-17 18:52:20 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|