mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-12-05 20:53:07 +05:00
feat: migrate network settings to NVS
This commit is contained in:
1 parent
2ad969bcab
commit
c142cdc0db
7 files changed
+322
-46
No files matched your search
@@ -0,0 +1,104 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LittleFS.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "Settings.h"
|
||||||
|
#include "strings_shared.h"
|
||||||
|
|
||||||
|
namespace LegacySettings {
|
||||||
|
inline const char networkFile[] = "/network.conf";
|
||||||
|
|
||||||
|
inline bool read(NetworkSettings& dst) {
|
||||||
|
// Keep the old FileData layout independent of the current NetworkSettings.
|
||||||
|
struct {
|
||||||
|
char hostname[25];
|
||||||
|
uint8_t useDhcp;
|
||||||
|
char ip[16], gateway[16], subnet[16], dns[16];
|
||||||
|
struct {
|
||||||
|
char ssid[33];
|
||||||
|
char password[65];
|
||||||
|
uint8_t channel;
|
||||||
|
} ap, sta;
|
||||||
|
uint8_t ethernetType;
|
||||||
|
uint8_t clk, mosi, miso, cs, irq, rst;
|
||||||
|
uint8_t mdc, mdio, power, phyAddr, clockMode;
|
||||||
|
uint8_t mode;
|
||||||
|
} record;
|
||||||
|
static_assert(sizeof(record) == 301);
|
||||||
|
|
||||||
|
File file = LittleFS.open(networkFile, FILE_READ);
|
||||||
|
if (!file || file.read() != 'n') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t readSize = file.available();
|
||||||
|
if (readSize != 296 && readSize != sizeof(record)) {
|
||||||
|
readSize = 288; // Common hostname, IPv4 and Wi-Fi fields.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.read(reinterpret_cast<uint8_t*>(&record), readSize) != readSize) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto copyString = [](const auto& source, auto& target) {
|
||||||
|
// A legacy string may be missing its terminating '\0'.
|
||||||
|
const size_t length = strnlen(source, sizeof(source));
|
||||||
|
|
||||||
|
// The source must contain a NUL and the target must have room for it.
|
||||||
|
if (length == sizeof(source) || length >= sizeof(target)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memcpy(target, source, length + 1);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Main network settings
|
||||||
|
NetworkSettings data;
|
||||||
|
data.useDhcp = record.useDhcp != 0;
|
||||||
|
data.wifi.ap.channel = record.ap.channel;
|
||||||
|
data.wifi.sta.channel = record.sta.channel;
|
||||||
|
|
||||||
|
bool valid = copyString(record.hostname, data.hostname);
|
||||||
|
valid &= copyString(record.ip, data.staticConfig.ip);
|
||||||
|
valid &= copyString(record.gateway, data.staticConfig.gateway);
|
||||||
|
valid &= copyString(record.subnet, data.staticConfig.subnet);
|
||||||
|
valid &= copyString(record.dns, data.staticConfig.dns);
|
||||||
|
valid &= copyString(record.ap.ssid, data.wifi.ap.ssid);
|
||||||
|
valid &= copyString(record.ap.password, data.wifi.ap.password);
|
||||||
|
valid &= copyString(record.sta.ssid, data.wifi.sta.ssid);
|
||||||
|
valid &= copyString(record.sta.password, data.wifi.sta.password);
|
||||||
|
if (!valid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// W5500
|
||||||
|
if (readSize >= 296) {
|
||||||
|
data.ethernet.type = static_cast<EthernetType>(record.ethernetType);
|
||||||
|
data.ethernet.w5500.clk = record.clk;
|
||||||
|
data.ethernet.w5500.mosi = record.mosi;
|
||||||
|
data.ethernet.w5500.miso = record.miso;
|
||||||
|
data.ethernet.w5500.cs = record.cs;
|
||||||
|
data.ethernet.w5500.irq = record.irq;
|
||||||
|
data.ethernet.w5500.rst = record.rst;
|
||||||
|
}
|
||||||
|
|
||||||
|
// LAN8720
|
||||||
|
if (readSize == sizeof(record)) {
|
||||||
|
data.ethernet.lan8720.mdc = record.mdc;
|
||||||
|
data.ethernet.lan8720.mdio = record.mdio;
|
||||||
|
data.ethernet.lan8720.power = record.power;
|
||||||
|
data.ethernet.lan8720.phyAddr = record.phyAddr;
|
||||||
|
data.ethernet.lan8720.clockMode = record.clockMode;
|
||||||
|
data.type = static_cast<NetworkMode>(record.mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
dst = data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool purge(const NetworkSettings&) {
|
||||||
|
return LittleFS.remove(networkFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
+213
-41
@@ -1,20 +1,68 @@
|
|||||||
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||||
|
|
||||||
#include <app.h>
|
#include <app.h>
|
||||||
|
|
||||||
#if USE_ETHERNET
|
#if USE_ETHERNET
|
||||||
#include <ETH.h>
|
#include <ETH.h>
|
||||||
#endif
|
#endif
|
||||||
#include <FileData.h>
|
#include <FileData.h>
|
||||||
|
#include <LegacySettings.h>
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
|
#include <Preferences.h>
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace NvsNetworkKeys {
|
||||||
|
static const auto& hostname = S_HOSTNAME;
|
||||||
|
static const char type[] = "mode";
|
||||||
|
|
||||||
|
namespace staticConfig {
|
||||||
|
static const char dhcp[] = "static.dhcp";
|
||||||
|
static const char ip[] = "static.ip";
|
||||||
|
static const char gateway[] = "static.gw";
|
||||||
|
static const char subnet[] = "static.subnet";
|
||||||
|
static const char dns[] = "static.dns";
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace wifi::ap {
|
||||||
|
static const char ssid[] = "ap.ssid";
|
||||||
|
static const char password[] = "ap.pwd";
|
||||||
|
static const char channel[] = "ap.chan";
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace wifi::sta {
|
||||||
|
static const char ssid[] = "sta.ssid";
|
||||||
|
static const char password[] = "sta.pwd";
|
||||||
|
static const char channel[] = "sta.chan";
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ethernet {
|
||||||
|
static const char type[] = "eth.type";
|
||||||
|
|
||||||
|
namespace w5500 {
|
||||||
|
static const char clk[] = "eth.w5500.clk";
|
||||||
|
static const char mosi[] = "eth.w5500.mosi";
|
||||||
|
static const char miso[] = "eth.w5500.miso";
|
||||||
|
static const char cs[] = "eth.w5500.cs";
|
||||||
|
static const char irq[] = "eth.w5500.irq";
|
||||||
|
static const char rst[] = "eth.w5500.rst";
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace lan8720 {
|
||||||
|
static const char mdc[] = "eth.8720.mdc";
|
||||||
|
static const char mdio[] = "eth.8720.mdio";
|
||||||
|
static const char power[] = "eth.8720.pwr";
|
||||||
|
static const char phyAddr[] = "eth.8720.addr";
|
||||||
|
static const char clockMode[] = "eth.8720.clkm";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NetworkSettings networkSettings;
|
NetworkSettings networkSettings;
|
||||||
Settings settings;
|
Settings settings;
|
||||||
|
|
||||||
Sensors::Settings sensorsSettings[SENSORS_AMOUNT] = {
|
Sensors::Settings sensorsSettings[SENSORS_AMOUNT] = {
|
||||||
{
|
{
|
||||||
false,
|
false,
|
||||||
@@ -97,16 +145,125 @@ Sensors::Settings sensorsSettings[SENSORS_AMOUNT] = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static FileData fsNetworkSettings(&LittleFS, "/network.conf", 'n', &networkSettings, sizeof(networkSettings), 1000);
|
|
||||||
static FileData fsSettings(&LittleFS, "/settings.conf", 's', &settings, sizeof(settings), 60000);
|
static FileData fsSettings(&LittleFS, "/settings.conf", 's', &settings, sizeof(settings), 60000);
|
||||||
static FileData fsSensorsSettings(&LittleFS, "/sensors.conf", 'e', &sensorsSettings, sizeof(sensorsSettings), 60000);
|
static FileData fsSensorsSettings(&LittleFS, "/sensors.conf", 'e', &sensorsSettings, sizeof(sensorsSettings), 60000);
|
||||||
|
|
||||||
// FileData requests can arrive while the save task is running.
|
|
||||||
static std::mutex settingsSaveMutex;
|
static std::mutex settingsSaveMutex;
|
||||||
|
static std::atomic<bool> saveNetworkSettingsRequested{false};
|
||||||
|
static std::atomic<uint32_t> saveNetworkSettingsRequestedAt{0};
|
||||||
|
|
||||||
|
static bool readFromNvs(NetworkSettings& dst) {
|
||||||
|
Preferences prefs;
|
||||||
|
if (!prefs.begin(S_NETWORK, true)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hostname
|
||||||
|
prefs.getString(NvsNetworkKeys::hostname, dst.hostname, sizeof(dst.hostname));
|
||||||
|
|
||||||
|
// IPv4 addressing
|
||||||
|
dst.useDhcp = prefs.getBool(NvsNetworkKeys::staticConfig::dhcp, dst.useDhcp);
|
||||||
|
prefs.getString(NvsNetworkKeys::staticConfig::ip, dst.staticConfig.ip, sizeof(dst.staticConfig.ip));
|
||||||
|
prefs.getString(NvsNetworkKeys::staticConfig::gateway, dst.staticConfig.gateway, sizeof(dst.staticConfig.gateway));
|
||||||
|
prefs.getString(NvsNetworkKeys::staticConfig::subnet, dst.staticConfig.subnet, sizeof(dst.staticConfig.subnet));
|
||||||
|
prefs.getString(NvsNetworkKeys::staticConfig::dns, dst.staticConfig.dns, sizeof(dst.staticConfig.dns));
|
||||||
|
|
||||||
|
// Wi-Fi access point
|
||||||
|
prefs.getString(NvsNetworkKeys::wifi::ap::ssid, dst.wifi.ap.ssid, sizeof(dst.wifi.ap.ssid));
|
||||||
|
prefs.getString(NvsNetworkKeys::wifi::ap::password, dst.wifi.ap.password, sizeof(dst.wifi.ap.password));
|
||||||
|
dst.wifi.ap.channel = prefs.getUChar(NvsNetworkKeys::wifi::ap::channel, dst.wifi.ap.channel);
|
||||||
|
|
||||||
|
// Wi-Fi station
|
||||||
|
prefs.getString(NvsNetworkKeys::wifi::sta::ssid, dst.wifi.sta.ssid, sizeof(dst.wifi.sta.ssid));
|
||||||
|
prefs.getString(NvsNetworkKeys::wifi::sta::password, dst.wifi.sta.password, sizeof(dst.wifi.sta.password));
|
||||||
|
dst.wifi.sta.channel = prefs.getUChar(NvsNetworkKeys::wifi::sta::channel, dst.wifi.sta.channel);
|
||||||
|
|
||||||
|
// Ethernet controller
|
||||||
|
dst.ethernet.type = static_cast<EthernetType>(prefs.getUChar(NvsNetworkKeys::ethernet::type, static_cast<uint8_t>(dst.ethernet.type)));
|
||||||
|
|
||||||
|
// W5500
|
||||||
|
dst.ethernet.w5500.clk = prefs.getUChar(NvsNetworkKeys::ethernet::w5500::clk, dst.ethernet.w5500.clk);
|
||||||
|
dst.ethernet.w5500.mosi = prefs.getUChar(NvsNetworkKeys::ethernet::w5500::mosi, dst.ethernet.w5500.mosi);
|
||||||
|
dst.ethernet.w5500.miso = prefs.getUChar(NvsNetworkKeys::ethernet::w5500::miso, dst.ethernet.w5500.miso);
|
||||||
|
dst.ethernet.w5500.cs = prefs.getUChar(NvsNetworkKeys::ethernet::w5500::cs, dst.ethernet.w5500.cs);
|
||||||
|
dst.ethernet.w5500.irq = prefs.getUChar(NvsNetworkKeys::ethernet::w5500::irq, dst.ethernet.w5500.irq);
|
||||||
|
dst.ethernet.w5500.rst = prefs.getUChar(NvsNetworkKeys::ethernet::w5500::rst, dst.ethernet.w5500.rst);
|
||||||
|
|
||||||
|
// LAN8720
|
||||||
|
dst.ethernet.lan8720.mdc = prefs.getUChar(NvsNetworkKeys::ethernet::lan8720::mdc, dst.ethernet.lan8720.mdc);
|
||||||
|
dst.ethernet.lan8720.mdio = prefs.getUChar(NvsNetworkKeys::ethernet::lan8720::mdio, dst.ethernet.lan8720.mdio);
|
||||||
|
dst.ethernet.lan8720.power = prefs.getUChar(NvsNetworkKeys::ethernet::lan8720::power, dst.ethernet.lan8720.power);
|
||||||
|
dst.ethernet.lan8720.phyAddr = prefs.getUChar(NvsNetworkKeys::ethernet::lan8720::phyAddr, dst.ethernet.lan8720.phyAddr);
|
||||||
|
dst.ethernet.lan8720.clockMode = prefs.getUChar(NvsNetworkKeys::ethernet::lan8720::clockMode, dst.ethernet.lan8720.clockMode);
|
||||||
|
|
||||||
|
// Network mode
|
||||||
|
dst.type = static_cast<NetworkMode>(prefs.getUChar(NvsNetworkKeys::type, static_cast<uint8_t>(dst.type)));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool writeToNvs(const NetworkSettings& src) {
|
||||||
|
Preferences prefs;
|
||||||
|
if (!prefs.begin(S_NETWORK)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preferences::putString returns zero on failure and for a stored empty string.
|
||||||
|
const auto putString = [&](const char* key, const char* value) {
|
||||||
|
return prefs.putString(key, value) ||
|
||||||
|
(value[0] == '\0' && prefs.getStringLength(key) == 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool result = true;
|
||||||
|
|
||||||
|
// Hostname
|
||||||
|
result &= putString(NvsNetworkKeys::hostname, src.hostname);
|
||||||
|
|
||||||
|
// IPv4 addressing
|
||||||
|
result &= prefs.putBool(NvsNetworkKeys::staticConfig::dhcp, src.useDhcp) != 0;
|
||||||
|
result &= putString(NvsNetworkKeys::staticConfig::ip, src.staticConfig.ip);
|
||||||
|
result &= putString(NvsNetworkKeys::staticConfig::gateway, src.staticConfig.gateway);
|
||||||
|
result &= putString(NvsNetworkKeys::staticConfig::subnet, src.staticConfig.subnet);
|
||||||
|
result &= putString(NvsNetworkKeys::staticConfig::dns, src.staticConfig.dns);
|
||||||
|
|
||||||
|
// Wi-Fi access point
|
||||||
|
result &= putString(NvsNetworkKeys::wifi::ap::ssid, src.wifi.ap.ssid);
|
||||||
|
result &= putString(NvsNetworkKeys::wifi::ap::password, src.wifi.ap.password);
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::wifi::ap::channel, src.wifi.ap.channel) != 0;
|
||||||
|
|
||||||
|
// Wi-Fi station
|
||||||
|
result &= putString(NvsNetworkKeys::wifi::sta::ssid, src.wifi.sta.ssid);
|
||||||
|
result &= putString(NvsNetworkKeys::wifi::sta::password, src.wifi.sta.password);
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::wifi::sta::channel, src.wifi.sta.channel) != 0;
|
||||||
|
|
||||||
|
// Ethernet controller
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::type, static_cast<uint8_t>(src.ethernet.type)) != 0;
|
||||||
|
|
||||||
|
// W5500
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::w5500::clk, src.ethernet.w5500.clk) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::w5500::mosi, src.ethernet.w5500.mosi) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::w5500::miso, src.ethernet.w5500.miso) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::w5500::cs, src.ethernet.w5500.cs) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::w5500::irq, src.ethernet.w5500.irq) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::w5500::rst, src.ethernet.w5500.rst) != 0;
|
||||||
|
|
||||||
|
// LAN8720
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::lan8720::mdc, src.ethernet.lan8720.mdc) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::lan8720::mdio, src.ethernet.lan8720.mdio) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::lan8720::power, src.ethernet.lan8720.power) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::lan8720::phyAddr, src.ethernet.lan8720.phyAddr) != 0;
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::ethernet::lan8720::clockMode, src.ethernet.lan8720.clockMode) != 0;
|
||||||
|
|
||||||
|
// Network mode
|
||||||
|
result &= prefs.putUChar(NvsNetworkKeys::type, static_cast<uint8_t>(src.type)) != 0;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void requestSaveNetworkSettings() {
|
void requestSaveNetworkSettings() {
|
||||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||||
fsNetworkSettings.update();
|
saveNetworkSettingsRequestedAt = millis();
|
||||||
|
saveNetworkSettingsRequested = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void requestSaveSettings() {
|
void requestSaveSettings() {
|
||||||
@@ -120,15 +277,27 @@ void requestSaveSensorsSettings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Task settingsSaveTask("Settings save", [](void*) {
|
static Task settingsSaveTask("Settings save", [](void*) {
|
||||||
FDstat_t status;
|
if (saveNetworkSettingsRequested) {
|
||||||
{
|
std::unique_lock<std::mutex> lock(settingsSaveMutex);
|
||||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
|
||||||
status = fsNetworkSettings.tick();
|
if (millis() - saveNetworkSettingsRequestedAt.load() >= 1000) {
|
||||||
|
const NetworkSettings snapshot = networkSettings;
|
||||||
|
|
||||||
|
saveNetworkSettingsRequested = false;
|
||||||
|
saveNetworkSettingsRequestedAt = 0;
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
if (writeToNvs(snapshot)) {
|
||||||
|
ESP_LOGI(L_NETWORK_SETTINGS, "Saved");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
requestSaveNetworkSettings();
|
||||||
|
ESP_LOGE(L_NETWORK_SETTINGS, "Write failed!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (status == FD_WRITE) {
|
|
||||||
ESP_LOGI(L_NETWORK_SETTINGS, "Updated");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FDstat_t status;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||||
status = fsSettings.tick();
|
status = fsSettings.tick();
|
||||||
@@ -147,59 +316,61 @@ static Task settingsSaveTask("Settings save", [](void*) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
void saveSettingsNow() {
|
void saveSettingsNow() {
|
||||||
|
// Save general settings.
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||||
fsSettings.updateNow();
|
fsSettings.updateNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save sensor settings.
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(Sensors::mutex);
|
std::lock_guard<std::mutex> lock(Sensors::mutex);
|
||||||
fsSensorsSettings.updateNow();
|
fsSensorsSettings.updateNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always preserve the network configuration
|
// Write the network snapshot to NVS.
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
std::unique_lock<std::mutex> lock(settingsSaveMutex);
|
||||||
if (fsNetworkSettings.updateNow() == FD_FILE_ERR && LittleFS.begin()) {
|
const NetworkSettings snapshot = networkSettings;
|
||||||
fsNetworkSettings.write();
|
lock.unlock();
|
||||||
|
|
||||||
|
if (writeToNvs(snapshot)) {
|
||||||
|
ESP_LOGI(L_NETWORK_SETTINGS, "Saved");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(L_NETWORK_SETTINGS, "Write failed!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialization
|
// Initialization
|
||||||
static bool loadNetworkSettings() {
|
static void loadNetworkSettings() {
|
||||||
bool loaded = false;
|
// Read from legacy network.conf, if found, migrate to NVS and delete the file
|
||||||
fsNetworkSettings.addWithoutWipe(true);
|
if (LegacySettings::read(networkSettings)) {
|
||||||
|
ESP_LOGI(L_NETWORK_SETTINGS, "Loaded legacy");
|
||||||
|
|
||||||
switch (fsNetworkSettings.read()) {
|
if (writeToNvs(networkSettings)) {
|
||||||
case FD_FS_ERR:
|
ESP_LOGI(L_NETWORK_SETTINGS, "Migrated to NVS");
|
||||||
ESP_LOGW(L_NETWORK_SETTINGS, "Filesystem error, load default");
|
LegacySettings::purge(networkSettings);
|
||||||
break;
|
|
||||||
|
|
||||||
case FD_FILE_ERR:
|
} else {
|
||||||
ESP_LOGW(L_NETWORK_SETTINGS, "Bad data, load default");
|
ESP_LOGE(L_NETWORK_SETTINGS, "Write failed!");
|
||||||
break;
|
}
|
||||||
|
|
||||||
case FD_WRITE:
|
|
||||||
ESP_LOGI(L_NETWORK_SETTINGS, "Not found, load default");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FD_ADD:
|
|
||||||
case FD_READ:
|
|
||||||
ESP_LOGI(L_NETWORK_SETTINGS, "Loaded");
|
|
||||||
loaded = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read from NVS, if not found, use defaults
|
||||||
|
if (readFromNvs(networkSettings)) {
|
||||||
|
ESP_LOGI(L_NETWORK_SETTINGS, "Loaded");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ESP_LOGI(L_NETWORK_SETTINGS, "Using defaults");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate hostname if it is empty
|
||||||
if (!networkSettings.hostname[0]) {
|
if (!networkSettings.hostname[0]) {
|
||||||
strcpy(networkSettings.hostname, getChipId("otgateway-").c_str());
|
strcpy(networkSettings.hostname, getChipId("otgateway-").c_str());
|
||||||
requestSaveNetworkSettings();
|
requestSaveNetworkSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
return loaded;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool loadSettings() {
|
static bool loadSettings() {
|
||||||
@@ -416,6 +587,7 @@ JsonVariant toJson(const NetworkSettings& src, JsonVariant dst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SettingResult fromJson(JsonVariantConst src, NetworkSettings& dst) {
|
SettingResult fromJson(JsonVariantConst src, NetworkSettings& dst) {
|
||||||
|
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||||
SettingResult result;
|
SettingResult result;
|
||||||
|
|
||||||
// hostname
|
// hostname
|
||||||
|
|||||||
@@ -591,7 +591,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"note": {
|
"note": {
|
||||||
"disclaimer1": "升级系统文件成功后,所有设置将恢复为默认值!升级前请务必备份配置。",
|
"disclaimer1": "升级文件系统后,除网络设置外,所有设置都将恢复为默认值!请务必在升级前创建备份。",
|
||||||
"disclaimer2": "升级成功后,设备将在15秒后自动重启。"
|
"disclaimer2": "升级成功后,设备将在15秒后自动重启。"
|
||||||
},
|
},
|
||||||
"settingsFile": "设置文件",
|
"settingsFile": "设置文件",
|
||||||
|
|||||||
@@ -591,7 +591,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"note": {
|
"note": {
|
||||||
"disclaimer1": "After a successful upgrade the filesystem, ALL settings will be reset to default values! Save backup before upgrading.",
|
"disclaimer1": "A filesystem upgrade resets ALL settings except network settings to their defaults! Create a backup BEFORE upgrading.",
|
||||||
"disclaimer2": "After a successful upgrade, the device will automatically reboot after 15 seconds."
|
"disclaimer2": "After a successful upgrade, the device will automatically reboot after 15 seconds."
|
||||||
},
|
},
|
||||||
"settingsFile": "Settings file",
|
"settingsFile": "Settings file",
|
||||||
|
|||||||
@@ -591,7 +591,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"note": {
|
"note": {
|
||||||
"disclaimer1": "Dopo un aggiornamento riuscito del filesystem, tutte le impostazioni sono impostate di default! Salva un backup prima di aggiornare.",
|
"disclaimer1": "Dopo l'upgrade del filesystem, TUTTE le impostazioni tranne quelle di rete verranno ripristinate ai valori predefiniti! Crea una copia di backup PRIMA dell'upgrade.",
|
||||||
"disclaimer2": "Dopo un aggiornamento riuscito, il sistema viene automaticamente riavviato dopo 15 secondi."
|
"disclaimer2": "Dopo un aggiornamento riuscito, il sistema viene automaticamente riavviato dopo 15 secondi."
|
||||||
},
|
},
|
||||||
"settingsFile": "Settings file",
|
"settingsFile": "Settings file",
|
||||||
|
|||||||
@@ -591,7 +591,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"note": {
|
"note": {
|
||||||
"disclaimer1": "Na een succesvolle upgrade van het bestandssysteem worden ALLE instellingen teruggezet naar de standaardwaarden! Sla een back-up op voordat u gaat upgraden.",
|
"disclaimer1": "Na de upgrade van het bestandssysteem worden ALLE instellingen behalve de netwerkinstellingen teruggezet naar de standaardwaarden! Maak VÓÓR de upgrade een back-up.",
|
||||||
"disclaimer2": "Na een succesvolle upgrade zal het apparaat automatisch herstarten na 15 seconden."
|
"disclaimer2": "Na een succesvolle upgrade zal het apparaat automatisch herstarten na 15 seconden."
|
||||||
},
|
},
|
||||||
"settingsFile": "Instellingenbestand",
|
"settingsFile": "Instellingenbestand",
|
||||||
|
|||||||
@@ -591,7 +591,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"note": {
|
"note": {
|
||||||
"disclaimer1": "После успешного обновления файловой системы ВСЕ настройки будут сброшены на стандартные! Создайте резервную копию ПЕРЕД обновлением.",
|
"disclaimer1": "После обновления файловой системы ВСЕ настройки, кроме сетевых, будут сброшены на стандартные! Создайте резервную копию ПЕРЕД обновлением.",
|
||||||
"disclaimer2": "После успешного обновления устройство автоматически перезагрузится через 15 секунд."
|
"disclaimer2": "После успешного обновления устройство автоматически перезагрузится через 15 секунд."
|
||||||
},
|
},
|
||||||
"settingsFile": "Файл настроек",
|
"settingsFile": "Файл настроек",
|
||||||
|
|||||||
Reference in new issue
Block a user