23 #include "../../interfaces/ARDUINO/ESPNOWHelper.h"
26 #ifndef EN_RESPONSE_TIMEOUT
27 #define EN_RESPONSE_TIMEOUT 100000ul
30 #ifndef EN_MAX_REMOTE_NODES
31 #define EN_MAX_REMOTE_NODES 10
35 #ifndef EN_RECEIVE_TIME
36 #define EN_RECEIVE_TIME 0
39 #define EN_MAGIC_HEADER (uint8_t*)"\xEE\xFE\x0E\xEF"
43 bool _espnow_initialised =
false;
44 bool _auto_registration =
true;
45 uint8_t _channel = 14;
46 char _espnow_pmk[17] =
47 "\xdd\xdb\xdd\x44\x34\xd5\x6a\x0b\x7e\x9f\x4e\x27\xd6\x5b\xa2\x81";
50 uint8_t _remote_node_count = 0;
51 uint8_t _remote_id[EN_MAX_REMOTE_NODES];
52 uint8_t _remote_mac[EN_MAX_REMOTE_NODES][ESP_NOW_ETH_ALEN];
58 if(!_espnow_initialised) {
59 en.set_magic_header(EN_MAGIC_HEADER);
60 if(en.begin(_channel,(uint8_t*)_espnow_pmk)) {
61 _espnow_initialised =
true;
64 return _espnow_initialised;
67 int16_t find_remote_node(uint8_t
id, uint8_t* mac)
69 for(uint8_t i = 0; i < _remote_node_count; i++)
71 ((_remote_id[i] ==
id) && (
id != PJON_NOT_ASSIGNED)) ||
72 (memcmp(_remote_mac, mac, ESP_NOW_ETH_ALEN) == 0)
79 void autoregister_sender(
const uint8_t *message, uint16_t length)
82 if(_auto_registration && length>4) {
85 PJONTools::parse_header(message, packet_info);
86 uint8_t sender_id = packet_info.tx.id;
88 ESP_LOGE(
"ESPNOW",
"AutoRegister parsing failed");
93 uint8_t sender_mac[ESP_NOW_ETH_ALEN];
94 en.get_sender(sender_mac);
97 int16_t pos = find_remote_node(sender_id, sender_mac);
99 ESP_LOGI(
"ESPNOW",
"Autoregister new sender %d",sender_id);
100 add_node(sender_id, sender_mac);
101 }
else if(memcmp(_remote_mac[pos], sender_mac, ESP_NOW_ETH_ALEN) == 0) {
105 "Update sender sender_id(%d) [%02X:%02X:%02X:%02X:%02X:%02X]",
114 memcpy(_remote_mac[pos], sender_mac, ESP_NOW_ETH_ALEN);
121 void set_pmk(
char *espnow_pmk)
123 memcpy(_espnow_pmk, espnow_pmk, 16);
126 void set_channel(uint8_t channel)
138 if(_remote_node_count == EN_MAX_REMOTE_NODES) {
141 _remote_id[_remote_node_count] = remote_id;
142 memcpy(_remote_mac[_remote_node_count], remote_mac, ESP_NOW_ETH_ALEN);
143 en.add_node_mac(remote_mac);
144 _remote_node_count++;
145 return _remote_node_count - 1;
151 void set_autoregistration(
bool enabled)
153 _auto_registration = enabled;
159 uint32_t back_off(uint8_t attempts)
161 return 10000ul * attempts + PJON_RANDOM(10000);
168 bool begin(uint8_t = 0)
184 static uint8_t get_max_attempts()
192 static uint16_t get_receive_time()
194 return EN_RECEIVE_TIME;
200 void handle_collision() { };
205 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
207 uint16_t length = en.receive_frame(
data, max_length);
208 if(length != PJON_FAIL) {
209 autoregister_sender(
data, length);
217 uint16_t receive_response()
222 uint32_t start = PJON_MICROS();
223 uint8_t result[PJON_PACKET_MAX_LENGTH];
224 uint16_t reply_length = 0;
226 reply_length = receive_frame(result,
sizeof result);
228 if(reply_length == 1)
229 if(result[0] == PJON_ACK) {
233 }
while ((uint32_t)(PJON_MICROS() - start) < EN_RESPONSE_TIMEOUT);
241 void send_response(uint8_t response)
243 en.send_response(response);
249 void send_frame(uint8_t *
data, uint16_t length)
252 uint8_t
id =
data[0];
254 PJONTools::parse_header(
data, packet_info);
256 en.send_frame(
data, length);
258 int16_t pos = find_remote_node(
id, packet_info.rx.mac);
260 en.send_frame(
data, length, _remote_mac[pos]);
262 en.send_frame(
data, length);