add monitor class

Co-authored-by: anecdata <16617689+anecdata@users.noreply.github.com>
This commit is contained in:
microDev 2021-11-02 11:00:11 +05:30
parent e2652f8aac
commit 95172cf3ce
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
9 changed files with 446 additions and 12 deletions

View File

@ -148,6 +148,10 @@ msgstr ""
msgid "%q must be power of 2"
msgstr ""
#: shared-bindings/wifi/Monitor.c
msgid "%q out of bounds"
msgstr ""
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
#: shared-bindings/canio/Match.c
msgid "%q out of range"
@ -3553,6 +3557,10 @@ msgstr ""
msgid "module not found"
msgstr ""
#: ports/espressif/common-hal/wifi/Monitor.c
msgid "monitor init failed"
msgstr ""
#: extmod/ulab/code/numpy/poly.c
msgid "more degrees of freedom than data points"
msgstr ""

View File

@ -0,0 +1,166 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "py/mpstate.h"
#include "py/runtime.h"
#include "shared-bindings/wifi/Monitor.h"
#include "shared-bindings/wifi/Packet.h"
#include "esp_log.h"
#define MONITOR_PAYLOAD_FCS_LEN (4)
#define MONITOR_QUEUE_TIMEOUT_TICK (0)
typedef struct {
void *payload;
unsigned channel;
uint32_t length;
signed rssi;
} monitor_packet_t;
static const char *TAG = "monitor";
static void wifi_monitor_cb(void *recv_buf, wifi_promiscuous_pkt_type_t type) {
wifi_promiscuous_pkt_t *pkt = (wifi_promiscuous_pkt_t *)recv_buf;
// prepare packet
monitor_packet_t packet = {
.channel = pkt->rx_ctrl.channel,
.length = pkt->rx_ctrl.sig_len,
.rssi = pkt->rx_ctrl.rssi,
};
// for now, the monitor only dumps the length of the MISC type frame
if (type != WIFI_PKT_MISC && !pkt->rx_ctrl.rx_state) {
packet.length -= MONITOR_PAYLOAD_FCS_LEN;
packet.payload = malloc(packet.length);
if (packet.payload) {
memcpy(packet.payload, pkt->payload, packet.length);
wifi_monitor_obj_t *self = MP_STATE_VM(wifi_monitor_singleton);
if (self->queue) {
// send packet
if (xQueueSendFromISR(self->queue, &packet, NULL) != pdTRUE) {
self->loss++;
free(packet.payload);
ESP_LOGE(TAG, "packet queue full");
}
}
} else {
ESP_LOGE(TAG, "not enough memory for packet");
}
}
}
void common_hal_wifi_monitor_construct(wifi_monitor_obj_t *self, uint8_t channel, size_t queue) {
const compressed_string_t *monitor_mode_init_error = translate("monitor init failed");
self->queue = xQueueCreate(queue, sizeof(monitor_packet_t));
if (!self->queue) {
mp_raise_RuntimeError(monitor_mode_init_error);
}
// start wifi promicuous mode
wifi_promiscuous_filter_t wifi_filter = {
.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT,
};
esp_wifi_set_promiscuous_filter(&wifi_filter);
esp_wifi_set_promiscuous_rx_cb(wifi_monitor_cb);
if (esp_wifi_set_promiscuous(true) != ESP_OK) {
mp_raise_RuntimeError(monitor_mode_init_error);
}
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
self->channel = channel;
self->queue_length = queue;
}
bool common_hal_wifi_monitor_deinited(void) {
bool enabled;
esp_wifi_get_promiscuous(&enabled);
return !enabled;
}
void common_hal_wifi_monitor_deinit(wifi_monitor_obj_t *self) {
if (common_hal_wifi_monitor_deinited()) {
return;
}
// disable wifi promiscuous mode
esp_wifi_set_promiscuous(false);
// make sure to free all resources in the left items
UBaseType_t left_items = uxQueueMessagesWaiting(self->queue);
monitor_packet_t packet;
while (left_items--) {
xQueueReceive(self->queue, &packet, MONITOR_QUEUE_TIMEOUT_TICK);
free(packet.payload);
}
vQueueDelete(self->queue);
self->queue = NULL;
}
void common_hal_wifi_monitor_set_channel(wifi_monitor_obj_t *self, uint8_t channel) {
self->channel = channel;
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
}
mp_obj_t common_hal_wifi_monitor_get_channel(wifi_monitor_obj_t *self) {
return MP_OBJ_NEW_SMALL_INT(self->channel);
}
mp_obj_t common_hal_wifi_monitor_get_queue(wifi_monitor_obj_t *self) {
return mp_obj_new_int_from_uint(self->queue_length);
}
mp_obj_t common_hal_wifi_monitor_get_loss(wifi_monitor_obj_t *self) {
size_t loss = self->loss;
self->loss = 0;
return mp_obj_new_int_from_uint(loss);
}
mp_obj_t common_hal_wifi_monitor_get_packet(wifi_monitor_obj_t *self) {
monitor_packet_t packet;
if (xQueueReceive(self->queue, &packet, MONITOR_QUEUE_TIMEOUT_TICK) != pdTRUE) {
return (mp_obj_t)&mp_const_empty_dict_obj;
}
mp_obj_dict_t *dict = MP_OBJ_TO_PTR(mp_obj_new_dict(4));
mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_CH), MP_OBJ_NEW_SMALL_INT(packet.channel));
mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_LEN), MP_OBJ_NEW_SMALL_INT(packet.length));
mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_RAW), mp_obj_new_bytes(packet.payload, packet.length));
free(packet.payload);
mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_RSSI), MP_OBJ_NEW_SMALL_INT(packet.rssi));
return MP_OBJ_FROM_PTR(dict);
}

