From c55f5aa5065b6ebfc0c1e8f5ef7f36b7e1605f71 Mon Sep 17 00:00:00 2001 From: ClockeNessMnstr Date: Tue, 17 Sep 2024 07:58:01 -0400 Subject: [PATCH] change(esp_now_serial): No teardown on retry limit (#10293) * change(ESP_NOW_Serial): No teardown on retry limit After max retries is met once the ESP_NOW_Serial_Class performs "end()". This removes the peer from ESP_NOW. Further messages to and from ESP_NOW_Serial are not received or sent. Peer should stay in ESP_NOW to re-establish connection even with data loss. This change will "retry and drop" the data piece by piece instead of aborting the connection. * feat(espnow): Add remove on fail parameter * feat(espnow): By default keep the peer when sending fails --------- Co-authored-by: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --- libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp | 13 ++++++++++--- libraries/ESP_NOW/src/ESP32_NOW_Serial.h | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp b/libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp index e4220d456..17740d133 100644 --- a/libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp +++ b/libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp @@ -11,7 +11,7 @@ * */ -ESP_NOW_Serial_Class::ESP_NOW_Serial_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) +ESP_NOW_Serial_Class::ESP_NOW_Serial_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk, bool remove_on_fail) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) { tx_ring_buf = NULL; rx_queue = NULL; @@ -19,6 +19,7 @@ ESP_NOW_Serial_Class::ESP_NOW_Serial_Class(const uint8_t *mac_addr, uint8_t chan queued_size = 0; queued_buff = NULL; resend_count = 0; + _remove_on_fail = remove_on_fail; } ESP_NOW_Serial_Class::~ESP_NOW_Serial_Class() { @@ -264,9 +265,15 @@ void ESP_NOW_Serial_Class::onSent(bool success) { //the data is lost in this case vRingbufferReturnItem(tx_ring_buf, queued_buff); queued_buff = NULL; - xSemaphoreGive(tx_sem); - end(); log_e(MACSTR " : RE-SEND_MAX[%u]", MAC2STR(addr()), resend_count); + //if we are not able to send the data and remove_on_fail is set, remove the peer + if (_remove_on_fail) { + xSemaphoreGive(tx_sem); + end(); + return; + } + //log_d(MACSTR ": NEXT", MAC2STR(addr())); + checkForTxData(); } } } diff --git a/libraries/ESP_NOW/src/ESP32_NOW_Serial.h b/libraries/ESP_NOW/src/ESP32_NOW_Serial.h index 42349f6c2..b1f414563 100644 --- a/libraries/ESP_NOW/src/ESP32_NOW_Serial.h +++ b/libraries/ESP_NOW/src/ESP32_NOW_Serial.h @@ -17,12 +17,13 @@ private: size_t queued_size; uint8_t *queued_buff; size_t resend_count; + bool _remove_on_fail; bool checkForTxData(); size_t tryToSend(); public: - ESP_NOW_Serial_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = NULL); + ESP_NOW_Serial_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = NULL, bool remove_on_fail = false); ~ESP_NOW_Serial_Class(); size_t setRxBufferSize(size_t); size_t setTxBufferSize(size_t);