configmanager: small fixes / rework

This commit is contained in:
cyberman54 committed 2021-12-27 21:03:41 +01:00
1 parent 6f0678f3b1
commit f8777f99f9
2 files changed
+23 -20

No files matched your search

+22 -19
View File
@@ -98,30 +98,33 @@ void saveConfig(bool erase) {
}
// load configuration from NVRAM into RAM and make it current
bool loadConfig() {
void loadConfig(void) {
int readBytes = 0;
ESP_LOGI(TAG, "Loading device configuration from NVRAM...");
if (!nvram.begin(DEVCONFIG, true)) {
if (nvram.begin(DEVCONFIG, true)) {
// load device runtime config from nvram and copy it to byte array
readBytes = nvram.getBytes(DEVCONFIG, buffer, cfgLen + cfgLen2);
nvram.end();
// check that runtime config data length matches
if (readBytes != cfgLen + cfgLen2) {
ESP_LOGE(TAG, "No valid configuration found");
migrateConfig();
}
} else {
ESP_LOGI(TAG, "NVRAM initialized, device starts with factory settings");
eraseConfig();
}
// simple check that runtime config data matches
// if (nvram.getBytesLength(DEVCONFIG) != (cfgLen + cfgLen2)) {
// ESP_LOGE(TAG, "Configuration invalid");
// return false;
//}
// load device runtime config from nvram and copy it to byte array
nvram.getBytes(DEVCONFIG, buffer, cfgLen + cfgLen2);
nvram.end();
// validate loaded configuration by checking magic bytes at end of array
// if (memcmp(buffer + cfgLen, &cfgMagicBytes, cfgLen2) != 0) {
// ESP_LOGW(TAG, "No configuration found");
// return false;
//}
if (memcmp(buffer + cfgLen, &cfgMagicBytes, cfgLen2) != 0) {
ESP_LOGE(TAG, "Configuration data corrupt");
eraseConfig();
}
// copy loaded configuration into runtime cfg struct
memcpy(&cfg, buffer, cfgLen);
@@ -132,13 +135,13 @@ bool loadConfig() {
case -1: // device configuration belongs to newer than current firmware
ESP_LOGE(TAG, "Incompatible device configuration");
eraseConfig();
return true;
break;
case 1: // device configuration belongs to older than current firmware
ESP_LOGW(TAG, "Device was updated, attempt to migrate configuration");
migrateConfig();
return true;
break;
default: // device configuration version matches current firmware version
return true;
break; // nothing to do here
}
}