DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
config_store.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file config_store.h
6 * @brief Typed NVS configuration store (DETWS_ENABLE_CONFIG_STORE).
7 *
8 * A small typed key/value API for core device settings - WiFi credentials, IP
9 * configuration, feature toggles - that routes them into the ESP32's native
10 * Non-Volatile Storage (NVS) partition as binary entries, rather than a JSON
11 * text file on the filesystem. NVS is wear-levelled and independent of the
12 * LittleFS/SPIFFS partition, so configuration survives a filesystem corruption
13 * and credentials live in the storage area meant for them.
14 *
15 * Three value types: null-terminated strings, `uint32_t`, and raw blobs - each
16 * with a default returned when the key is absent. On ESP32 the backend is the
17 * Arduino `Preferences` NVS wrapper; on host builds it is a fixed in-memory table
18 * (`DETWS_CONFIG_MAX_ENTRIES` x `DETWS_CONFIG_VAL_MAX`) so the typed contract is
19 * unit-testable without flash.
20 *
21 * Writes hit NVS, so call the setters at provisioning / config time, not in the
22 * request hot path. Keys are limited to 15 chars (NVS), plus null.
23 *
24 * @author Douglas Quigg (dstroy0)
25 * @date 2026
26 */
27
28#ifndef DETERMINISTICESPASYNCWEBSERVER_CONFIG_STORE_H
29#define DETERMINISTICESPASYNCWEBSERVER_CONFIG_STORE_H
30
31#include "ServerConfig.h"
32#include <stddef.h>
33#include <stdint.h>
34
35#if DETWS_ENABLE_CONFIG_STORE
36
37/**
38 * @brief Open a configuration namespace (e.g. "wifi", "net"). Call once before
39 * get/set. On ESP32 this opens the NVS namespace read-write.
40 * @return true on success.
41 */
42bool detws_config_begin(const char *ns);
43
44/** @brief Store a string value. @return true on success. */
45bool detws_config_set_str(const char *key, const char *val);
46
47/**
48 * @brief Read a string value into @p out (always null-terminated, bounded by
49 * @p out_cap). Returns @p def when the key is absent.
50 * @return number of characters written (excluding the null terminator).
51 */
52size_t detws_config_get_str(const char *key, char *out, size_t out_cap, const char *def);
53
54/** @brief Store a `uint32_t` value. @return true on success. */
55bool detws_config_set_u32(const char *key, uint32_t val);
56
57/** @brief Read a `uint32_t` value, or @p def if the key is absent. */
58uint32_t detws_config_get_u32(const char *key, uint32_t def);
59
60/** @brief Store a raw blob. @return true on success. */
61bool detws_config_set_blob(const char *key, const void *data, size_t len);
62
63/**
64 * @brief Read a blob into @p out (bounded by @p out_cap).
65 * @return number of bytes written (0 if the key is absent).
66 */
67size_t detws_config_get_blob(const char *key, void *out, size_t out_cap);
68
69/** @brief Erase a single key. @return true if the key existed. */
70bool detws_config_erase(const char *key);
71
72/** @brief Erase every key in the open namespace. @return true on success. */
73bool detws_config_clear(void);
74
75#endif // DETWS_ENABLE_CONFIG_STORE
76#endif // DETERMINISTICESPASYNCWEBSERVER_CONFIG_STORE_H
User-facing configuration for DeterministicESPAsyncWebServer.