DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ota_rollback.cpp
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.cpp
6 * @brief OTA rollback decision (pure) + esp_ota_ops commit/rollback (ESP32).
7 */
8
10
11#if DETWS_ENABLE_OTA_ROLLBACK
12
13DetwsOtaAction detws_ota_decide(uint8_t img_state, bool self_test_ok, uint32_t ms_since_boot, uint32_t window_ms)
14{
15 if (img_state != DetwsOtaImg::DETWS_OTA_IMG_PENDING_VERIFY)
16 return DetwsOtaAction::DETWS_OTA_WAIT; // not a freshly-updated image: nothing to do
17 if (self_test_ok)
18 return DetwsOtaAction::DETWS_OTA_COMMIT;
19 if (ms_since_boot >= window_ms)
20 return DetwsOtaAction::DETWS_OTA_ROLLBACK; // never confirmed in time -> self-heal
21 return DetwsOtaAction::DETWS_OTA_WAIT;
22}
23
24#ifdef ARDUINO
25
26#include "esp_ota_ops.h"
27#include "services/clock.h" // detws_millis() (pulls in Arduino millis())
28
29uint8_t detws_ota_img_state(void)
30{
31 const esp_partition_t *running = esp_ota_get_running_partition();
32 esp_ota_img_states_t st = ESP_OTA_IMG_UNDEFINED;
33 if (!running || esp_ota_get_state_partition(running, &st) != ESP_OK)
34 return DetwsOtaImg::DETWS_OTA_IMG_UNDEFINED;
35 return (uint8_t)st;
36}
37
38void detws_ota_commit(void)
39{
40 esp_ota_mark_app_valid_cancel_rollback();
41}
42
43void detws_ota_rollback(void)
44{
45 esp_ota_mark_app_invalid_rollback_and_reboot(); // does not return
46}
47
48DetwsOtaAction detws_ota_rollback_tick(bool self_test_ok)
49{
50 DetwsOtaAction a =
51 detws_ota_decide(detws_ota_img_state(), self_test_ok, detws_millis(), DETWS_OTA_CONFIRM_WINDOW_MS);
52 if (a == DetwsOtaAction::DETWS_OTA_COMMIT)
53 detws_ota_commit();
54 else if (a == DetwsOtaAction::DETWS_OTA_ROLLBACK)
55 detws_ota_rollback();
56 return a;
57}
58
59#else // host build - no OTA partitions
60
61uint8_t detws_ota_img_state(void)
62{
63 return DetwsOtaImg::DETWS_OTA_IMG_UNDEFINED;
64}
65void detws_ota_commit(void)
66{
67}
68void detws_ota_rollback(void)
69{
70}
71DetwsOtaAction detws_ota_rollback_tick(bool)
72{
73 return DetwsOtaAction::DETWS_OTA_WAIT;
74}
75
76#endif // ARDUINO
77
78#endif // DETWS_ENABLE_OTA_ROLLBACK
#define DETWS_OTA_CONFIRM_WINDOW_MS
Confirm window (ms): a pending image not confirmed within this rolls back.
Pluggable monotonic clock for all library timing.
uint32_t detws_millis(void)
The library's monotonic time at 1000 Hz (milliseconds).
Definition clock.h:71
OTA rollback protection / soft-brick safeguard (DETWS_ENABLE_OTA_ROLLBACK).