diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index ebc62bc3..a397657e 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1459,8 +1459,9 @@ void MyMesh::formatRadioStatsReply(char *reply) { } void MyMesh::formatPacketStatsReply(char *reply) { - StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(), getNumSentDirect(), - getNumRecvFlood(), getNumRecvDirect()); + StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(), getNumSentDirect(), + getNumRecvFlood(), getNumRecvDirect(), + getNumTxStartFail(), getNumTxTimeout()); } void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 6c9e3c1b..aaf48538 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -19,6 +19,7 @@ namespace mesh { void Dispatcher::begin() { n_sent_flood = n_sent_direct = 0; n_recv_flood = n_recv_direct = 0; + n_tx_start_fail = n_tx_timeout = 0; _err_flags = 0; radio_nonrx_start = _ms->getMillis(); @@ -117,9 +118,20 @@ void Dispatcher::loop() { MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): WARNING: outbound packed send timed out!", getLogDateTime()); _radio->onSendFinished(); - logTxFail(outbound, 2 + outbound->getPathByteLen() + outbound->payload_len); - releasePacket(outbound); // return to pool + // CUSTOM (TeTeHacko): the send-complete IRQ never arrived (SoftDevice/BLE + // preemption can wedge the LoRa driver mid-TX). Requeue with a short + // backoff instead of dropping -- a duplicate on air is harmless (hasSeen + // dedup), a dropped direct forward is a black hole. + n_tx_timeout++; + _err_flags |= ERR_EVENT_TX_TIMEOUT; + if (outbound->_tx_attempts < MESH_TX_RETRIES) { + outbound->_tx_attempts++; + _mgr->queueOutbound(outbound, 0, futureMillis(40 + 80 * outbound->_tx_attempts)); + } else { + logTxFail(outbound, 2 + outbound->getPathByteLen() + outbound->payload_len); + releasePacket(outbound); // return to pool + } outbound = NULL; } else { return; // can't do any more radio activity until send is complete or timed out @@ -204,6 +216,7 @@ void Dispatcher::checkRecv() { if (tryParsePacket(pkt, raw, len)) { pkt->_snr = _radio->getLastSNR() * 4.0f; pkt->_rssi = (int8_t)_radio->getLastRSSI(); + pkt->_tx_attempts = 0; score = _radio->packetScore(_radio->getLastSNR(), len); air_time = _radio->getEstAirtimeFor(len); rx_air_time += air_time; @@ -330,9 +343,18 @@ void Dispatcher::checkSend() { if (!success) { MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): ERROR: send start failed!", getLogDateTime()); - logTxFail(outbound, outbound->getRawLength()); - - releasePacket(outbound); // return to pool + // CUSTOM (TeTeHacko): startTransmit() failing is usually transient + // (SPI/BUSY hiccup while the SoftDevice services a BLE connection + // event). Requeue with a short backoff instead of dropping. + n_tx_start_fail++; + _err_flags |= ERR_EVENT_TX_START_FAIL; + if (outbound->_tx_attempts < MESH_TX_RETRIES) { + outbound->_tx_attempts++; + _mgr->queueOutbound(outbound, 0, futureMillis(40 + 80 * outbound->_tx_attempts)); + } else { + logTxFail(outbound, outbound->getRawLength()); + releasePacket(outbound); // return to pool + } outbound = NULL; return; } @@ -360,6 +382,7 @@ Packet* Dispatcher::obtainNewPacket() { } else { pkt->payload_len = pkt->path_len = 0; pkt->_snr = 0; + pkt->_tx_attempts = 0; } return pkt; } diff --git a/src/Dispatcher.h b/src/Dispatcher.h index dd032f13..d3f39ae3 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -108,6 +108,17 @@ typedef uint32_t DispatcherAction; #define ERR_EVENT_FULL (1 << 0) #define ERR_EVENT_CAD_TIMEOUT (1 << 1) #define ERR_EVENT_STARTRX_TIMEOUT (1 << 2) +#define ERR_EVENT_TX_START_FAIL (1 << 3) // CUSTOM (TeTeHacko): startSendRaw() returned error (radio busy/SPI hiccup) +#define ERR_EVENT_TX_TIMEOUT (1 << 4) // CUSTOM (TeTeHacko): send-complete IRQ never came before outbound_expiry + +// CUSTOM (TeTeHacko): how many times a failed TX (start fail or send timeout) is +// requeued before the packet is finally dropped. Transient failures are common +// when the nRF52 SoftDevice (BLE connection events) preempts the LoRa driver — +// measured ~40 % direct-forward loss with an idle BLE connection. A short retry +// converts those losses into ~100-300 ms of extra latency. +#ifndef MESH_TX_RETRIES + #define MESH_TX_RETRIES 3 +#endif /** * \brief The low-level task that manages detecting incoming Packets, and the queueing @@ -123,6 +134,7 @@ 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_tx_start_fail, n_tx_timeout; // CUSTOM (TeTeHacko): TX failure tallies (see MESH_TX_RETRIES) unsigned long tx_budget_ms; unsigned long last_budget_update; unsigned long duty_cycle_window_ms; @@ -184,6 +196,8 @@ public: uint32_t getNumSentDirect() const { return n_sent_direct; } uint32_t getNumRecvFlood() const { return n_recv_flood; } uint32_t getNumRecvDirect() const { return n_recv_direct; } + uint32_t getNumTxStartFail() const { return n_tx_start_fail; } // CUSTOM (TeTeHacko) + uint32_t getNumTxTimeout() const { return n_tx_timeout; } // CUSTOM (TeTeHacko) void resetStats() { n_sent_flood = n_sent_direct = n_recv_flood = n_recv_direct = 0; _err_flags = 0; diff --git a/src/Packet.h b/src/Packet.h index 91d61145..e46b42ff 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -50,6 +50,7 @@ public: uint8_t payload[MAX_PACKET_PAYLOAD]; int8_t _snr; int8_t _rssi; // CUSTOM (TeTeHacko): last-hop RSSI (dBm) captured at RX, for the passive mesh analyzer + uint8_t _tx_attempts; // CUSTOM (TeTeHacko): failed TX tries so far (RAM only; Dispatcher requeues instead of dropping) /** * \brief calculate the hash of payload + type diff --git a/src/helpers/StatsFormatHelper.h b/src/helpers/StatsFormatHelper.h index bf619133..443e69b7 100644 --- a/src/helpers/StatsFormatHelper.h +++ b/src/helpers/StatsFormatHelper.h @@ -40,16 +40,20 @@ public: uint32_t n_sent_flood, uint32_t n_sent_direct, uint32_t n_recv_flood, - uint32_t n_recv_direct) { - sprintf(reply, - "{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,\"recv_errors\":%u}", + uint32_t n_recv_direct, + uint32_t n_tx_start_fail = 0, // CUSTOM (TeTeHacko): TX failure tallies + uint32_t n_tx_timeout = 0) { // (defaults keep other callers unchanged) + sprintf(reply, + "{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,\"recv_errors\":%u,\"tx_start_fail\":%u,\"tx_timeout\":%u}", driver.getPacketsRecv(), driver.getPacketsSent(), n_sent_flood, n_sent_direct, n_recv_flood, n_recv_direct, - driver.getPacketsRecvErrors() + driver.getPacketsRecvErrors(), + n_tx_start_fail, + n_tx_timeout ); } };