rewrite configmanager (experimental)

This commit is contained in:
Klaus K Wilting 2020-10-02 23:07:24 +02:00
parent 31765e3b65
commit ef84966917
3 changed files with 79 additions and 340 deletions

View File

@ -1,11 +1,11 @@
#ifndef _CONFIGMANAGER_H #ifndef _CONFIGMANAGER_H
#define _CONFIGMANAGER_H #define _CONFIGMANAGER_H
#include <nvs.h> #include <Preferences.h>
#include <nvs_flash.h> #include "globals.h"
void eraseConfig(void); void saveConfig(bool erase = false);
void saveConfig(void);
void loadConfig(void); void loadConfig(void);
void eraseConfig(void);
#endif #endif

View File

@ -60,7 +60,7 @@ enum runmode_t {
}; };
// Struct holding devices's runtime configuration // Struct holding devices's runtime configuration
typedef struct { typedef struct __attribute__((packed)) {
uint8_t loradr; // 0-15, lora datarate uint8_t loradr; // 0-15, lora datarate
uint8_t txpower; // 2-15, lora tx power uint8_t txpower; // 2-15, lora tx power
uint8_t adrmode; // 0=disabled, 1=enabled uint8_t adrmode; // 0=disabled, 1=enabled
@ -80,8 +80,10 @@ typedef struct {
uint8_t runmode; // 0=normal, 1=update uint8_t runmode; // 0=normal, 1=update
uint8_t payloadmask; // bitswitches for payload data uint8_t payloadmask; // bitswitches for payload data
char version[10]; // Firmware version char version[10]; // Firmware version
#ifdef HAS_BME680
uint8_t uint8_t
bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; // BSEC state for BME680 sensor bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; // BSEC state for BME680 sensor
#endif
} configData_t; } configData_t;
// Struct holding payload for data send queue // Struct holding payload for data send queue

View File

@ -4,362 +4,99 @@
#include "configmanager.h" #include "configmanager.h"
// Local logging tag // Local logging tag
static const char TAG[] = "flash"; static const char TAG[] = __FILE__;
nvs_handle my_handle; #define PAYLOADMASK \
esp_err_t err; ((GPS_DATA | ALARM_DATA | MEMS_DATA | COUNT_DATA | SENSOR1_DATA | \
SENSOR2_DATA | SENSOR3_DATA) & \
(~BATT_DATA))
#define PAYLOADMASK \ // namespace for device runtime preferences
((GPS_DATA | ALARM_DATA | MEMS_DATA | COUNT_DATA | \ #define DEVCONFIG "paxcntcfg"
SENSOR1_DATA | SENSOR2_DATA | SENSOR3_DATA) & \
(~BATT_DATA) )
// populate cfg vars with factory settings Preferences nvram;
void defaultConfig() {
cfg.loradr = LORADRDEFAULT; // 0-15, lora datarate, see paxcounter.conf // populate runtime config with factory settings
cfg.txpower = LORATXPOWDEFAULT; // 0-15, lora tx power void defaultConfig(configData_t *myconfig) {
cfg.adrmode = 1; // 0=disabled, 1=enabled char version[10];
cfg.screensaver = 0; // 0=disabled, 1=enabled snprintf(version, 10, "%-10s", PROGVERSION);
cfg.screenon = 1; // 0=disabled, 1=enabled
cfg.countermode = COUNTERMODE; // 0=cyclic, 1=cumulative, 2=cyclic confirmed myconfig->loradr = LORADRDEFAULT; // 0-15, lora datarate, see paxcounter.conf
cfg.rssilimit = 0; // threshold for rssilimiter, negative value! myconfig->txpower = LORATXPOWDEFAULT; // 0-15, lora tx power
cfg.sendcycle = SENDCYCLE; // payload send cycle [seconds/2] myconfig->adrmode = 1; // 0=disabled, 1=enabled
cfg.wifichancycle = myconfig->screensaver = 0; // 0=disabled, 1=enabled
myconfig->screenon = 1; // 0=disabled, 1=enabled
myconfig->countermode =
COUNTERMODE; // 0=cyclic, 1=cumulative, 2=cyclic confirmed
myconfig->rssilimit = 0; // threshold for rssilimiter, negative value!
myconfig->sendcycle = SENDCYCLE; // payload send cycle [seconds/2]
myconfig->wifichancycle =
WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100] WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100]
cfg.blescantime = myconfig->blescantime =
BLESCANINTERVAL / BLESCANINTERVAL /
10; // BT channel scan cycle [seconds/100], default 1 (= 10ms) 10; // BT channel scan cycle [seconds/100], default 1 (= 10ms)
cfg.blescan = 1; // 0=disabled, 1=enabled myconfig->blescan = 1; // 0=disabled, 1=enabled
cfg.wifiscan = 1; // 0=disabled, 1=enabled myconfig->wifiscan = 1; // 0=disabled, 1=enabled
cfg.wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4) myconfig->wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4)
cfg.vendorfilter = VENDORFILTER; // 0=disabled, 1=enabled myconfig->vendorfilter = VENDORFILTER; // 0=disabled, 1=enabled
cfg.rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%) myconfig->rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%)
cfg.monitormode = 0; // 0=disabled, 1=enabled myconfig->monitormode = 0; // 0=disabled, 1=enabled
cfg.payloadmask = PAYLOADMASK; // all payload switched on myconfig->payloadmask = PAYLOADMASK; // all payload switched on
cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] = { memcpy(myconfig->version, version, 10); // Firmware version [exactly 10 chars]
0}; // init BSEC state for BME680 sensor #ifdef HAS_BME680
// initial BSEC state for BME680 sensor
strncpy(cfg.version, PROGVERSION, sizeof(cfg.version) - 1); myconfig->bsecstate[BSEC_MAX_STATE_BLOB_SIZE] = {0};
} #endif
void open_storage() {
err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
// Open
ESP_LOGI(TAG, "Opening NVS");
err = nvs_open("config", NVS_READWRITE, &my_handle);
if (err != ESP_OK)
ESP_LOGI(TAG, "Error (%d) opening NVS handle", err);
else
ESP_LOGI(TAG, "Done");
}
// erase all keys and values in NVRAM
void eraseConfig() {
ESP_LOGI(TAG, "Clearing settings in NVS");
open_storage();
if (err == ESP_OK) {
nvs_erase_all(my_handle);
nvs_commit(my_handle);
nvs_close(my_handle);
ESP_LOGI(TAG, "Done");
} else {
ESP_LOGW(TAG, "NVS erase failed");
}
} }
// save current configuration from RAM to NVRAM // save current configuration from RAM to NVRAM
void saveConfig() { void saveConfig(bool erase) {
ESP_LOGI(TAG, "Storing settings in NVS"); ESP_LOGI(TAG, "Storing settings in NVRAM");
open_storage();
if (err == ESP_OK) {
uint8_t flash8 = 0;
int16_t flash16 = 0;
size_t required_size;
uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE + 1];
char storedversion[10];
if (nvs_get_blob(my_handle, "bsecstate", bsecstate_buffer, nvram.begin(DEVCONFIG, false);
&required_size) != ESP_OK ||
memcmp(bsecstate_buffer, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE + 1) !=
0)
nvs_set_blob(my_handle, "bsecstate", cfg.bsecstate,
BSEC_MAX_STATE_BLOB_SIZE + 1);
if (nvs_get_str(my_handle, "version", storedversion, &required_size) != if (erase) {
ESP_OK || ESP_LOGI(TAG, "Resetting NVRAM to factory settings");
strcmp(storedversion, cfg.version) != 0) nvram.clear();
nvs_set_str(my_handle, "version", cfg.version); defaultConfig(&cfg);
if (nvs_get_u8(my_handle, "loradr", &flash8) != ESP_OK ||
flash8 != cfg.loradr)
nvs_set_u8(my_handle, "loradr", cfg.loradr);
if (nvs_get_u8(my_handle, "txpower", &flash8) != ESP_OK ||
flash8 != cfg.txpower)
nvs_set_u8(my_handle, "txpower", cfg.txpower);
if (nvs_get_u8(my_handle, "adrmode", &flash8) != ESP_OK ||
flash8 != cfg.adrmode)
nvs_set_u8(my_handle, "adrmode", cfg.adrmode);
if (nvs_get_u8(my_handle, "screensaver", &flash8) != ESP_OK ||
flash8 != cfg.screensaver)
nvs_set_u8(my_handle, "screensaver", cfg.screensaver);
if (nvs_get_u8(my_handle, "screenon", &flash8) != ESP_OK ||
flash8 != cfg.screenon)
nvs_set_u8(my_handle, "screenon", cfg.screenon);
if (nvs_get_u8(my_handle, "countermode", &flash8) != ESP_OK ||
flash8 != cfg.countermode)
nvs_set_u8(my_handle, "countermode", cfg.countermode);
if (nvs_get_u8(my_handle, "sendcycle", &flash8) != ESP_OK ||
flash8 != cfg.sendcycle)
nvs_set_u8(my_handle, "sendcycle", cfg.sendcycle);
if (nvs_get_u8(my_handle, "wifichancycle", &flash8) != ESP_OK ||
flash8 != cfg.wifichancycle)
nvs_set_u8(my_handle, "wifichancycle", cfg.wifichancycle);
if (nvs_get_u8(my_handle, "blescantime", &flash8) != ESP_OK ||
flash8 != cfg.blescantime)
nvs_set_u8(my_handle, "blescantime", cfg.blescantime);
if (nvs_get_u8(my_handle, "blescanmode", &flash8) != ESP_OK ||
flash8 != cfg.blescan)
nvs_set_u8(my_handle, "blescanmode", cfg.blescan);
if (nvs_get_u8(my_handle, "wifiscanmode", &flash8) != ESP_OK ||
flash8 != cfg.wifiscan)
nvs_set_u8(my_handle, "wifiscanmode", cfg.wifiscan);
if (nvs_get_u8(my_handle, "wifiant", &flash8) != ESP_OK ||
flash8 != cfg.wifiant)
nvs_set_u8(my_handle, "wifiant", cfg.wifiant);
if (nvs_get_u8(my_handle, "vendorfilter", &flash8) != ESP_OK ||
flash8 != cfg.vendorfilter)
nvs_set_u8(my_handle, "vendorfilter", cfg.vendorfilter);
if (nvs_get_u8(my_handle, "rgblum", &flash8) != ESP_OK ||
flash8 != cfg.rgblum)
nvs_set_u8(my_handle, "rgblum", cfg.rgblum);
if (nvs_get_u8(my_handle, "payloadmask", &flash8) != ESP_OK ||
flash8 != cfg.payloadmask)
nvs_set_u8(my_handle, "payloadmask", cfg.payloadmask);
if (nvs_get_u8(my_handle, "monitormode", &flash8) != ESP_OK ||
flash8 != cfg.monitormode)
nvs_set_u8(my_handle, "monitormode", cfg.monitormode);
if (nvs_get_i16(my_handle, "rssilimit", &flash16) != ESP_OK ||
flash16 != cfg.rssilimit)
nvs_set_i16(my_handle, "rssilimit", cfg.rssilimit);
err = nvs_commit(my_handle);
nvs_close(my_handle);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Done");
} else {
ESP_LOGW(TAG, "NVS config write failed");
}
} else {
ESP_LOGW(TAG, "Error (%d) opening NVS handle", err);
} }
}
// set and save cfg.version // Copy device runtime config cfg to byte array
void migrateVersion() { const size_t cfgLen = sizeof(configData_t);
snprintf(cfg.version, 10, "%s", PROGVERSION); char buffer[cfgLen];
ESP_LOGI(TAG, "version set to %s", cfg.version); memcpy(buffer, &cfg, cfgLen);
saveConfig();
// save byte array to NVRAM
nvram.putBytes(DEVCONFIG, buffer, cfgLen);
nvram.end();
} }
// load configuration from NVRAM into RAM and make it current // load configuration from NVRAM into RAM and make it current
void loadConfig() { void loadConfig() {
defaultConfig(); // start with factory settings
ESP_LOGI(TAG, "Reading settings from NVS");
open_storage();
if (err != ESP_OK) {
ESP_LOGW(TAG, "Error (%d) opening NVS handle, storing defaults", err);
saveConfig();
} // saves factory settings to NVRAM
else {
uint8_t flash8 = 0;
int16_t flash16 = 0;
size_t required_size;
// check if configuration stored in NVRAM matches PROGVERSION ESP_LOGI(TAG, "Loading runtime settings from NVS");
if (nvs_get_str(my_handle, "version", NULL, &required_size) == ESP_OK) {
nvs_get_str(my_handle, "version", cfg.version, &required_size); if (!nvram.begin(DEVCONFIG, true)) {
ESP_LOGI(TAG, "NVRAM settings version = %s", cfg.version); ESP_LOGI(TAG, "Initializing NVRAM");
if (strcmp(cfg.version, PROGVERSION)) { eraseConfig();
ESP_LOGI(TAG, "migrating NVRAM settings to new version %s", } else {
PROGVERSION); // simple check that runtime config data matches
nvs_close(my_handle); const size_t cfgLen = nvram.getBytesLength(DEVCONFIG);
migrateVersion();
} if (cfgLen % sizeof(configData_t)) {
} else { ESP_LOGW(TAG, "NVRAM settings invalid");
ESP_LOGI(TAG, "new version %s, deleting NVRAM settings", PROGVERSION);
nvs_close(my_handle);
eraseConfig(); eraseConfig();
migrateVersion();
}
// populate pre set defaults with current values from NVRAM
if (nvs_get_blob(my_handle, "bsecstate", NULL, &required_size) == ESP_OK) {
nvs_get_blob(my_handle, "bsecstate", cfg.bsecstate, &required_size);
ESP_LOGI(TAG, "bsecstate = %d", cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE]);
};
if (nvs_get_u8(my_handle, "loradr", &flash8) == ESP_OK) {
cfg.loradr = flash8;
ESP_LOGI(TAG, "loradr = %d", flash8);
} else { } else {
ESP_LOGI(TAG, "loradr set to default %d", cfg.loradr);
saveConfig();
}
if (nvs_get_u8(my_handle, "txpower", &flash8) == ESP_OK) { // load device runtime config from nvram and copy it to byte array
cfg.txpower = flash8; char *buffer = new char[cfgLen];
ESP_LOGI(TAG, "txpower = %d", flash8); nvram.getBytes(DEVCONFIG, buffer, cfgLen);
} else { nvram.end();
ESP_LOGI(TAG, "txpower set to default %d", cfg.txpower);
saveConfig();
}
if (nvs_get_u8(my_handle, "adrmode", &flash8) == ESP_OK) { // copy the byte array into runtime cfg struct
cfg.adrmode = flash8; memcpy(&cfg, buffer, cfgLen);
ESP_LOGI(TAG, "adrmode = %d", flash8);
} else {
ESP_LOGI(TAG, "adrmode set to default %d", cfg.adrmode);
saveConfig();
} }
if (nvs_get_u8(my_handle, "screensaver", &flash8) == ESP_OK) {
cfg.screensaver = flash8;
ESP_LOGI(TAG, "screensaver = %d", flash8);
} else {
ESP_LOGI(TAG, "screensaver set to default %d", cfg.screensaver);
saveConfig();
}
if (nvs_get_u8(my_handle, "screenon", &flash8) == ESP_OK) {
cfg.screenon = flash8;
ESP_LOGI(TAG, "screenon = %d", flash8);
} else {
ESP_LOGI(TAG, "screenon set to default %d", cfg.screenon);
saveConfig();
}
if (nvs_get_u8(my_handle, "countermode", &flash8) == ESP_OK) {
cfg.countermode = flash8;
ESP_LOGI(TAG, "countermode = %d", flash8);
} else {
ESP_LOGI(TAG, "countermode set to default %d", cfg.countermode);
saveConfig();
}
if (nvs_get_u8(my_handle, "sendcycle", &flash8) == ESP_OK) {
cfg.sendcycle = flash8;
ESP_LOGI(TAG, "sendcycle = %d", flash8);
} else {
ESP_LOGI(TAG, "Payload send cycle set to default %d", cfg.sendcycle);
saveConfig();
}
if (nvs_get_u8(my_handle, "wifichancycle", &flash8) == ESP_OK) {
cfg.wifichancycle = flash8;
ESP_LOGI(TAG, "wifichancycle = %d", flash8);
} else {
ESP_LOGI(TAG, "WIFI channel cycle set to default %d", cfg.wifichancycle);
saveConfig();
}
if (nvs_get_u8(my_handle, "wifiant", &flash8) == ESP_OK) {
cfg.wifiant = flash8;
ESP_LOGI(TAG, "wifiantenna = %d", flash8);
} else {
ESP_LOGI(TAG, "WIFI antenna switch set to default %d", cfg.wifiant);
saveConfig();
}
if (nvs_get_u8(my_handle, "vendorfilter", &flash8) == ESP_OK) {
cfg.vendorfilter = flash8;
ESP_LOGI(TAG, "vendorfilter = %d", flash8);
} else {
ESP_LOGI(TAG, "Vendorfilter mode set to default %d", cfg.vendorfilter);
saveConfig();
}
if (nvs_get_u8(my_handle, "rgblum", &flash8) == ESP_OK) {
cfg.rgblum = flash8;
ESP_LOGI(TAG, "rgbluminosity = %d", flash8);
} else {
ESP_LOGI(TAG, "RGB luminosity set to default %d", cfg.rgblum);
saveConfig();
}
if (nvs_get_u8(my_handle, "blescantime", &flash8) == ESP_OK) {
cfg.blescantime = flash8;
ESP_LOGI(TAG, "blescantime = %d", flash8);
} else {
ESP_LOGI(TAG, "BLEscantime set to default %d", cfg.blescantime);
saveConfig();
}
if (nvs_get_u8(my_handle, "blescanmode", &flash8) == ESP_OK) {
cfg.blescan = flash8;
ESP_LOGI(TAG, "BLEscanmode = %d", flash8);
} else {
ESP_LOGI(TAG, "BLEscanmode set to default %d", cfg.blescan);
saveConfig();
}
if (nvs_get_u8(my_handle, "wifiscanmode", &flash8) == ESP_OK) {
cfg.wifiscan = flash8;
ESP_LOGI(TAG, "WIFIscanmode = %d", flash8);
} else {
ESP_LOGI(TAG, "WIFIscanmode set to default %d", cfg.wifiscan);
saveConfig();
}
if (nvs_get_i16(my_handle, "rssilimit", &flash16) == ESP_OK) {
cfg.rssilimit = flash16;
ESP_LOGI(TAG, "rssilimit = %d", flash16);
} else {
ESP_LOGI(TAG, "rssilimit set to default %d", cfg.rssilimit);
saveConfig();
}
if (nvs_get_u8(my_handle, "payloadmask", &flash8) == ESP_OK) {
cfg.payloadmask = flash8;
ESP_LOGI(TAG, "payloadmask = %hhu", flash8);
} else {
ESP_LOGI(TAG, "payloadmask set to default %hhu", cfg.payloadmask);
saveConfig();
}
if (nvs_get_u8(my_handle, "monitormode", &flash8) == ESP_OK) {
cfg.monitormode = flash8;
ESP_LOGI(TAG, "Monitor mode = %d", flash8);
} else {
ESP_LOGI(TAG, "Monitor mode set to default %d", cfg.monitormode);
saveConfig();
}
nvs_close(my_handle);
ESP_LOGI(TAG, "Done");
} }
} // loadConfig() }
void eraseConfig(void) { saveConfig(true); }