Skip to content

Commit

Permalink
fix(ESP-NOW): Remove all peers on ESP_NOW.end() (#10102)
Browse files Browse the repository at this point in the history
* fix(esp-now): Remove all peers on ESP_NOW.end()

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
P-R-O-C-H-Y and pre-commit-ci-lite[bot] authored Aug 2, 2024
1 parent 23b84e5 commit 5aaa49e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
12 changes: 11 additions & 1 deletion libraries/ESP_NOW/src/ESP32_NOW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

/*
Expand Down
47 changes: 26 additions & 21 deletions libraries/ESP_NOW/src/ESP32_NOW.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;

0 comments on commit 5aaa49e

Please sign in to comment.