From f3d5df5cfe4d4542c580c10039d62355da1507da Mon Sep 17 00:00:00 2001 From: Wessel Nieboer Date: Wed, 4 Mar 2026 23:05:40 +0100 Subject: [PATCH 1/5] Add SNR-driven dynamic coding rate selection for repeater retransmits When a repeater forwards a direct-routed pkt, it looks up the next-hop neighbor's SNR, computes the link margin against the SF-specific demodulation floor, then determines coding rate (RT) based on SNR. Weaker links get higher CR. We apply CR on a per-packet basis and restore to default configuration after TX. Margin thresholds: <3 dB - CR 4/8 3-6 dB - CR 4/7 6-10 dB - CR 4/6 >=10 dB - CR 4/5 Flood repeats adhere to configured CR, as do forwards to unknown neighbors. --- examples/simple_repeater/MyMesh.cpp | 38 ++++++++++++++++++++ examples/simple_repeater/MyMesh.h | 2 ++ src/Dispatcher.cpp | 16 ++++++++- src/Dispatcher.h | 7 ++++ src/Mesh.cpp | 3 +- src/Mesh.h | 8 +++++ src/Packet.cpp | 2 ++ src/Packet.h | 1 + src/helpers/radiolib/CustomLLCC68Wrapper.h | 2 ++ src/helpers/radiolib/CustomLR1110Wrapper.h | 2 ++ src/helpers/radiolib/CustomSTM32WLxWrapper.h | 2 ++ src/helpers/radiolib/CustomSX1262Wrapper.h | 2 ++ src/helpers/radiolib/CustomSX1268Wrapper.h | 2 ++ src/helpers/radiolib/CustomSX1276Wrapper.h | 2 ++ 14 files changed, 87 insertions(+), 2 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index b50d19d013d..24ff5d9b652 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1,5 +1,6 @@ #include "MyMesh.h" #include +#include /* ------------------------------ Config -------------------------------- */ @@ -87,6 +88,42 @@ void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float sn #endif } +int8_t MyMesh::findNeighbourSNR(const uint8_t* hash, uint8_t hash_size) { +#if MAX_NEIGHBOURS + for (int i = 0; i < MAX_NEIGHBOURS; i++) { + if (neighbours[i].heard_timestamp != 0 && neighbours[i].id.isHashMatch(hash, hash_size)) { + return neighbours[i].snr; + } + } +#endif + return INT8_MAX; +} + +// Approximate SNR demod floor per SF (same as RadioLibWrappers.cpp) +static float cr_snr_thresholds[] = { + -7.5f, // SF7 + -10.0f, // SF8 + -12.5f, // SF9 + -15.0f, // SF10 + -17.5f, // SF11 + -20.0f // SF12 +}; + +uint8_t MyMesh::selectCodingRateForPeer(const uint8_t* hash, uint8_t hash_size) { + int8_t snr4 = findNeighbourSNR(hash, hash_size); + if (snr4 == INT8_MAX) return 0; // unknown neighbor, use default + + float snr = snr4 / 4.0f; + float threshold = (_prefs.sf >= 7 && _prefs.sf <= 12) + ? cr_snr_thresholds[_prefs.sf - 7] : -15.0f; + float margin = snr - threshold; + + if (margin < 3.0f) return 8; + if (margin < 6.0f) return 7; + if (margin < 10.0f) return 6; + return 5; // good margin, use lightest CR +} + uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood) { ClientInfo* client = NULL; if (data[0] == 0) { // blank password, just check if sender is in ACL @@ -979,6 +1016,7 @@ void MyMesh::begin(FILESYSTEM *fs) { radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); radio_driver.setTxPower(_prefs.tx_power_dbm); + setDefaultCR(_prefs.cr); radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain); MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s", diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index dc89325861c..bcbc166f711 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -117,6 +117,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #endif void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr); + int8_t findNeighbourSNR(const uint8_t* hash, uint8_t hash_size); uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood); uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data); uint8_t handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data); @@ -156,6 +157,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { uint8_t getExtraAckTransmitCount() const override { return _prefs.multi_acks; } + uint8_t selectCodingRateForPeer(const uint8_t* hash, uint8_t hash_size) override; #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index c0610b7f8a7..55caa98346d 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -106,6 +106,9 @@ void Dispatcher::loop() { } _radio->onSendFinished(); + if (outbound->_tx_cr != 0 && outbound->_tx_cr != _default_cr) { + _radio->setCodingRate(_default_cr); + } logTx(outbound, 2 + outbound->getPathByteLen() + outbound->payload_len); if (outbound->isRouteFlood()) { n_sent_flood++; @@ -118,6 +121,9 @@ void Dispatcher::loop() { MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): WARNING: outbound packed send timed out!", getLogDateTime()); _radio->onSendFinished(); + if (outbound->_tx_cr != 0 && outbound->_tx_cr != _default_cr) { + _radio->setCodingRate(_default_cr); + } logTxFail(outbound, 2 + outbound->getPathByteLen() + outbound->payload_len); releasePacket(outbound); // return to pool @@ -324,14 +330,21 @@ void Dispatcher::checkSend() { } else { memcpy(&raw[len], outbound->payload, outbound->payload_len); len += outbound->payload_len; + if (outbound->_tx_cr != 0 && outbound->_tx_cr != _default_cr) { + _radio->setCodingRate(outbound->_tx_cr); + } + uint32_t max_airtime = _radio->getEstAirtimeFor(len)*3/2; outbound_start = _ms->getMillis(); bool success = _radio->startSendRaw(raw, len); if (!success) { MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): ERROR: send start failed!", getLogDateTime()); + if (outbound->_tx_cr != 0 && outbound->_tx_cr != _default_cr) { + _radio->setCodingRate(_default_cr); + } logTxFail(outbound, outbound->getRawLength()); - + releasePacket(outbound); // return to pool outbound = NULL; return; @@ -360,6 +373,7 @@ Packet* Dispatcher::obtainNewPacket() { } else { pkt->payload_len = pkt->path_len = 0; pkt->_snr = 0; + pkt->_tx_cr = 0; } return pkt; } diff --git a/src/Dispatcher.h b/src/Dispatcher.h index aad6cba3ec2..bf4fe4ecf39 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -76,6 +76,8 @@ class Radio { */ virtual bool isReceiving() { return false; } + virtual void setCodingRate(uint8_t cr) { } + virtual float getLastRSSI() const { return 0; } virtual float getLastSNR() const { return 0; } }; @@ -138,6 +140,8 @@ class Dispatcher { MillisecondClock* _ms; uint16_t _err_flags; + uint8_t _default_cr; + Dispatcher(Radio& radio, MillisecondClock& ms, PacketManager& mgr) : _radio(&radio), _ms(&ms), _mgr(&mgr) { @@ -152,6 +156,7 @@ class Dispatcher { tx_budget_ms = 0; last_budget_update = 0; duty_cycle_window_ms = 3600000; + _default_cr = 5; } virtual DispatcherAction onRecvPacket(Packet* pkt) = 0; @@ -187,6 +192,8 @@ class Dispatcher { uint32_t getNumSentDirect() const { return n_sent_direct; } uint32_t getNumRecvFlood() const { return n_recv_flood; } uint32_t getNumRecvDirect() const { return n_recv_direct; } + void setDefaultCR(uint8_t cr) { _default_cr = cr; } + void resetStats() { n_sent_flood = n_sent_direct = n_recv_flood = n_recv_direct = 0; _err_flags = 0; diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 9dc42f0b04b..eb8441a641e 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -102,8 +102,9 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { if (!_tables->wasSeen(pkt)) { _tables->markSeen(pkt); removeSelfFromPath(pkt); + pkt->_tx_cr = selectCodingRateForPeer(pkt->path, pkt->getPathHashSize()); - return ACTION_RETRANSMIT_DELAYED(0, d); // Routed traffic is HIGHEST priority + return ACTION_RETRANSMIT_DELAYED(0, d); // Routed traffic is HIGHEST priority } } return ACTION_RELEASE; // this node is NOT the next hop (OR this packet has already been forwarded), so discard. diff --git a/src/Mesh.h b/src/Mesh.h index 22bb5c6d592..da301253985 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -71,6 +71,14 @@ class Mesh : public Dispatcher { */ virtual uint8_t getExtraAckTransmitCount() const; + /** + * \brief Select a coding rate for the next-hop peer based on link quality. + * \param hash the hash of the next-hop peer (from path[0..hash_size-1]) + * \param hash_size bytes per hash entry + * \returns 0 = use node default CR, 5-8 = override CR for this packet + */ + virtual uint8_t selectCodingRateForPeer(const uint8_t* hash, uint8_t hash_size) { return 0; } + /** * \brief Perform search of local DB of peers/contacts. * \returns Number of peers with matching hash diff --git a/src/Packet.cpp b/src/Packet.cpp index aad3e2f48ee..43f8c3a0231 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -8,6 +8,8 @@ Packet::Packet() { header = 0; path_len = 0; payload_len = 0; + _snr = 0; + _tx_cr = 0; } bool Packet::isValidPathLen(uint8_t path_len) { diff --git a/src/Packet.h b/src/Packet.h index c19d9e9d8fc..ad2b0124f57 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -49,6 +49,7 @@ class Packet { uint8_t path[MAX_PATH_SIZE]; uint8_t payload[MAX_PACKET_PAYLOAD]; int8_t _snr; + uint8_t _tx_cr; // 0 = use node default, 5-8 = override CR for this TX (not serialized to wire) /** * \brief calculate the hash of payload + type diff --git a/src/helpers/radiolib/CustomLLCC68Wrapper.h b/src/helpers/radiolib/CustomLLCC68Wrapper.h index b8b4096a6fe..bab90aa8926 100644 --- a/src/helpers/radiolib/CustomLLCC68Wrapper.h +++ b/src/helpers/radiolib/CustomLLCC68Wrapper.h @@ -34,6 +34,8 @@ class CustomLLCC68Wrapper : public RadioLibWrapper { float getLastRSSI() const override { return ((CustomLLCC68 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomLLCC68 *)_radio)->getSNR(); } + void setCodingRate(uint8_t cr) override { ((CustomLLCC68 *)_radio)->setCodingRate(cr); } + float packetScore(float snr, int packet_len) override { int sf = ((CustomLLCC68 *)_radio)->spreadingFactor; return packetScoreInt(snr, sf, packet_len); diff --git a/src/helpers/radiolib/CustomLR1110Wrapper.h b/src/helpers/radiolib/CustomLR1110Wrapper.h index 3c8acbb49ab..db91d4fe293 100644 --- a/src/helpers/radiolib/CustomLR1110Wrapper.h +++ b/src/helpers/radiolib/CustomLR1110Wrapper.h @@ -43,6 +43,8 @@ class CustomLR1110Wrapper : public RadioLibWrapper { _radio->setPreambleLength(preambleLengthForSF(getSpreadingFactor())); // overcomes weird issues with small and big pkts } + void setCodingRate(uint8_t cr) override { ((CustomLR1110 *)_radio)->setCodingRate(cr); } + float getLastRSSI() const override { return ((CustomLR1110 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomLR1110 *)_radio)->getSNR(); } diff --git a/src/helpers/radiolib/CustomSTM32WLxWrapper.h b/src/helpers/radiolib/CustomSTM32WLxWrapper.h index bc93de7584a..8dd1dd27131 100644 --- a/src/helpers/radiolib/CustomSTM32WLxWrapper.h +++ b/src/helpers/radiolib/CustomSTM32WLxWrapper.h @@ -34,6 +34,8 @@ class CustomSTM32WLxWrapper : public RadioLibWrapper { float getLastRSSI() const override { return ((CustomSTM32WLx *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSTM32WLx *)_radio)->getSNR(); } + void setCodingRate(uint8_t cr) override { ((CustomSTM32WLx *)_radio)->setCodingRate(cr); } + float packetScore(float snr, int packet_len) override { int sf = ((CustomSTM32WLx *)_radio)->spreadingFactor; return packetScoreInt(snr, sf, packet_len); diff --git a/src/helpers/radiolib/CustomSX1262Wrapper.h b/src/helpers/radiolib/CustomSX1262Wrapper.h index cf393200818..456c6b3eb84 100644 --- a/src/helpers/radiolib/CustomSX1262Wrapper.h +++ b/src/helpers/radiolib/CustomSX1262Wrapper.h @@ -37,6 +37,8 @@ class CustomSX1262Wrapper : public RadioLibWrapper { float getLastRSSI() const override { return ((CustomSX1262 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1262 *)_radio)->getSNR(); } + void setCodingRate(uint8_t cr) override { ((CustomSX1262 *)_radio)->setCodingRate(cr); } + float packetScore(float snr, int packet_len) override { int sf = ((CustomSX1262 *)_radio)->spreadingFactor; return packetScoreInt(snr, sf, packet_len); diff --git a/src/helpers/radiolib/CustomSX1268Wrapper.h b/src/helpers/radiolib/CustomSX1268Wrapper.h index ba1a635e3c0..eaa4317c714 100644 --- a/src/helpers/radiolib/CustomSX1268Wrapper.h +++ b/src/helpers/radiolib/CustomSX1268Wrapper.h @@ -37,6 +37,8 @@ class CustomSX1268Wrapper : public RadioLibWrapper { float getLastRSSI() const override { return ((CustomSX1268 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1268 *)_radio)->getSNR(); } + void setCodingRate(uint8_t cr) override { ((CustomSX1268 *)_radio)->setCodingRate(cr); } + float packetScore(float snr, int packet_len) override { int sf = ((CustomSX1268 *)_radio)->spreadingFactor; return packetScoreInt(snr, sf, packet_len); diff --git a/src/helpers/radiolib/CustomSX1276Wrapper.h b/src/helpers/radiolib/CustomSX1276Wrapper.h index 9d75ce12a1b..e0af4587709 100644 --- a/src/helpers/radiolib/CustomSX1276Wrapper.h +++ b/src/helpers/radiolib/CustomSX1276Wrapper.h @@ -28,6 +28,8 @@ class CustomSX1276Wrapper : public RadioLibWrapper { float getLastRSSI() const override { return ((CustomSX1276 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1276 *)_radio)->getSNR(); } + void setCodingRate(uint8_t cr) override { ((CustomSX1276 *)_radio)->setCodingRate(cr); } + float packetScore(float snr, int packet_len) override { int sf = ((CustomSX1276 *)_radio)->spreadingFactor; return packetScoreInt(snr, sf, packet_len); From 5f3d20eab679734656d9577d565e1cfb80dbb17c Mon Sep 17 00:00:00 2001 From: Wessel Nieboer Date: Thu, 5 Mar 2026 04:46:38 +0100 Subject: [PATCH 2/5] make it a const --- examples/simple_repeater/MyMesh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 24ff5d9b652..72ea6978d10 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -100,7 +100,7 @@ int8_t MyMesh::findNeighbourSNR(const uint8_t* hash, uint8_t hash_size) { } // Approximate SNR demod floor per SF (same as RadioLibWrappers.cpp) -static float cr_snr_thresholds[] = { +static const float cr_snr_thresholds[] = { -7.5f, // SF7 -10.0f, // SF8 -12.5f, // SF9 From e91fb635ee70693dcabedc5d9c88bdaf6bcfc18b Mon Sep 17 00:00:00 2001 From: Wessel Nieboer Date: Tue, 16 Jun 2026 12:45:18 +0200 Subject: [PATCH 3/5] Reset _tx_cr on inbound packets to prevent stale CR override Inbound packets are allocated via PacketManager::allocNew() directly, bypassing obtainNewPacket()'s field reset. Since the static pool reuses Packet objects without zeroing, a packet whose slot previously held a direct-forward TX (with _tx_cr set to 6/7/8) would carry that stale value into a flood/non-direct retransmit, which sets neither _tx_cr. Reset it next to the _snr init in checkRecv(). --- src/Dispatcher.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 55caa98346d..301ba18c781 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -210,6 +210,7 @@ void Dispatcher::checkRecv() { } else { if (tryParsePacket(pkt, raw, len)) { pkt->_snr = _radio->getLastSNR() * 4.0f; + pkt->_tx_cr = 0; // pool slots are reused without zeroing; clear stale CR override so flood/non-direct retransmits use the default CR score = _radio->packetScore(_radio->getLastSNR(), len); air_time = _radio->getEstAirtimeFor(len); rx_air_time += air_time; From a2456327788c4f3d6da3e1f82e94a9024bedcdd8 Mon Sep 17 00:00:00 2001 From: Robert Ekl Date: Wed, 11 Mar 2026 00:00:20 -0500 Subject: [PATCH 4/5] Respect dynamic CR in transmit timing (cherry picked from commit d2e230d51bb89d298a06dddd9065201df331f35c) --- examples/companion_radio/MyMesh.cpp | 21 +++++++--- examples/companion_radio/MyMesh.h | 1 + examples/simple_repeater/MyMesh.cpp | 10 +++-- examples/simple_repeater/MyMesh.h | 3 ++ examples/simple_room_server/MyMesh.cpp | 11 ++++-- examples/simple_room_server/MyMesh.h | 3 ++ examples/simple_secure_chat/main.cpp | 4 ++ examples/simple_sensor/SensorMesh.cpp | 11 ++++-- examples/simple_sensor/SensorMesh.h | 3 ++ src/Dispatcher.cpp | 24 +++++++++-- src/Dispatcher.h | 4 ++ src/Mesh.cpp | 9 +++-- src/helpers/BaseChatMesh.cpp | 55 +++++++++++++++++--------- 13 files changed, 116 insertions(+), 43 deletions(-) diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 755c56d14c0..eec31f9f28c 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -263,6 +263,10 @@ float MyMesh::getAirtimeBudgetFactor() const { return _prefs.airtime_factor; } +uint32_t MyMesh::estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr) const { + return estimateLoRaAirtimeFor(len_bytes, _prefs.sf, _prefs.bw, tx_cr != 0 ? tx_cr : _prefs.cr); +} + int MyMesh::getInterferenceThreshold() const { return _prefs.interference_threshold; } @@ -276,12 +280,12 @@ int MyMesh::calcRxDelay(float score, uint32_t air_time) const { } uint32_t MyMesh::getRetransmitDelay(const mesh::Packet *packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.tx_delay_factor); - return getRNG()->nextInt(0, 5*t + 1); + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.tx_delay_factor); + return getRNG()->nextInt(0, 5 * tx_airtime_ms + 1); } uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); - return getRNG()->nextInt(0, 5*t + 1); + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.direct_tx_delay_factor); + return getRNG()->nextInt(0, 5 * tx_airtime_ms + 1); } uint8_t MyMesh::getExtraAckTransmitCount() const { @@ -1003,6 +1007,7 @@ void MyMesh::begin() { _store->loadChannels(this); radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); radio_driver.setTxPower(_prefs.tx_power_dbm); radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain); @@ -1450,6 +1455,7 @@ void MyMesh::handleCmdFrame(size_t len) { savePrefs(); radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); MESH_DEBUG_PRINTLN("OK: CMD_SET_RADIO_PARAMS: f=%d, bw=%d, sf=%d, cr=%d", freq, bw, (uint32_t)sf, (uint32_t)cr); @@ -1824,10 +1830,13 @@ void MyMesh::handleCmdFrame(size_t len) { memcpy(&auth, &cmd_frame[5], 4); auto pkt = createTrace(tag, auth, flags); if (pkt) { + if ((path_len >> path_sz) > 0) { + pkt->_tx_cr = selectCodingRateForPeer(&cmd_frame[10], 1 << path_sz); + } sendDirect(pkt, &cmd_frame[10], path_len); - uint32_t t = _radio->getEstAirtimeFor(pkt->payload_len + pkt->path_len + 2); - uint32_t est_timeout = calcDirectTimeoutMillisFor(t, path_len >> path_sz); + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->payload_len + pkt->path_len + 2, pkt->_tx_cr); + uint32_t est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, path_len >> path_sz); out_frame[0] = RESP_CODE_SENT; out_frame[1] = 0; diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index 944790df922..3fd276378f2 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -128,6 +128,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost { protected: float getAirtimeBudgetFactor() const override; + uint32_t estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr=0) const override; int getInterferenceThreshold() const override; bool getCADEnabled() const override; int getAGCResetInterval() const override { diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 72ea6978d10..9450309b7d0 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -582,12 +582,12 @@ int MyMesh::calcRxDelay(float score, uint32_t air_time) const { } uint32_t MyMesh::getRetransmitDelay(const mesh::Packet *packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.tx_delay_factor); - return getRNG()->nextInt(0, 5*t + 1); + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.tx_delay_factor); + return getRNG()->nextInt(0, 5 * tx_airtime_ms + 1); } uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); - return getRNG()->nextInt(0, 5*t + 1); + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.direct_tx_delay_factor); + return getRNG()->nextInt(0, 5 * tx_airtime_ms + 1); } mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) { @@ -1348,12 +1348,14 @@ void MyMesh::loop() { if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params set_radio_at = 0; // clear timer radio_driver.setParams(pending_freq, pending_bw, pending_sf, pending_cr); + setDefaultCR(pending_cr); MESH_DEBUG_PRINTLN("Temp radio params"); } if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig revert_radio_at = 0; // clear timer radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); MESH_DEBUG_PRINTLN("Radio params restored"); } diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index bcbc166f711..dcd84346168 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -132,6 +132,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { float getAirtimeBudgetFactor() const override { return _prefs.airtime_factor; } + uint32_t estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr=0) const override { + return estimateLoRaAirtimeFor(len_bytes, _prefs.sf, _prefs.bw, tx_cr != 0 ? tx_cr : _prefs.cr); + } bool allowPacketForward(const mesh::Packet* packet) override; const char* getLogDateTime() override; diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 935fd7f92fe..6bb353b30a6 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -290,12 +290,12 @@ const char *MyMesh::getLogDateTime() { } uint32_t MyMesh::getRetransmitDelay(const mesh::Packet *packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.tx_delay_factor); - return getRNG()->nextInt(0, 5*t + 1); + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.tx_delay_factor); + return getRNG()->nextInt(0, 5 * tx_airtime_ms + 1); } uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); - return getRNG()->nextInt(0, 5*t + 1); + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.direct_tx_delay_factor); + return getRNG()->nextInt(0, 5 * tx_airtime_ms + 1); } bool MyMesh::allowPacketForward(const mesh::Packet *packet) { @@ -724,6 +724,7 @@ void MyMesh::begin(FILESYSTEM *fs) { } radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); radio_driver.setTxPower(_prefs.tx_power_dbm); radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain); @@ -1058,12 +1059,14 @@ void MyMesh::loop() { if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params set_radio_at = 0; // clear timer radio_driver.setParams(pending_freq, pending_bw, pending_sf, pending_cr); + setDefaultCR(pending_cr); MESH_DEBUG_PRINTLN("Temp radio params"); } if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig revert_radio_at = 0; // clear timer radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); MESH_DEBUG_PRINTLN("Radio params restored"); } diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 4dee8bdbf40..c4e73489191 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -132,6 +132,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { float getAirtimeBudgetFactor() const override { return _prefs.airtime_factor; } + uint32_t estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr=0) const override { + return estimateLoRaAirtimeFor(len_bytes, _prefs.sf, _prefs.bw, tx_cr != 0 ? tx_cr : _prefs.cr); + } void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override; void logRx(mesh::Packet* pkt, int len, float score) override; diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index 2e6ba7126d3..b849faac2f8 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -193,6 +193,9 @@ class MyMesh : public BaseChatMesh, ContactVisitor { float getAirtimeBudgetFactor() const override { return _prefs.airtime_factor; } + uint32_t estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr=0) const override { + return estimateLoRaAirtimeFor(len_bytes, LORA_SF, LORA_BW, tx_cr != 0 ? tx_cr : LORA_CR); + } int calcRxDelay(float score, uint32_t air_time) const override { return 0; // disable rxdelay @@ -594,6 +597,7 @@ void setup() { #endif radio_driver.setParams(the_mesh.getFreqPref(), LORA_BW, LORA_SF, LORA_CR); + the_mesh.setDefaultCR(LORA_CR); radio_driver.setTxPower(the_mesh.getTxPowerPref()); the_mesh.showWelcome(); diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index b186066810d..512b8f753b5 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -328,12 +328,12 @@ int SensorMesh::calcRxDelay(float score, uint32_t air_time) const { } uint32_t SensorMesh::getRetransmitDelay(const mesh::Packet* packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.tx_delay_factor); - return getRNG()->nextInt(0, 6)*t; + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.tx_delay_factor); + return getRNG()->nextInt(0, 6) * tx_airtime_ms; } uint32_t SensorMesh::getDirectRetransmitDelay(const mesh::Packet* packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); - return getRNG()->nextInt(0, 6)*t; + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2, packet->_tx_cr) * _prefs.direct_tx_delay_factor); + return getRNG()->nextInt(0, 6) * tx_airtime_ms; } int SensorMesh::getInterferenceThreshold() const { return _prefs.interference_threshold; @@ -856,6 +856,7 @@ void SensorMesh::begin(FILESYSTEM* fs) { } radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); radio_driver.setTxPower(_prefs.tx_power_dbm); board.attachDynamicPrefs(_prefs.getCustom()); @@ -1018,12 +1019,14 @@ void SensorMesh::loop() { if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params set_radio_at = 0; // clear timer radio_driver.setParams(pending_freq, pending_bw, pending_sf, pending_cr); + setDefaultCR(pending_cr); MESH_DEBUG_PRINTLN("Temp radio params"); } if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig revert_radio_at = 0; // clear timer radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + setDefaultCR(_prefs.cr); MESH_DEBUG_PRINTLN("Radio params restored"); } diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index 590ca0c7657..31c25f48747 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -121,6 +121,9 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks { // Mesh overrides float getAirtimeBudgetFactor() const override; + uint32_t estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr=0) const override { + return estimateLoRaAirtimeFor(len_bytes, _prefs.sf, _prefs.bw, tx_cr != 0 ? tx_cr : _prefs.cr); + } bool allowPacketForward(const mesh::Packet* packet) override; int calcRxDelay(float score, uint32_t air_time) const override; uint32_t getRetransmitDelay(const mesh::Packet* packet) override; diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 301ba18c781..f6b22a8a413 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -63,6 +63,25 @@ uint32_t Dispatcher::getCADFailMaxDuration() const { return 4000; // 4 seconds } +uint32_t Dispatcher::estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr) const { + (void)tx_cr; + return _radio->getEstAirtimeFor(len_bytes); +} + +// Matches the standard LoRa time-on-air equation for the configured modem settings. +uint32_t Dispatcher::estimateLoRaAirtimeFor(int len_bytes, uint8_t sf, float bw_khz, uint8_t cr) const { + bool ldro_enabled = (((float)(1UL << sf)) / bw_khz) > 16.0f; + float low_data_rate_optimize = ldro_enabled ? 1.0f : 0.0f; + float implicit_header = 0.0f; + float crc_enabled = 1.0f; + float preamble_symbols = 16.0f; + float payload_symbols = 8.0f + fmaxf(ceilf((8.0f * (float)len_bytes - 4.0f * (float)sf + 28.0f + 16.0f * crc_enabled - 20.0f * implicit_header) / + (4.0f * (float)sf - 8.0f * low_data_rate_optimize)) * (float)cr, 0.0f); + float total_symbols = preamble_symbols + payload_symbols + 4.25f; + float symbol_length_ms = ((float)(uint32_t(1) << sf) / bw_khz); + return (uint32_t)ceilf(symbol_length_ms * total_symbols); +} + void Dispatcher::loop() { if (millisHasNowPassed(next_floor_calib_time)) { _radio->triggerNoiseFloorCalibrate(getInterferenceThreshold()); @@ -334,8 +353,7 @@ void Dispatcher::checkSend() { if (outbound->_tx_cr != 0 && outbound->_tx_cr != _default_cr) { _radio->setCodingRate(outbound->_tx_cr); } - - uint32_t max_airtime = _radio->getEstAirtimeFor(len)*3/2; + uint32_t max_airtime = estimateTxAirtimeFor(len, outbound->_tx_cr) * 3 / 2; outbound_start = _ms->getMillis(); bool success = _radio->startSendRaw(raw, len); if (!success) { @@ -402,4 +420,4 @@ unsigned long Dispatcher::futureMillis(int millis_from_now) const { return _ms->getMillis() + millis_from_now; } -} \ No newline at end of file +} diff --git a/src/Dispatcher.h b/src/Dispatcher.h index bf4fe4ecf39..28813937a4a 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -176,6 +176,10 @@ class Dispatcher { virtual bool getCADEnabled() const { return false; } // hardware CAD disabled by default virtual int getAGCResetInterval() const { return 0; } // disabled by default virtual unsigned long getDutyCycleWindowMs() const { return 3600000; } + // Estimates TX airtime using an explicit per-packet CR override when present. + virtual uint32_t estimateTxAirtimeFor(int len_bytes, uint8_t tx_cr=0) const; + // LoRa airtime helper for meshes that know the active SF/BW/CR settings directly. + uint32_t estimateLoRaAirtimeFor(int len_bytes, uint8_t sf, float bw_khz, uint8_t cr) const; public: void begin(); diff --git a/src/Mesh.cpp b/src/Mesh.cpp index eb8441a641e..85763955260 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -15,9 +15,9 @@ bool Mesh::allowPacketForward(const mesh::Packet* packet) { return false; // by default, Transport NOT enabled } uint32_t Mesh::getRetransmitDelay(const mesh::Packet* packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->getRawLength()) * 52 / 50) / 2; + uint32_t tx_airtime_ms = (estimateTxAirtimeFor(packet->getRawLength(), packet->_tx_cr) * 52 / 50) / 2; - return _rng->nextInt(0, 5)*t; + return _rng->nextInt(0, 5) * tx_airtime_ms; } uint32_t Mesh::getDirectRetransmitDelay(const Packet* packet) { return 0; // by default, no delay @@ -705,6 +705,9 @@ void Mesh::sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uin pri = 5; // maybe make this configurable } else { packet->path_len = Packet::copyPath(packet->path, path, path_len); + if (packet->_tx_cr == 0 && packet->getPathHashCount() > 0) { + packet->_tx_cr = selectCodingRateForPeer(packet->path, packet->getPathHashSize()); + } if (packet->getPayloadType() == PAYLOAD_TYPE_PATH) { pri = 1; // slightly less priority } else { @@ -739,4 +742,4 @@ void Mesh::sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay sendPacket(packet, 0, delay_millis); } -} \ No newline at end of file +} diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 4368d17f1c2..eecfa82be6b 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -480,16 +480,18 @@ int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint32_t timestamp, mesh::Packet* pkt = composeMsgPacket(recipient, timestamp, attempt, text, expected_ack); if (pkt == NULL) return MSG_SEND_FAILED; - uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); - int rc; if (recipient.out_path_len == OUT_PATH_UNKNOWN) { + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendFloodScoped(recipient, pkt); - txt_send_timeout = futureMillis(est_timeout = calcFloodTimeoutMillisFor(t)); + txt_send_timeout = futureMillis(est_timeout = calcFloodTimeoutMillisFor(tx_airtime_ms)); rc = MSG_SEND_SENT_FLOOD; } else { + pkt->_tx_cr = selectCodingRateForPeer(recipient.out_path, (recipient.out_path_len >> 6) + 1); + // Downstream repeater CR choices are not knowable here, so only the local hop is modeled exactly. + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendDirect(pkt, recipient.out_path, recipient.out_path_len); - txt_send_timeout = futureMillis(est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len)); + txt_send_timeout = futureMillis(est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, recipient.out_path_len)); rc = MSG_SEND_SENT_DIRECT; } return rc; @@ -507,15 +509,18 @@ int BaseChatMesh::sendCommandData(const ContactInfo& recipient, uint32_t timest auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.getSharedSecret(self_id), temp, 5 + text_len); if (pkt == NULL) return MSG_SEND_FAILED; - uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); int rc; if (recipient.out_path_len == OUT_PATH_UNKNOWN) { + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendFloodScoped(recipient, pkt); - txt_send_timeout = futureMillis(est_timeout = calcFloodTimeoutMillisFor(t)); + txt_send_timeout = futureMillis(est_timeout = calcFloodTimeoutMillisFor(tx_airtime_ms)); rc = MSG_SEND_SENT_FLOOD; } else { + pkt->_tx_cr = selectCodingRateForPeer(recipient.out_path, (recipient.out_path_len >> 6) + 1); + // Downstream repeater CR choices are not knowable here, so only the local hop is modeled exactly. + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendDirect(pkt, recipient.out_path, recipient.out_path_len); - txt_send_timeout = futureMillis(est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len)); + txt_send_timeout = futureMillis(est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, recipient.out_path_len)); rc = MSG_SEND_SENT_DIRECT; } return rc; @@ -632,14 +637,17 @@ int BaseChatMesh::sendLogin(const ContactInfo& recipient, const char* password, pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.getSharedSecret(self_id), temp, tlen); } if (pkt) { - uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); if (recipient.out_path_len == OUT_PATH_UNKNOWN) { + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendFloodScoped(recipient, pkt); - est_timeout = calcFloodTimeoutMillisFor(t); + est_timeout = calcFloodTimeoutMillisFor(tx_airtime_ms); return MSG_SEND_SENT_FLOOD; } else { + pkt->_tx_cr = selectCodingRateForPeer(recipient.out_path, (recipient.out_path_len >> 6) + 1); + // Downstream repeater CR choices are not knowable here, so only the local hop is modeled exactly. + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendDirect(pkt, recipient.out_path, recipient.out_path_len); - est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len); + est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, recipient.out_path_len); return MSG_SEND_SENT_DIRECT; } } @@ -657,14 +665,17 @@ int BaseChatMesh::sendAnonReq(const ContactInfo& recipient, const uint8_t* data, pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.getSharedSecret(self_id), temp, 4 + len); } if (pkt) { - uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); if (recipient.out_path_len == OUT_PATH_UNKNOWN) { + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendFloodScoped(recipient, pkt); - est_timeout = calcFloodTimeoutMillisFor(t); + est_timeout = calcFloodTimeoutMillisFor(tx_airtime_ms); return MSG_SEND_SENT_FLOOD; } else { + pkt->_tx_cr = selectCodingRateForPeer(recipient.out_path, (recipient.out_path_len >> 6) + 1); + // Downstream repeater CR choices are not knowable here, so only the local hop is modeled exactly. + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendDirect(pkt, recipient.out_path, recipient.out_path_len); - est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len); + est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, recipient.out_path_len); return MSG_SEND_SENT_DIRECT; } } @@ -684,14 +695,17 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, const uint8_t* req_ pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.getSharedSecret(self_id), temp, 4 + data_len); } if (pkt) { - uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); if (recipient.out_path_len == OUT_PATH_UNKNOWN) { + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendFloodScoped(recipient, pkt); - est_timeout = calcFloodTimeoutMillisFor(t); + est_timeout = calcFloodTimeoutMillisFor(tx_airtime_ms); return MSG_SEND_SENT_FLOOD; } else { + pkt->_tx_cr = selectCodingRateForPeer(recipient.out_path, (recipient.out_path_len >> 6) + 1); + // Downstream repeater CR choices are not knowable here, so only the local hop is modeled exactly. + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendDirect(pkt, recipient.out_path, recipient.out_path_len); - est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len); + est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, recipient.out_path_len); return MSG_SEND_SENT_DIRECT; } } @@ -711,14 +725,17 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, uint8_t req_type, u pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.getSharedSecret(self_id), temp, sizeof(temp)); } if (pkt) { - uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); if (recipient.out_path_len == OUT_PATH_UNKNOWN) { + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendFloodScoped(recipient, pkt); - est_timeout = calcFloodTimeoutMillisFor(t); + est_timeout = calcFloodTimeoutMillisFor(tx_airtime_ms); return MSG_SEND_SENT_FLOOD; } else { + pkt->_tx_cr = selectCodingRateForPeer(recipient.out_path, (recipient.out_path_len >> 6) + 1); + // Downstream repeater CR choices are not knowable here, so only the local hop is modeled exactly. + uint32_t tx_airtime_ms = estimateTxAirtimeFor(pkt->getRawLength(), pkt->_tx_cr); sendDirect(pkt, recipient.out_path, recipient.out_path_len); - est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len); + est_timeout = calcDirectTimeoutMillisFor(tx_airtime_ms, recipient.out_path_len); return MSG_SEND_SENT_DIRECT; } } From d3c562750283235c77286e96b9e9939130f16d37 Mon Sep 17 00:00:00 2001 From: Wessel Nieboer Date: Tue, 16 Jun 2026 12:58:57 +0200 Subject: [PATCH 5/5] Fix airtime estimate: use SF-dependent preamble length estimateLoRaAirtimeFor() hardcoded a 16-symbol preamble, but MeshCore sets the preamble per-SF via RadioLibWrapper::preambleLengthForSF (32 symbols for SF<=8, 16 otherwise). On the default SF8/BW62.5 this made the estimate ~65ms low versus the radio's real getTimeOnAir(), skewing retransmit jitter and tightening the TX-expiry watchdog. Use the same SF<=8 ? 32 : 16 rule so the estimate matches the radio for default CR and only diverges when CR is actually overridden. --- src/Dispatcher.cpp | 5 ++++- src/helpers/radiolib/CustomLR1121Wrapper.h | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index f6b22a8a413..e5f19f86796 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -74,7 +74,10 @@ uint32_t Dispatcher::estimateLoRaAirtimeFor(int len_bytes, uint8_t sf, float bw_ float low_data_rate_optimize = ldro_enabled ? 1.0f : 0.0f; float implicit_header = 0.0f; float crc_enabled = 1.0f; - float preamble_symbols = 16.0f; + // Preamble length is SF-dependent (RadioLibWrapper::preambleLengthForSF): 32 symbols for + // SF<=8, else 16. Hardcoding 16 under-counts airtime by 16 symbols on SF7/SF8 (the default + // is SF8), which would skew retransmit delays and the TX-expiry watchdog. + float preamble_symbols = (sf <= 8) ? 32.0f : 16.0f; float payload_symbols = 8.0f + fmaxf(ceilf((8.0f * (float)len_bytes - 4.0f * (float)sf + 28.0f + 16.0f * crc_enabled - 20.0f * implicit_header) / (4.0f * (float)sf - 8.0f * low_data_rate_optimize)) * (float)cr, 0.0f); float total_symbols = preamble_symbols + payload_symbols + 4.25f; diff --git a/src/helpers/radiolib/CustomLR1121Wrapper.h b/src/helpers/radiolib/CustomLR1121Wrapper.h index d1b6814b235..5d0b6a2a023 100644 --- a/src/helpers/radiolib/CustomLR1121Wrapper.h +++ b/src/helpers/radiolib/CustomLR1121Wrapper.h @@ -19,6 +19,8 @@ class CustomLR1121Wrapper : public RadioLibWrapper { ((CustomLR1121 *)_radio)->setMaxPayloadMillis(pm.payloadMillis); } + void setCodingRate(uint8_t cr) override { ((CustomLR1121 *)_radio)->setCodingRate(cr); } + void doResetAGC() override { lr11x0ResetAGC((LR11x0 *)_radio, ((CustomLR1121 *)_radio)->getFreqMHz(), getRxBoostedGainMode()); } bool lastRxHadCRC() override { bool crc = true;