Merge branch 'master' into release/v3.3.x

This commit is contained in:
Me No Dev 2025-06-23 14:49:32 +03:00 committed by GitHub
commit 18c909a8fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 81 additions and 62 deletions

View file

@ -58,7 +58,7 @@ public:
uint32_t msg_count = 0;
// Create a broadcast peer object
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);
/* Main */

View file

@ -52,7 +52,8 @@ public:
/* Global Variables */
// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;
// Note: Using pointers instead of objects to prevent dangling pointers when the vector reallocates
std::vector<ESP_NOW_Peer_Class *> masters;
/* Callbacks */
@ -62,13 +63,14 @@ void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, i
Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
Serial.println("Registering the peer as a master");
ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
masters.push_back(new_master);
if (!masters.back().add_peer()) {
ESP_NOW_Peer_Class *new_master = new ESP_NOW_Peer_Class(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);
if (!new_master->add_peer()) {
Serial.println("Failed to register the new master");
delete new_master;
return;
}
masters.push_back(new_master);
Serial.printf("Successfully registered master " MACSTR " (total masters: %zu)\n", MAC2STR(new_master->addr()), masters.size());
} else {
// The slave will only receive broadcast messages
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
@ -103,11 +105,23 @@ void setup() {
}
// Register the new peer callback
ESP_NOW.onNewPeer(register_new_master, NULL);
ESP_NOW.onNewPeer(register_new_master, nullptr);
Serial.println("Setup complete. Waiting for a master to broadcast a message...");
}
void loop() {
delay(1000);
// Print debug information every 10 seconds
static unsigned long last_debug = 0;
if (millis() - last_debug > 10000) {
last_debug = millis();
Serial.printf("Registered masters: %zu\n", masters.size());
for (size_t i = 0; i < masters.size(); i++) {
if (masters[i]) {
Serial.printf(" Master %zu: " MACSTR "\n", i, MAC2STR(masters[i]->addr()));
}
}
}
delay(100);
}

View file

