ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hotswap.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 hotswap.h
6 * @brief Safeties for removable storage that can vanish mid-write (PROTOCORE_ENABLE_HOTSWAP).
7 *
8 * An SD card is a connector, and a connector can be pulled - during a log append, an upload, a
9 * core-dump save. The failure is nasty because it is quiet: the driver keeps handing back a mounted
10 * volume, every write reports an error nobody checks, and the code carries on believing it has
11 * storage. What should happen instead is that the medium is declared unusable, stale handles are
12 * dropped, and callers are told to stop rather than write into nothing.
13 *
14 * That is what this owns. One state machine per volume:
15 *
16 * ABSENT --probe finds a card, mount succeeds--> READY
17 * READY --fail_threshold consecutive I/O errors--> FAULTED (unmount fires immediately)
18 * FAULTED --probe interval elapses, remount succeeds--> READY
19 *
20 * The threshold matters: a single failed write is not proof a card left (a transient bus error, a
21 * full volume), so one error does not tear down a working volume. A run of them is proof enough.
22 * Any success resets the run, so intermittent noise never accumulates into a false removal.
23 *
24 * Callers gate on `protocore_hotswap_ready()` and report every filesystem outcome through
25 * `protocore_hotswap_io()`. It is deliberately **fail-closed**: while not READY, ready() is false, so a
26 * caller that honors it writes nothing rather than writing into a stale mount.
27 *
28 * The core is pure and takes an explicit `now`, so the whole state machine is host-testable with a
29 * synthetic clock; the device binding is three app callbacks (mount / unmount / optional
30 * card-detect), because how a volume is mounted is the application's business, not this owner's.
31 *
32 * @author Douglas Quigg (dstroy0)
33 * @date 2026
34 */
35
36#ifndef PROTOCORE_HOTSWAP_H
37#define PROTOCORE_HOTSWAP_H
38
39#include "protocore_config.h" // the entry point: protocore_types.h for the widths
40
41#if PROTOCORE_ENABLE_HOTSWAP
42
43EMBED_BEGIN_DECLS
44
45// PROTOCORE_HOTSWAP_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
46// it into its arena. A caller takes them once and passes the pointer to every call. How they
47// are carved is this module's and is never named here.
48
49/** @brief Where a removable volume currently stands. */
50EMBED_ENUM_BYTE(StorageState,
51 STORAGE_STATE_ABSENT = 0, ///< nothing mounted; no filesystem call is safe.
52 STORAGE_STATE_READY = 1, ///< mounted and healthy.
53 STORAGE_STATE_FAULTED = 2, ///< was mounted, I/O is failing; unmounted and awaiting a remount probe.
54);
55
56/** @brief The whole state machine. Pure: it decides, the binding acts. */
57typedef struct
58{
59 StorageState state; ///< current state.
60 uint8_t fail_run; ///< consecutive I/O failures seen while READY.
61 uint8_t fail_threshold; ///< failures in a row that declare the medium gone (>= 1).
62 uint32_t probe_interval_ms; ///< minimum gap between remount attempts while not READY.
63 uint32_t last_probe_ms; ///< when the last probe ran.
64 uint32_t mounts; ///< successful mounts since init (a removal/insert cycle count).
65 uint32_t faults; ///< times a healthy volume was declared faulted.
66} HotswapCore;
67
68/** @brief Mount the volume. @return true on success. */
69typedef embed_bool (*protocore_hotswap_mount)(void *ctx);
70
71/** @brief Drop the mount and any handles it owns. Must tolerate being called when not mounted. */
72typedef void (*protocore_hotswap_unmount)(void *ctx);
73
74/** @brief Optional card-detect probe. nullptr means "assume present and let the mount decide". */
75typedef embed_bool (*protocore_hotswap_present)(void *ctx);
76
77/** @brief Fired on every state change, so an app can log it or light an LED. */
78typedef void (*protocore_hotswap_event)(StorageState from, StorageState to, void *ctx);
79
80/** @brief What core_init takes: c, fail_threshold, probe_interval_ms, ... */
81typedef struct
82{
83 HotswapCore *c;
84 uint8_t
85 fail_threshold; ///< consecutive I/O errors that declare the medium gone; clamped to >= 1. Starting ABSENT ...
86 uint32_t probe_interval_ms;
87 uint32_t now;
88} HotswapCoreInitArgs;
89
90/** @brief What core_io takes: c, ok. */
91typedef struct
92{
93 HotswapCore *c;
94 embed_bool ok;
95} HotswapCoreIoArgs;
96
97/** @brief What core_due takes: c, now. */
98typedef struct
99{
100 const HotswapCore *c;
101 uint32_t now;
102} HotswapCoreDueArgs;
103
104/** @brief What core_probe takes: c, present, mounted, now. */
105typedef struct
106{
107 HotswapCore *c;
108 embed_bool present; ///< true if a medium appears to be there (card-detect, or "assume yes" without one)
109 embed_bool mounted; ///< true if the mount actually succeeded. Present-but-unmountable stays ABSENT rather than ...
110 uint32_t now;
111} HotswapCoreProbeArgs;
112
113/** @brief What begin takes: mount, unmount, present, ctx. */
114typedef struct
115{
116 protocore_hotswap_mount mount;
117 protocore_hotswap_unmount unmount;
118 protocore_hotswap_present present;
119 void *ctx;
120} HotswapBeginArgs;
121
122/** @brief What set_event_cb takes: cb. */
123typedef struct
124{
125 protocore_hotswap_event cb;
126} HotswapSetEventCbArgs;
127
128/** @brief What poll_at takes: now. */
129typedef struct
130{
131 uint32_t now;
132} HotswapPollAtArgs;
133
134/** @brief What io takes: ok. */
135typedef struct
136{
137 embed_bool ok;
138} HotswapIoArgs;
139
140/** @brief What state_name takes: s. */
141typedef struct
142{
143 StorageState s;
144} HotswapStateNameArgs;
145
146/** @brief What json takes: out, cap. */
147typedef struct
148{
149 char *out;
150 size_t cap;
151} HotswapJsonArgs;
152
153/**
154 * @brief Safeties for removable storage that can vanish mid-write (PROTOCORE_ENABLE_HOTSWAP).
155 *
156 * A caller sets the members a call takes, invokes it through ::Hotswap with the bytes it runs
157 * out of, and reads the outcome off the same handle.
158 *
159 * Hotswap.core_init_args.c = ...;
160 * Hotswap.core_init_args.fail_threshold = ...;
161 * Hotswap.core_init_args.probe_interval_ms = ...;
162 * Hotswap.core_init_args.now = ...;
163 * Hotswap.core_init(work);
164 *
165 * @var HotswapNs::core_init_args what core_init takes: c, fail_threshold, probe_interval_ms,
166 * @var HotswapNs::core_io_args what core_io takes: c, ok
167 * @var HotswapNs::core_due_args what core_due takes: c, now
168 * @var HotswapNs::core_probe_args what core_probe takes: c, present, mounted, now
169 * @var HotswapNs::begin_args what begin takes: mount, unmount, present, ctx
170 * @var HotswapNs::set_event_cb_args what set_event_cb takes: cb
171 * @var HotswapNs::poll_at_args what poll_at takes: now
172 * @var HotswapNs::io_args what io takes: ok
173 * @var HotswapNs::state_name_args what state_name takes: s
174 * @var HotswapNs::json_args what json takes: out, cap
175 * @var HotswapNs::ok true if the state changed (so the binding knows to unmount + notify)
176 * @var HotswapNs::value the value a call reports
177 * @var HotswapNs::text the string a call reports
178 * @var HotswapNs::n length written (excl NUL), or 0 on overflow
179 * @var HotswapNs::core_init initialize to ABSENT at now
180 * @var HotswapNs::core_io report one filesystem outcome. A success while READY clears the ...
181 * @var HotswapNs::core_due is a (re)mount probe due at now? Only while not READY, and only ...
182 * @var HotswapNs::core_probe report what a probe found
183 * @var HotswapNs::begin install the callbacks and reset to ABSENT. A first poll will ...
184 * @var HotswapNs::set_event_cb install (or clear, with nullptr) the state-change callback
185 * @var HotswapNs::poll run the state machine: probe when due, unmount on a fresh fault. ...
186 * @var HotswapNs::poll_at poll_at
187 * @var HotswapNs::ready is it safe to touch the filesystem right now? The gate every caller ...
188 * @var HotswapNs::io report a filesystem outcome; unmounts and notifies if this is the ...
189 * @var HotswapNs::state current state
190 * @var HotswapNs::state_name short name for s ("absent" / "ready" / "faulted"), for logs and JSON
191 * @var HotswapNs::json serialize as `{"storage":"ready","mounts":N,"faults":N}` for a ...
192 *
193 * @c work is PROTOCORE_HOTSWAP_BORROW bytes the CALLER took, at an address it knows. It is not held past the call, so
194 * nothing here aliases it. How those bytes are carved is this module's and is never named here.
195 */
196typedef struct
197{
198 HotswapCoreInitArgs core_init_args;
199 HotswapCoreIoArgs core_io_args;
200 HotswapCoreDueArgs core_due_args;
201 HotswapCoreProbeArgs core_probe_args;
202 HotswapBeginArgs begin_args;
203 HotswapSetEventCbArgs set_event_cb_args;
204 HotswapPollAtArgs poll_at_args;
205 HotswapIoArgs io_args;
206 HotswapStateNameArgs state_name_args;
207 HotswapJsonArgs json_args;
208 embed_bool ok;
209 StorageState value;
210 const char *text;
211 size_t n;
212} HotswapVars;
213
214/** @brief The operands and the outcome. */
215extern HotswapVars HotswapV;
216
217/** @brief The entries. */
218typedef struct
219{
220 void (*const core_init)(const uint8_t *work);
221 void (*const core_io)(const uint8_t *work);
222 void (*const core_due)(const uint8_t *work);
223 void (*const core_probe)(const uint8_t *work);
224 void (*const begin)(uint8_t *work);
225 void (*const set_event_cb)(uint8_t *work);
226 void (*const poll)(uint8_t *work);
227 void (*const poll_at)(uint8_t *work);
228 void (*const ready)(uint8_t *work);
229 void (*const io)(uint8_t *work);
230 void (*const state)(uint8_t *work);
231 void (*const state_name)(const uint8_t *work);
232 void (*const json)(uint8_t *work);
233} HotswapNs;
234
235// What the table binds, defined once in the .c and taking one parameter each: everything
236// else an entry needs is an operand in HotswapV or a region of the borrow at a fixed offset.
237void protocore_hotswap_core_init(const uint8_t *work);
238void protocore_hotswap_core_io(const uint8_t *work);
239void protocore_hotswap_core_due(const uint8_t *work);
240void protocore_hotswap_core_probe(const uint8_t *work);
241void protocore_hotswap_begin(uint8_t *work);
242void protocore_hotswap_set_event_cb(uint8_t *work);
243void protocore_hotswap_poll(uint8_t *work);
244void protocore_hotswap_poll_at(uint8_t *work);
245void protocore_hotswap_ready(uint8_t *work);
246void protocore_hotswap_io(uint8_t *work);
247void protocore_hotswap_state(uint8_t *work);
248void protocore_hotswap_state_name(const uint8_t *work);
249void protocore_hotswap_json(uint8_t *work);
250
251// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
252// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
253// `Hotswap.core_init(work)` resolves to a named function and becomes a DIRECT call. An extern table
254// leaves the call indirect and the symbol live at every level, -O2 -flto included.
255static const HotswapNs Hotswap __attribute__((unused)) = {
256 .core_init = protocore_hotswap_core_init,
257 .core_io = protocore_hotswap_core_io,
258 .core_due = protocore_hotswap_core_due,
259 .core_probe = protocore_hotswap_core_probe,
260 .begin = protocore_hotswap_begin,
261 .set_event_cb = protocore_hotswap_set_event_cb,
262 .poll = protocore_hotswap_poll,
263 .poll_at = protocore_hotswap_poll_at,
264 .ready = protocore_hotswap_ready,
265 .io = protocore_hotswap_io,
266 .state = protocore_hotswap_state,
267 .state_name = protocore_hotswap_state_name,
268 .json = protocore_hotswap_json,
269};
270
271/**
272 * @brief The PROTOCORE_HOTSWAP_BORROW bytes this module's state lives in.
273 *
274 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
275 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
276 * walks, so the state lasts the life of the program.
277 *
278 * @return the span.
279 */
280uint8_t *protocore_hotswap_span(void);
281
282EMBED_END_DECLS
283
284#endif // PROTOCORE_ENABLE_HOTSWAP
285
286#endif // PROTOCORE_HOTSWAP_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.