diff options
Diffstat (limited to 'circuitpython/shared-bindings/wifi')
-rw-r--r-- | circuitpython/shared-bindings/wifi/AuthMode.c | 76 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/AuthMode.h | 45 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Monitor.c | 171 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Monitor.h | 50 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Network.c | 134 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Network.h | 45 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Packet.c | 70 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Packet.h | 41 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Radio.c | 542 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/Radio.h | 107 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/ScannedNetworks.c | 72 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/ScannedNetworks.h | 39 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/__init__.c | 74 | ||||
-rw-r--r-- | circuitpython/shared-bindings/wifi/__init__.h | 37 |
14 files changed, 1503 insertions, 0 deletions
diff --git a/circuitpython/shared-bindings/wifi/AuthMode.c b/circuitpython/shared-bindings/wifi/AuthMode.c new file mode 100644 index 0000000..528fcd4 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/AuthMode.c @@ -0,0 +1,76 @@ +/* + * This file is part of the Micro Python 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/enum.h" + +#include "shared-bindings/wifi/AuthMode.h" + +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, OPEN, AUTHMODE_OPEN); +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WEP, AUTHMODE_WEP); +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WPA, AUTHMODE_WPA); +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WPA2, AUTHMODE_WPA2); +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WPA3, AUTHMODE_WPA3); +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, PSK, AUTHMODE_PSK); +MAKE_ENUM_VALUE(wifi_authmode_type, authmode, ENTERPRISE, AUTHMODE_ENTERPRISE); + +//| class AuthMode: +//| """The authentication protocols used by WiFi.""" +//| +//| OPEN: object +//| """Open network. No authentication required.""" +//| +//| WEP: object +//| """Wired Equivalent Privacy.""" +//| +//| WPA: object +//| """Wireless Protected Access.""" +//| +//| WPA2: object +//| """Wireless Protected Access 2.""" +//| +//| WPA3: object +//| """Wireless Protected Access 3.""" +//| +//| PSK: object +//| """Pre-shared Key. (password)""" +//| +//| ENTERPRISE: object +//| """Each user has a unique credential.""" +//| +MAKE_ENUM_MAP(wifi_authmode) { + MAKE_ENUM_MAP_ENTRY(authmode, OPEN), + MAKE_ENUM_MAP_ENTRY(authmode, WEP), + MAKE_ENUM_MAP_ENTRY(authmode, WPA), + MAKE_ENUM_MAP_ENTRY(authmode, WPA2), + MAKE_ENUM_MAP_ENTRY(authmode, WPA3), + MAKE_ENUM_MAP_ENTRY(authmode, PSK), + MAKE_ENUM_MAP_ENTRY(authmode, ENTERPRISE), +}; +STATIC MP_DEFINE_CONST_DICT(wifi_authmode_locals_dict, wifi_authmode_locals_table); + +MAKE_PRINTER(wifi, wifi_authmode); + +MAKE_ENUM_TYPE(wifi, AuthMode, wifi_authmode); diff --git a/circuitpython/shared-bindings/wifi/AuthMode.h b/circuitpython/shared-bindings/wifi/AuthMode.h new file mode 100644 index 0000000..a1016d6 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/AuthMode.h @@ -0,0 +1,45 @@ +/* + * This file is part of the Micro Python 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_AUTHMODE_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H + +#include "py/enum.h" + +typedef enum { + AUTHMODE_OPEN, + AUTHMODE_WEP, + AUTHMODE_WPA, + AUTHMODE_WPA2, + AUTHMODE_WPA3, + AUTHMODE_PSK, + AUTHMODE_ENTERPRISE +} wifi_authmode_t; + +extern const mp_obj_type_t wifi_authmode_type; +extern const cp_enum_obj_t authmode_OPEN_obj; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H diff --git a/circuitpython/shared-bindings/wifi/Monitor.c b/circuitpython/shared-bindings/wifi/Monitor.c new file mode 100644 index 0000000..a63c0a8 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Monitor.c @@ -0,0 +1,171 @@ +/* + * 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, size_t n_kw, const mp_obj_t *all_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_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_channel].u_int < 1 || args[ARG_channel].u_int > 13) { + 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) { + mp_int_t c = mp_obj_get_int(channel); + if (c < 1 || c > 13) { + mp_raise_ValueError_varg(translate("%q out of bounds"), MP_QSTR_channel); + } + common_hal_wifi_monitor_set_channel(self_in, c); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(wifi_monitor_set_channel_obj, wifi_monitor_obj_set_channel); + +MP_PROPERTY_GETSET(wifi_monitor_channel_obj, + (mp_obj_t)&wifi_monitor_get_channel_obj, + (mp_obj_t)&wifi_monitor_set_channel_obj); + +//| 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); + +MP_PROPERTY_GETTER(wifi_monitor_queue_obj, + (mp_obj_t)&wifi_monitor_get_queue_obj); + +//| 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); + +//| def lost(self) -> int: +//| """Returns the packet loss count. The counter resets after each poll.""" +//| ... +//| +STATIC mp_obj_t wifi_monitor_obj_get_lost(mp_obj_t self_in) { + return common_hal_wifi_monitor_get_lost(self_in); +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_lost_obj, wifi_monitor_obj_get_lost); + +//| def queued(self) -> int: +//| """Returns the packet queued count.""" +//| ... +//| +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); +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_monitor_queued_obj, wifi_monitor_obj_get_queued); + +//| def packet(self) -> dict: +//| """Returns the monitor packet.""" +//| ... +//| +STATIC mp_obj_t wifi_monitor_obj_get_packet(mp_obj_t self_in) { + if (common_hal_wifi_monitor_deinited()) { + raise_deinited_error(); + } + 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_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) }, +}; +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, +}; diff --git a/circuitpython/shared-bindings/wifi/Monitor.h b/circuitpython/shared-bindings/wifi/Monitor.h new file mode 100644 index 0000000..38c52a0 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Monitor.h @@ -0,0 +1,50 @@ +/* + * 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_lost(wifi_monitor_obj_t *self); + +mp_obj_t common_hal_wifi_monitor_get_queued(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 diff --git a/circuitpython/shared-bindings/wifi/Network.c b/circuitpython/shared-bindings/wifi/Network.c new file mode 100644 index 0000000..9a457b9 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Network.c @@ -0,0 +1,134 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * 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/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/wifi/Network.h" + +//| class Network: +//| """A wifi network provided by a nearby access point. +//| +//| """ +//| + +//| def __init__(self) -> None: +//| """You cannot create an instance of `wifi.Network`. They are returned by `wifi.Radio.start_scanning_networks`.""" +//| ... +//| + +//| ssid: str +//| """String id of the network""" +//| +STATIC mp_obj_t wifi_network_get_ssid(mp_obj_t self) { + return common_hal_wifi_network_get_ssid(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_ssid_obj, wifi_network_get_ssid); + +MP_PROPERTY_GETTER(wifi_network_ssid_obj, + (mp_obj_t)&wifi_network_get_ssid_obj); + + +//| bssid: bytes +//| """BSSID of the network (usually the AP's MAC address)""" +//| +STATIC mp_obj_t wifi_network_get_bssid(mp_obj_t self) { + return common_hal_wifi_network_get_bssid(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_bssid_obj, wifi_network_get_bssid); + +MP_PROPERTY_GETTER(wifi_network_bssid_obj, + (mp_obj_t)&wifi_network_get_bssid_obj); + + +//| rssi: int +//| """Signal strength of the network""" +//| +STATIC mp_obj_t wifi_network_get_rssi(mp_obj_t self) { + return common_hal_wifi_network_get_rssi(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_rssi_obj, wifi_network_get_rssi); + +MP_PROPERTY_GETTER(wifi_network_rssi_obj, + (mp_obj_t)&wifi_network_get_rssi_obj); + + +//| channel: int +//| """Channel number the network is operating on""" +//| +STATIC mp_obj_t wifi_network_get_channel(mp_obj_t self) { + return common_hal_wifi_network_get_channel(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_channel_obj, wifi_network_get_channel); + +MP_PROPERTY_GETTER(wifi_network_channel_obj, + (mp_obj_t)&wifi_network_get_channel_obj); + +//| country: str +//| """String id of the country code""" +//| +STATIC mp_obj_t wifi_network_get_country(mp_obj_t self) { + return common_hal_wifi_network_get_country(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_country_obj, wifi_network_get_country); + +MP_PROPERTY_GETTER(wifi_network_country_obj, + (mp_obj_t)&wifi_network_get_country_obj); + +//| authmode: str +//| """String id of the authmode""" +//| +STATIC mp_obj_t wifi_network_get_authmode(mp_obj_t self) { + return common_hal_wifi_network_get_authmode(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_authmode_obj, wifi_network_get_authmode); + +MP_PROPERTY_GETTER(wifi_network_authmode_obj, + (mp_obj_t)&wifi_network_get_authmode_obj); + +STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_ssid), MP_ROM_PTR(&wifi_network_ssid_obj) }, + { MP_ROM_QSTR(MP_QSTR_bssid), MP_ROM_PTR(&wifi_network_bssid_obj) }, + { MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&wifi_network_rssi_obj) }, + { MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_network_channel_obj) }, + { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&wifi_network_country_obj) }, + { MP_ROM_QSTR(MP_QSTR_authmode), MP_ROM_PTR(&wifi_network_authmode_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(wifi_network_locals_dict, wifi_network_locals_dict_table); + +const mp_obj_type_t wifi_network_type = { + .base = { &mp_type_type }, + .name = MP_QSTR_Network, + .locals_dict = (mp_obj_t)&wifi_network_locals_dict, +}; diff --git a/circuitpython/shared-bindings/wifi/Network.h b/circuitpython/shared-bindings/wifi/Network.h new file mode 100644 index 0000000..0f07e7b --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Network.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * 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_NETWORK_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H + +#include <stdint.h> + +#include "common-hal/wifi/Network.h" + +#include "py/objstr.h" + +const mp_obj_type_t wifi_network_type; + +extern mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self); +extern mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self); +extern mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self); +extern mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self); +extern mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self); +extern mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H diff --git a/circuitpython/shared-bindings/wifi/Packet.c b/circuitpython/shared-bindings/wifi/Packet.c new file mode 100644 index 0000000..d21c8b0 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Packet.c @@ -0,0 +1,70 @@ +/* + * This file is part of the Micro Python 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/enum.h" + +#include "shared-bindings/wifi/Packet.h" + +MAKE_ENUM_VALUE(wifi_packet_type, packet, CH, PACKET_CH); +MAKE_ENUM_VALUE(wifi_packet_type, packet, LEN, PACKET_LEN); +MAKE_ENUM_VALUE(wifi_packet_type, packet, RAW, PACKET_RAW); +MAKE_ENUM_VALUE(wifi_packet_type, packet, RSSI, PACKET_RSSI); + +//| class Packet: +//| """The packet parameters.""" +//| +//| CH: object +//| """The packet's channel.""" +//| +//| LEN: object +//| """The packet's length.""" +//| +//| RAW: object +//| """The packet's payload.""" +//| +//| RSSI: object +//| """The packet's rssi.""" +//| +MAKE_ENUM_MAP(wifi_packet) { + MAKE_ENUM_MAP_ENTRY(packet, CH), + MAKE_ENUM_MAP_ENTRY(packet, LEN), + MAKE_ENUM_MAP_ENTRY(packet, RAW), + MAKE_ENUM_MAP_ENTRY(packet, RSSI), +}; +STATIC MP_DEFINE_CONST_DICT(wifi_packet_locals_dict, wifi_packet_locals_table); + +MAKE_PRINTER(wifi, wifi_packet); + +const mp_obj_type_t wifi_packet_type = { + { &mp_type_type }, + .name = MP_QSTR_Packet, + .print = wifi_packet_print, + .locals_dict = (mp_obj_t)&wifi_packet_locals_dict, + .flags = MP_TYPE_FLAG_EXTENDED, + MP_TYPE_EXTENDED_FIELDS( + .unary_op = mp_generic_unary_op, + ), +}; diff --git a/circuitpython/shared-bindings/wifi/Packet.h b/circuitpython/shared-bindings/wifi/Packet.h new file mode 100644 index 0000000..09dfddf --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Packet.h @@ -0,0 +1,41 @@ +/* + * This file is part of the Micro Python 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_PACKET_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_PACKET_H + +#include "py/enum.h" + +typedef enum { + PACKET_CH, + PACKET_LEN, + PACKET_RAW, + PACKET_RSSI, +} wifi_packet_t; + +extern const mp_obj_type_t wifi_packet_type; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_PACKET_H diff --git a/circuitpython/shared-bindings/wifi/Radio.c b/circuitpython/shared-bindings/wifi/Radio.c new file mode 100644 index 0000000..cf1e9df --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Radio.c @@ -0,0 +1,542 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * 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 "shared-bindings/wifi/__init__.h" +#include "shared-bindings/wifi/AuthMode.h" + +#include <regex.h> +#include <string.h> + +#include "py/runtime.h" +#include "py/objproperty.h" + +#define MAC_ADDRESS_LENGTH 6 + +//| class Radio: +//| """Native wifi radio. +//| +//| This class manages the station and access point functionality of the native +//| Wifi radio. +//| +//| """ +//| + +//| def __init__(self) -> None: +//| """You cannot create an instance of `wifi.Radio`. +//| Use `wifi.radio` to access the sole instance available.""" +//| ... +//| + +//| enabled: bool +//| """``True`` when the wifi radio is enabled. +//| If you set the value to ``False``, any open sockets will be closed. +//| """ +//| +STATIC mp_obj_t wifi_radio_get_enabled(mp_obj_t self) { + return mp_obj_new_bool(common_hal_wifi_radio_get_enabled(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_enabled_obj, wifi_radio_get_enabled); + +static mp_obj_t wifi_radio_set_enabled(mp_obj_t self, mp_obj_t value) { + const bool enabled = mp_obj_is_true(value); + + common_hal_wifi_radio_set_enabled(self, enabled); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_enabled_obj, wifi_radio_set_enabled); + +MP_PROPERTY_GETSET(wifi_radio_enabled_obj, + (mp_obj_t)&wifi_radio_get_enabled_obj, + (mp_obj_t)&wifi_radio_set_enabled_obj); + +//| hostname: Union[str | ReadableBuffer] +//| """Hostname for wifi interface. When the hostname is altered after interface started/connected +//| the changes would only be reflected once the interface restarts/reconnects.""" +//| +STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) { + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + return common_hal_wifi_radio_get_hostname(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname); + +STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) { + mp_buffer_info_t hostname; + mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ); + + if (hostname.len < 1 || hostname.len > 253) { + mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters")); + } + + #ifndef CONFIG_IDF_TARGET_ESP32C3 + regex_t regex; // validate hostname according to RFC 1123 + regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB); + if (regexec(®ex, hostname.buf, 0, NULL, 0)) { + mp_raise_ValueError(translate("invalid hostname")); + } + regfree(®ex); + #endif + + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_wifi_radio_set_hostname(self, hostname.buf); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname); + +MP_PROPERTY_GETSET(wifi_radio_hostname_obj, + (mp_obj_t)&wifi_radio_get_hostname_obj, + (mp_obj_t)&wifi_radio_set_hostname_obj); + +//| mac_address: ReadableBuffer +//| """MAC address for the station. When the address is altered after interface is connected +//| the changes would only be reflected once the interface reconnects.""" +//| +STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self_in) { + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_FROM_PTR(common_hal_wifi_radio_get_mac_address(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_mac_address_obj, wifi_radio_get_mac_address); + +STATIC mp_obj_t wifi_radio_set_mac_address(mp_obj_t self_in, mp_obj_t mac_address_in) { + mp_buffer_info_t mac_address; + mp_get_buffer_raise(mac_address_in, &mac_address, MP_BUFFER_READ); + + if (mac_address.len != MAC_ADDRESS_LENGTH) { + mp_raise_ValueError(translate("Invalid MAC address")); + } + + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_wifi_radio_set_mac_address(self, mac_address.buf); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_mac_address_obj, wifi_radio_set_mac_address); + +MP_PROPERTY_GETSET(wifi_radio_mac_address_obj, + (mp_obj_t)&wifi_radio_get_mac_address_obj, + (mp_obj_t)&wifi_radio_set_mac_address_obj); + +//| mac_address_ap: ReadableBuffer +//| """MAC address for the AP. When the address is altered after interface is started +//| the changes would only be reflected once the interface restarts.""" +//| +STATIC mp_obj_t wifi_radio_get_mac_address_ap(mp_obj_t self_in) { + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_FROM_PTR(common_hal_wifi_radio_get_mac_address_ap(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_mac_address_ap_obj, wifi_radio_get_mac_address_ap); + +STATIC mp_obj_t wifi_radio_set_mac_address_ap(mp_obj_t self_in, mp_obj_t mac_address_in) { + mp_buffer_info_t mac_address; + mp_get_buffer_raise(mac_address_in, &mac_address, MP_BUFFER_READ); + + if (mac_address.len != MAC_ADDRESS_LENGTH) { + mp_raise_ValueError(translate("Invalid MAC address")); + } + + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_wifi_radio_set_mac_address_ap(self, mac_address.buf); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_mac_address_ap_obj, wifi_radio_set_mac_address_ap); + +MP_PROPERTY_GETSET(wifi_radio_mac_address_ap_obj, + (mp_obj_t)&wifi_radio_get_mac_address_ap_obj, + (mp_obj_t)&wifi_radio_set_mac_address_ap_obj); + +//| def start_scanning_networks(self, *, start_channel: int = 1, stop_channel: int = 11) -> Iterable[Network]: +//| """Scans for available wifi networks over the given channel range. Make sure the channels are allowed in your country.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_start_scanning_networks(mp_obj_t self_in) { + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + + return common_hal_wifi_radio_start_scanning_networks(self); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_start_scanning_networks_obj, wifi_radio_start_scanning_networks); + +//| def stop_scanning_networks(self) -> None: +//| """Stop scanning for Wifi networks and free any resources used to do it.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) { + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_wifi_radio_stop_scanning_networks(self); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks); + +//| def start_station(self) -> None: +//| """Starts a Station.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_start_station(mp_obj_t self) { + common_hal_wifi_radio_start_station(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_start_station_obj, wifi_radio_start_station); + +//| def stop_station(self) -> None: +//| """Stops the Station.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_stop_station(mp_obj_t self) { + common_hal_wifi_radio_stop_station(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station); + +//| def start_ap(self, +//| ssid: Union[str | ReadableBuffer], +//| password: Union[str | ReadableBuffer] = "", +//| *, +//| channel: Optional[int] = 1, +//| authmode: Optional[AuthMode], +//| max_connections: Optional[int] = 4) -> None: +//| """Starts an Access Point with the specified ssid and password. +//| +//| If ``channel`` is given, the access point will use that channel unless +//| a station is already operating on a different channel. +//| +//| If ``authmode`` is given, the access point will use that Authentication +//| mode. If a password is given, ``authmode`` must not be ``OPEN``. +//| If ``authmode`` isn't given, ``OPEN`` will be used when password isn't provided, +//| otherwise ``WPA_WPA2_PSK``. +//| +//| If ``max_connections`` is given, the access point will allow up to +//| that number of stations to connect.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_ssid, ARG_password, ARG_channel, ARG_authmode, ARG_max_connections }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, + { MP_QSTR_authmode, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_max_connections, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 4} }, + }; + + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + uint8_t authmode = 0; + if (args[ARG_authmode].u_obj != MP_OBJ_NULL) { + mp_obj_iter_buf_t iter_buf; + mp_obj_t item, iterable = mp_getiter(args[ARG_authmode].u_obj, &iter_buf); + while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { + authmode |= (1 << (wifi_authmode_t)cp_enum_value(&wifi_authmode_type, item)); + } + } + + mp_buffer_info_t ssid; + mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ); + + mp_buffer_info_t password; + password.len = 0; + if (args[ARG_password].u_obj != MP_OBJ_NULL) { + if (authmode == 1) { + mp_raise_ValueError(translate("AuthMode.OPEN is not used with password")); + } else if (authmode == 0) { + authmode = (1 << AUTHMODE_WPA) | (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_PSK); + } + mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ); + if (password.len > 0 && (password.len < 8 || password.len > 63)) { + mp_raise_ValueError(translate("WiFi password must be between 8 and 63 characters")); + } + } else { + authmode = 1; + } + + common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, authmode, args[ARG_max_connections].u_int); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_start_ap_obj, 1, wifi_radio_start_ap); + +//| def stop_ap(self) -> None: +//| """Stops the Access Point.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_stop_ap(mp_obj_t self) { + common_hal_wifi_radio_stop_ap(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_ap_obj, wifi_radio_stop_ap); + +//| def connect(self, +//| ssid: Union[str | ReadableBuffer], +//| password: Union[str | ReadableBuffer] = "", +//| *, +//| channel: Optional[int] = 0, +//| bssid: Optional[Union[str | ReadableBuffer]] = "", +//| timeout: Optional[float] = None) -> None: +//| """Connects to the given ssid and waits for an ip address. Reconnections are handled +//| automatically once one connection succeeds. +//| +//| By default, this will scan all channels and connect to the access point (AP) with the +//| given ``ssid`` and greatest signal strength (rssi). +//| +//| If ``channel`` is given, the scan will begin with the given channel and connect to +//| the first AP with the given ``ssid``. This can speed up the connection time +//| significantly because a full scan doesn't occur. +//| +//| If ``bssid`` is given, the scan will start at the first channel or the one given and +//| connect to the AP with the given ``bssid`` and ``ssid``.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_ssid, ARG_password, ARG_channel, ARG_bssid, ARG_timeout }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + }; + + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + mp_float_t timeout = 0; + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + } + + mp_buffer_info_t ssid; + ssid.len = 0; + mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ); + if (ssid.len > 32) { + mp_raise_ValueError(translate("ssid can't be more than 32 bytes")); + } + + mp_buffer_info_t password; + password.len = 0; + if (args[ARG_password].u_obj != MP_OBJ_NULL) { + mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ); + if (password.len > 0 && (password.len < 8 || password.len > 63)) { + mp_raise_ValueError(translate("WiFi password must be between 8 and 63 characters")); + } + } + + #define MAC_ADDRESS_LENGTH 6 + + mp_buffer_info_t bssid; + bssid.len = 0; + // Should probably make sure bssid is just bytes and not something else too + if (args[ARG_bssid].u_obj != MP_OBJ_NULL) { + mp_get_buffer_raise(args[ARG_bssid].u_obj, &bssid, MP_BUFFER_READ); + if (bssid.len != MAC_ADDRESS_LENGTH) { + mp_raise_ValueError(translate("Invalid BSSID")); + } + } + + wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout, bssid.buf, bssid.len); + if (error == WIFI_RADIO_ERROR_AUTH_FAIL) { + mp_raise_ConnectionError(translate("Authentication failure")); + } else if (error == WIFI_RADIO_ERROR_NO_AP_FOUND) { + mp_raise_ConnectionError(translate("No network with that ssid")); + } else if (error != WIFI_RADIO_ERROR_NONE) { + mp_raise_msg_varg(&mp_type_ConnectionError, translate("Unknown failure %d"), error); + } + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect); + +//| ipv4_gateway: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the station gateway when connected to an access point. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_gateway(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_gateway(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_obj, wifi_radio_get_ipv4_gateway); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_gateway_obj, + (mp_obj_t)&wifi_radio_get_ipv4_gateway_obj); + +//| ipv4_gateway_ap: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the access point gateway, when enabled. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_gateway_ap(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_gateway_ap(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_ap_obj, wifi_radio_get_ipv4_gateway_ap); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_gateway_ap_obj, + (mp_obj_t)&wifi_radio_get_ipv4_gateway_ap_obj); + +//| ipv4_subnet: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the station subnet when connected to an access point. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_subnet(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_subnet(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_obj, wifi_radio_get_ipv4_subnet); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_subnet_obj, + (mp_obj_t)&wifi_radio_get_ipv4_subnet_obj); + +//| ipv4_subnet_ap: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the access point subnet, when enabled. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_subnet_ap(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_subnet_ap(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_ap_obj, wifi_radio_get_ipv4_subnet_ap); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_subnet_ap_obj, + (mp_obj_t)&wifi_radio_get_ipv4_subnet_ap_obj); + +//| ipv4_address: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the station when connected to an access point. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_address(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_address(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_address_obj, wifi_radio_get_ipv4_address); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_address_obj, + (mp_obj_t)&wifi_radio_get_ipv4_address_obj); + +//| ipv4_address_ap: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the access point, when enabled. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_address_ap(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_address_ap(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_address_ap_obj, wifi_radio_get_ipv4_address_ap); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_address_ap_obj, + (mp_obj_t)&wifi_radio_get_ipv4_address_ap_obj); + +//| ipv4_dns: Optional[ipaddress.IPv4Address] +//| """IP v4 Address of the DNS server in use when connected to an access point. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ipv4_dns(mp_obj_t self) { + return common_hal_wifi_radio_get_ipv4_dns(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_dns_obj, wifi_radio_get_ipv4_dns); + +MP_PROPERTY_GETTER(wifi_radio_ipv4_dns_obj, + (mp_obj_t)&wifi_radio_get_ipv4_dns_obj); + +//| ap_info: Optional[Network] +//| """Network object containing BSSID, SSID, authmode, channel, country and RSSI when connected to an access point. None otherwise.""" +//| +STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) { + return common_hal_wifi_radio_get_ap_info(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info); + +MP_PROPERTY_GETTER(wifi_radio_ap_info_obj, + (mp_obj_t)&wifi_radio_get_ap_info_obj); + +//| def ping(self, ip: ipaddress.IPv4Address, *, timeout: Optional[float] = 0.5) -> Optional[float]: +//| """Ping an IP to test connectivity. Returns echo time in seconds. +//| Returns None when it times out.""" +//| ... +//| +STATIC mp_obj_t wifi_radio_ping(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_ip, ARG_timeout }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_ip, MP_ARG_REQUIRED | MP_ARG_OBJ, }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + }; + + wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + mp_float_t timeout = 0.5; + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + } + + mp_int_t time_ms = common_hal_wifi_radio_ping(self, args[ARG_ip].u_obj, timeout); + if (time_ms == -1) { + return mp_const_none; + } + + return mp_obj_new_float(time_ms / 1000.0); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_ping_obj, 1, wifi_radio_ping); + +STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&wifi_radio_enabled_obj) }, + + { MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) }, + + { MP_ROM_QSTR(MP_QSTR_mac_address), MP_ROM_PTR(&wifi_radio_mac_address_obj) }, + { MP_ROM_QSTR(MP_QSTR_mac_address_ap), MP_ROM_PTR(&wifi_radio_mac_address_ap_obj) }, + + { MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) }, + { MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) }, + + { MP_ROM_QSTR(MP_QSTR_start_station), MP_ROM_PTR(&wifi_radio_start_station_obj) }, + { MP_ROM_QSTR(MP_QSTR_stop_station), MP_ROM_PTR(&wifi_radio_stop_station_obj) }, + + { MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) }, + { MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) }, + + { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) }, + // { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) }, + + { MP_ROM_QSTR(MP_QSTR_ap_info), MP_ROM_PTR(&wifi_radio_ap_info_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_gateway), MP_ROM_PTR(&wifi_radio_ipv4_gateway_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_gateway_ap), MP_ROM_PTR(&wifi_radio_ipv4_gateway_ap_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_subnet), MP_ROM_PTR(&wifi_radio_ipv4_subnet_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_subnet_ap), MP_ROM_PTR(&wifi_radio_ipv4_subnet_ap_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_address), MP_ROM_PTR(&wifi_radio_ipv4_address_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipv4_address_ap), MP_ROM_PTR(&wifi_radio_ipv4_address_ap_obj) }, + + // { MP_ROM_QSTR(MP_QSTR_access_point_active), MP_ROM_PTR(&wifi_radio_access_point_active_obj) }, + // { MP_ROM_QSTR(MP_QSTR_start_access_point), MP_ROM_PTR(&wifi_radio_start_access_point_obj) }, + + { MP_ROM_QSTR(MP_QSTR_ping), MP_ROM_PTR(&wifi_radio_ping_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(wifi_radio_locals_dict, wifi_radio_locals_dict_table); + +const mp_obj_type_t wifi_radio_type = { + .base = { &mp_type_type }, + .name = MP_QSTR_Radio, + .locals_dict = (mp_obj_t)&wifi_radio_locals_dict, +}; diff --git a/circuitpython/shared-bindings/wifi/Radio.h b/circuitpython/shared-bindings/wifi/Radio.h new file mode 100644 index 0000000..cee9f6e --- /dev/null +++ b/circuitpython/shared-bindings/wifi/Radio.h @@ -0,0 +1,107 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * 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_RADIO_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_RADIO_H + +#include <stdint.h> + +#include "common-hal/wifi/Radio.h" + +#include "py/objstr.h" + +const mp_obj_type_t wifi_radio_type; + +typedef enum { + // 0 is circuitpython-specific; 1-53 are IEEE; 200+ are Espressif + WIFI_RADIO_ERROR_NONE = 0, + WIFI_RADIO_ERROR_UNSPECIFIED = 1, + WIFI_RADIO_ERROR_AUTH_EXPIRE = 2, + WIFI_RADIO_ERROR_AUTH_LEAVE = 3, + WIFI_RADIO_ERROR_ASSOC_EXPIRE = 4, + WIFI_RADIO_ERROR_ASSOC_TOOMANY = 5, + WIFI_RADIO_ERROR_NOT_AUTHED = 6, + WIFI_RADIO_ERROR_NOT_ASSOCED = 7, + WIFI_RADIO_ERROR_ASSOC_LEAVE = 8, + WIFI_RADIO_ERROR_ASSOC_NOT_AUTHED = 9, + WIFI_RADIO_ERROR_DISASSOC_PWRCAP_BAD = 10, + WIFI_RADIO_ERROR_DISASSOC_SUPCHAN_BAD = 11, + WIFI_RADIO_ERROR_IE_INVALID = 13, + WIFI_RADIO_ERROR_MIC_FAILURE = 14, + WIFI_RADIO_ERROR_4WAY_HANDSHAKE_TIMEOUT = 15, + WIFI_RADIO_ERROR_GROUP_KEY_UPDATE_TIMEOUT = 16, + WIFI_RADIO_ERROR_IE_IN_4WAY_DIFFERS = 17, + WIFI_RADIO_ERROR_GROUP_CIPHER_INVALID = 18, + WIFI_RADIO_ERROR_PAIRWISE_CIPHER_INVALID = 19, + WIFI_RADIO_ERROR_AKMP_INVALID = 20, + WIFI_RADIO_ERROR_UNSUPP_RSN_IE_VERSION = 21, + WIFI_RADIO_ERROR_INVALID_RSN_IE_CAP = 22, + WIFI_RADIO_ERROR_802_1X_AUTH_FAILED = 23, + WIFI_RADIO_ERROR_CIPHER_SUITE_REJECTED = 24, + WIFI_RADIO_ERROR_INVALID_PMKID = 53, + WIFI_RADIO_ERROR_BEACON_TIMEOUT = 200, + WIFI_RADIO_ERROR_NO_AP_FOUND = 201, + WIFI_RADIO_ERROR_AUTH_FAIL = 202, + WIFI_RADIO_ERROR_ASSOC_FAIL = 203, + WIFI_RADIO_ERROR_HANDSHAKE_TIMEOUT = 204, + WIFI_RADIO_ERROR_CONNECTION_FAIL = 205, + WIFI_RADIO_ERROR_AP_TSF_RESET = 206, +} wifi_radio_error_t; + +extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self); +extern void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled); + +extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self); +extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname); + +extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self); +extern void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac); +extern mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self); +extern void common_hal_wifi_radio_set_mac_address_ap(wifi_radio_obj_t *self, const uint8_t *mac); + +extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self); +extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self); + +extern void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self); +extern void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self); + +extern void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint8_t authmode, uint8_t max_connections); +extern void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self); + +extern wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len); + +extern mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self); + +extern mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_RADIO_H diff --git a/circuitpython/shared-bindings/wifi/ScannedNetworks.c b/circuitpython/shared-bindings/wifi/ScannedNetworks.c new file mode 100644 index 0000000..0706d8f --- /dev/null +++ b/circuitpython/shared-bindings/wifi/ScannedNetworks.c @@ -0,0 +1,72 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries + * Copyright (c) 2018 Artur Pacholec + * Copyright (c) 2017 Glenn Ruben Bakke + * + * 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/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/wifi/ScannedNetworks.h" + +//| class ScannedNetworks: +//| """Iterates over all `wifi.Network` objects found while scanning. This object is always created +//| by a `wifi.Radio`: it has no user-visible constructor.""" +//| +STATIC mp_obj_t scannednetworks_iternext(mp_obj_t self_in) { + mp_check_self(mp_obj_is_type(self_in, &wifi_scannednetworks_type)); + wifi_scannednetworks_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_obj_t network = common_hal_wifi_scannednetworks_next(self); + if (network != mp_const_none) { + return network; + } + + return MP_OBJ_STOP_ITERATION; +} + +//| def __init__(self) -> None: +//| """Cannot be instantiated directly. Use `wifi.Radio.start_scanning_networks`.""" +//| ... +//| +//| def __iter__(self) -> Iterator[Network]: +//| """Returns itself since it is the iterator.""" +//| ... +//| +//| def __next__(self) -> Network: +//| """Returns the next `wifi.Network`. +//| Raises `StopIteration` if scanning is finished and no other results are available.""" +//| ... +//| + +const mp_obj_type_t wifi_scannednetworks_type = { + { &mp_type_type }, + .flags = MP_TYPE_FLAG_EXTENDED, + .name = MP_QSTR_ScannedNetworks, + MP_TYPE_EXTENDED_FIELDS( + .getiter = mp_identity_getiter, + .iternext = scannednetworks_iternext, + ) +}; diff --git a/circuitpython/shared-bindings/wifi/ScannedNetworks.h b/circuitpython/shared-bindings/wifi/ScannedNetworks.h new file mode 100644 index 0000000..8e0aa43 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/ScannedNetworks.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries + * Copyright (c) 2018 Artur Pacholec + * Copyright (c) 2017 Glenn Ruben Bakke + * + * 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_SCANNEDNETWORKS_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_SCANNEDNETWORKS_H + +#include "py/obj.h" +#include "common-hal/wifi/ScannedNetworks.h" + +extern const mp_obj_type_t wifi_scannednetworks_type; + +mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_SCANNEDNETWORKS_H diff --git a/circuitpython/shared-bindings/wifi/__init__.c b/circuitpython/shared-bindings/wifi/__init__.c new file mode 100644 index 0000000..0f9dee8 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/__init__.c @@ -0,0 +1,74 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * 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 "shared-bindings/wifi/__init__.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 +//| wifi connections. Use `socketpool` for communicating over the network.""" +//| +//| radio: Radio +//| """Wifi radio used to manage both station and AP modes. +//| 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(); + return mp_const_none; +} +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) }, + + // 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, +}; + +MP_REGISTER_MODULE(MP_QSTR_wifi, wifi_module, CIRCUITPY_WIFI); diff --git a/circuitpython/shared-bindings/wifi/__init__.h b/circuitpython/shared-bindings/wifi/__init__.h new file mode 100644 index 0000000..e626727 --- /dev/null +++ b/circuitpython/shared-bindings/wifi/__init__.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * 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___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI___INIT___H + +#include "shared-bindings/wifi/Radio.h" + +extern wifi_radio_obj_t common_hal_wifi_radio_obj; + +void common_hal_wifi_init(void); +void common_hal_wifi_gc_collect(void); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI___INIT___H |