rearrange hostname

This commit is contained in:
microDev 2021-11-02 11:11:11 +05:30
parent 95172cf3ce
commit 4e207853f0
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
4 changed files with 60 additions and 58 deletions

View File

@ -101,6 +101,19 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
}
}
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
const char *hostname = NULL;
esp_netif_get_hostname(self->netif, &hostname);
if (hostname == NULL) {
return mp_const_none;
}
return mp_obj_new_str(hostname, strlen(hostname));
}
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
esp_netif_set_hostname(self->netif, hostname);
}
mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
uint8_t mac[MAC_ADDRESS_LENGTH];
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) {
self->current_scan = NULL;
}
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
const char *hostname = NULL;
esp_netif_get_hostname(self->netif, &hostname);
if (hostname == NULL) {
return mp_const_none;
}
return mp_obj_new_str(hostname, strlen(hostname));
}
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
esp_netif_set_hostname(self->netif, hostname);
}
void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
set_mode_station(self, true);
}

View File

@ -57,7 +57,6 @@ typedef struct {
uint8_t retries_left;
uint8_t starting_retries;
uint8_t last_disconnect_reason;
wifi_config_t ap_config;
esp_netif_ip_info_t ap_ip_info;
esp_netif_t *ap_netif;

View File

@ -74,6 +74,47 @@ const mp_obj_property_t wifi_radio_enabled_obj = {
MP_ROM_NONE },
};
//| 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"));
}
#ifndef CONFIG_IDF_TARGET_ESP32C3
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);
#endif
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_hostname(self, hostname.buf);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
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_ROM_NONE},
};
//| mac_address: bytes
//| """MAC address of the wifi radio station. (read-only)"""
//|
@ -132,47 +173,6 @@ 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"));
}
#ifndef CONFIG_IDF_TARGET_ESP32C3
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);
#endif
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_hostname(self, hostname.buf);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
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_ROM_NONE},
};
//| def start_station(self) -> None:
//| """Starts a Station."""
//| ...
@ -503,18 +503,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_ping_obj, 1, wifi_radio_ping);
STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&wifi_radio_enabled_obj) },
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
{ MP_ROM_QSTR(MP_QSTR_mac_address), MP_ROM_PTR(&wifi_radio_mac_address_obj) },
{ MP_ROM_QSTR(MP_QSTR_mac_address_ap), MP_ROM_PTR(&wifi_radio_mac_address_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
{ MP_ROM_QSTR(MP_QSTR_start_station), MP_ROM_PTR(&wifi_radio_start_station_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_station), MP_ROM_PTR(&wifi_radio_stop_station_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },

View File

@ -85,6 +85,7 @@ extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self)
extern void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint8_t authmode);
extern void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self);