Skip to content

Commit f100838

Browse files
committed
remove peers_table from espnow
1 parent 14c3b52 commit f100838

3 files changed

Lines changed: 15 additions & 82 deletions

File tree

ports/espressif/bindings/espnow/ESPNow.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t
9595
common_hal_espnow_set_phy_rate(self, args[ARG_phy_rate].u_int);
9696

9797
self->peers = espnow_peers_new();
98-
self->peers_table = mp_obj_new_dict(0);
99-
100-
// Prevent user code modifying the dict
101-
mp_obj_dict_get_map(self->peers_table)->is_fixed = 1;
10298

10399
// Set the global singleton pointer for the espnow protocol.
104100
MP_STATE_PORT(espnow_singleton) = self;
@@ -277,25 +273,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_obj, espnow_get_peers);
277273
MP_PROPERTY_GETTER(espnow_peers_obj,
278274
(mp_obj_t)&espnow_get_peers_obj);
279275

280-
//| peers_table: Dict[bytes, List[int]]
281-
//| """The dictionary of peers we have seen. (read-only)
282-
//|
283-
//| A `dict` of {peer: [rssi, time], ...}
284-
//|
285-
//| where:
286-
//| * peer is a byte string containing the 6-byte mac address of the peer.
287-
//| * rssi is the wifi signal strength from the last msg received (in dBm from -127 to 0).
288-
//| * time is the time in milliseconds since device last booted."""
289-
//|
290-
STATIC mp_obj_t espnow_get_peers_table(mp_obj_t self_in) {
291-
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
292-
return self->peers_table;
293-
}
294-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_table_obj, espnow_get_peers_table);
295-
296-
MP_PROPERTY_GETTER(espnow_peers_table_obj,
297-
(mp_obj_t)&espnow_get_peers_table_obj);
298-
299276
STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
300277
// Context managers
301278
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
@@ -316,7 +293,6 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
316293

317294
// Peer related properties
318295
{ MP_ROM_QSTR(MP_QSTR_peers), MP_ROM_PTR(&espnow_peers_obj) },
319-
{ MP_ROM_QSTR(MP_QSTR_peers_table), MP_ROM_PTR(&espnow_peers_table_obj) },
320296
};
321297
STATIC MP_DEFINE_CONST_DICT(espnow_locals_dict, espnow_locals_dict_table);
322298

ports/espressif/common-hal/espnow/ESPNow.c

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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),

ports/espressif/common-hal/espnow/ESPNow.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ typedef struct _espnow_obj_t {
4545
volatile size_t tx_responses; // # of sent packet responses received
4646
volatile size_t tx_failures; // # of sent packet responses failed
4747
espnow_peers_obj_t *peers; // Cache the # of peers for send(sync=True)
48-
mp_obj_t peers_table; // A dictionary of discovered peers
4948
} espnow_obj_t;
5049

5150
extern void espnow_reset(void);

0 commit comments

Comments
 (0)