Skip to content

Trigger OTA via NATS

This guide covers driving a firmware update on a running service with the NATS CLI.

Prerequisites

  • The NATS CLI, configured for the device's NATS server
  • A service running an OTAPlugin. The examples use swupdate as the service name and the plugin id

Watch Status and Replies

Open two terminals and leave them running:

nats sub events.infos    # status events, one per state change
nats sub events.params   # command replies

Run an Update

  1. Notify the update:

    nats pub commands.swupdate.req \
      '[{"n":"NotifyOTAUpdate","vs":"{\"id\":\"swupdate\",\"fw_name\":\"firmware\",\"fw_version\":\"1.2.3\",\"url\":\"https://example.com/firmware/v1.2.3.swu\"}"}]'
    

    The status moves to update_available. fw_version must be newer than the installed firmware, unless the plugin allows an equal or older one. See OTA Commands.

  2. Download the package. The status moves to downloading, then update_ready:

    nats pub commands.swupdate.req '[{"n":"StartOTADownload","vs":"{\"id\":\"swupdate\"}"}]'
    
  3. Install it. The status moves to updating, then update_successful:

    nats pub commands.swupdate.req '[{"n":"InstallOTAUpdate","vs":"{\"id\":\"swupdate\"}"}]'
    

Read the Status

nats pub commands.swupdate.req '[{"n":"GetOTAStatus","vs":"{\"id\":\"swupdate\"}"}]'

To match a reply to its request, add --reply "<unique-id>". The reply on events.params carries the same id.

Pause and Resume a Download

nats pub commands.swupdate.req '[{"n":"PauseOTADownload","vs":"{\"id\":\"swupdate\"}"}]'
nats pub commands.swupdate.req '[{"n":"ResumeOTADownload","vs":"{\"id\":\"swupdate\"}"}]'

Recover from a Failure

Command From Result
AbortOTAUpdate download_failed, update_failed update_available, package deleted
CancelOTAUpdate downloading, paused, update_ready update_available, package deleted
ClearOTAUpdate update_available updated, pending update dropped
ResetOTAState Any except updating, update_successful updated, pending update and checkpoint dropped
nats pub commands.swupdate.req '[{"n":"AbortOTAUpdate","vs":"{\"id\":\"swupdate\"}"}]'

After AbortOTAUpdate or CancelOTAUpdate, download the package again before installing.

Troubleshooting

The status stays updated after NotifyOTAUpdate

The version check ignored the update: fw_version is equal to or older than the installed firmware. Send a newer version, or set allow_same_version or allow_downgrade in the plugin's configuration.

The reply has "result": "error"

The error field gives the reason. The common one is a command sent from a state that does not allow it; see the Valid from column in OTA Commands.

See also