2019-11-27 22:16:07 +01:00
|
|
|
/* configmanager persists runtime configuration using NVRAM of ESP32*/
|
|
|
|
|
|
|
|
#include "globals.h"
|
2020-03-29 18:08:52 +02:00
|
|
|
#include "configmanager.h"
|
2019-11-27 22:16:07 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2020-10-03 12:14:07 +02:00
|
|
|
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);
|
|
|
|
|
2020-10-03 12:14:07 +02:00
|
|
|
// 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 =
|
2019-11-27 22:16:07 +01:00
|
|
|
WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100]
|
2020-10-02 23:07:24 +02:00
|
|
|
myconfig->blescantime =
|
2019-11-27 22:16:07 +01:00
|
|
|
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)
|
2020-10-03 12:14:07 +02:00
|
|
|
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-03 12:14:07 +02:00
|
|
|
|
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
|
2019-11-27 22:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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");
|
2019-11-27 22:16:07 +01:00
|
|
|
|
2020-10-02 23:07:24 +02:00
|
|
|
nvram.begin(DEVCONFIG, false);
|
2019-11-27 22:16:07 +01:00
|
|
|
|
2020-10-02 23:07:24 +02:00
|
|
|
if (erase) {
|
|
|
|
ESP_LOGI(TAG, "Resetting NVRAM to factory settings");
|
|
|
|
nvram.clear();
|
|
|
|
defaultConfig(&cfg);
|
2019-11-27 22:16:07 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2019-11-27 22:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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");
|
2019-11-27 22:16:07 +01:00
|
|
|
|
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
|
2020-10-03 12:14:07 +02:00
|
|
|
if (nvram.getBytesLength(DEVCONFIG) != cfgLen) {
|
2020-10-02 23:07:24 +02:00
|
|
|
ESP_LOGW(TAG, "NVRAM settings invalid");
|
|
|
|
eraseConfig();
|
2019-11-27 22:16:07 +01:00
|
|
|
} 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();
|
2019-11-27 22:16:07 +01:00
|
|
|
|
2020-10-02 23:07:24 +02:00
|
|
|
// copy the byte array into runtime cfg struct
|
|
|
|
memcpy(&cfg, buffer, cfgLen);
|
2019-11-27 22:16:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-02 23:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void eraseConfig(void) { saveConfig(true); }
|