From 154a41bef15e48ea9f2fcedd9a81596af53bc605 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 4 Jul 2022 22:24:40 +0200 Subject: [PATCH 1/7] manage hostname with sdk string --- cores/esp8266/LwipIntf.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/LwipIntf.cpp b/cores/esp8266/LwipIntf.cpp index 6b84aab16d..3670a723da 100644 --- a/cores/esp8266/LwipIntf.cpp +++ b/cores/esp8266/LwipIntf.cpp @@ -16,6 +16,9 @@ extern "C" #include "debug.h" #include "LwipIntf.h" +extern "C" char* wifi_station_hostname; // sdk's hostname location +extern "C" char* wifi_station_default_hostname; // sdk's hostname location + // args | esp order arduino order // ---- + --------- ------------- // local_ip | local_ip local_ip @@ -66,7 +69,7 @@ bool LwipIntf::ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1 */ String LwipIntf::hostname(void) { - return wifi_station_get_hostname(); + return wifi_station_hostname; //wifi_station_get_hostname(); } /** @@ -75,7 +78,7 @@ String LwipIntf::hostname(void) */ const char* LwipIntf::getHostname(void) { - return wifi_station_get_hostname(); + return wifi_station_hostname; //wifi_station_get_hostname(); } /** @@ -136,20 +139,26 @@ bool LwipIntf::hostname(const char* aHostname) DEBUGV("hostname '%s' is not compliant with RFC952\n", aHostname); } +#if 0 bool ret = wifi_station_set_hostname(aHostname); if (!ret) { DEBUGV("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); return false; } +#else + bool ret = true; + strcpy(wifi_station_hostname, aHostname); + strcpy(wifi_station_default_hostname, aHostname); +#endif // now we should inform dhcp server for this change, using lwip_renew() // looping through all existing interface // harmless for AP, also compatible with ethernet adapters (to come) 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; //wifi_station_get_hostname(); if (netif_dhcp_data(intf) != nullptr) { From 9b4627829f6e286bd49ad0bfeeef770e264cde57 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 4 Jul 2022 23:41:34 +0200 Subject: [PATCH 2/7] comments --- cores/esp8266/LwipIntf.cpp | 29 +++++++++---------- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 9 ++++++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cores/esp8266/LwipIntf.cpp b/cores/esp8266/LwipIntf.cpp index 3670a723da..84e1969e95 100644 --- a/cores/esp8266/LwipIntf.cpp +++ b/cores/esp8266/LwipIntf.cpp @@ -16,8 +16,16 @@ extern "C" #include "debug.h" #include "LwipIntf.h" -extern "C" char* wifi_station_hostname; // sdk's hostname location -extern "C" char* wifi_station_default_hostname; // sdk's hostname location +// 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 // ---- + --------- ------------- @@ -69,7 +77,7 @@ bool LwipIntf::ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1 */ String LwipIntf::hostname(void) { - return wifi_station_hostname; //wifi_station_get_hostname(); + return wifi_station_hostname; } /** @@ -78,7 +86,7 @@ String LwipIntf::hostname(void) */ const char* LwipIntf::getHostname(void) { - return wifi_station_hostname; //wifi_station_get_hostname(); + return wifi_station_hostname; } /** @@ -139,26 +147,17 @@ bool LwipIntf::hostname(const char* aHostname) DEBUGV("hostname '%s' is not compliant with RFC952\n", aHostname); } -#if 0 - bool ret = wifi_station_set_hostname(aHostname); - if (!ret) - { - DEBUGV("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); - return false; - } -#else bool ret = true; strcpy(wifi_station_hostname, aHostname); - strcpy(wifi_station_default_hostname, aHostname); -#endif + // now we should inform dhcp server for this change, using lwip_renew() // looping through all existing interface // harmless for AP, also compatible with ethernet adapters (to come) for (netif* intf = netif_list; intf; intf = intf->next) { // unconditionally update all known interfaces - intf->hostname = wifi_station_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; } From 352382d070ffed0172266d65db9812146e649154 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jul 2022 14:17:37 +0200 Subject: [PATCH 3/7] fix emulation on host --- tests/host/common/MockEsp.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/host/common/MockEsp.cpp b/tests/host/common/MockEsp.cpp index 18e7be6c83..b48c644a42 100644 --- a/tests/host/common/MockEsp.cpp +++ b/tests/host/common/MockEsp.cpp @@ -39,6 +39,8 @@ #include struct rst_info resetInfo; +char wifi_station_hostname [33]; // exists in nonosdk + unsigned long long operator"" _kHz(unsigned long long x) { return x * 1000; From e28c40478e870562e1a36f71c9cb3eeb6b11a555 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jul 2022 21:22:26 +0200 Subject: [PATCH 4/7] remove duplicate hostname per review --- tests/host/common/MockEsp.cpp | 2 -- tests/host/common/user_interface.cpp | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/host/common/MockEsp.cpp b/tests/host/common/MockEsp.cpp index b48c644a42..18e7be6c83 100644 --- a/tests/host/common/MockEsp.cpp +++ b/tests/host/common/MockEsp.cpp @@ -39,8 +39,6 @@ #include struct rst_info resetInfo; -char wifi_station_hostname [33]; // exists in nonosdk - unsigned long long operator"" _kHz(unsigned long long x) { return x * 1000; diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index c35a7cc236..1ce91fa78e 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -309,10 +309,10 @@ extern "C" return wifi_station_get_config(config); } - char wifi_station_get_hostname_str[128]; + extern "C" char wifi_station_hostname [33]; // exists in nonosdk const char* wifi_station_get_hostname(void) { - return strcpy(wifi_station_get_hostname_str, "esposix"); + return strcpy(wifi_station_hostname, "esposix"); } bool wifi_station_get_reconnect_policy() From a5471e8837a81991f8948b8d09234d0e0010b71b Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jul 2022 21:23:29 +0200 Subject: [PATCH 5/7] restyle --- tests/host/common/user_interface.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 1ce91fa78e..3cd65bb016 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -309,8 +309,8 @@ extern "C" return wifi_station_get_config(config); } - extern "C" char wifi_station_hostname [33]; // exists in nonosdk - const char* wifi_station_get_hostname(void) + extern "C" char wifi_station_hostname[33]; // exists in nonosdk + const char* wifi_station_get_hostname(void) { return strcpy(wifi_station_hostname, "esposix"); } From f9b2ab14bbb0093e8918d65d6b3e885d7e4389ba Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jul 2022 21:43:46 +0200 Subject: [PATCH 6/7] wifi_station_hostname is a pointer, not an array --- tests/host/common/user_interface.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 3cd65bb016..c03196e244 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -309,8 +309,10 @@ extern "C" return wifi_station_get_config(config); } - extern "C" char wifi_station_hostname[33]; // exists in nonosdk - const char* wifi_station_get_hostname(void) + extern "C" char* wifi_station_hostname; // exists in nonosdk + char wifi_station_hostname_str[33]; + char* wifi_station_hostname = wifi_station_hostname_str; + const char* wifi_station_get_hostname(void) { return strcpy(wifi_station_hostname, "esposix"); } From f417a230e8f3259562095b137a4e9dd2f6f45462 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jul 2022 22:04:40 +0200 Subject: [PATCH 7/7] simplify --- tests/host/common/user_interface.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index c03196e244..80c98b7a80 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -310,11 +310,11 @@ extern "C" } extern "C" char* wifi_station_hostname; // exists in nonosdk - char wifi_station_hostname_str[33]; + 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_hostname, "esposix"); + return wifi_station_hostname; } bool wifi_station_get_reconnect_policy()