From dc34ef00f9de680a81fb51431927a06f88d7dd39 Mon Sep 17 00:00:00 2001 From: rjwats Date: Mon, 8 Mar 2021 00:13:29 +0000 Subject: [PATCH 1/2] quick fix for ip address issue under esp32 with latest espressif (#230) --- lib/framework/WiFiSettingsService.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index 3d1dc22..b979c9c 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -24,6 +24,8 @@ #define WIFI_RECONNECTION_DELAY 1000 * 30 +const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE); + class WiFiSettings { public: // core wifi configuration @@ -68,7 +70,7 @@ class WiFiSettings { JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2); // Swap around the dns servers if 2 is populated but 1 is not - if (settings.dnsIP1 == INADDR_NONE && settings.dnsIP2 != INADDR_NONE) { + if (settings.dnsIP1 == IP_NOT_SET && settings.dnsIP2 != IP_NOT_SET) { settings.dnsIP1 = settings.dnsIP2; settings.dnsIP2 = INADDR_NONE; } @@ -77,7 +79,7 @@ class WiFiSettings { // of ipAddress, gateway and subnet. This may change to static ip only // as sensible defaults can be assumed for gateway and subnet if (settings.staticIPConfig && - (settings.localIP == INADDR_NONE || settings.gatewayIP == INADDR_NONE || settings.subnetMask == INADDR_NONE)) { + (settings.localIP == IP_NOT_SET || settings.gatewayIP == IP_NOT_SET || settings.subnetMask == IP_NOT_SET)) { settings.staticIPConfig = false; } return StateUpdateResult::CHANGED; From 8a869c9ad8a324b1c38ea7ce7cb0b3292c0a3014 Mon Sep 17 00:00:00 2001 From: rjwats Date: Tue, 9 Mar 2021 09:17:03 +0000 Subject: [PATCH 2/2] Introduce IPUtils for checking if IPAddresses are set or not (#231) --- lib/framework/IPUtils.h | 18 ++++++++++++++++++ lib/framework/JsonUtils.h | 4 ++-- lib/framework/WiFiSettingsService.h | 8 +++----- lib/framework/WiFiStatus.cpp | 4 ++-- lib/framework/WiFiStatus.h | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 lib/framework/IPUtils.h diff --git a/lib/framework/IPUtils.h b/lib/framework/IPUtils.h new file mode 100644 index 0000000..74886a0 --- /dev/null +++ b/lib/framework/IPUtils.h @@ -0,0 +1,18 @@ +#ifndef IPUtils_h +#define IPUtils_h + +#include + +const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE); + +class IPUtils { + public: + static bool isSet(const IPAddress& ip) { + return ip != IP_NOT_SET; + } + static bool isNotSet(const IPAddress& ip) { + return ip == IP_NOT_SET; + } +}; + +#endif // end IPUtils_h diff --git a/lib/framework/JsonUtils.h b/lib/framework/JsonUtils.h index 0c40898..57d94b8 100644 --- a/lib/framework/JsonUtils.h +++ b/lib/framework/JsonUtils.h @@ -2,7 +2,7 @@ #define JsonUtils_h #include -#include +#include #include class JsonUtils { @@ -20,7 +20,7 @@ class JsonUtils { } } static void writeIP(JsonObject& root, const String& key, const IPAddress& ip) { - if (ip != INADDR_NONE) { + if (IPUtils::isSet(ip)) { root[key] = ip.toString(); } } diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index b979c9c..1f5036b 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -24,8 +24,6 @@ #define WIFI_RECONNECTION_DELAY 1000 * 30 -const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE); - class WiFiSettings { public: // core wifi configuration @@ -70,7 +68,7 @@ class WiFiSettings { JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2); // Swap around the dns servers if 2 is populated but 1 is not - if (settings.dnsIP1 == IP_NOT_SET && settings.dnsIP2 != IP_NOT_SET) { + if (IPUtils::isNotSet(settings.dnsIP1) && IPUtils::isSet(settings.dnsIP2)) { settings.dnsIP1 = settings.dnsIP2; settings.dnsIP2 = INADDR_NONE; } @@ -78,8 +76,8 @@ class WiFiSettings { // Turning off static ip config if we don't meet the minimum requirements // of ipAddress, gateway and subnet. This may change to static ip only // as sensible defaults can be assumed for gateway and subnet - if (settings.staticIPConfig && - (settings.localIP == IP_NOT_SET || settings.gatewayIP == IP_NOT_SET || settings.subnetMask == IP_NOT_SET)) { + if (settings.staticIPConfig && (IPUtils::isNotSet(settings.localIP) || IPUtils::isNotSet(settings.gatewayIP) || + IPUtils::isNotSet(settings.subnetMask))) { settings.staticIPConfig = false; } return StateUpdateResult::CHANGED; diff --git a/lib/framework/WiFiStatus.cpp b/lib/framework/WiFiStatus.cpp index de69542..81bc372 100644 --- a/lib/framework/WiFiStatus.cpp +++ b/lib/framework/WiFiStatus.cpp @@ -63,10 +63,10 @@ void WiFiStatus::wifiStatus(AsyncWebServerRequest* request) { root["gateway_ip"] = WiFi.gatewayIP().toString(); IPAddress dnsIP1 = WiFi.dnsIP(0); IPAddress dnsIP2 = WiFi.dnsIP(1); - if (dnsIP1 != INADDR_NONE) { + if (IPUtils::isSet(dnsIP1)) { root["dns_ip_1"] = dnsIP1.toString(); } - if (dnsIP2 != INADDR_NONE) { + if (IPUtils::isSet(dnsIP2)) { root["dns_ip_2"] = dnsIP2.toString(); } } diff --git a/lib/framework/WiFiStatus.h b/lib/framework/WiFiStatus.h index 197bd64..213c99b 100644 --- a/lib/framework/WiFiStatus.h +++ b/lib/framework/WiFiStatus.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #define MAX_WIFI_STATUS_SIZE 1024