summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2020-09-10 11:20:44 -0700
committerGitHub <noreply@github.com>2020-09-10 11:20:44 -0700
commit683462c1b15055e20da55c40243e314d660f803a (patch)
treec7b527d041f67e4be31e48d7872ff6b75a585c66 /shared-bindings
parentaf742b9ba7baad02113370cdab5eab606bdafccf (diff)
parentf3fc7c1c72b4119112c198cfc22fb1799d9f566c (diff)
Merge pull request #3326 from tannewt/native_wifi
Add native wifi API with ESP32S2 support
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/ipaddress/IPv4Address.c198
-rw-r--r--shared-bindings/ipaddress/IPv4Address.h38
-rw-r--r--shared-bindings/ipaddress/__init__.c113
-rw-r--r--shared-bindings/ipaddress/__init__.h36
-rw-r--r--shared-bindings/socketpool/Socket.c468
-rw-r--r--shared-bindings/socketpool/Socket.h43
-rw-r--r--shared-bindings/socketpool/SocketPool.c162
-rw-r--r--shared-bindings/socketpool/SocketPool.h55
-rw-r--r--shared-bindings/socketpool/__init__.c53
-rw-r--r--shared-bindings/socketpool/__init__.h30
-rw-r--r--shared-bindings/ssl/SSLContext.c95
-rw-r--r--shared-bindings/ssl/SSLContext.h41
-rw-r--r--shared-bindings/ssl/__init__.c66
-rw-r--r--shared-bindings/ssl/__init__.h34
-rw-r--r--shared-bindings/wifi/Network.c107
-rw-r--r--shared-bindings/wifi/Network.h42
-rw-r--r--shared-bindings/wifi/Radio.c224
-rw-r--r--shared-bindings/wifi/Radio.h60
-rw-r--r--shared-bindings/wifi/ScannedNetworks.c73
-rw-r--r--shared-bindings/wifi/ScannedNetworks.h39
-rw-r--r--shared-bindings/wifi/__init__.c70
-rw-r--r--shared-bindings/wifi/__init__.h38
22 files changed, 2085 insertions, 0 deletions
diff --git a/shared-bindings/ipaddress/IPv4Address.c b/shared-bindings/ipaddress/IPv4Address.c
new file mode 100644
index 000000000..b2a10158a
--- /dev/null
+++ b/shared-bindings/ipaddress/IPv4Address.c
@@ -0,0 +1,198 @@
+/*
+ * 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
+ *
+ * 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/ipaddress/IPv4Address.h"
+
+#include <string.h>
+#include <stdio.h>
+
+#include "py/objproperty.h"
+#include "py/objstr.h"
+#include "py/runtime.h"
+#include "shared-bindings/ipaddress/__init__.h"
+
+//| class IPv4Address:
+//| """Encapsulates an IPv4 address."""
+//|
+
+//| def __init__(self, address: Union[int, str, bytes]) -> None:
+//| """Create a new IPv4Address object encapsulating the address value.
+//|
+//| The value itself can either be bytes or a string formatted address."""
+//| ...
+//|
+STATIC mp_obj_t ipaddress_ipv4address_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_address };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_address, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ };
+
+ 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);
+
+ const mp_obj_t address = args[ARG_address].u_obj;
+
+ uint32_t value;
+ uint8_t* buf = NULL;
+ if (mp_obj_get_int_maybe(address, (mp_int_t*) &value)) {
+ // We're done.
+ buf = (uint8_t*) value;
+ } else if (MP_OBJ_IS_STR(address)) {
+ GET_STR_DATA_LEN(address, str_data, str_len);
+ if (!ipaddress_parse_ipv4address((const char*) str_data, str_len, &value)) {
+ mp_raise_ValueError(translate("Not a valid IP string"));
+ }
+ } else {
+ mp_buffer_info_t buf_info;
+ if (mp_get_buffer(address, &buf_info, MP_BUFFER_READ)) {
+ if (buf_info.len != 4) {
+ mp_raise_ValueError_varg(translate("Address must be %d bytes long"), 4);
+ }
+ buf = buf_info.buf;
+ }
+ }
+
+
+ ipaddress_ipv4address_obj_t *self = m_new_obj(ipaddress_ipv4address_obj_t);
+ self->base.type = &ipaddress_ipv4address_type;
+
+ common_hal_ipaddress_ipv4address_construct(self, buf, 4);
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+//| packed: bytes
+//| """The bytes that make up the address (read-only)."""
+//|
+STATIC mp_obj_t ipaddress_ipv4address_get_packed(mp_obj_t self_in) {
+ ipaddress_ipv4address_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return common_hal_ipaddress_ipv4address_get_packed(self);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(ipaddress_ipv4address_get_packed_obj, ipaddress_ipv4address_get_packed);
+
+const mp_obj_property_t ipaddress_ipv4address_packed_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&ipaddress_ipv4address_get_packed_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| version: int
+//| """4 for IPv4, 6 for IPv6"""
+//|
+STATIC mp_obj_t ipaddress_ipv4address_get_version(mp_obj_t self_in) {
+ ipaddress_ipv4address_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ mp_buffer_info_t buf_info;
+ mp_obj_t address_bytes = common_hal_ipaddress_ipv4address_get_packed(self);
+ mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
+ mp_int_t version = 6;
+ if (buf_info.len == 4) {
+ version = 4;
+ }
+
+ return MP_OBJ_NEW_SMALL_INT(version);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(ipaddress_ipv4address_get_version_obj, ipaddress_ipv4address_get_version);
+
+const mp_obj_property_t ipaddress_ipv4address_version_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&ipaddress_ipv4address_get_version_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| def __eq__(self, other: IPv4Address) -> bool:
+//| """Two Address objects are equal if their addresses and address types are equal."""
+//| ...
+//|
+STATIC mp_obj_t ipaddress_ipv4address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
+ switch (op) {
+ // Two Addresses are equal if their address bytes and address_type are equal
+ case MP_BINARY_OP_EQUAL:
+ if (MP_OBJ_IS_TYPE(rhs_in, &ipaddress_ipv4address_type)) {
+ ipaddress_ipv4address_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
+ ipaddress_ipv4address_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
+ return mp_obj_new_bool(
+ mp_obj_equal(common_hal_ipaddress_ipv4address_get_packed(lhs),
+ common_hal_ipaddress_ipv4address_get_packed(rhs)));
+
+ } else {
+ return mp_const_false;
+ }
+
+ default:
+ return MP_OBJ_NULL; // op not supported
+ }
+}
+
+//| def __hash__(self) -> int:
+//| """Returns a hash for the IPv4Address data."""
+//| ...
+//|
+STATIC mp_obj_t ipaddress_ipv4address_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
+ switch (op) {
+ // Two Addresses are equal if their address bytes and address_type are equal
+ case MP_UNARY_OP_HASH: {
+ mp_obj_t bytes = common_hal_ipaddress_ipv4address_get_packed(MP_OBJ_TO_PTR(self_in));
+ GET_STR_HASH(bytes, h);
+ if (h == 0) {
+ GET_STR_DATA_LEN(bytes, data, len);
+ h = qstr_compute_hash(data, len);
+ }
+ return MP_OBJ_NEW_SMALL_INT(h);
+ }
+ default:
+ return MP_OBJ_NULL; // op not supported
+ }
+}
+
+STATIC void ipaddress_ipv4address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ ipaddress_ipv4address_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ mp_buffer_info_t buf_info;
+ mp_obj_t address_bytes = common_hal_ipaddress_ipv4address_get_packed(self);
+ mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
+
+ const uint8_t *buf = (uint8_t *) buf_info.buf;
+ mp_printf(print, "%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]);
+}
+
+STATIC const mp_rom_map_elem_t ipaddress_ipv4address_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_packed), MP_ROM_PTR(&ipaddress_ipv4address_packed_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(ipaddress_ipv4address_locals_dict, ipaddress_ipv4address_locals_dict_table);
+
+const mp_obj_type_t ipaddress_ipv4address_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Address,
+ .make_new = ipaddress_ipv4address_make_new,
+ .print = ipaddress_ipv4address_print,
+ .unary_op = ipaddress_ipv4address_unary_op,
+ .binary_op = ipaddress_ipv4address_binary_op,
+ .locals_dict = (mp_obj_dict_t*)&ipaddress_ipv4address_locals_dict
+};
diff --git a/shared-bindings/ipaddress/IPv4Address.h b/shared-bindings/ipaddress/IPv4Address.h
new file mode 100644
index 000000000..b45cf3bac
--- /dev/null
+++ b/shared-bindings/ipaddress/IPv4Address.h
@@ -0,0 +1,38 @@
+/*
+ * 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_IPADDRESS_IPV4ADDRESS_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_IPADDRESS_IPV4ADDRESS_H
+
+#include "shared-module/ipaddress/IPv4Address.h"
+
+extern const mp_obj_type_t ipaddress_ipv4address_type;
+
+mp_obj_t common_hal_ipaddress_new_ipv4address(uint32_t value);
+void common_hal_ipaddress_ipv4address_construct(ipaddress_ipv4address_obj_t* self, uint8_t* buf, size_t len);
+mp_obj_t common_hal_ipaddress_ipv4address_get_packed(ipaddress_ipv4address_obj_t* self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_IPADDRESS_IPV4ADDRESS_H
diff --git a/shared-bindings/ipaddress/__init__.c b/shared-bindings/ipaddress/__init__.c
new file mode 100644
index 000000000..7ec2984ef
--- /dev/null
+++ b/shared-bindings/ipaddress/__init__.c
@@ -0,0 +1,113 @@
+/*
+ * 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 "py/objexcept.h"
+#include "py/objstr.h"
+#include "py/parsenum.h"
+#include "py/runtime.h"
+#include "shared-bindings/ipaddress/__init__.h"
+#include "shared-bindings/ipaddress/IPv4Address.h"
+
+//| """
+//| The `ipaddress` module provides types for IP addresses. It is a subset of CPython's ipaddress
+//| module.
+//| """
+//|
+
+
+bool ipaddress_parse_ipv4address(const char* str_data, size_t str_len, uint32_t* ip_out) {
+ size_t period_count = 0;
+ size_t period_index[4] = {0, 0, 0, str_len};
+ for (size_t i = 0; i < str_len; i++) {
+ if (str_data[i] == '.') {
+ if (period_count < 3) {
+ period_index[period_count] = i;
+ }
+ period_count++;
+ }
+ }
+ if (period_count > 3) {
+ return false;
+ }
+
+ size_t last_period = 0;
+ if (ip_out != NULL) {
+ *ip_out = 0;
+ }
+ for (size_t i = 0; i < 4; i++) {
+ // Catch exceptions thrown by mp_parse_num_integer
+ nlr_buf_t nlr;
+ mp_obj_t octet;
+ if (nlr_push(&nlr) == 0) {
+ octet = mp_parse_num_integer((const char*) str_data + last_period, period_index[i] - last_period, 10, NULL);
+ nlr_pop();
+ } else {
+ return false;
+ }
+ last_period = period_index[i] + 1;
+ if (ip_out != NULL) {
+ mp_int_t int_octet = MP_OBJ_SMALL_INT_VALUE(octet);
+ *ip_out |= int_octet << (i * 8);
+ }
+ }
+ return true;
+}
+
+//| def ip_address(obj: Union[int]) -> IPv4Address:
+//| """Return a corresponding IP address object or raise ValueError if not possible."""
+//| ...
+//|
+
+STATIC mp_obj_t ipaddress_ip_address(mp_obj_t ip_in) {
+ uint32_t value;
+ if (mp_obj_get_int_maybe(ip_in, (mp_int_t*) &value)) {
+ // We're done.
+ } else if (MP_OBJ_IS_STR(ip_in)) {
+ GET_STR_DATA_LEN(ip_in, str_data, str_len);
+ if (!ipaddress_parse_ipv4address((const char*) str_data, str_len, &value)) {
+ mp_raise_ValueError(translate("Not a valid IP string"));
+ }
+ } else {
+ mp_raise_ValueError(translate("Only raw int supported for ip"));
+ }
+
+ return common_hal_ipaddress_new_ipv4address(value);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(ipaddress_ip_address_obj, ipaddress_ip_address);
+
+STATIC const mp_rom_map_elem_t ipaddress_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ipaddress) },
+ { MP_ROM_QSTR(MP_QSTR_ip_address), MP_ROM_PTR(&ipaddress_ip_address_obj) },
+ { MP_ROM_QSTR(MP_QSTR_IPv4Address), MP_ROM_PTR(&ipaddress_ipv4address_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(ipaddress_module_globals, ipaddress_module_globals_table);
+
+
+const mp_obj_module_t ipaddress_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&ipaddress_module_globals,
+};
diff --git a/shared-bindings/ipaddress/__init__.h b/shared-bindings/ipaddress/__init__.h
new file mode 100644
index 000000000..a1c31775f
--- /dev/null
+++ b/shared-bindings/ipaddress/__init__.h
@@ -0,0 +1,36 @@
+/*
+ * 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_IPADDRESS___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_IPADDRESS___INIT___H
+
+#include "shared-module/ipaddress/__init__.h"
+
+bool ipaddress_parse_ipv4address(const char* ip_str, size_t len, uint32_t* ip_out);
+
+mp_obj_t common_hal_ipaddress_new_ipv4address(uint32_t value);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_IPADDRESS___INIT___H
diff --git a/shared-bindings/socketpool/Socket.c b/shared-bindings/socketpool/Socket.c
new file mode 100644
index 000000000..250e9c874
--- /dev/null
+++ b/shared-bindings/socketpool/Socket.c
@@ -0,0 +1,468 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * SPDX-FileCopyrightText: Copyright (c) 2014 Damien P. George
+ * 2018 Nick Moore 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/socketpool/Socket.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include "lib/utils/context_manager_helpers.h"
+#include "py/objtuple.h"
+#include "py/objlist.h"
+#include "py/runtime.h"
+#include "py/mperrno.h"
+
+#include "esp_log.h"
+static const char* TAG = "socket binding";
+
+//| class Socket:
+//| """TCP, UDP and RAW socket. Cannot be created directly. Instead, call
+//| `SocketPool.socket()`.
+//|
+//| Provides a subset of CPython's `socket.socket` API. It only implements the versions of
+//| recv that do not allocate bytes objects."""
+//|
+
+//| def __enter__(self) -> Socket:
+//| """No-op used by Context Managers."""
+//| ...
+//|
+// Provided by context manager helper.
+
+//| def __exit__(self) -> None:
+//| """Automatically closes the Socket when exiting a context. See
+//| :ref:`lifetime-and-contextmanagers` for more info."""
+//| ...
+//|
+STATIC mp_obj_t socketpool_socket___exit__(size_t n_args, const mp_obj_t *args) {
+ (void)n_args;
+ common_hal_socketpool_socket_close(args[0]);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket___exit___obj, 4, 4, socketpool_socket___exit__);
+
+// //| def bind(self, address: tuple) -> None:
+// //| """Bind a socket to an address
+// //|
+// //| :param ~tuple address: tuple of (remote_address, remote_port)"""
+// //| ...
+// //|
+
+// STATIC mp_obj_t socketpool_socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
+// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+// // // get address
+// // uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+// // mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
+
+// // // check if we need to select a NIC
+// // socket_select_nic(self, ip);
+
+// // // call the NIC to bind the socket
+// // int _errno;
+// // if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
+// // mp_raise_OSError(_errno);
+// // }
+
+// return mp_const_none;
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_bind_obj, socketpool_socket_bind);
+
+// //| def listen(self, backlog: int) -> None:
+// //| """Set socket to listen for incoming connections
+// //|
+// //| :param ~int backlog: length of backlog queue for waiting connetions"""
+// //| ...
+// //|
+
+// STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
+// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+// // if (self->nic == MP_OBJ_NULL) {
+// // // not connected
+// // // TODO I think we can listen even if not bound...
+// // mp_raise_OSError(MP_ENOTCONN);
+// // }
+
+// // int _errno;
+// // if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
+// // mp_raise_OSError(_errno);
+// // }
+
+// return mp_const_none;
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_listen_obj, socketpool_socket_listen);
+
+// //| def accept(self) -> tuple:
+// //| """Accept a connection on a listening socket of type SOCK_STREAM,
+// //| creating a new socket of type SOCK_STREAM.
+// //| Returns a tuple of (new_socket, remote_address)"""
+// //|
+
+// STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
+// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+// // // create new socket object
+// // // starts with empty NIC so that finaliser doesn't run close() method if accept() fails
+// // mod_network_socket_obj_t *socket2 = m_new_obj_with_finaliser(mod_network_socket_obj_t);
+// // socket2->base.type = &socket_type;
+// // socket2->nic = MP_OBJ_NULL;
+// // socket2->nic_type = NULL;
+
+// // // accept incoming connection
+// // uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+// // mp_uint_t port;
+// // int _errno;
+// // if (self->nic_type->accept(self, socket2, ip, &port, &_errno) != 0) {
+// // mp_raise_OSError(_errno);
+// // }
+
+// // // new socket has valid state, so set the NIC to the same as parent
+// // socket2->nic = self->nic;
+// // socket2->nic_type = self->nic_type;
+
+// // // make the return value
+// // mp_obj_tuple_t *client = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
+// // client->items[0] = MP_OBJ_FROM_PTR(socket2);
+// // client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
+
+// return mp_const_none;
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, socketpool_socket_accept);
+
+//| def close(self) -> None:
+//| """Closes this Socket and makes its resources available to its SocketPool."""
+//|
+STATIC mp_obj_t socketpool_socket_close(mp_obj_t self_in) {
+ socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_socketpool_socket_close(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_close_obj, socketpool_socket_close);
+
+//| def connect(self, address: tuple) -> None:
+//| """Connect a socket to a remote address
+//|
+//| :param ~tuple address: tuple of (remote_address, remote_port)"""
+//| ...
+//|
+
+STATIC mp_obj_t socketpool_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
+ socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ mp_obj_t *addr_items;
+ mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
+
+ size_t hostlen;
+ const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
+ mp_int_t port = mp_obj_get_int(addr_items[1]);
+
+ bool ok = common_hal_socketpool_socket_connect(self, host, hostlen, port);
+ if (!ok) {
+ ESP_EARLY_LOGW(TAG, "socket connect failed");
+ mp_raise_OSError(0);
+ }
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_connect_obj, socketpool_socket_connect);
+
+//| def send(self, bytes: ReadableBuffer) -> int:
+//| """Send some bytes to the connected remote address.
+//| Suits sockets of type SOCK_STREAM
+//|
+//| :param ~bytes bytes: some bytes to send"""
+//| ...
+//|
+
+STATIC mp_obj_t socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
+ socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ if (common_hal_socketpool_socket_get_closed(self)) {
+ // Bad file number.
+ mp_raise_OSError(MP_EBADF);
+ }
+ if (!common_hal_socketpool_socket_get_connected(self)) {
+ mp_raise_BrokenPipeError();
+ }
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
+ mp_int_t ret = common_hal_socketpool_socket_send(self, bufinfo.buf, bufinfo.len);
+ if (ret == -1) {
+ mp_raise_BrokenPipeError();
+ }
+ return mp_obj_new_int_from_uint(ret);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_send);
+
+
+// helper function for socket_recv and socket_recv_into to handle common operations of both
+// STATIC mp_int_t _socket_recv_into(mod_network_socket_obj_t *sock, byte *buf, mp_int_t len) {
+// mp_int_t ret = 0;
+// // int _errno;
+// // mp_int_t ret = sock->nic_type->recv(sock, buf, len, &_errno);
+// // if (ret == -1) {
+// // mp_raise_OSError(_errno);
+// // }
+// return ret;
+// }
+
+
+//| def recv_into(self, buffer: WriteableBuffer, bufsize: int) -> int:
+//| """Reads some bytes from the connected remote address, writing
+//| into the provided buffer. If bufsize <= len(buffer) is given,
+//| a maximum of bufsize bytes will be read into the buffer. If no
+//| valid value is given for bufsize, the default is the length of
+//| the given buffer.
+//|
+//| Suits sockets of type SOCK_STREAM
+//| Returns an int of number of bytes read.
+//|
+//| :param bytearray buffer: buffer to receive into
+//| :param int bufsize: optionally, a maximum number of bytes to read."""
+//| ...
+//|
+
+STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args) {
+ socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+ if (common_hal_socketpool_socket_get_closed(self)) {
+ // Bad file number.
+ mp_raise_OSError(MP_EBADF);
+ }
+ if (!common_hal_socketpool_socket_get_connected(self)) {
+ // not connected
+ mp_raise_OSError(MP_ENOTCONN);
+ }
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
+ mp_int_t len = bufinfo.len;
+ if (n_args == 3) {
+ mp_int_t given_len = mp_obj_get_int(args[2]);
+ if (given_len < len) {
+ len = given_len;
+ }
+ }
+
+ if (len == 0) {
+ return MP_OBJ_NEW_SMALL_INT(0);
+ }
+
+ mp_int_t ret = common_hal_socketpool_socket_recv_into(self, (byte*)bufinfo.buf, len);
+ return mp_obj_new_int_from_uint(ret);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3, socketpool_socket_recv_into);
+
+// //| def sendto(self, bytes: ReadableBuffer, address: tuple) -> int:
+// //| """Send some bytes to a specific address.
+// //| Suits sockets of type SOCK_DGRAM
+// //|
+// //| :param ~bytes bytes: some bytes to send
+// //| :param ~tuple address: tuple of (remote_address, remote_port)"""
+// //| ...
+// //|
+
+// STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) {
+// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+// // // get the data
+// // mp_buffer_info_t bufinfo;
+// // mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
+
+// // // get address
+// // uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+// // mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
+
+// // // check if we need to select a NIC
+// // socket_select_nic(self, ip);
+
+// // // call the NIC to sendto
+// // int _errno;
+// // mp_int_t ret = self->nic_type->sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
+// // if (ret == -1) {
+// // mp_raise_OSError(_errno);
+// // }
+// mp_int_t ret = 0;
+
+// return mp_obj_new_int(ret);
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_3(socketpool_socket_sendto_obj, socketpool_socket_sendto);
+
+// //| def recvfrom(self, bufsize: int) -> Tuple[bytes, tuple]:
+// //| """Reads some bytes from the connected remote address.
+// //| Suits sockets of type SOCK_STREAM
+// //|
+// //| Returns a tuple containing
+// //| * a bytes() of length <= bufsize
+// //| * a remote_address, which is a tuple of ip address and port number
+// //|
+// //| :param ~int bufsize: maximum number of bytes to receive"""
+// //| ...
+// //|
+
+// STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t len_in) {
+// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+// // if (self->nic == MP_OBJ_NULL) {
+// // // not connected
+// // mp_raise_OSError(MP_ENOTCONN);
+// // }
+// // vstr_t vstr;
+// // vstr_init_len(&vstr, mp_obj_get_int(len_in));
+// // byte ip[4];
+// // mp_uint_t port;
+// // int _errno;
+// // mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
+// // if (ret == -1) {
+// // mp_raise_OSError(_errno);
+// // }
+// mp_obj_t tuple[2];
+// // if (ret == 0) {
+// // tuple[0] = mp_const_empty_bytes;
+// // } else {
+// // vstr.len = ret;
+// // tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
+// // }
+// // tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
+// return mp_obj_new_tuple(2, tuple);
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool_socket_recvfrom_into);
+
+// //| def setsockopt(self, level: int, optname: int, value: int) -> None:
+// //| """Sets socket options"""
+// //| ...
+// //|
+
+// STATIC mp_obj_t socketpool_socket_setsockopt(size_t n_args, const mp_obj_t *args) {
+// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+
+// // mp_int_t level = mp_obj_get_int(args[1]);
+// // mp_int_t opt = mp_obj_get_int(args[2]);
+
+// // const void *optval;
+// // mp_uint_t optlen;
+// // mp_int_t val;
+// // if (mp_obj_is_integer(args[3])) {
+// // val = mp_obj_get_int_truncated(args[3]);
+// // optval = &val;
+// // optlen = sizeof(val);
+// // } else {
+// // mp_buffer_info_t bufinfo;
+// // mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
+// // optval = bufinfo.buf;
+// // optlen = bufinfo.len;
+// // }
+
+// // int _errno;
+// // if (self->nic_type->setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
+// // mp_raise_OSError(_errno);
+// // }
+
+// return mp_const_none;
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_setsockopt_obj, 4, 4, socketpool_socket_setsockopt);
+
+//| def settimeout(self, value: int) -> None:
+//| """Set the timeout value for this socket.
+//|
+//| :param ~int value: timeout in seconds. 0 means non-blocking. None means block indefinitely."""
+//| ...
+//|
+
+STATIC mp_obj_t socketpool_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
+ socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ mp_uint_t timeout_ms;
+ if (timeout_in == mp_const_none) {
+ timeout_ms = -1;
+ } else {
+ #if MICROPY_PY_BUILTINS_FLOAT
+ timeout_ms = 1000 * mp_obj_get_float(timeout_in);
+ #else
+ timeout_ms = 1000 * mp_obj_get_int(timeout_in);
+ #endif
+ }
+ common_hal_socketpool_socket_settimeout(self, timeout_ms);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_settimeout_obj, socketpool_socket_settimeout);
+
+// //| def setblocking(self, flag: bool) -> Optional[int]:
+// //| """Set the blocking behaviour of this socket.
+// //|
+// //| :param ~bool flag: False means non-blocking, True means block indefinitely."""
+// //| ...
+// //|
+
+// // method socket.setblocking(flag)
+// STATIC mp_obj_t socketpool_socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
+// // if (mp_obj_is_true(blocking)) {
+// // return socket_settimeout(self_in, mp_const_none);
+// // } else {
+// // return socket_settimeout(self_in, MP_OBJ_NEW_SMALL_INT(0));
+// // }
+// return mp_const_none;
+// }
+// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_setblocking_obj, socketpool_socket_setblocking);
+
+//| def __hash__(self) -> int:
+//| """Returns a hash for the Socket."""
+//| ...
+//|
+STATIC mp_obj_t socketpool_socket_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
+ switch (op) {
+ case MP_UNARY_OP_HASH: {
+ return MP_OBJ_NEW_SMALL_INT(common_hal_socketpool_socket_get_hash(MP_OBJ_TO_PTR(self_in)));
+ }
+ default:
+ return MP_OBJ_NULL; // op not supported
+ }
+}
+
+STATIC const mp_rom_map_elem_t socketpool_socket_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
+ { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&socketpool_socket___exit___obj) },
+ { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socketpool_socket_close_obj) },
+ { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socketpool_socket_close_obj) },
+
+ // { MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socketpool_socket_bind_obj) },
+ // { MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socketpool_socket_listen_obj) },
+ // { MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socketpool_socket_accept_obj) },
+ { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socketpool_socket_connect_obj) },
+ { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socketpool_socket_send_obj) },
+ // { MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socketpool_socket_sendto_obj) },
+ // { MP_ROM_QSTR(MP_QSTR_recvfrom_into), MP_ROM_PTR(&socketpool_socket_recvfrom_into_obj) },
+ { MP_ROM_QSTR(MP_QSTR_recv_into), MP_ROM_PTR(&socketpool_socket_recv_into_obj) },
+ // { MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socketpool_socket_setsockopt_obj) },
+ { MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socketpool_socket_settimeout_obj) },
+ // { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socketpool_socket_setblocking_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(socketpool_socket_locals_dict, socketpool_socket_locals_dict_table);
+
+const mp_obj_type_t socketpool_socket_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Socket,
+ .locals_dict = (mp_obj_dict_t*)&socketpool_socket_locals_dict,
+ .unary_op = socketpool_socket_unary_op,
+};
diff --git a/shared-bindings/socketpool/Socket.h b/shared-bindings/socketpool/Socket.h
new file mode 100644
index 000000000..f0be95c92
--- /dev/null
+++ b/shared-bindings/socketpool/Socket.h
@@ -0,0 +1,43 @@
+/*
+ * 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_SOCKETPOOL_SOCKET_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H
+
+#include "common-hal/socketpool/Socket.h"
+
+extern const mp_obj_type_t socketpool_socket_type;
+
+void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms);
+bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
+mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
+mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
+void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self);
+bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self);
+bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self);
+mp_uint_t common_hal_socketpool_socket_get_hash(socketpool_socket_obj_t* self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H
diff --git a/shared-bindings/socketpool/SocketPool.c b/shared-bindings/socketpool/SocketPool.c
new file mode 100644
index 000000000..0eeebd691
--- /dev/null
+++ b/shared-bindings/socketpool/SocketPool.c
@@ -0,0 +1,162 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * SPDX-FileCopyrightText: Copyright (c) 2014 Damien P. George
+ * 2018 Nick Moore 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 <stdio.h>
+#include <string.h>
+
+#include "py/objtuple.h"
+#include "py/objlist.h"
+#include "py/runtime.h"
+#include "py/mperrno.h"
+
+#include "shared-bindings/ipaddress/__init__.h"
+#include "shared-bindings/socketpool/Socket.h"
+#include "shared-bindings/socketpool/SocketPool.h"
+
+#include "esp_log.h"
+static const char* TAG = "socketpool binding";
+
+//| class SocketPool:
+//| """A pool of socket resources available for the given radio. Only one
+//| SocketPool can be created for each radio.
+//|
+//| SocketPool should be used in place of CPython's socket which provides
+//| a pool of sockets provided by the underlying OS."""
+//|
+
+STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+ mp_arg_check_num(n_args, kw_args, 1, 1, false);
+
+ socketpool_socketpool_obj_t *s = m_new_obj_with_finaliser(socketpool_socketpool_obj_t);
+ s->base.type = &socketpool_socketpool_type;
+ mp_obj_t radio = args[0];
+
+ common_hal_socketpool_socketpool_construct(s, radio);
+
+ return MP_OBJ_FROM_PTR(s);
+}
+
+
+//| def socket(self, family: int = AF_INET, type: int = SOCK_STREAM, proto: int = IPPROTO_TCP) -> None:
+//| """Create a new socket
+//|
+//| :param ~int family: AF_INET or AF_INET6
+//| :param ~int type: SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
+//| :param ~int proto: IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW (ignored)"""
+//| ...
+//|
+STATIC mp_obj_t socketpool_socketpool_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ mp_arg_check_num(n_args, kw_args, 0, 5, false);
+
+ socketpool_socketpool_obj_t *self = pos_args[0];
+ socketpool_socketpool_addressfamily_t family = SOCKETPOOL_AF_INET;
+ socketpool_socketpool_sock_t type = SOCKETPOOL_SOCK_STREAM;
+ if (n_args >= 2) {
+ family = mp_obj_get_int(pos_args[1]);
+ if (n_args >= 3) {
+ type = mp_obj_get_int(pos_args[2]);
+ }
+ }
+ return common_hal_socketpool_socket(self, family, type);
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_socket_obj, 1, socketpool_socketpool_socket);
+
+//| def getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> tuple:
+//| """Gets the address information for a hostname and port
+//|
+//| Returns the appropriate family, socket type, socket protocol and
+//| address information to call socket.socket() and socket.connect() with,
+//| as a tuple."""
+//| ...
+//|
+
+STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_host, ARG_port, ARG_family, ARG_type, ARG_proto, ARG_flags };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_host, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ { MP_QSTR_port, MP_ARG_INT | MP_ARG_REQUIRED },
+ { MP_QSTR_family, MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_type, MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_port, MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_flags, MP_ARG_INT, {.u_int = 0} },
+ };
+ socketpool_socketpool_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);
+
+ const char *host = mp_obj_str_get_str(args[ARG_host].u_obj);
+ mp_int_t port = args[ARG_port].u_int;
+ mp_obj_t ip_str = mp_const_none;
+
+ if (strlen(host) > 0 && ipaddress_parse_ipv4address(host, strlen(host), NULL)) {
+ ip_str = args[ARG_host].u_obj;
+ }
+
+ if (ip_str == mp_const_none) {
+ ip_str = common_hal_socketpool_socketpool_gethostbyname(self, host);
+ }
+
+ if (ip_str == mp_const_none) {
+ ESP_EARLY_LOGW(TAG, "no ip str");
+ mp_raise_OSError(0);
+ }
+
+ mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
+ tuple->items[0] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_AF_INET);
+ tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_SOCK_STREAM);
+ tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
+ tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
+ mp_obj_tuple_t *sockaddr = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
+ sockaddr->items[0] = ip_str;
+ sockaddr->items[1] = MP_OBJ_NEW_SMALL_INT(port);
+ tuple->items[4] = MP_OBJ_FROM_PTR(sockaddr);
+ return mp_obj_new_list(1, (mp_obj_t*)&tuple);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_getaddrinfo_obj, 3, socketpool_socketpool_getaddrinfo);
+
+STATIC const mp_rom_map_elem_t socketpool_socketpool_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&socketpool_socketpool_socket_obj) },
+ { MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&socketpool_socketpool_getaddrinfo_obj) },
+
+ // class constants
+ { MP_ROM_QSTR(MP_QSTR_AF_INET), MP_ROM_INT(SOCKETPOOL_AF_INET) },
+ { MP_ROM_QSTR(MP_QSTR_AF_INET6), MP_ROM_INT(SOCKETPOOL_AF_INET6) },
+
+ { MP_ROM_QSTR(MP_QSTR_SOCK_STREAM), MP_ROM_INT(SOCKETPOOL_SOCK_STREAM) },
+ { MP_ROM_QSTR(MP_QSTR_SOCK_DGRAM), MP_ROM_INT(SOCKETPOOL_SOCK_DGRAM) },
+ { MP_ROM_QSTR(MP_QSTR_SOCK_RAW), MP_ROM_INT(SOCKETPOOL_SOCK_RAW) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(socketpool_socketpool_locals_dict, socketpool_socketpool_locals_dict_table);
+
+const mp_obj_type_t socketpool_socketpool_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_SocketPool,
+ .make_new = socketpool_socketpool_make_new,
+ .locals_dict = (mp_obj_dict_t*)&socketpool_socketpool_locals_dict,
+};
diff --git a/shared-bindings/socketpool/SocketPool.h b/shared-bindings/socketpool/SocketPool.h
new file mode 100644
index 000000000..b007aad8f
--- /dev/null
+++ b/shared-bindings/socketpool/SocketPool.h
@@ -0,0 +1,55 @@
+/*
+ * 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_SOCKETPOOL_SOCKETPOOL_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKETPOOL_H
+
+#include "common-hal/socketpool/SocketPool.h"
+
+#include "shared-bindings/socketpool/Socket.h"
+
+extern const mp_obj_type_t socketpool_socketpool_type;
+
+typedef enum {
+ SOCKETPOOL_SOCK_STREAM,
+ SOCKETPOOL_SOCK_DGRAM,
+ SOCKETPOOL_SOCK_RAW
+} socketpool_socketpool_sock_t;
+
+typedef enum {
+ SOCKETPOOL_AF_INET,
+ SOCKETPOOL_AF_INET6
+} socketpool_socketpool_addressfamily_t;
+
+void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t* self, mp_obj_t radio);
+
+socketpool_socket_obj_t* common_hal_socketpool_socket(socketpool_socketpool_obj_t* self,
+ socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type);
+
+mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t* self,
+ const char* host);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKETPOOL_H
diff --git a/shared-bindings/socketpool/__init__.c b/shared-bindings/socketpool/__init__.c
new file mode 100644
index 000000000..d3f8ba302
--- /dev/null
+++ b/shared-bindings/socketpool/__init__.c
@@ -0,0 +1,53 @@
+/*
+ * 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 "py/objexcept.h"
+#include "py/objstr.h"
+#include "py/parsenum.h"
+#include "py/runtime.h"
+#include "shared-bindings/socketpool/__init__.h"
+#include "shared-bindings/socketpool/Socket.h"
+#include "shared-bindings/socketpool/SocketPool.h"
+
+//| """
+//| The `socketpool` module provides sockets through a pool. The pools themselves
+//| act like CPython's `socket` module.
+//| """
+//|
+
+STATIC const mp_rom_map_elem_t socketpool_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_socketpool) },
+
+ { MP_ROM_QSTR(MP_QSTR_SocketPool), MP_ROM_PTR(&socketpool_socketpool_type) },
+ { MP_ROM_QSTR(MP_QSTR_Socket), MP_ROM_PTR(&socketpool_socket_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(socketpool_globals, socketpool_globals_table);
+
+const mp_obj_module_t socketpool_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&socketpool_globals,
+};
diff --git a/shared-bindings/socketpool/__init__.h b/shared-bindings/socketpool/__init__.h
new file mode 100644
index 000000000..a017e96c6
--- /dev/null
+++ b/shared-bindings/socketpool/__init__.h
@@ -0,0 +1,30 @@
+/*
+ * 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_SOCKETPOOL___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL___INIT___H
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL___INIT___H
diff --git a/shared-bindings/ssl/SSLContext.c b/shared-bindings/ssl/SSLContext.c
new file mode 100644
index 000000000..d2c236d3b
--- /dev/null
+++ b/shared-bindings/ssl/SSLContext.c
@@ -0,0 +1,95 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * SPDX-FileCopyrightText: 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 <stdio.h>
+#include <string.h>
+
+#include "py/objtuple.h"
+#include "py/objlist.h"
+#include "py/runtime.h"
+#include "py/mperrno.h"
+
+#include "shared-bindings/ssl/SSLContext.h"
+
+//| class SSLContext:
+//| """Settings related to SSL that can be applied to a socket by wrapping it.
+//| This is useful to provide SSL certificates to specific connections
+//| rather than all of them."""
+//|
+
+STATIC mp_obj_t ssl_sslcontext_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+ mp_arg_check_num(n_args, kw_args, 0, 1, false);
+
+ ssl_sslcontext_obj_t *s = m_new_obj(ssl_sslcontext_obj_t);
+ s->base.type = &ssl_sslcontext_type;
+
+ common_hal_ssl_sslcontext_construct(s);
+
+ return MP_OBJ_FROM_PTR(s);
+}
+
+//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: str = None) -> socketpool.Socket:
+//| """Wraps the socket into a socket-compatible class that handles SSL negotiation.
+//| The socket must be of type SOCK_STREAM."""
+//| ...
+//|
+
+STATIC mp_obj_t ssl_sslcontext_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_sock, ARG_server_side, ARG_server_hostname };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_sock, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ { MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
+ };
+ ssl_sslcontext_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);
+
+ const char *server_hostname = mp_obj_str_get_str(args[ARG_server_hostname].u_obj);
+ bool server_side = args[ARG_server_side].u_bool;
+ if (server_side && server_hostname != NULL) {
+ mp_raise_ValueError(translate("Server side context cannot have hostname"));
+ }
+
+ socketpool_socket_obj_t* sock = args[ARG_sock].u_obj;
+
+ return common_hal_ssl_sslcontext_wrap_socket(self, sock, server_side, server_hostname);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ssl_sslcontext_wrap_socket_obj, 2, ssl_sslcontext_wrap_socket);
+
+STATIC const mp_rom_map_elem_t ssl_sslcontext_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&ssl_sslcontext_wrap_socket_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(ssl_sslcontext_locals_dict, ssl_sslcontext_locals_dict_table);
+
+const mp_obj_type_t ssl_sslcontext_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_SSLContext,
+ .make_new = ssl_sslcontext_make_new,
+ .locals_dict = (mp_obj_dict_t*)&ssl_sslcontext_locals_dict,
+};
diff --git a/shared-bindings/ssl/SSLContext.h b/shared-bindings/ssl/SSLContext.h
new file mode 100644
index 000000000..f7f985af7
--- /dev/null
+++ b/shared-bindings/ssl/SSLContext.h
@@ -0,0 +1,41 @@
+/*
+ * 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_SSL_SSLCONTEXT_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLCONTEXT_H
+
+#include "common-hal/ssl/SSLContext.h"
+
+#include "shared-bindings/socketpool/Socket.h"
+
+extern const mp_obj_type_t ssl_sslcontext_type;
+
+void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t* self);
+
+socketpool_socket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
+ socketpool_socket_obj_t* sock, bool server_side, const char* server_hostname);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLCONTEXT_H
diff --git a/shared-bindings/ssl/__init__.c b/shared-bindings/ssl/__init__.c
new file mode 100644
index 000000000..57f4e6f4a
--- /dev/null
+++ b/shared-bindings/ssl/__init__.c
@@ -0,0 +1,66 @@
+/*
+ * 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 "py/objexcept.h"
+#include "py/objstr.h"
+#include "py/parsenum.h"
+#include "py/runtime.h"
+#include "shared-bindings/ssl/__init__.h"
+#include "shared-bindings/ssl/SSLContext.h"
+
+//| """
+//| The `ssl` module provides SSL contexts to wrap sockets in.
+//| """
+//|
+
+//| def create_default_context() -> ssl.SSLContext:
+//| """Return the default SSLContext."""
+//| ...
+//|
+
+STATIC mp_obj_t ssl_create_default_context(void) {
+ ssl_sslcontext_obj_t *s = m_new_obj(ssl_sslcontext_obj_t);
+ s->base.type = &ssl_sslcontext_type;
+
+ common_hal_ssl_create_default_context(s);
+ return s;
+}
+MP_DEFINE_CONST_FUN_OBJ_0(ssl_create_default_context_obj, ssl_create_default_context);
+
+STATIC const mp_rom_map_elem_t ssl_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ssl) },
+
+ { MP_ROM_QSTR(MP_QSTR_create_default_context), MP_ROM_PTR(&ssl_create_default_context_obj) },
+
+ { MP_ROM_QSTR(MP_QSTR_SSLContext), MP_ROM_PTR(&ssl_sslcontext_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(ssl_globals, ssl_globals_table);
+
+const mp_obj_module_t ssl_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&ssl_globals,
+};
diff --git a/shared-bindings/ssl/__init__.h b/shared-bindings/ssl/__init__.h
new file mode 100644
index 000000000..5ddada64e
--- /dev/null
+++ b/shared-bindings/ssl/__init__.h
@@ -0,0 +1,34 @@
+/*
+ * 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_SSL___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_SSL___INIT___H
+
+#include "common-hal/ssl/SSLContext.h"
+
+void common_hal_ssl_create_default_context(ssl_sslcontext_obj_t* self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL___INIT___H
diff --git a/shared-bindings/wifi/Network.c b/shared-bindings/wifi/Network.c
new file mode 100644
index 000000000..b6d5e0901
--- /dev/null
+++ b/shared-bindings/wifi/Network.c
@@ -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.
+ */
+
+#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);
+
+const mp_obj_property_t wifi_network_ssid_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_network_get_ssid_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_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);
+
+const mp_obj_property_t wifi_network_rssi_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_network_get_rssi_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_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);
+
+const mp_obj_property_t wifi_network_channel_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_network_get_channel_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_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_rssi), MP_ROM_PTR(&wifi_network_rssi_obj) },
+ { MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_network_channel_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/shared-bindings/wifi/Network.h b/shared-bindings/wifi/Network.h
new file mode 100644
index 000000000..6a7f7be4a
--- /dev/null
+++ b/shared-bindings/wifi/Network.h
@@ -0,0 +1,42 @@
+/*
+ * 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_rssi(wifi_network_obj_t *self);
+extern mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H
diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c
new file mode 100644
index 000000000..329dcb1b5
--- /dev/null
+++ b/shared-bindings/wifi/Radio.c
@@ -0,0 +1,224 @@
+/*
+ * 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/__init__.h"
+
+//| 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."""
+//|
+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);
+
+const mp_obj_property_t wifi_radio_enabled_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_enabled_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| mac_address: bytes
+//| """MAC address of the wifi radio. (read-only)"""
+//|
+STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self) {
+ 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);
+
+const mp_obj_property_t wifi_radio_mac_address_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_mac_address_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+
+//| def start_scanning_networks(self, *, start_channel=1, stop_channel=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 connect(self, ssid: ReadableBuffer, password: ReadableBuffer = b"", *, channel: Optional[int] = 0, timeout: Optional[float] = None) -> bool:
+//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
+//| automatically once one connection succeeds."""
+//| ...
+//|
+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_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_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;
+ 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) {
+ 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"));
+ }
+ }
+
+ 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);
+ if (error == WIFI_RADIO_ERROR_AUTH) {
+ 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_ConnectionError(translate("Unknown failure"));
+ }
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect);
+
+//| ipv4_address: Optional[ipaddress.IPv4Address]
+//| """IP v4 Address of the radio 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);
+
+const mp_obj_property_t wifi_radio_ipv4_address_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_address_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| def ping(self, ip, *, timeout: float = 0.5) -> 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_mac_address), MP_ROM_PTR(&wifi_radio_mac_address_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_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_ipv4_address), MP_ROM_PTR(&wifi_radio_ipv4_address_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/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h
new file mode 100644
index 000000000..812814f9e
--- /dev/null
+++ b/shared-bindings/wifi/Radio.h
@@ -0,0 +1,60 @@
+/*
+ * 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 {
+ WIFI_RADIO_ERROR_NONE,
+ WIFI_RADIO_ERROR_UNKNOWN,
+ WIFI_RADIO_ERROR_AUTH,
+ WIFI_RADIO_ERROR_NO_AP_FOUND
+} 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_mac_address(wifi_radio_obj_t *self);
+
+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 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);
+
+extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(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/shared-bindings/wifi/ScannedNetworks.c b/shared-bindings/wifi/ScannedNetworks.c
new file mode 100644
index 000000000..c927d7282
--- /dev/null
+++ b/shared-bindings/wifi/ScannedNetworks.c
@@ -0,0 +1,73 @@
+/*
+ * 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"
+
+#include "esp_log.h"
+static const char *TAG = "cp iternext";
+
+//| 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;
+ }
+
+ ESP_EARLY_LOGI(TAG, "stop iteration");
+ 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 },
+ .name = MP_QSTR_ScannedNetworks,
+ .getiter = mp_identity_getiter,
+ .iternext = scannednetworks_iternext,
+};
diff --git a/shared-bindings/wifi/ScannedNetworks.h b/shared-bindings/wifi/ScannedNetworks.h
new file mode 100644
index 000000000..8e0aa435d
--- /dev/null
+++ b/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/shared-bindings/wifi/__init__.c b/shared-bindings/wifi/__init__.c
new file mode 100644
index 000000000..352ceb331
--- /dev/null
+++ b/shared-bindings/wifi/__init__.c
@@ -0,0 +1,70 @@
+/*
+ * 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 "py/objexcept.h"
+#include "py/runtime.h"
+#include "shared-bindings/wifi/__init__.h"
+#include "shared-bindings/wifi/Network.h"
+#include "shared-bindings/wifi/Radio.h"
+
+//| """
+//| The `wifi` module provides necessary low-level functionality for managing wifi
+//| 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[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_wifi) },
+ { MP_ROM_QSTR(MP_QSTR_Network), MP_ROM_PTR(&wifi_network_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) },
+
+ // Initialization
+ { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&wifi___init___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,
+};
diff --git a/shared-bindings/wifi/__init__.h b/shared-bindings/wifi/__init__.h
new file mode 100644
index 000000000..c06ee16be
--- /dev/null
+++ b/shared-bindings/wifi/__init__.h
@@ -0,0 +1,38 @@
+/*
+ * 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 "py/objlist.h"
+
+#include "shared-bindings/wifi/Radio.h"
+
+extern wifi_radio_obj_t common_hal_wifi_radio_obj;
+
+void common_hal_wifi_init(void);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI___INIT___H