Skip to content

Commit 65a3ce3

Browse files
jimmodpgeorge
authored andcommitted
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>
1 parent b329fdc commit 65a3ce3

10 files changed

Lines changed: 28 additions & 37 deletions

File tree

extmod/modnetwork.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ char mod_network_country_code[2] = "XX";
5656
#error "MICROPY_PY_NETWORK_HOSTNAME_DEFAULT must be set in mpconfigport.h or mpconfigboard.h"
5757
#endif
5858

59-
char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
59+
char mod_network_hostname_data[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
6060

6161
#ifdef MICROPY_PORT_NETWORK_INTERFACES
6262

@@ -116,20 +116,21 @@ STATIC mp_obj_t network_country(size_t n_args, const mp_obj_t *args) {
116116
// TODO: Non-static to allow backwards-compatible pyb.country.
117117
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_country_obj, 0, 1, network_country);
118118

119-
STATIC mp_obj_t network_hostname(size_t n_args, const mp_obj_t *args) {
119+
mp_obj_t mod_network_hostname(size_t n_args, const mp_obj_t *args) {
120120
if (n_args == 0) {
121-
return mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
121+
return mp_obj_new_str(mod_network_hostname_data, strlen(mod_network_hostname_data));
122122
} else {
123123
size_t len;
124124
const char *str = mp_obj_str_get_data(args[0], &len);
125125
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
126126
mp_raise_ValueError(NULL);
127127
}
128-
strcpy(mod_network_hostname, str);
128+
memcpy(mod_network_hostname_data, str, len);
129+
mod_network_hostname_data[len] = 0;
129130
return mp_const_none;
130131
}
131132
}
132-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, network_hostname);
133+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, mod_network_hostname);
133134

134135
STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
135136
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },

extmod/modnetwork.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ extern char mod_network_country_code[2];
6161
#endif
6262

6363
// This is a null-terminated string.
64-
extern char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1];
64+
extern char mod_network_hostname_data[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1];
65+
66+
// To support backwards-compatible (esp32, esp8266, cyw43)
67+
// `if.config(hostname=...)` to forward directly to the implementation of
68+
// `network.hostname(...)`.
69+
mp_obj_t mod_network_hostname(size_t n_args, const mp_obj_t *args);
6570

6671
#if MICROPY_PY_LWIP
6772
struct netif;

extmod/network_cyw43.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map
422422
}
423423
case MP_QSTR_hostname: {
424424
// TODO: Deprecated. Use network.hostname() instead.
425-
return mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
425+
return mod_network_hostname(0, NULL);
426426
}
427427
default:
428428
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
498498
}
499499
case MP_QSTR_hostname: {
500500
// TODO: Deprecated. Use network.hostname(name) instead.
501-
size_t len;
502-
const char *str = mp_obj_str_get_data(e->value, &len);
503-
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
504-
mp_raise_ValueError(NULL);
505-
}
506-
strcpy(mod_network_hostname, str);
501+
mod_network_hostname(1, &e->value);
507502
break;
508503
}
509504
default:

