@@ -95,7 +95,21 @@ static void send_cb(const uint8_t *mac, esp_now_send_status_t status) {
9595 }
9696}
9797
98- static inline int8_t _get_rssi_from_wifi_packet (const uint8_t * msg );
98+ // Get the RSSI value from the wifi packet header
99+ static inline int8_t _get_rssi_from_wifi_packet (const uint8_t * msg ) {
100+ // Warning: Secret magic to get the rssi from the wifi packet header
101+ // See espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/
102+ // In the wifi packet the msg comes after a wifi_promiscuous_pkt_t
103+ // and a espnow_frame_format_t.
104+ // Backtrack to get a pointer to the wifi_promiscuous_pkt_t.
105+ #define SIZEOF_ESPNOW_FRAME_FORMAT 39
106+ #pragma GCC diagnostic push
107+ #pragma GCC diagnostic ignored "-Wcast-align"
108+ wifi_promiscuous_pkt_t * wifi_packet = (wifi_promiscuous_pkt_t * )(
109+ msg - SIZEOF_ESPNOW_FRAME_FORMAT - sizeof (wifi_promiscuous_pkt_t ));
110+ #pragma GCC diagnostic pop
111+ return wifi_packet -> rx_ctrl .rssi ;
112+ }
99113
100114// Callback triggered when an ESP-NOW packet is received.
101115// Write the peer MAC address and the message into the recv_buffer as an ESPNow packet.
@@ -185,59 +199,6 @@ void common_hal_espnow_set_pmk(espnow_obj_t *self, const uint8_t *key) {
185199 check_esp_err (esp_now_set_pmk (key ));
186200}
187201
188- // --- Maintaining the peer table and reading RSSI values ---
189-
190- // We maintain a peers table for several reasons, to:
191- // - support monitoring the RSSI values for all peers; and
192- // - to return unique bytestrings for each peer which supports more efficient
193- // application memory usage and peer handling.
194-
195- // Get the RSSI value from the wifi packet header
196- static inline int8_t _get_rssi_from_wifi_packet (const uint8_t * msg ) {
197- // Warning: Secret magic to get the rssi from the wifi packet header
198- // See espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/
199- // In the wifi packet the msg comes after a wifi_promiscuous_pkt_t
200- // and a espnow_frame_format_t.
201- // Backtrack to get a pointer to the wifi_promiscuous_pkt_t.
202- #define SIZEOF_ESPNOW_FRAME_FORMAT 39
203- #pragma GCC diagnostic push
204- #pragma GCC diagnostic ignored "-Wcast-align"
205- wifi_promiscuous_pkt_t * wifi_packet = (wifi_promiscuous_pkt_t * )(
206- msg - SIZEOF_ESPNOW_FRAME_FORMAT - sizeof (wifi_promiscuous_pkt_t ));
207- #pragma GCC diagnostic pop
208- return wifi_packet -> rx_ctrl .rssi ;
209- }
210-
211- // Lookup a peer in the peers table and return a reference to the item in the peers_table.
212- // Add peer to the table if it is not found (may alloc memory). Will not return NULL.
213- static mp_map_elem_t * _lookup_add_peer (espnow_obj_t * self , const uint8_t * peer ) {
214- // We do not want to allocate any new memory in the case that the peer
215- // already exists in the peers_table (which is almost all the time).
216- // So, we use a byte string on the stack and look that up in the dict.
217- mp_map_t * map = mp_obj_dict_get_map (self -> peers_table );
218- mp_obj_str_t peer_obj = {{& mp_type_bytes }, 0 , ESP_NOW_ETH_ALEN , peer };
219- mp_map_elem_t * item = mp_map_lookup (map , & peer_obj , MP_MAP_LOOKUP );
220- if (item == NULL ) {
221- // If not found, add the peer using a new bytestring
222- map -> is_fixed = 0 ; // Allow to modify the dict
223- mp_obj_t new_peer = mp_obj_new_bytes (peer , ESP_NOW_ETH_ALEN );
224- item = mp_map_lookup (map , new_peer , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND );
225- item -> value = mp_obj_new_list (2 , NULL );
226- map -> is_fixed = 1 ; // Relock the dict
227- }
228- return item ;
229- }
230-
231- // Update the peers table with the new rssi value from a received packet and
232- // return a reference to the item in the peers_table.
233- static void _update_rssi (espnow_obj_t * self , const uint8_t * peer , int8_t rssi , uint32_t time_ms ) {
234- // Lookup the peer in the device table
235- mp_map_elem_t * item = _lookup_add_peer (self , peer );
236- mp_obj_list_t * list = MP_OBJ_TO_PTR (item -> value );
237- list -> items [0 ] = MP_OBJ_NEW_SMALL_INT (rssi );
238- list -> items [1 ] = mp_obj_new_int (time_ms );
239- }
240-
241202// --- Send and Receive ESP-NOW data ---
242203
243204// Used by espnow_send() for sends() with sync==True.
@@ -313,9 +274,6 @@ mp_obj_t common_hal_espnow_recv(espnow_obj_t *self) {
313274 mp_arg_error_invalid (MP_QSTR_buffer );
314275 }
315276
316- // Update rssi value in the peer device table
317- _update_rssi (self , mac_buf , header .rssi , header .time_ms );
318-
319277 mp_obj_t elems [4 ] = {
320278 mp_obj_new_bytes (mac_buf , ESP_NOW_ETH_ALEN ),
321279 mp_obj_new_bytes (msg_buf , msg_len ),
0 commit comments