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
+323
-47
No files matched your search
+214
-42
@@ -1,20 +1,68 @@
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
|
||||
#include <app.h>
|
||||
|
||||
#if USE_ETHERNET
|
||||
#include <ETH.h>
|
||||
#endif
|
||||
#include <FileData.h>
|
||||
#include <LegacySettings.h>
|
||||
#include <LittleFS.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#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;
|
||||
Settings settings;
|
||||
|
||||
Sensors::Settings sensorsSettings[SENSORS_AMOUNT] = {
|
||||
{
|
||||
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 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::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() {
|
||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||
fsNetworkSettings.update();
|
||||
saveNetworkSettingsRequestedAt = millis();
|
||||
saveNetworkSettingsRequested = true;
|
||||
}
|
||||
|
||||
void requestSaveSettings() {
|
||||
@@ -120,15 +277,27 @@ void requestSaveSensorsSettings() {
|
||||
}
|
||||
|
||||
static Task settingsSaveTask("Settings save", [](void*) {
|
||||
FDstat_t status;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||
status = fsNetworkSettings.tick();
|
||||
}
|
||||
if (status == FD_WRITE) {
|
||||
ESP_LOGI(L_NETWORK_SETTINGS, "Updated");
|
||||
if (saveNetworkSettingsRequested) {
|
||||
std::unique_lock<std::mutex> lock(settingsSaveMutex);
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FDstat_t status;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||
status = fsSettings.tick();
|
||||
@@ -147,59 +316,61 @@ static Task settingsSaveTask("Settings save", [](void*) {
|
||||
});
|
||||
|
||||
void saveSettingsNow() {
|
||||
// Save general settings.
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||
fsSettings.updateNow();
|
||||
}
|
||||
|
||||
// Save sensor settings.
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(Sensors::mutex);
|
||||
fsSensorsSettings.updateNow();
|
||||
}
|
||||
|
||||
// Always preserve the network configuration
|
||||
// Write the network snapshot to NVS.
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||
if (fsNetworkSettings.updateNow() == FD_FILE_ERR && LittleFS.begin()) {
|
||||
fsNetworkSettings.write();
|
||||
std::unique_lock<std::mutex> lock(settingsSaveMutex);
|
||||
const NetworkSettings snapshot = networkSettings;
|
||||
lock.unlock();
|
||||
|
||||
if (writeToNvs(snapshot)) {
|
||||
ESP_LOGI(L_NETWORK_SETTINGS, "Saved");
|
||||
|
||||
} else {
|
||||
ESP_LOGE(L_NETWORK_SETTINGS, "Write failed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialization
|
||||
static bool loadNetworkSettings() {
|
||||
bool loaded = false;
|
||||
fsNetworkSettings.addWithoutWipe(true);
|
||||
static void loadNetworkSettings() {
|
||||
// Read from legacy network.conf, if found, migrate to NVS and delete the file
|
||||
if (LegacySettings::read(networkSettings)) {
|
||||
ESP_LOGI(L_NETWORK_SETTINGS, "Loaded legacy");
|
||||
|
||||
switch (fsNetworkSettings.read()) {
|
||||
case FD_FS_ERR:
|
||||
ESP_LOGW(L_NETWORK_SETTINGS, "Filesystem error, load default");
|
||||
break;
|
||||
if (writeToNvs(networkSettings)) {
|
||||
ESP_LOGI(L_NETWORK_SETTINGS, "Migrated to NVS");
|
||||
LegacySettings::purge(networkSettings);
|
||||
|
||||
case FD_FILE_ERR:
|
||||
ESP_LOGW(L_NETWORK_SETTINGS, "Bad data, load default");
|
||||
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;
|
||||
} else {
|
||||
ESP_LOGE(L_NETWORK_SETTINGS, "Write failed!");
|
||||
}
|
||||
}
|
||||
|
||||
// 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]) {
|
||||
strcpy(networkSettings.hostname, getChipId("otgateway-").c_str());
|
||||
requestSaveNetworkSettings();
|
||||
}
|
||||
|
||||
return loaded;
|
||||
}
|
||||
|
||||
static bool loadSettings() {
|
||||
@@ -416,6 +587,7 @@ JsonVariant toJson(const NetworkSettings& src, JsonVariant dst) {
|
||||
}
|
||||
|
||||
SettingResult fromJson(JsonVariantConst src, NetworkSettings& dst) {
|
||||
std::lock_guard<std::mutex> lock(settingsSaveMutex);
|
||||
SettingResult result;
|
||||
|
||||
// hostname
|
||||
|
||||
Reference in new issue
Block a user