diff --git a/cores/esp8266/LwipIntf.cpp b/cores/esp8266/LwipIntf.cpp index 6b84aab16d..84e1969e95 100644 --- a/cores/esp8266/LwipIntf.cpp +++ b/cores/esp8266/LwipIntf.cpp @@ -16,6 +16,17 @@ extern "C" #include "debug.h" #include "LwipIntf.h" +// wifi_station_hostname is SDK's station(=global) hostname location +// - It is never nullptr but wifi_station_get_hostname() +// can return nullptr when STA is down +// - Because WiFi is started in off mode at boot time, +// wifi_station_set/get_hostname() is now no more used +// because setting hostname firt does not work anymore +// - wifi_station_hostname is overwritten by SDK when wifi is +// woken up in WiFi::mode() +// +extern "C" char* wifi_station_hostname; + // args | esp order arduino order // ---- + --------- ------------- // local_ip | local_ip local_ip @@ -66,7 +77,7 @@ bool LwipIntf::ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1 */ String LwipIntf::hostname(void) { - return wifi_station_get_hostname(); + return wifi_station_hostname; } /** @@ -75,7 +86,7 @@ String LwipIntf::hostname(void) */ const char* LwipIntf::getHostname(void) { - return wifi_station_get_hostname(); + return wifi_station_hostname; } /** @@ -136,12 +147,9 @@ bool LwipIntf::hostname(const char* aHostname) DEBUGV("hostname '%s' is not compliant with RFC952\n", aHostname); } - bool ret = wifi_station_set_hostname(aHostname); - if (!ret) - { - DEBUGV("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); - return false; - } + bool ret = true; + + strcpy(wifi_station_hostname, aHostname); // now we should inform dhcp server for this change, using lwip_renew() // looping through all existing interface @@ -149,7 +157,7 @@ bool LwipIntf::hostname(const char* aHostname) for (netif* intf = netif_list; intf; intf = intf->next) { // unconditionally update all known interfaces - intf->hostname = wifi_station_get_hostname(); + intf->hostname = wifi_station_hostname; if (netif_dhcp_data(intf) != nullptr) { diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index d45627e9cf..fe4161ee38 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -49,6 +49,9 @@ extern "C" { #include "debug.h" #include "include/WiFiState.h" +// see comments on wifi_station_hostname in LwipIntf.cpp +extern "C" char* wifi_station_hostname; // sdk's hostname location + // ----------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- @@ -419,7 +422,10 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { return true; } + char backup_hostname [33] { 0 }; // hostname is 32 chars long (RFC) + if (m != WIFI_OFF && wifi_fpm_get_sleep_type() != NONE_SLEEP_T) { + memcpy(backup_hostname, wifi_station_hostname, sizeof(backup_hostname)); // wifi starts asleep by default wifi_fpm_do_wakeup(); wifi_fpm_close(); @@ -452,6 +458,9 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { } } + if (backup_hostname[0]) + memcpy(wifi_station_hostname, backup_hostname, sizeof(backup_hostname)); + return ret; } diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index c35a7cc236..80c98b7a80 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -309,10 +309,12 @@ extern "C" return wifi_station_get_config(config); } - char wifi_station_get_hostname_str[128]; - const char* wifi_station_get_hostname(void) + extern "C" char* wifi_station_hostname; // exists in nonosdk + char wifi_station_hostname_str[33] { "esposix" }; + char* wifi_station_hostname = wifi_station_hostname_str; + const char* wifi_station_get_hostname(void) { - return strcpy(wifi_station_get_hostname_str, "esposix"); + return wifi_station_hostname; } bool wifi_station_get_reconnect_policy()