@ -123,7 +123,7 @@ public:
}
bool send_message(const uint8_t *data, size_t len) {
if (data == NULL || len == 0) {
if (data == nullptr || len == 0) {
log_e("Data to be sent is NULL or has a length of 0");
return false;
}
@ -169,9 +169,12 @@ public:
/* Peers */
std::vector<ESP_NOW_Network_Peer *> peers; // Create a vector to store the peer pointers
ESP_NOW_Network_Peer broadcast_peer(ESP_NOW.BROADCAST_ADDR, 0, NULL); // Register the broadcast peer (no encryption support for the broadcast address)
ESP_NOW_Network_Peer *master_peer = nullptr; // Pointer to peer that is the master
// Create a vector to store the peer pointers
std::vector<ESP_NOW_Network_Peer *> peers;
// Register the broadcast peer (no encryption support for the broadcast address)
ESP_NOW_Network_Peer broadcast_peer(ESP_NOW.BROADCAST_ADDR, 0, nullptr);
// Pointer to the peer that is the master
ESP_NOW_Network_Peer *master_peer = nullptr;
/* Helper functions */
@ -279,7 +282,7 @@ void setup() {
}
// Register the callback to be called when a new peer is found
ESP_NOW.onNewPeer(register_new_peer, NULL);
ESP_NOW.onNewPeer(register_new_peer, nullptr);
Serial.println("Setup complete. Broadcasting own priority to find the master...");
memset(&new_msg, 0, sizeof(new_msg));

View file

@ -9,12 +9,12 @@
#include "esp32-hal.h"
#include "esp_wifi.h"
static void (*new_cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) = NULL;
static void *new_arg = NULL; // * tx_arg = NULL, * rx_arg = NULL,
static void (*new_cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) = nullptr;
static void *new_arg = nullptr; // * tx_arg = nullptr, * rx_arg = nullptr,
static bool _esp_now_has_begun = false;
static ESP_NOW_Peer *_esp_now_peers[ESP_NOW_MAX_TOTAL_PEER_NUM];
static esp_err_t _esp_now_add_peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk, ESP_NOW_Peer *_peer = NULL) {
static esp_err_t _esp_now_add_peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk, ESP_NOW_Peer *_peer = nullptr) {
log_v(MACSTR, MAC2STR(mac_addr));
if (esp_now_is_peer_exist(mac_addr)) {
log_e("Peer Already Exists");
@ -26,16 +26,16 @@ static esp_err_t _esp_now_add_peer(const uint8_t *mac_addr, uint8_t channel, wif
memcpy(peer.peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
peer.channel = channel;
peer.ifidx = iface;
peer.encrypt = lmk != NULL;
peer.encrypt = lmk != nullptr;
if (lmk) {
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
}
esp_err_t result = esp_now_add_peer(&peer);
if (result == ESP_OK) {
if (_peer != NULL) {
if (_peer != nullptr) {
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] == NULL) {
if (_esp_now_peers[i] == nullptr) {
_esp_now_peers[i] = _peer;
return ESP_OK;
}
@ -67,8 +67,8 @@ static esp_err_t _esp_now_del_peer(const uint8_t *mac_addr) {
}
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
_esp_now_peers[i] = NULL;
if (_esp_now_peers[i] != nullptr && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
_esp_now_peers[i] = nullptr;
break;
}
}
@ -87,7 +87,7 @@ static esp_err_t _esp_now_modify_peer(const uint8_t *mac_addr, uint8_t channel,
memcpy(peer.peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
peer.channel = channel;
peer.ifidx = iface;
peer.encrypt = lmk != NULL;
peer.encrypt = lmk != nullptr;
if (lmk) {
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
}
@ -111,17 +111,17 @@ static void _esp_now_rx_cb(const esp_now_recv_info_t *info, const uint8_t *data,
bool broadcast = memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
log_v("%s from " MACSTR ", data length : %u", broadcast ? "Broadcast" : "Unicast", MAC2STR(info->src_addr), len);
log_buf_v(data, len);
if (!esp_now_is_peer_exist(info->src_addr) && new_cb != NULL) {
if (!esp_now_is_peer_exist(info->src_addr) && new_cb != nullptr) {
log_v("Calling new_cb, peer not found.");
new_cb(info, data, len, new_arg);
return;
}
//find the peer and call it's callback
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL) {
if (_esp_now_peers[i] != nullptr) {
log_v("Checking peer " MACSTR, MAC2STR(_esp_now_peers[i]->addr()));
}
if (_esp_now_peers[i] != NULL && memcmp(info->src_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
if (_esp_now_peers[i] != nullptr && memcmp(info->src_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
log_v("Calling onReceive");
_esp_now_peers[i]->onReceive(data, len, broadcast);
return;
@ -138,7 +138,7 @@ static void _esp_now_tx_cb(const uint8_t *mac_addr, esp_now_send_status_t status
log_v(MACSTR " : %s", MAC2STR(mac_addr), (status == ESP_NOW_SEND_SUCCESS) ? "SUCCESS" : "FAILED");
//find the peer and call it's callback
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
if (_esp_now_peers[i] != nullptr && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
_esp_now_peers[i]->onSent(status == ESP_NOW_SEND_SUCCESS);
return;
}
@ -202,7 +202,7 @@ bool ESP_NOW_Class::end() {
}
//remove all peers
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL) {
if (_esp_now_peers[i] != nullptr) {
removePeer(*_esp_now_peers[i]);
}
}
@ -254,7 +254,7 @@ size_t ESP_NOW_Class::write(const uint8_t *data, size_t len) {
if (len > ESP_NOW_MAX_DATA_LEN) {
len = ESP_NOW_MAX_DATA_LEN;
}
esp_err_t result = esp_now_send(NULL, data, len);
esp_err_t result = esp_now_send(nullptr, data, len);
if (result == ESP_OK) {
return len;
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
@ -297,7 +297,7 @@ ESP_NOW_Peer::ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel, wifi_interf
}
chan = channel;
ifc = iface;
encrypt = lmk != NULL;
encrypt = lmk != nullptr;
if (encrypt) {
memcpy(key, lmk, 16);
}
@ -310,7 +310,7 @@ bool ESP_NOW_Peer::add() {
if (added) {
return true;
}
if (_esp_now_add_peer(mac, chan, ifc, encrypt ? key : NULL, this) != ESP_OK) {
if (_esp_now_add_peer(mac, chan, ifc, encrypt ? key : nullptr, this) != ESP_OK) {
return false;
}
log_v("Peer added - " MACSTR, MAC2STR(mac));
@ -355,7 +355,7 @@ bool ESP_NOW_Peer::setChannel(uint8_t channel) {
if (!_esp_now_has_begun || !added) {
return true;
}
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : NULL) == ESP_OK;
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : nullptr) == ESP_OK;
}
wifi_interface_t ESP_NOW_Peer::getInterface() const {
@ -367,7 +367,7 @@ bool ESP_NOW_Peer::setInterface(wifi_interface_t iface) {
if (!_esp_now_has_begun || !added) {
return true;
}
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : NULL) == ESP_OK;
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : nullptr) == ESP_OK;
}
bool ESP_NOW_Peer::isEncrypted() const {
@ -375,14 +375,14 @@ bool ESP_NOW_Peer::isEncrypted() const {
}
bool ESP_NOW_Peer::setKey(const uint8_t *lmk) {
encrypt = lmk != NULL;
encrypt = lmk != nullptr;
if (encrypt) {
memcpy(key, lmk, 16);
}
if (!_esp_now_has_begun || !added) {
return true;
}
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : NULL) == ESP_OK;
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : nullptr) == ESP_OK;
}
size_t ESP_NOW_Peer::send(const uint8_t *data, int len) {

View file

@ -20,7 +20,7 @@ public:
ESP_NOW_Class();
~ESP_NOW_Class();
bool begin(const uint8_t *pmk = NULL /* 16 bytes */);
bool begin(const uint8_t *pmk = nullptr /* 16 bytes */);
bool end();
int getTotalPeerCount();
@ -50,7 +50,7 @@ protected:
bool remove();
size_t send(const uint8_t *data, int len);
ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel = 0, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = NULL);
ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel = 0, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = nullptr);
public:
virtual ~ESP_NOW_Peer() {}

View file

@ -18,11 +18,11 @@
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;
tx_sem = NULL;
tx_ring_buf = nullptr;
rx_queue = nullptr;
tx_sem = nullptr;
queued_size = 0;
queued_buff = NULL;
queued_buff = nullptr;
resend_count = 0;
_remove_on_fail = remove_on_fail;
}
@ -34,7 +34,7 @@ ESP_NOW_Serial_Class::~ESP_NOW_Serial_Class() {
size_t ESP_NOW_Serial_Class::setTxBufferSize(size_t tx_queue_len) {
if (tx_ring_buf) {
vRingbufferDelete(tx_ring_buf);
tx_ring_buf = NULL;
tx_ring_buf = nullptr;
}
if (!tx_queue_len) {
return 0;
@ -49,7 +49,7 @@ size_t ESP_NOW_Serial_Class::setTxBufferSize(size_t tx_queue_len) {
size_t ESP_NOW_Serial_Class::setRxBufferSize(size_t rx_queue_len) {
if (rx_queue) {
vQueueDelete(rx_queue);
rx_queue = NULL;
rx_queue = nullptr;
}
if (!rx_queue_len) {
return 0;
@ -65,7 +65,7 @@ bool ESP_NOW_Serial_Class::begin(unsigned long baud) {
if (!ESP_NOW.begin() || !add()) {
return false;
}
if (tx_sem == NULL) {
if (tx_sem == nullptr) {
tx_sem = xSemaphoreCreateBinary();
//xSemaphoreTake(tx_sem, 0);
xSemaphoreGive(tx_sem);
@ -79,22 +79,22 @@ void ESP_NOW_Serial_Class::end() {
remove();
setRxBufferSize(0);
setTxBufferSize(0);
if (tx_sem != NULL) {
if (tx_sem != nullptr) {
vSemaphoreDelete(tx_sem);
tx_sem = NULL;
tx_sem = nullptr;
}
}
//Stream
int ESP_NOW_Serial_Class::available(void) {
if (rx_queue == NULL) {
if (rx_queue == nullptr) {
return 0;
}
return uxQueueMessagesWaiting(rx_queue);
}
int ESP_NOW_Serial_Class::peek(void) {
if (rx_queue == NULL) {
if (rx_queue == nullptr) {
return -1;
}
uint8_t c;
@ -105,7 +105,7 @@ int ESP_NOW_Serial_Class::peek(void) {
}
int ESP_NOW_Serial_Class::read(void) {
if (rx_queue == NULL) {
if (rx_queue == nullptr) {
return -1;
}
uint8_t c = 0;
@ -116,7 +116,7 @@ int ESP_NOW_Serial_Class::read(void) {
}
size_t ESP_NOW_Serial_Class::read(uint8_t *buffer, size_t size) {
if (rx_queue == NULL) {
if (rx_queue == nullptr) {
return -1;
}
uint8_t c = 0;
@ -128,11 +128,11 @@ size_t ESP_NOW_Serial_Class::read(uint8_t *buffer, size_t size) {
}
void ESP_NOW_Serial_Class::flush() {
if (tx_ring_buf == NULL) {
if (tx_ring_buf == nullptr) {
return;
}
UBaseType_t uxItemsWaiting = 0;
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
vRingbufferGetInfo(tx_ring_buf, nullptr, nullptr, nullptr, nullptr, &uxItemsWaiting);
if (uxItemsWaiting) {
// Now trigger the ISR to read data from the ring buffer.
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
@ -141,13 +141,13 @@ void ESP_NOW_Serial_Class::flush() {
}
while (uxItemsWaiting) {
delay(5);
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
vRingbufferGetInfo(tx_ring_buf, nullptr, nullptr, nullptr, nullptr, &uxItemsWaiting);
}
}
//RX callback
void ESP_NOW_Serial_Class::onReceive(const uint8_t *data, size_t len, bool broadcast) {
if (rx_queue == NULL) {
if (rx_queue == nullptr) {
return;
}
for (uint32_t i = 0; i < len; i++) {
@ -165,7 +165,7 @@ void ESP_NOW_Serial_Class::onReceive(const uint8_t *data, size_t len, bool broad
//Print
int ESP_NOW_Serial_Class::availableForWrite() {
//return ESP_NOW_MAX_DATA_LEN;
if (tx_ring_buf == NULL) {
if (tx_ring_buf == nullptr) {
return 0;
}
return xRingbufferGetCurFreeSize(tx_ring_buf);
@ -178,7 +178,7 @@ size_t ESP_NOW_Serial_Class::tryToSend() {
//_onSent will not be called anymore
//the data is lost in this case
vRingbufferReturnItem(tx_ring_buf, queued_buff);
queued_buff = NULL;
queued_buff = nullptr;
xSemaphoreGive(tx_sem);
end();
}
@ -188,12 +188,12 @@ size_t ESP_NOW_Serial_Class::tryToSend() {
bool ESP_NOW_Serial_Class::checkForTxData() {
//do we have something that failed the last time?
resend_count = 0;
if (queued_buff == NULL) {
if (queued_buff == nullptr) {
queued_buff = (uint8_t *)xRingbufferReceiveUpTo(tx_ring_buf, &queued_size, 0, ESP_NOW_MAX_DATA_LEN);
} else {
log_d(MACSTR " : PREVIOUS", MAC2STR(addr()));
}
if (queued_buff != NULL) {
if (queued_buff != nullptr) {
return tryToSend() > 0;
}
//log_d(MACSTR ": EMPTY", MAC2STR(addr()));
@ -203,7 +203,7 @@ bool ESP_NOW_Serial_Class::checkForTxData() {
size_t ESP_NOW_Serial_Class::write(const uint8_t *buffer, size_t size, uint32_t timeout) {
log_v(MACSTR ", size %u", MAC2STR(addr()), size);
if (tx_sem == NULL || tx_ring_buf == NULL || !added) {
if (tx_sem == nullptr || tx_ring_buf == nullptr || !added) {
return 0;
}
size_t space = availableForWrite();
@ -249,12 +249,12 @@ size_t ESP_NOW_Serial_Class::write(const uint8_t *buffer, size_t size, uint32_t
//TX Done Callback
void ESP_NOW_Serial_Class::onSent(bool success) {
log_v(MACSTR " : %s", MAC2STR(addr()), success ? "OK" : "FAIL");
if (tx_sem == NULL || tx_ring_buf == NULL || !added) {
if (tx_sem == nullptr || tx_ring_buf == nullptr || !added) {
return;
}
if (success) {
vRingbufferReturnItem(tx_ring_buf, queued_buff);
queued_buff = NULL;
queued_buff = nullptr;
//send next packet?
//log_d(MACSTR ": NEXT", MAC2STR(addr()));
checkForTxData();
@ -269,7 +269,7 @@ void ESP_NOW_Serial_Class::onSent(bool success) {
//resend limit reached
//the data is lost in this case
vRingbufferReturnItem(tx_ring_buf, queued_buff);
queued_buff = NULL;
queued_buff = nullptr;
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) {

View file

@ -28,7 +28,9 @@ private:
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, bool remove_on_fail = false);
ESP_NOW_Serial_Class(
const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = nullptr, bool remove_on_fail = false
);
~ESP_NOW_Serial_Class();
size_t setRxBufferSize(size_t);
size_t setTxBufferSize(size_t);