ports/esp32/network_wlan.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ static void network_wlan_ip_event_handler(void *event_handler_arg, esp_event_bas
169169
if (!mdns_initialised) {
170170
mdns_init();
171171
#if MICROPY_HW_ENABLE_MDNS_RESPONDER
172-
mdns_hostname_set(mod_network_hostname);
173-
mdns_instance_name_set(mod_network_hostname);
172+
mdns_hostname_set(mod_network_hostname_data);
173+
mdns_instance_name_set(mod_network_hostname_data);
174174
#endif
175175
mdns_initialised = true;
176176
}
@@ -305,7 +305,7 @@ STATIC mp_obj_t network_wlan_connect(size_t n_args, const mp_obj_t *pos_args, mp
305305
esp_exceptions(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
306306
}
307307

308-
esp_exceptions(esp_netif_set_hostname(wlan_sta_obj.netif, mod_network_hostname));
308+
esp_exceptions(esp_netif_set_hostname(wlan_sta_obj.netif, mod_network_hostname_data));
309309

310310
wifi_sta_reconnects = 0;
311311
// 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_
522522
case MP_QSTR_hostname:
523523
case MP_QSTR_dhcp_hostname: {
524524
// TODO: Deprecated. Use network.hostname(name) instead.
525-
size_t len;
526-
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
527-
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
528-
mp_raise_ValueError(NULL);
529-
}
530-
strcpy(mod_network_hostname, str);
525+
mod_network_hostname(1, &kwargs->table[i].value);
531526
break;
532527
}
533528
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_
630625
case MP_QSTR_dhcp_hostname: {
631626
// TODO: Deprecated. Use network.hostname() instead.
632627
req_if = ESP_IF_WIFI_STA;
633-
val = mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
628+
val = mod_network_hostname(0, NULL);
634629
break;
635630
}
636631
case MP_QSTR_max_clients: {

ports/esp8266/network_wlan.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
152152
error_check(wifi_station_set_config(&config), "Cannot set STA config");
153153
}
154154

155-
wifi_station_set_hostname(mod_network_hostname);
155+
wifi_station_set_hostname(mod_network_hostname_data);
156156

157157
error_check(wifi_station_connect(), "Cannot connect to AP");
158158

@@ -402,12 +402,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
402402
case MP_QSTR_hostname:
403403
case MP_QSTR_dhcp_hostname: {
404404
// TODO: Deprecated. Use network.hostname(name) instead.
405-
size_t len;
406-
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
407-
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
408-
mp_raise_ValueError(NULL);
409-
}
410-
strcpy(mod_network_hostname, str);
405+
mod_network_hostname(1, &kwargs->table[i].value);
411406
break;
412407
}
413408
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
481476
break;
482477
case MP_QSTR_hostname:
483478
case MP_QSTR_dhcp_hostname: {
484-
req_if = STATION_IF;
485479
// TODO: Deprecated. Use network.hostname() instead.
486-
val = mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname));
480+
req_if = STATION_IF;
481+
val = mod_network_hostname(0, NULL);
487482
break;
488483
}
489484
case MP_QSTR_protocol: {

ports/mimxrt/cyw43_configport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
6262
#define CYW43_THREAD_LOCK_CHECK
6363

64-
#define CYW43_HOST_NAME mod_network_hostname
64+
#define CYW43_HOST_NAME mod_network_hostname_data
6565

6666
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFI();
6767
#define CYW43_DO_IOCTL_WAIT __WFI();

ports/mimxrt/eth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ STATIC void eth_lwip_init(eth_t *self) {
559559
n->name[0] = 'e';
560560
n->name[1] = (self == &eth_instance0 ? '0' : '1');
561561
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
562-
netif_set_hostname(n, mod_network_hostname);
562+
netif_set_hostname(n, mod_network_hostname_data);
563563
netif_set_default(n);
564564
netif_set_up(n);
565565

ports/rp2/cyw43_configport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
4949
#define CYW43_THREAD_LOCK_CHECK
5050

51-
#define CYW43_HOST_NAME mod_network_hostname
51+
#define CYW43_HOST_NAME mod_network_hostname_data
5252

5353
#define CYW43_SDPCM_SEND_COMMON_WAIT \
5454
if (get_core_num() == 0) { \

ports/stm32/cyw43_configport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
6363
#define CYW43_THREAD_LOCK_CHECK
6464

65-
#define CYW43_HOST_NAME mod_network_hostname
65+
#define CYW43_HOST_NAME mod_network_hostname_data
6666

6767
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFI();
6868
#define CYW43_DO_IOCTL_WAIT __WFI();

ports/stm32/eth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ STATIC void eth_lwip_init(eth_t *self) {
737737
n->name[0] = 'e';
738738
n->name[1] = '0';
739739
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
740-
netif_set_hostname(n, mod_network_hostname);
740+
netif_set_hostname(n, mod_network_hostname_data);
741741
netif_set_default(n);
742742
netif_set_up(n);
743743

0 commit comments

Comments
 (0)