Skip to content

Commit 4e20785

Browse files
committed
rearrange hostname
1 parent 95172cf commit 4e20785

File tree

4 files changed

+60
-58
lines changed

4 files changed

+60
-58
lines changed

ports/espressif/common-hal/wifi/Radio.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
101101
}
102102
}
103103

104+
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
105+
const char *hostname = NULL;
106+
esp_netif_get_hostname(self->netif, &hostname);
107+
if (hostname == NULL) {
108+
return mp_const_none;
109+
}
110+
return mp_obj_new_str(hostname, strlen(hostname));
111+
}
112+
113+
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
114+
esp_netif_set_hostname(self->netif, hostname);
115+
}
116+
104117
mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
105118
uint8_t mac[MAC_ADDRESS_LENGTH];
106119
esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
@@ -142,19 +155,6 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
142155
self->current_scan = NULL;
143156
}
144157

145-
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
146-
const char *hostname = NULL;
147-
esp_netif_get_hostname(self->netif, &hostname);
148-
if (hostname == NULL) {
149-
return mp_const_none;
150-
}
151-
return mp_obj_new_str(hostname, strlen(hostname));
152-
}
153-
154-
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
155-
esp_netif_set_hostname(self->netif, hostname);
156-
}
157-
158158
void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
159159
set_mode_station(self, true);
160160
}

ports/espressif/common-hal/wifi/Radio.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ typedef struct {
5757
uint8_t retries_left;
5858
uint8_t starting_retries;
5959
uint8_t last_disconnect_reason;
60-
6160
wifi_config_t ap_config;
6261
esp_netif_ip_info_t ap_ip_info;
6362
esp_netif_t *ap_netif;

shared-bindings/wifi/Radio.c

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,47 @@ const mp_obj_property_t wifi_radio_enabled_obj = {
7474
MP_ROM_NONE },
7575
};
7676

77+
//| hostname: ReadableBuffer
78+
//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
79+
//| the changes would only be reflected once the interface restarts/reconnects."""
80+
//|
81+
STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
82+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
83+
return common_hal_wifi_radio_get_hostname(self);
84+
}
85+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
86+
87+
STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
88+
mp_buffer_info_t hostname;
89+
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
90+
91+
if (hostname.len < 1 || hostname.len > 253) {
92+
mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
93+
}
94+
95+
#ifndef CONFIG_IDF_TARGET_ESP32C3
96+
regex_t regex; // validate hostname according to RFC 1123
97+
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);
98+
if (regexec(&regex, hostname.buf, 0, NULL, 0)) {
99+
mp_raise_ValueError(translate("invalid hostname"));
100+
}
101+
regfree(&regex);
102+
#endif
103+
104+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
105+
common_hal_wifi_radio_set_hostname(self, hostname.buf);
106+
107+
return mp_const_none;
108+
}
109+
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
110+
111+
const mp_obj_property_t wifi_radio_hostname_obj = {
112+
.base.type = &mp_type_property,
113+
.proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
114+
(mp_obj_t)&wifi_radio_set_hostname_obj,
115+
MP_ROM_NONE},
116+
};
117+
77118
//| mac_address: bytes
78119
//| """MAC address of the wifi radio station. (read-only)"""
79120
//|
@@ -132,47 +173,6 @@ STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
132173
}
133174
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
134175

135-
//| hostname: ReadableBuffer
136-
//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
137-
//| the changes would only be reflected once the interface restarts/reconnects."""
138-
//|
139-
STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
140-
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
141-
return common_hal_wifi_radio_get_hostname(self);
142-
}
143-
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
144-
145-
STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
146-
mp_buffer_info_t hostname;
147-
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
148-
149-
if (hostname.len < 1 || hostname.len > 253) {
150-
mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
151-
}
152-
153-
#ifndef CONFIG_IDF_TARGET_ESP32C3
154-
regex_t regex; // validate hostname according to RFC 1123
155-
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);
156-
if (regexec(&regex, hostname.buf, 0, NULL, 0)) {
157-
mp_raise_ValueError(translate("invalid hostname"));
158-
}
159-
regfree(&regex);
160-
#endif
161-
162-
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
163-
common_hal_wifi_radio_set_hostname(self, hostname.buf);
164-
165-
return mp_const_none;
166-
}
167-
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
168-
169-
const mp_obj_property_t wifi_radio_hostname_obj = {
170-
.base.type = &mp_type_property,
171-
.proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
172-
(mp_obj_t)&wifi_radio_set_hostname_obj,
173-
MP_ROM_NONE},
174-
};
175-
176176
//| def start_station(self) -> None:
177177
//| """Starts a Station."""
178178
//| ...
@@ -503,18 +503,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_ping_obj, 1, wifi_radio_ping);
503503

504504
STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
505505
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&wifi_radio_enabled_obj) },
506+
507+
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
508+
506509
{ MP_ROM_QSTR(MP_QSTR_mac_address), MP_ROM_PTR(&wifi_radio_mac_address_obj) },
507510
{ MP_ROM_QSTR(MP_QSTR_mac_address_ap), MP_ROM_PTR(&wifi_radio_mac_address_ap_obj) },
508511

509512
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
510513
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
511514

512-
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
513-
514515
{ MP_ROM_QSTR(MP_QSTR_start_station), MP_ROM_PTR(&wifi_radio_start_station_obj) },
515516
{ MP_ROM_QSTR(MP_QSTR_stop_station), MP_ROM_PTR(&wifi_radio_stop_station_obj) },
516-
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
517+
517518
{ MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) },
519+
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
518520

519521
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
520522
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },

shared-bindings/wifi/Radio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self)
8585

8686
extern void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self);
8787
extern void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self);
88+
8889
extern void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint8_t authmode);
8990
extern void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self);
9091

0 commit comments

Comments
 (0)