From 0aefaaef55a7d3566bf93d5be2f24dfc53137cbf Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 8 Mar 2021 12:54:44 +0100 Subject: [PATCH] merge ipaddress fix from esp8266-react --- lib/framework/IPUtils.h | 18 ++++++++++++++++++ lib/framework/JsonUtils.h | 3 ++- lib/framework/NetworkSettingsService.h | 4 ++-- lib/framework/NetworkStatus.cpp | 4 ++-- lib/framework/NetworkStatus.h | 1 + 5 files changed, 25 insertions(+), 5 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 000000000..868ec8ac8 --- /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 b1272376a..d0219856f 100644 --- a/lib/framework/JsonUtils.h +++ b/lib/framework/JsonUtils.h @@ -3,6 +3,7 @@ #include #include +#include #include class JsonUtils { @@ -20,7 +21,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/NetworkSettingsService.h b/lib/framework/NetworkSettingsService.h index 734c684d8..15fb43a90 100644 --- a/lib/framework/NetworkSettingsService.h +++ b/lib/framework/NetworkSettingsService.h @@ -71,7 +71,7 @@ class NetworkSettings { JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2); // Swap around the dns servers if 2 is populated but 1 is not - if (settings.dnsIP1 == (IPAddress)INADDR_NONE && settings.dnsIP2 != INADDR_NONE) { + if (IPUtils::isNotSet(settings.dnsIP1) && IPUtils::isSet(settings.dnsIP2)) { settings.dnsIP1 = settings.dnsIP2; settings.dnsIP2 = INADDR_NONE; } @@ -79,7 +79,7 @@ class NetworkSettings { // 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 == (IPAddress)INADDR_NONE || settings.gatewayIP == (IPAddress)INADDR_NONE || settings.subnetMask == (IPAddress)INADDR_NONE)) { + if (settings.staticIPConfig && (IPUtils::isNotSet(settings.localIP) || IPUtils::isNotSet(settings.gatewayIP) || IPUtils::isNotSet(settings.subnetMask))) { settings.staticIPConfig = false; } diff --git a/lib/framework/NetworkStatus.cpp b/lib/framework/NetworkStatus.cpp index e2bbc4069..ec1c96f8b 100644 --- a/lib/framework/NetworkStatus.cpp +++ b/lib/framework/NetworkStatus.cpp @@ -46,10 +46,10 @@ void NetworkStatus::networkStatus(AsyncWebServerRequest * request) { root["gateway_ip"] = ETH.gatewayIP().toString(); IPAddress dnsIP1 = ETH.dnsIP(0); IPAddress dnsIP2 = ETH.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/NetworkStatus.h b/lib/framework/NetworkStatus.h index 70ed03505..dd7d5402c 100644 --- a/lib/framework/NetworkStatus.h +++ b/lib/framework/NetworkStatus.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #define MAX_NETWORK_STATUS_SIZE 1024