Skip to content

Commit 67cfde7

Browse files
committed
Restore legacy behaviour for fallback hostname
1 parent 0222a41 commit 67cfde7

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

wled00/cfg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
5757
#endif
5858

5959
JsonObject id = doc["id"];
60+
getStringFromJson(serverDescription, id["name"], sizeof(serverDescription));
6061
// legacy behaviour
6162
getStringFromJson(hostName, id[F("mdns")], sizeof(hostName));
6263
if (strlen(hostName) == 0) {
6364
mDNSenabled = false; // if no host name is set, disable mDNS
64-
sprintf_P(hostName, PSTR("wled-%.*s"), 6, escapedMac.c_str() + 6);
65+
prepareHostname(hostName, sizeof(hostName)-1);
6566
}
6667

67-
getStringFromJson(serverDescription, id["name"], sizeof(serverDescription));
6868
#ifndef WLED_DISABLE_ALEXA
6969
getStringFromJson(alexaInvocationName, id[F("inv")], sizeof(alexaInvocationName));
7070
#endif

wled00/fcn_declare.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ size_t printSetFormValue(Print& settingsScript, const char* key, int val);
516516
size_t printSetFormValue(Print& settingsScript, const char* key, const char* val);
517517
size_t printSetFormIndex(Print& settingsScript, const char* key, int index);
518518
size_t printSetClassElementHTML(Print& settingsScript, const char* key, const int index, const char* val);
519+
void prepareHostname(char* hostname, size_t maxLen = 32);
519520
[[gnu::pure]] bool isAsterisksOnly(const char* str, byte maxLen);
520521
bool requestJSONBufferLock(uint8_t moduleID=255);
521522
void releaseJSONBufferLock();

wled00/set.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
6464
}
6565

6666
strlcpy(hostName, request->arg(F("CM")).c_str(), sizeof(hostName));
67-
if (strlen(hostName) == 0) sprintf_P(hostName, PSTR("wled-%.*s"), 6, escapedMac.c_str() + 6); // hostname must not be empty
67+
if (strlen(hostName) == 0) prepareHostname(hostName, sizeof(hostName)-1);
6868
#ifdef ARDUINO_ARCH_ESP32
6969
#ifdef WLED_USE_ETHERNET
7070
ETH.setHostname(hostName);

wled00/util.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ size_t printSetClassElementHTML(Print& settingsScript, const char* key, const in
123123
return settingsScript.printf_P(PSTR("d.getElementsByClassName(\"%s\")[%d].innerHTML=\"%s\";"), key, index, val);
124124
}
125125

126+
// prepare a unique hostname based on the last 6 digits of the MAC address
127+
// if no mDNS name or serverDescription is set, otherwise use cmDNS or serverDescription
128+
// the hostname will be at most 24 characters long, starting with "wled-"
129+
// and containing only alphanumeric characters and hyphens
130+
// the hostname will not end with a hyphen and will be null-terminated
131+
void prepareHostname(char* hostname, size_t maxLen)
132+
{
133+
// create a unique hostname based on the last 6 digits of the MAC address if no serverDescription is set
134+
sprintf_P(hostname, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
135+
const char *pC = serverDescription; // use serverDescription, this method is not called if hostName is set
136+
unsigned pos = 5; // keep "wled-" from unique name
137+
while (*pC && pos < maxLen) { // while !null and not over length
138+
if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname
139+
hostname[pos++] = *pC;
140+
} else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') {
141+
hostname[pos++] = '-';
142+
}
143+
// else do nothing - no leading hyphens and do not include hyphens for all other characters.
144+
pC++;
145+
}
146+
// last character must not be hyphen
147+
while (pos > 4 && hostname[pos-1] == '-') pos--;
148+
hostname[pos] = '\0'; // terminate string (leave at least "wled")
149+
}
126150

127151
bool isAsterisksOnly(const char* str, byte maxLen)
128152
{

wled00/wled.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ void WLED::setup()
426426
escapedMac.toLowerCase();
427427

428428
// generate host name if no compile time default is set
429-
if (strcmp(hostName, DEFAULT_MDNS_NAME) == 0) sprintf_P(hostName, PSTR("wled-%.*s"), 6, escapedMac.c_str() + 6);
429+
if (strcmp(hostName, DEFAULT_MDNS_NAME) == 0) prepareHostname(hostName, sizeof(hostName)-1);
430430
WLED_SET_AP_SSID(); // otherwise it is empty on first boot until config is saved
431431
multiWiFi.push_back(WiFiConfig(CLIENT_SSID,CLIENT_PASS)); // initialise vector with default WiFi
432432

0 commit comments

Comments
 (0)