summaryrefslogtreecommitdiff
path: root/shared-bindings/wifi
diff options
context:
space:
mode:
Diffstat (limited to 'shared-bindings/wifi')
-rw-r--r--shared-bindings/wifi/Radio.c48
-rw-r--r--shared-bindings/wifi/Radio.h4
-rw-r--r--shared-bindings/wifi/ScannedNetworks.c3
3 files changed, 48 insertions, 7 deletions
diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c
index 928f21da9..b548d66f6 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);
@@ -102,6 +103,45 @@ 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."""
@@ -216,6 +256,8 @@ 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) },
diff --git a/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h
index b5341d51c..a6a616154 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,6 +45,9 @@ 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);
diff --git a/shared-bindings/wifi/ScannedNetworks.c b/shared-bindings/wifi/ScannedNetworks.c
index d04f0f92a..9316faa45 100644
--- a/shared-bindings/wifi/ScannedNetworks.c
+++ b/shared-bindings/wifi/ScannedNetworks.c
@@ -32,8 +32,6 @@
#include "py/runtime.h"
#include "shared-bindings/wifi/ScannedNetworks.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."""
@@ -46,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;
}