SimpleOTA client v1.0.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
57/*
58 * Outbox. Once a boot has worked out what happened to a trial, the report
59 * moves here and the trial keys are cleared, so a later update never
60 * overwrites a report that has not been sent. Stored as one NVS blob. When
61 * full, the oldest report makes room for the new one.
62 */
63#define SOTA_OUTBOX_VERSION 1u
64#define SOTA_OUTBOX_SLOTS 2u
67typedef struct {
68 char event[12];
69 char reason[24];
70 char deployment[64];
71 uint32_t build;
73
75typedef struct {
76 uint8_t version;
77 uint8_t count;
80
82static inline bool sota_copy_field(char *dst, size_t cap, const char *src)
83{
84 size_t len = src ? strlen(src) : 0;
85 if (len >= cap) return false;
86 if (len) memcpy(dst, src, len);
87 dst[len] = '\0';
88 return true;
89}
90
92static inline void sota_outbox_init(sota_outbox_t *box)
93{
94 memset(box, 0, sizeof(*box));
96}
97
99static inline bool sota_field_terminated(const char *field, size_t cap)
100{
101 return memchr(field, '\0', cap) != NULL;
102}
103
106static inline bool sota_outbox_valid(const sota_outbox_t *box)
107{
108 if (box->version != SOTA_OUTBOX_VERSION || box->count > SOTA_OUTBOX_SLOTS) return false;
109 for (uint8_t i = 0; i < box->count; ++i) {
110 const sota_report_t *r = &box->reports[i];
111 if (!sota_field_terminated(r->event, sizeof(r->event)) ||
112 !sota_field_terminated(r->reason, sizeof(r->reason)) ||
114 !r->event[0] || !r->deployment[0] || r->build == 0) {
115 return false;
116 }
117 }
118 return true;
119}
120
123static inline bool sota_outbox_push(sota_outbox_t *box, const char *event,
124 const char *reason, const char *deployment,
125 uint32_t build)
126{
128 memset(&r, 0, sizeof(r));
129 if (!event || !event[0] || !deployment || !deployment[0] || build == 0) return false;
130 if (!sota_copy_field(r.event, sizeof(r.event), event) ||
131 !sota_copy_field(r.reason, sizeof(r.reason), reason) ||
132 !sota_copy_field(r.deployment, sizeof(r.deployment), deployment)) {
133 return false;
134 }
135 r.build = build;
136 if (box->count == SOTA_OUTBOX_SLOTS) {
137 memmove(&box->reports[0], &box->reports[1],
138 sizeof(sota_report_t) * (SOTA_OUTBOX_SLOTS - 1));
139 box->count--;
140 }
141 box->reports[box->count++] = r;
142 return true;
143}
144
146static inline void sota_outbox_pop(sota_outbox_t *box)
147{
148 if (box->count == 0) return;
149 memmove(&box->reports[0], &box->reports[1],
150 sizeof(sota_report_t) * (SOTA_OUTBOX_SLOTS - 1));
151 box->count--;
152 memset(&box->reports[box->count], 0, sizeof(sota_report_t));
153}
154
155#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:99
static void sota_outbox_pop(sota_outbox_t *box)
Remove the report at the front.
Definition sota_report.h:146
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:106
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 bool sota_copy_field(char *dst, size_t cap, const char *src)
Copy src into a fixed field.
Definition sota_report.h:82
#define SOTA_OUTBOX_VERSION
Layout of the stored outbox blob.
Definition sota_report.h:63
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:64
#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:92
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:123
The outbox, oldest report first.
Definition sota_report.h:75
sota_report_t reports[2u]
The reports, oldest first.
Definition sota_report.h:78
uint8_t count
Reports in use.
Definition sota_report.h:77
uint8_t version
SOTA_OUTBOX_VERSION.
Definition sota_report.h:76
One status report waiting to be sent.
Definition sota_report.h:67
char deployment[64]
The deployment the report is about.
Definition sota_report.h:70
uint32_t build
The build the report is about.
Definition sota_report.h:71
char event[12]
"confirmed" or "rolled_back".
Definition sota_report.h:68
char reason[24]
Empty when the event has none.
Definition sota_report.h:69