Introduce IPUtils for checking if IPAddresses are set or not (#231)

This commit is contained in:
rjwats 2021-03-09 09:17:03 +00:00 committed by GitHub
parent dc34ef00f9
commit 8a869c9ad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 10 deletions

18
lib/framework/IPUtils.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef IPUtils_h
#define IPUtils_h
#include <IPAddress.h>
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

View File

@ -2,7 +2,7 @@
#define JsonUtils_h #define JsonUtils_h
#include <Arduino.h> #include <Arduino.h>
#include <IPAddress.h> #include <IPUtils.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
class JsonUtils { class JsonUtils {
@ -20,7 +20,7 @@ class JsonUtils {
} }
} }
static void writeIP(JsonObject& root, const String& key, const IPAddress& ip) { static void writeIP(JsonObject& root, const String& key, const IPAddress& ip) {
if (ip != INADDR_NONE) { if (IPUtils::isSet(ip)) {
root[key] = ip.toString(); root[key] = ip.toString();
} }
} }

View File

@ -24,8 +24,6 @@
#define WIFI_RECONNECTION_DELAY 1000 * 30 #define WIFI_RECONNECTION_DELAY 1000 * 30
const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE);
class WiFiSettings { class WiFiSettings {
public: public:
// core wifi configuration // core wifi configuration
@ -70,7 +68,7 @@ class WiFiSettings {
JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2); JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2);
// Swap around the dns servers if 2 is populated but 1 is not // 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.dnsIP1 = settings.dnsIP2;
settings.dnsIP2 = INADDR_NONE; settings.dnsIP2 = INADDR_NONE;
} }
@ -78,8 +76,8 @@ class WiFiSettings {
// Turning off static ip config if we don't meet the minimum requirements // 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 // of ipAddress, gateway and subnet. This may change to static ip only
// as sensible defaults can be assumed for gateway and subnet // as sensible defaults can be assumed for gateway and subnet
if (settings.staticIPConfig && if (settings.staticIPConfig && (IPUtils::isNotSet(settings.localIP) || IPUtils::isNotSet(settings.gatewayIP) ||
(settings.localIP == IP_NOT_SET || settings.gatewayIP == IP_NOT_SET || settings.subnetMask == IP_NOT_SET)) { IPUtils::isNotSet(settings.subnetMask))) {
settings.staticIPConfig = false; settings.staticIPConfig = false;
} }
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;

View File

@ -63,10 +63,10 @@ void WiFiStatus::wifiStatus(AsyncWebServerRequest* request) {
root["gateway_ip"] = WiFi.gatewayIP().toString(); root["gateway_ip"] = WiFi.gatewayIP().toString();
IPAddress dnsIP1 = WiFi.dnsIP(0); IPAddress dnsIP1 = WiFi.dnsIP(0);
IPAddress dnsIP2 = WiFi.dnsIP(1); IPAddress dnsIP2 = WiFi.dnsIP(1);
if (dnsIP1 != INADDR_NONE) { if (IPUtils::isSet(dnsIP1)) {
root["dns_ip_1"] = dnsIP1.toString(); root["dns_ip_1"] = dnsIP1.toString();
} }
if (dnsIP2 != INADDR_NONE) { if (IPUtils::isSet(dnsIP2)) {
root["dns_ip_2"] = dnsIP2.toString(); root["dns_ip_2"] = dnsIP2.toString();
} }
} }

View File

@ -12,7 +12,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <IPAddress.h> #include <IPUtils.h>
#include <SecurityManager.h> #include <SecurityManager.h>
#define MAX_WIFI_STATUS_SIZE 1024 #define MAX_WIFI_STATUS_SIZE 1024