From b9a8683a4bfe9c453313554f8de1e85acb789383 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Mon, 26 Aug 2024 15:48:47 -0500 Subject: [PATCH] Mask out random bits when doing queue ordering (#4561) * Mask out random bits when doing queue ordering * Parenthesis --- src/mesh/MeshPacketQueue.cpp | 7 ++++--- src/mesh/MeshTypes.h | 5 +++-- src/mesh/Router.cpp | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mesh/MeshPacketQueue.cpp b/src/mesh/MeshPacketQueue.cpp index 24756fd2a..f1c6c4ff3 100644 --- a/src/mesh/MeshPacketQueue.cpp +++ b/src/mesh/MeshPacketQueue.cpp @@ -20,8 +20,9 @@ bool CompareMeshPacketFunc(const meshtastic_MeshPacket *p1, const meshtastic_Mes // If priorities differ, use that // for equal priorities, order by id (older packets have higher priority - this will briefly be wrong when IDs roll over but // no big deal) - return (p1p != p2p) ? (p1p < p2p) // prefer bigger priorities - : (p1->id >= p2->id); // prefer smaller packet ids + return (p1p != p2p) + ? (p1p < p2p) // prefer bigger priorities + : ((p1->id & ID_COUNTER_MASK) >= (p2->id & ID_COUNTER_MASK)); // Mask to counter portion, prefer smaller packet ids } MeshPacketQueue::MeshPacketQueue(size_t _maxLen) : maxLen(_maxLen) {} @@ -127,4 +128,4 @@ bool MeshPacketQueue::replaceLowerPriorityPacket(meshtastic_MeshPacket *p) std::make_heap(queue.begin(), queue.end(), &CompareMeshPacketFunc); return true; -} +} \ No newline at end of file diff --git a/src/mesh/MeshTypes.h b/src/mesh/MeshTypes.h index c0919bf5d..1c9099c39 100644 --- a/src/mesh/MeshTypes.h +++ b/src/mesh/MeshTypes.h @@ -14,8 +14,9 @@ typedef uint32_t PacketId; // A packet sequence number 1 // Reserved to only deliver packets over high speed (non-lora) transports, such as MQTT or BLE mesh (not yet implemented) #define ERRNO_OK 0 #define ERRNO_NO_INTERFACES 33 -#define ERRNO_UNKNOWN 32 // pick something that doesn't conflict with RH_ROUTER_ERROR_UNABLE_TO_DELIVER -#define ERRNO_DISABLED 34 // the interface is disabled +#define ERRNO_UNKNOWN 32 // pick something that doesn't conflict with RH_ROUTER_ERROR_UNABLE_TO_DELIVER +#define ERRNO_DISABLED 34 // the interface is disabled +#define ID_COUNTER_MASK (UINT32_MAX >> 22) // mask to select the counter portion of the ID /* * Source of a received message diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index bdd8c4e6c..61b1bbfb6 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -109,7 +109,7 @@ PacketId generatePacketId() rollingPacketId++; - rollingPacketId &= UINT32_MAX >> 22; // Mask out the top 22 bits + rollingPacketId &= ID_COUNTER_MASK; // Mask out the top 22 bits PacketId id = rollingPacketId | random(UINT32_MAX & 0x7fffffff) << 10; // top 22 bits LOG_DEBUG("Partially randomized packet id %u\n", id); return id;