Skip to content

OTA Mechanism

A state machine drives every update, and a checkpoint file lets it resume after a restart or a power cut.

A State Machine Owns the Update

An update is long and can fail at any step. OTAStateMachine lists the allowed transitions, so the update can only move along them, and every failure has a defined state. The current state is always readable with GetOTAStatus and is published on events.infos after every transition.

OTAPlugin adds the work around the state machine: the download task, the version check and the cleanup of the package. A concrete plugin adds the connection, in connect and disconnect, and the install, in install_update and monitor_update.

The Checkpoint Survives Restarts

Entering a state that matters after a restart writes an OTASnapshot to the checkpoint file. The snapshot holds the state, the installed firmware and the pending update. Entering updated or update_successful deletes the file.

At startup the plugin enters bootstrapping, reads the checkpoint and resolves to the saved state. Three cases resume differently:

  • downloading resumes as paused. The download task is gone; ResumeOTADownload starts a new one.
  • updating is never saved, so an install cut short resumes in the state before it: update_ready or update_failed. The install can be sent again.
  • update_ready or update_failed whose package file is gone resumes as update_available, ready for a new download.

The checkpoint path comes from the plugin's [plugins.<id>] configuration table. The plugin id is known only after connect, so the state machine waits in bootstrapping until the plugin assigns the path. See Configuration File.

Restored Updates Pass the Version Check

The firmware may change while the checkpoint sits on disk, for example through a USB update. Before restoring, the pending update is checked with the same rule as NotifyOTAUpdate. An update the rule rejects is dropped: the checkpoint is deleted and the state machine resolves to updated.

The Download Task Drives the Download States

OTAPlugin runs the download as a TaskManager task and listens to its state changes. A completed task fires download_success, a failed one download_fail, and pausing or resuming the task fires pause or resume. The state machine never touches asyncio tasks directly.

Failures Have a Retry and a Way Back

State Retry Back to update_available Back to updated
download_failed StartOTADownload AbortOTAUpdate ResetOTAState
update_failed InstallOTAUpdate AbortOTAUpdate ResetOTAState

AbortOTAUpdate and CancelOTAUpdate delete the package but keep the pending update, so the next step is a new download. ResetOTAState also drops the pending update and the checkpoint; it is refused while updating.

Every command checks its transition before it touches anything, so a command sent at the wrong time is refused and changes nothing. A download that fails while paused moves to download_failed like any other failed download.

See also