Fixed hostname generation and hostname assignment

This commit is contained in:
Thomas Basler 2022-09-06 18:44:26 +02:00
parent 185fea987d
commit f5328100b6
2 changed files with 73 additions and 31 deletions

View File

@ -50,7 +50,7 @@ public:
IPAddress gatewayIP(); IPAddress gatewayIP();
IPAddress dnsIP(uint8_t dns_no = 0); IPAddress dnsIP(uint8_t dns_no = 0);
String macAddress(); String macAddress();
const char* getHostname(); static String getHostname();
bool isConnected(); bool isConnected();
network_mode NetworkMode(); network_mode NetworkMode();
@ -62,6 +62,7 @@ private:
void setStaticIp(); void setStaticIp();
void setupMode(); void setupMode();
void NetworkEvent(WiFiEvent_t event); void NetworkEvent(WiFiEvent_t event);
static uint32_t getChipId();
bool adminEnabled = true; bool adminEnabled = true;
bool forceDisconnection = false; bool forceDisconnection = false;
int adminTimeoutCounter = 0; int adminTimeoutCounter = 0;

View File

@ -142,11 +142,7 @@ void NetworkSettingsClass::enableAdminMode()
String NetworkSettingsClass::getApName() String NetworkSettingsClass::getApName()
{ {
uint32_t chipId = 0; return String(ACCESS_POINT_NAME + String(getChipId()));
for (int i = 0; i < 17; i += 8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
return String(ACCESS_POINT_NAME + String(chipId));
} }
void NetworkSettingsClass::loop() void NetworkSettingsClass::loop()
@ -241,32 +237,37 @@ void NetworkSettingsClass::applyConfig()
void NetworkSettingsClass::setHostname() void NetworkSettingsClass::setHostname()
{ {
Serial.print(F("Setting Hostname... ")); Serial.print(F("Setting Hostname... "));
if (strcmp(Configuration.get().WiFi_Hostname, "")) {
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
if (WiFi.hostname(Configuration.get().WiFi_Hostname)) { if (WiFi.hostname(getHostname())) {
Serial.println(F("done")); Serial.println(F("done"));
} else { } else {
Serial.println(F("failed")); Serial.println(F("failed"));
} }
// Evil bad hack to get the hostname set up correctly
WiFi.mode(WIFI_MODE_APSTA);
WiFi.mode(WIFI_MODE_STA);
setupMode();
} }
#ifdef OPENDTU_ETHERNET #ifdef OPENDTU_ETHERNET
else if (_networkMode == network_mode::Ethernet) { else if (_networkMode == network_mode::Ethernet) {
if (ETH.setHostname(Configuration.get().WiFi_Hostname)) { if (ETH.setHostname(getHostname())) {
Serial.println(F("done")); Serial.println(F("done"));
} else { } else {
Serial.println(F("failed")); Serial.println(F("failed"));
} }
} }
#endif #endif
} else {
Serial.println(F("failed (Hostname empty)"));
}
} }
void NetworkSettingsClass::setStaticIp() void NetworkSettingsClass::setStaticIp()
{ {
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
if (!Configuration.get().WiFi_Dhcp) { if (Configuration.get().WiFi_Dhcp) {
Serial.print(F("Configuring WiFi STA DHCP IP... "));
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
Serial.println(F("done"));
} else {
Serial.print(F("Configuring WiFi STA static IP... ")); Serial.print(F("Configuring WiFi STA static IP... "));
WiFi.config( WiFi.config(
IPAddress(Configuration.get().WiFi_Ip), IPAddress(Configuration.get().WiFi_Ip),
@ -280,7 +281,9 @@ void NetworkSettingsClass::setStaticIp()
#ifdef OPENDTU_ETHERNET #ifdef OPENDTU_ETHERNET
else if (_networkMode == network_mode::Ethernet) { else if (_networkMode == network_mode::Ethernet) {
if (Configuration.get().WiFi_Dhcp) { if (Configuration.get().WiFi_Dhcp) {
Serial.print(F("Configuring Ethernet DHCP IP... "));
ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE); ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
Serial.println(F("done"));
} else { } else {
Serial.print(F("Configuring Ethernet static IP... ")); Serial.print(F("Configuring Ethernet static IP... "));
ETH.config( ETH.config(
@ -375,14 +378,43 @@ String NetworkSettingsClass::macAddress()
} }
} }
const char* NetworkSettingsClass::getHostname() String NetworkSettingsClass::getHostname()
{ {
#ifdef OPENDTU_ETHERNET const CONFIG_T& config = Configuration.get();
if (_networkMode == network_mode::Ethernet) { char preparedHostname[WIFI_MAX_HOSTNAME_STRLEN + 1];
return ETH.getHostname(); char resultHostname[WIFI_MAX_HOSTNAME_STRLEN + 1];
uint8_t pos = 0;
uint32_t chipId = getChipId();
snprintf(preparedHostname, WIFI_MAX_HOSTNAME_STRLEN + 1, config.WiFi_Hostname, chipId);
const char* pC = preparedHostname;
while (*pC && pos < WIFI_MAX_HOSTNAME_STRLEN) { // while !null and not over length
if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname
resultHostname[pos] = *pC;
pos++;
} else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') {
resultHostname[pos] = '-';
pos++;
} }
#endif // else do nothing - no leading hyphens and do not include hyphens for all other characters.
return WiFi.getHostname(); pC++;
}
resultHostname[pos] = '\0'; // terminate string
// last character must not be hyphen
while (pos > 0 && resultHostname[pos - 1] == '-') {
resultHostname[pos - 1] = '\0';
pos--;
}
// Fallback if no other rule applied
if (strlen(resultHostname) == 0) {
snprintf(resultHostname, WIFI_MAX_HOSTNAME_STRLEN + 1, APP_HOSTNAME, chipId);
}
return resultHostname;
} }
bool NetworkSettingsClass::isConnected() bool NetworkSettingsClass::isConnected()
@ -399,4 +431,13 @@ network_mode NetworkSettingsClass::NetworkMode()
return _networkMode; return _networkMode;
} }
uint32_t NetworkSettingsClass::getChipId()
{
uint32_t chipId = 0;
for (int i = 0; i < 17; i += 8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
return chipId;
}
NetworkSettingsClass NetworkSettings; NetworkSettingsClass NetworkSettings;