8000 Introduce IPUtils for checking if IPAddresses are set or not (#231) · thisiscam/esp8266-react@8a869c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a869c9

Browse files
authored
Introduce IPUtils for checking if IPAddresses are set or not (rjwats#231)
1 parent dc34ef0 commit 8a869c9

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

lib/framework/IPUtils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef IPUtils_h
2+
#define IPUtils_h
3+
4+
#include <IPAddress.h>
5+
6+
const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE);
7+
8+
class IPUtils {
9+
public:
10+
static bool isSet(const IPAddress& ip) {
11+
return ip != IP_NOT_SET;
12+
}
13+
static bool isNotSet(const IPAddress& ip) {
14+
return ip == IP_NOT_SET;
15+
}
16+
};
17+
18+
#endif // end IPUtils_h

lib/framework/JsonUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define JsonUtils_h
33

44
#include <Arduino.h>
5-
#include <IPAddress.h>
5+
#include <IPUtils.h>
66
#include <ArduinoJson.h>
77

88
class JsonUtils {
@@ -20,7 +20,7 @@ class JsonUtils {
2020
}
2121
}
2222
static void writeIP(JsonObject& root, const String& key, const IPAddress& ip) {
23-
if (ip != INADDR_NONE) {
23+
if (IPUtils::isSet(ip)) {
2424
root[key] = ip.toString();
2525
}
2626
}

lib/framework/WiFiSettingsService.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
#define WIFI_RECONNECTION_DELAY 1000 * 30
2626

27-
const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE);
28-
2927
class WiFiSettings {
3028
public:
3129
// core wifi configuration
@@ -70,16 +68,16 @@ class WiFiSettings {
7068
JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2);
7169

7270
// Swap around the dns servers if 2 is populated but 1 is not
73-
if (settings.dnsIP1 == IP_NOT_SET && settings.dnsIP2 != IP_NOT_SET) {
71+
if (IPUtils::isNotSet(settings.dnsIP1) && IPUtils::isSet(settings.dnsIP2)) {
7472
settings.dnsIP1 = settings.dnsIP2;
7573
settings.dnsIP2 = INADDR_NONE;
7674
}
7775

7876
// Turning off static ip config if we don't meet the minimum requirements
7977
// of ipAddress, gateway and subnet. This may change to static ip only
8078
// as sensible defaults can be assumed for gateway and subnet
81-
if (settings.staticIPConfig &&
82-
(settings.localIP == IP_NOT_SET || settings.gatewayIP == IP_NOT_SET || settings.subnetMask == IP_NOT_SET)) {
79+
if (settings.staticIPConfig && (IPUtils::isNotSet(settings.localIP) || IPUtils::isNotSet(settings.gatewayIP) ||
80+
IPUtils::isNotSet(settings.subnetMask))) {
8381
settings.staticIPConfig = false;
8482
}
8583
return StateUpdateResult::CHANGED;

lib/framework/WiFiStatus.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ void WiFiStatus::wifiStatus(AsyncWebServerRequest* request) {
6363
root["gateway_ip"] = WiFi.gatewayIP().toString();
6464
IPAddress dnsIP1 = WiFi.dnsIP(0);
6565
IPAddress dnsIP2 = WiFi.dnsIP(1);
66-
if (dnsIP1 != INADDR_NONE) {
66+
if (IPUtils::isSet(dnsIP1)) {
6767
root["dns_ip_1"] = dnsIP1.toString();
6868
}
69-
if (dnsIP2 != INADDR_NONE) {
69+
if (IPUtils::isSet(dnsIP2)) {
7070
root["dns_ip_2"] = dnsIP2.toString();
7171
}
7272
}

lib/framework/WiFiStatus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <ArduinoJson.h>
1313
#include <AsyncJson.h>
1414
#include <ESPAsyncWebServer.h>
15-
#include <IPAddress.h>
15+
#include <IPUtils.h>
1616
#include <SecurityManager.h>
1717

1818
#define MAX_WIFI_STATUS_SIZE 1024

0 commit comments

Comments
 (0)
0