2021-11-02 01:30:11 -04:00
|
|
|
/*
|
|
|
|
* 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."""
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2021-11-02 01:30:11 -04:00
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| def __init__(self, channel: Optional[int] = 1, queue: Optional[int] = 128) -> None:
|
|
|
|
//| """Initialize `wifi.Monitor` singleton.
|
2021-11-02 01:30:11 -04:00
|
|
|
//|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| :param int channel: The WiFi channel to scan.
|
|
|
|
//| :param int queue: The queue size for buffering the packet.
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| """
|
|
|
|
//| ...
|
2021-11-02 02:54:48 -04:00
|
|
|
STATIC mp_obj_t wifi_monitor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2021-11-02 01:30:11 -04:00
|
|
|
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)];
|
2021-11-02 02:54:48 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2021-11-02 01:30:11 -04:00
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_int_t channel = mp_arg_validate_int_range(args[ARG_channel].u_int, 1, 13, MP_QSTR_channel);
|
|
|
|
mp_int_t queue = mp_arg_validate_int_min(args[ARG_queue].u_int, 0, MP_QSTR_queue);
|
2021-11-02 01:30:11 -04:00
|
|
|
|
|
|
|
wifi_monitor_obj_t *self = MP_STATE_VM(wifi_monitor_singleton);
|
|
|
|
if (common_hal_wifi_monitor_deinited()) {
|
2023-08-07 20:45:57 -04:00
|
|
|
self = mp_obj_malloc(wifi_monitor_obj_t, &wifi_monitor_type);
|
2022-05-13 15:33:43 -04:00
|
|
|
common_hal_wifi_monitor_construct(self, channel, queue);
|
2021-11-02 01:30:11 -04:00
|
|
|
MP_STATE_VM(wifi_monitor_singleton) = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| channel: int
|
|
|
|
//| """The WiFi channel to scan."""
|
2021-11-02 01:30:11 -04:00
|
|
|
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) {
|
2022-02-08 18:14:28 -05:00
|
|
|
mp_int_t c = mp_obj_get_int(channel);
|
|
|
|
if (c < 1 || c > 13) {
|
2022-02-08 18:55:44 -05:00
|
|
|
mp_raise_ValueError_varg(translate("%q out of bounds"), MP_QSTR_channel);
|
2022-02-08 18:14:28 -05:00
|
|
|
}
|
|
|
|
common_hal_wifi_monitor_set_channel(self_in, c);
|
2021-11-02 01:30:11 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(wifi_monitor_set_channel_obj, wifi_monitor_obj_set_channel);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(wifi_monitor_channel_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&wifi_monitor_get_channel_obj,
|
|
|
|
(mp_obj_t)&wifi_monitor_set_channel_obj);
|
2021-11-02 01:30:11 -04:00
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| queue: int
|
|
|
|
//| """The queue size for buffering the packet."""
|
2021-11-02 01:30:11 -04:00
|
|
|
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);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(wifi_monitor_queue_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&wifi_monitor_get_queue_obj);
|
2021-11-02 01:30:11 -04:00
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| def deinit(self) -> None:
|
|
|
|
//| """De-initialize `wifi.Monitor` singleton."""
|
|
|
|
//| ...
|
2021-11-02 01:30:11 -04:00
|
|
|
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);
|
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| def lost(self) -> int:
|
|
|
|
//| """Returns the packet loss count. The counter resets after each poll."""
|
|
|
|
//| ...
|
2021-11-04 06:44:18 -04:00
|
|
|
STATIC mp_obj_t wifi_monitor_obj_get_lost(mp_obj_t self_in) {
|
|
|
|
return common_hal_wifi_monitor_get_lost(self_in);
|
2021-11-02 01:30:11 -04:00
|
|
|
}
|
2021-11-04 06:44:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_lost_obj, wifi_monitor_obj_get_lost);
|
2021-11-02 01:30:11 -04:00
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| def queued(self) -> int:
|
|
|
|
//| """Returns the packet queued count."""
|
|
|
|
//| ...
|
2021-11-04 06:44:18 -04:00
|
|
|
STATIC mp_obj_t wifi_monitor_obj_get_queued(mp_obj_t self_in) {
|
|
|
|
if (common_hal_wifi_monitor_deinited()) {
|
|
|
|
return mp_obj_new_int_from_uint(0);
|
|
|
|
}
|
|
|
|
return common_hal_wifi_monitor_get_queued(self_in);
|
2021-11-02 01:30:11 -04:00
|
|
|
}
|
2021-11-04 06:44:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_queued_obj, wifi_monitor_obj_get_queued);
|
2021-11-02 01:30:11 -04:00
|
|
|
|
2022-11-28 14:30:08 -05:00
|
|
|
//| def packet(self) -> dict:
|
|
|
|
//| """Returns the monitor packet."""
|
|
|
|
//| ...
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2021-11-02 01:30:11 -04:00
|
|
|
STATIC mp_obj_t wifi_monitor_obj_get_packet(mp_obj_t self_in) {
|
2021-11-04 06:44:18 -04:00
|
|
|
if (common_hal_wifi_monitor_deinited()) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
2021-11-02 01:30:11 -04:00
|
|
|
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) },
|
2021-11-04 06:44:18 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_queue), MP_ROM_PTR(&wifi_monitor_queue_obj) },
|
2021-11-02 01:30:11 -04:00
|
|
|
|
|
|
|
// functions
|
2021-11-04 06:44:18 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&wifi_monitor_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_lost), MP_ROM_PTR(&wifi_monitor_lost_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_queued), MP_ROM_PTR(&wifi_monitor_queued_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_packet), MP_ROM_PTR(&wifi_monitor_packet_obj) },
|
2021-11-02 01:30:11 -04:00
|
|
|
};
|
|
|
|
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,
|
|
|
|
};
|