From e5c625951654127ffae9be62ab8615d8f63d91af Mon Sep 17 00:00:00 2001 From: IMAN4K Date: Thu, 11 Jan 2018 12:45:43 +0330 Subject: [PATCH 1/5] softAP SSID & PSK query API added. Signatures: String ESP8266WiFiAP::softAPSSID() const; String ESP8266WiFiAP::softAPPSK() const; --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 20 ++++++++++++++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 6cf361b47e..613789a888 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -333,3 +333,23 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) { sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return String(macStr); } + +/** +* Get the configured(Not-In-Flash) softAP SSID name. +* @return String SSID. +*/ +String ESP8266WiFiAPClass::softAPSSID() const { + struct softap_config config; + wifi_softap_get_config(&config); + return String(reinterpret_cast(config.ssid)); +} + +/** +* Get the configurd(Not-In-Flash) softAP PSK or PASSWORD. +* @return String psk. +*/ +String ESP8266WiFiAPClass::softAPPSK() const { + struct softap_config config; + wifi_softap_get_config(&config); + return String(reinterpret_cast(config.password)); +} diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index fae8efd567..f5e3363c0a 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -47,6 +47,9 @@ class ESP8266WiFiAPClass { uint8_t* softAPmacAddress(uint8_t* mac); String softAPmacAddress(void); + String softAPSSID() const; + String softAPPSK() const; + protected: }; From 3b79cb46d586605e53391d428f8901434266a857 Mon Sep 17 00:00:00 2001 From: IMAN4K Date: Wed, 17 Jan 2018 13:12:42 +0330 Subject: [PATCH 2/5] Fix for proper C-style string copy --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 613789a888..0b8f801eb1 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -341,15 +341,25 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) { String ESP8266WiFiAPClass::softAPSSID() const { struct softap_config config; wifi_softap_get_config(&config); - return String(reinterpret_cast(config.ssid)); + char* name = reinterpret_cast(config.ssid); + char ssid[sizeof(config.ssid) + 1]; + memcpy(ssid, name, sizeof(config.ssid)); + ssid[sizeof(config.ssid)] = '\0'; + + return String(ssid); } /** -* Get the configurd(Not-In-Flash) softAP PSK or PASSWORD. +* Get the configured(Not-In-Flash) softAP PSK or PASSWORD. * @return String psk. */ String ESP8266WiFiAPClass::softAPPSK() const { struct softap_config config; wifi_softap_get_config(&config); - return String(reinterpret_cast(config.password)); + char* pass = reinterpret_cast(config.password); + char psk[sizeof(config.password) + 1]; + memcpy(psk, pass, sizeof(config.password)); + psk[sizeof(config.password)] = '\0'; + + return String(psk); } From dcf64e167600eeb3e09139b200e2298b840d5e9a Mon Sep 17 00:00:00 2001 From: IMAN4K Date: Mon, 19 Feb 2018 12:55:43 +0330 Subject: [PATCH 3/5] add API to validate input ip as string Signatures: static bool IPAddress::isValid(const String& arg); static bool IPAddress::isValid(const char* arg, size_t len); --- cores/esp8266/IPAddress.cpp | 47 +++++++++++++++++++++++++++++++++++++ cores/esp8266/IPAddress.h | 8 +++++++ 2 files changed, 55 insertions(+) diff --git a/cores/esp8266/IPAddress.cpp b/cores/esp8266/IPAddress.cpp index 3349c2c5b1..30283a2033 100644 --- a/cores/esp8266/IPAddress.cpp +++ b/cores/esp8266/IPAddress.cpp @@ -112,4 +112,51 @@ String IPAddress::toString() const return String(szRet); } +/* ref: https://code.woboq.org/userspace/glibc/resolv/inet_pton.c.html#inet_pton4 */ +static bool inet_pton4(const char *start, const char *end) { + bool saw_digit = false; + char ch; + uint8_t octets = 0; + unsigned char tmp[4], *tp; + *(tp = tmp) = 0; + + while (start < end) { + ch = *start++; + if (isdigit(ch)) { + unsigned int _new = *tp * 10 + (ch - '0'); + if (saw_digit && *tp == 0) + return false; + if (_new > 255) + return false; + *tp = _new; + if (!saw_digit) { + if (++octets > 4) + return false; + saw_digit = true; + } + } else if (ch == '.' && saw_digit) { + if (octets == 4) + return false; + *++tp = 0; + saw_digit = false; + } else { + return false; + } + } + + if (octets < 4) + return false; + + return true; +} + +bool IPAddress::isValid(const String& arg) { + auto c_str = arg.c_str(); + return inet_pton4(c_str, c_str + arg.length()); +} + +bool IPAddress::isValid(const char* arg, size_t len) { + return inet_pton4(arg, arg + len); +} + const IPAddress INADDR_NONE(0, 0, 0, 0); diff --git a/cores/esp8266/IPAddress.h b/cores/esp8266/IPAddress.h index 1d0d3acafd..099969cb14 100644 --- a/cores/esp8266/IPAddress.h +++ b/cores/esp8266/IPAddress.h @@ -79,6 +79,14 @@ class IPAddress: public Printable { virtual size_t printTo(Print& p) const; String toString() const; + /* + check if input string(arg) is a valid IPV4 address or not. + return true on valid. + return false on invalid. + */ + static bool isValid(const String& arg); + static bool isValid(const char* arg, size_t len); + friend class EthernetClass; friend class UDP; friend class Client; From a62c5ab062b3319784b2898a5f2c85270082b5f5 Mon Sep 17 00:00:00 2001 From: IMAN4K Date: Mon, 19 Feb 2018 18:21:57 +0330 Subject: [PATCH 4/5] fix indentation --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 12 ++++++------ libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 0b8f801eb1..611244aa7c 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -335,9 +335,9 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) { } /** -* Get the configured(Not-In-Flash) softAP SSID name. -* @return String SSID. -*/ + * Get the configured(Not-In-Flash) softAP SSID name. + * @return String SSID. + */ String ESP8266WiFiAPClass::softAPSSID() const { struct softap_config config; wifi_softap_get_config(&config); @@ -350,9 +350,9 @@ String ESP8266WiFiAPClass::softAPSSID() const { } /** -* Get the configured(Not-In-Flash) softAP PSK or PASSWORD. -* @return String psk. -*/ + * Get the configured(Not-In-Flash) softAP PSK or PASSWORD. + * @return String psk. + */ String ESP8266WiFiAPClass::softAPPSK() const { struct softap_config config; wifi_softap_get_config(&config); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index f5e3363c0a..78156f102a 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -47,8 +47,8 @@ class ESP8266WiFiAPClass { uint8_t* softAPmacAddress(uint8_t* mac); String softAPmacAddress(void); - String softAPSSID() const; - String softAPPSK() const; + String softAPSSID() const; + String softAPPSK() const; protected: From 6ca28ab01b550f062470110bba82b7623aa52491 Mon Sep 17 00:00:00 2001 From: IMAN4K Date: Wed, 28 Mar 2018 13:20:53 +0430 Subject: [PATCH 5/5] fix ip string validation to use built-in implementation. signatures: static bool isValid(const String& arg); static bool isValid(const char* arg); --- cores/esp8266/IPAddress.cpp | 45 +++---------------------------------- cores/esp8266/IPAddress.h | 2 +- 2 files changed, 4 insertions(+), 43 deletions(-) diff --git a/cores/esp8266/IPAddress.cpp b/cores/esp8266/IPAddress.cpp index 30283a2033..ab051bfe38 100644 --- a/cores/esp8266/IPAddress.cpp +++ b/cores/esp8266/IPAddress.cpp @@ -112,51 +112,12 @@ String IPAddress::toString() const return String(szRet); } -/* ref: https://code.woboq.org/userspace/glibc/resolv/inet_pton.c.html#inet_pton4 */ -static bool inet_pton4(const char *start, const char *end) { - bool saw_digit = false; - char ch; - uint8_t octets = 0; - unsigned char tmp[4], *tp; - *(tp = tmp) = 0; - - while (start < end) { - ch = *start++; - if (isdigit(ch)) { - unsigned int _new = *tp * 10 + (ch - '0'); - if (saw_digit && *tp == 0) - return false; - if (_new > 255) - return false; - *tp = _new; - if (!saw_digit) { - if (++octets > 4) - return false; - saw_digit = true; - } - } else if (ch == '.' && saw_digit) { - if (octets == 4) - return false; - *++tp = 0; - saw_digit = false; - } else { - return false; - } - } - - if (octets < 4) - return false; - - return true; -} - bool IPAddress::isValid(const String& arg) { - auto c_str = arg.c_str(); - return inet_pton4(c_str, c_str + arg.length()); + return IPAddress().fromString(arg); } -bool IPAddress::isValid(const char* arg, size_t len) { - return inet_pton4(arg, arg + len); +bool IPAddress::isValid(const char* arg) { + return IPAddress().fromString(arg); } const IPAddress INADDR_NONE(0, 0, 0, 0); diff --git a/cores/esp8266/IPAddress.h b/cores/esp8266/IPAddress.h index 099969cb14..0ef1eba725 100644 --- a/cores/esp8266/IPAddress.h +++ b/cores/esp8266/IPAddress.h @@ -85,7 +85,7 @@ class IPAddress: public Printable { return false on invalid. */ static bool isValid(const String& arg); - static bool isValid(const char* arg, size_t len); + static bool isValid(const char* arg); friend class EthernetClass; friend class UDP;