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

The SimpleOTA client for ESP32, the same API in the ESP-IDF component and the Arduino library. More...

Go to the source code of this file.

Data Structures

struct  simpleota_event_t
 One lifecycle event, valid only during the event_cb call. More...
 
struct  simpleota_signing_key_t
 A pinned Ed25519 public key for signed-firmware verification. More...
 
struct  simpleota_config_t
 Configuration. More...
 

Macros

API version

The version of this API, not of either package: the minor version grows when something is added, the major one only for a breaking change.

Both the Arduino library and the ESP-IDF component ship this header unchanged.

#if SIMPLEOTA_API_VERSION >= SIMPLEOTA_API_VERSION_NUM(1, 1)
// use something added in 1.1
#endif
#define SIMPLEOTA_API_VERSION_MAJOR   1
 Breaking changes.
 
#define SIMPLEOTA_API_VERSION_MINOR   2
 Additions.
 
#define SIMPLEOTA_API_VERSION_NUM(major, minor)   ((major) * 1000 + (minor))
 A version as one comparable number.
 
#define SIMPLEOTA_API_VERSION    SIMPLEOTA_API_VERSION_NUM(SIMPLEOTA_API_VERSION_MAJOR, SIMPLEOTA_API_VERSION_MINOR)
 This header's version, as SIMPLEOTA_API_VERSION_NUM() gives it.
 

Enumerations

enum  simpleota_security_t { SIMPLEOTA_SECURITY_NONE = 0 , SIMPLEOTA_SECURITY_BASIC , SIMPLEOTA_SECURITY_SIGNED , SIMPLEOTA_SECURITY_STRICT }
 The security mode a device reports on check-ins. More...
 
enum  simpleota_result_t {
  SIMPLEOTA_OK = 0 , SIMPLEOTA_CHECKSUM_FAIL = 1 , SIMPLEOTA_FLASH_FAIL = 2 , SIMPLEOTA_NO_OFFER = 3 ,
  SIMPLEOTA_SIGNATURE_FAIL = 4 , SIMPLEOTA_REBOOT_PENDING = 5 , SIMPLEOTA_UNCONFIRMED = 6 , SIMPLEOTA_DOWNGRADE = 7
}
 What simpleota_apply() did. More...
 
enum  simpleota_event_id_t {
  SIMPLEOTA_EVENT_CHECK_DONE , SIMPLEOTA_EVENT_UPDATE_AVAILABLE , SIMPLEOTA_EVENT_DOWNLOAD_PROGRESS , SIMPLEOTA_EVENT_UPDATE_FAILED ,
  SIMPLEOTA_EVENT_BEFORE_REBOOT , SIMPLEOTA_EVENT_TRIAL_BOOT , SIMPLEOTA_EVENT_CONFIRMED , SIMPLEOTA_EVENT_ROLLING_BACK
}
 Events delivered to simpleota_config_t::event_cb. More...
 

Functions

esp_err_t simpleota_init (const simpleota_config_t *config)
 Initialize the client: copy config, apply defaults, parse and pin signing keys, load persisted state, and run boot-time trial validation (detects rollbacks, arms the confirm timeout for trial boots).
 
esp_err_t simpleota_start (void)
 Start the managed background task: check on the interval, apply what is offered, confirm a trial on its first successful check-in (unless manual_confirm), and post reports.
 
esp_err_t simpleota_stop (void)
 Stop the managed task.
 
esp_err_t simpleota_check_now (void)
 Wake the managed task to check immediately.
 
esp_err_t simpleota_check (bool *update_available)
 Polling mode: check in, and keep any offer for simpleota_apply().
 
simpleota_result_t simpleota_apply (void)
 Polling mode: download, verify and flash the offer from the last simpleota_check(), then reboot unless disable_auto_reboot is set.
 
esp_err_t simpleota_set_server_url (const char *url)
 Point check-ins and status posts at another SimpleOTA server.
 
const char * simpleota_last_offered_version (void)
 
esp_err_t simpleota_confirm (void)
 Confirm the running trial image, after the app's own health checks pass (manual_confirm, or polling mode).
 
