14#if PC_ENABLE_CONFIG_STORE
20#include <Preferences.h>
34bool pc_config_begin(
const char *ns)
40 s_cfg.open = s_cfg.prefs.begin(ns,
false);
44bool pc_config_set_str(
const char *key,
const char *val)
53size_t pc_config_get_str(
const char *key,
char *out,
size_t out_cap,
const char *def)
55 if (!out || out_cap == 0)
59 if (!s_cfg.prefs.isKey(key))
61 size_t n = def ? strnlen(def, out_cap) : 0;
73 return s_cfg.prefs.getString(key, out, out_cap);
76bool pc_config_set_u32(
const char *key, uint32_t val)
78 return s_cfg.prefs.putUInt(key, val) ==
sizeof(uint32_t);
81uint32_t pc_config_get_u32(
const char *key, uint32_t def)
83 return s_cfg.prefs.getUInt(key, def);
86bool pc_config_set_blob(
const char *key,
const void *data,
size_t len)
92 return s_cfg.prefs.putBytes(key, data, len) == len;
95size_t pc_config_get_blob(
const char *key,
void *out,
size_t out_cap)
97 if (!out || !s_cfg.prefs.isKey(key))
101 return s_cfg.prefs.getBytes(key, out, out_cap);
104bool pc_config_erase(
const char *key)
106 return s_cfg.prefs.remove(key);
109bool pc_config_clear(
void)
111 return s_cfg.prefs.clear();
133bool key_ok(
const char *key)
139Entry *find(ConfigStoreCtx &c,
const char *key)
143 if (c.tbl[i].used && strcmp(c.tbl[i].key, key) == 0)
151Entry *find_or_alloc(ConfigStoreCtx &c,
const char *key)
153 Entry *e = find(c, key);
162 c.tbl[i].used =
true;
172bool store(ConfigStoreCtx &c,
const char *key,
const void *data,
size_t len)
178 Entry *e = find_or_alloc(c, key);
183 memcpy(e->val, data, len);
189bool pc_config_begin(
const char *ns)
195bool pc_config_set_str(
const char *key,
const char *val)
204size_t pc_config_get_str(
const char *key,
char *out,
size_t out_cap,
const char *def)
206 if (!out || out_cap == 0)
210 Entry *e = key_ok(key) ? find(s_cfg, key) : nullptr;
211 const char *src = e ? (
const char *)e->val : (def ? def :
"");
212 size_t n = strnlen(src, out_cap);
222bool pc_config_set_u32(
const char *key, uint32_t val)
224 uint8_t b[4] = {(uint8_t)val, (uint8_t)(val >> 8), (uint8_t)(val >> 16), (uint8_t)(val >> 24)};
225 return store(s_cfg, key, b,
sizeof(b));
228uint32_t pc_config_get_u32(
const char *key, uint32_t def)
230 Entry *e = key_ok(key) ? find(s_cfg, key) : nullptr;
231 if (!e || e->len < 4)
235 return (uint32_t)e->val[0] | ((uint32_t)e->val[1] << 8) | ((uint32_t)e->val[2] << 16) | ((uint32_t)e->val[3] << 24);
238bool pc_config_set_blob(
const char *key,
const void *data,
size_t len)
244 return store(s_cfg, key, data, len);
247size_t pc_config_get_blob(
const char *key,
void *out,
size_t out_cap)
249 Entry *e = key_ok(key) ? find(s_cfg, key) : nullptr;
254 size_t n = e->len < out_cap ? e->len : out_cap;
255 memcpy(out, e->val, n);
259bool pc_config_erase(
const char *key)
261 Entry *e = key_ok(key) ? find(s_cfg, key) : nullptr;
271bool pc_config_clear(
void)
275 s_cfg.tbl[i] = Entry{};
Typed NVS configuration store (PC_ENABLE_CONFIG_STORE).
#define PC_CONFIG_KEY_MAX
Max key length incl. null (NVS caps keys at 15 chars).
#define PC_CONFIG_VAL_MAX
Max value bytes per entry in the host (test) config backend.
#define PC_CONFIG_MAX_ENTRIES
Max key/value entries in the host (test) config backend.