SimpleOTA client v1.2.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_wdt_calc.h
Go to the documentation of this file.
1
22#ifndef SOTA_WDT_CALC_H
23#define SOTA_WDT_CALC_H
24
25#include <stdbool.h>
26#include <stdint.h>
27
29#define SOTA_TRIAL_WDT_MARGIN_S 60u
30
37static inline uint32_t sota_wdt_floor_sec(uint32_t http_timeout_ms)
38{
39 uint64_t request_s = ((uint64_t)http_timeout_ms * 4u + 999u) / 1000u + 10u;
41 return request_s > UINT32_MAX ? UINT32_MAX : (uint32_t)request_s;
42}
43
45static inline uint32_t sota_wdt_margin_sec(uint32_t confirm_sec, uint32_t floor_sec)
46{
47 uint32_t tenth = confirm_sec / 10u;
48 return tenth > floor_sec ? tenth : floor_sec;
49}
50
59static inline uint32_t sota_wdt_stage_ticks(uint32_t confirm_sec, uint32_t floor_sec,
60 uint32_t cal_q13_19, uint32_t nominal_hz,
61 bool *clamped)
62{
63 uint64_t seconds = (uint64_t)confirm_sec + sota_wdt_margin_sec(confirm_sec, floor_sec);
64 uint64_t ticks;
65 if (cal_q13_19 != 0) {
66 uint64_t us = seconds * 1000000u; /* fits: seconds < 2^33 */
67 ticks = us > (UINT64_MAX >> 19) ? UINT64_MAX : (us << 19) / cal_q13_19;
68 } else {
69 ticks = seconds * (uint64_t)(nominal_hz ? nominal_hz : 1u);
70 }
71 *clamped = ticks > UINT32_MAX;
72 if (*clamped) return UINT32_MAX;
73 return ticks < 2 ? 2u : (uint32_t)ticks;
74}
75
76#endif /* SOTA_WDT_CALC_H */
static uint32_t sota_wdt_floor_sec(uint32_t http_timeout_ms)
Margin floor for a given HTTP timeout: SOTA_TRIAL_WDT_MARGIN_S, or more when one request can take lon...
Definition sota_wdt_calc.h:37
static uint32_t sota_wdt_stage_ticks(uint32_t confirm_sec, uint32_t floor_sec, uint32_t cal_q13_19, uint32_t nominal_hz, bool *clamped)
Stage-0 value, in slow-clock ticks, for the confirm timeout plus its margin.
Definition sota_wdt_calc.h:59
static uint32_t sota_wdt_margin_sec(uint32_t confirm_sec, uint32_t floor_sec)
Extra seconds on top of the confirm timeout.
Definition sota_wdt_calc.h:45
#define SOTA_TRIAL_WDT_MARGIN_S
Smallest extra seconds added to the confirm timeout.
Definition sota_wdt_calc.h:29