NervesHubLink.Client is the behaviour through which nerves_hub_link asks your application what to do with an update. Implement it when a device must not update at any moment, when you want to show progress, or when a reboot needs preparation. Without it, a device applies every update it is offered and reboots when the update finishes.
Requirements
The callbacks on this page work with anynerves_hub_link 2.x release, except where this table says otherwise.
Write a Client
Create a module that callsuse NervesHubLink.Client and override the callbacks you need. Every other callback keeps its default.
nerves_hub_link to use it:
MyApp.busy?/0, MyApp.Display and MyApp.shutdown_gracefully/0 stand for your own code.
You can use @behaviour NervesHubLink.Client instead of use. You then have to implement update_available/1, archive_available/1, archive_ready/2, handle_fwup_message/1, handle_error/1 and identify/0 yourself.
Decide whether to apply: update_available/1
nerves_hub_link calls update_available/1 when NervesHub offers an update, after it has confirmed it holds a public key to verify the firmware with. The argument is a NervesHubLink.Message.UpdateInfo struct:
Return one of:
The default implementation returns
:ignore when the offered firmware’s UUID equals the running firmware’s UUID, and :apply otherwise.
What ignoring and rescheduling do
nerves_hub_link does not keep a timer for a rescheduled update. It reports the decision to NervesHub and waits to be offered the update again.
- Rescheduled. The device asks NervesHub to hold the update back for the delay you returned. A delay in milliseconds is rounded to whole minutes. Delays under 5 minutes are raised to 5 minutes. NervesHub offers the update again after the delay.
- Ignored. NervesHub holds updates back from the device for the deployment group’s penalty timeout, as it does after a failed update. See rollout controls.
nerves_hub_link drops it and lets the running update finish.
Follow progress: handle_fwup_message/1
While an update is applied, nerves_hub_link passes each message from fwup to handle_fwup_message/1:
The return value is not checked. The default implementation logs each message.
The device also reports progress to NervesHub on its own. Download and install progress are sent as separate stages, at most once every 500 ms and only when the percentage has gone up. NervesHub shows this on the device’s Details tab.
While an update is in progress the
NervesHubLink.UpdateInProgress alarm is set. It is cleared when the update completes or fails.
Control the reboot: reboot/0
After fwup finishes, nerves_hub_link reports the update to NervesHub as completed and calls reboot/0 in a new process. If your Client does not define reboot/0, it calls Nerves.Runtime.reboot/0.
Define reboot/0 to notify users or stop work cleanly first. The new firmware does not run until the device reboots, so your implementation must end in a reboot.
Since nerves_hub_link 2.13.0 the same callback runs when someone reboots the device from NervesHub.
When an update fails
When a download gives up orfwup reports an error, nerves_hub_link:
- Clears the
NervesHubLink.UpdateInProgressalarm. - Logs the reason.
- Reports the update to NervesHub as failed, with a reason that starts with
Download failed,FWUP errororUpdate failed.
handle_fwup_message/1 receives the {:error, code, message} from fwup before the update stops.
handle_error/1 is not called for failed updates. In nerves_hub_link 2.14.0 it is called when the connection to NervesHub drops or the device channel closes, with the reason.Validation and auto-revert
After an update, a Nerves device boots the new firmware slot before that firmware has been marked valid. On systems that revert automatically, firmware that is not validated in time is abandoned and the device reboots into the previous slot. When the device connects, it sends:- Whether the running firmware is validated, from
firmware_validated?/0. - Whether an automatic revert just happened, from
firmware_auto_revert_detected?/0.
nerves_hub_link checks again every 10 seconds and tells NervesHub as soon as it is.
A device whose firmware is reported as not validated is not offered updates by its deployment group until it is.
When a revert is detected at startup, nerves_hub_link sets the NervesHubLink.FirmwareReverted alarm.
Defaults
Both callbacks are optional.
Override them if your
fwup.conf or fwup-ops.conf tracks validation or prevents reverts differently:
firmware_validated?/0 is treated as true and firmware_auto_revert_detected?/0 as false, with a warning in the log.
nerves_hub_link only reports validation. Your application still has to mark the firmware valid, for example with Nerves.Runtime.validate_firmware/0, once it has decided the firmware works.

