2020-05-11 23:14:53 +00:00
|
|
|
#include "PacketHistory.h"
|
|
|
|
#include "configuration.h"
|
2020-05-22 03:31:22 +00:00
|
|
|
#include "mesh-pb-constants.h"
|
2020-05-11 23:14:53 +00:00
|
|
|
|
|
|
|
PacketHistory::PacketHistory()
|
|
|
|
{
|
|
|
|
recentPackets.reserve(MAX_NUM_NODES); // Prealloc the worst case # of records - to prevent heap fragmentation
|
|
|
|
// setup our periodic task
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update recentBroadcasts and return true if we have already seen this packet
|
|
|
|
*/
|
2020-05-19 22:51:07 +00:00
|
|
|
bool PacketHistory::wasSeenRecently(const MeshPacket *p, bool withUpdate)
|
2020-05-11 23:14:53 +00:00
|
|
|
{
|
|
|
|
if (p->id == 0) {
|
|
|
|
DEBUG_MSG("Ignoring message with zero id\n");
|
|
|
|
return false; // Not a floodable message ID, so we don't care
|
|
|
|
}
|
|
|
|
|
2020-09-05 19:34:48 +00:00
|
|
|
uint32_t now = millis();
|
2020-05-11 23:14:53 +00:00
|
|
|
for (size_t i = 0; i < recentPackets.size();) {
|
|
|
|
PacketRecord &r = recentPackets[i];
|
|
|
|
|
|
|
|
if ((now - r.rxTimeMsec) >= FLOOD_EXPIRE_TIME) {
|
|
|
|
// DEBUG_MSG("Deleting old broadcast record %d\n", i);
|
|
|
|
recentPackets.erase(recentPackets.begin() + i); // delete old record
|
|
|
|
} else {
|
2021-03-05 02:19:27 +00:00
|
|
|
if (r.id == p->id && r.sender == getFrom(p)) {
|
2021-05-03 02:53:06 +00:00
|
|
|
DEBUG_MSG("Found existing packet record for fr=0x%x,to=0x%x,id=0x%x\n", p->from, p->to, p->id);
|
2020-05-11 23:14:53 +00:00
|
|
|
|
|
|
|
// Update the time on this record to now
|
2020-05-19 22:51:07 +00:00
|
|
|
if (withUpdate)
|
|
|
|
r.rxTimeMsec = now;
|
2020-05-11 23:14:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find an existing record, make one
|
2020-05-19 22:51:07 +00:00
|
|
|
if (withUpdate) {
|
|
|
|
PacketRecord r;
|
|
|
|
r.id = p->id;
|
2021-03-05 02:19:27 +00:00
|
|
|
r.sender = getFrom(p);
|
2020-05-19 22:51:07 +00:00
|
|
|
r.rxTimeMsec = now;
|
|
|
|
recentPackets.push_back(r);
|
2020-06-14 22:30:42 +00:00
|
|
|
printPacket("Adding packet record", p);
|
2020-05-19 22:51:07 +00:00
|
|
|
}
|
2020-05-11 23:14:53 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|