ESP32-PaxCounter/src/configmanager.cpp

104 lines
3.4 KiB
C++
Raw Normal View History

/* configmanager persists runtime configuration using NVRAM of ESP32*/
#include "globals.h"
2020-03-29 18:08:52 +02:00
#include "configmanager.h"
// Local logging tag
2020-10-02 23:07:24 +02:00
static const char TAG[] = __FILE__;
#define PAYLOADMASK \
((GPS_DATA | ALARM_DATA | MEMS_DATA | COUNT_DATA | SENSOR1_DATA | \
SENSOR2_DATA | SENSOR3_DATA) & \
(~BATT_DATA))
// namespace for device runtime preferences
#define DEVCONFIG "paxcntcfg"
Preferences nvram;
static const size_t cfgLen = sizeof(cfg);
2020-10-02 23:07:24 +02:00
// populate runtime config with factory settings
void defaultConfig(configData_t *myconfig) {
char version[10];
snprintf(version, 10, "%-10s", PROGVERSION);
// factory settings
2020-10-02 23:07:24 +02:00
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
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]
2020-10-02 23:07:24 +02:00
myconfig->blescantime =
BLESCANINTERVAL /
10; // BT channel scan cycle [seconds/100], default 1 (= 10ms)
2020-10-02 23:07:24 +02:00
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
2020-10-02 23:07:24 +02:00
memcpy(myconfig->version, version, 10); // Firmware version [exactly 10 chars]
2020-10-02 23:07:24 +02:00
#ifdef HAS_BME680
// initial BSEC state for BME680 sensor
myconfig->bsecstate[BSEC_MAX_STATE_BLOB_SIZE] = {0};
#endif
}
// save current configuration from RAM to NVRAM
2020-10-02 23:07:24 +02:00
void saveConfig(bool erase) {
ESP_LOGI(TAG, "Storing settings in NVRAM");
2020-10-02 23:07:24 +02:00
nvram.begin(DEVCONFIG, false);
2020-10-02 23:07:24 +02:00
if (erase) {
ESP_LOGI(TAG, "Resetting NVRAM to factory settings");
nvram.clear();
defaultConfig(&cfg);
}
2020-10-02 23:07:24 +02:00
// Copy device runtime config cfg to byte array
char buffer[cfgLen];
memcpy(buffer, &cfg, cfgLen);
// save byte array to NVRAM
nvram.putBytes(DEVCONFIG, buffer, cfgLen);
nvram.end();
}
// load configuration from NVRAM into RAM and make it current
void loadConfig() {
2020-10-02 23:07:24 +02:00
ESP_LOGI(TAG, "Loading runtime settings from NVS");
2020-10-02 23:07:24 +02:00
if (!nvram.begin(DEVCONFIG, true)) {
ESP_LOGI(TAG, "Initializing NVRAM");
eraseConfig();
} else {
// simple check that runtime config data matches
if (nvram.getBytesLength(DEVCONFIG) != cfgLen) {
2020-10-02 23:07:24 +02:00
ESP_LOGW(TAG, "NVRAM settings invalid");
eraseConfig();
} else {
2020-10-02 23:07:24 +02:00
// load device runtime config from nvram and copy it to byte array
char *buffer = new char[cfgLen];
nvram.getBytes(DEVCONFIG, buffer, cfgLen);
nvram.end();
2020-10-02 23:07:24 +02:00
// copy the byte array into runtime cfg struct
memcpy(&cfg, buffer, cfgLen);
}
}
2020-10-02 23:07:24 +02:00
}
void eraseConfig(void) { saveConfig(true); }