Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/huntercat_nfc/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "W25Q16JVxQ"
LONGINT_IMPL = MPZ

CIRCUITPY_CODEOP = 0
17 changes: 12 additions & 5 deletions ports/espressif/common-hal/mii/Ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 32 additions & 15 deletions ports/espressif/common-hal/wifi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -610,6 +601,32 @@ void common_hal_wifi_init(bool user_initiated) {
common_hal_wifi_radio_set_enabled(self, true);
}

bool wifi_is_initialized(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();
Expand Down
14 changes: 14 additions & 0 deletions ports/raspberrypi/common-hal/wifi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
#include "shared-bindings/wifi/Monitor.h"
#include "shared-bindings/wifi/Radio.h"

#include <string.h>

#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
Expand Down Expand Up @@ -62,6 +66,16 @@ void common_hal_wifi_init(bool user_initiated) {
common_hal_wifi_radio_set_enabled(self, true);
}

bool wifi_is_initialized(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();
Expand Down
42 changes: 29 additions & 13 deletions ports/zephyr-cp/common-hal/mii/Ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -354,21 +355,12 @@ void common_hal_mii_ethernet_construct(mii_ethernet_obj_t *self,
net_mgmt_add_event_callback(&eth_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

Expand Down Expand Up @@ -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.
Expand Down
47 changes: 33 additions & 14 deletions ports/zephyr-cp/common-hal/wifi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -362,6 +353,34 @@ void common_hal_wifi_init(bool user_initiated) {
LOG_DBG("common_hal_wifi_init done");
}

bool wifi_is_initialized(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();
Expand Down
31 changes: 31 additions & 0 deletions ports/zephyr-cp/tests/test_os.py
Original file line number Diff line number Diff line change
@@ -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 "CircuitPython". With no Wi-Fi or Ethernet, nodename is the microcontroller name."""
circuitpython.wait_until_done()

output = circuitpython.serial.all_output
assert "sysname: CircuitPython" in output
nodename = output.split("nodename: ", 1)[1].split("\n", 1)[0].strip()
assert nodename
# machine is "<board name> with <microcontroller name>".
assert f" with {nodename}" in output
assert "done" in output
7 changes: 7 additions & 0 deletions shared-bindings/mii/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
34 changes: 32 additions & 2 deletions shared-bindings/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,51 @@
//| """The type of values that :py:func:`.uname()` returns"""
//|
//| sysname: str
//| """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.
//| 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, "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);

#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,
Expand All @@ -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:
Expand Down
Loading
Loading