Skip to content

Commit

Permalink
Introduce IPUtils for checking if IPAddresses are set or not (rjwats#231
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rjwats authored Mar 9, 2021
1 parent dc34ef0 commit 8a869c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
18 changes: 18 additions & 0 deletions lib/framework/IPUtils.h
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/framework/JsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define JsonUtils_h

#include <Arduino.h>
#include <IPAddress.h>
#include <IPUtils.h>
#include <ArduinoJson.h>

class JsonUtils {
Expand All @@ -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();
}
}
Expand Down
8 changes: 3 additions & 5 deletions lib/framework/WiFiSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

#define WIFI_RECONNECTION_DELAY 1000 * 30

const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE);

class WiFiSettings {
public:
// core wifi configuration
Expand Down Expand Up @@ -70,16 +68,16 @@ 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;
}

// 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;
Expand Down
4 changes: 2 additions & 2 deletions lib/framework/WiFiStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/WiFiStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
#include <IPAddress.h>
#include <IPUtils.h>
#include <SecurityManager.h>

#define MAX_WIFI_STATUS_SIZE 1024
Expand Down

0 comments on commit 8a869c9

Please sign in to comment.