Skip to content

Commit 801d889

Browse files
d-a-vhasenradball
authored andcommitted
Fix hostname management (esp8266#8626)
* manage hostname with sdk string
1 parent da5f944 commit 801d889

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

cores/esp8266/LwipIntf.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ extern "C"
1616
#include "debug.h"
1717
#include "LwipIntf.h"
1818

19+
// wifi_station_hostname is SDK's station(=global) hostname location
20+
// - It is never nullptr but wifi_station_get_hostname()
21+
// can return nullptr when STA is down
22+
// - Because WiFi is started in off mode at boot time,
23+
// wifi_station_set/get_hostname() is now no more used
24+
// because setting hostname firt does not work anymore
25+
// - wifi_station_hostname is overwritten by SDK when wifi is
26+
// woken up in WiFi::mode()
27+
//
28+
extern "C" char* wifi_station_hostname;
29+
1930
// args | esp order arduino order
2031
// ---- + --------- -------------
2132
// local_ip | local_ip local_ip
@@ -66,7 +77,7 @@ bool LwipIntf::ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1
6677
*/
6778
String LwipIntf::hostname(void)
6879
{
69-
return wifi_station_get_hostname();
80+
return wifi_station_hostname;
7081
}
7182

7283
/**
@@ -75,7 +86,7 @@ String LwipIntf::hostname(void)
7586
*/
7687
const char* LwipIntf::getHostname(void)
7788
{
78-
return wifi_station_get_hostname();
89+
return wifi_station_hostname;
7990
}
8091

8192
/**
@@ -136,20 +147,17 @@ bool LwipIntf::hostname(const char* aHostname)
136147
DEBUGV("hostname '%s' is not compliant with RFC952\n", aHostname);
137148
}
138149

139-
bool ret = wifi_station_set_hostname(aHostname);
140-
if (!ret)
141-
{
142-
DEBUGV("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
143-
return false;
144-
}
150+
bool ret = true;
151+
152+
strcpy(wifi_station_hostname, aHostname);
145153

146154
// now we should inform dhcp server for this change, using lwip_renew()
147155
// looping through all existing interface
148156
// harmless for AP, also compatible with ethernet adapters (to come)
149157
for (netif* intf = netif_list; intf; intf = intf->next)
150158
{
151159
// unconditionally update all known interfaces
152-
intf->hostname = wifi_station_get_hostname();
160+
intf->hostname = wifi_station_hostname;
153161

154162
if (netif_dhcp_data(intf) != nullptr)
155163
{

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ extern "C" {
4949
#include "debug.h"
5050
#include "include/WiFiState.h"
5151

52+
// see comments on wifi_station_hostname in LwipIntf.cpp
53+
extern "C" char* wifi_station_hostname; // sdk's hostname location
54+
5255
// -----------------------------------------------------------------------------------------------------------------------
5356
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
5457
// -----------------------------------------------------------------------------------------------------------------------
@@ -419,7 +422,10 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) {
419422
return true;
420423
}
421424

425+
char backup_hostname [33] { 0 }; // hostname is 32 chars long (RFC)
426+
422427
if (m != WIFI_OFF && wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
428+
memcpy(backup_hostname, wifi_station_hostname, sizeof(backup_hostname));
423429
// wifi starts asleep by default
424430
wifi_fpm_do_wakeup();
425431
wifi_fpm_close();
@@ -452,6 +458,9 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) {
452458
}
453459
}
454460

461+
if (backup_hostname[0])
462+
memcpy(wifi_station_hostname, backup_hostname, sizeof(backup_hostname));
463+
455464
return ret;
456465
}
457466

tests/host/common/user_interface.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,12 @@ extern "C"
309309
return wifi_station_get_config(config);
310310
}
311311

312-
char wifi_station_get_hostname_str[128];
313-
const char* wifi_station_get_hostname(void)
312+
extern "C" char* wifi_station_hostname; // exists in nonosdk
313+
char wifi_station_hostname_str[33] { "esposix" };
314+
char* wifi_station_hostname = wifi_station_hostname_str;
315+
const char* wifi_station_get_hostname(void)
314316
{
315-
return strcpy(wifi_station_get_hostname_str, "esposix");
317+
return wifi_station_hostname;
316318
}
317319

318320
bool wifi_station_get_reconnect_policy()

0 commit comments

Comments
 (0)