remove peers_table from espnow

This commit is contained in:
MicroDev 2023-02-03 10:14:50 +05:30
parent 14c3b52b8d
commit f100838ae5
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
3 changed files with 15 additions and 82 deletions

View File

@ -95,10 +95,6 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t
common_hal_espnow_set_phy_rate(self, args[ARG_phy_rate].u_int); common_hal_espnow_set_phy_rate(self, args[ARG_phy_rate].u_int);
self->peers = espnow_peers_new(); self->peers = espnow_peers_new();
self->peers_table = mp_obj_new_dict(0);
// Prevent user code modifying the dict
mp_obj_dict_get_map(self->peers_table)->is_fixed = 1;
// Set the global singleton pointer for the espnow protocol. // Set the global singleton pointer for the espnow protocol.
MP_STATE_PORT(espnow_singleton) = self; MP_STATE_PORT(espnow_singleton) = self;
@ -277,25 +273,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_obj, espnow_get_peers);
MP_PROPERTY_GETTER(espnow_peers_obj, MP_PROPERTY_GETTER(espnow_peers_obj,
(mp_obj_t)&espnow_get_peers_obj); (mp_obj_t)&espnow_get_peers_obj);
//| peers_table: Dict[bytes, List[int]]
//| """The dictionary of peers we have seen. (read-only)
//|
//| A `dict` of {peer: [rssi, time], ...}
//|
//| where:
//| * peer is a byte string containing the 6-byte mac address of the peer.
//| * rssi is the wifi signal strength from the last msg received (in dBm from -127 to 0).
//| * time is the time in milliseconds since device last booted."""
//|
STATIC mp_obj_t espnow_get_peers_table(mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return self->peers_table;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_table_obj, espnow_get_peers_table);
MP_PROPERTY_GETTER(espnow_peers_table_obj,
(mp_obj_t)&espnow_get_peers_table_obj);
STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = { STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
// Context managers // Context managers
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, { 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[] = {
// Peer related properties // Peer related properties
{ MP_ROM_QSTR(MP_QSTR_peers), MP_ROM_PTR(&espnow_peers_obj) }, { MP_ROM_QSTR(MP_QSTR_peers), MP_ROM_PTR(&espnow_peers_obj) },
{ MP_ROM_QSTR(MP_QSTR_peers_table), MP_ROM_PTR(&espnow_peers_table_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(espnow_locals_dict, espnow_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(espnow_locals_dict, espnow_locals_dict_table);

View File

@ -95,7 +95,21 @@ static void send_cb(const uint8_t *mac, esp_now_send_status_t status) {
} }
} }
static inline int8_t _get_rssi_from_wifi_packet(const uint8_t *msg); // Get the RSSI value from the wifi packet header
static inline int8_t _get_rssi_from_wifi_packet(const uint8_t *msg) {
// Warning: Secret magic to get the rssi from the wifi packet header
// See espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/
// In the wifi packet the msg comes after a wifi_promiscuous_pkt_t
// and a espnow_frame_format_t.
// Backtrack to get a pointer to the wifi_promiscuous_pkt_t.
#define SIZEOF_ESPNOW_FRAME_FORMAT 39
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
wifi_promiscuous_pkt_t *wifi_packet = (wifi_promiscuous_pkt_t *)(
msg - SIZEOF_ESPNOW_FRAME_FORMAT - sizeof(wifi_promiscuous_pkt_t));
#pragma GCC diagnostic pop
return wifi_packet->rx_ctrl.rssi;
}
// Callback triggered when an ESP-NOW packet is received. // Callback triggered when an ESP-NOW packet is received.
// Write the peer MAC address and the message into the recv_buffer as an ESPNow packet. // 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) {
check_esp_err(esp_now_set_pmk(key)); check_esp_err(esp_now_set_pmk(key));
} }
// --- Maintaining the peer table and reading RSSI values ---
// We maintain a peers table for several reasons, to:
// - support monitoring the RSSI values for all peers; and
// - to return unique bytestrings for each peer which supports more efficient
// application memory usage and peer handling.
// Get the RSSI value from the wifi packet header
static inline int8_t _get_rssi_from_wifi_packet(const uint8_t *msg) {
// Warning: Secret magic to get the rssi from the wifi packet header
// See espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/
// In the wifi packet the msg comes after a wifi_promiscuous_pkt_t
// and a espnow_frame_format_t.
// Backtrack to get a pointer to the wifi_promiscuous_pkt_t.
#define SIZEOF_ESPNOW_FRAME_FORMAT 39
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
wifi_promiscuous_pkt_t *wifi_packet = (wifi_promiscuous_pkt_t *)(
msg - SIZEOF_ESPNOW_FRAME_FORMAT - sizeof(wifi_promiscuous_pkt_t));
#pragma GCC diagnostic pop
return wifi_packet->rx_ctrl.rssi;
}
// Lookup a peer in the peers table and return a reference to the item in the peers_table.
// Add peer to the table if it is not found (may alloc memory). Will not return NULL.
static mp_map_elem_t *_lookup_add_peer(espnow_obj_t *self, const uint8_t *peer) {
// We do not want to allocate any new memory in the case that the peer
// already exists in the peers_table (which is almost all the time).
// So, we use a byte string on the stack and look that up in the dict.
mp_map_t *map = mp_obj_dict_get_map(self->peers_table);
mp_obj_str_t peer_obj = {{&mp_type_bytes}, 0, ESP_NOW_ETH_ALEN, peer};
mp_map_elem_t *item = mp_map_lookup(map, &peer_obj, MP_MAP_LOOKUP);
if (item == NULL) {
// If not found, add the peer using a new bytestring
map->is_fixed = 0; // Allow to modify the dict
mp_obj_t new_peer = mp_obj_new_bytes(peer, ESP_NOW_ETH_ALEN);
item = mp_map_lookup(map, new_peer, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
item->value = mp_obj_new_list(2, NULL);
map->is_fixed = 1; // Relock the dict
}
return item;
}
// Update the peers table with the new rssi value from a received packet and
// return a reference to the item in the peers_table.
static void _update_rssi(espnow_obj_t *self, const uint8_t *peer, int8_t rssi, uint32_t time_ms) {
// Lookup the peer in the device table
mp_map_elem_t *item = _lookup_add_peer(self, peer);
mp_obj_list_t *list = MP_OBJ_TO_PTR(item->value);
list->items[0] = MP_OBJ_NEW_SMALL_INT(rssi);
list->items[1] = mp_obj_new_int(time_ms);
}
// --- Send and Receive ESP-NOW data --- // --- Send and Receive ESP-NOW data ---
// Used by espnow_send() for sends() with sync==True. // Used by espnow_send() for sends() with sync==True.
@ -313,9 +274,6 @@ mp_obj_t common_hal_espnow_recv(espnow_obj_t *self) {
mp_arg_error_invalid(MP_QSTR_buffer); mp_arg_error_invalid(MP_QSTR_buffer);
} }
// Update rssi value in the peer device table
_update_rssi(self, mac_buf, header.rssi, header.time_ms);
mp_obj_t elems[4] = { mp_obj_t elems[4] = {
mp_obj_new_bytes(mac_buf, ESP_NOW_ETH_ALEN), mp_obj_new_bytes(mac_buf, ESP_NOW_ETH_ALEN),
mp_obj_new_bytes(msg_buf, msg_len), mp_obj_new_bytes(msg_buf, msg_len),

View File

@ -45,7 +45,6 @@ typedef struct _espnow_obj_t {
volatile size_t tx_responses; // # of sent packet responses received volatile size_t tx_responses; // # of sent packet responses received
volatile size_t tx_failures; // # of sent packet responses failed volatile size_t tx_failures; // # of sent packet responses failed
espnow_peers_obj_t *peers; // Cache the # of peers for send(sync=True) espnow_peers_obj_t *peers; // Cache the # of peers for send(sync=True)
mp_obj_t peers_table; // A dictionary of discovered peers
} espnow_obj_t; } espnow_obj_t;
extern void espnow_reset(void); extern void espnow_reset(void);