Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ESP-NOW): Remove all peers on ESP_NOW.end() #10102

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading