From a3773026238c7c779cad79aa9d9c468e680e7004 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 1 Feb 2023 14:19:45 +1100 Subject: [PATCH] extmod/modnetwork: Add network.hostname() and network.country(). This provides a standard interface to setting the global networking config for all interfaces and interface types. For ports that already use either a static hostname (mimxrt, rp2) they will now use the configured value. The default is configured by the port (or optionally the board). For interfaces that previously supported .config(hostname), this is still supported but now implemented using the global network.hostname. Similarly, pyb.country and rp2.country are now deprecated, but the methods still exist (and forward to network.hostname). Because ESP32/ESP8266 do not use extmod/modnetwork.c they are not affected by this commit. Signed-off-by: Jim Mussared --- extmod/extmod.cmake | 3 ++ extmod/modnetwork.c | 42 +++++++++++++++++++ extmod/modnetwork.h | 8 ++++ extmod/network_cyw43.c | 10 ++++- .../boards/MIMXRT1020_EVK/mpconfigboard.h | 2 + .../boards/MIMXRT1050_EVK/mpconfigboard.h | 2 + .../boards/MIMXRT1060_EVK/mpconfigboard.h | 2 + .../boards/MIMXRT1064_EVK/mpconfigboard.h | 2 + .../boards/MIMXRT1170_EVK/mpconfigboard.h | 3 ++ .../boards/SEEED_ARCH_MIX/mpconfigboard.h | 2 + ports/mimxrt/boards/TEENSY41/mpconfigboard.h | 2 + ports/mimxrt/eth.c | 2 +- ports/mimxrt/mpconfigport.h | 4 ++ ports/rp2/CMakeLists.txt | 12 ------ ports/rp2/boards/PICO_W/mpconfigboard.h | 1 + .../boards/W5100S_EVB_PICO/mpconfigboard.h | 23 +++++----- .../rp2/boards/W5500_EVB_PICO/mpconfigboard.h | 23 +++++----- ports/rp2/cyw43_configport.h | 3 ++ ports/rp2/modrp2.c | 25 +++-------- ports/rp2/mpconfigport.h | 7 ++-- ports/rp2/mpnetworkport.c | 2 - .../ARDUINO_PORTENTA_H7/mpconfigboard.h | 2 + ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 2 + ports/stm32/eth.c | 2 +- ports/stm32/modpyb.c | 27 ++++-------- ports/stm32/mpconfigport.h | 4 ++ 26 files changed, 138 insertions(+), 79 deletions(-) diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index ed7d16d1b5..2a19a0e85c 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -37,6 +37,9 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_EXTMOD_DIR}/moduwebsocket.c ${MICROPY_EXTMOD_DIR}/moduzlib.c ${MICROPY_EXTMOD_DIR}/modwebrepl.c + ${MICROPY_EXTMOD_DIR}/network_cyw43.c + ${MICROPY_EXTMOD_DIR}/network_ninaw10.c + ${MICROPY_EXTMOD_DIR}/network_wiznet5k.c ${MICROPY_EXTMOD_DIR}/uos_dupterm.c ${MICROPY_EXTMOD_DIR}/utime_mphal.c ${MICROPY_EXTMOD_DIR}/vfs.c diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c index d5c8f83ea4..0431e5f418 100644 --- a/extmod/modnetwork.c +++ b/extmod/modnetwork.c @@ -54,6 +54,14 @@ /// /// This module provides network drivers and routing configuration. +char mod_network_country_code[2] = "XX"; + +#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT +#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] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT; + void mod_network_init(void) { mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0); } @@ -89,9 +97,43 @@ STATIC mp_obj_t network_route(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route); +STATIC mp_obj_t network_country(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + return mp_obj_new_str(mod_network_country_code, 2); + } else { + size_t len; + const char *str = mp_obj_str_get_data(args[0], &len); + if (len != 2) { + mp_raise_ValueError(NULL); + } + mod_network_country_code[0] = str[0]; + mod_network_country_code[1] = str[1]; + return mp_const_none; + } +} +// 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) { + if (n_args == 0) { + return mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname)); + } 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); + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, 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) }, { MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) }, + { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) }, + { MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&mod_network_hostname_obj) }, // Defined per port in mpconfigport.h MICROPY_PORT_NETWORK_INTERFACES diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h index a28ec6e420..0ef612d108 100644 --- a/extmod/modnetwork.h +++ b/extmod/modnetwork.h @@ -52,6 +52,14 @@ #define MOD_NETWORK_SS_CONNECTED (2) #define MOD_NETWORK_SS_CLOSED (3) +extern char mod_network_country_code[2]; + +#ifndef MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN +#define MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN (16) +#endif + +extern char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN]; + #if MICROPY_PY_LWIP struct netif; void mod_network_lwip_init(void); diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c index 90d964a67b..95901eaddf 100644 --- a/extmod/network_cyw43.c +++ b/extmod/network_cyw43.c @@ -38,6 +38,7 @@ #if MICROPY_PY_NETWORK_CYW43_USE_LIB_DRIVER #include "lib/cyw43-driver/src/cyw43.h" +#include "lib/cyw43-driver/src/cyw43_country.h" #else #include "drivers/cyw43/cyw43.h" #endif @@ -119,14 +120,21 @@ STATIC mp_obj_t network_cyw43_deinit(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_cyw43_deinit_obj, network_cyw43_deinit); +#if !MICROPY_PY_NETWORK_CYW43_USE_LIB_DRIVER +// TODO: The old driver expects this to be available at link time. +char pyb_country_code[2]; +#endif + STATIC mp_obj_t network_cyw43_active(size_t n_args, const mp_obj_t *args) { network_cyw43_obj_t *self = MP_OBJ_TO_PTR(args[0]); if (n_args == 1) { return mp_obj_new_bool(cyw43_tcpip_link_status(self->cyw, self->itf)); } else { #if MICROPY_PY_NETWORK_CYW43_USE_LIB_DRIVER - cyw43_wifi_set_up(self->cyw, self->itf, mp_obj_is_true(args[1]), MICROPY_CYW43_COUNTRY); + uint32_t country = CYW43_COUNTRY(mod_network_country_code[0], mod_network_country_code[1], 0); + cyw43_wifi_set_up(self->cyw, self->itf, mp_obj_is_true(args[1]), country); #else + memcpy(pyb_country_code, mod_network_country_code, sizeof(pyb_country_code)); cyw43_wifi_set_up(self->cyw, self->itf, mp_obj_is_true(args[1])); #endif return mp_const_none; diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h index 9a9befa126..390e91814d 100644 --- a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h @@ -1,6 +1,8 @@ #define MICROPY_HW_BOARD_NAME "i.MX RT1020 EVK" #define MICROPY_HW_MCU_NAME "MIMXRT1021DAG5A" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-1020evk" + // i.MX RT1020 EVK has 1 board LED // Todo: think about replacing the define with searching in the generated pins? #define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_05) diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h index 9cc740b1a6..134c9637ea 100644 --- a/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h @@ -1,6 +1,8 @@ #define MICROPY_HW_BOARD_NAME "i.MX RT1050 EVKB-A1" #define MICROPY_HW_MCU_NAME "MIMXRT1052DVL6B" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-1050evk" + // MIMXRT1050_EVKB has 1 user LED #define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h index 7eecb1f6b4..01ae3ba303 100644 --- a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h @@ -1,6 +1,8 @@ #define MICROPY_HW_BOARD_NAME "i.MX RT1060 EVK" #define MICROPY_HW_MCU_NAME "MIMXRT1062DVJ6A" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-1060evk" + // MIMXRT1060_EVK has 1 user LED #define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h index 367b4a780b..b6752c3e1b 100644 --- a/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h @@ -1,6 +1,8 @@ #define MICROPY_HW_BOARD_NAME "i.MX RT1064 EVK" #define MICROPY_HW_MCU_NAME "MIMXRT1064DVL6A" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-1064evk" + // MIMXRT1064_EVK has 1 user LED #define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h index 0ec7ed7778..d37050eb56 100644 --- a/ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h @@ -1,5 +1,8 @@ #define MICROPY_HW_BOARD_NAME "i.MX RT1170 EVK" #define MICROPY_HW_MCU_NAME "MIMXRT1176DVMAA" + +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-1070evk" + #define MICROPY_EVENT_POLL_HOOK \ do { \ extern void mp_handle_pending(bool); \ diff --git a/ports/mimxrt/boards/SEEED_ARCH_MIX/mpconfigboard.h b/ports/mimxrt/boards/SEEED_ARCH_MIX/mpconfigboard.h index 37ff6a94c2..c98cdcb0a7 100644 --- a/ports/mimxrt/boards/SEEED_ARCH_MIX/mpconfigboard.h +++ b/ports/mimxrt/boards/SEEED_ARCH_MIX/mpconfigboard.h @@ -1,6 +1,8 @@ #define MICROPY_HW_BOARD_NAME "Seeed ARCH MIX" #define MICROPY_HW_MCU_NAME "MIMXRT1052DVL5B" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-arch-mix" + // MIMXRT1050_EVKB has 1 user LED #define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED2_PIN (pin_GPIO_AD_B0_10) diff --git a/ports/mimxrt/boards/TEENSY41/mpconfigboard.h b/ports/mimxrt/boards/TEENSY41/mpconfigboard.h index 7fe97fd9cc..56740f48ea 100644 --- a/ports/mimxrt/boards/TEENSY41/mpconfigboard.h +++ b/ports/mimxrt/boards/TEENSY41/mpconfigboard.h @@ -1,6 +1,8 @@ #define MICROPY_HW_BOARD_NAME "Teensy 4.1" #define MICROPY_HW_MCU_NAME "MIMXRT1062DVJ6A" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-teensy41" + // Teensy 4.1 has 1 board LED #define MICROPY_HW_LED1_PIN (pin_GPIO_B0_03) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/eth.c b/ports/mimxrt/eth.c index ad015941a2..f1814286e6 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 == ð_instance0 ? '0' : '1'); netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input); - netif_set_hostname(n, "MPY"); + netif_set_hostname(n, mod_network_hostname); netif_set_default(n); netif_set_up(n); diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 2e955fd06a..772f080a92 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -118,6 +118,10 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_LWIP_REENTER MICROPY_PY_PENDSV_REENTER #define MICROPY_PY_LWIP_EXIT MICROPY_PY_PENDSV_EXIT +#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-mimxrt" +#endif + #endif // For regular code that wants to prevent "background tasks" from running. diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 357125e734..8e8db47355 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -263,10 +263,6 @@ if (MICROPY_PY_NETWORK_CYW43) machine_pin_cyw43.c ) - list(APPEND MICROPY_SOURCE_EXTMOD - ${MICROPY_DIR}/extmod/network_cyw43.c - ) - target_link_libraries(${MICROPY_TARGET} cyw43_driver_picow cmsis_core @@ -289,10 +285,6 @@ if (MICROPY_PY_NETWORK_NINAW10) ${MICROPY_DIR}/drivers/ninaw10/nina_wifi_bsp.c ${MICROPY_DIR}/drivers/ninaw10/machine_pin_nina.c ) - - list(APPEND MICROPY_SOURCE_EXTMOD - ${MICROPY_DIR}/extmod/network_ninaw10.c - ) endif() if (MICROPY_PY_NETWORK_WIZNET5K) @@ -331,10 +323,6 @@ if (MICROPY_PY_NETWORK_WIZNET5K) ${MICROPY_DIR}/lib/wiznet5k/Internet/DNS/dns.c ${MICROPY_DIR}/lib/wiznet5k/Internet/DHCP/dhcp.c ) - - list(APPEND MICROPY_SOURCE_EXTMOD - ${MICROPY_DIR}/extmod/network_wiznet5k.c - ) endif() # Add qstr sources for extmod and usermod, in case they are modified by components above. diff --git a/ports/rp2/boards/PICO_W/mpconfigboard.h b/ports/rp2/boards/PICO_W/mpconfigboard.h index 227e7e3ff4..ef812b6301 100644 --- a/ports/rp2/boards/PICO_W/mpconfigboard.h +++ b/ports/rp2/boards/PICO_W/mpconfigboard.h @@ -6,6 +6,7 @@ // Enable networking. #define MICROPY_PY_NETWORK 1 +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "PicoW" // CYW43 driver configuration. #define CYW43_USE_SPI (1) diff --git a/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.h b/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.h index ca2f7d0cea..323cee7a69 100644 --- a/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.h +++ b/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.h @@ -1,19 +1,20 @@ // Board config for Wiznet W5100S-EVB-Pico. // Board and hardware specific configuration -#define MICROPY_HW_BOARD_NAME "W5100S-EVB-Pico" -#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) +#define MICROPY_HW_BOARD_NAME "W5100S-EVB-Pico" +#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) // Enable networking. -#define MICROPY_PY_NETWORK (1) +#define MICROPY_PY_NETWORK (1) +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "W5100S-EVB" // Wiznet HW config. -#define MICROPY_HW_WIZNET_SPI_ID (0) -#define MICROPY_HW_WIZNET_SPI_BAUDRATE (20 * 1000 * 1000) -#define MICROPY_HW_WIZNET_SPI_SCK (18) -#define MICROPY_HW_WIZNET_SPI_MOSI (19) -#define MICROPY_HW_WIZNET_SPI_MISO (16) -#define MICROPY_HW_WIZNET_PIN_CS (17) -#define MICROPY_HW_WIZNET_PIN_RST (20) +#define MICROPY_HW_WIZNET_SPI_ID (0) +#define MICROPY_HW_WIZNET_SPI_BAUDRATE (20 * 1000 * 1000) +#define MICROPY_HW_WIZNET_SPI_SCK (18) +#define MICROPY_HW_WIZNET_SPI_MOSI (19) +#define MICROPY_HW_WIZNET_SPI_MISO (16) +#define MICROPY_HW_WIZNET_PIN_CS (17) +#define MICROPY_HW_WIZNET_PIN_RST (20) // Connecting the INTN pin enables RECV interrupt handling of incoming data. -#define MICROPY_HW_WIZNET_PIN_INTN (21) +#define MICROPY_HW_WIZNET_PIN_INTN (21) diff --git a/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h index 1ad7a0e905..e8526cdba4 100644 --- a/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h +++ b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h @@ -1,19 +1,20 @@ // Board config for Wiznet W5500-EVB-Pico. // Board and hardware specific configuration -#define MICROPY_HW_BOARD_NAME "W5500-EVB-Pico" -#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) +#define MICROPY_HW_BOARD_NAME "W5500-EVB-Pico" +#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) // Enable networking. -#define MICROPY_PY_NETWORK (1) +#define MICROPY_PY_NETWORK (1) +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "W5500-EVB" // Wiznet HW config. -#define MICROPY_HW_WIZNET_SPI_ID (0) -#define MICROPY_HW_WIZNET_SPI_BAUDRATE (20 * 1000 * 1000) -#define MICROPY_HW_WIZNET_SPI_SCK (18) -#define MICROPY_HW_WIZNET_SPI_MOSI (19) -#define MICROPY_HW_WIZNET_SPI_MISO (16) -#define MICROPY_HW_WIZNET_PIN_CS (17) -#define MICROPY_HW_WIZNET_PIN_RST (20) +#define MICROPY_HW_WIZNET_SPI_ID (0) +#define MICROPY_HW_WIZNET_SPI_BAUDRATE (20 * 1000 * 1000) +#define MICROPY_HW_WIZNET_SPI_SCK (18) +#define MICROPY_HW_WIZNET_SPI_MOSI (19) +#define MICROPY_HW_WIZNET_SPI_MISO (16) +#define MICROPY_HW_WIZNET_PIN_CS (17) +#define MICROPY_HW_WIZNET_PIN_RST (20) // Connecting the INTN pin enables RECV interrupt handling of incoming data. -#define MICROPY_HW_WIZNET_PIN_INTN (21) +#define MICROPY_HW_WIZNET_PIN_INTN (21) diff --git a/ports/rp2/cyw43_configport.h b/ports/rp2/cyw43_configport.h index c17b7a5c52..08cded6f1f 100644 --- a/ports/rp2/cyw43_configport.h +++ b/ports/rp2/cyw43_configport.h @@ -30,6 +30,7 @@ #include "py/mpconfig.h" #include "py/mperrno.h" #include "py/mphal.h" +#include "extmod/modnetwork.h" #include "pendsv.h" #define CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE "w43439A0_7_95_49_00_combined.h" @@ -48,6 +49,8 @@ #define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT #define CYW43_THREAD_LOCK_CHECK +#define CYW43_HOST_NAME mod_network_hostname + #define CYW43_SDPCM_SEND_COMMON_WAIT \ if (get_core_num() == 0) { \ cyw43_yield(); \ diff --git a/ports/rp2/modrp2.c b/ports/rp2/modrp2.c index 2601a7f443..5dfedb9929 100644 --- a/ports/rp2/modrp2.c +++ b/ports/rp2/modrp2.c @@ -29,25 +29,11 @@ #include "modrp2.h" #if MICROPY_PY_NETWORK_CYW43 -#include "lib/cyw43-driver/src/cyw43_country.h" +#include "extmod/modnetwork.h" +#endif -extern uint32_t cyw43_country_code; - -STATIC mp_obj_t rp2_country(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - char code[2] = {cyw43_country_code, cyw43_country_code >> 8}; - return mp_obj_new_str(code, 2); - } else { - size_t len; - const char *str = mp_obj_str_get_data(args[0], &len); - if (len != 2) { - mp_raise_ValueError(NULL); - } - cyw43_country_code = CYW43_COUNTRY(str[0], str[1], 0); - return mp_const_none; - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_country_obj, 0, 1, rp2_country); +#if MICROPY_PY_NETWORK_CYW43 +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_country_obj); #endif STATIC const mp_rom_map_elem_t rp2_module_globals_table[] = { @@ -57,7 +43,8 @@ STATIC const mp_rom_map_elem_t rp2_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_StateMachine), MP_ROM_PTR(&rp2_state_machine_type) }, #if MICROPY_PY_NETWORK_CYW43 - { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&rp2_country_obj) }, + // Deprecated (use network.country instead). + { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) }, #endif }; STATIC MP_DEFINE_CONST_DICT(rp2_module_globals, rp2_module_globals_table); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 35bc22e55a..180216329a 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -141,6 +141,10 @@ // By default networking should include sockets, ssl, websockets, webrepl, dupterm. #if MICROPY_PY_NETWORK +#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-rp2" +#endif + #ifndef MICROPY_PY_USOCKET #define MICROPY_PY_USOCKET (1) #endif @@ -263,10 +267,7 @@ typedef intptr_t mp_off_t; extern uint32_t rosc_random_u32(void); extern void lwip_lock_acquire(void); extern void lwip_lock_release(void); - -extern uint32_t cyw43_country_code; extern void cyw43_irq_init(void); extern void cyw43_post_poll_hook(void); #define CYW43_POST_POLL_HOOK cyw43_post_poll_hook(); -#define MICROPY_CYW43_COUNTRY cyw43_country_code diff --git a/ports/rp2/mpnetworkport.c b/ports/rp2/mpnetworkport.c index 96cd16af0f..d8e33e229d 100644 --- a/ports/rp2/mpnetworkport.c +++ b/ports/rp2/mpnetworkport.c @@ -40,14 +40,12 @@ static alarm_id_t lwip_alarm_id = -1; #if MICROPY_PY_NETWORK_CYW43 #include "lib/cyw43-driver/src/cyw43.h" -#include "lib/cyw43-driver/src/cyw43_country.h" #include "lib/cyw43-driver/src/cyw43_stats.h" #include "hardware/irq.h" #define CYW43_IRQ_LEVEL GPIO_IRQ_LEVEL_HIGH #define CYW43_SHARED_IRQ_HANDLER_PRIORITY PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY -uint32_t cyw43_country_code = CYW43_COUNTRY_WORLDWIDE; volatile int cyw43_has_pending = 0; static void gpio_irq_handler(void) { diff --git a/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h b/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h index a2981352a1..986ed2b877 100644 --- a/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h +++ b/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h @@ -9,6 +9,8 @@ #define MICROPY_PY_SYS_PLATFORM "Portenta" #define MICROPY_HW_FLASH_FS_LABEL "portenta" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "Portenta" + #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C) #define UINT_FMT "%u" #define INT_FMT "%d" diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 6dfb8c38d2..09736d8bd7 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -27,6 +27,8 @@ #define MICROPY_HW_BOARD_NAME "PYBD-SF2W" #define MICROPY_HW_MCU_NAME "STM32F722IEK" +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "PYBD" + #define MICROPY_PY_PYB_LEGACY (1) #define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0) #define MICROPY_HW_HAS_SWITCH (1) diff --git a/ports/stm32/eth.c b/ports/stm32/eth.c index be418235e1..1207a728c3 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, "MPY"); + netif_set_hostname(n, mod_network_hostname); netif_set_default(n); netif_set_up(n); diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 7f87dd5820..641d66efaf 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -52,11 +52,10 @@ #include "usb.h" #include "portmodules.h" #include "modmachine.h" +#include "extmod/modnetwork.h" #include "extmod/vfs.h" #include "extmod/utime_mphal.h" -char pyb_country_code[2]; - #if MICROPY_PY_PYB STATIC mp_obj_t pyb_fault_debug(mp_obj_t value) { @@ -115,21 +114,9 @@ STATIC mp_obj_t pyb_repl_uart(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart); -STATIC mp_obj_t pyb_country(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - return mp_obj_new_str(pyb_country_code, 2); - } else { - size_t len; - const char *str = mp_obj_str_get_data(args[0], &len); - if (len != 2) { - mp_raise_ValueError(NULL); - } - pyb_country_code[0] = str[0]; - pyb_country_code[1] = str[1]; - return mp_const_none; - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_country_obj, 0, 1, pyb_country); +#if MICROPY_PY_NETWORK +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_country_obj); +#endif STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) }, @@ -160,7 +147,11 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) }, { MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&pyb_repl_uart_obj) }, - { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&pyb_country_obj) }, + + #if MICROPY_PY_NETWORK + // Deprecated (use network.country instead). + { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) }, + #endif #if MICROPY_HW_ENABLE_USB { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) }, diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 18521ef1d8..ee12ab8140 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -290,6 +290,10 @@ static inline mp_uint_t disable_irq(void) { #define MICROPY_PY_LWIP_REENTER MICROPY_PY_PENDSV_REENTER #define MICROPY_PY_LWIP_EXIT MICROPY_PY_PENDSV_EXIT +#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT +#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-stm32" +#endif + #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // Bluetooth code only runs in the scheduler, no locking/mutex required. #define MICROPY_PY_BLUETOOTH_ENTER uint32_t atomic_state = 0;