diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 3e573d5c..e140e29a 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -160,9 +160,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 af05bf1a..c5ff642c 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; diff --git a/src/mesh/ReliableRouter.cpp b/src/mesh/ReliableRouter.cpp index 09639468..7f91be49 100644 --- a/src/mesh/ReliableRouter.cpp +++ b/src/mesh/ReliableRouter.cpp @@ -150,7 +150,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;