ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ota_rollback.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 ota_rollback.h
6 * @brief OTA rollback protection / soft-brick safeguard (PROTOCORE_ENABLE_OTA_ROLLBACK).
7 *
8 * After an OTA update the new image boots in the PENDING_VERIFY state. This service
9 * decides, each tick, whether to commit it (a self-test passed), roll back to the
10 * previous image (self-test failed, or the confirm window elapsed without success),
11 * or keep waiting - so a bad update self-heals instead of soft-bricking. The
12 * decision is a pure function (host-tested); the commit / rollback reach the platform
13 * seam. Needs the bootloader's app-rollback support.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef PROTOCORE_OTA_ROLLBACK_H
20#define PROTOCORE_OTA_ROLLBACK_H
21
22#include "protocore_config.h" // the entry point: protocore_types.h for the widths
23
24#if PROTOCORE_ENABLE_OTA_ROLLBACK
25
26EMBED_BEGIN_DECLS
27
28/** @brief OTA image states, mirroring PROTOCORE_PLATFORM_IMG_* so the core is host-pure. These arrive
29 * from the platform seam as a uint8_t and are compared, so integer constants - cast-free. */
30#define PROTOCORE_OTA_IMG_NEW 0
31#define PROTOCORE_OTA_IMG_PENDING_VERIFY 1
32#define PROTOCORE_OTA_IMG_VALID 2
33#define PROTOCORE_OTA_IMG_INVALID 3
34#define PROTOCORE_OTA_IMG_ABORTED 4
35#define PROTOCORE_OTA_IMG_UNDEFINED 0xFF
36
37/** @brief What the rollback tick should do. */
38EMBED_ENUM_BYTE(protocore_ota_action,
39 PROTOCORE_OTA_WAIT = 0, ///< still pending, within the window: keep waiting.
40 PROTOCORE_OTA_COMMIT = 1, ///< self-test passed: mark the image valid.
41 PROTOCORE_OTA_ROLLBACK = 2, ///< self-test failed or window elapsed: roll back + reboot.
42);
43
44// ---------------------------------------------------------------------------
45// Host-testable decision core
46// ---------------------------------------------------------------------------
47
48/** @brief What the pure decision reads. */
49typedef struct
50{
51 uint8_t img_state; ///< the running image's state (PROTOCORE_OTA_IMG_*)
52 embed_bool self_test_ok; ///< the application has confirmed itself healthy
53 uint32_t ms_since_boot; ///< how long this image has been running
54 uint32_t window_ms; ///< how long it has to confirm before the rollback self-heals
55} OtaDecideArgs;
56
57/**
58 * @brief The OTA confirm-or-roll-back policy.
59 *
60 * A caller sets the members a call takes, invokes it through ::OtaRollback, and reads the outcome
61 * off the same handle. The decision is pure; the commit and the rollback reach the platform seam.
62 *
63 * @var OtaRollbackNs::decide_args what the pure decision reads
64 * @var OtaRollbackNs::self_test_ok what a tick reports about the application's own health
65 * @var OtaRollbackNs::action the action a decide or a tick chose
66 * @var OtaRollbackNs::img_state the running image's state a lookup reports
67 * @var OtaRollbackNs::decide choose an action, reading nothing outside decide_args
68 * @var OtaRollbackNs::state the running image's state, from the platform seam
69 * @var OtaRollbackNs::commit mark the running image valid and cancel the pending rollback
70 * @var OtaRollbackNs::rollback mark it invalid and reboot into the previous one
71 * @var OtaRollbackNs::tick decide against the clock, then carry the decision out
72 *
73 * No storage member: the policy holds nothing between calls; the image state lives in the part.
74 */
75typedef struct
76{
77 OtaDecideArgs decide_args;
78 embed_bool self_test_ok;
79 protocore_ota_action action;
80 uint8_t img_state;
81} OtaRollbackVars;
82
83/** @brief The operands and the outcome. */
84extern OtaRollbackVars OtaRollbackV;
85
86/** @brief The entries. */
87typedef struct
88{
89 void (*const decide)(const uint8_t *work);
90 void (*const state)(const uint8_t *work);
91 void (*const commit)(const uint8_t *work);
92 void (*const rollback)(const uint8_t *work);
93 void (*const tick)(const uint8_t *work);
94} OtaRollbackNs;
95
96// What the table binds, defined once in the .c and taking one parameter each: everything
97// else an entry needs is an operand in OtaRollbackV or a region of the borrow at a fixed offset.
98void protocore_ota_rollback_decide(const uint8_t *work);
99void protocore_ota_rollback_state(const uint8_t *work);
100void protocore_ota_rollback_commit(const uint8_t *work);
101void protocore_ota_rollback_rollback(const uint8_t *work);
102void protocore_ota_rollback_tick(const uint8_t *work);
103
104// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
105// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
106// `OtaRollback.decide(work)` resolves to a named function and becomes a DIRECT call. An extern table
107// leaves the call indirect and the symbol live at every level, -O2 -flto included.
108static const OtaRollbackNs OtaRollback __attribute__((unused)) = {
109 .decide = protocore_ota_rollback_decide,
110 .state = protocore_ota_rollback_state,
111 .commit = protocore_ota_rollback_commit,
112 .rollback = protocore_ota_rollback_rollback,
113 .tick = protocore_ota_rollback_tick,
114};
115
116EMBED_END_DECLS
117
118#endif // PROTOCORE_ENABLE_OTA_ROLLBACK
119
120#endif // PROTOCORE_OTA_ROLLBACK_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.