@@ -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
127151bool isAsterisksOnly (const char * str, byte maxLen)
128152{
0 commit comments