SimpleOTA client v1.2.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_engine.h File Reference

The update engine both clients run: boot validation, check-ins, applying an update, the trial and its confirm or rollback, and the deferred reports. More...

Go to the source code of this file.

Data Structures

struct  sota_config_t
 Strings are borrowed: they must outlive the engine. More...
 
struct  sota_engine_t
 One engine's state. More...
 

Macros

#define SOTA_DEFAULT_CHECK_INTERVAL_S   3600u
 Seconds between check-ins.
 
#define SOTA_DEFAULT_CONFIRM_TIMEOUT_S   300u
 Seconds a trial has to confirm.
 
#define SOTA_DEFAULT_HTTP_TIMEOUT_MS   15000u
 Per-request timeout.
 
#define SOTA_TRIAL_RETRY_INTERVAL_S   10u
 Seconds between check-ins during a trial, and while a report waits for one of its quick retries.
 

Enumerations

enum  sota_apply_result_t {
  SOTA_APPLY_OK = 0 , SOTA_APPLY_CHECKSUM_FAIL = 1 , SOTA_APPLY_FLASH_FAIL = 2 , SOTA_APPLY_NO_OFFER = 3 ,
  SOTA_APPLY_SIGNATURE_FAIL = 4 , SOTA_APPLY_REBOOT_PENDING = 5 , SOTA_APPLY_UNCONFIRMED = 6 , SOTA_APPLY_DOWNGRADE = 7
}
 sota_engine_apply() results, the same values as simpleota_result_t. More...
 
enum  sota_confirm_result_t { SOTA_CONFIRM_DONE , SOTA_CONFIRM_NO_TRIAL , SOTA_CONFIRM_FAILED }
 What sota_engine_confirm() did. More...
 

Functions

bool sota_engine_init (sota_engine_t *e, const sota_config_t *cfg)
 Set up an engine.
 
bool sota_engine_add_key (sota_engine_t *e, const char *key_id, const char *pem)
 Pin an Ed25519 public key (PEM).
 
void sota_engine_validate_boot (sota_engine_t *e)
 Work out what this boot means for a saved trial: start supervising it, queue its report, or roll back.
 
bool sota_engine_check (sota_engine_t *e)
 Check in.
 
sota_apply_result_t sota_engine_apply (sota_engine_t *e)
 Download, verify and flash the offer.
 
sota_confirm_result_t sota_engine_confirm (sota_engine_t *e)
 Confirm the running trial image.
 
void sota_engine_timeout_fired (sota_engine_t *e)
 Called by the port's timer, from any context: note the timeout and sota_app_wake().
 
void sota_engine_service (sota_engine_t *e)
 Roll back when the confirm timeout has fired.
 
uint32_t sota_engine_step (sota_engine_t *e, bool connected)
 One pass of a managed task: service, then check and apply when connected.
 
bool sota_engine_reboot_for_update (sota_engine_t *e)
 With auto_reboot off: post "reboot" for the waiting update and restart.
 
void sota_engine_deinit (sota_engine_t *e)
 Stop the timer and the watchdog, free the offer, and let another engine be set up.
 
bool sota_engine_update_waiting (void)
 A flashed update waits for its reboot (for this boot, every engine).
 
bool sota_engine_hold_pending_image (void)
 For Arduino's verifyRollbackLater(): the running image is on probation and is a SimpleOTA trial.
 

Detailed Description

The update engine both clients run: boot validation, check-ins, applying an update, the trial and its confirm or rollback, and the deferred reports.

The public API layer (port/esp32/simpleota.c) owns a sota_engine_t and calls these functions. A device runs one engine at a time (sota_engine_init()). The engine reaches the platform only through the link-time interfaces sota_kv.h, sota_image.h, sota_boot.h, sota_sys.h, sota_net.h and sota_app.h, which the port implements.

Polling use: sota_engine_check(), then sota_engine_apply() when it offers an update. A managed task calls sota_engine_step() and waits the seconds it returns, waking early on sota_app_wake().

Enumeration Type Documentation

◆ sota_apply_result_t

sota_engine_apply() results, the same values as simpleota_result_t.

Enumerator
SOTA_APPLY_OK 

Flashed; the reboot is next.

SOTA_APPLY_CHECKSUM_FAIL 

The download did not match its sha256.

SOTA_APPLY_FLASH_FAIL 

Network, URL, flash or state write failure.

SOTA_APPLY_NO_OFFER 

No offer from a successful check.

SOTA_APPLY_SIGNATURE_FAIL 

Required signature missing or wrong.

SOTA_APPLY_REBOOT_PENDING 

A flashed update waits for its reboot.

SOTA_APPLY_UNCONFIRMED 

The running image is on trial; the offer is kept.

SOTA_APPLY_DOWNGRADE 

The offer's build is not above the running one.

◆ sota_confirm_result_t

What sota_engine_confirm() did.

Enumerator
SOTA_CONFIRM_DONE 

The trial is over and its report queued.

SOTA_CONFIRM_NO_TRIAL 

Nothing to confirm, or a rollback is under way.

SOTA_CONFIRM_FAILED 

The bootloader could not record it; the trial goes on.

Function Documentation

◆ sota_engine_init()

bool sota_engine_init ( sota_engine_t *  e,
const sota_config_t *  cfg 
)

Set up an engine.

No storage or network access. False for a token or project id with control characters, an unusable server URL, or while another engine is set up: a device has one trial record, so it runs one engine until sota_engine_deinit().

◆ sota_engine_add_key()

bool sota_engine_add_key ( sota_engine_t *  e,
const char *  key_id,
const char *  pem 
)

Pin an Ed25519 public key (PEM).

key_id may be NULL. False when the PEM does not parse, key_id is over SOTA_KEY_ID_MAX, two keys are pinned already, or the build leaves signed updates out (SOTA_SIGNED_UPDATES).

◆ sota_engine_validate_boot()

void sota_engine_validate_boot ( sota_engine_t *  e)

Work out what this boot means for a saved trial: start supervising it, queue its report, or roll back.

Runs once; check() and apply() call it.

◆ sota_engine_check()

bool sota_engine_check ( sota_engine_t *  e)

Check in.

True when an update is offered. Sends queued reports first, except during a trial, and confirms a trial when auto_confirm is set.

◆ sota_engine_apply()

sota_apply_result_t sota_engine_apply ( sota_engine_t *  e)

Download, verify and flash the offer.

Restarts on success when auto_reboot is set.

◆ sota_engine_service()

void sota_engine_service ( sota_engine_t *  e)

Roll back when the confirm timeout has fired.

Call from a task that may block: the managed task, or the API layer after sota_app_wake().

◆ sota_engine_step()

uint32_t sota_engine_step ( sota_engine_t *  e,
bool  connected 
)

One pass of a managed task: service, then check and apply when connected.

Events report the outcome. Returns the seconds to wait before the next pass.

◆ sota_engine_reboot_for_update()

bool sota_engine_reboot_for_update ( sota_engine_t *  e)

With auto_reboot off: post "reboot" for the waiting update and restart.

Returns false, without restarting, when no update is waiting.

◆ sota_engine_hold_pending_image()

bool sota_engine_hold_pending_image ( void  )

For Arduino's verifyRollbackLater(): the running image is on probation and is a SimpleOTA trial.

Reads storage; NVS must be initialised.