173 lines
3.7 KiB
C++
173 lines
3.7 KiB
C++
#include "config.h"
|
|
#include "log.h"
|
|
|
|
#ifdef ESP32
|
|
|
|
#include <nvs_flash.h>
|
|
#include <Preferences.h>
|
|
|
|
Preferences config;
|
|
|
|
void configSetup() {
|
|
config.begin("config", false);
|
|
const String &stored = config.getString("HOSTNAME", "");
|
|
if (!stored.equals(HOSTNAME)) {
|
|
error(R"([WARNING] Stored config hostname mismatch: "%s" != "%s" [WARNING])", stored.c_str(), HOSTNAME);
|
|
}
|
|
}
|
|
|
|
void configReset() {
|
|
info("[CONFIG RESET]");
|
|
config.end();
|
|
nvs_flash_erase();
|
|
nvs_flash_init();
|
|
config.begin("config", false);
|
|
config.putString("HOSTNAME", HOSTNAME);
|
|
configLoaded();
|
|
}
|
|
|
|
String configGetString(const char *name, const char *fallback, bool allowEmpty) {
|
|
if (!config.isKey(name)) {
|
|
return fallback;
|
|
}
|
|
String value = config.getString(name);
|
|
if (!allowEmpty && value.isEmpty()) {
|
|
return fallback;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
bool configPutString(const char *name, const char *value) {
|
|
return config.putString(name, value) == strlen(value);
|
|
}
|
|
|
|
double configGetDouble(const char *name, double fallback) {
|
|
if (!config.isKey(name)) {
|
|
return fallback;
|
|
}
|
|
return config.getDouble(name);
|
|
}
|
|
|
|
bool configPutDouble(const char *name, double value) {
|
|
return config.value(name, value) == 0;
|
|
}
|
|
|
|
#else
|
|
|
|
#include <EEPROM.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
JsonDocument json;
|
|
|
|
bool configRead();
|
|
|
|
bool configWrite();
|
|
|
|
size_t configWriteBytes(int *address, uint8_t *data, size_t size);
|
|
|
|
size_t configReadBytes(int *address, uint8_t *data, size_t size);
|
|
|
|
void configSetup() {
|
|
configRead();
|
|
}
|
|
|
|
void configReset() {
|
|
json.clear();
|
|
json["HOSTNAME"] = HOSTNAME;
|
|
configWrite();
|
|
}
|
|
|
|
String configGetString(const char *name, const char *fallback, bool allowEmpty) {
|
|
if (!json.containsKey(name)) {
|
|
return fallback;
|
|
}
|
|
String value = json[name];
|
|
if (!allowEmpty && value.isEmpty()) {
|
|
return fallback;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
bool configPutString(const char *name, const char *value) {
|
|
json[name] = value;
|
|
return configWrite();
|
|
}
|
|
|
|
double configGetDouble(const char *name, double fallback) {
|
|
if (!json.containsKey(name)) {
|
|
return fallback;
|
|
}
|
|
return json[name];
|
|
}
|
|
|
|
bool configPutDouble(const char *name, double value) {
|
|
json[name] = value;
|
|
return configWrite();
|
|
}
|
|
|
|
bool configRead() {
|
|
size_t length;
|
|
char buffer[512];
|
|
|
|
bool ok = true;
|
|
int address = 0;
|
|
EEPROM.begin(512);
|
|
ok &= configReadBytes(&address, reinterpret_cast<uint8_t *>(&length), sizeof length);
|
|
ok &= configReadBytes(&address, reinterpret_cast<uint8_t *>(buffer), length);
|
|
ok &= EEPROM.end();
|
|
|
|
if (ok) {
|
|
JsonDocument tmp;
|
|
deserializeJson(tmp, buffer);
|
|
if (tmp.is<JsonObject>() && !tmp.isNull() && tmp.containsKey("HOSTNAME")) {
|
|
info("Config loaded");
|
|
json = tmp;
|
|
return true;
|
|
} else {
|
|
error("Failed to parse config JSON");
|
|
}
|
|
} else {
|
|
error("Failed to load config from EEPROM");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool configWrite() {
|
|
char buffer[512];
|
|
size_t length = serializeJson(json, buffer, sizeof buffer);
|
|
|
|
bool ok = true;
|
|
int address = 0;
|
|
EEPROM.begin(512);
|
|
ok &= configWriteBytes(&address, reinterpret_cast<uint8_t *>(&length), sizeof length);
|
|
ok &= configWriteBytes(&address, reinterpret_cast<uint8_t *>(buffer), length);
|
|
ok &= EEPROM.end();
|
|
return ok;
|
|
}
|
|
|
|
size_t configWriteBytes(int *address, uint8_t *data, size_t size) {
|
|
uint8_t *b = data;
|
|
for (; b < data + size; b++) {
|
|
EEPROM.write((*address)++, *b);
|
|
if (*address >= EEPROM.length()) {
|
|
error("END OF EEPROM!!!");
|
|
break;
|
|
}
|
|
}
|
|
return b - data;
|
|
}
|
|
|
|
size_t configReadBytes(int *address, uint8_t *data, size_t size) {
|
|
uint8_t *b = data;
|
|
for (; b < data + size; b++) {
|
|
*b = EEPROM.read((*address)++);
|
|
if (*address >= EEPROM.length()) {
|
|
error("END OF EEPROM!!!");
|
|
break;
|
|
}
|
|
}
|
|
return b - data;
|
|
}
|
|
|
|
#endif
|