DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ota_rollback.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 ota_rollback.h
6 * @brief OTA rollback protection / soft-brick safeguard (DETWS_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 wrap esp_ota_ops
13 * on ESP32. Needs the bootloader's app-rollback support.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef DETERMINISTICESPASYNCWEBSERVER_OTA_ROLLBACK_H
20#define DETERMINISTICESPASYNCWEBSERVER_OTA_ROLLBACK_H
21
22#include "ServerConfig.h"
23#include <stdint.h>
24
25#if DETWS_ENABLE_OTA_ROLLBACK
26
27/** @brief OTA image states (mirror esp_ota_img_states_t so the core is host-pure). These arrive from
28 * ESP-IDF as a uint8_t and are compared, so integer constants in a namespacing struct - cast-free. */
29struct DetwsOtaImg
30{
31 static constexpr uint8_t DETWS_OTA_IMG_NEW = 0;
32 static constexpr uint8_t DETWS_OTA_IMG_PENDING_VERIFY = 1;
33 static constexpr uint8_t DETWS_OTA_IMG_VALID = 2;
34 static constexpr uint8_t DETWS_OTA_IMG_INVALID = 3;
35 static constexpr uint8_t DETWS_OTA_IMG_ABORTED = 4;
36 static constexpr uint8_t DETWS_OTA_IMG_UNDEFINED = 0xFF;
37};
38
39/** @brief What the rollback tick should do. */
40enum class DetwsOtaAction : uint8_t
41{
42 DETWS_OTA_WAIT = 0, ///< still pending, within the window: keep waiting.
43 DETWS_OTA_COMMIT = 1, ///< self-test passed: mark the image valid.
44 DETWS_OTA_ROLLBACK = 2, ///< self-test failed or window elapsed: roll back + reboot.
45};
46
47// ---------------------------------------------------------------------------
48// Host-testable decision core
49// ---------------------------------------------------------------------------
50
51/**
52 * @brief Decide the rollback action.
53 * @param img_state current running image state (DETWS_OTA_IMG_*).
54 * @param self_test_ok application self-test result.
55 * @param ms_since_boot uptime in ms.
56 * @param window_ms confirm window.
57 * @return DetwsOtaAction::DETWS_OTA_WAIT / _COMMIT / _ROLLBACK. Only a PENDING_VERIFY image acts;
58 * any other state returns WAIT (nothing to do).
59 */
60DetwsOtaAction detws_ota_decide(uint8_t img_state, bool self_test_ok, uint32_t ms_since_boot, uint32_t window_ms);
61
62// ---------------------------------------------------------------------------
63// ESP32 actions (no-op / stubs on host)
64// ---------------------------------------------------------------------------
65
66/** @brief Current running image's OTA state (DetwsOtaImg::DETWS_OTA_IMG_UNDEFINED on host). */
67uint8_t detws_ota_img_state(void);
68
69/** @brief Commit the running image (cancel rollback). */
70void detws_ota_commit(void);
71
72/** @brief Mark the running image invalid and reboot into the previous one. */
73void detws_ota_rollback(void);
74
75/**
76 * @brief One rollback step: read the image state, decide with @p self_test_ok and
77 * millis(), and act. Call periodically until the image is committed.
78 * @return the action taken.
79 */
80DetwsOtaAction detws_ota_rollback_tick(bool self_test_ok);
81
82#endif // DETWS_ENABLE_OTA_ROLLBACK
83#endif // DETERMINISTICESPASYNCWEBSERVER_OTA_ROLLBACK_H
User-facing configuration for DeterministicESPAsyncWebServer.