SimpleOTA client v1.1.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_net.h
Go to the documentation of this file.
1
10#ifndef SOTA_NET_H
11#define SOTA_NET_H
12
13#include <stddef.h>
14#include <stdint.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
21#define SOTA_NET_MAX_REDIRECTS 3
22
24static inline int sota_net_is_redirect(int status)
25{
26 return status == 301 || status == 302 || status == 303 || status == 307 || status == 308;
27}
28
30enum {
32 SOTA_NET_AGAIN = -2,
34};
35
38static inline uint32_t sota_net_parse_retry_after(const char *value)
39{
40 uint32_t s = 0;
41 while (*value == ' ' || *value == '\t') ++value;
42 if (*value < '0' || *value > '9') return 0;
43 for (; *value >= '0' && *value <= '9'; ++value) {
44 uint32_t d = (uint32_t)(*value - '0');
45 s = s > (UINT32_MAX - d) / 10u ? UINT32_MAX : s * 10u + d;
46 }
47 while (*value == ' ' || *value == '\t') ++value;
48 return *value ? 0 : s;
49}
50
59int sota_net_post(void *user, const char *url, const char *token, const char *body,
60 char *resp, size_t cap, uint32_t *retry_after_s);
61
72int sota_net_dl_open(void *user, const char *url, int64_t *len, char *location,
73 size_t location_cap);
74
78int sota_net_dl_read(void *user, uint8_t *buf, size_t cap);
79
81void sota_net_dl_close(void *user);
82
83#ifdef __cplusplus
84}
85#endif
86
87#endif /* SOTA_NET_H */
int sota_net_post(void *user, const char *url, const char *token, const char *body, char *resp, size_t cap, uint32_t *retry_after_s)
POST body as JSON to url, with "Authorization: Bearer <token>".
void sota_net_dl_close(void *user)
End the download and free its connection.
int sota_net_dl_read(void *user, uint8_t *buf, size_t cap)
Read the next bytes of the body: a count, 0 once the whole body has arrived, SOTA_NET_AGAIN,...
static int sota_net_is_redirect(int status)
A status sota_net_dl_open() returns for a redirect.
Definition sota_net.h:24
int sota_net_dl_open(void *user, const char *url, int64_t *len, char *location, size_t location_cap)
Start a firmware download: a GET without the Authorization header (the pre-signed URL carries its own...
static uint32_t sota_net_parse_retry_after(const char *value)
A Retry-After header's value in seconds, saturated; 0 when it is not a plain number of seconds (the H...
Definition sota_net.h:38
@ SOTA_NET_ERROR
The connection failed.
Definition sota_net.h:31
@ SOTA_NET_AGAIN
No byte yet: the core calls again, and gives up with download_stalled after http_timeout_ms.
Definition sota_net.h:32