fix(esp_now): Fix broadcast example and use nullptr (#11490)
* fix(esp_now): Fix example and use nullptr * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
parent
9d84c78cf2
commit
dc82467ba4
7 changed files with 81 additions and 62 deletions
|
|
@ -58,7 +58,7 @@ public:
|
||||||
uint32_t msg_count = 0;
|
uint32_t msg_count = 0;
|
||||||
|
|
||||||
// Create a broadcast peer object
|
// 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 */
|
/* Main */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,8 @@ public:
|
||||||
/* Global Variables */
|
/* Global Variables */
|
||||||
|
|
||||||
// List of all the masters. It will be populated when a new master is registered
|
// 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 */
|
/* 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.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
|
||||||
Serial.println("Registering the peer as a master");
|
Serial.println("Registering the peer as a master");
|
||||||
|
|
||||||
ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
|
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()) {
|
||||||
masters.push_back(new_master);
|
|
||||||
if (!masters.back().add_peer()) {
|
|
||||||
Serial.println("Failed to register the new master");
|
Serial.println("Failed to register the new master");
|
||||||
|
delete new_master;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
masters.push_back(new_master);
|
||||||
|
Serial.printf("Successfully registered master " MACSTR " (total masters: %zu)\n", MAC2STR(new_master->addr()), masters.size());
|
||||||
} else {
|
} else {
|
||||||
// The slave will only receive broadcast messages
|
// The slave will only receive broadcast messages
|
||||||
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
|
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
|
||||||
|
|
@ -103,11 +105,23 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the new peer callback
|
// 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...");
|
Serial.println("Setup complete. Waiting for a master to broadcast a message...");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool send_message(const uint8_t *data, size_t len) {
|
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");
|
log_e("Data to be sent is NULL or has a length of 0");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -169,9 +169,12 @@ public:
|
||||||
|
|
||||||
/* Peers */
|
/* Peers */
|
||||||
|
|
||||||
std::vector<ESP_NOW_Network_Peer *> peers; // Create a vector to store the peer pointers
|
// 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)
|
std::vector<ESP_NOW_Network_Peer *> peers;
|
||||||
ESP_NOW_Network_Peer *master_peer = nullptr; // Pointer to peer that is the master
|
// 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 */
|
/* Helper functions */
|
||||||
|
|
||||||
|
|
@ -279,7 +282,7 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the callback to be called when a new peer is found
|
// 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...");
|
Serial.println("Setup complete. Broadcasting own priority to find the master...");
|
||||||
memset(&new_msg, 0, sizeof(new_msg));
|
memset(&new_msg, 0, sizeof(new_msg));
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,12 @@
|
||||||
#include "esp32-hal.h"
|
#include "esp32-hal.h"
|
||||||
#include "esp_wifi.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_cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) = nullptr;
|
||||||
static void *new_arg = NULL; // * tx_arg = NULL, * rx_arg = NULL,
|
static void *new_arg = nullptr; // * tx_arg = nullptr, * rx_arg = nullptr,
|
||||||
static bool _esp_now_has_begun = false;
|
static bool _esp_now_has_begun = false;
|
||||||
static ESP_NOW_Peer *_esp_now_peers[ESP_NOW_MAX_TOTAL_PEER_NUM];
|
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));
|
log_v(MACSTR, MAC2STR(mac_addr));
|
||||||
if (esp_now_is_peer_exist(mac_addr)) {
|
if (esp_now_is_peer_exist(mac_addr)) {
|
||||||
log_e("Peer Already Exists");
|
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);
|
memcpy(peer.peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
|
||||||
peer.channel = channel;
|
peer.channel = channel;
|
||||||
peer.ifidx = iface;
|
peer.ifidx = iface;
|
||||||
peer.encrypt = lmk != NULL;
|
peer.encrypt = lmk != nullptr;
|
||||||
if (lmk) {
|
if (lmk) {
|
||||||
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
|
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t result = esp_now_add_peer(&peer);
|
esp_err_t result = esp_now_add_peer(&peer);
|
||||||
if (result == ESP_OK) {
|
if (result == ESP_OK) {
|
||||||
if (_peer != NULL) {
|
if (_peer != nullptr) {
|
||||||
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) {
|
if (_esp_now_peers[i] == nullptr) {
|
||||||
_esp_now_peers[i] = _peer;
|
_esp_now_peers[i] = _peer;
|
||||||
return ESP_OK;
|
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++) {
|
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] = NULL;
|
_esp_now_peers[i] = nullptr;
|
||||||
break;
|
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);
|
memcpy(peer.peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
|
||||||
peer.channel = channel;
|
peer.channel = channel;
|
||||||
peer.ifidx = iface;
|
peer.ifidx = iface;
|
||||||
peer.encrypt = lmk != NULL;
|
peer.encrypt = lmk != nullptr;
|
||||||
if (lmk) {
|
if (lmk) {
|
||||||
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
|
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;
|
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_v("%s from " MACSTR ", data length : %u", broadcast ? "Broadcast" : "Unicast", MAC2STR(info->src_addr), len);
|
||||||
log_buf_v(data, 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.");
|
log_v("Calling new_cb, peer not found.");
|
||||||
new_cb(info, data, len, new_arg);
|
new_cb(info, data, len, new_arg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//find the peer and call it's callback
|
//find the peer and call it's callback
|
||||||
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) {
|
if (_esp_now_peers[i] != nullptr) {
|
||||||
log_v("Checking peer " MACSTR, MAC2STR(_esp_now_peers[i]->addr()));
|
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");
|
log_v("Calling onReceive");
|
||||||
_esp_now_peers[i]->onReceive(data, len, broadcast);
|
_esp_now_peers[i]->onReceive(data, len, broadcast);
|
||||||
return;
|
return;
|
||||||
|
|
@ -133,7 +133,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");
|
log_v(MACSTR " : %s", MAC2STR(mac_addr), (status == ESP_NOW_SEND_SUCCESS) ? "SUCCESS" : "FAILED");
|
||||||
//find the peer and call it's callback
|
//find the peer and call it's callback
|
||||||
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 && 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);
|
_esp_now_peers[i]->onSent(status == ESP_NOW_SEND_SUCCESS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -197,7 +197,7 @@ bool ESP_NOW_Class::end() {
|
||||||
}
|
}
|
||||||
//remove all peers
|
//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) {
|
if (_esp_now_peers[i] != nullptr) {
|
||||||
removePeer(*_esp_now_peers[i]);
|
removePeer(*_esp_now_peers[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -249,7 +249,7 @@ size_t ESP_NOW_Class::write(const uint8_t *data, size_t len) {
|
||||||
if (len > ESP_NOW_MAX_DATA_LEN) {
|
if (len > ESP_NOW_MAX_DATA_LEN) {
|
||||||
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) {
|
if (result == ESP_OK) {
|
||||||
return len;
|
return len;
|
||||||
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
|
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
|
||||||
|
|
@ -292,7 +292,7 @@ ESP_NOW_Peer::ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel, wifi_interf
|
||||||
}
|
}
|
||||||
chan = channel;
|
chan = channel;
|
||||||
ifc = iface;
|
ifc = iface;
|
||||||
encrypt = lmk != NULL;
|
encrypt = lmk != nullptr;
|
||||||
if (encrypt) {
|
if (encrypt) {
|
||||||
memcpy(key, lmk, 16);
|
memcpy(key, lmk, 16);
|
||||||
}
|
}
|
||||||
|
|
@ -305,7 +305,7 @@ bool ESP_NOW_Peer::add() {
|
||||||
if (added) {
|
if (added) {
|
||||||
return true;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
log_v("Peer added - " MACSTR, MAC2STR(mac));
|
log_v("Peer added - " MACSTR, MAC2STR(mac));
|
||||||
|
|
@ -350,7 +350,7 @@ bool ESP_NOW_Peer::setChannel(uint8_t channel) {
|
||||||
if (!_esp_now_has_begun || !added) {
|
if (!_esp_now_has_begun || !added) {
|
||||||
return true;
|
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 {
|
wifi_interface_t ESP_NOW_Peer::getInterface() const {
|
||||||
|
|
@ -362,7 +362,7 @@ bool ESP_NOW_Peer::setInterface(wifi_interface_t iface) {
|
||||||
if (!_esp_now_has_begun || !added) {
|
if (!_esp_now_has_begun || !added) {
|
||||||
return true;
|
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 {
|
bool ESP_NOW_Peer::isEncrypted() const {
|
||||||
|
|
@ -370,14 +370,14 @@ bool ESP_NOW_Peer::isEncrypted() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESP_NOW_Peer::setKey(const uint8_t *lmk) {
|
bool ESP_NOW_Peer::setKey(const uint8_t *lmk) {
|
||||||
encrypt = lmk != NULL;
|
encrypt = lmk != nullptr;
|
||||||
if (encrypt) {
|
if (encrypt) {
|
||||||
memcpy(key, lmk, 16);
|
memcpy(key, lmk, 16);
|
||||||
}
|
}
|
||||||
if (!_esp_now_has_begun || !added) {
|
if (!_esp_now_has_begun || !added) {
|
||||||
return true;
|
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) {
|
size_t ESP_NOW_Peer::send(const uint8_t *data, int len) {
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ public:
|
||||||
ESP_NOW_Class();
|
ESP_NOW_Class();
|
||||||
~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();
|
bool end();
|
||||||
|
|
||||||
int getTotalPeerCount();
|
int getTotalPeerCount();
|
||||||
|
|
@ -50,7 +50,7 @@ protected:
|
||||||
bool remove();
|
bool remove();
|
||||||
size_t send(const uint8_t *data, int len);
|
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:
|
public:
|
||||||
virtual ~ESP_NOW_Peer() {}
|
virtual ~ESP_NOW_Peer() {}
|
||||||
|
|
|
||||||
|
|
@ -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_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) {
|
: ESP_NOW_Peer(mac_addr, channel, iface, lmk) {
|
||||||
tx_ring_buf = NULL;
|
tx_ring_buf = nullptr;
|
||||||
rx_queue = NULL;
|
rx_queue = nullptr;
|
||||||
tx_sem = NULL;
|
tx_sem = nullptr;
|
||||||
queued_size = 0;
|
queued_size = 0;
|
||||||
queued_buff = NULL;
|
queued_buff = nullptr;
|
||||||
resend_count = 0;
|
resend_count = 0;
|
||||||
_remove_on_fail = remove_on_fail;
|
_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) {
|
size_t ESP_NOW_Serial_Class::setTxBufferSize(size_t tx_queue_len) {
|
||||||
if (tx_ring_buf) {
|
if (tx_ring_buf) {
|
||||||
vRingbufferDelete(tx_ring_buf);
|
vRingbufferDelete(tx_ring_buf);
|
||||||
tx_ring_buf = NULL;
|
tx_ring_buf = nullptr;
|
||||||
}
|
}
|
||||||
if (!tx_queue_len) {
|
if (!tx_queue_len) {
|
||||||
return 0;
|
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) {
|
size_t ESP_NOW_Serial_Class::setRxBufferSize(size_t rx_queue_len) {
|
||||||
if (rx_queue) {
|
if (rx_queue) {
|
||||||
vQueueDelete(rx_queue);
|
vQueueDelete(rx_queue);
|
||||||
rx_queue = NULL;
|
rx_queue = nullptr;
|
||||||
}
|
}
|
||||||
if (!rx_queue_len) {
|
if (!rx_queue_len) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -65,7 +65,7 @@ bool ESP_NOW_Serial_Class::begin(unsigned long baud) {
|
||||||
if (!ESP_NOW.begin() || !add()) {
|
if (!ESP_NOW.begin() || !add()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (tx_sem == NULL) {
|
if (tx_sem == nullptr) {
|
||||||
tx_sem = xSemaphoreCreateBinary();
|
tx_sem = xSemaphoreCreateBinary();
|
||||||
//xSemaphoreTake(tx_sem, 0);
|
//xSemaphoreTake(tx_sem, 0);
|
||||||
xSemaphoreGive(tx_sem);
|
xSemaphoreGive(tx_sem);
|
||||||
|
|
@ -79,22 +79,22 @@ void ESP_NOW_Serial_Class::end() {
|
||||||
remove();
|
remove();
|
||||||
setRxBufferSize(0);
|
setRxBufferSize(0);
|
||||||
setTxBufferSize(0);
|
setTxBufferSize(0);
|
||||||
if (tx_sem != NULL) {
|
if (tx_sem != nullptr) {
|
||||||
vSemaphoreDelete(tx_sem);
|
vSemaphoreDelete(tx_sem);
|
||||||
tx_sem = NULL;
|
tx_sem = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Stream
|
//Stream
|
||||||
int ESP_NOW_Serial_Class::available(void) {
|
int ESP_NOW_Serial_Class::available(void) {
|
||||||
if (rx_queue == NULL) {
|
if (rx_queue == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return uxQueueMessagesWaiting(rx_queue);
|
return uxQueueMessagesWaiting(rx_queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ESP_NOW_Serial_Class::peek(void) {
|
int ESP_NOW_Serial_Class::peek(void) {
|
||||||
if (rx_queue == NULL) {
|
if (rx_queue == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uint8_t c;
|
uint8_t c;
|
||||||
|
|
@ -105,7 +105,7 @@ int ESP_NOW_Serial_Class::peek(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int ESP_NOW_Serial_Class::read(void) {
|
int ESP_NOW_Serial_Class::read(void) {
|
||||||
if (rx_queue == NULL) {
|
if (rx_queue == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uint8_t c = 0;
|
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) {
|
size_t ESP_NOW_Serial_Class::read(uint8_t *buffer, size_t size) {
|
||||||
if (rx_queue == NULL) {
|
if (rx_queue == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uint8_t c = 0;
|
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() {
|
void ESP_NOW_Serial_Class::flush() {
|
||||||
if (tx_ring_buf == NULL) {
|
if (tx_ring_buf == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
UBaseType_t uxItemsWaiting = 0;
|
UBaseType_t uxItemsWaiting = 0;
|
||||||
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
|
vRingbufferGetInfo(tx_ring_buf, nullptr, nullptr, nullptr, nullptr, &uxItemsWaiting);
|
||||||
if (uxItemsWaiting) {
|
if (uxItemsWaiting) {
|
||||||
// Now trigger the ISR to read data from the ring buffer.
|
// Now trigger the ISR to read data from the ring buffer.
|
||||||
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
|
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
|
||||||
|
|
@ -141,13 +141,13 @@ void ESP_NOW_Serial_Class::flush() {
|
||||||
}
|
}
|
||||||
while (uxItemsWaiting) {
|
while (uxItemsWaiting) {
|
||||||
delay(5);
|
delay(5);
|
||||||
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
|
vRingbufferGetInfo(tx_ring_buf, nullptr, nullptr, nullptr, nullptr, &uxItemsWaiting);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//RX callback
|
//RX callback
|
||||||
void ESP_NOW_Serial_Class::onReceive(const uint8_t *data, size_t len, bool broadcast) {
|
void ESP_NOW_Serial_Class::onReceive(const uint8_t *data, size_t len, bool broadcast) {
|
||||||
if (rx_queue == NULL) {
|
if (rx_queue == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (uint32_t i = 0; i < len; i++) {
|
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
|
//Print
|
||||||
int ESP_NOW_Serial_Class::availableForWrite() {
|
int ESP_NOW_Serial_Class::availableForWrite() {
|
||||||
//return ESP_NOW_MAX_DATA_LEN;
|
//return ESP_NOW_MAX_DATA_LEN;
|
||||||
if (tx_ring_buf == NULL) {
|
if (tx_ring_buf == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return xRingbufferGetCurFreeSize(tx_ring_buf);
|
return xRingbufferGetCurFreeSize(tx_ring_buf);
|
||||||
|
|
@ -178,7 +178,7 @@ size_t ESP_NOW_Serial_Class::tryToSend() {
|
||||||
//_onSent will not be called anymore
|
//_onSent will not be called anymore
|
||||||
//the data is lost in this case
|
//the data is lost in this case
|
||||||
vRingbufferReturnItem(tx_ring_buf, queued_buff);
|
vRingbufferReturnItem(tx_ring_buf, queued_buff);
|
||||||
queued_buff = NULL;
|
queued_buff = nullptr;
|
||||||
xSemaphoreGive(tx_sem);
|
xSemaphoreGive(tx_sem);
|
||||||
end();
|
end();
|
||||||
}
|
}
|
||||||
|
|
@ -188,12 +188,12 @@ size_t ESP_NOW_Serial_Class::tryToSend() {
|
||||||
bool ESP_NOW_Serial_Class::checkForTxData() {
|
bool ESP_NOW_Serial_Class::checkForTxData() {
|
||||||
//do we have something that failed the last time?
|
//do we have something that failed the last time?
|
||||||
resend_count = 0;
|
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);
|
queued_buff = (uint8_t *)xRingbufferReceiveUpTo(tx_ring_buf, &queued_size, 0, ESP_NOW_MAX_DATA_LEN);
|
||||||
} else {
|
} else {
|
||||||
log_d(MACSTR " : PREVIOUS", MAC2STR(addr()));
|
log_d(MACSTR " : PREVIOUS", MAC2STR(addr()));
|
||||||
}
|
}
|
||||||
if (queued_buff != NULL) {
|
if (queued_buff != nullptr) {
|
||||||
return tryToSend() > 0;
|
return tryToSend() > 0;
|
||||||
}
|
}
|
||||||
//log_d(MACSTR ": EMPTY", MAC2STR(addr()));
|
//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) {
|
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);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
size_t space = availableForWrite();
|
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
|
//TX Done Callback
|
||||||
void ESP_NOW_Serial_Class::onSent(bool success) {
|
void ESP_NOW_Serial_Class::onSent(bool success) {
|
||||||
log_v(MACSTR " : %s", MAC2STR(addr()), success ? "OK" : "FAIL");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if (success) {
|
if (success) {
|
||||||
vRingbufferReturnItem(tx_ring_buf, queued_buff);
|
vRingbufferReturnItem(tx_ring_buf, queued_buff);
|
||||||
queued_buff = NULL;
|
queued_buff = nullptr;
|
||||||
//send next packet?
|
//send next packet?
|
||||||
//log_d(MACSTR ": NEXT", MAC2STR(addr()));
|
//log_d(MACSTR ": NEXT", MAC2STR(addr()));
|
||||||
checkForTxData();
|
checkForTxData();
|
||||||
|
|
@ -269,7 +269,7 @@ void ESP_NOW_Serial_Class::onSent(bool success) {
|
||||||
//resend limit reached
|
//resend limit reached
|
||||||
//the data is lost in this case
|
//the data is lost in this case
|
||||||
vRingbufferReturnItem(tx_ring_buf, queued_buff);
|
vRingbufferReturnItem(tx_ring_buf, queued_buff);
|
||||||
queued_buff = NULL;
|
queued_buff = nullptr;
|
||||||
log_e(MACSTR " : RE-SEND_MAX[%u]", MAC2STR(addr()), resend_count);
|
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 we are not able to send the data and remove_on_fail is set, remove the peer
|
||||||
if (_remove_on_fail) {
|
if (_remove_on_fail) {
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,9 @@ private:
|
||||||
size_t tryToSend();
|
size_t tryToSend();
|
||||||
|
|
||||||
public:
|
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();
|
~ESP_NOW_Serial_Class();
|
||||||
size_t setRxBufferSize(size_t);
|
size_t setRxBufferSize(size_t);
|
||||||
size_t setTxBufferSize(size_t);
|
size_t setTxBufferSize(size_t);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue