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 <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2023-02-01 14:19:45 +11:00 committed by Damien George
parent fc4c47f7bc
commit a377302623
26 changed files with 138 additions and 79 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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)

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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); \

View File

@ -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)

View File

@ -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))

View File

@ -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, "MPY");
netif_set_hostname(n, mod_network_hostname);
netif_set_default(n);
netif_set_up(n);

View File

@ -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.

View File

@ -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.

View File

@ -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)

View File

@ -6,6 +6,7 @@
// Enable networking.
#define MICROPY_PY_NETWORK (1)
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "W5100S-EVB"
// Wiznet HW config.
#define MICROPY_HW_WIZNET_SPI_ID (0)

View File

@ -6,6 +6,7 @@
// Enable networking.
#define MICROPY_PY_NETWORK (1)
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "W5500-EVB"
// Wiznet HW config.
#define MICROPY_HW_WIZNET_SPI_ID (0)

View File

@ -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(); \

View File

@ -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);

View File

@ -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

View File

@ -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) {

View File

@ -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"

View File

@ -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)

View File

@ -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);

View File

@ -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) },

View File

@ -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;