configmanager: enhancements for version migration

This commit is contained in:
Klaus K Wilting committed 2020-10-04 19:10:36 +02:00
1 parent 1cbf0d7124
commit 414bc41404
5 files changed
+78 -56

No files matched your search

+72 -32
View File
@@ -16,16 +16,20 @@ static const char TAG[] = __FILE__;
Preferences nvram;
static const char cfgMagicBytes[] = {0x21, 0x76, 0x87, 0x32, 0xf3};
static const uint8_t cfgMagicBytes[] = {0x21, 0x76, 0x87, 0x32, 0xf4};
static const size_t cfgLen = sizeof(cfg), cfgLen2 = sizeof(cfgMagicBytes);
static char buffer[cfgLen + cfgLen2];
static uint8_t buffer[cfgLen + cfgLen2];
// populate runtime config with factory settings
// populate runtime config with device factory settings
static void defaultConfig(configData_t *myconfig) {
char version[10];
snprintf(version, 10, "%-10s", PROGVERSION);
// char version[10];
// snprintf(version, 10, "%-10s", PROGVERSION);
// memcpy(myconfig->version, version, 10); // Firmware version [exactly 10
// chars]
memcpy(myconfig->version, &PROGVERSION,
10); // Firmware version [exactly 10 chars]
// factory settings
// device factory settings
myconfig->loradr = LORADRDEFAULT; // 0-15, lora datarate, see paxcounter.conf
myconfig->txpower = LORATXPOWDEFAULT; // 0-15, lora tx power
myconfig->adrmode = 1; // 0=disabled, 1=enabled
@@ -43,11 +47,10 @@ static void defaultConfig(configData_t *myconfig) {
myconfig->blescan = 1; // 0=disabled, 1=enabled
myconfig->wifiscan = 1; // 0=disabled, 1=enabled
myconfig->wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4)
myconfig->vendorfilter = VENDORFILTER; // 0=disabled, 1=enabled
myconfig->rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%)
myconfig->monitormode = 0; // 0=disabled, 1=enabled
myconfig->payloadmask = PAYLOADMASK; // all payload switched on
memcpy(myconfig->version, version, 10); // Firmware version [exactly 10 chars]
myconfig->vendorfilter = VENDORFILTER; // 0=disabled, 1=enabled
myconfig->rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%)
myconfig->monitormode = 0; // 0=disabled, 1=enabled
myconfig->payloadmask = PAYLOADMASK; // all payload switched on
#ifdef HAS_BME680
// initial BSEC state for BME680 sensor
@@ -55,6 +58,15 @@ static void defaultConfig(configData_t *myconfig) {
#endif
}
// migrate runtime config from earlier version to current
static void migrateConfig(void) {
// currently no migration rules are implemented, we just reset config to
// factory settings
ESP_LOGI(TAG, "Migrating device configuration from %s to %s", cfg.version,
PROGVERSION);
eraseConfig();
}
// save current configuration from RAM to NVRAM
void saveConfig(bool erase) {
ESP_LOGI(TAG, "Storing settings to NVRAM...");
@@ -89,30 +101,58 @@ bool loadConfig() {
ESP_LOGW(TAG, "NVRAM initialized, device starts with factory settings");
eraseConfig();
return true;
}
} else {
// simple check that runtime config data matches
if (nvram.getBytesLength(DEVCONFIG) != (cfgLen + cfgLen2)) {
ESP_LOGE(TAG, "configuration invalid");
return false;
// simple check that runtime config data matches
if (nvram.getBytesLength(DEVCONFIG) != (cfgLen + cfgLen2)) {
ESP_LOGE(TAG, "Configuration invalid");
return false;
}
} else {
// load device runtime config from nvram and copy it to byte array
nvram.getBytes(DEVCONFIG, buffer, cfgLen + cfgLen2);
nvram.end();
// validate configuration by checking magic bytes at end of array
if (memcmp(buffer + cfgLen, &cfgMagicBytes, cfgLen2) != 0) {
ESP_LOGW(TAG, "No configuration found");
return false;
// load device runtime config from nvram and copy it to byte array
nvram.getBytes(DEVCONFIG, buffer, cfgLen + cfgLen2);
nvram.end();
} else {
// copy byte array into runtime cfg struct
memcpy(&cfg, buffer, cfgLen);
ESP_LOGI(TAG, "Runtime configuration loaded");
return true;
}
}
// 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;
}
// copy loaded configuration into runtime cfg struct
memcpy(&cfg, buffer, cfgLen);
ESP_LOGI(TAG, "Runtime configuration v%s loaded", cfg.version);
// check if config version matches current firmware version
switch (version_compare(PROGVERSION, cfg.version)) {
case -1: // device configuration belongs to newer than current firmware
ESP_LOGE(TAG, "Incompatible device configuration, aborting");
return false;
case 1: // device configuration belongs to older than current firmware
ESP_LOGW(TAG, "Device was updated, migrating device configuration");
migrateConfig();
return true;
default: // device configuration version matches current firmware version
return true;
}
}
void eraseConfig(void) { saveConfig(true); }
// helper function to convert strings into lower case
bool comp(char s1, char s2) { return (tolower(s1) < tolower(s2)); }
// helper function to lexicographically compare two versions. Returns 1 if v2
// is smaller, -1 if v1 is smaller, 0 if equal
int version_compare(const String v1, const String v2) {
if (v1 == v2)
return 0;
const char *a1 = v1.c_str(), *a2 = v2.c_str();
if (std::lexicographical_compare(a1, a1 + strlen(a1), a2, a2 + strlen(a2), comp))
return -1;
else
return 1;
}
void eraseConfig(void) { saveConfig(true); }
-18
View File
@@ -328,22 +328,4 @@ void show_progress(unsigned long current, unsigned long size) {
#endif
}
// helper function to convert strings into lower case
bool comp(char s1, char s2) { return tolower(s1) < tolower(s2); }
// helper function to lexicographically compare two versions. Returns 1 if v2 is
// smaller, -1 if v1 is smaller, 0 if equal
int version_compare(const String v1, const String v2) {
if (v1 == v2)
return 0;
const char *a1 = v1.c_str(), *a2 = v2.c_str();
if (lexicographical_compare(a1, a1 + strlen(a1), a2, a2 + strlen(a2), comp))
return -1;
else
return 1;
}
#endif // USE_OTA