From ee3c4baea5e420f8798ad313861fb1ed5bb2f730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Br=C3=A1zio?= Date: Thu, 4 Sep 2025 23:50:13 +0100 Subject: [PATCH] Prevent packet loops and duplicates Implement a "seen packets" table to track packets that have already been processed by the serial bridge. This prevents packets from being re-transmitted over the serial link if they have already been seen, and it stops inbound packets from serial from being re-injected into the mesh if they are duplicates. Duplicate inbound packets are now freed to prevent memory leaks. --- src/helpers/SerialBridge.cpp | 16 +++++++++++----- src/helpers/SerialBridge.h | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/helpers/SerialBridge.cpp b/src/helpers/SerialBridge.cpp index 235661aa..bbd2e0dd 100644 --- a/src/helpers/SerialBridge.cpp +++ b/src/helpers/SerialBridge.cpp @@ -42,10 +42,12 @@ void SerialBridge::begin() { } void SerialBridge::onPacketTransmitted(mesh::Packet* packet) { - SerialPacket spkt; - spkt.len = packet->writeTo(spkt.payload); - spkt.crc = fletcher16(spkt.payload, spkt.len); - _serial->write((uint8_t *)&spkt, sizeof(SerialPacket)); + if (!_seen_packets.hasSeen(packet)) { + SerialPacket spkt; + spkt.len = packet->writeTo(spkt.payload); + spkt.crc = fletcher16(spkt.payload, spkt.len); + _serial->write((uint8_t *)&spkt, sizeof(SerialPacket)); + } } void SerialBridge::loop() { @@ -92,7 +94,11 @@ void SerialBridge::onPacketReceived() { } new_pkt->readFrom(bytes, len); - _mgr->queueInbound(new_pkt, 0); + if (!_seen_packets.hasSeen(new_pkt)) { + _mgr->queueInbound(new_pkt, 0); + } else { + _mgr->free(new_pkt); + } } #endif diff --git a/src/helpers/SerialBridge.h b/src/helpers/SerialBridge.h index 3a4f0776..fe3c176f 100644 --- a/src/helpers/SerialBridge.h +++ b/src/helpers/SerialBridge.h @@ -1,6 +1,7 @@ #pragma once #include "helpers/AbstractBridge.h" +#include "helpers/SimpleMeshTables.h" #include #ifdef BRIDGE_OVER_SERIAL @@ -25,6 +26,7 @@ public: private: Stream* _serial; mesh::PacketManager* _mgr; + SimpleMeshTables _seen_packets; }; #endif