ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
config_io.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file config_io.h
6 * @brief Schema-driven config export / restore (PROTOCORE_ENABLE_CONFIG_IO).
7 *
8 * The app declares a fixed schema - an array of {key, type} fields - and this
9 * service serializes their current values from the config store to a portable
10 * `key=value` text blob (one field per line) for backup / migration, and parses
11 * such a blob back into the store for restore / bulk provisioning. Schema-driven
12 * (rather than enumerating NVS) keeps it deterministic and zero-heap; the
13 * serialize / parse round-trip is host-tested against the in-memory config store.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef PROTOCORE_CONFIG_IO_H
20#define PROTOCORE_CONFIG_IO_H
21
22#include "protocore_config.h" // the entry point: protocore_types.h for the widths
23
24#if PROTOCORE_ENABLE_CONFIG_IO
25
26EMBED_BEGIN_DECLS
27
28// This module holds nothing between calls, so it carves no borrow and states none. An entry
29// takes one all the same, and never reads it, so every namespace in the tree is invoked the
30// same way.
31
32/** @brief Type of a config field (selects the typed get/set used). */
33EMBED_ENUM_BYTE(protocore_cfg_type,
34 PROTOCORE_CFG_STR = 0, ///< null-terminated string.
35 PROTOCORE_CFG_U32 = 1, ///< unsigned 32-bit integer (serialized as decimal).
36);
37
38/** @brief One field in an export/restore schema. */
39typedef struct
40{
41 const char *key; ///< config-store key (<= 15 chars).
42 protocore_cfg_type type; ///< the field's value type.
43} protocore_cfg_field;
44
45/** @brief What export takes: ns, fields, n, out, cap. */
46typedef struct
47{
48 const char *ns;
49 const protocore_cfg_field *fields;
50 size_t n;
51 char *out;
52 size_t cap;
53} ConfigIoExportArgs;
54
55/** @brief What import takes: ns, fields, n, text, len. */
56typedef struct
57{
58 const char *ns;
59 const protocore_cfg_field *fields;
60 size_t n;
61 const char *text;
62 size_t len;
63} ConfigIoImportArgs;
64
65/**
66 * @brief Schema-driven config export / restore (PROTOCORE_ENABLE_CONFIG_IO). The app declares a fixed schema - an ...
67 *
68 * A caller sets the members a call takes, invokes it through ::ConfigIo with the bytes it runs
69 * out of, and reads the outcome off the same handle.
70 *
71 * ConfigIo.export_args.ns = ...;
72 * ConfigIo.export_args.fields = ...;
73 * ConfigIo.export_args.n = ...;
74 * ConfigIo.export_args.out = ...;
75 * ConfigIo.export_args.cap = ...;
76 * ConfigIo.export_lines(work);
77 * // ConfigIo.n is what the call reports
78 *
79 * @var ConfigIoNs::export_args what export takes: ns, fields, n, out, cap
80 * @var ConfigIoNs::import_args what import takes: ns, fields, n, text, len
81 * @var ConfigIoNs::ok a call's true/false outcome
82 * @var ConfigIoNs::n characters written, or 0 on a too-small buffer / failure ...
83 * @var ConfigIoNs::export_lines export the schema's current values from namespace ns as `key=value` ...
84 * @var ConfigIoNs::import_lines import `key=value` lines from text into namespace ns, writing each ...
85 *
86 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
87 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
88 * a caller drives every namespace the same way.
89 */
90typedef struct
91{
92 ConfigIoExportArgs export_args;
93 ConfigIoImportArgs import_args;
94 embed_bool ok;
95 int n;
96} ConfigIoVars;
97
98/** @brief The operands and the outcome. */
99extern ConfigIoVars ConfigIoV;
100
101/** @brief The entries. */
102typedef struct
103{
104 void (*const export_lines)(const uint8_t *work);
105 void (*const import_lines)(const uint8_t *work);
106} ConfigIoNs;
107
108// What the table binds, defined once in the .c and taking one parameter each: everything
109// else an entry needs is an operand in ConfigIoV or a region of the borrow at a fixed offset.
110void protocore_config_io_export(const uint8_t *work);
111void protocore_config_io_import(const uint8_t *work);
112
113// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
114// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
115// `ConfigIo.export_lines(work)` resolves to a named function and becomes a DIRECT call. An extern table
116// leaves the call indirect and the symbol live at every level, -O2 -flto included.
117static const ConfigIoNs ConfigIo __attribute__((unused)) = {
118 .export_lines = protocore_config_io_export,
119 .import_lines = protocore_config_io_import,
120};
121
122EMBED_END_DECLS
123
124#endif // PROTOCORE_ENABLE_CONFIG_IO
125
126#endif // PROTOCORE_CONFIG_IO_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.