From 5a6b504ec824708259029ef5dd6942b1a71f9d4d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 18 Nov 2026 14:11:04 -0500 Subject: [PATCH 1/4] os.uname(): sysname is CircuitPython; nodename is the hostname sysname and nodename were both the microcontroller name. - sysname is "CircuitPython", or "Zephyr" on zephyr-cp (CIRCUITPY_OS_UNAME_SYSNAME). - On boards with Wi-Fi or Ethernet, nodename is the network hostname, read on each call. Before the interface is set up, it is the hostname the interface will use: CIRCUITPY_WIFI_HOSTNAME if set, otherwise the port's default. - On boards without networking, nodename is still the microcontroller name, and os.uname() still returns a constant tuple. Move the "cpy--" default-hostname formatting, which was copied in three places, into supervisor/shared/network.c. Add per-port wifi_hostname_is_set(), wifi_get_default_hostname() and mii_get_default_hostname(), which work before the interface is set up. Fixes #10153. Co-Authored-By: Claude Opus 5.5 --- ports/espressif/common-hal/mii/Ethernet.c | 17 ++++--- ports/espressif/common-hal/wifi/__init__.c | 47 +++++++++++++------- ports/raspberrypi/common-hal/wifi/__init__.c | 14 ++++++ ports/zephyr-cp/common-hal/mii/Ethernet.c | 42 +++++++++++------ ports/zephyr-cp/common-hal/wifi/__init__.c | 47 ++++++++++++++------ ports/zephyr-cp/mpconfigport.h | 2 + ports/zephyr-cp/tests/test_os.py | 31 +++++++++++++ py/circuitpy_mpconfig.h | 5 +++ shared-bindings/mii/__init__.h | 7 +++ shared-bindings/os/__init__.c | 34 +++++++++++++- shared-bindings/os/__init__.h | 9 ++++ shared-bindings/wifi/__init__.h | 10 +++++ shared-module/os/__init__.c | 43 ++++++++++++++++++ supervisor/shared/network.c | 30 +++++++++++++ supervisor/shared/network.h | 23 ++++++++++ supervisor/supervisor.mk | 5 +++ 16 files changed, 317 insertions(+), 49 deletions(-) create mode 100644 ports/zephyr-cp/tests/test_os.py create mode 100644 supervisor/shared/network.c create mode 100644 supervisor/shared/network.h diff --git a/ports/espressif/common-hal/mii/Ethernet.c b/ports/espressif/common-hal/mii/Ethernet.c index e3a8c77d6c8..349a3938e15 100644 --- a/ports/espressif/common-hal/mii/Ethernet.c +++ b/ports/espressif/common-hal/mii/Ethernet.c @@ -170,12 +170,8 @@ void common_hal_mii_ethernet_construct(mii_ethernet_obj_t *self, CHECK_ESP_RESULT(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, _eth_event_handler, self)); // Set hostname - uint8_t local_mac[MAC_ADDRESS_LENGTH]; - esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, local_mac); char hostname[32]; - snprintf(hostname, sizeof(hostname), "cpy-%02x%02x%02x%02x%02x%02x", - local_mac[0], local_mac[1], local_mac[2], - local_mac[3], local_mac[4], local_mac[5]); + mii_get_default_hostname(hostname, sizeof(hostname)); esp_netif_set_hostname(self->netif, hostname); // Start the ethernet driver (DHCP starts automatically with default netif config) @@ -235,6 +231,17 @@ const char *mii_get_hostname_raw(void) { return hostname == NULL ? "" : hostname; } +void mii_get_default_hostname(char *buf, size_t buf_size) { + if (buf_size == 0) { + return; + } + // The Ethernet constructor sets the MAC to the chip's factory default MAC. + uint8_t mac[MAC_ADDRESS_LENGTH] = {0}; + esp_efuse_mac_get_default(mac); + snprintf(buf, buf_size, "cpy-%02x%02x%02x%02x%02x%02x", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + // Raw uint32_t IPv4 address of the interface, in network byte order, or 0 // when the interface has none. Used by supervisor/shared/web_workflow. uint32_t mii_get_ipv4_address(void) { diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index 08818ce5022..d2f8faa9766 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -17,6 +17,7 @@ #include "py/mpstate.h" #include "py/runtime.h" +#include "esp_mac.h" #include "esp_wifi.h" #include "components/heap/include/esp_heap_caps.h" @@ -26,6 +27,7 @@ wifi_radio_obj_t common_hal_wifi_radio_obj; #include "components/log/include/esp_log.h" #include "supervisor/port.h" +#include "supervisor/shared/network.h" #include "supervisor/workflow.h" #include "lwip/sockets.h" @@ -585,23 +587,12 @@ void common_hal_wifi_init(bool user_initiated) { } #endif else { - // Set the default lwip_local_hostname capped at 32 characters. We trim off - // the start of the board name (likely manufacturer) because the end is - // often more unique to the board. - size_t board_len = MIN(32 - ((MAC_ADDRESS_LENGTH * 2) + 6), strlen(CIRCUITPY_BOARD_ID)); - size_t board_trim = strlen(CIRCUITPY_BOARD_ID) - board_len; - // Avoid double _ in the hostname. - if (CIRCUITPY_BOARD_ID[board_trim] == '_') { - board_trim++; - } - - char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6]; + // Set the default hostname, capped at 32 characters. + char cpy_default_hostname[32]; uint8_t mac[MAC_ADDRESS_LENGTH]; esp_wifi_get_mac(WIFI_IF_STA, mac); - snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%02x%02x%02x%02x%02x%02x", CIRCUITPY_BOARD_ID + board_trim, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - - const char *default_lwip_local_hostname = cpy_default_hostname; - ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname)); + network_format_default_hostname(cpy_default_hostname, sizeof(cpy_default_hostname), mac); + ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, cpy_default_hostname)); } // set station mode to avoid the default SoftAP @@ -610,6 +601,32 @@ void common_hal_wifi_init(bool user_initiated) { common_hal_wifi_radio_set_enabled(self, true); } +bool wifi_hostname_is_set(void) { + return wifi_inited; +} + +void wifi_get_default_hostname(char *buf, size_t buf_size) { + if (buf_size < 18) { + if (buf_size > 0) { + buf[0] = '\0'; + } + return; + } + // esp_wifi_get_mac() works only after the Wi-Fi driver starts. + // esp_read_mac() reads the factory MAC from the chip's one-time-programmable + // memory (efuse), so it works before the driver starts. + // + // With ESP-Hosted, a separate co-processor chip runs the Wi-Fi radio, and + // common_hal_wifi_init() uses that chip's MAC. So on those boards this + // hostname may differ from the one common_hal_wifi_init() sets. + uint8_t mac[MAC_ADDRESS_LENGTH] = {0}; + if (esp_read_mac(mac, ESP_MAC_WIFI_STA) != ESP_OK) { + memset(mac, 0, sizeof(mac)); + } + // Use the same length limit as common_hal_wifi_init(). + network_format_default_hostname(buf, MIN(buf_size, 32), mac); +} + void wifi_user_reset(void) { if (wifi_user_initiated) { wifi_reset(); diff --git a/ports/raspberrypi/common-hal/wifi/__init__.c b/ports/raspberrypi/common-hal/wifi/__init__.c index 84dba581fbf..606cc8480fb 100644 --- a/ports/raspberrypi/common-hal/wifi/__init__.c +++ b/ports/raspberrypi/common-hal/wifi/__init__.c @@ -11,10 +11,14 @@ #include "shared-bindings/wifi/Monitor.h" #include "shared-bindings/wifi/Radio.h" +#include + #include "py/mperrno.h" #include "py/mpstate.h" #include "py/runtime.h" +#include "lib/cyw43-driver/src/cyw43_config.h" + #if CIRCUITPY_SETTINGS_TOML #include "supervisor/shared/settings.h" #endif @@ -62,6 +66,16 @@ void common_hal_wifi_init(bool user_initiated) { common_hal_wifi_radio_set_enabled(self, true); } +bool wifi_hostname_is_set(void) { + return wifi_inited; +} + +void wifi_get_default_hostname(char *buf, size_t buf_size) { + // When the station interface starts, the driver for the CYW43 Wi-Fi chip + // gives it the name CYW43_HOST_NAME. The Pico SDK sets that to "PicoW". + strlcpy(buf, CYW43_HOST_NAME, buf_size); +} + void wifi_user_reset(void) { if (wifi_user_initiated) { wifi_reset(); diff --git a/ports/zephyr-cp/common-hal/mii/Ethernet.c b/ports/zephyr-cp/common-hal/mii/Ethernet.c index 29bba01c589..c5923ca3d45 100644 --- a/ports/zephyr-cp/common-hal/mii/Ethernet.c +++ b/ports/zephyr-cp/common-hal/mii/Ethernet.c @@ -12,6 +12,7 @@ #include "bindings/zephyr_kernel/__init__.h" #include "supervisor/port.h" +#include "supervisor/shared/network.h" #include "supervisor/workflow.h" #include "shared/runtime/interrupt_char.h" @@ -354,21 +355,12 @@ void common_hal_mii_ethernet_construct(mii_ethernet_obj_t *self, net_mgmt_add_event_callback(ð_ipv4_cb); #if defined(CONFIG_NET_HOSTNAME) - // Set hostname based on board name and MAC address. - size_t board_len = MIN(NET_HOSTNAME_MAX_LEN - ((MAC_ADDRESS_LENGTH * 2) + 6), strlen(CIRCUITPY_BOARD_ID)); - size_t board_trim = strlen(CIRCUITPY_BOARD_ID) - board_len; - if (CIRCUITPY_BOARD_ID[board_trim] == '_') { - board_trim++; - } - - char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6]; + // Set the default hostname, capped at NET_HOSTNAME_MAX_LEN characters. + char cpy_default_hostname[NET_HOSTNAME_MAX_LEN]; struct net_linkaddr *mac = net_if_get_link_addr(self->netif); if (mac->len >= MAC_ADDRESS_LENGTH) { - snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%02x%02x%02x%02x%02x%02x", - CIRCUITPY_BOARD_ID + board_trim, - mac->addr[0], mac->addr[1], mac->addr[2], - mac->addr[3], mac->addr[4], mac->addr[5]); - net_hostname_set(cpy_default_hostname, strlen(cpy_default_hostname)); + size_t len = network_format_default_hostname(cpy_default_hostname, sizeof(cpy_default_hostname), mac->addr); + net_hostname_set(cpy_default_hostname, len); } #endif @@ -412,6 +404,30 @@ const char *mii_get_hostname_raw(void) { return net_hostname_get(); } +void mii_get_default_hostname(char *buf, size_t buf_size) { + if (buf_size == 0) { + return; + } + buf[0] = '\0'; + #if defined(CONFIG_NET_HOSTNAME) + if (buf_size < 18) { + return; + } + // The Ethernet constructor uses the first Ethernet network interface. + // Zephyr creates that interface at boot, so it already exists. + struct net_if *netif = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET)); + if (netif == NULL) { + return; + } + struct net_linkaddr *mac = net_if_get_link_addr(netif); + if (mac->len < MAC_ADDRESS_LENGTH) { + return; + } + // Use the same length limit as the constructor. + network_format_default_hostname(buf, MIN(buf_size, NET_HOSTNAME_MAX_LEN), mac->addr); + #endif +} + // Blocks until the DHCP lease is bound. DHCPv4 parses its options (which adds // the DNS server to the resolver) and only then binds the address, so a // preferred global address implies getaddrinfo() has everything it needs. diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.c b/ports/zephyr-cp/common-hal/wifi/__init__.c index 182ae5a3c30..071a4655d17 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.c +++ b/ports/zephyr-cp/common-hal/wifi/__init__.c @@ -20,6 +20,7 @@ wifi_radio_obj_t common_hal_wifi_radio_obj; #include "supervisor/port.h" +#include "supervisor/shared/network.h" #include "supervisor/workflow.h" #if CIRCUITPY_SETTINGS_TOML @@ -322,16 +323,6 @@ void common_hal_wifi_init(bool user_initiated) { net_mgmt_add_event_callback(&ipv4_cb); #if defined(CONFIG_NET_HOSTNAME) - // Set the default hostname capped at NET_HOSTNAME_MAX_LEN characters. We trim off - // the start of the board name (likely manufacturer) because the end is - // often more unique to the board. - size_t board_len = MIN(NET_HOSTNAME_MAX_LEN - ((MAC_ADDRESS_LENGTH * 2) + 6), strlen(CIRCUITPY_BOARD_ID)); - size_t board_trim = strlen(CIRCUITPY_BOARD_ID) - board_len; - // Avoid double _ in the hostname. - if (CIRCUITPY_BOARD_ID[board_trim] == '_') { - board_trim++; - } - #if CIRCUITPY_SETTINGS_TOML char hostname[NET_HOSTNAME_MAX_LEN]; if (settings_get_str("CIRCUITPY_WIFI_HOSTNAME", hostname, sizeof(hostname)) == SETTINGS_OK) { @@ -342,14 +333,14 @@ void common_hal_wifi_init(bool user_initiated) { } #endif else { - char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6]; + // Set the default hostname, capped at NET_HOSTNAME_MAX_LEN characters. + char cpy_default_hostname[NET_HOSTNAME_MAX_LEN]; struct net_linkaddr *mac = net_if_get_link_addr(self->sta_netif); if (mac->len < MAC_ADDRESS_LENGTH) { LOG_ERR("MAC address too short"); } - snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%02x%02x%02x%02x%02x%02x", CIRCUITPY_BOARD_ID + board_trim, mac->addr[0], mac->addr[1], mac->addr[2], mac->addr[3], mac->addr[4], mac->addr[5]); - - CHECK_ZEPHYR_RESULT(net_hostname_set(cpy_default_hostname, strlen(cpy_default_hostname))); + size_t len = network_format_default_hostname(cpy_default_hostname, sizeof(cpy_default_hostname), mac->addr); + CHECK_ZEPHYR_RESULT(net_hostname_set(cpy_default_hostname, len)); } #else LOG_WRN("Hostname support disabled in Zephyr config"); @@ -362,6 +353,34 @@ void common_hal_wifi_init(bool user_initiated) { LOG_DBG("common_hal_wifi_init done"); } +bool wifi_hostname_is_set(void) { + return wifi_inited; +} + +void wifi_get_default_hostname(char *buf, size_t buf_size) { + if (buf_size == 0) { + return; + } + buf[0] = '\0'; + #if defined(CONFIG_NET_HOSTNAME) + if (buf_size < 18) { + return; + } + // Zephyr creates the interface at boot, so it exists before + // common_hal_wifi_init() runs. + struct net_if *sta_netif = net_if_get_wifi_sta(); + if (sta_netif == NULL) { + return; + } + struct net_linkaddr *mac = net_if_get_link_addr(sta_netif); + if (mac->len < MAC_ADDRESS_LENGTH) { + return; + } + // Use the same length limit as common_hal_wifi_init(). + network_format_default_hostname(buf, MIN(buf_size, NET_HOSTNAME_MAX_LEN), mac->addr); + #endif +} + void wifi_user_reset(void) { if (wifi_user_initiated) { wifi_reset(); diff --git a/ports/zephyr-cp/mpconfigport.h b/ports/zephyr-cp/mpconfigport.h index cc990c0208e..4f0a94bd65a 100644 --- a/ports/zephyr-cp/mpconfigport.h +++ b/ports/zephyr-cp/mpconfigport.h @@ -18,6 +18,8 @@ #endif #define MICROPY_PY_SYS_PLATFORM "Zephyr" +// Override the default name "CircuitPython", set in py/circuitpy_mpconfig.h. +#define CIRCUITPY_OS_UNAME_SYSNAME "Zephyr" // Add "(Zephyr)" to distinguish zephyr-cp builds from classic-port builds for the same board in the banner. #define MICROPY_BANNER_MACHINE MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME " (Zephyr)" diff --git a/ports/zephyr-cp/tests/test_os.py b/ports/zephyr-cp/tests/test_os.py new file mode 100644 index 00000000000..37ef9d74b40 --- /dev/null +++ b/ports/zephyr-cp/tests/test_os.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2026 Dan Halbert for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""Test the os core module.""" + +import pytest + + +UNAME_CODE = """\ +import os + +u = os.uname() +print(f"sysname: {u.sysname}") +print(f"nodename: {u.nodename}") +print(f"machine: {u.machine}") +print("done") +""" + + +@pytest.mark.circuitpy_drive({"code.py": UNAME_CODE}) +def test_os_uname(circuitpython): + """sysname is "Zephyr". With no Wi-Fi or Ethernet, nodename is the microcontroller name.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "sysname: Zephyr" in output + nodename = output.split("nodename: ", 1)[1].split("\n", 1)[0].strip() + assert nodename + # machine is " with ". + assert f" with {nodename}" in output + assert "done" in output diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 82f528d264a..126f5326a29 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -576,6 +576,11 @@ void background_callback_run_all(void); #define CIRCUITPY_VERBOSE_BLE 0 #endif +// Value of os.uname().sysname. +#ifndef CIRCUITPY_OS_UNAME_SYSNAME +#define CIRCUITPY_OS_UNAME_SYSNAME "CircuitPython" +#endif + // Display the Blinka logo in the REPL on displayio displays. #ifndef CIRCUITPY_REPL_LOGO #define CIRCUITPY_REPL_LOGO (1) diff --git a/shared-bindings/mii/__init__.h b/shared-bindings/mii/__init__.h index 1c3312cedb2..3343215c114 100644 --- a/shared-bindings/mii/__init__.h +++ b/shared-bindings/mii/__init__.h @@ -29,6 +29,13 @@ uint32_t mii_get_ipv4_address(void); // so use it promptly rather than holding onto it. const char *mii_get_hostname_raw(void); +// Gets the hostname that common_hal_mii_ethernet_construct() gives the Ethernet +// interface. Can be called before common_hal_mii_ethernet_construct() runs. +// Writes the hostname to buf as a NUL-terminated string, truncated to fit in +// buf_size bytes. If there is no default hostname, writes "". Never allocates +// or raises. +void mii_get_default_hostname(char *buf, size_t buf_size); + // Blocks until the interface has an IPv4 address (DHCP lease or static), or // 20s pass, or a hard interrupt arrives. Returns immediately when the // interface cannot ever get one (down, disabled or no link). diff --git a/shared-bindings/os/__init__.c b/shared-bindings/os/__init__.c index 6478a2cf5b4..73a3a3c80c4 100644 --- a/shared-bindings/os/__init__.c +++ b/shared-bindings/os/__init__.c @@ -38,22 +38,51 @@ //| """The type of values that :py:func:`.uname()` returns""" //| //| sysname: str +//| """``"CircuitPython"``, or ``"Zephyr"`` on the Zephyr port.""" //| nodename: str +//| """The network hostname, read each time :py:func:`.uname()` is called. +//| Before Wi-Fi or Ethernet is set up, this is the hostname that the interface will use. +//| For Wi-Fi, that is ``CIRCUITPY_WIFI_HOSTNAME`` from ``settings.toml`` if it is set. +//| Otherwise it is the interface's default hostname. +//| On boards where Wi-Fi runs on a separate co-processor, +//| the hostname before Wi-Fi is set up may not exactly match the one it will use. +//| On boards without Wi-Fi or Ethernet, this is the microcontroller name, such as ``"nRF52840"``.""" //| release: str +//| """The CircuitPython version.""" //| version: str +//| """The CircuitPython build version and build date.""" //| machine: str +//| """The board name and microcontroller name.""" //| //| static const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine }; -static const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, MICROPY_HW_MCU_NAME); -static const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, MICROPY_HW_MCU_NAME); +static const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, CIRCUITPY_OS_UNAME_SYSNAME); static const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING); static const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE); static const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME); +#if CIRCUITPY_WIFI || CIRCUITPY_MII +// The hostname can change, so build a new tuple each time. +static mp_obj_t os_uname(void) { + // A hostname has at most 63 characters. + char nodename[64]; + common_hal_os_get_nodename(nodename, sizeof(nodename)); + const mp_obj_t items[] = { + MP_OBJ_FROM_PTR(&os_uname_info_sysname_obj), + mp_obj_new_str(nodename, strlen(nodename)), + MP_OBJ_FROM_PTR(&os_uname_info_release_obj), + MP_OBJ_FROM_PTR(&os_uname_info_version_obj), + MP_OBJ_FROM_PTR(&os_uname_info_machine_obj), + }; + return mp_obj_new_attrtuple(os_uname_info_fields, MP_ARRAY_SIZE(items), items); +} +#else +// Without networking, nodename is always the microcontroller name, so the +// tuple never changes. +static const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, MICROPY_HW_MCU_NAME); static MP_DEFINE_ATTRTUPLE( os_uname_info_obj, @@ -69,6 +98,7 @@ static MP_DEFINE_ATTRTUPLE( static mp_obj_t os_uname(void) { return MP_OBJ_FROM_PTR(&os_uname_info_obj); } +#endif static MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); //| def chdir(path: str) -> None: diff --git a/shared-bindings/os/__init__.h b/shared-bindings/os/__init__.h index 3729f43d5b6..f81ed4219e9 100644 --- a/shared-bindings/os/__init__.h +++ b/shared-bindings/os/__init__.h @@ -17,6 +17,15 @@ mp_obj_t common_hal_os_getcwd(void); mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_); mp_obj_t common_hal_os_getenv_path(const char *path, const char *key, mp_obj_t default_); +#if CIRCUITPY_WIFI || CIRCUITPY_MII +// Gets the value of os.uname().nodename. That is the network hostname, or the +// hostname the network interface will be given once it is set up, or +// MICROPY_HW_MCU_NAME if there is no hostname. Writes the value to buf as a +// NUL-terminated string, truncated to fit in buf_size bytes. If buf_size is 0, +// writes nothing. Never allocates or raises. +void common_hal_os_get_nodename(char *buf, size_t buf_size); +#endif + mp_obj_t common_hal_os_listdir(const char *path); void common_hal_os_mkdir(const char *path); void common_hal_os_remove(const char *path); diff --git a/shared-bindings/wifi/__init__.h b/shared-bindings/wifi/__init__.h index a4de567023a..9c9166dae44 100644 --- a/shared-bindings/wifi/__init__.h +++ b/shared-bindings/wifi/__init__.h @@ -21,3 +21,13 @@ void wifi_user_reset(void); // so use it promptly rather than holding onto it. Used by // supervisor/shared/web_workflow. const char *wifi_get_hostname_raw(void); + +// True once common_hal_wifi_init() has set the interface's hostname, so that +// wifi_get_hostname_raw() returns it. Never allocates or raises. +bool wifi_hostname_is_set(void); + +// Gets the hostname that common_hal_wifi_init() sets when +// CIRCUITPY_WIFI_HOSTNAME is not in settings.toml. Can be called before the +// radio is initialized. Writes the hostname to buf as a NUL-terminated string, +// truncated to fit in buf_size bytes. Never allocates or raises. +void wifi_get_default_hostname(char *buf, size_t buf_size); diff --git a/shared-module/os/__init__.c b/shared-module/os/__init__.c index 3a74fe4a7b0..ba62552b667 100644 --- a/shared-module/os/__init__.c +++ b/shared-module/os/__init__.c @@ -21,6 +21,12 @@ #include "supervisor/shared/settings.h" #endif +#if CIRCUITPY_WIFI +#include "shared-bindings/wifi/__init__.h" +#elif CIRCUITPY_MII +#include "shared-bindings/mii/__init__.h" +#endif + // This provides all VFS related OS functions so that ports can share the code // as needed. @@ -306,3 +312,40 @@ mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_) { #endif MP_REGISTER_ROOT_POINTER(const char *cwd_path); + +#if CIRCUITPY_WIFI || CIRCUITPY_MII +void common_hal_os_get_nodename(char *buf, size_t buf_size) { + if (buf_size == 0) { + return; + } + buf[0] = '\0'; + + #if CIRCUITPY_WIFI + if (wifi_hostname_is_set()) { + strlcpy(buf, wifi_get_hostname_raw(), buf_size); + } else { + // Report the hostname common_hal_wifi_init() will set. + #if CIRCUITPY_SETTINGS_TOML + if (settings_get_str("CIRCUITPY_WIFI_HOSTNAME", buf, buf_size) != SETTINGS_OK) { + buf[0] = '\0'; + } + #endif + if (buf[0] == '\0') { + wifi_get_default_hostname(buf, buf_size); + } + } + #elif CIRCUITPY_MII + const char *hostname = mii_get_hostname_raw(); + if (hostname[0] != '\0') { + strlcpy(buf, hostname, buf_size); + } else { + // Report the hostname the Ethernet constructor will set. + mii_get_default_hostname(buf, buf_size); + } + #endif + + if (buf[0] == '\0') { + strlcpy(buf, MICROPY_HW_MCU_NAME, buf_size); + } +} +#endif diff --git a/supervisor/shared/network.c b/supervisor/shared/network.c new file mode 100644 index 00000000000..9a97aae3def --- /dev/null +++ b/supervisor/shared/network.c @@ -0,0 +1,30 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Dan Halbert for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include +#include + +#include "py/misc.h" +#include "supervisor/shared/network.h" + +// Bytes used by everything except the board part: "cpy-", "-", 12 hex digits, +// and the NUL. +#define DEFAULT_HOSTNAME_OVERHEAD (4 + 1 + 12 + 1) + +size_t network_format_default_hostname(char *buf, size_t buf_size, const uint8_t mac[6]) { + const size_t board_id_len = strlen(CIRCUITPY_BOARD_ID); + size_t board_len = MIN(buf_size - DEFAULT_HOSTNAME_OVERHEAD, board_id_len); + size_t board_trim = board_id_len - board_len; + // If trimming leaves a leading "_", drop it too, so the name doesn't start + // with "cpy-_". + if (CIRCUITPY_BOARD_ID[board_trim] == '_') { + board_trim++; + } + int len = snprintf(buf, buf_size, "cpy-%s-%02x%02x%02x%02x%02x%02x", + CIRCUITPY_BOARD_ID + board_trim, + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return len < 0 ? 0 : (size_t)len; +} diff --git a/supervisor/shared/network.h b/supervisor/shared/network.h new file mode 100644 index 00000000000..17f8607359b --- /dev/null +++ b/supervisor/shared/network.h @@ -0,0 +1,23 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Dan Halbert for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +// Formats the default network hostname, "cpy--", into buf. +// +// is CIRCUITPY_BOARD_ID, with its start trimmed off so the result fits +// in buf_size bytes, including the terminating NUL. The start is usually the +// manufacturer, so the end is more likely to distinguish the board. is +// the six bytes of mac as lowercase hex. +// +// buf_size must be at least 18, the length of "cpy--", 12 hex digits, and the +// NUL. Always writes a NUL-terminated string to buf. Never allocates or raises. +// +// Returns the length of the string, not counting the NUL. +size_t network_format_default_hostname(char *buf, size_t buf_size, const uint8_t mac[6]); diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk index 67b2e8224ae..41939bed19b 100644 --- a/supervisor/supervisor.mk +++ b/supervisor/supervisor.mk @@ -32,6 +32,11 @@ ifeq ($(CIRCUITPY_ASYNC_SPI),1) SRC_SUPERVISOR += supervisor/shared/awaitable.c endif +# Networking code shared by wifi and mii. +ifneq ($(filter 1,$(CIRCUITPY_WIFI) $(CIRCUITPY_MII)),) +SRC_SUPERVISOR += supervisor/shared/network.c +endif + # For tlsf CFLAGS += -D_DEBUG=0 From 033fce7a329661ca8f452de00260267cb7db719f Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 18 Nov 2026 14:38:17 -0500 Subject: [PATCH 2/4] Rename wifi_hostname_is_set() to wifi_is_initialized() It reports whether common_hal_wifi_init() has run, which is more general than whether the hostname is set. Co-Authored-By: Claude Opus 5.5 --- ports/espressif/common-hal/wifi/__init__.c | 2 +- ports/raspberrypi/common-hal/wifi/__init__.c | 2 +- ports/zephyr-cp/common-hal/wifi/__init__.c | 2 +- shared-bindings/wifi/__init__.h | 7 ++++--- shared-module/os/__init__.c | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index d2f8faa9766..8eea964b017 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -601,7 +601,7 @@ void common_hal_wifi_init(bool user_initiated) { common_hal_wifi_radio_set_enabled(self, true); } -bool wifi_hostname_is_set(void) { +bool wifi_is_initialized(void) { return wifi_inited; } diff --git a/ports/raspberrypi/common-hal/wifi/__init__.c b/ports/raspberrypi/common-hal/wifi/__init__.c index 606cc8480fb..9c475886035 100644 --- a/ports/raspberrypi/common-hal/wifi/__init__.c +++ b/ports/raspberrypi/common-hal/wifi/__init__.c @@ -66,7 +66,7 @@ void common_hal_wifi_init(bool user_initiated) { common_hal_wifi_radio_set_enabled(self, true); } -bool wifi_hostname_is_set(void) { +bool wifi_is_initialized(void) { return wifi_inited; } diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.c b/ports/zephyr-cp/common-hal/wifi/__init__.c index 071a4655d17..f878f708111 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.c +++ b/ports/zephyr-cp/common-hal/wifi/__init__.c @@ -353,7 +353,7 @@ void common_hal_wifi_init(bool user_initiated) { LOG_DBG("common_hal_wifi_init done"); } -bool wifi_hostname_is_set(void) { +bool wifi_is_initialized(void) { return wifi_inited; } diff --git a/shared-bindings/wifi/__init__.h b/shared-bindings/wifi/__init__.h index 9c9166dae44..8bad737b25e 100644 --- a/shared-bindings/wifi/__init__.h +++ b/shared-bindings/wifi/__init__.h @@ -22,9 +22,10 @@ void wifi_user_reset(void); // supervisor/shared/web_workflow. const char *wifi_get_hostname_raw(void); -// True once common_hal_wifi_init() has set the interface's hostname, so that -// wifi_get_hostname_raw() returns it. Never allocates or raises. -bool wifi_hostname_is_set(void); +// True once common_hal_wifi_init() has run, until wifi_reset() undoes it. Some +// ports never undo it. While true, wifi_get_hostname_raw() returns the +// interface's hostname. Never allocates or raises. +bool wifi_is_initialized(void); // Gets the hostname that common_hal_wifi_init() sets when // CIRCUITPY_WIFI_HOSTNAME is not in settings.toml. Can be called before the diff --git a/shared-module/os/__init__.c b/shared-module/os/__init__.c index ba62552b667..c62119fcf18 100644 --- a/shared-module/os/__init__.c +++ b/shared-module/os/__init__.c @@ -321,7 +321,7 @@ void common_hal_os_get_nodename(char *buf, size_t buf_size) { buf[0] = '\0'; #if CIRCUITPY_WIFI - if (wifi_hostname_is_set()) { + if (wifi_is_initialized()) { strlcpy(buf, wifi_get_hostname_raw(), buf_size); } else { // Report the hostname common_hal_wifi_init() will set. From ec1e2434f33bba428f3791a5ce7cec37b76e3406 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 18 Nov 2026 15:00:42 -0500 Subject: [PATCH 3/4] huntercat_nfc: turn off codeop to make room The de_DE build overflowed by 4 bytes after adding the "CircuitPython" os.uname().sysname string. Co-Authored-By: Claude Opus 5.5 --- ports/atmel-samd/boards/huntercat_nfc/mpconfigboard.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/atmel-samd/boards/huntercat_nfc/mpconfigboard.mk b/ports/atmel-samd/boards/huntercat_nfc/mpconfigboard.mk index 5e9a1372b45..af825c6cf39 100644 --- a/ports/atmel-samd/boards/huntercat_nfc/mpconfigboard.mk +++ b/ports/atmel-samd/boards/huntercat_nfc/mpconfigboard.mk @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21 SPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICES = "W25Q16JVxQ" LONGINT_IMPL = MPZ + +CIRCUITPY_CODEOP = 0 From 3651c684882593b74c6082b9cc23386708c6d86c Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 18 Nov 2026 16:58:52 -0500 Subject: [PATCH 4/4] os.uname().sysname is CircuitPython on zephyr-cp too The RTOS is an implementation detail. Drop the zephyr-cp override and the CIRCUITPY_OS_UNAME_SYSNAME setting, which now has only one value. Co-Authored-By: Claude Opus 5.5 --- ports/zephyr-cp/mpconfigport.h | 2 -- ports/zephyr-cp/tests/test_os.py | 4 ++-- py/circuitpy_mpconfig.h | 5 ----- shared-bindings/os/__init__.c | 4 ++-- 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/ports/zephyr-cp/mpconfigport.h b/ports/zephyr-cp/mpconfigport.h index 4f0a94bd65a..cc990c0208e 100644 --- a/ports/zephyr-cp/mpconfigport.h +++ b/ports/zephyr-cp/mpconfigport.h @@ -18,8 +18,6 @@ #endif #define MICROPY_PY_SYS_PLATFORM "Zephyr" -// Override the default name "CircuitPython", set in py/circuitpy_mpconfig.h. -#define CIRCUITPY_OS_UNAME_SYSNAME "Zephyr" // Add "(Zephyr)" to distinguish zephyr-cp builds from classic-port builds for the same board in the banner. #define MICROPY_BANNER_MACHINE MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME " (Zephyr)" diff --git a/ports/zephyr-cp/tests/test_os.py b/ports/zephyr-cp/tests/test_os.py index 37ef9d74b40..0588ee184b9 100644 --- a/ports/zephyr-cp/tests/test_os.py +++ b/ports/zephyr-cp/tests/test_os.py @@ -19,11 +19,11 @@ @pytest.mark.circuitpy_drive({"code.py": UNAME_CODE}) def test_os_uname(circuitpython): - """sysname is "Zephyr". With no Wi-Fi or Ethernet, nodename is the microcontroller name.""" + """sysname is "CircuitPython". With no Wi-Fi or Ethernet, nodename is the microcontroller name.""" circuitpython.wait_until_done() output = circuitpython.serial.all_output - assert "sysname: Zephyr" in output + assert "sysname: CircuitPython" in output nodename = output.split("nodename: ", 1)[1].split("\n", 1)[0].strip() assert nodename # machine is " with ". diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 126f5326a29..82f528d264a 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -576,11 +576,6 @@ void background_callback_run_all(void); #define CIRCUITPY_VERBOSE_BLE 0 #endif -// Value of os.uname().sysname. -#ifndef CIRCUITPY_OS_UNAME_SYSNAME -#define CIRCUITPY_OS_UNAME_SYSNAME "CircuitPython" -#endif - // Display the Blinka logo in the REPL on displayio displays. #ifndef CIRCUITPY_REPL_LOGO #define CIRCUITPY_REPL_LOGO (1) diff --git a/shared-bindings/os/__init__.c b/shared-bindings/os/__init__.c index 73a3a3c80c4..0673ee9f698 100644 --- a/shared-bindings/os/__init__.c +++ b/shared-bindings/os/__init__.c @@ -38,7 +38,7 @@ //| """The type of values that :py:func:`.uname()` returns""" //| //| sysname: str -//| """``"CircuitPython"``, or ``"Zephyr"`` on the Zephyr port.""" +//| """Always ``"CircuitPython"``.""" //| nodename: str //| """The network hostname, read each time :py:func:`.uname()` is called. //| Before Wi-Fi or Ethernet is set up, this is the hostname that the interface will use. @@ -59,7 +59,7 @@ static const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine }; -static const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, CIRCUITPY_OS_UNAME_SYSNAME); +static const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "CircuitPython"); static const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING); static const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE); static const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);