firmware/src/mesh/PacketHistory.cpp

53 lines
1.6 KiB
C++
Raw Normal View History

#include "PacketHistory.h"
#include "configuration.h"
2020-05-22 03:31:22 +00:00
#include "mesh-pb-constants.h"
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)
{
if (p->id == 0) {
DEBUG_MSG("Ignoring message with zero id\n");
return false; // Not a floodable message ID, so we don't care
}
uint32_t now = millis();
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 {
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);
// Update the time on this record to now
2020-05-19 22:51:07 +00:00
if (withUpdate)
r.rxTimeMsec = now;
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;
r.sender = getFrom(p);
2020-05-19 22:51:07 +00:00
r.rxTimeMsec = now;
recentPackets.push_back(r);
printPacket("Adding packet record", p);
2020-05-19 22:51:07 +00:00
}
return false;
}