refactor: rework after #312

This commit is contained in:
Yurii committed 2026-12-06 16:13:00 +03:00
1 parent 75e22e92e9
commit 96473129a8
9 files changed
+111 -128

No files matched your search

+9 -10
View File
@@ -106,6 +106,12 @@ static FileData fsSensorsSettings(&LittleFS, "/sensors.conf", 'e', &sensorsSetti
// FileData requests can arrive while the save task is running.
static std::mutex settingsSaveMutex;
// Kept in NVS rather than in Settings, so the settings file layout does not change.
static constexpr char API_TOKEN_NAMESPACE[] = "portal";
static constexpr char API_TOKEN_KEY[] = "api_token";
static constexpr size_t API_TOKEN_LENGTH = 32;
static char apiToken[API_TOKEN_LENGTH + 1] = "";
void requestSaveNetworkSettings() {
std::lock_guard<std::mutex> lock(settingsSaveMutex);
fsNetworkSettings.update();
@@ -169,13 +175,6 @@ void saveSettingsNow() {
}
// API token
// Kept in NVS rather than in Settings, so the settings file layout does not change.
static constexpr char API_TOKEN_NAMESPACE[] = "otgw";
static constexpr char API_TOKEN_KEY[] = "api_token";
static constexpr size_t API_TOKEN_LENGTH = 32;
static char apiToken[API_TOKEN_LENGTH + 1] = "";
static bool isApiTokenValid(const char* token) {
if (strlen(token) != API_TOKEN_LENGTH) {
return false;
@@ -210,7 +209,7 @@ const char* getApiToken() {
return apiToken;
}
bool regenerateApiToken() {
bool refreshApiToken() {
uint8_t bytes[API_TOKEN_LENGTH / 2];
esp_fill_random(bytes, sizeof(bytes));
@@ -232,7 +231,7 @@ static void loadApiToken() {
nvs_close(handle);
}
if (!isApiTokenValid(apiToken) && !regenerateApiToken()) {
if (!isApiTokenValid(apiToken) && !refreshApiToken()) {
apiToken[0] = '\0';
ESP_LOGW(L_SETTINGS, "Failed to create the API token");
}
@@ -952,7 +951,7 @@ SettingResult fromJson(JsonVariantConst src, PortalSettings& dst) {
}
}
// Accepted for restoring a backup; the portal shows the token read-only.
// Both the portal and backup restore accept a hexadecimal token.
if (src[S_API_TOKEN].is<JsonString>()) {
const JsonString value = src[S_API_TOKEN].as<JsonString>();