From ec6a243e3e8ec793c23f845559c41ba0378fb1c2 Mon Sep 17 00:00:00 2001 From: Aircoookie Date: Sun, 19 Jul 2020 16:24:26 +0200 Subject: [PATCH] Hostname alphanumeric (#1048) * Use string derived from serverDescription for wifi.hostname() The code was sending illegal hostname strings to WiFi.hostname() (which is then submitted to DHCP and often times to DNS.) A valid hostname contains only alphanumeric characters and hyphens (though it can't start with a hypen.) This change simply alters the value passed to wifi.hostname() by replacing all non alphanum chars with hyphens while ensuring the first char is never a hyphen. If the resulting hostname is empty, it uses the escapedMac value (which I'm assuming is initialized by the time this code executes.) This change would result issue #1033 * replace string with char array prefix wled improve documentation Co-authored-by: garyd9 Co-authored-by: Gary Dezern --- CHANGELOG.md | 4 +++- wled00/FX_fcn.cpp | 11 +++++++++++ wled00/wled.cpp | 30 ++++++++++++++++++++++++++++-- wled00/wled.h | 2 +- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63271ad2d..b02aef1f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ### Development versions after 0.10.0 release -#### Build 2007020 +#### Build 2007190 + +- Fixed hostname containing illegal characters (#1035) #### Build 2006251 diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 399df185b..a1b4096cb 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -801,6 +801,16 @@ void WS2812FX::handle_palette(void) } } + +/* + * Gets a single color from the currently selected palette. + * @param i Palette Index (if mapping is true, the full palette will be SEGLEN long, if false, 255). Will wrap around automatically. + * @param mapping if true, LED position in segment is considered for color + * @param wrap FastLED palettes will usally wrap back to the start smoothly. Set false to get a hard edge + * @param mcol If the default palette 0 is selected, return the standard color 0, 1 or 2 instead. If >2, Party palette is used instead + * @param pbri Value to scale the brightness of the returned color by. Default is 255. (no scaling) + * @returns Single color from palette + */ uint32_t WS2812FX::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri) { if (SEGMENT.palette == 0 && mcol < 3) return SEGCOLOR(mcol); //WS2812FX default @@ -812,6 +822,7 @@ uint32_t WS2812FX::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8 return fastled_col.r*65536 + fastled_col.g*256 + fastled_col.b; } +//@returns `true` if color, mode, speed, intensity and palette match bool WS2812FX::segmentsAreIdentical(Segment* a, Segment* b) { //if (a->start != b->start) return false; diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 594c92252..586986549 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -324,15 +324,41 @@ void WLED::initConnection() DEBUG_PRINT(clientSSID); DEBUG_PRINTLN("..."); + // convert the "serverDescription" into a valid DNS hostname (alphanumeric) + char hostname[25] = "wled-"; + const char *pC = serverDescription; + uint8_t pos = 5; + + while (*pC && pos < 24) { // while !null and not over length + if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname + hostname[pos] = *pC; + pos++; + } else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') { + hostname[pos] = '-'; + pos++; + } + // else do nothing - no leading hyphens and do not include hyphens for all other characters. + pC++; + } + // if the hostname is left blank, use the mac address/default mdns name + if (pos < 6) { + sprintf(hostname + 5, "%*s", 6, escapedMac.c_str() + 6); + } else { //last character must not be hyphen + while (pos > 0 && hostname[pos -1] == '-') { + hostname[pos -1] = 0; + pos--; + } + } + #ifdef ESP8266 - WiFi.hostname(serverDescription); + WiFi.hostname(hostname); #endif WiFi.begin(clientSSID, clientPass); #ifdef ARDUINO_ARCH_ESP32 WiFi.setSleep(!noWifiSleep); - WiFi.setHostname(serverDescription); + WiFi.setHostname(hostname); #else wifi_set_sleep_type((noWifiSleep) ? NONE_SLEEP_T : MODEM_SLEEP_T); #endif diff --git a/wled00/wled.h b/wled00/wled.h index fda93b52f..fbe6ab016 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2007020 +#define VERSION 2007190 // ESP8266-01 (blue) got too little storage space to work with all features of WLED. To use it, you must use ESP8266 Arduino Core v2.4.2 and the setting 512K(No SPIFFS).