Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix hostname management #8626

Merged
merged 7 commits into from
Jul 6, 2022
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
26 changes: 17 additions & 9 deletions cores/esp8266/LwipIntf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -75,7 +86,7 @@ String LwipIntf::hostname(void)
*/
const char* LwipIntf::getHostname(void)
{
return wifi_station_get_hostname();
return wifi_station_hostname;
}

/**
Expand Down Expand Up @@ -136,20 +147,17 @@ 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
// 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;

if (netif_dhcp_data(intf) != nullptr)
{
Expand Down
9 changes: 9 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 5 additions & 3 deletions tests/host/common/user_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down