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
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user