Skip to content

Commit 07f7de0

Browse files
committed
Add argument to receive callback to know if a message was broadcasted
1 parent 6bf981e commit 07f7de0

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

libraries/ESP_NOW/src/ESP32_NOW.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ static esp_err_t _esp_now_modify_peer(const uint8_t *mac_addr, uint8_t channel,
104104
}
105105

106106
static void _esp_now_rx_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len){
107-
log_v(MACSTR", data lenght : %d", MAC2STR(info->src_addr), len);
107+
bool broadcast = memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
108+
log_v("%s from " MACSTR ", data lenght : %u", broadcast ? "Broadcast" : "Unicast", MAC2STR(info->src_addr), len);
108109
log_buf_v(data, len);
109110
if(!esp_now_is_peer_exist(info->src_addr) && new_cb != NULL){
110111
log_v("Calling new_cb, peer not found.");
@@ -114,7 +115,7 @@ static void _esp_now_rx_cb(const esp_now_recv_info_t *info, const uint8_t *data,
114115
//find the peer and call it's callback
115116
for(uint8_t i=0; i<ESP_NOW_MAX_TOTAL_PEER_NUM; i++){
116117
if(_esp_now_peers[i] != NULL && memcmp(info->src_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0){
117-
_esp_now_peers[i]->_onReceive(data, len);
118+
_esp_now_peers[i]->_onReceive(data, len, broadcast);
118119
return;
119120
}
120121
}

libraries/ESP_NOW/src/ESP32_NOW.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "esp_wifi_types.h"
44
#include "Print.h"
55
#include "esp_now.h"
6-
#include "MacAddress.h"
76

87
class ESP_NOW_Peer {
98
private:
@@ -38,13 +37,13 @@ class ESP_NOW_Peer {
3837
operator bool() const;
3938

4039
//must be implemented by the upper class
41-
virtual void _onReceive(const uint8_t * data, size_t len) = 0;
40+
virtual void _onReceive(const uint8_t * data, size_t len, bool broadcast) = 0;
4241
virtual void _onSent(bool success) = 0;
4342
};
4443

4544
class ESP_NOW_Class : public Print {
4645
public:
47-
const MacAddress BROADCAST_ADDR = MacAddress(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
46+
const uint8_t BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
4847

4948
ESP_NOW_Class();
5049
~ESP_NOW_Class();

libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ void ESP_NOW_Serial_Class::flush(){
140140
}
141141

142142
//RX callback
143-
void ESP_NOW_Serial_Class::_onReceive(const uint8_t * data, size_t len){
143+
void ESP_NOW_Serial_Class::_onReceive(const uint8_t * data, size_t len, bool broadcast){
144144
if(rx_queue == NULL){
145145
return;
146146
}
147-
log_v(MACSTR ", data lenght : %u", MAC2STR(addr()), len);
147+
log_v("%s from " MACSTR ", data lenght : %u", broadcast ? "Broadcast" : "Unicast", MAC2STR(addr()), len);
148148
for(uint32_t i=0; i<len; i++){
149149
if(!xQueueSend(rx_queue, data+i, 0)){
150150
log_e("RX Overflow!");

libraries/ESP_NOW/src/ESP32_NOW_Serial.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ESP_NOW_Serial_Class : public Stream, public ESP_NOW_Peer {
3939
size_t write(const uint8_t *buffer, size_t size){ return write(buffer, size, 1000); }
4040
size_t write(uint8_t data){ return write(&data, 1); }
4141
//ESP_NOW_Peer
42-
void _onReceive(const uint8_t * data, size_t len);
42+
void _onReceive(const uint8_t * data, size_t len, bool broadcast);
4343
void _onSent(bool success);
4444
};
4545

0 commit comments

Comments
 (0)