From 348e78718d0810c0a736d3340c5ee359911b33c4 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 7 May 2022 15:39:14 +0200 Subject: [PATCH 1/2] Call cancelSending in stopRetransmission This also removes pending packet from txQueue if it was already in there --- src/mesh/ReliableRouter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/ReliableRouter.cpp b/src/mesh/ReliableRouter.cpp index ee513bae0..899004d12 100644 --- a/src/mesh/ReliableRouter.cpp +++ b/src/mesh/ReliableRouter.cpp @@ -151,7 +151,7 @@ bool ReliableRouter::stopRetransmission(GlobalPacketId key) if (old) { auto numErased = pending.erase(key); assert(numErased == 1); - packetPool.release(old->packet); + cancelSending(getFrom(old->packet), old->packet->id); return true; } else return false; From a7f4263db41e19c6c8875cd338b0e3a39e2ed074 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 7 May 2022 15:43:35 +0200 Subject: [PATCH 2/2] Optimize retransmission timer Based on airtime of packet + transmit, processing and CAD delays --- src/mesh/RadioInterface.cpp | 12 +++++++++--- src/mesh/RadioInterface.h | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 03989bc41..95f4d775b 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -159,9 +159,15 @@ uint32_t RadioInterface::getPacketTime(MeshPacket *p) uint32_t RadioInterface::getRetransmissionMsec(const MeshPacket *p) { assert(shortPacketMsec); // Better be non zero - - // was 20 and 22 secs respectively, but now with shortPacketMsec as 2269, this should give the same range - return random(9 * shortPacketMsec, 10 * shortPacketMsec); + static uint8_t bytes[MAX_RHPACKETLEN]; + size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), Data_fields, &p->decoded); + uint32_t packetAirtime = getPacketTime(numbytes + sizeof(PacketHeader)); + uint32_t tCADmsec = 2 * (1 << sf) / bw; // duration of CAD is roughly 2 symbols according to SX127x datasheet + /* Make sure enough time has elapsed for this packet to be sent and an ACK is received. + * Right now we have to wait until another node floods the same packet, as that is our implicit ACK. + * TODO: Revise when want_ack will be used (right now it is always set to 0 afterwards). + */ + return 2*packetAirtime + 2*MIN_TX_WAIT_MSEC + shortPacketMsec + shortPacketMsec*2 + PROCESSING_TIME_MSEC + 2*tCADmsec; } /** The delay to use when we want to send something but the ether is busy */ diff --git a/src/mesh/RadioInterface.h b/src/mesh/RadioInterface.h index af05bf1a0..c5ff642c9 100644 --- a/src/mesh/RadioInterface.h +++ b/src/mesh/RadioInterface.h @@ -63,6 +63,8 @@ class RadioInterface uint8_t cr = 7; uint16_t preambleLength = 32; // 8 is default, but we use longer to increase the amount of sleep time when receiving + const uint32_t MIN_TX_WAIT_MSEC = 100; // minimum time to wait before transmitting after sensing the channel in ms + const uint32_t PROCESSING_TIME_MSEC = 4500; // time to construct, process and construct a packet again (empirically determined) MeshPacket *sendingPacket = NULL; // The packet we are currently sending uint32_t lastTxStart = 0L;