SimpleOTA client v1.2.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_trial.h
Go to the documentation of this file.
1
27#ifndef SOTA_TRIAL_H
28#define SOTA_TRIAL_H
29
30#include <stdbool.h>
31#include <stdint.h>
32
45
47enum {
51};
52
58enum {
61};
62
64typedef struct {
65 uint8_t trial;
66 uint8_t staged;
67 uint32_t new_part;
68 uint32_t prev_part;
69 uint32_t running_part;
70 uint32_t boot_part;
73
76static inline bool sota_on_new_image(const sota_boot_facts_t *f)
77{
78 if (f->new_part != 0) return f->running_part == f->new_part;
79 return f->prev_part != 0 && f->running_part != f->prev_part;
80}
81
85{
86 /* sota_staged is written first and sota_trial last, so staged with no
87 * trial is a record cut short before the slot switch. */
88 if (f->trial == 0) return f->staged ? SOTA_BOOT_ABANDONED : SOTA_BOOT_NONE;
89 /* A rollback saves sota_trial = 2 before it leaves the new image, so
90 * still running that image means power was lost in between. */
91 if (f->trial == SOTA_TRIAL_ROLLED_BACK) {
92 return f->running_part != 0 && f->prev_part != 0 && sota_on_new_image(f)
95 }
96 if (f->running_part == 0 || (f->new_part == 0 && f->prev_part == 0)) {
98 }
99 bool on_new = sota_on_new_image(f);
100
101 /* Like the bootloader's probation, a trial ends at the first reset of
102 * the new image, whatever the cause: a crash, a watchdog, a power loss,
103 * a restart. Without that, an image that resets before it is confirmed
104 * gets a fresh confirm timeout on every boot, and never rolls back.
105 * Deep sleep counts too: where the bootloader resumes the image on a
106 * wake without validating it (CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_
107 * SLEEP), the trial would otherwise outlive any number of wakes. */
108 if (on_new) {
111 }
112
113 /* A deep-sleep wake can resume the old image while the boot slot already
114 * points at the new one, so the slot switch did happen. */
115 if (f->deep_sleep_wake && f->boot_part != 0 &&
116 (f->new_part != 0 ? f->boot_part == f->new_part
117 : f->boot_part != f->running_part)) {
118 return SOTA_BOOT_WAITING;
119 }
120 if (f->staged) return SOTA_BOOT_ABANDONED;
121 /* Any other reset that is not on the new image means it did not run,
122 * which is a rollback. */
124}
125
130static inline bool sota_abandon_restores_identity(uint8_t staged)
131{
132 return staged >= SOTA_STAGED_SNAPSHOT;
133}
134
141static inline bool sota_hold_pending_image(bool image_pending, sota_boot_outcome_t outcome)
142{
143 return image_pending &&
144 (outcome == SOTA_BOOT_TRIAL || outcome == SOTA_BOOT_TRIAL_RESET ||
145 outcome == SOTA_BOOT_ROLLBACK_CUT);
146}
147
149enum {
150 SOTA_RST_POWERON = 1,
151 SOTA_RST_SW = 3,
152 SOTA_RST_PANIC = 4,
153 SOTA_RST_INT_WDT = 5,
154 SOTA_RST_TASK_WDT = 6,
155 SOTA_RST_WDT = 7,
156 SOTA_RST_DEEPSLEEP = 8,
157 SOTA_RST_BROWNOUT = 9,
158};
159
166static inline bool sota_reset_is_crash(int reset_reason)
167{
168 return reset_reason == SOTA_RST_PANIC || reset_reason == SOTA_RST_INT_WDT ||
169 reset_reason == SOTA_RST_TASK_WDT || reset_reason == SOTA_RST_WDT;
170}
171
173static inline const char *sota_rollback_reason(int reset_reason)
174{
175 switch (reset_reason) {
176 case SOTA_RST_PANIC: return "panic";
177 case SOTA_RST_INT_WDT: return "int_wdt";
178 case SOTA_RST_TASK_WDT: return "task_wdt";
179 case SOTA_RST_WDT: return "watchdog";
180 case SOTA_RST_BROWNOUT: return "brownout";
181 case SOTA_RST_POWERON: return "power_on";
182 case SOTA_RST_DEEPSLEEP: return "deep_sleep";
183 default: return "reset";
184 }
185}
186
187#endif /* SOTA_TRIAL_H */
static bool sota_abandon_restores_identity(uint8_t staged)
An abandoned record puts the previous identity back only when it was saved in full.
Definition sota_trial.h:130
static bool sota_reset_is_crash(int reset_reason)
The reset was a crash or a watchdog.
Definition sota_trial.h:166
static bool sota_hold_pending_image(bool image_pending, sota_boot_outcome_t outcome)
Arduino's verifyRollbackLater(): keep an image the bootloader still has on probation (pending verify)...
Definition sota_trial.h:141
sota_boot_outcome_t
What a boot means for the saved trial record, and what to do about it.
Definition sota_trial.h:34
@ SOTA_BOOT_WAITING
Flashed, not booted yet (deep-sleep wake).
Definition sota_trial.h:39
@ SOTA_BOOT_NONE
No trial record.
Definition sota_trial.h:35
@ SOTA_BOOT_ROLLED_BACK
Not on the new image: restore identity, report.
Definition sota_trial.h:37
@ SOTA_BOOT_TRIAL
Running the new image: supervise it.
Definition sota_trial.h:36
@ SOTA_BOOT_ABANDONED
The slot switch never happened: drop quietly.
Definition sota_trial.h:40
@ SOTA_BOOT_TRIAL_RESET
The new image reset before it was confirmed: roll back.
Definition sota_trial.h:42
@ SOTA_BOOT_INCOMPLETE
The record cannot be checked: drop it.
Definition sota_trial.h:41
@ SOTA_BOOT_REPORT_PENDING
A previous boot recorded a rollback: report it.
Definition sota_trial.h:38
@ SOTA_BOOT_ROLLBACK_CUT
A rollback was saved but its slot switch never happened.
Definition sota_trial.h:43
@ SOTA_TRIAL_BOOTED
The new image booted once; any reset ends the trial.
Definition sota_trial.h:50
@ SOTA_TRIAL_ROLLED_BACK
A rollback was decided and saved.
Definition sota_trial.h:49
@ SOTA_TRIAL_STAGED
The new image has not booted yet.
Definition sota_trial.h:48
static sota_boot_outcome_t sota_classify_boot(const sota_boot_facts_t *f)
Classify this boot against the trial record.
Definition sota_trial.h:84
@ SOTA_STAGED_STARTED
The record is being written.
Definition sota_trial.h:59
@ SOTA_STAGED_SNAPSHOT
The previous image's identity is saved.
Definition sota_trial.h:60
static const char * sota_rollback_reason(int reset_reason)
Reason sent with rolled_back, from the reset that ended the trial.
Definition sota_trial.h:173
static bool sota_on_new_image(const sota_boot_facts_t *f)
Whether the running image is the update's, from sota_new_part or else sota_prev_part.
Definition sota_trial.h:76
The trial record and the boot's facts that sota_classify_boot() reads.
Definition sota_trial.h:64
uint32_t prev_part
sota_prev_part, 0 when missing.
Definition sota_trial.h:68
uint32_t boot_part
Partition the next boot selects, 0 when unknown.
Definition sota_trial.h:70
uint32_t new_part
sota_new_part, 0 when missing.
Definition sota_trial.h:67
uint8_t staged
sota_staged, 0 when missing.
Definition sota_trial.h:66
bool deep_sleep_wake
This boot is a wake from deep sleep.
Definition sota_trial.h:71
uint32_t running_part
Running partition address, 0 when unknown.
Definition sota_trial.h:69
uint8_t trial
sota_trial.
Definition sota_trial.h:65