summaryrefslogtreecommitdiff
path: root/shared-bindings/wifi
diff options
context:
space:
mode:
Diffstat (limited to 'shared-bindings/wifi')
-rw-r--r--shared-bindings/wifi/Network.c18
-rw-r--r--shared-bindings/wifi/Network.h1
-rw-r--r--shared-bindings/wifi/Radio.c138
-rw-r--r--shared-bindings/wifi/Radio.h10
-rw-r--r--shared-bindings/wifi/ScannedNetworks.c4
5 files changed, 157 insertions, 14 deletions
diff --git a/shared-bindings/wifi/Network.c b/shared-bindings/wifi/Network.c
index b6d5e0901..bf970a9c0 100644
--- a/shared-bindings/wifi/Network.c
+++ b/shared-bindings/wifi/Network.c
@@ -58,6 +58,23 @@ const mp_obj_property_t wifi_network_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);
+
+const mp_obj_property_t wifi_network_bssid_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_network_get_bssid_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+
//| rssi: int
//| """Signal strength of the network"""
//|
@@ -94,6 +111,7 @@ const mp_obj_property_t wifi_network_channel_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) },
};
diff --git a/shared-bindings/wifi/Network.h b/shared-bindings/wifi/Network.h
index 6a7f7be4a..c9bbc685e 100644
--- a/shared-bindings/wifi/Network.h
+++ b/shared-bindings/wifi/Network.h
@@ -36,6 +36,7 @@
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);
diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c
index 329dcb1b5..e81e8793c 100644
--- a/shared-bindings/wifi/Radio.c
+++ b/shared-bindings/wifi/Radio.c
@@ -24,11 +24,13 @@
* THE SOFTWARE.
*/
+#include "shared-bindings/wifi/__init__.h"
+
+#include <regex.h>
#include <string.h>
-#include "py/objproperty.h"
#include "py/runtime.h"
-#include "shared-bindings/wifi/__init__.h"
+#include "py/objproperty.h"
//| class Radio:
//| """Native wifi radio.
@@ -50,7 +52,6 @@
//|
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);
@@ -78,7 +79,7 @@ const mp_obj_property_t wifi_radio_mac_address_obj = {
};
-//| def start_scanning_networks(self, *, start_channel=1, stop_channel=11) -> Iterable[Network]:
+//| 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."""
//| ...
//|
@@ -102,17 +103,57 @@ STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
+//| hostname: 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"));
+ }
+
+ regex_t regex; //validate hostname according to RFC 1123
+ regcomp(&regex,"^(([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(&regex, hostname.buf, 0, NULL, 0)) {
+ mp_raise_ValueError(translate("invalid hostname"));
+ }
+ regfree(&regex);
+
+ 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);
+
+const mp_obj_property_t wifi_radio_hostname_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
+ (mp_obj_t)&wifi_radio_set_hostname_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
//| 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 };
+ 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} },
};
@@ -125,7 +166,6 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
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);
@@ -138,7 +178,19 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
}
- 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);
+ #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) {
mp_raise_ConnectionError(translate("Authentication failure"));
} else if (error == WIFI_RADIO_ERROR_NO_AP_FOUND) {
@@ -151,6 +203,38 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
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 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);
+
+const mp_obj_property_t wifi_radio_ipv4_gateway_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_gateway_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| ipv4_subnet: Optional[ipaddress.IPv4Address]
+//| """IP v4 Address of the 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);
+
+const mp_obj_property_t wifi_radio_ipv4_subnet_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_subnet_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
//| ipv4_address: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the radio when connected to an access point. None otherwise."""
//|
@@ -167,7 +251,39 @@ const mp_obj_property_t wifi_radio_ipv4_address_obj = {
(mp_obj_t)&mp_const_none_obj },
};
-//| def ping(self, ip, *, timeout: float = 0.5) -> float:
+//| 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);
+
+const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_dns_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| ap_info: Optional[Network]
+//| """Network object containing BSSID, SSID, channel, 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);
+
+const mp_obj_property_t wifi_radio_ap_info_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ap_info_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| def ping(self, ip: ipaddress.IPv4Address, *, timeout: Optional[float] = 0.5) -> float:
//| """Ping an IP to test connectivity. Returns echo time in seconds.
//| Returns None when it times out."""
//| ...
@@ -204,9 +320,15 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
{ 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_hostname), MP_ROM_PTR(&wifi_radio_hostname_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_subnet), MP_ROM_PTR(&wifi_radio_ipv4_subnet_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) },
diff --git a/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h
index 812814f9e..1e8f362e3 100644
--- a/shared-bindings/wifi/Radio.h
+++ b/shared-bindings/wifi/Radio.h
@@ -35,7 +35,6 @@
const mp_obj_type_t wifi_radio_type;
-
typedef enum {
WIFI_RADIO_ERROR_NONE,
WIFI_RADIO_ERROR_UNKNOWN,
@@ -46,13 +45,20 @@ typedef enum {
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 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 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_subnet(wifi_radio_obj_t *self);
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);
diff --git a/shared-bindings/wifi/ScannedNetworks.c b/shared-bindings/wifi/ScannedNetworks.c
index c927d7282..9316faa45 100644
--- a/shared-bindings/wifi/ScannedNetworks.c
+++ b/shared-bindings/wifi/ScannedNetworks.c
@@ -32,9 +32,6 @@
#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."""
@@ -47,7 +44,6 @@ STATIC mp_obj_t scannednetworks_iternext(mp_obj_t self_in) {
return network;
}
- ESP_EARLY_LOGI(TAG, "stop iteration");
return MP_OBJ_STOP_ITERATION;
}