57 lines
1.7 KiB
C
57 lines
1.7 KiB
C
// Persistence for the SHORT LIST of config keys allowed to survive power
|
|
// cycles: duty_budget_ppm and repeat_delay_ms. Battery-standalone testing
|
|
// needs settings that outlive reboots (no host to re-send commands).
|
|
// Everything else in g_cfg stays deliberately RAM-only.
|
|
//
|
|
// Persisted values are restored at boot and logged as a cfg_restore
|
|
// event; the OLED config page shows the effective values. A device with a
|
|
// persisted duty_budget_ppm of 0 (guard disabled) stays non-compliant
|
|
// until explicitly set back -- docs/STANDARDS_GAPS.md.
|
|
#include "cfg_store.h"
|
|
|
|
#include <stdio.h>
|
|
#include "nvs_flash.h"
|
|
#include "nvs.h"
|
|
|
|
#define NS "repeater"
|
|
|
|
static bool s_ready;
|
|
|
|
void cfg_store_init(void)
|
|
{
|
|
esp_err_t err = nvs_flash_init();
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
if (nvs_flash_erase() != ESP_OK) return;
|
|
err = nvs_flash_init();
|
|
}
|
|
s_ready = (err == ESP_OK);
|
|
if (!s_ready) {
|
|
printf("{\"event\":\"cfg_store\",\"ok\":false,\"err\":%d}\n", (int)err);
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
|
|
bool cfg_store_load_u32(const char *key, uint32_t *out)
|
|
{
|
|
if (!s_ready) return false;
|
|
nvs_handle_t h;
|
|
if (nvs_open(NS, NVS_READONLY, &h) != ESP_OK) return false;
|
|
uint32_t v = 0;
|
|
esp_err_t err = nvs_get_u32(h, key, &v);
|
|
nvs_close(h);
|
|
if (err != ESP_OK) return false;
|
|
*out = v;
|
|
return true;
|
|
}
|
|
|
|
bool cfg_store_save_u32(const char *key, uint32_t v)
|
|
{
|
|
if (!s_ready) return false;
|
|
nvs_handle_t h;
|
|
if (nvs_open(NS, NVS_READWRITE, &h) != ESP_OK) return false;
|
|
esp_err_t err = nvs_set_u32(h, key, v);
|
|
if (err == ESP_OK) err = nvs_commit(h);
|
|
nvs_close(h);
|
|
return err == ESP_OK;
|
|
}
|