summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2023-10-03 13:32:48 +1100
committerDamien George <damien@micropython.org>2023-10-04 12:39:51 +1100
commit65a3ce39a31e5ed7e5634c6905ca981039f42cb3 (patch)
tree73dea6741bfc01a7d99936ac9b5b2f8748b7a932
parentb329fdcb7394f7a30cb81c48e04ba3d557d29396 (diff)
downloadmicropython-65a3ce39a31e5ed7e5634c6905ca981039f42cb3.tar.gz
micropython-65a3ce39a31e5ed7e5634c6905ca981039f42cb3.zip
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>
-rw-r--r--extmod/modnetwork.c11
-rw-r--r--extmod/modnetwork.h7
-rw-r--r--extmod/network_cyw43.c9
-rw-r--r--ports/esp32/network_wlan.c15
-rw-r--r--ports/esp8266/network_wlan.c13
-rw-r--r--ports/mimxrt/cyw43_configport.h2
-rw-r--r--ports/mimxrt/eth.c2
-rw-r--r--ports/rp2/cyw43_configport.h2
-rw-r--r--ports/stm32/cyw43_configport.h2
-rw-r--r--ports/stm32/eth.c2
10 files changed, 28 insertions, 37 deletions
diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c
index 378b45b9c8..f5734af9b5 100644
--- a/extmod/modnetwork.c
+++ b/extmod/modnetwork.c
@@ -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) },
diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h
index e9769e309b..e3239c2a05 100644
--- a/extmod/modnetwork.h
+++ b/extmod/modnetwork.h
@@ -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;
diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c
index f26a835c16..834f09eae9 100644
--- a/extmod/network_cyw43.c
+++ b/extmod/network_cyw43.c
@@ -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:
diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c
index f06143bf76..e1d16b4191 100644
--- a/ports/esp32/network_wlan.c
+++ b/ports/esp32/network_wlan.c
@@ -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: {
diff --git a/ports/esp8266/network_wlan.c b/ports/esp8266/network_wlan.c
index 012cc970b2..b0e6b3aeb1 100644
--- a/ports/esp8266/network_wlan.c
+++ b/ports/esp8266/network_wlan.c
@@ -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: {
diff --git a/ports/mimxrt/cyw43_configport.h b/ports/mimxrt/cyw43_configport.h
index 5f96856474..f1cfc86cb0 100644
--- a/ports/mimxrt/cyw43_configport.h
+++ b/ports/mimxrt/cyw43_configport.h
@@ -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();
diff --git a/ports/mimxrt/eth.c b/ports/mimxrt/eth.c
index d0df7610cc..befccca1cd 100644
--- a/ports/mimxrt/eth.c
+++ b/ports/mimxrt/eth.c
@@ -559,7 +559,7 @@ STATIC void eth_lwip_init(eth_t *self) {
n->name[0] = 'e';
n->name[1] = (self == &eth_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);
diff --git a/ports/rp2/cyw43_configport.h b/ports/rp2/cyw43_configport.h
index 0c4a032100..929d83537b 100644
--- a/ports/rp2/cyw43_configport.h
+++ b/ports/rp2/cyw43_configport.h
@@ -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) { \
diff --git a/ports/stm32/cyw43_configport.h b/ports/stm32/cyw43_configport.h
index f2a880603a..1ea1ebde17 100644
--- a/ports/stm32/cyw43_configport.h
+++ b/ports/stm32/cyw43_configport.h
@@ -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();
diff --git a/ports/stm32/eth.c b/ports/stm32/eth.c
index 0feacdae2d..b01eee3f2b 100644
--- a/ports/stm32/eth.c
+++ b/ports/stm32/eth.c
@@ -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);