View File

@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_WIFI_MONITOR_H
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_WIFI_MONITOR_H
#include "py/obj.h"
#include "components/esp_wifi/include/esp_wifi.h"
typedef struct {
mp_obj_base_t base;
uint8_t channel;
size_t loss;
size_t queue_length;
QueueHandle_t queue;
} wifi_monitor_obj_t;
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_WIFI_MONITOR_H

View File

@ -27,8 +27,10 @@
#include "common-hal/wifi/__init__.h"
#include "shared-bindings/ipaddress/IPv4Address.h"
#include "shared-bindings/wifi/Monitor.h"
#include "shared-bindings/wifi/Radio.h"
#include "py/mpstate.h"
#include "py/runtime.h"
#include "components/esp_wifi/include/esp_wifi.h"
@ -158,6 +160,7 @@ void wifi_reset(void) {
if (!wifi_inited) {
return;
}
common_hal_wifi_monitor_deinit(MP_STATE_VM(wifi_monitor_singleton));
wifi_radio_obj_t *radio = &common_hal_wifi_radio_obj;
common_hal_wifi_radio_set_enabled(radio, false);
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT,

View File

@ -319,6 +319,12 @@ extern const struct _mp_obj_module_t nvm_module;
#endif
#endif
#if CIRCUITPY_WIFI
#define WIFI_MONITOR_ROOT_POINTERS mp_obj_t wifi_monitor_singleton;
#else
#define WIFI_MONITOR_ROOT_POINTERS
#endif
// Define certain native modules with weak links so they can be replaced with Python
// implementations. This list may grow over time.
@ -442,6 +448,7 @@ struct _supervisor_allocation_node;
KEYPAD_ROOT_POINTERS \
GAMEPAD_ROOT_POINTERS \
BOARD_UART_ROOT_POINTER \
WIFI_MONITOR_ROOT_POINTERS \
MEMORYMONITOR_ROOT_POINTERS \
vstr_t *repl_line; \
mp_obj_t pew_singleton; \

View File

@ -0,0 +1,165 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mpstate.h"
#include "py/runtime.h"
#include "py/objproperty.h"
#include "shared-bindings/util.h"
#include "shared-bindings/wifi/Packet.h"
#include "shared-bindings/wifi/Monitor.h"
//| class Monitor:
//| """For monitoring WiFi packets."""
//|
//| def __init__(self, channel: Optional[int] = 1, queue: Optional[int] = 128) -> None:
//| """Initialize `wifi.Monitor` singleton.
//|
//| :param int channel: The WiFi channel to scan.
//| :param int queue: The queue size for buffering the packet.
//|
//| """
//| ...
//|
STATIC mp_obj_t wifi_monitor_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_channel, ARG_queue };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_channel, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
{ MP_QSTR_queue, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 128} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (args[ARG_channel].u_int < 0 || args[ARG_channel].u_int > 11) {
mp_raise_ValueError_varg(translate("%q out of bounds"), MP_QSTR_channel);
}
if (args[ARG_queue].u_int < 0) {
mp_raise_ValueError_varg(translate("%q out of bounds"), MP_QSTR_channel);
}
wifi_monitor_obj_t *self = MP_STATE_VM(wifi_monitor_singleton);
if (common_hal_wifi_monitor_deinited()) {
self = m_new_obj(wifi_monitor_obj_t);
self->base.type = &wifi_monitor_type;
common_hal_wifi_monitor_construct(self, args[ARG_channel].u_int, args[ARG_queue].u_int);
MP_STATE_VM(wifi_monitor_singleton) = self;
}
return MP_OBJ_FROM_PTR(self);
}
//| channel: int
//| """The WiFi channel to scan."""
//|
STATIC mp_obj_t wifi_monitor_obj_get_channel(mp_obj_t self_in) {
return common_hal_wifi_monitor_get_channel(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_get_channel_obj, wifi_monitor_obj_get_channel);
STATIC mp_obj_t wifi_monitor_obj_set_channel(mp_obj_t self_in, mp_obj_t channel) {
common_hal_wifi_monitor_set_channel(self_in, mp_obj_get_int(channel));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_monitor_set_channel_obj, wifi_monitor_obj_set_channel);
const mp_obj_property_t wifi_monitor_channel_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_monitor_get_channel_obj,
(mp_obj_t)&wifi_monitor_set_channel_obj,
MP_ROM_NONE },
};
//| queue: int
//| """The queue size for buffering the packet."""
//|
STATIC mp_obj_t wifi_monitor_obj_get_queue(mp_obj_t self_in) {
return common_hal_wifi_monitor_get_queue(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_get_queue_obj, wifi_monitor_obj_get_queue);
const mp_obj_property_t wifi_monitor_queue_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_monitor_get_queue_obj,
MP_ROM_NONE,
MP_ROM_NONE },
};
//| def deinit(self) -> None:
//| """De-initialize `wifi.Monitor` singleton."""
//| ...
//|
STATIC mp_obj_t wifi_monitor_obj_deinit(mp_obj_t self_in) {
common_hal_wifi_monitor_deinit(self_in);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_deinit_obj, wifi_monitor_obj_deinit);
STATIC void check_for_deinit(mp_obj_t self_in) {
if (common_hal_wifi_monitor_deinited()) {
raise_deinited_error();
}
}
//| def loss(self) -> int:
//| """Returns the packet loss count. The counter resets after each poll."""
//| ...
//|
STATIC mp_obj_t wifi_monitor_obj_get_loss(mp_obj_t self_in) {
return common_hal_wifi_monitor_get_loss(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_loss_obj, wifi_monitor_obj_get_loss);
//| def packet(self) -> dict:
//| """Returns the monitor packet."""
//| ...
//|
STATIC mp_obj_t wifi_monitor_obj_get_packet(mp_obj_t self_in) {
check_for_deinit(self_in);
return common_hal_wifi_monitor_get_packet(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_packet_obj, wifi_monitor_obj_get_packet);
STATIC const mp_rom_map_elem_t wifi_monitor_locals_dict_table[] = {
// properties
{ MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_monitor_channel_obj) },
{ MP_ROM_QSTR(MP_QSTR_queue), MP_ROM_PTR(&wifi_monitor_queue_obj) },
// functions
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&wifi_monitor_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_loss), MP_ROM_PTR(&wifi_monitor_loss_obj) },
{ MP_ROM_QSTR(MP_QSTR_packet), MP_ROM_PTR(&wifi_monitor_packet_obj) },
};
STATIC MP_DEFINE_CONST_DICT(wifi_monitor_locals_dict, wifi_monitor_locals_dict_table);
const mp_obj_type_t wifi_monitor_type = {
.base = { &mp_type_type },
.name = MP_QSTR_Monitor,
.make_new = wifi_monitor_make_new,
.locals_dict = (mp_obj_t)&wifi_monitor_locals_dict,
};

