diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index c4ff453013..22874808fb 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -277,6 +277,10 @@ esp_err_t esp_netif_get_mac(esp_netif_t *esp_netif, uint8_t mac[]); /** * @brief Set the hostname of an interface * + * The configured hostname overrides the default configuration value CONFIG_LWIP_LOCAL_HOSTNAME. + * Please note that when the hostname is altered after interface started/connected the changes + * would only be reflected once the interface restarts/reconnects + * * @param[in] esp_netif Handle to esp-netif instance * @param[in] hostname New hostname for the interface. Maximum length 32 bytes. * diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index f14a2583bb..fbbf22697f 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -1048,11 +1048,11 @@ esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname) #if LWIP_NETIF_HOSTNAME struct netif *p_netif = esp_netif->lwip_netif; - if (p_netif != NULL) { + if (p_netif != NULL && p_netif->hostname != NULL) { *hostname = p_netif->hostname; return ESP_OK; } else { - return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + return ESP_ERR_ESP_NETIF_IF_NOT_READY; } #else return ESP_ERR_ESP_NETIF_IF_NOT_READY; diff --git a/components/esp_netif/test/test_esp_netif.c b/components/esp_netif/test/test_esp_netif.c index be1c99798c..a3d8447099 100644 --- a/components/esp_netif/test/test_esp_netif.c +++ b/components/esp_netif/test/test_esp_netif.c @@ -225,3 +225,31 @@ TEST_CASE("esp_netif: create custom wifi interfaces", "[esp_netif][leaks=0]") esp_netif_destroy(sta); } + +TEST_CASE("esp_netif: get/set hostname", "[esp_netif]") +{ + const char *hostname; + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + + test_case_uses_tcpip(); + esp_netif_t *esp_netif = esp_netif_new(&cfg); + + // specific hostname not set yet, get_hostname should fail + TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname)); + + TEST_ASSERT_NOT_NULL(esp_netif); + esp_netif_attach_wifi_station(esp_netif); + + esp_netif_action_start(esp_netif, NULL, 0, NULL); + + // specific hostname not set yet, but if started, get_hostname to return default config value + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname)); + TEST_ASSERT_EQUAL_STRING(hostname, CONFIG_LWIP_LOCAL_HOSTNAME); + + // specific hostname set and get + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_set_hostname(esp_netif, "new_name")); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname)); + TEST_ASSERT_EQUAL_STRING(hostname, "new_name"); + + esp_netif_destroy(esp_netif); +} diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index ff2909d942..89f16e410e 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -4,7 +4,8 @@ menu "LWIP" string "Local netif hostname" default 'espressif' help - The name this device will report to other devices on the network + The default name this device will report to other devices on the network. + Could be updated at runtime with esp_netif_set_hostname() config LWIP_DNS_SUPPORT_MDNS_QUERIES bool "Enable mDNS queries in resolving host name" diff --git a/components/lwip/port/esp32/netif/ethernetif.c b/components/lwip/port/esp32/netif/ethernetif.c index d513e181b6..7e1b7dbd78 100644 --- a/components/lwip/port/esp32/netif/ethernetif.c +++ b/components/lwip/port/esp32/netif/ethernetif.c @@ -196,11 +196,14 @@ err_t ethernetif_init(struct netif *netif) { LWIP_ASSERT("netif != NULL", (netif != NULL)); /* Have to get the esp-netif handle from netif first and then driver==ethernet handle from there */ - esp_eth_handle_t eth_handle = esp_netif_get_io_driver(esp_netif_get_handle_from_netif_impl(netif)); + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); + esp_eth_handle_t eth_handle = esp_netif_get_io_driver(esp_netif); /* Initialize interface hostname */ #if LWIP_NETIF_HOSTNAME #if ESP_LWIP - netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME; + if (esp_netif_get_hostname(esp_netif, &netif->hostname) != ESP_OK) { + netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME; + } #else netif->hostname = "lwip"; #endif diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index 88f137fc3a..9a27d1c335 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -219,7 +219,9 @@ wlanif_init(struct netif *netif) /* Initialize interface hostname */ #if ESP_LWIP - netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME; + if (esp_netif_get_hostname(esp_netif_get_handle_from_netif_impl(netif), &netif->hostname) != ESP_OK) { + netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME; + } #else netif->hostname = "lwip"; #endif