SimpleOTA client v1.1.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_report.h
Go to the documentation of this file.
1
14#ifndef SOTA_REPORT_H
15#define SOTA_REPORT_H
16
17#include <stdbool.h>
18#include <stdint.h>
19#include <string.h>
20
22#define SOTA_REPORT_QUICK_RETRIES 3u
23
25#define SOTA_CONNECT_POLL_S 1u
26
28static inline bool sota_report_delivered(int http_status)
29{
30 return http_status > 0;
31}
32
45static inline uint32_t sota_poll_interval_s(bool connected, bool in_trial,
46 bool report_pending,
47 uint32_t report_failures,
48 uint32_t quick_s,
49 uint32_t check_interval_s)
50{
51 if (!connected) return SOTA_CONNECT_POLL_S;
52 if (in_trial) return quick_s;
53 if (report_pending && report_failures < SOTA_REPORT_QUICK_RETRIES) return quick_s;
54 return check_interval_s;
55}
56
60static inline bool sota_http_transient(int http_status)
61{
62 return http_status <= 0 || http_status >= 500 || http_status == 408 ||
63 http_status == 429;
64}
65
72static inline uint32_t sota_retry_interval_s(uint32_t failures, uint32_t check_interval_s)
73{
74 static const uint16_t steps[] = {30u, 120u, 600u};
75 if (failures == 0 || failures > sizeof(steps) / sizeof(steps[0])) return check_interval_s;
76 return steps[failures - 1u] < check_interval_s ? steps[failures - 1u] : check_interval_s;
77}
78
80#define SOTA_RETRY_AFTER_MAX_S 86400u
81
88static inline uint32_t sota_retry_after_wait_s(uint32_t wait_s, uint32_t retry_after_s,
89 uint32_t random)
90{
91 if (retry_after_s > SOTA_RETRY_AFTER_MAX_S) retry_after_s = SOTA_RETRY_AFTER_MAX_S;
92 if (retry_after_s <= wait_s) return wait_s;
93 return retry_after_s + (uint32_t)(((uint64_t)random * (retry_after_s / 10u + 1u)) >> 32);
94}
95
100#define SOTA_JITTER_MIN_S 60u
101
109static inline uint32_t sota_jitter_s(uint32_t wait_s, uint32_t random)
110{
111 if (wait_s < SOTA_JITTER_MIN_S) return wait_s;
112 const uint32_t span = wait_s / 5u;
113 const uint64_t jittered =
114 (uint64_t)wait_s - span / 2u + (((uint64_t)random * ((uint64_t)span + 1u)) >> 32);
115 return jittered > UINT32_MAX ? UINT32_MAX : (uint32_t)jittered;
116}
117
118/*
119 * Outbox. Once a boot has worked out what happened to a trial, the report
120 * moves here and the trial keys are cleared, so a later update never
121 * overwrites a report that has not been sent. Stored as one NVS blob. When
122 * full, the oldest report makes room for the new one.
123 */
124#define SOTA_OUTBOX_VERSION 1u
125#define SOTA_OUTBOX_SLOTS 2u
128typedef struct {
129 char event[12];
130 char reason[24];
131 char deployment[64];
132 uint32_t build;
134
136typedef struct {
137 uint8_t version;
138 uint8_t count;
141
143static inline bool sota_copy_field(char *dst, size_t cap, const char *src)
144{
145 size_t len = src ? strlen(src) : 0;
146 if (len >= cap) return false;
147 if (len) memcpy(dst, src, len);
148 dst[len] = '\0';
149 return true;
150}
151
153static inline void sota_outbox_init(sota_outbox_t *box)
154{
155 memset(box, 0, sizeof(*box));
157}
158
160static inline bool sota_field_terminated(const char *field, size_t cap)
161{
162 return memchr(field, '\0', cap) != NULL;
163}
164
167static inline bool sota_outbox_valid(const sota_outbox_t *box)
168{
169 if (box->version != SOTA_OUTBOX_VERSION || box->count > SOTA_OUTBOX_SLOTS) return false;
170 for (uint8_t i = 0; i < box->count; ++i) {
171 const sota_report_t *r = &box->reports[i];
172 if (!sota_field_terminated(r->event, sizeof(r->event)) ||
173 !sota_field_terminated(r->reason, sizeof(r->reason)) ||
175 !r->event[0] || !r->deployment[0] || r->build == 0) {
176 return false;
177 }
178 }
179 return true;
180}
181
184static inline bool sota_outbox_push(sota_outbox_t *box, const char *event,
185 const char *reason, const char *deployment,
186 uint32_t build)
187{
189 memset(&r, 0, sizeof(r));
190 if (!event || !event[0] || !deployment || !deployment[0] || build == 0) return false;
191 if (!sota_copy_field(r.event, sizeof(r.event), event) ||
192 !sota_copy_field(r.reason, sizeof(r.reason), reason) ||
193 !sota_copy_field(r.deployment, sizeof(r.deployment), deployment)) {
194 return false;
195 }
196 r.build = build;
197 if (box->count == SOTA_OUTBOX_SLOTS) {
198 memmove(&box->reports[0], &box->reports[1],
199 sizeof(sota_report_t) * (SOTA_OUTBOX_SLOTS - 1));
200 box->count--;
201 }
202 box->reports[box->count++] = r;
203 return true;
204}
205
207static inline void sota_outbox_pop(sota_outbox_t *box)
208{
209 if (box->count == 0) return;
210 memmove(&box->reports[0], &box->reports[1],
211 sizeof(sota_report_t) * (SOTA_OUTBOX_SLOTS - 1));
212 box->count--;
213 memset(&box->reports[box->count], 0, sizeof(sota_report_t));
214}
215
216#endif /* SOTA_REPORT_H */
static bool sota_field_terminated(const char *field, size_t cap)
True when field has a NUL within cap bytes.
Definition sota_report.h:160
static void sota_outbox_pop(sota_outbox_t *box)
Remove the report at the front.
Definition sota_report.h:207
#define SOTA_JITTER_MIN_S
Waits from this long on are spread, so devices that start together (a power cut ending,...
Definition sota_report.h:100
static uint32_t sota_retry_after_wait_s(uint32_t wait_s, uint32_t retry_after_s, uint32_t random)
The wait once a 429 or 503 asked for retry_after_s seconds (0: it did not): never sooner than asked,...
Definition sota_report.h:88
static bool sota_http_transient(int http_status)
An HTTP result worth trying again soon: no response, a server error, or a server asking for patience.
Definition sota_report.h:60
static bool sota_outbox_valid(const sota_outbox_t *box)
True when a loaded blob has this layout, a count it can hold, and only reports that sota_outbox_push(...
Definition sota_report.h:167
static uint32_t sota_jitter_s(uint32_t wait_s, uint32_t random)
wait_s moved within a span of wait_s / 5 (rounded down) seconds: down by up to span / 2 rounded down,...
Definition sota_report.h:109
static uint32_t sota_poll_interval_s(bool connected, bool in_trial, bool report_pending, uint32_t report_failures, uint32_t quick_s, uint32_t check_interval_s)
Seconds until the next check.
Definition sota_report.h:45
static uint32_t sota_retry_interval_s(uint32_t failures, uint32_t check_interval_s)
Seconds until the next check after one that failed on the network, at the check-in or in the download...
Definition sota_report.h:72
static bool sota_copy_field(char *dst, size_t cap, const char *src)
Copy src into a fixed field.
Definition sota_report.h:143
#define SOTA_RETRY_AFTER_MAX_S
The longest a server's Retry-After is honoured: a day.
Definition sota_report.h:80
#define SOTA_OUTBOX_VERSION
Layout of the stored outbox blob.
Definition sota_report.h:124
static bool sota_report_delivered(int http_status)
A report is delivered once the server returns any HTTP response.
Definition sota_report.h:28
#define SOTA_REPORT_QUICK_RETRIES
Quick retries for a pending report before it waits for the check interval.
Definition sota_report.h:22
#define SOTA_OUTBOX_SLOTS
Reports the outbox holds.
Definition sota_report.h:125
#define SOTA_CONNECT_POLL_S
Seconds between is_connected() polls while the network is down.
Definition sota_report.h:25
static void sota_outbox_init(sota_outbox_t *box)
Empty the outbox.
Definition sota_report.h:153
static bool sota_outbox_push(sota_outbox_t *box, const char *event, const char *reason, const char *deployment, uint32_t build)
Add a report at the back.
Definition sota_report.h:184
The outbox, oldest report first.
Definition sota_report.h:136
sota_report_t reports[2u]
The reports, oldest first.
Definition sota_report.h:139
uint8_t count
Reports in use.
Definition sota_report.h:138
uint8_t version
SOTA_OUTBOX_VERSION.
Definition sota_report.h:137
One status report waiting to be sent.
Definition sota_report.h:128
char deployment[64]
The deployment the report is about.
Definition sota_report.h:131
uint32_t build
The build the report is about.
Definition sota_report.h:132
char event[12]
"confirmed" or "rolled_back".
Definition sota_report.h:129
char reason[24]
Empty when the event has none.
Definition sota_report.h:130