mirror of
https://github.com/micropython/micropython.git
synced 2026-01-05 03:30:14 +01:00
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:
committed by
Damien George
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) },
|
||||
|
||||
Reference in New Issue
Block a user