View File

@ -0,0 +1,48 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_MONITOR_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_MONITOR_H
#include "common-hal/wifi/Monitor.h"
const mp_obj_type_t wifi_monitor_type;
void common_hal_wifi_monitor_construct(wifi_monitor_obj_t *self,
uint8_t channel, size_t queue);
void common_hal_wifi_monitor_deinit(wifi_monitor_obj_t *self);
bool common_hal_wifi_monitor_deinited(void);
void common_hal_wifi_monitor_set_channel(wifi_monitor_obj_t *self, uint8_t channel);
mp_obj_t common_hal_wifi_monitor_get_channel(wifi_monitor_obj_t *self);
mp_obj_t common_hal_wifi_monitor_get_queue(wifi_monitor_obj_t *self);
mp_obj_t common_hal_wifi_monitor_get_loss(wifi_monitor_obj_t *self);
mp_obj_t common_hal_wifi_monitor_get_packet(wifi_monitor_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_MONITOR_H

View File

@ -24,13 +24,12 @@
* THE SOFTWARE.
*/
#include "py/objexcept.h"
#include "py/runtime.h"
#include "shared-bindings/wifi/__init__.h"
#include "shared-bindings/wifi/Radio.h"
#include "shared-bindings/wifi/Network.h"
#include "shared-bindings/wifi/AuthMode.h"
#include "shared-bindings/wifi/Network.h"
#include "shared-bindings/wifi/Monitor.h"
#include "shared-bindings/wifi/Packet.h"
#include "shared-bindings/wifi/Radio.h"
//| """
//| The `wifi` module provides necessary low-level functionality for managing
@ -41,7 +40,6 @@
//| This object is the sole instance of `wifi.Radio`."""
//|
// Called when wifi is imported.
STATIC mp_obj_t wifi___init__(void) {
common_hal_wifi_init();
@ -49,25 +47,25 @@ STATIC mp_obj_t wifi___init__(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(wifi___init___obj, wifi___init__);
STATIC const mp_rom_map_elem_t wifi_module_globals_table[] = {
// Name
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_wifi) },
// Initialization
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&wifi___init___obj) },
{ MP_ROM_QSTR(MP_QSTR_Radio), MP_ROM_PTR(&wifi_radio_type) },
{ MP_ROM_QSTR(MP_QSTR_Network), MP_ROM_PTR(&wifi_network_type) },
// Classes
{ MP_ROM_QSTR(MP_QSTR_AuthMode), MP_ROM_PTR(&wifi_authmode_type) },
{ MP_ROM_QSTR(MP_QSTR_Monitor), MP_ROM_PTR(&wifi_monitor_type) },
{ MP_ROM_QSTR(MP_QSTR_Network), MP_ROM_PTR(&wifi_network_type) },
{ MP_ROM_QSTR(MP_QSTR_Packet), MP_ROM_PTR(&wifi_packet_type) },
{ MP_ROM_QSTR(MP_QSTR_Radio), MP_ROM_PTR(&wifi_radio_type) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_radio), MP_ROM_PTR(&common_hal_wifi_radio_obj) },
};
STATIC MP_DEFINE_CONST_DICT(wifi_module_globals, wifi_module_globals_table);
const mp_obj_module_t wifi_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&wifi_module_globals,

View File

@ -27,8 +27,6 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WIFI___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI___INIT___H
#include "py/objlist.h"
#include "shared-bindings/wifi/Radio.h"
extern wifi_radio_obj_t common_hal_wifi_radio_obj;