diff --git a/README-repeated-sending.md b/README-repeated-sending.md new file mode 100644 index 00000000000..ee494b64014 --- /dev/null +++ b/README-repeated-sending.md @@ -0,0 +1,56 @@ +# Repeated Sending (branch `feature/repeated-sending-2`) + +Reliable delivery of direct-routed packets via **cancellable retransmissions**. The feature augments DIRECT/TRACE routing with an automatic, self-limiting retransmission that switches itself off one hop downstream as soon as successful forwarding is detected. + +## Problem + +A direct-routed packet (DIRECT / TRACE) that travels to its destination via one or more relays is considered "sent" by the originator as soon as it is on air. If it is lost on the first hop (collision, interference, fading), there is no retry at the MeshCore layer — the packet is gone, and the application layer must re-schedule the entire message. Flood traffic does not have this problem because it is widely broadcast anyway; DIRECT packets, however, are the cost-conscious path for targeted messages. + +## Solution + +After every DIRECT packet is sent, the Dispatcher schedules a retransmission. The key trick: **the originator listens for a downstream relay forwarding the packet**. As soon as it receives that forward (the "forwarding echo"), it knows its packet survived at least the next hop and cancels the pending retransmission. The result is an error-correction mechanism that produces **no** extra packet in the success case, and in the failure case retransmits precisely where the packet actually got stuck. + +Flow in detail: + +1. **Schedule the resend** – `Dispatcher::resendPacket()` runs after TX only for DIRECT packets with at least one relay hash in the path (`getPathHashCount() > 0`). It does not wait for a timeout; instead it re-queues the packet after a computed **cancel window** `W = C0 + K·airtime + margin + (attempt−1)·jitter`, capped at an airtime-proportional ceiling (`CAP_BASE + CAP_AIRTIME_X·airtime`). The window always outlasts the downstream forward for any packet length or spreading factor (a flat ceiling under-covered SF10 long messages, so every long packet fired a redundant, colliding resend), while staying as short as the airtime allows so the echo arrives before the resend fires. +2. **Detect the forward and cancel** – In `Mesh::onRecvPacket()`, every received DIRECT packet is checked: is it a downstream forward of one of our own pending resends (`Packet::isRetryMatch()`)? If so, the resend is removed from the outbound queue (`removeOutboundByIdx`) — the current `outbound` packet and every queued entry alike. A deterministic per-hop busy-recheck bucket (`pathHashCount % 3`) keeps deferred resenders of the same chain from re-checking the channel on the same loop tick. +3. **Non-invasive LBT** – Resends use *no* hardware CAD, but `isResendChannelActive()`: a pure IRQ/register read (preamble detection + RSSI margin above the noise floor). RX stays open so the forwarding echo is not missed. First sends still use normal CAD (collision avoidance is worth the momentary deafness). + +### TRACE handling + +TRACE packets append an SNR byte per hop, which changes their hash. `isRetryMatch()` therefore compares payload + SNR-path prefix instead of the hash for TRACE. Because the TRACE branch in `onRecvPacket()` always returns, the generic echo-cancel block is never reached for TRACE — the TRACE branch therefore performs its own cancel scan: when a later hop's forward of a TRACE we already sent is overheard, our queued TRACE resend is dropped (for well-formed, non-terminal legs only). Without this, every intermediate hop would re-send each TRACE up to `max_resend_attempts` times even though the next hop had already forwarded it. + +At the final forwarding hop (destination does not forward) the packet is flagged `no_retry` and sent once through the normal TX gate — a retry here could never be cancelled by overhearing a forward, and pre-setting the attempt counter (the earlier approach) made the first send look like a resend: it went through the resend LBT gate instead of the normal channel check and was counted in the resend statistics. + +### Final-hop handling + +On the final relay hop there is no downstream forward to overhear — the destination does not forward. For **TXT_MSG with ACK** the destination acknowledges receipt with an ACK. This hop is allowed exactly one ACK-cancellable resend (flag `final_hop_ack_resend`). `Mesh::cancelPendingFinalHopResend()` cancels the oldest final-hop resend still sitting in the queue as soon as the ACK returns (FIFO, since ACKs are produced in delivery order). A resend already on air is deliberately not aborted (the destination dedups the duplicate via `wasSeen`). + +## Configuration + +| Location | Value | Meaning | +|---|---|---| +| CLI `set max.resend <0–3>` / `get max.resend` | Default `2` | Maximum resend attempts for DIRECT packets. `0` disables the feature entirely. `get` also reports the resend ratio, e.g. `2, resends 64/173 (37%)`. Available on CommonCLI nodes (repeater/chat/room-server) **and on companion radios** via their USB/BLE CLI (`CMD_RUN_CLI_COMMAND` passthrough) — identical commands and reply format everywhere. | +| `NodePrefs` (persisted) | `max_resend_attempts` | Saved/loaded via the ConfigSerializer prefs file; sanitised to 0–3. (The legacy binary migration path reads it at byte offset 295.) | +| Companion-radio `CMD_SET_*` frame | Byte 6 in the path block | App-protocol path to set `max_resend_attempts` from the companion. | +| `RESEND_INTERFERENCE_MARGIN` (compile-time) | Default `12` dB | Margin above the noise floor at which a resend is blocked (non-invasive LBT). | +| `RESEND_DEFERRAL_*` (compile-time) | `C0 1500`, `K 2`, `margin 40`, `cap 1500 + 3·airtime`, `jitter 100` | Cancel-window model, in milliseconds/airtime multiples (see `src/Dispatcher.h`). | + +## Implementation (core files) + +- `src/Dispatcher.cpp` / `.h` – `resendPacket()`, `resendWindowFor()`, `isResendChannelActive()`, `getMaxResendAttempts()`, pool-shedding skip, resend statistics. +- `src/Mesh.cpp` / `.h` – forwarding detection and resend cancellation in `onRecvPacket()` (generic DIRECT block + TRACE branch scan), `cancelPendingFinalHopResend()`, final-hop marking (`final_hop_ack_resend`, `no_retry`). +- `src/Packet.cpp` / `.h` – cached packet hash (`hash`, `hash_hex`), `isRetryMatch()` (TRACE-specific vs. hash-based), fields `sending_attempts` / `no_retry` / `final_hop_ack_resend`. +- `src/helpers/SimpleMeshTables.h` – dedicated ACK dedup table (`_acks[]`, via `ack_crc`), so multi-ACK/resend duplicates do not evict the flood-dedup entries. +- `src/helpers/StaticPoolPacketManager.*` – `peek()` / `peekNextOutbound()` (non-consuming) and reset of `sending_attempts` / `no_retry` / `final_hop_ack_resend` in `free()` (see below). +- `src/helpers/CommonCLI.*`, `docs/cli_commands.md`, `examples/*/MyMesh.*`, `NodePrefs.h` – config knobs and persistence. + +## Note: pool-reuse fix + +`sending_attempts` and `final_hop_ack_resend` were originally zeroed only in the `Packet` constructor, **not** when the packet was returned to the pool (`free()`). A recycled pool slot thus kept a stale `sending_attempts >= getMaxResendAttempts()`, causing `resendPacket()` to skip the resend for every subsequent DIRECT packet routed through that slot. Symptom on hardware: resends work right after boot (fresh pool) but degrade to zero once the pool has turned over (~`pool_size` sends) — forwards kept working because they do not check `sending_attempts`. `StaticPoolPacketManager::free()` now resets the cached hash and all resend fields in that chokepoint. + +## Status & scope + +- Rebased on `upstream/dev`; three commits: simulator hooks, the resend feature, and the TRACE resend fixes (found by @Kernel-Error, measured by @m0urs — see m0urs/MeshCore#25, m0urs/MeshCore#26). +- Applies exclusively to **DIRECT/TRACE-routed** traffic. Flood traffic is unaffected. +- Only active when `max.resend > 0` (default `2`). diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 7bd0e3ace36..a83efc7db47 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -515,6 +515,18 @@ This document provides an overview of CLI commands that can be sent to MeshCore --- +#### View or change the maximum direct-route resend attempts +**Usage:** +- `get max.resend` +- `set max.resend ` + +**Parameters:** +- `value`: Maximum number of resend attempts for direct-routed packets (0–3). `0` disables resending entirely. + +**Default:** `2` + +--- + #### View or change the retransmit delay factor for flood traffic **Usage:** - `get txdelay` diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index 06c56a7a444..0de1550c0da 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -238,6 +238,7 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs) { file.read((uint8_t *)&_prefs.rx_boosted_gain, sizeof(_prefs.rx_boosted_gain)); // 89 file.read((uint8_t *)_prefs.default_scope_name, sizeof(_prefs.default_scope_name)); // 90 file.read((uint8_t *)_prefs.default_scope_key, sizeof(_prefs.default_scope_key)); // 121 + file.read((uint8_t *)&_prefs.max_resend_attempts, sizeof(_prefs.max_resend_attempts)); // 137 // migrate old fields _prefs.setRepeatEn(_prefs._client_repeat != 0); diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index cd6966e8a6b..9381cf7f354 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -5,6 +5,7 @@ #include #endif #include +#include #define CMD_APP_START 1 #define CMD_SEND_TXT_MSG 2 @@ -111,6 +112,22 @@ #define DIRECT_SEND_PERHOP_EXTRA_MILLIS 250 #define LAZY_CONTACTS_WRITE_DELAY 5000 +// Fixed RSSI margin (dB above the noise floor) at which a DIRECT resend treats the channel as busy +// (isResendChannelActive(): busy when isReceivingPacket() || currentRSSI - noiseFloor >= this). It is +// the resend-only counterpart of the general-LBT _prefs.interference_threshold, which flows through +// getInterferenceThreshold() -> triggerNoiseFloorCalibrate() -> RadioLibWrapper::isChannelActive(). +// They are separate today only because interference_threshold is blanket-initialised to 0 (disabled; +// see getInterferenceThreshold(), cited there as a currentRSSI() reliability concern). The margin +// currentRSSI-noiseFloor is only as trustworthy as both terms: the median noise-floor estimator +// (PR #2933) stabilises getNoiseFloor() and removes the dominant drift source (the old one-way +// ratchet to -120 that inflated every margin), substantially rehabilitating the threshold path. +// Once interference_threshold is enabled across the fleet, this constant can be superseded by it. +// NB: here getInterferenceThreshold() is hardcoded to 0 (not prefs-driven), so superseding this +// constant via interference_threshold additionally requires dropping that override. +#ifndef RESEND_INTERFERENCE_MARGIN + #define RESEND_INTERFERENCE_MARGIN 12 // dB above noise floor that blocks a resend (non-invasive LBT) +#endif + #define PUBLIC_GROUP_PSK "izOH6cXN6mrJ5e26oRXNcg==" // these are _pushed_ to client app at any time @@ -270,6 +287,15 @@ bool MyMesh::getCADEnabled() const { return _prefs.cad_enabled; } +bool MyMesh::isResendChannelActive() { + // Non-invasive resend LBT (NO CAD, so RX stays open to overhear the downstream forward + // and cancel the resend). Block the resend when a LoRa preamble/header is being received + // (often that forward itself) OR the live channel energy is well above the noise floor + // (foreign interference). isReceivingPacket()/getCurrentRSSI() are IRQ/register reads. + int margin = (int)radio_driver.getCurrentRSSI() - _radio->getNoiseFloor(); + return radio_driver.isReceivingPacket() || (margin >= RESEND_INTERFERENCE_MARGIN); +} + int MyMesh::calcRxDelay(float score, uint32_t air_time) const { if (_prefs.rx_delay_base <= 0.0f) return 0; return (int)((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time); @@ -279,6 +305,7 @@ 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 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); @@ -925,6 +952,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe _prefs.tx_power_dbm = LORA_TX_POWER; _prefs.gps_enabled = 0; // GPS disabled by default _prefs.gps_interval = 0; // No automatic GPS updates by default + _prefs.max_resend_attempts = 2; _prefs.radio_fem_rxgain = 1; #ifdef RADIO_FEM_TXGAIN _prefs.radio_fem_txgain = RADIO_FEM_TXGAIN; // board-specific default for PA @@ -995,6 +1023,7 @@ void MyMesh::begin() { _prefs.gps_interval = constrain(_prefs.gps_interval, 0, 86400); // Max 24 hours _prefs.adc_multiplier = constrain(_prefs.adc_multiplier, 0.0f, 10.0f); if (!board.setAdcMultiplier(_prefs.adc_multiplier)) _prefs.adc_multiplier = 0.0f; + _prefs.max_resend_attempts = constrain(_prefs.max_resend_attempts, 0, 3); _active_ble_pin = _prefs.ble_pin; @@ -1500,6 +1529,9 @@ void MyMesh::handleCmdFrame(size_t len) { _prefs.advert_loc_policy = cmd_frame[3]; if (len >= 5) { _prefs.multi_acks = cmd_frame[4]; + if (len >= 6) { + _prefs.max_resend_attempts = constrain(cmd_frame[5], 0, 3); + } } } } @@ -2131,6 +2163,27 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char* return true; } + // repeated sending (feature): same command and reply format as the CommonCLI nodes + // (repeaters/chat), so the app's CLI passthrough behaves identically everywhere. + if (memcmp(command, "set max.resend ", 15) == 0) { + int v = atoi(&command[15]); + if (v < 0 || v > 3) { + strcpy(reply, "ERROR: max.resend must be 0-3"); + } else { + _prefs.max_resend_attempts = (uint8_t)v; + savePrefs(); + strcpy(reply, "OK"); + } + return true; + } + if (strcmp(command, "get max.resend") == 0) { + sprintf(reply, "> %d", (uint32_t)_prefs.max_resend_attempts); + if (_prefs.max_resend_attempts) { // plain "> 0" when resends are disabled + StatsFormatHelper::formatResendRatio(reply, getNumResentDirect(), getNumSentDirect()); + } + return true; + } + #ifdef PIN_BUZZER if (strcmp(command, "get buzzer") == 0) { sprintf(reply, "> %s", _prefs.buzzer_quiet ? "off" : "on"); diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index 846708b5326..03744f6551d 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -130,6 +130,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost { float getAirtimeBudgetFactor() const override; int getInterferenceThreshold() const override; bool getCADEnabled() const override; + bool isResendChannelActive() override; int getAGCResetInterval() const override { return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds } @@ -137,6 +138,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost { uint32_t getRetransmitDelay(const mesh::Packet *packet) override; uint32_t getDirectRetransmitDelay(const mesh::Packet *packet) override; uint8_t getExtraAckTransmitCount() const override; + uint8_t getMaxResendAttempts() const override { return _prefs.max_resend_attempts; } bool filterRecvFloodPacket(mesh::Packet* packet) override; bool allowPacketForward(const mesh::Packet* packet) override; diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index 9150cccc951..9e9ca0437ba 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -55,6 +55,7 @@ class NodePrefs : public ConfigSerializer { // persisted to file uint8_t default_scope_key[16]; float tz_offset = 0; float adc_multiplier = 0; // battery ADC multiplier, 0 = board default + uint8_t max_resend_attempts; // 0 = disabled, 1-3, default 2 (repeated sending) #ifdef ENABLE_WIFI_INTERFACE #ifndef WIFI_SSID #define WIFI_SSID "" @@ -287,6 +288,7 @@ class NodePrefs : public ConfigSerializer { // persisted to file //def("f_adv_int", flood_advert_interval); def("lat", node_lat); def("lon", node_lon); + def("max_resend", max_resend_attempts); // repeated sending (feature) def("radio", radio); def("gps", gps); def("repeat", repeat); diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 21f833a588d..f4dc01cc36b 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -60,6 +60,20 @@ #define LAZY_CONTACTS_WRITE_DELAY 5000 +// Fixed RSSI margin (dB above the noise floor) at which a DIRECT resend treats the channel as busy +// (isResendChannelActive(): busy when isReceivingPacket() || currentRSSI - noiseFloor >= this). It is +// the resend-only counterpart of the general-LBT _prefs.interference_threshold, which flows through +// getInterferenceThreshold() -> triggerNoiseFloorCalibrate() -> RadioLibWrapper::isChannelActive(). +// They are separate today only because interference_threshold is blanket-initialised to 0 (disabled; +// see getInterferenceThreshold(), cited there as a currentRSSI() reliability concern). The margin +// currentRSSI-noiseFloor is only as trustworthy as both terms: the median noise-floor estimator +// (PR #2933) stabilises getNoiseFloor() and removes the dominant drift source (the old one-way +// ratchet to -120 that inflated every margin), substantially rehabilitating the threshold path. +// Once interference_threshold is enabled across the fleet, this constant can be superseded by it. +#ifndef RESEND_INTERFERENCE_MARGIN + #define RESEND_INTERFERENCE_MARGIN 12 // dB above noise floor that blocks a resend (non-invasive LBT) +#endif + void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) { #if MAX_NEIGHBOURS // check if neighbours enabled // find existing neighbour, else use least recently updated @@ -553,6 +567,14 @@ uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { return getRNG()->nextInt(0, 5*t + 1); } +bool MyMesh::isResendChannelActive() { + // Non-invasive resend LBT (NO CAD, so RX stays open to overhear the downstream forward + // and cancel the resend). Block when a LoRa preamble/header is being received (often that + // forward) OR the live channel energy is well above the noise floor (foreign interference). + int margin = (int)radio_driver.getCurrentRSSI() - _radio->getNoiseFloor(); + return radio_driver.isReceivingPacket() || (margin >= RESEND_INTERFERENCE_MARGIN); +} + mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) { if (pkt->getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD) { recv_pkt_region = region_map.findMatch(pkt, REGION_DENY_FLOOD); @@ -893,6 +915,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc _prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0; _prefs.tx_delay_factor = 0.5f; // was 0.25f _prefs.direct_tx_delay_factor = 0.3f; // was 0.2 + _prefs.max_resend_attempts = 2; StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name)); _prefs.node_lat = ADVERT_LAT; _prefs.node_lon = ADVERT_LON; @@ -1177,6 +1200,11 @@ void MyMesh::formatPacketStatsReply(char *reply) { getNumRecvFlood(), getNumRecvDirect()); } +void MyMesh::formatResendRatioReply(char *reply) { + if (!_prefs.max_resend_attempts) return; // plain "> 0" when resends are disabled + StatsFormatHelper::formatResendRatio(reply, getNumResentDirect(), getNumSentDirect()); +} + void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) IdentityStore store(*_fs, ""); diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 7e4cfd4f22e..bb4b7e44952 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -150,12 +150,16 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { bool getCADEnabled() const override { return _prefs.cad_enabled; } + bool isResendChannelActive() override; // non-invasive resend LBT (preamble/RSSI, no CAD) int getAGCResetInterval() const override { return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds } uint8_t getExtraAckTransmitCount() const override { return _prefs.multi_acks; } + uint8_t getMaxResendAttempts() const override { + return _prefs.max_resend_attempts; + } #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { @@ -224,6 +228,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { void formatStatsReply(char *reply) override; void formatRadioStatsReply(char *reply) override; void formatPacketStatsReply(char *reply) override; + void formatResendRatioReply(char *reply) override; void startRegionsLoad() override; bool saveRegions() override; void onDefaultRegionChanged(const RegionEntry* r) override; diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index c1a6b09ff07..b41cfb3f96c 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -11,6 +11,20 @@ #define POST_SYNC_DELAY_SECS 6 +// Fixed RSSI margin (dB above the noise floor) at which a DIRECT resend treats the channel as busy +// (isResendChannelActive(): busy when isReceivingPacket() || currentRSSI - noiseFloor >= this). It is +// the resend-only counterpart of the general-LBT _prefs.interference_threshold, which flows through +// getInterferenceThreshold() -> triggerNoiseFloorCalibrate() -> RadioLibWrapper::isChannelActive(). +// They are separate today only because interference_threshold is blanket-initialised to 0 (disabled; +// see getInterferenceThreshold(), cited there as a currentRSSI() reliability concern). The margin +// currentRSSI-noiseFloor is only as trustworthy as both terms: the median noise-floor estimator +// (PR #2933) stabilises getNoiseFloor() and removes the dominant drift source (the old one-way +// ratchet to -120 that inflated every margin), substantially rehabilitating the threshold path. +// Once interference_threshold is enabled across the fleet, this constant can be superseded by it. +#ifndef RESEND_INTERFERENCE_MARGIN + #define RESEND_INTERFERENCE_MARGIN 12 // dB above noise floor that blocks a resend (non-invasive LBT) +#endif + #define FIRMWARE_VER_LEVEL 1 #define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS @@ -298,6 +312,14 @@ uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { return getRNG()->nextInt(0, 5*t + 1); } +bool MyMesh::isResendChannelActive() { + // Non-invasive resend LBT (NO CAD, so RX stays open to overhear the downstream forward + // and cancel the resend). Block when a LoRa preamble/header is being received (often that + // forward) OR the live channel energy is well above the noise floor (foreign interference). + int margin = (int)radio_driver.getCurrentRSSI() - _radio->getNoiseFloor(); + return radio_driver.isReceivingPacket() || (margin >= RESEND_INTERFERENCE_MARGIN); +} + bool MyMesh::allowPacketForward(const mesh::Packet *packet) { if (_prefs.disable_fwd) return false; if (packet->isRouteFlood() @@ -650,6 +672,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc _prefs.rx_delay_base = 0.0f; // off by default, was 10.0 _prefs.tx_delay_factor = 0.5f; // was 0.25f; _prefs.direct_tx_delay_factor = 0.2f; // was zero + _prefs.max_resend_attempts = 2; StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name)); _prefs.node_lat = ADVERT_LAT; _prefs.node_lon = ADVERT_LON; diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 507b42b1aad..c589358dd84 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -149,12 +149,16 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { bool getCADEnabled() const override { return _prefs.cad_enabled; } + bool isResendChannelActive() override; // non-invasive resend LBT (preamble/RSSI, no CAD) int getAGCResetInterval() const override { return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds } uint8_t getExtraAckTransmitCount() const override { return _prefs.multi_acks; } + uint8_t getMaxResendAttempts() const override { + return _prefs.max_resend_attempts; + } mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override; diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index 4916ddcdf0d..1f8c3bf5adf 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -801,6 +801,7 @@ SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::Millise _prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0; _prefs.tx_delay_factor = 0.5f; // was 0.25f _prefs.direct_tx_delay_factor = 0.2f; // was zero + _prefs.max_resend_attempts = 2; StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name)); _prefs.node_lat = ADVERT_LAT; _prefs.node_lon = ADVERT_LON; diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index e6746d470c7..04309a37f2a 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -129,6 +129,7 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks { bool getCADEnabled() const override; int getAGCResetInterval() const override; mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override; + uint8_t getMaxResendAttempts() const override { return _prefs.max_resend_attempts; } void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override; int searchPeersByHash(const uint8_t* hash) override; void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override; diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index c0610b7f8a7..7bb8dba0e57 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -4,6 +4,10 @@ #include #endif +#ifdef MESHCORE_SIMULATOR + #include "sim_context.h" +#endif + #include namespace mesh { @@ -19,6 +23,7 @@ namespace mesh { void Dispatcher::begin() { n_sent_flood = n_sent_direct = 0; n_recv_flood = n_recv_direct = 0; + n_resent_direct = 0; _err_flags = 0; radio_nonrx_start = _ms->getMillis(); @@ -111,8 +116,12 @@ void Dispatcher::loop() { n_sent_flood++; } else { n_sent_direct++; + if (outbound->sending_attempts > 0) n_resent_direct++; // resend TX (subset of n_sent_direct) + } + // allow for possible retransmission for reliability + if (!resendPacket(outbound)) { + releasePacket(outbound); // return to pool } - releasePacket(outbound); // return to pool outbound = NULL; } else if (millisHasNowPassed(outbound_expiry)) { MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): WARNING: outbound packed send timed out!", getLogDateTime()); @@ -223,10 +232,9 @@ void Dispatcher::checkRecv() { pkt->getRawLength(), pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len, (int)pkt->getSNR(), (int)_radio->getLastRSSI(), (int)(score*1000), air_time); - static uint8_t packet_hash[MAX_HASH_SIZE]; - pkt->calculatePacketHash(packet_hash); + pkt->calculatePacketHash(); Serial.print(" hash="); - mesh::Utils::printHex(Serial, packet_hash, MAX_HASH_SIZE); + mesh::Utils::printHex(Serial, pkt->hash, MAX_HASH_SIZE); if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ || pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { @@ -286,7 +294,16 @@ void Dispatcher::checkSend() { } if (!millisHasNowPassed(next_tx_time)) return; - if (_radio->isReceiving()) { + + // Pick the channel-busy check by packet kind. Resends (direct, already attempted) + // use a NON-invasive gate via isResendChannelActive() so the radio stays in RX and + // can still overhear the downstream forward → resend cancellation. First sends keep + // the CAD-based carrier sense (collision avoidance worth the momentary deafness). + Packet* pending = _mgr->peekNextOutbound(_ms->getMillis()); + bool resend_lbt = pending && pending->isRouteDirect() && pending->sending_attempts > 0; + bool channel_busy = resend_lbt ? isResendChannelActive() : _radio->isReceiving(); + + if (channel_busy) { if (cad_busy_start == 0) { cad_busy_start = _ms->getMillis(); // record when CAD busy state started } @@ -298,7 +315,22 @@ void Dispatcher::checkSend() { // channel activity has gone on too long... (Radio might be in a bad state) // force the pending transmit below... } else { - next_tx_time = futureMillis(getCADFailRetryDelay()); + if (resend_lbt) { + // Deterministic per-hop busy-recheck bucket for DIRECT resends (Problem 1: when an + // interferer clears, deferred resenders that share a re-check cadence fire on the same + // loop tick and collide). getPathHashCount() decreases by exactly 1 each forwarding hop, + // so the originator and every relay of the SAME direct packet carry distinct counts + // (N, N-1, N-2, ...). count%3 therefore assigns mutually-in-range chain neighbours to + // different re-check cadences: any two nodes <=2 hops apart differ by <=2 in count, hence + // are always distinct mod 3. The only same-bucket pairs are >=3 hops apart and do not + // radio-interfere. Same {120,240,360} ms range as the random draw -> no latency inflation, + // and NOT a quiet-dwell-style window-inflating stagger (the cancel window in + // resendPacket() is untouched). First sends keep the randomized getCADFailRetryDelay(). + uint32_t pos = pending->getPathHashCount() % 3; + next_tx_time = futureMillis((pos + 1) * 120); + } else { + next_tx_time = futureMillis(getCADFailRetryDelay()); + } return; } } @@ -340,8 +372,8 @@ void Dispatcher::checkSend() { #if MESH_PACKET_LOGGING Serial.print(getLogDateTime()); - Serial.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d)", - len, outbound->getPayloadType(), outbound->isRouteDirect() ? "D" : "F", outbound->payload_len); + Serial.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d, attempt=%d)", len, + outbound->getPayloadType(), outbound->isRouteDirect() ? "D" : "F", outbound->payload_len, outbound->sending_attempts); if (outbound->getPayloadType() == PAYLOAD_TYPE_PATH || outbound->getPayloadType() == PAYLOAD_TYPE_REQ || outbound->getPayloadType() == PAYLOAD_TYPE_RESPONSE || outbound->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { Serial.printf(" [%02X -> %02X]\n", (uint32_t)outbound->payload[1], (uint32_t)outbound->payload[0]); @@ -384,7 +416,73 @@ bool Dispatcher::millisHasNowPassed(unsigned long timestamp) const { } unsigned long Dispatcher::futureMillis(int millis_from_now) const { - return _ms->getMillis() + millis_from_now; + unsigned long wake_time = _ms->getMillis() + millis_from_now; +#ifdef MESHCORE_SIMULATOR // Register wake time with simulator for accurate scheduling + if (auto *ctx = SIM_CTX()) { + ctx->wake_registry.registerWakeTime(wake_time); + } +#endif + return wake_time; +} + +bool Dispatcher::resendPacket(mesh::Packet *packet) { + if (packet->no_retry) return false; // e.g. last-hop TRACE: no retransmission from this hop + + // Pool-shedding: under low free-pool pressure, do NOT queue a resend. A resend consumes a + // pool slot shared with RX (allocNew in checkRecv); under sustained load it would exhaust + // the pool and deafen this node. The PRIMARY direct forward already transmitted, so shedding + // here only drops the REDUNDANT retry. + if (_mgr->getFreeCount() <= POOL_SHED_FREE_THRESHOLD) { + return false; + } + + // prepare error correction via potential retransmit: + // re-send only direct routed packets that carry at least one relay hash, so that a + // downstream relay's forward can be overheard to cancel this re-send. + // The final relay hop (path empty after removeSelfFromPath, flagged via + // final_hop_ack_resend) is the exception: there is no downstream forward to overhear, but + // the destination ACKs receipt. Allow exactly one resend there (sending_attempts == 0), + // cancellable by the returning ACK (see Mesh::cancelPendingFinalHopResend). + if (packet->isRouteDirect() && packet->sending_attempts < getMaxResendAttempts() && + (packet->getPathHashCount() > 0 || + (packet->final_hop_ack_resend && packet->sending_attempts == 0))) { + packet->sending_attempts++; + + MESH_DEBUG_PRINTLN("Dispatcher::resendPacket %s attempt=%d", packet->getHashHex(), + packet->sending_attempts); + + // Resend window: W = C0 + K*airtime + margin + (attempt-1)*jitter. C0 (RESEND_DEFERRAL_FIXED_MS) + // is a static, SF-independent constant calibrated to the measured fixed forward-latency + // component; the K*airtime term carries the SF-dependence (airtime comes from getEstAirtimeFor(), + // i.e. from the configured SF/BW/CR), so the window tracks the spreading factor automatically — + // no per-regime constant. The K*airtime term already covers the downstream relay's forward TX + // and its getDirectRetransmitDelay() spread, so the originator's OWN direct-retransmit delay is + // NOT added on top here — it would only inflate the wait without covering any forward-latency + // component. getDirectRetransmitDelay() stays in force for actual relaying (see Mesh.cpp). + uint32_t packet_airtime_ms = _radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2); + uint32_t cancel_window = resendWindowFor((uint16_t)packet_airtime_ms, packet->sending_attempts); + _mgr->queueOutbound(packet, 1, futureMillis((int)cancel_window)); + return true; + } + + return false; +} + +// W = C0 + K*airtime + margin + (attempt-1)*jitter, capped at an AIRTIME-PROPORTIONAL ceiling +// (CAP_BASE + CAP_AIRTIME_X*airtime). C0 = RESEND_DEFERRAL_FIXED_MS (static); the K*airtime term +// carries SF-dependence. The ceiling grows with airtime (CAP_AIRTIME_X >= K) so it never clips the +// formula's K*airtime term for long packets / high SF — the earlier flat 1500 ms ceiling under-covered +// SF10 long messages (downstream forward alone is ~2-3 s), so the resend fired before the forward +// could cancel it and every long message picked up a redundant, colliding resend. +uint32_t Dispatcher::resendWindowFor(uint16_t airtime_ms, uint8_t sending_attempts) const { + uint32_t w = (uint32_t)RESEND_DEFERRAL_FIXED_MS + + (uint32_t)airtime_ms * RESEND_DEFERRAL_AIRTIME_X + + RESEND_DEFERRAL_MARGIN_MS + + (uint32_t)(sending_attempts - 1) * RESEND_BACKOFF_JITTER_MS; + uint32_t cap = (uint32_t)RESEND_DEFERRAL_CAP_BASE_MS + + (uint32_t)airtime_ms * RESEND_DEFERRAL_CAP_AIRTIME_X; + if (w > cap) w = cap; + return w; } } \ No newline at end of file diff --git a/src/Dispatcher.h b/src/Dispatcher.h index aad6cba3ec2..7922fe25d84 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -91,6 +91,7 @@ class PacketManager { virtual void queueOutbound(Packet* packet, uint8_t priority, uint32_t scheduled_for) = 0; virtual Packet* getNextOutbound(uint32_t now) = 0; // by priority + virtual Packet* peekNextOutbound(uint32_t now) { return NULL; } // same as getNextOutbound but non-consuming (default: none) virtual int getOutboundCount(uint32_t now) const = 0; virtual int getOutboundTotal() const = 0; virtual int getFreeCount() const = 0; @@ -111,12 +112,70 @@ typedef uint32_t DispatcherAction; #define ERR_EVENT_CAD_TIMEOUT (1 << 1) #define ERR_EVENT_STARTRX_TIMEOUT (1 << 2) +// Pool-shedding backpressure: when the free packet pool (slots available for allocNew/RX) drops +// to or below this threshold, skip DIRECT resends (resendPacket) to protect RX liveness. The +// PRIMARY direct forward is never shed (different path) — only redundant resends. Tune per pool +// size (repeaters=32, companions=16); default covers a short RX burst. +#ifndef POOL_SHED_FREE_THRESHOLD + #define POOL_SHED_FREE_THRESHOLD 6 +#endif + +// Resend window model. The resend must wait for the DOWNSTREAM relay's forward to arrive and +// cancel it via isRetryMatch. The forward latency decomposes into: +// - a fixed part: relay processing + LBT/CAD-retry (in 120ms units) + optional dwell + scheduling, +// - an airtime-proportional part: the relay's forward TX (~1x airtime) + its retransmit_delay. +// The window is therefore modelled as: W = C0 + K*airtime + margin + (attempt-1)*jitter +// C0 (RESEND_DEFERRAL_FIXED_MS) is a STATIC value calibrated from observer runs — the measured +// fixed forward-latency component, SF-INDEPENDENT because the SF-dependent forward TX airtime is +// carried entirely by the K*airtime term (getEstAirtimeFor already encodes the configured SF/BW/CR). +// So one formula covers any spreading factor without a per-regime constant or an SF branch. The +// former flat 5x-airtime multiplier could not represent the fixed part and under-covered it at low SF +// (SF5/SF8), where airtime is only a few ms while the fixed relay deferral is ~600 ms. (An earlier +// runtime-adaptive C via EWMA on overheard forwards was removed as over-engineering: the forward +// latency is stable, so the EWMA converged to the same value a correctly-set static C0 gives.) +#ifndef RESEND_DEFERRAL_FIXED_MS + // C0: fixed (airtime-INDEPENDENT) forward-latency component = relay processing + CAD-retry + + // quiet-dwell gate + scheduling. Recalibrated for SF10 + quiet-dwell from the long-message HW A/B + // (feature/repeated-sending-3, 2026-07-21): prompt-forward latency ~2.85 s minus the 2*airtime term + // (~1.56 s for a 160 B / ~780 ms-airtime packet) leaves a fixed residual of ~1.29 s median, with the + // prompt-forward cluster reaching ~1.76 s. The prior 600 ms was calibrated on SF8 observer runs and + // under-covered SF10 long messages, where the downstream forward alone takes 2-3 s — so the resend + // became eligible before the forward could cancel it and every long message picked up a redundant, + // colliding resend. 1500 ms covers the prompt-forward regime; a heavily congested repeater (forward + // pushed to 5-10 s) will still race, but that is a channel-load problem, not a window-sizing one. + #define RESEND_DEFERRAL_FIXED_MS 1500 +#endif +#ifndef RESEND_DEFERRAL_AIRTIME_X + #define RESEND_DEFERRAL_AIRTIME_X 2 // K: forward TX (1x) + mean downstream retransmit_delay (~1x), expressed in airtimes +#endif +#ifndef RESEND_DEFERRAL_MARGIN_MS + #define RESEND_DEFERRAL_MARGIN_MS 40 // extra safety for scheduling jitter / latency variance +#endif +// Airtime-PROPORTIONAL ceiling on W, replacing the earlier flat RESEND_DEFERRAL_MAX_MS (=1500). The +// flat cap clipped the formula's K*airtime term for any payload > ~85 B at SF10, so long messages were +// pinned at 1500 ms regardless of length — below even the fastest observed forward latency. The +// ceiling now grows with airtime: CAP_BASE + CAP_AIRTIME_X*airtime. CAP_AIRTIME_X (3) >= K (2), so the +// ceiling always sits above the formula for realistic packets and the formula governs; it only bounds +// genuinely pathological cases (very high SF / extreme airtime) to keep recovery latency finite. +#ifndef RESEND_DEFERRAL_CAP_BASE_MS + #define RESEND_DEFERRAL_CAP_BASE_MS 1500 // ceiling floor (tiny-packet / low-SF case) +#endif +#ifndef RESEND_DEFERRAL_CAP_AIRTIME_X + #define RESEND_DEFERRAL_CAP_AIRTIME_X 3 // ceiling slope in airtimes (>= K so it never under-covers) +#endif +#ifndef RESEND_BACKOFF_JITTER_MS + #define RESEND_BACKOFF_JITTER_MS 100 // per-attempt stretch (attempt-1)*100 - the light stretch for 2nd/3rd attempt +#endif + /** * \brief The low-level task that manages detecting incoming Packets, and the queueing * and scheduling of outbound Packets. */ class Dispatcher { +protected: Packet* outbound; // current outbound packet + +private: unsigned long outbound_expiry, outbound_start, total_air_time, rx_air_time; unsigned long next_tx_time; unsigned long cad_busy_start; @@ -125,10 +184,14 @@ class Dispatcher { bool prev_isrecv_mode; uint32_t n_sent_flood, n_sent_direct; uint32_t n_recv_flood, n_recv_direct; + uint32_t n_resent_direct; // subset of n_sent_direct that were resend TXs (sending_attempts>0) unsigned long tx_budget_ms; unsigned long last_budget_update; unsigned long duty_cycle_window_ms; + // W = C0 + K*airtime + margin + (attempt-1)*jitter, with C0 = RESEND_DEFERRAL_FIXED_MS (static). + uint32_t resendWindowFor(uint16_t airtime_ms, uint8_t sending_attempts) const; + void processRecvPacket(Packet* pkt); void updateTxBudget(); @@ -169,9 +232,19 @@ class Dispatcher { virtual uint32_t getCADFailMaxDuration() const; virtual int getInterferenceThreshold() const { return 0; } // disabled by default virtual bool getCADEnabled() const { return false; } // hardware CAD disabled by default + // Channel-busy check used ONLY for resend TX gating (direct packets with sending_attempts>0). + // Default falls back to CAD carrier sense; examples override with a NON-invasive check + // (preamble/header IRQ + RSSI margin) so RX stays open to overhear the downstream forward + // and cancel the resend. Non-const because _radio->isReceiving() is non-const. + virtual bool isResendChannelActive() { return _radio->isReceiving(); } virtual int getAGCResetInterval() const { return 0; } // disabled by default virtual unsigned long getDutyCycleWindowMs() const { return 3600000; } + /** + * \returns maximum number of direct-route resend attempts (0 = disabled, default = 2, max = 3). + */ + virtual uint8_t getMaxResendAttempts() const { return 2; } + public: void begin(); void loop(); @@ -179,6 +252,21 @@ class Dispatcher { Packet* obtainNewPacket(); void releasePacket(Packet* packet); void sendPacket(Packet* packet, uint8_t priority, uint32_t delay_millis=0); + /** + * \brief re-send the given packet (for retransmission) if conditions apply. + * \return true, if packet was re-sent. + */ + bool resendPacket(Packet *packet); + + /** + * \returns number of milliseconds delay to apply to retransmitting the given packet. + */ + virtual uint32_t getRetransmitDelay(const Packet *packet) { return 0; }; + + /** + * \returns number of milliseconds delay to apply to retransmitting the given packet, for DIRECT mode. + */ + virtual uint32_t getDirectRetransmitDelay(const Packet *packet) { return 0; }; unsigned long getTotalAirTime() const { return total_air_time; } unsigned long getReceiveAirTime() const {return rx_air_time; } @@ -187,8 +275,12 @@ 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; } + // resend TXs (subset of n_sent_direct): direct-route transmissions with sending_attempts>0, i.e. + // re-sends rather than first/forward sends. Reported as n_resends / n_sent_direct. Last-hop TRACE + // forwards are flagged no_retry (Mesh.cpp) and count as first sends. + uint32_t getNumResentDirect() const { return n_resent_direct; } void resetStats() { - n_sent_flood = n_sent_direct = n_recv_flood = n_recv_direct = 0; + n_sent_flood = n_sent_direct = n_recv_flood = n_recv_direct = n_resent_direct = 0; _err_flags = 0; } diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 9dc42f0b04b..16e7da158b6 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -41,6 +41,34 @@ int Mesh::searchChannelsByHash(const uint8_t* hash, GroupChannel channels[], int DispatcherAction Mesh::onRecvPacket(Packet* pkt) { if (pkt->isRouteDirect() && pkt->getPayloadType() == PAYLOAD_TYPE_TRACE) { if (pkt->path_len < MAX_PATH_SIZE) { + // The generic resend-cancel block further down is never reached for TRACE (this branch always + // returns). Do the same here: when a later hop's forward of a TRACE we already sent is overheard + // (isRetryMatch: same payload, longer SNR list with ours as prefix), drop our queued resend. + // Only for well-formed TRACE (plain SNR count, whole route entries, no more SNRs than hops) and + // only for non-terminal legs (a terminal-leg resend is never queued, see below). + // 'outbound' is not touched; the received packet is processed normally afterwards. + if ((pkt->path_len & 0xC0) == 0 && pkt->payload_len >= 9 && _mgr->getOutboundTotal() > 0) { + uint8_t sz = pkt->payload[8] & 0x03; + uint16_t route_len = pkt->payload_len - 9; + if ((route_len % (1u << sz)) == 0 && ((uint16_t)pkt->path_len << sz) <= route_len) { + for (int qi = _mgr->getOutboundTotal() - 1; qi >= 0; qi--) { + Packet *q = _mgr->getOutboundByIdx(qi); + if (!q || q->sending_attempts == 0 || !q->isRouteDirect()) continue; + if (q->getPayloadType() != PAYLOAD_TYPE_TRACE || q->payload_len < 9) continue; + uint8_t qsz = q->payload[8] & 0x03; + if (((uint16_t)(q->path_len & 0x3F) << qsz) >= (uint16_t)(q->payload_len - 9)) continue; // terminal leg + if (pkt->isRetryMatch(q)) { + Packet *removed = _mgr->removeOutboundByIdx(qi); + if (removed) { + MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): downstream forwarded queued TRACE resend, canceling", + getLogDateTime()); + _mgr->free(removed); + } + break; + } + } + } + } uint8_t i = 0; uint32_t trace_tag; memcpy(&trace_tag, &pkt->payload[i], 4); i += 4; @@ -60,6 +88,17 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { // append SNR (Not hash!) pkt->path[pkt->path_len++] = (int8_t) (pkt->getSNR()*4); + // Last forwarding hop before the TRACE destination: the destination does not forward, + // so a retry could never be cancelled by overhearing a downstream forward. To avoid + // flooding the destination, send it once without retransmissions from this hop. + // Higher layers or the sender must retry the entire TRACE if needed. + // no_retry instead of pre-setting sending_attempts = max: a pre-set counter made the + // first send look like a resend, so it went through the resend LBT gate (IRQ or RSSI + // margin over the noise floor) instead of the normal one and was counted as a resend. + if (offset + (1 << path_sz) >= len) { + pkt->no_retry = true; + } + uint32_t d = getDirectRetransmitDelay(pkt); return ACTION_RETRANSMIT_DELAYED(5, d); // schedule with priority 5 (for now), maybe make configurable? } @@ -75,7 +114,66 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { return ACTION_RELEASE; } - if (pkt->isRouteDirect() && pkt->getPathHashCount() > 0) { + if (pkt->isRouteDirect()) { + const bool is_next_hop = (pkt->getPathHashCount() > 0) && self_id.isHashMatch(pkt->path, pkt->getPathHashSize()); + + // Only do this when the packet is NOT addressed to us as next hop to avoid dropping fresh packets. + if (!is_next_hop) { + // Check if this is a retransmit of a packet we recently sent; + // if yes, the next hop successfully forwarded it, so remove our scheduled retransmit from outbound queue. + // This runs for ANY path_len (including 0 = downstream relay forwarding to final destination), + // so that we cancel pending retries as soon as we overhear a relay forwarding our packet. + // + // For TRACE packets we cannot use the packet hash, because TRACE appends SNR bytes + // and increments path_len per hop, which changes the hash. Packet::isRetryMatch() + // handles this by comparing payload and SNR prefix for TRACE, and hash for all others. + + // First check the current outbound packet being prepared/sent + if (outbound && outbound->sending_attempts > 0 && outbound->isRouteDirect()) { + if (pkt->isRetryMatch(outbound)) { + MESH_DEBUG_PRINTLN( + "%s Mesh::onRecvPacket(): downstream forwarded current outbound, canceling (attempt=%d)", + getLogDateTime(), outbound->sending_attempts); + releasePacket(outbound); + outbound = NULL; + return ACTION_RELEASE; + } + } + + if (_mgr->getOutboundTotal() > 0) { + for (int i = _mgr->getOutboundTotal() - 1; i >= 0; i--) { + Packet *queued_pkt = _mgr->getOutboundByIdx(i); + if (queued_pkt && queued_pkt->sending_attempts > 0 && queued_pkt->isRouteDirect()) { + if (pkt->isRetryMatch(queued_pkt)) { + MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): downstream forwarded packet detected, canceling " + "retransmit (attempt=%d)", + getLogDateTime(), queued_pkt->sending_attempts); + Packet *removed = _mgr->removeOutboundByIdx(i); + if (removed) _mgr->free(removed); + return ACTION_RELEASE; // don't process further: confirmed successful forwarding + } + } + } + } + + } + + // For direct packets with no relay hashes (zero-hop, addressed straight to us), + // no further path-based processing applies. + // A destination's receipt-ACK for a TXT_MSG we delivered as the final relay hop arrives + // exactly this way: zero-path direct, because the destination ACKs its immediate + // neighbour (it has no usable multi-hop return path to the originator, so the ACK only + // travels one hop). The ACK-relay branch below is gated on is_next_hop, which requires + // getPathHashCount() > 0 — unreachable for such an ACK. Cancel any pending final-hop + // resend here, before the early-exit, so the resend does not fire after a delivery the + // destination has already acknowledged. + if (pkt->getPathHashCount() == 0) { + if (pkt->getPayloadType() == PAYLOAD_TYPE_ACK) { + cancelPendingFinalHopResend(); + } + goto direct_path_done; + } + // check for 'early received' ACK if (pkt->getPayloadType() == PAYLOAD_TYPE_ACK) { int i = 0; @@ -86,11 +184,17 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { } } - if (self_id.isHashMatch(pkt->path, pkt->getPathHashSize()) && allowPacketForward(pkt)) { + if (is_next_hop && allowPacketForward(pkt)) { uint32_t d = getDirectRetransmitDelay(pkt); if (pkt->getPayloadType() == PAYLOAD_TYPE_MULTIPART) { return forwardMultipartDirect(pkt, d); } else if (pkt->getPayloadType() == PAYLOAD_TYPE_ACK) { + // This ACK is addressed back along the return path with us as the next hop, i.e. it + // transits back through the very relay that delivered the original message. That is + // proof the destination received it: cancel any pending final-hop resend so we do not + // needlessly retransmit to the destination. + cancelPendingFinalHopResend(); + if (!_tables->wasSeen(pkt)) { // don't retransmit! _tables->markSeen(pkt); removeSelfFromPath(pkt); @@ -101,14 +205,30 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { if (!_tables->wasSeen(pkt)) { _tables->markSeen(pkt); + + // Detect the final relay hop: only our own hash remained in the path, so after + // removeSelfFromPath() the destination is reached implicitly (it lives in the payload + // dest_hash, not as a relay hash) and getPathHashCount() becomes 0. The normal resend + // guard (getPathHashCount() > 0) would then suppress any retransmit, leaving a lost + // final-hop TX unrecoverable at the relay layer. TXT_MSG destinations ACK receipt, so + // mark this packet to allow exactly one ACK-cancellable resend (see resendPacket() and + // cancelPendingFinalHopResend()): the resend self-cancels on the returning ACK, so the + // destination is not flooded. + if (pkt->getPathHashCount() == 1 && pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { + pkt->final_hop_ack_resend = true; + } + removeSelfFromPath(pkt); - return ACTION_RETRANSMIT_DELAYED(0, d); // Routed traffic is HIGHEST priority + MESH_DEBUG_PRINTLN("Mesh::onRecvPacket(): prepare to repeat packet %s", pkt->getHashHex()); + + 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. } +direct_path_done: if (pkt->isRouteFlood() && filterRecvFloodPacket(pkt)) return ACTION_RELEASE; DispatcherAction action = ACTION_RELEASE; @@ -341,6 +461,34 @@ void Mesh::removeSelfFromPath(Packet* pkt) { } } +void Mesh::cancelPendingFinalHopResend() { + // The destination has ACKed receipt. ACKs are produced in delivery order and (for the + // direct zero-path ACKs a companion sends) travel only one hop back to us, so the OLDEST + // pending final-hop resend corresponds to the message this ACK acknowledges (FIFO). Cancel + // exactly one — the earliest still-queued resend. + // + // The sending_attempts > 0 guard is essential: it selects only actual resends (the attempt + // counter is bumped in resendPacket() before re-queueing) and never the original final-hop + // forward, which is also flagged final_hop_ack_resend while it sits in the queue waiting to + // be TXed. Without it, an ACK arriving in that brief pre-TX window would cancel the delivery + // itself. (send_queue.add() appends, so index 0 is the oldest entry.) + // + // An in-flight resend (the 'outbound' packet, already on-air) is deliberately NOT touched: + // aborting a mid-TX send risks radio state, and the duplicate it delivers is harmless (the + // destination dedups via wasSeen). Only queued, not-yet-sent resends are cancelled. + for (int i = 0; i < _mgr->getOutboundTotal(); i++) { + Packet* queued = _mgr->getOutboundByIdx(i); + if (queued && queued->final_hop_ack_resend && queued->sending_attempts > 0 && + queued->isRouteDirect() && queued->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) { + MESH_DEBUG_PRINTLN("%s Mesh::cancelPendingFinalHopResend(): ACK heard, canceling oldest " + "final-hop resend (queued) %s", getLogDateTime(), queued->getHashHex()); + Packet* removed = _mgr->removeOutboundByIdx(i); + if (removed) _mgr->free(removed); + return; + } + } +} + DispatcherAction Mesh::routeRecvPacket(Packet* packet) { uint8_t n = packet->getPathHashCount(); if (packet->isRouteFlood() && !packet->isMarkedDoNotRetransmit() diff --git a/src/Mesh.h b/src/Mesh.h index 22bb5c6d592..4ae3bf2d789 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -31,6 +31,7 @@ class Mesh : public Dispatcher { void removeSelfFromPath(Packet* packet); void routeDirectRecvAcks(Packet* packet, uint32_t delay_millis); + void cancelPendingFinalHopResend(); // drop a queued final-hop resend when the destination's ACK transits back //void routeRecvAcks(Packet* packet, uint32_t delay_millis); DispatcherAction forwardMultipartDirect(Packet* pkt, uint32_t delay_millis); @@ -59,12 +60,12 @@ class Mesh : public Dispatcher { /** * \returns number of milliseconds delay to apply to retransmitting the given packet. */ - virtual uint32_t getRetransmitDelay(const Packet* packet); + virtual uint32_t getRetransmitDelay(const Packet* packet) override; /** * \returns number of milliseconds delay to apply to retransmitting the given packet, for DIRECT mode. */ - virtual uint32_t getDirectRetransmitDelay(const Packet* packet); + virtual uint32_t getDirectRetransmitDelay(const Packet* packet) override; /** * \returns number of extra (Direct) ACK transmissions wanted. diff --git a/src/Packet.cpp b/src/Packet.cpp index aad3e2f48ee..d175e3e99d6 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -1,13 +1,21 @@ #include "Packet.h" +#include "Utils.h" #include #include namespace mesh { +static const uint8_t ZERO_HASH[MAX_HASH_SIZE] = { 0 }; + Packet::Packet() { header = 0; path_len = 0; payload_len = 0; + memcpy(hash, ZERO_HASH, MAX_HASH_SIZE); + memset(hash_hex, 0, sizeof(hash_hex)); + sending_attempts = 0; + no_retry = false; + final_hop_ack_resend = false; } bool Packet::isValidPathLen(uint8_t path_len) { @@ -38,15 +46,50 @@ int Packet::getRawLength() const { return 2 + getPathByteLen() + payload_len + (hasTransportCodes() ? 4 : 0); } -void Packet::calculatePacketHash(uint8_t* hash) const { - SHA256 sha; - uint8_t t = getPayloadType(); - sha.update(&t, 1); - if (t == PAYLOAD_TYPE_TRACE) { - sha.update(&path_len, sizeof(path_len)); // CAVEAT: TRACE packets can revisit same node on return path +uint8_t *Packet::calculatePacketHash() const { + if (memcmp(this->hash, ZERO_HASH, MAX_HASH_SIZE) == 0) { + SHA256 sha; + uint8_t t = getPayloadType(); + sha.update(&t, 1); + if (t == PAYLOAD_TYPE_TRACE) { + sha.update(&path_len, sizeof(path_len)); // CAVEAT: TRACE packets can revisit same node on return path + } + sha.update(payload, payload_len); + sha.finalize((uint8_t *)this->hash, MAX_HASH_SIZE); + } + return (uint8_t *)this->hash; +} + +const char* Packet::getHashHex() const { + calculatePacketHash(); + Utils::toHex(hash_hex, hash, MAX_HASH_SIZE); + return hash_hex; +} + +bool Packet::isRetryMatch(const Packet* outbound) const { + // Only packets of the same payload type can be retries of each other. + if (this->getPayloadType() != outbound->getPayloadType()) return false; + + // TRACE packets append an SNR byte to path[] and increment path_len per hop. + // A forwarded TRACE therefore has the same payload and a longer path with the + // previous hop's path as a prefix. Accept any downstream hop, not just the + // immediate next hop, so retries are canceled as soon as we overhear a later + // forward of the same TRACE. + if (this->getPayloadType() == PAYLOAD_TYPE_TRACE) { + if (this->payload_len != outbound->payload_len) return false; + if (memcmp(this->payload, outbound->payload, this->payload_len) != 0) return false; + if (this->path_len <= outbound->path_len) return false; + if (outbound->path_len > 0 && + memcmp(this->path, outbound->path, outbound->path_len) != 0) { + return false; + } + return true; } - sha.update(payload, payload_len); - sha.finalize(hash, MAX_HASH_SIZE); + + // Default behaviour for all other payload types: compare cached hashes. + const uint8_t* h1 = this->calculatePacketHash(); + const uint8_t* h2 = outbound->calculatePacketHash(); + return memcmp(h1, h2, MAX_HASH_SIZE) == 0; } uint8_t Packet::writeTo(uint8_t dest[]) const { diff --git a/src/Packet.h b/src/Packet.h index c19d9e9d8fc..0b9b0dd95ec 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -49,12 +49,30 @@ class Packet { uint8_t path[MAX_PATH_SIZE]; uint8_t payload[MAX_PACKET_PAYLOAD]; int8_t _snr; + uint8_t hash[MAX_HASH_SIZE]; + mutable char hash_hex[MAX_HASH_SIZE * 2 + 1]; + uint8_t sending_attempts; + bool no_retry; // runtime-only: never queue a resend for this packet (last-hop TRACE) + bool final_hop_ack_resend; // runtime-only: set when this node forwards the final relay hop + // of an ACK-producing direct packet (e.g. TXT_MSG). Allows exactly + // one ACK-cancellable resend from resendPacket(); not wire-serialized. /** * \brief calculate the hash of payload + type - * \param dest_hash destination to store the hash (must be MAX_HASH_SIZE bytes) + * \return hash pointer */ - void calculatePacketHash(uint8_t* dest_hash) const; + uint8_t *calculatePacketHash() const; + + /** + * \brief Check if this received packet is a downstream forwarding of the + * supplied outbound packet, so that a pending retransmit can be cancelled. + * For TRACE packets the payload and SNR path prefix are compared, + * because TRACE changes path_len (and thus its hash) at every hop. + * For all other types the cached packet hash is compared. + */ + bool isRetryMatch(const Packet* outbound) const; + + const char* getHashHex() const; /** * \returns one of ROUTE_ values diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 58c08deaa3f..07a9f3a7449 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -847,7 +847,7 @@ void BaseChatMesh::resetPathTo(ContactInfo& recipient) { recipient.out_path_len = OUT_PATH_UNKNOWN; } -static ContactInfo* table; // pass via global :-( +static thread_local ContactInfo *table; // pass via global (thread_local for simulation) static int cmp_adv_timestamp(const void *a, const void *b) { int a_idx = *((int *)a); diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 64fed99b34d..4a4e1d24a01 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -102,7 +102,8 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { // Legacy file.read((uint8_t *)&_prefs->flood_max_advert, sizeof(_prefs->flood_max_advert)); // 292 file.read((uint8_t *)&_prefs->radio_fem_rxgain, sizeof(_prefs->radio_fem_rxgain)); // 293 file.read((uint8_t *)&_prefs->cad_enabled, sizeof(_prefs->cad_enabled)); // 294 - // next: 295 + file.read((uint8_t *)&_prefs->max_resend_attempts, sizeof(_prefs->max_resend_attempts)); // 295 + // next: 296 // sanitise bad pref values _prefs->rx_delay_base = constrain(_prefs->rx_delay_base, 0, 20.0f); @@ -135,6 +136,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { // Legacy _prefs->radio_fem_rxgain = constrain(_prefs->radio_fem_rxgain, 0, 1); // boolean _prefs->radio_fem_txgain = constrain(_prefs->radio_fem_txgain, 0, 1); // boolean _prefs->cad_enabled = constrain(_prefs->cad_enabled, 0, 1); // boolean + _prefs->max_resend_attempts = constrain(_prefs->max_resend_attempts, 0, 3); file.close(); } @@ -442,8 +444,16 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* reply) { const char* config = &command[4]; - - if (memcmp(config, "allow.read.only ", 16) == 0) { + if (memcmp(config, "max.resend ", 11) == 0) { + int v = atoi(&config[11]); + if (v < 0 || v > 3) { + strcpy(reply, "ERROR: max.resend must be 0-3"); + } else { + _prefs->max_resend_attempts = (uint8_t)v; + savePrefs(); + strcpy(reply, "OK"); + } + } else if (memcmp(config, "allow.read.only ", 16) == 0) { _prefs->allow_read_only = memcmp(&config[16], "on", 2) == 0; savePrefs(); strcpy(reply, "OK"); @@ -675,7 +685,10 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* reply) { const char* config = &command[4]; - if (memcmp(config, "allow.read.only", 15) == 0) { + if (memcmp(config, "max.resend", 10) == 0) { + sprintf(reply, "> %d", (uint32_t) _prefs->max_resend_attempts); + _callbacks->formatResendRatioReply(reply + strlen(reply)); + } else if (memcmp(config, "allow.read.only", 15) == 0) { sprintf(reply, "> %s", _prefs->allow_read_only ? "on" : "off"); } else if (memcmp(config, "flood.advert.interval", 21) == 0) { sprintf(reply, "> %d", ((uint32_t) _prefs->flood_advert_interval)); diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 24b1fe26bd1..7b8814b73b5 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -75,6 +75,7 @@ class NodePrefs : public ConfigSerializer { uint8_t radio_fem_txgain = 0; // LoRa FEM TX gain setting uint8_t path_hash_mode = 0; // which path mode to use when sending uint8_t loop_detect = 0; + uint8_t max_resend_attempts; // 0 = disabled, 1-3, default 2 (repeated sending) uint8_t cad_enabled = 0; // hardware Channel Activity Detection before TX (boolean) uint8_t extra_sf[4]; @@ -252,6 +253,7 @@ class NodePrefs : public ConfigSerializer { def("f_adv_int", flood_advert_interval); def("lat", node_lat); def("lon", node_lon); + def("max_resend", max_resend_attempts); // repeated sending (feature) def("radio", radio); def("bridge", bridge); def("gps", gps); @@ -299,6 +301,9 @@ class CommonCLICallbacks { virtual void formatStatsReply(char *reply) = 0; virtual void formatRadioStatsReply(char *reply) = 0; virtual void formatPacketStatsReply(char *reply) = 0; + // Appends the resend-ratio suffix (", resends N/M (P%)") to the max.resend get-reply. Default is a + // no-op so wrappers without Dispatcher access keep the plain "> N" reply. + virtual void formatResendRatioReply(char *reply) { } virtual mesh::LocalIdentity& getSelfId() = 0; virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0; virtual void clearStats() = 0; diff --git a/src/helpers/SimpleMeshTables.h b/src/helpers/SimpleMeshTables.h index 956f36faa6e..487a5748ac2 100644 --- a/src/helpers/SimpleMeshTables.h +++ b/src/helpers/SimpleMeshTables.h @@ -7,16 +7,21 @@ #endif #define MAX_PACKET_HASHES (128+32) +#define MAX_PACKET_ACKS 64 class SimpleMeshTables : public mesh::MeshTables { uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE]; int _next_idx; + uint32_t _acks[MAX_PACKET_ACKS]; + int _next_ack_idx; uint32_t _direct_dups, _flood_dups; public: SimpleMeshTables() { memset(_hashes, 0, sizeof(_hashes)); _next_idx = 0; + memset(_acks, 0, sizeof(_acks)); + _next_ack_idx = 0; _direct_dups = _flood_dups = 0; } @@ -32,12 +37,46 @@ class SimpleMeshTables : public mesh::MeshTables { #endif bool wasSeen(const mesh::Packet* packet) override { - uint8_t hash[MAX_HASH_SIZE]; - packet->calculatePacketHash(hash); + // ACK packets receive dedicated deduplication for three reasons: + // + // 1. The first 4 payload bytes (ack_crc) already uniquely identify the ACK – they + // are the CRC of the original packet being acknowledged. A direct uint32_t + // comparison is therefore both correct and far cheaper than computing a full + // SHA256 via calculatePacketHash() (which the general path below would do). + // + // 2. With repeated sending (max_resend_attempts > 0) and multi-ACKs the same ACK + // can arrive multiple times. Storing each occurrence in the shared _hashes[] + // table (160 entries) would evict entries needed for long-lived flood-packet + // deduplication. The dedicated _acks[] table (MAX_PACKET_ACKS entries) keeps + // ACK and flood deduplication budgets separate. + // + // 3. clear() for ACKs is a simple 4-byte linear scan; using the general table + // would require SHA256 + a full memcmp loop over MAX_PACKET_HASHES entries. + // + // Note: wasSeen() is a pure predicate and does NOT insert. Every miss must be + // followed by markSeen() at the call site to record the packet (see Mesh.cpp). + if (packet->getPayloadType() == PAYLOAD_TYPE_ACK) { + uint32_t ack; + memcpy(&ack, packet->payload, 4); + for (int i = 0; i < MAX_PACKET_ACKS; i++) { + if (ack == _acks[i]) { + if (packet->isRouteDirect()) { + _direct_dups++; // keep some stats + } else { + _flood_dups++; + } + MESH_DEBUG_PRINTLN("SimpleMeshTables::wasSeen(): ACK already seen (crc=%08x)", (unsigned)ack); + return true; + } + } + return false; + } + + packet->calculatePacketHash(); const uint8_t* sp = _hashes; for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { - if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) { + if (memcmp(packet->hash, sp, MAX_HASH_SIZE) == 0) { if (packet->isRouteDirect()) { _direct_dups++; } else { @@ -50,21 +89,38 @@ class SimpleMeshTables : public mesh::MeshTables { } void markSeen(const mesh::Packet* packet) override { - uint8_t hash[MAX_HASH_SIZE]; - packet->calculatePacketHash(hash); - memcpy(&_hashes[_next_idx * MAX_HASH_SIZE], hash, MAX_HASH_SIZE); - _next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; + if (packet->getPayloadType() == PAYLOAD_TYPE_ACK) { + uint32_t ack; + memcpy(&ack, packet->payload, 4); + _acks[_next_ack_idx] = ack; + _next_ack_idx = (_next_ack_idx + 1) % MAX_PACKET_ACKS; // cyclic table + return; + } + + packet->calculatePacketHash(); + memcpy(&_hashes[_next_idx * MAX_HASH_SIZE], packet->hash, MAX_HASH_SIZE); + _next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; // cyclic table } void clear(const mesh::Packet* packet) override { - uint8_t hash[MAX_HASH_SIZE]; - packet->calculatePacketHash(hash); + if (packet->getPayloadType() == PAYLOAD_TYPE_ACK) { + uint32_t ack; + memcpy(&ack, packet->payload, 4); + for (int i = 0; i < MAX_PACKET_ACKS; i++) { + if (ack == _acks[i]) { + _acks[i] = 0; + break; + } + } + } else { + packet->calculatePacketHash(); - uint8_t* sp = _hashes; - for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { - if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) { - memset(sp, 0, MAX_HASH_SIZE); - break; + uint8_t* sp = _hashes; + for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { + if (memcmp(packet->hash, sp, MAX_HASH_SIZE) == 0) { + memset(sp, 0, MAX_HASH_SIZE); + break; + } } } } diff --git a/src/helpers/StaticPoolPacketManager.cpp b/src/helpers/StaticPoolPacketManager.cpp index 8a8eefe8cf4..10c561bcf95 100644 --- a/src/helpers/StaticPoolPacketManager.cpp +++ b/src/helpers/StaticPoolPacketManager.cpp @@ -43,6 +43,20 @@ mesh::Packet* PacketQueue::get(uint32_t now) { return top; } +mesh::Packet* PacketQueue::peek(uint32_t now) const { + // Same selection as get() (highest-priority, non-future entry) but without removing it. + uint8_t min_pri = 0xFF; + int best_idx = -1; + for (int j = 0; j < _num; j++) { + if ((int32_t)(_schedule_table[j] - now) > 0) continue; // scheduled for future... ignore + if (_pri_table[j] < min_pri) { + min_pri = _pri_table[j]; + best_idx = j; + } + } + return (best_idx >= 0) ? _table[best_idx] : NULL; +} + mesh::Packet* PacketQueue::removeByIdx(int i) { if (i < 0 || i >= _num) return NULL; // invalid index @@ -81,6 +95,16 @@ mesh::Packet* StaticPoolPacketManager::allocNew() { void StaticPoolPacketManager::free(mesh::Packet* packet) { if (packet == NULL) return; + memset(packet->hash, 0, MAX_HASH_SIZE); // clear stale cached hash so calculatePacketHash() recomputes on next use + packet->hash_hex[0] = '\0'; + // Reset per-send resend state. sending_attempts and final_hop_ack_resend are otherwise only + // zeroed by the Packet ctor, so a reused pool slot would keep a stale sending_attempts >= + // getMaxResendAttempts(). resendPacket() would then silently skip resends for every subsequent + // direct packet routed through that slot — resends degrade to zero once the pool has turned over + // (~pool_size direct sends), even though forwards (which don't check sending_attempts) keep working. + packet->sending_attempts = 0; + packet->no_retry = false; + packet->final_hop_ack_resend = false; if (!unused.add(packet, 0, 0)) { MESH_DEBUG_PRINTLN("free: unused queue full, possible double-free detected"); } @@ -99,6 +123,10 @@ mesh::Packet* StaticPoolPacketManager::getNextOutbound(uint32_t now) { return send_queue.get(now); } +mesh::Packet* StaticPoolPacketManager::peekNextOutbound(uint32_t now) { + return send_queue.peek(now); +} + int StaticPoolPacketManager::getOutboundCount(uint32_t now) const { return send_queue.countBefore(now); } diff --git a/src/helpers/StaticPoolPacketManager.h b/src/helpers/StaticPoolPacketManager.h index f63700e519a..f7bdc834ea2 100644 --- a/src/helpers/StaticPoolPacketManager.h +++ b/src/helpers/StaticPoolPacketManager.h @@ -11,6 +11,7 @@ class PacketQueue { public: PacketQueue(int max_entries); mesh::Packet* get(uint32_t now); + mesh::Packet* peek(uint32_t now) const; // same selection as get(), but non-consuming bool add(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for); int count() const { return _num; } int countBefore(uint32_t now) const; @@ -31,6 +32,7 @@ class StaticPoolPacketManager : public mesh::PacketManager { void free(mesh::Packet* packet) override; void queueOutbound(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) override; mesh::Packet* getNextOutbound(uint32_t now) override; + mesh::Packet* peekNextOutbound(uint32_t now) override; int getOutboundCount(uint32_t now) const override; int getOutboundTotal() const override; int getFreeCount() const override; diff --git a/src/helpers/StatsFormatHelper.h b/src/helpers/StatsFormatHelper.h index bf619133e93..dae723bfa4b 100644 --- a/src/helpers/StatsFormatHelper.h +++ b/src/helpers/StatsFormatHelper.h @@ -1,6 +1,7 @@ #pragma once #include "Mesh.h" +#include // strlen (used by formatResendRatio) class StatsFormatHelper { public: @@ -24,7 +25,7 @@ class StatsFormatHelper { RadioDriverType& driver, uint32_t total_air_time_ms, uint32_t total_rx_air_time_ms) { - sprintf(reply, + sprintf(reply, "{\"noise_floor\":%d,\"last_rssi\":%d,\"last_snr\":%.2f,\"tx_air_secs\":%u,\"rx_air_secs\":%u}", (int16_t)radio->getNoiseFloor(), (int16_t)driver.getLastRSSI(), @@ -41,7 +42,7 @@ class StatsFormatHelper { uint32_t n_sent_direct, uint32_t n_recv_flood, uint32_t n_recv_direct) { - sprintf(reply, + sprintf(reply, "{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,\"recv_errors\":%u}", driver.getPacketsRecv(), driver.getPacketsSent(), @@ -52,4 +53,11 @@ class StatsFormatHelper { driver.getPacketsRecvErrors() ); } + + // Appends ", resends / (%)" to reply (which already holds the + // max.resend value). Reports the resend share of direct-route TXs; pct is 0 when nothing was sent. + static void formatResendRatio(char* reply, uint32_t n_resent, uint32_t n_sent_direct) { + uint32_t pct = (n_sent_direct > 0) ? (n_resent * 100U) / n_sent_direct : 0; + sprintf(reply + strlen(reply), ", resends %u/%u (%u%%)", n_resent, n_sent_direct, pct); + } };