Skip to content

Commit

Permalink
extmod/modnetwork: Add network.hostname() and network.country().
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jimmo authored and dpgeorge committed Feb 28, 2023
1 parent fc4c47f commit a377302
Show file tree
Hide file tree
Showing 26 changed files with 138 additions and 79 deletions.
3 changes: 3 additions & 0 deletions extmod/extmod.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions extmod/modnetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions extmod/modnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion extmod/network_cyw43.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
3 changes: 3 additions & 0 deletions ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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); \
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/SEEED_ARCH_MIX/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/TEENSY41/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion ports/mimxrt/eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions ports/mimxrt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions ports/rp2/boards/PICO_W/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 12 additions & 11 deletions ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions ports/rp2/cyw43_configport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(); \
Expand Down
25 changes: 6 additions & 19 deletions ports/rp2/modrp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,11 @@
#include "modrp2.h"

#if MICROPY_PY_NETWORK_CYW43
#include "lib/cyw43-driver/src/cyw43_country.h"

extern uint32_t cyw43_country_code;
#include "extmod/modnetwork.h"
#endif

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[] = {
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions ports/rp2/mpnetworkport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit a377302

Please sign in to comment.