ESP32-PaxCounter/src/configmanager.cpp

362 lines
12 KiB
C++
Raw Normal View History

2018-03-18 19:45:17 +01:00
/* configmanager persists runtime configuration using NVRAM of ESP32*/
#include "globals.h"
#include <nvs.h>
#include <nvs_flash.h>
// Local logging tag
2018-06-02 18:28:01 +02:00
static const char TAG[] = "flash";
2018-03-18 19:45:17 +01:00
nvs_handle my_handle;
esp_err_t err;
// populate cfg vars with factory settings
void defaultConfig() {
cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora sf, see pacounter.conf
cfg.txpower = 15; // 2-15, lora tx power
cfg.adrmode = 1; // 0=disabled, 1=enabled
cfg.screensaver = 0; // 0=disabled, 1=enabled
cfg.screenon = 1; // 0=disabled, 1=enabled
cfg.countermode = 0; // 0=cyclic, 1=cumulative, 2=cyclic confirmed
cfg.rssilimit = 0; // threshold for rssilimiter, negative value!
2019-03-03 13:07:48 +01:00
cfg.sendcycle = SENDCYCLE; // payload send cycle [seconds/2]
cfg.wifichancycle =
WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100]
cfg.blescantime =
BLESCANINTERVAL /
10; // BT channel scan cycle [seconds/100], default 1 (= 10ms)
2018-12-02 14:08:50 +01:00
cfg.blescan = 0; // 0=disabled, 1=enabled
cfg.wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4)
cfg.vendorfilter = 1; // 0=disabled, 1=enabled
cfg.rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%)
cfg.monitormode = 0; // 0=disabled, 1=enabled
2018-09-15 18:59:20 +02:00
cfg.runmode = 0; // 0=normal, 1=update
2019-03-16 12:02:12 +01:00
cfg.payloadmask = 0x08; // transmit counter data only
2019-01-01 21:23:45 +01:00
cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] = {
0}; // init BSEC state for BME680 sensor
strncpy(cfg.version, PROGVERSION, sizeof(cfg.version) - 1);
2018-03-18 19:45:17 +01:00
}
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());
2018-03-18 19:45:17 +01:00
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");
2018-03-18 19:45:17 +01:00
}
// 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");
}
2018-03-18 19:45:17 +01:00
}
// save current configuration from RAM to NVRAM
void saveConfig() {
ESP_LOGI(TAG, "Storing settings in NVS");
open_storage();
if (err == ESP_OK) {
int8_t flash8 = 0;
int16_t flash16 = 0;
size_t required_size;
2019-01-01 21:23:45 +01:00
uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE + 1];
char storedversion[10];
2019-01-01 21:23:45 +01:00
if (nvs_get_blob(my_handle, "bsecstate", bsecstate_buffer,
&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);
2018-03-31 23:30:32 +02:00
if (nvs_get_str(my_handle, "version", storedversion, &required_size) !=
ESP_OK ||
strcmp(storedversion, cfg.version) != 0)
nvs_set_str(my_handle, "version", cfg.version);
2018-03-31 23:30:32 +02:00
if (nvs_get_i8(my_handle, "lorasf", &flash8) != ESP_OK ||
flash8 != cfg.lorasf)
nvs_set_i8(my_handle, "lorasf", cfg.lorasf);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "txpower", &flash8) != ESP_OK ||
flash8 != cfg.txpower)
nvs_set_i8(my_handle, "txpower", cfg.txpower);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "adrmode", &flash8) != ESP_OK ||
flash8 != cfg.adrmode)
nvs_set_i8(my_handle, "adrmode", cfg.adrmode);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "screensaver", &flash8) != ESP_OK ||
flash8 != cfg.screensaver)
nvs_set_i8(my_handle, "screensaver", cfg.screensaver);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "screenon", &flash8) != ESP_OK ||
flash8 != cfg.screenon)
nvs_set_i8(my_handle, "screenon", cfg.screenon);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "countermode", &flash8) != ESP_OK ||
flash8 != cfg.countermode)
nvs_set_i8(my_handle, "countermode", cfg.countermode);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "sendcycle", &flash8) != ESP_OK ||
flash8 != cfg.sendcycle)
nvs_set_i8(my_handle, "sendcycle", cfg.sendcycle);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "wifichancycle", &flash8) != ESP_OK ||
flash8 != cfg.wifichancycle)
nvs_set_i8(my_handle, "wifichancycle", cfg.wifichancycle);
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "blescantime", &flash8) != ESP_OK ||
flash8 != cfg.blescantime)
nvs_set_i8(my_handle, "blescantime", cfg.blescantime);
if (nvs_get_i8(my_handle, "blescanmode", &flash8) != ESP_OK ||
flash8 != cfg.blescan)
nvs_set_i8(my_handle, "blescanmode", cfg.blescan);
if (nvs_get_i8(my_handle, "wifiant", &flash8) != ESP_OK ||
flash8 != cfg.wifiant)
nvs_set_i8(my_handle, "wifiant", cfg.wifiant);
2018-03-31 23:30:32 +02:00
if (nvs_get_i8(my_handle, "vendorfilter", &flash8) != ESP_OK ||
flash8 != cfg.vendorfilter)
nvs_set_i8(my_handle, "vendorfilter", cfg.vendorfilter);
2018-04-15 12:12:06 +02:00
if (nvs_get_i8(my_handle, "rgblum", &flash8) != ESP_OK ||
flash8 != cfg.rgblum)
nvs_set_i8(my_handle, "rgblum", cfg.rgblum);
2018-03-18 19:45:17 +01:00
2018-11-19 00:41:15 +01:00
if (nvs_get_i8(my_handle, "payloadmask", &flash8) != ESP_OK ||
flash8 != cfg.payloadmask)
nvs_set_i8(my_handle, "payloadmask", cfg.payloadmask);
2018-06-08 22:41:37 +02:00
if (nvs_get_i8(my_handle, "monitormode", &flash8) != ESP_OK ||
flash8 != cfg.monitormode)
nvs_set_i8(my_handle, "monitormode", cfg.monitormode);
2018-09-15 18:59:20 +02:00
if (nvs_get_i8(my_handle, "runmode", &flash8) != ESP_OK ||
flash8 != cfg.runmode)
nvs_set_i8(my_handle, "runmode", cfg.runmode);
if (nvs_get_i16(my_handle, "rssilimit", &flash16) != ESP_OK ||
flash16 != cfg.rssilimit)
nvs_set_i16(my_handle, "rssilimit", cfg.rssilimit);
2018-03-31 23:30:32 +02:00
err = nvs_commit(my_handle);
nvs_close(my_handle);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Done");
2018-03-18 19:45:17 +01:00
} else {
ESP_LOGW(TAG, "NVS config write failed");
2018-03-18 19:45:17 +01:00
}
} else {
ESP_LOGW(TAG, "Error (%d) opening NVS handle", err);
}
2018-03-18 19:45:17 +01:00
}
// set and save cfg.version
void migrateVersion() {
sprintf(cfg.version, "%s", PROGVERSION);
ESP_LOGI(TAG, "version set to %s", cfg.version);
saveConfig();
}
// load configuration from NVRAM into RAM and make it current
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
2018-03-18 19:45:17 +01:00
else {
int8_t flash8 = 0;
2018-03-18 19:45:17 +01:00
int16_t flash16 = 0;
size_t required_size;
2018-03-31 23:30:32 +02:00
2018-03-18 19:45:17 +01:00
// check if configuration stored in NVRAM matches PROGVERSION
if (nvs_get_str(my_handle, "version", NULL, &required_size) == ESP_OK) {
2018-03-18 19:45:17 +01:00
nvs_get_str(my_handle, "version", cfg.version, &required_size);
ESP_LOGI(TAG, "NVRAM settings version = %s", cfg.version);
if (strcmp(cfg.version, PROGVERSION)) {
ESP_LOGI(TAG, "migrating NVRAM settings to new version %s",
PROGVERSION);
2018-03-18 19:45:17 +01:00
nvs_close(my_handle);
migrateVersion();
}
} else {
ESP_LOGI(TAG, "new version %s, deleting NVRAM settings", PROGVERSION);
nvs_close(my_handle);
eraseConfig();
migrateVersion();
2018-03-18 19:45:17 +01:00
}
// populate pre set defaults with current values from NVRAM
2019-01-01 21:23:45 +01:00
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",
2019-01-01 21:23:45 +01:00
cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE]);
};
if (nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.lorasf = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "lorasf = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "lorasf set to default %d", cfg.lorasf);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "txpower", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.txpower = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "txpower = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "txpower set to default %d", cfg.txpower);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "adrmode", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.adrmode = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "adrmode = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "adrmode set to default %d", cfg.adrmode);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "screensaver", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.screensaver = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "screensaver = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "screensaver set to default %d", cfg.screensaver);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "screenon", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.screenon = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "screenon = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "screenon set to default %d", cfg.screenon);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "countermode", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.countermode = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "countermode = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "countermode set to default %d", cfg.countermode);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "sendcycle", &flash8) == ESP_OK) {
2018-04-28 13:07:35 +02:00
cfg.sendcycle = flash8;
ESP_LOGI(TAG, "sendcycle = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-28 13:07:35 +02:00
ESP_LOGI(TAG, "Payload send cycle set to default %d", cfg.sendcycle);
2018-03-18 19:45:17 +01:00
saveConfig();
}
if (nvs_get_i8(my_handle, "wifichancycle", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.wifichancycle = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "wifichancycle = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "WIFI channel cycle set to default %d", cfg.wifichancycle);
2018-03-18 19:45:17 +01:00
saveConfig();
}
2018-03-31 23:30:32 +02:00
if (nvs_get_i8(my_handle, "wifiant", &flash8) == ESP_OK) {
cfg.wifiant = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "wifiantenna = %d", flash8);
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "WIFI antenna switch set to default %d", cfg.wifiant);
saveConfig();
}
2018-03-18 19:45:17 +01:00
if (nvs_get_i8(my_handle, "vendorfilter", &flash8) == ESP_OK) {
2018-04-15 12:12:06 +02:00
cfg.vendorfilter = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "vendorfilter = %d", flash8);
2018-04-15 12:12:06 +02:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "Vendorfilter mode set to default %d", cfg.vendorfilter);
2018-04-15 12:12:06 +02:00
saveConfig();
}
if (nvs_get_i8(my_handle, "rgblum", &flash8) == ESP_OK) {
2018-03-31 23:30:32 +02:00
cfg.rgblum = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "rgbluminosity = %d", flash8);
2018-03-31 23:30:32 +02:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "RGB luminosity set to default %d", cfg.rgblum);
2018-03-31 23:30:32 +02:00
saveConfig();
}
if (nvs_get_i8(my_handle, "blescantime", &flash8) == ESP_OK) {
cfg.blescantime = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "blescantime = %d", flash8);
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "BLEscantime set to default %d", cfg.blescantime);
saveConfig();
}
if (nvs_get_i8(my_handle, "blescanmode", &flash8) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.blescan = flash8;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "BLEscanmode = %d", flash8);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "BLEscanmode set to default %d", cfg.blescan);
2018-03-18 19:45:17 +01:00
saveConfig();
}
2018-03-31 23:30:32 +02:00
if (nvs_get_i16(my_handle, "rssilimit", &flash16) == ESP_OK) {
2018-03-18 19:45:17 +01:00
cfg.rssilimit = flash16;
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "rssilimit = %d", flash16);
2018-03-18 19:45:17 +01:00
} else {
2018-04-27 21:29:46 +02:00
ESP_LOGI(TAG, "rssilimit set to default %d", cfg.rssilimit);
2018-03-18 19:45:17 +01:00
saveConfig();
}
2018-03-31 23:30:32 +02:00
2018-11-19 00:41:15 +01:00
if (nvs_get_i8(my_handle, "payloadmask", &flash8) == ESP_OK) {
cfg.payloadmask = flash8;
2019-01-01 21:23:45 +01:00
ESP_LOGI(TAG, "payloadmask = %d", flash8);
} else {
2019-01-01 21:23:45 +01:00
ESP_LOGI(TAG, "payloadmask set to default %d", cfg.payloadmask);
saveConfig();
}
if (nvs_get_i8(my_handle, "monitormode", &flash8) == ESP_OK) {
cfg.monitormode = flash8;
ESP_LOGI(TAG, "Monitor mode = %d", flash8);
2018-06-08 22:41:37 +02:00
} else {
ESP_LOGI(TAG, "Monitor mode set to default %d", cfg.monitormode);
2018-06-08 22:41:37 +02:00
saveConfig();
}
2018-09-15 18:59:20 +02:00
if (nvs_get_i8(my_handle, "runmode", &flash8) == ESP_OK) {
cfg.runmode = flash8;
ESP_LOGI(TAG, "Run mode = %d", flash8);
} else {
ESP_LOGI(TAG, "Run mode set to default %d", cfg.runmode);
saveConfig();
}
2018-03-18 19:45:17 +01:00
nvs_close(my_handle);
ESP_LOGI(TAG, "Done");
}
} // loadConfig()