ESP32-PaxCounter/src/configmanager.cpp

119 lines
4.1 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;
2020-10-04 13:15:17 +02:00
static const char cfgMagicBytes[] = {0x21, 0x76, 0x87, 0x32, 0xf3};
static const size_t cfgLen = sizeof(cfg), cfgLen2 = sizeof(cfgMagicBytes);
static char buffer[cfgLen + cfgLen2];
2020-10-02 23:07:24 +02:00
// populate runtime config with factory settings
2020-10-04 13:15:17 +02:00
static void defaultConfig(configData_t *myconfig) {
2020-10-02 23:07:24 +02:00
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) {
2020-10-04 13:15:17 +02:00
ESP_LOGI(TAG, "Storing settings to NVRAM...");
2020-10-02 23:07:24 +02:00
nvram.begin(DEVCONFIG, false);
2020-10-02 23:07:24 +02:00
if (erase) {
2020-10-04 13:15:17 +02:00
ESP_LOGI(TAG, "Resetting device to factory settings");
2020-10-02 23:07:24 +02:00
nvram.clear();
defaultConfig(&cfg);
}
2020-10-04 13:15:17 +02:00
// Copy device runtime config cfg to byte array, padding it with magicBytes
2020-10-02 23:07:24 +02:00
memcpy(buffer, &cfg, cfgLen);
2020-10-04 13:15:17 +02:00
memcpy(buffer + cfgLen, &cfgMagicBytes, cfgLen2);
// save byte array to NVRAM, padding with cfg magicbyes
if (nvram.putBytes(DEVCONFIG, buffer, cfgLen + cfgLen2))
ESP_LOGI(TAG, "Device settings saved");
else
ESP_LOGE(TAG, "NVRAM Error, device settings not saved");
2020-10-02 23:07:24 +02:00
nvram.end();
}
// load configuration from NVRAM into RAM and make it current
2020-10-04 13:15:17 +02:00
bool loadConfig() {
2020-10-04 13:15:17 +02:00
ESP_LOGI(TAG, "Loading device runtime configuration from NVRAM...");
2020-10-02 23:07:24 +02:00
if (!nvram.begin(DEVCONFIG, true)) {
2020-10-04 13:15:17 +02:00
ESP_LOGW(TAG, "NVRAM initialized, device starts with factory settings");
2020-10-02 23:07:24 +02:00
eraseConfig();
2020-10-04 13:15:17 +02:00
return true;
2020-10-02 23:07:24 +02:00
} else {
// simple check that runtime config data matches
2020-10-04 13:15:17 +02:00
if (nvram.getBytesLength(DEVCONFIG) != (cfgLen + cfgLen2)) {
ESP_LOGE(TAG, "configuration invalid");
return false;
2020-10-04 13:15:17 +02:00
} else {
2020-10-02 23:07:24 +02:00
// load device runtime config from nvram and copy it to byte array
2020-10-04 13:15:17 +02:00
nvram.getBytes(DEVCONFIG, buffer, cfgLen + cfgLen2);
2020-10-02 23:07:24 +02:00
nvram.end();
2020-10-04 13:15:17 +02:00
// 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;
} else {
// copy byte array into runtime cfg struct
memcpy(&cfg, buffer, cfgLen);
ESP_LOGI(TAG, "Runtime configuration loaded");
return true;
}
}
}
2020-10-02 23:07:24 +02:00
}
void eraseConfig(void) { saveConfig(true); }