extmod/modnetwork: Forward if.config(hostname) to network.hostname.
This removes the duplicate code in cyw43, esp32, esp8266 that implements the same logic as network.hostname. Renames the `mod_network_hostname` (where we store the hostname value in `.data`) to `mod_network_hostname_data` to make way for calling the shared function `mod_network_hostname`. And uses memcpy for mod_network_hostname_data, because the length of source is already known and removes reliance on string data being null-terminated. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
parent
b329fdcb73
commit
65a3ce39a3
|
@ -56,7 +56,7 @@ char mod_network_country_code[2] = "XX";
|
|||
#error "MICROPY_PY_NETWORK_HOSTNAME_DEFAULT must be set in mpconfigport.h or mpconfigboard.h"
|
||||
#endif
|
||||
|
||||
char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
|
||||
char mod_network_hostname_data[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
|
||||
|
||||
#ifdef MICROPY_PORT_NETWORK_INTERFACES
|
||||
|
||||
|
@ -116,20 +116,21 @@ STATIC mp_obj_t network_country(size_t n_args, const mp_obj_t *args) {
|
|||
// TODO: Non-static to allow backwards-compatible pyb.country.
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_country_obj, 0, 1, network_country);
|
||||
|
||||
STATIC mp_obj_t network_hostname(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_t mod_network_hostname(size_t n_args, const mp_obj_t *args) {
|
||||
if (n_args == 0) {
|
||||
return mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
|
||||
return mp_obj_new_str(mod_network_hostname_data, strlen(mod_network_hostname_data));
|
||||
} else {
|
||||
size_t len;
|
||||
const char *str = mp_obj_str_get_data(args[0], &len);
|
||||
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
|
||||
mp_raise_ValueError(NULL);
|
||||
}
|
||||
strcpy(mod_network_hostname, str);
|
||||
memcpy(mod_network_hostname_data, str, len);
|
||||
mod_network_hostname_data[len] = 0;
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, network_hostname);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, mod_network_hostname);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
|
||||
|
|
|
@ -61,7 +61,12 @@ extern char mod_network_country_code[2];
|
|||
#endif
|
||||
|
||||
// This is a null-terminated string.
|
||||
extern char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1];
|
||||
extern char mod_network_hostname_data[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1];
|
||||
|
||||
// To support backwards-compatible (esp32, esp8266, cyw43)
|
||||
// `if.config(hostname=...)` to forward directly to the implementation of
|
||||
// `network.hostname(...)`.
|
||||
mp_obj_t mod_network_hostname(size_t n_args, const mp_obj_t *args);
|
||||
|
||||
#if MICROPY_PY_LWIP
|
||||
struct netif;
|
||||
|
|
|
@ -422,7 +422,7 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map
|
|||
}
|
||||
case MP_QSTR_hostname: {
|
||||
// TODO: Deprecated. Use network.hostname() instead.
|
||||
return mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
|
||||
return mod_network_hostname(0, NULL);
|
||||
}
|
||||
default:
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
|
||||
|
@ -498,12 +498,7 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map
|
|||
}
|
||||
case MP_QSTR_hostname: {
|
||||
// TODO: Deprecated. Use network.hostname(name) instead.
|
||||
size_t len;
|
||||
const char *str = mp_obj_str_get_data(e->value, &len);
|
||||
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
|
||||
mp_raise_ValueError(NULL);
|
||||
}
|
||||
strcpy(mod_network_hostname, str);
|
||||
mod_network_hostname(1, &e->value);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
|
|
@ -169,8 +169,8 @@ static void network_wlan_ip_event_handler(void *event_handler_arg, esp_event_bas
|
|||
if (!mdns_initialised) {
|
||||
mdns_init();
|
||||
#if MICROPY_HW_ENABLE_MDNS_RESPONDER
|
||||
mdns_hostname_set(mod_network_hostname);
|
||||
mdns_instance_name_set(mod_network_hostname);
|
||||
mdns_hostname_set(mod_network_hostname_data);
|
||||
mdns_instance_name_set(mod_network_hostname_data);
|
||||
#endif
|
||||
mdns_initialised = true;
|
||||
}
|
||||
|
@ -305,7 +305,7 @@ STATIC mp_obj_t network_wlan_connect(size_t n_args, const mp_obj_t *pos_args, mp
|
|||
esp_exceptions(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
|
||||
}
|
||||
|
||||
esp_exceptions(esp_netif_set_hostname(wlan_sta_obj.netif, mod_network_hostname));
|
||||
esp_exceptions(esp_netif_set_hostname(wlan_sta_obj.netif, mod_network_hostname_data));
|
||||
|
||||
wifi_sta_reconnects = 0;
|
||||
// connect to the WiFi AP
|
||||
|
@ -522,12 +522,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
|
|||
case MP_QSTR_hostname:
|
||||
case MP_QSTR_dhcp_hostname: {
|
||||
// TODO: Deprecated. Use network.hostname(name) instead.
|
||||
size_t len;
|
||||
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
|
||||
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
|
||||
mp_raise_ValueError(NULL);
|
||||
}
|
||||
strcpy(mod_network_hostname, str);
|
||||
mod_network_hostname(1, &kwargs->table[i].value);
|
||||
break;
|
||||
}
|
||||
case MP_QSTR_max_clients: {
|
||||
|
@ -630,7 +625,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
|
|||
case MP_QSTR_dhcp_hostname: {
|
||||
// TODO: Deprecated. Use network.hostname() instead.
|
||||
req_if = ESP_IF_WIFI_STA;
|
||||
val = mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
|
||||
val = mod_network_hostname(0, NULL);
|
||||
break;
|
||||
}
|
||||
case MP_QSTR_max_clients: {
|
||||
|
|
|
@ -152,7 +152,7 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||
error_check(wifi_station_set_config(&config), "Cannot set STA config");
|
||||
}
|
||||
|
||||
wifi_station_set_hostname(mod_network_hostname);
|
||||
wifi_station_set_hostname(mod_network_hostname_data);
|
||||
|
||||
error_check(wifi_station_connect(), "Cannot connect to AP");
|
||||
|
||||
|
@ -402,12 +402,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
|
|||
case MP_QSTR_hostname:
|
||||
case MP_QSTR_dhcp_hostname: {
|
||||
// TODO: Deprecated. Use network.hostname(name) instead.
|
||||
size_t len;
|
||||
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
|
||||
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
|
||||
mp_raise_ValueError(NULL);
|
||||
}
|
||||
strcpy(mod_network_hostname, str);
|
||||
mod_network_hostname(1, &kwargs->table[i].value);
|
||||
break;
|
||||
}
|
||||
case MP_QSTR_protocol: {
|
||||
|
@ -481,9 +476,9 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
|
|||
break;
|
||||
case MP_QSTR_hostname:
|
||||
case MP_QSTR_dhcp_hostname: {
|
||||
req_if = STATION_IF;
|
||||
// TODO: Deprecated. Use network.hostname() instead.
|
||||
val = mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
|
||||
req_if = STATION_IF;
|
||||
val = mod_network_hostname(0, NULL);
|
||||
break;
|
||||
}
|
||||
case MP_QSTR_protocol: {
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
|
||||
#define CYW43_THREAD_LOCK_CHECK
|
||||
|
||||
#define CYW43_HOST_NAME mod_network_hostname
|
||||
#define CYW43_HOST_NAME mod_network_hostname_data
|
||||
|
||||
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFI();
|
||||
#define CYW43_DO_IOCTL_WAIT __WFI();
|
||||
|
|
|
@ -559,7 +559,7 @@ STATIC void eth_lwip_init(eth_t *self) {
|
|||
n->name[0] = 'e';
|
||||
n->name[1] = (self == ð_instance0 ? '0' : '1');
|
||||
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
|
||||
netif_set_hostname(n, mod_network_hostname);
|
||||
netif_set_hostname(n, mod_network_hostname_data);
|
||||
netif_set_default(n);
|
||||
netif_set_up(n);
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
|
||||
#define CYW43_THREAD_LOCK_CHECK
|
||||
|
||||
#define CYW43_HOST_NAME mod_network_hostname
|
||||
#define CYW43_HOST_NAME mod_network_hostname_data
|
||||
|
||||
#define CYW43_SDPCM_SEND_COMMON_WAIT \
|
||||
if (get_core_num() == 0) { \
|
||||
|
|
|
@ -62,7 +62,7 @@
|
|||
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
|
||||
#define CYW43_THREAD_LOCK_CHECK
|
||||
|
||||
#define CYW43_HOST_NAME mod_network_hostname
|
||||
#define CYW43_HOST_NAME mod_network_hostname_data
|
||||
|
||||
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFI();
|
||||
#define CYW43_DO_IOCTL_WAIT __WFI();
|
||||
|
|
|
@ -737,7 +737,7 @@ STATIC void eth_lwip_init(eth_t *self) {
|
|||
n->name[0] = 'e';
|
||||
n->name[1] = '0';
|
||||
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
|
||||
netif_set_hostname(n, mod_network_hostname);
|
||||
netif_set_hostname(n, mod_network_hostname_data);
|
||||
netif_set_default(n);
|
||||
netif_set_up(n);
|
||||
|
||||
|
|
Loading…
Reference in New Issue