From fad439145898cbadcade65948d46126fe08dbead Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:31:34 +0200 Subject: [PATCH 1/2] fix(esp-now): Remove all peers on ESP_NOW.end() --- libraries/ESP_NOW/src/ESP32_NOW.cpp | 12 +++++++- libraries/ESP_NOW/src/ESP32_NOW.h | 47 ++++++++++++++++------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/libraries/ESP_NOW/src/ESP32_NOW.cpp b/libraries/ESP_NOW/src/ESP32_NOW.cpp index a27fafac15e..c60fa829449 100644 --- a/libraries/ESP_NOW/src/ESP32_NOW.cpp +++ b/libraries/ESP_NOW/src/ESP32_NOW.cpp @@ -190,13 +190,19 @@ bool ESP_NOW_Class::end() { if (!_esp_now_has_begun) { return true; } - //remove all peers? + //remove all peers + for(uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) { + if (_esp_now_peers[i] != NULL) { + removePeer(*_esp_now_peers[i]); + } + } esp_err_t err = esp_now_deinit(); if (err != ESP_OK) { log_e("esp_now_deinit failed! 0x%x", err); return false; } _esp_now_has_begun = false; + //clear the peer list memset(_esp_now_peers, 0, sizeof(ESP_NOW_Peer *) * ESP_NOW_MAX_TOTAL_PEER_NUM); return true; } @@ -262,6 +268,10 @@ void ESP_NOW_Class::onNewPeer(void (*cb)(const esp_now_recv_info_t *info, const new_arg = arg; } +bool ESP_NOW_Class::removePeer(ESP_NOW_Peer &peer) { + return peer.remove(); +} + ESP_NOW_Class ESP_NOW; /* diff --git a/libraries/ESP_NOW/src/ESP32_NOW.h b/libraries/ESP_NOW/src/ESP32_NOW.h index 03a34330700..1154d21f543 100644 --- a/libraries/ESP_NOW/src/ESP32_NOW.h +++ b/libraries/ESP_NOW/src/ESP32_NOW.h @@ -6,6 +6,31 @@ #include "esp32-hal-log.h" #include "esp_mac.h" +class ESP_NOW_Peer; //forward declaration for friend function + +class ESP_NOW_Class : public Print { +public: + const uint8_t BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + ESP_NOW_Class(); + ~ESP_NOW_Class(); + + bool begin(const uint8_t *pmk = NULL /* 16 bytes */); + bool end(); + + int getTotalPeerCount(); + int getEncryptedPeerCount(); + + int availableForWrite(); + size_t write(const uint8_t *data, size_t len); + size_t write(uint8_t data) { + return write(&data, 1); + } + + void onNewPeer(void (*cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg), void *arg); + bool removePeer(ESP_NOW_Peer &peer); +}; + class ESP_NOW_Peer { private: uint8_t mac[6]; @@ -47,28 +72,8 @@ class ESP_NOW_Peer { virtual void onSent(bool success) { log_i("Message transmission to peer " MACSTR " %s", MAC2STR(mac), success ? "successful" : "failed"); } -}; - -class ESP_NOW_Class : public Print { -public: - const uint8_t BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - - ESP_NOW_Class(); - ~ESP_NOW_Class(); - - bool begin(const uint8_t *pmk = NULL /* 16 bytes */); - bool end(); - - int getTotalPeerCount(); - int getEncryptedPeerCount(); - - int availableForWrite(); - size_t write(const uint8_t *data, size_t len); - size_t write(uint8_t data) { - return write(&data, 1); - } - void onNewPeer(void (*cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg), void *arg); + friend bool ESP_NOW_Class::removePeer(ESP_NOW_Peer&); }; extern ESP_NOW_Class ESP_NOW; From 7f98186c090f0b8efab887ab9cbf1f8d1bc09c48 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 2 Aug 2024 08:11:22 +0000 Subject: [PATCH 2/2] ci(pre-commit): Apply automatic fixes --- libraries/ESP_NOW/src/ESP32_NOW.cpp | 2 +- libraries/ESP_NOW/src/ESP32_NOW.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP_NOW/src/ESP32_NOW.cpp b/libraries/ESP_NOW/src/ESP32_NOW.cpp index c60fa829449..69b8ddd96a5 100644 --- a/libraries/ESP_NOW/src/ESP32_NOW.cpp +++ b/libraries/ESP_NOW/src/ESP32_NOW.cpp @@ -191,7 +191,7 @@ bool ESP_NOW_Class::end() { return true; } //remove all peers - for(uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) { + for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) { if (_esp_now_peers[i] != NULL) { removePeer(*_esp_now_peers[i]); } diff --git a/libraries/ESP_NOW/src/ESP32_NOW.h b/libraries/ESP_NOW/src/ESP32_NOW.h index 1154d21f543..1bbcabb2557 100644 --- a/libraries/ESP_NOW/src/ESP32_NOW.h +++ b/libraries/ESP_NOW/src/ESP32_NOW.h @@ -6,7 +6,7 @@ #include "esp32-hal-log.h" #include "esp_mac.h" -class ESP_NOW_Peer; //forward declaration for friend function +class ESP_NOW_Peer; //forward declaration for friend function class ESP_NOW_Class : public Print { public: @@ -73,7 +73,7 @@ class ESP_NOW_Peer { log_i("Message transmission to peer " MACSTR " %s", MAC2STR(mac), success ? "successful" : "failed"); } - friend bool ESP_NOW_Class::removePeer(ESP_NOW_Peer&); + friend bool ESP_NOW_Class::removePeer(ESP_NOW_Peer &); }; extern ESP_NOW_Class ESP_NOW;