⚡ Make ip be uint32 instead of strings
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <utils/json_utils.h>
|
||||
#include <template/state_result.h>
|
||||
#include <string>
|
||||
|
||||
@@ -53,6 +52,12 @@
|
||||
|
||||
enum APNetworkStatus { ACTIVE = 0, INACTIVE, LINGERING };
|
||||
|
||||
inline uint32_t parseIPv4(const char *str) {
|
||||
IPAddress ip;
|
||||
ip.fromString(str);
|
||||
return (uint32_t)ip;
|
||||
}
|
||||
|
||||
class APSettings {
|
||||
public:
|
||||
uint8_t provisionMode;
|
||||
@@ -79,9 +84,9 @@ class APSettings {
|
||||
root["channel"] = settings.channel;
|
||||
root["ssid_hidden"] = settings.ssidHidden;
|
||||
root["max_clients"] = settings.maxClients;
|
||||
root["local_ip"] = settings.localIP.toString();
|
||||
root["gateway_ip"] = settings.gatewayIP.toString();
|
||||
root["subnet_mask"] = settings.subnetMask.toString();
|
||||
root["local_ip"] = (uint32_t)(settings.localIP);
|
||||
root["gateway_ip"] = (uint32_t)(settings.gatewayIP);
|
||||
root["subnet_mask"] = (uint32_t)(settings.subnetMask);
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonVariant &root, APSettings &settings) {
|
||||
@@ -99,9 +104,9 @@ class APSettings {
|
||||
newSettings.ssidHidden = root["ssid_hidden"] | FACTORY_AP_SSID_HIDDEN;
|
||||
newSettings.maxClients = root["max_clients"] | FACTORY_AP_MAX_CLIENTS;
|
||||
|
||||
JsonUtils::readIP(root, "local_ip", newSettings.localIP, FACTORY_AP_LOCAL_IP);
|
||||
JsonUtils::readIP(root, "gateway_ip", newSettings.gatewayIP, FACTORY_AP_GATEWAY_IP);
|
||||
JsonUtils::readIP(root, "subnet_mask", newSettings.subnetMask, FACTORY_AP_SUBNET_MASK);
|
||||
newSettings.localIP = IPAddress(root["local_ip"] | parseIPv4(FACTORY_AP_LOCAL_IP));
|
||||
newSettings.gatewayIP = IPAddress(root["gateway_ip"] | parseIPv4(FACTORY_AP_GATEWAY_IP));
|
||||
newSettings.subnetMask = IPAddress(root["subnet_mask"] | parseIPv4(FACTORY_AP_SUBNET_MASK));
|
||||
|
||||
if (newSettings == settings) {
|
||||
return StateUpdateResult::UNCHANGED;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <utils/json_utils.h>
|
||||
#include <template/state_result.h>
|
||||
#include <filesystem.h>
|
||||
#include <string>
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <utils/json_utils.h>
|
||||
#include <template/state_result.h>
|
||||
#include <string>
|
||||
|
||||
@@ -40,11 +39,11 @@ typedef struct {
|
||||
json["password"] = password.c_str();
|
||||
json["static_ip_config"] = staticIPConfig;
|
||||
if (staticIPConfig) {
|
||||
JsonUtils::writeIP(json, "local_ip", localIP);
|
||||
JsonUtils::writeIP(json, "gateway_ip", gatewayIP);
|
||||
JsonUtils::writeIP(json, "subnet_mask", subnetMask);
|
||||
JsonUtils::writeIP(json, "dns_ip_1", dnsIP1);
|
||||
JsonUtils::writeIP(json, "dns_ip_2", dnsIP2);
|
||||
json["local_ip"] = (uint32_t)(localIP);
|
||||
json["gateway_ip"] = (uint32_t)(gatewayIP);
|
||||
json["subnet_mask"] = (uint32_t)(subnetMask);
|
||||
json["dns_ip_1"] = (uint32_t)(dnsIP1);
|
||||
json["dns_ip_2"] = (uint32_t)(dnsIP2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,11 +58,11 @@ typedef struct {
|
||||
password = newPassword;
|
||||
staticIPConfig = json["static_ip_config"] | false;
|
||||
if (staticIPConfig) {
|
||||
JsonUtils::readIP(json, "local_ip", localIP);
|
||||
JsonUtils::readIP(json, "gateway_ip", gatewayIP);
|
||||
JsonUtils::readIP(json, "subnet_mask", subnetMask);
|
||||
JsonUtils::readIP(json, "dns_ip_1", dnsIP1);
|
||||
JsonUtils::readIP(json, "dns_ip_2", dnsIP2);
|
||||
localIP = IPAddress(json["local_ip"] | 0u);
|
||||
gatewayIP = IPAddress(json["gateway_ip"] | 0u);
|
||||
subnetMask = IPAddress(json["subnet_mask"] | 0u);
|
||||
dnsIP1 = IPAddress(json["dns_ip_1"] | 0u);
|
||||
dnsIP2 = IPAddress(json["dns_ip_2"] | 0u);
|
||||
if (dnsIP1 == IPAddress(0, 0, 0, 0) && dnsIP2 != IPAddress(0, 0, 0, 0)) {
|
||||
dnsIP1 = dnsIP2;
|
||||
dnsIP2 = IPAddress(0, 0, 0, 0);
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <string>
|
||||
|
||||
class JsonUtils {
|
||||
public:
|
||||
static void readIP(const JsonVariant &root, const std::string &key, IPAddress &ip, const std::string &def) {
|
||||
IPAddress defaultIp = {};
|
||||
if (!defaultIp.fromString(def.c_str())) {
|
||||
defaultIp = IPAddress(0, 0, 0, 0);
|
||||
}
|
||||
readIP(root, key, ip, defaultIp);
|
||||
}
|
||||
|
||||
static void readIP(const JsonVariant &root, const std::string &key, IPAddress &ip,
|
||||
const IPAddress &defaultIp = IPAddress(0, 0, 0, 0)) {
|
||||
if (!root[key].is<std::string>() || !ip.fromString(root[key].as<std::string>().c_str())) {
|
||||
ip = defaultIp;
|
||||
}
|
||||
}
|
||||
|
||||
static void writeIP(JsonVariant &root, const std::string &key, const IPAddress &ip) {
|
||||
if (ip != IPAddress(0, 0, 0, 0)) {
|
||||
root[key] = ip.toString();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -21,7 +21,7 @@ esp_err_t APService::getStatus(PsychicRequest *request) {
|
||||
|
||||
void APService::status(JsonObject &root) {
|
||||
root["status"] = getAPNetworkStatus();
|
||||
root["ip_address"] = WiFi.softAPIP().toString();
|
||||
root["ip_address"] = (uint32_t)(WiFi.softAPIP());
|
||||
root["mac_address"] = WiFi.softAPmacAddress();
|
||||
root["station_num"] = WiFi.softAPgetStationNum();
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ esp_err_t handleSleep(PsychicRequest *request) {
|
||||
return request->reply(200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void reset() {
|
||||
ESP_LOGI(TAG, "Resetting device");
|
||||
File root = ESP_FS.open(FS_CONFIG_DIRECTORY);
|
||||
@@ -73,25 +71,23 @@ void sleep() {
|
||||
ESP_LOGI(TAG, "Setting device to sleep");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void getStaticSystemInformation(socket_message_StaticSystemInformation &info) {
|
||||
size_t fs_total = 0, fs_used = 0;
|
||||
esp_littlefs_info("spiffs", &fs_total, &fs_used);
|
||||
|
||||
info.esp_platform = (char*) ESP_PLATFORM_NAME;
|
||||
info.esp_platform = (char *)ESP_PLATFORM_NAME;
|
||||
info.firmware_version = APP_VERSION;
|
||||
info.cpu_freq_mhz = ESP.getCpuFreqMHz();
|
||||
info.cpu_type = (char*) ESP.getChipModel();
|
||||
info.cpu_type = (char *)ESP.getChipModel();
|
||||
info.cpu_rev = ESP.getChipRevision();
|
||||
info.cpu_cores = ESP.getChipCores();
|
||||
info.sketch_size = ESP.getSketchSize();
|
||||
info.free_sketch_space = ESP.getFreeSketchSpace();
|
||||
info.sdk_version = (char*) ESP.getSdkVersion();
|
||||
info.sdk_version = (char *)ESP.getSdkVersion();
|
||||
info.arduino_version = ARDUINO_VERSION;
|
||||
info.flash_chip_size = ESP.getFlashChipSize();
|
||||
info.flash_chip_speed = ESP.getFlashChipSpeed();
|
||||
info.cpu_reset_reason = (char*) resetReason(esp_reset_reason());
|
||||
info.cpu_reset_reason = (char *)resetReason(esp_reset_reason());
|
||||
}
|
||||
|
||||
void getAnalytics(socket_message_AnalyticsData &analytics) {
|
||||
|
||||
@@ -91,21 +91,21 @@ void WiFiService::getNetworkStatus(JsonObject &root) {
|
||||
wl_status_t status = WiFi.status();
|
||||
root["status"] = (uint8_t)status;
|
||||
if (status == WL_CONNECTED) {
|
||||
root["local_ip"] = WiFi.localIP().toString();
|
||||
root["local_ip"] = (uint32_t)(WiFi.localIP());
|
||||
root["mac_address"] = WiFi.macAddress();
|
||||
root["rssi"] = WiFi.RSSI();
|
||||
root["ssid"] = WiFi.SSID();
|
||||
root["bssid"] = WiFi.BSSIDstr();
|
||||
root["channel"] = WiFi.channel();
|
||||
root["subnet_mask"] = WiFi.subnetMask().toString();
|
||||
root["gateway_ip"] = WiFi.gatewayIP().toString();
|
||||
root["subnet_mask"] = (uint32_t)(WiFi.subnetMask());
|
||||
root["gateway_ip"] = (uint32_t)(WiFi.gatewayIP());
|
||||
IPAddress dnsIP1 = WiFi.dnsIP(0);
|
||||
IPAddress dnsIP2 = WiFi.dnsIP(1);
|
||||
if (dnsIP1 != IPAddress(0, 0, 0, 0)) {
|
||||
root["dns_ip_1"] = dnsIP1.toString();
|
||||
root["dns_ip_1"] = (uint32_t)(dnsIP1);
|
||||
}
|
||||
if (dnsIP2 != IPAddress(0, 0, 0, 0)) {
|
||||
root["dns_ip_2"] = dnsIP2.toString();
|
||||
root["dns_ip_2"] = (uint32_t)(dnsIP2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user