esp_err_t simpleota_reboot_for_update (void)
 Reboot into a downloaded update when disable_auto_reboot was set.
 
bool simpleota_is_trial_boot (void)
 
uint32_t simpleota_current_build (void)
 
esp_err_t simpleota_deinit (void)
 Stop the managed task if needed and release all resources.
 

Detailed Description

The SimpleOTA client for ESP32, the same API in the ESP-IDF component and the Arduino library.

Thin client for the SimpleOTA firmware update platform (https://simpleota.com). Polls the check endpoint, streams and flashes offered firmware with on-the-fly SHA-256 and Ed25519 signature verification, reports lifecycle events, and runs every new image as a trial that rolls back unless it is confirmed.

The application owns network bring-up (Wi-Fi/Ethernet) and decides when the client may talk to the network via the optional is_connected callback; the client does the rest from its own background task (managed mode). In polling mode the application calls simpleota_check() and simpleota_apply() from its own task instead, and confirms a trial with simpleota_confirm(). Each runs on a short-lived task with CONFIG_SIMPLEOTA_TASK_STACK_SIZE while the caller waits, so the caller's stack size does not matter.

Quick start:

simpleota_config_t cfg = {0}; // {} in an Arduino sketch
cfg.token = "sota_pk_..."; // project (or device) token
cfg.is_connected = wifi_up; // or start after the IP arrives
ESP_ERROR_CHECK(simpleota_init(&cfg));
ESP_ERROR_CHECK(simpleota_start());
esp_err_t simpleota_init(const simpleota_config_t *config)
Initialize the client: copy config, apply defaults, parse and pin signing keys, load persisted state,...
esp_err_t simpleota_start(void)
Start the managed background task: check on the interval, apply what is offered, confirm a trial on i...
Configuration.
Definition simpleota.h:201
bool(* is_connected)(void *user_ctx)
Return false while the link is down (e.g.
Definition simpleota.h:266
const char * token
Required.
Definition simpleota.h:202

The device needs a partition table with two OTA app slots, which Arduino-ESP32's default has. On ESP-IDF, rollback by the bootloader needs CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y, which Arduino-ESP32 sets. Both ship with the first serial flash and cannot change over OTA. Without bootloader rollback the client rolls a failed trial back itself (CONFIG_SIMPLEOTA_SOFTWARE_ROLLBACK): on the confirm timeout, or on any reset before the confirm, as the bootloader would. It cannot catch an image that crashes before simpleota_init() on every boot.

The client is a process-wide singleton (one instance per device); a second simpleota_init() returns ESP_ERR_INVALID_STATE.

Thread-safety: configure and init from one task. After init the public functions are safe to call from any task, except that polling mode and simpleota_set_server_url() belong to one task of the application's.

Logs use the tag "simpleota". Warnings and errors show by default; esp_log_level_set("simpleota", ESP_LOG_DEBUG) after simpleota_init() shows every step.

Compatibility, which every later 1.x release keeps:

  • A configuration field left at zero (or NULL, or false) gets its default. New fields follow the same rule, so a config filled from {0} keeps building and keeps its behaviour.
  • Functions, fields, result values and event ids are only added, never changed or removed; check SIMPLEOTA_API_VERSION before using a newer one.
  • The reason tokens listed at simpleota_event_t are stable. New tokens may be added, so treat an unknown one as a generic failure.

Enumeration Type Documentation

◆ simpleota_security_t

The security mode a device reports on check-ins.

The server offers it only artifacts of the same mode, or any artifact when it reports none.

Enumerator
SIMPLEOTA_SECURITY_NONE 

Report none: every artifact matches.

SIMPLEOTA_SECURITY_BASIC 

Unsigned artifacts, verified by checksum.

SIMPLEOTA_SECURITY_SIGNED 

Signed artifacts only, each verified against a pinned key before it can boot.

Needs at least one signing key.

SIMPLEOTA_SECURITY_STRICT 

SIGNED, and each update must also carry a strict signature: a second signature, made by the SimpleOTA CLI with the same key, over the project id, the signing key id, the build number and the image's SHA-256.

The server then cannot pass an old signed image off as a newer build, or offer another project's image when projects share a key. Needs a signing key, project_id, and builds the CLI has made strict-ready. Since API 1.2.

◆ simpleota_result_t

What simpleota_apply() did.

The values are stable across releases.

Enumerator
SIMPLEOTA_OK 

Flashed and verified; the reboot is next.

SIMPLEOTA_CHECKSUM_FAIL 

The download did not match its SHA-256.

SIMPLEOTA_FLASH_FAIL 

Network, URL, flash or state write failure.

SIMPLEOTA_NO_OFFER 

No offer from a successful simpleota_check().

SIMPLEOTA_SIGNATURE_FAIL 

Required signature missing or wrong.

SIMPLEOTA_REBOOT_PENDING 

A flashed update waits for simpleota_reboot_for_update().

SIMPLEOTA_UNCONFIRMED 

The running image is on trial: confirm it, then apply again.

The offer is kept.

SIMPLEOTA_DOWNGRADE 

The offered build is not above the running one; refused (anti-rollback).

◆ simpleota_event_id_t

Events delivered to simpleota_config_t::event_cb.

Enumerator
SIMPLEOTA_EVENT_CHECK_DONE 

A check completed; no update offered.

reason: the server's status.

SIMPLEOTA_EVENT_UPDATE_AVAILABLE 

An update was offered.

The managed task downloads it next, unless a trial runs.

SIMPLEOTA_EVENT_DOWNLOAD_PROGRESS 

bytes_done / bytes_total advanced.

SIMPLEOTA_EVENT_UPDATE_FAILED 

Update aborted; reason carries the token.

SIMPLEOTA_EVENT_BEFORE_REBOOT 

Flashed + verified.

Restart is imminent, or deferred until simpleota_reboot_for_update() when disable_auto_reboot is set.

SIMPLEOTA_EVENT_TRIAL_BOOT 

Running an unconfirmed (trial) image.

SIMPLEOTA_EVENT_CONFIRMED 

The trial image's confirmed report was delivered.

SIMPLEOTA_EVENT_ROLLING_BACK 

The trial failed; the previous image starts next.

reason says why.

Function Documentation

◆ simpleota_init()

esp_err_t simpleota_init ( const simpleota_config_t *  config)

Initialize the client: copy config, apply defaults, parse and pin signing keys, load persisted state, and run boot-time trial validation (detects rollbacks, arms the confirm timeout for trial boots).

Call it as early as possible, after nvs_flash_init() (Arduino-ESP32 has already called it): on a trial boot the confirm timeout starts here.

Parameters
configThe configuration; copied, so it need not outlive the call.
Return values
ESP_OKReady.
ESP_ERR_INVALID_ARGA missing token or one with control characters, an unparseable signing key, a security_mode out of range, a signed mode without a key, STRICT without a project_id, a project_id that is not a lowercase hyphenated UUID, a base_url that is not https://, or a signing key or signed mode in a build without signed updates.
ESP_ERR_INVALID_STATEAlready initialized, or neither the certificate bundle nor a cert_pem is available.
ESP_ERR_NO_MEMOut of memory.

◆ simpleota_start()

esp_err_t simpleota_start ( void  )

Start the managed background task: check on the interval, apply what is offered, confirm a trial on its first successful check-in (unless manual_confirm), and post reports.

On a trial boot the confirm timeout rolls back also without the task. Calling it again does nothing.

Return values
ESP_OKRunning.
ESP_ERR_INVALID_STATENot initialized.
ESP_ERR_NO_MEMThe task could not be created.

◆ simpleota_stop()

esp_err_t simpleota_stop ( void  )

Stop the managed task.

Waits for its current step, up to 60 s, so a download or flash write is never cut short.

Return values
ESP_OKStopped, or was not running.
ESP_ERR_TIMEOUTThe task is still running after 60 s.

◆ simpleota_check_now()

esp_err_t simpleota_check_now ( void  )

Wake the managed task to check immediately.

Return values
ESP_OKThe task was woken.
ESP_ERR_INVALID_STATEThe managed task is not running.

◆ simpleota_check()

esp_err_t simpleota_check ( bool *  update_available)

Polling mode: check in, and keep any offer for simpleota_apply().

Sends queued reports first, except during a trial.

bool update = false;
if (simpleota_check(&update) == ESP_OK && update) simpleota_apply();
esp_err_t simpleota_check(bool *update_available)
Polling mode: check in, and keep any offer for simpleota_apply().
simpleota_result_t simpleota_apply(void)
Polling mode: download, verify and flash the offer from the last simpleota_check(),...
Parameters
[out]update_availableSet to true when an update is offered, and to false otherwise, whatever is returned.
Return values
ESP_OKThe server answered. An offer it sent that cannot be used logs a warning and counts as no update.
ESP_FAILNo response, or not a 2xx: try again later.
ESP_ERR_INVALID_ARGupdate_available is NULL.
ESP_ERR_INVALID_STATENot initialized, or (with a warning) the managed task runs.
ESP_ERR_NO_MEMNo memory for the task the check runs on.

◆ simpleota_apply()

simpleota_result_t simpleota_apply ( void  )

Polling mode: download, verify and flash the offer from the last simpleota_check(), then reboot unless disable_auto_reboot is set.

Returns
What happened. With auto-reboot, a successful apply restarts the device before it returns. SIMPLEOTA_NO_OFFER, with a warning, while the managed task runs. SIMPLEOTA_FLASH_FAIL, with an error and the offer kept, when there is no memory for its task.

◆ simpleota_set_server_url()

esp_err_t simpleota_set_server_url ( const char *  url)

Point check-ins and status posts at another SimpleOTA server.

Firmware downloads still use the offer's pre-signed URL. Not while simpleota_check() or simpleota_apply() runs on another task.

Parameters
urlThe new base URL; same rules as base_url: https:// with a host.
Return values
ESP_OKSwitched.
ESP_ERR_INVALID_ARGRejected; the previous URL is kept.
ESP_ERR_INVALID_STATEBefore init, or while the managed task runs.
ESP_ERR_NO_MEMOut of memory; the previous URL is kept.

◆ simpleota_last_offered_version()

const char * simpleota_last_offered_version ( void  )
Returns
The version label of the last offer, or "" when there is none. Valid until the next check.

◆ simpleota_confirm()

esp_err_t simpleota_confirm ( void  )

Confirm the running trial image, after the app's own health checks pass (manual_confirm, or polling mode).

Marks the image valid in the bootloader, cancels the confirm timer, and queues the "confirmed" report.

Return values
ESP_OKConfirmed, or there is no trial.
ESP_FAILThe bootloader could not record the image as good; the trial keeps running, so call again.
ESP_ERR_INVALID_STATENot initialized.

◆ simpleota_reboot_for_update()

esp_err_t simpleota_reboot_for_update ( void  )

Reboot into a downloaded update when disable_auto_reboot was set.

Until then the client does not download another update, because the next update slot holds the one waiting for this reboot.

Stops the background task, posts the "reboot" status from a short-lived task with CONFIG_SIMPLEOTA_TASK_STACK_SIZE (so any caller's stack is enough), and restarts.

Return values
ESP_ERR_INVALID_STATENo update is waiting. Otherwise it does not return.

◆ simpleota_is_trial_boot()

bool simpleota_is_trial_boot ( void  )
Returns
true while running an unconfirmed trial image.

◆ simpleota_current_build()

uint32_t simpleota_current_build ( void  )
Returns
The build number this device reports: 0 before its first update from SimpleOTA, and before simpleota_init().

◆ simpleota_deinit()

esp_err_t simpleota_deinit ( void  )

Stop the managed task if needed and release all resources.

Mainly for tests.

Return values
ESP_OKReleased; simpleota_init() may run again.
ESP_ERR_TIMEOUTThe task did not stop; nothing is released.
ESP_ERR_INVALID_STATECalled from event_cb on the managed task.