Supervision

By default :nerves_hub_link is an OTP application that starts NervesHubLink.Supervisor automatically and builds its configuration from the application environment (i.e. your config.exs). This is the historical behaviour and requires no changes.

If you'd prefer to own the connection's lifecycle - for example to start it only after some runtime setup, or to build the configuration programmatically — you can opt out of the automatic startup:

config :nerves_hub_link, start: :manual

With start: :manual, the application starts nothing on boot. Instead, add NervesHubLink.Supervisor to your own supervision tree:

children = [
  # ...
  NervesHubLink.Supervisor
]

When started without options, the supervisor still builds its configuration from the application environment via NervesHubLink.Configurator.build/0, so config :nerves_hub_link, ... keeps working. To pass configuration explicitly instead, provide a NervesHubLink.Configurator.Config struct:

children = [
  {NervesHubLink.Supervisor, config: MyApp.build_nh_config()}
]

The default (start: :auto) and the manual mode share the same code path, so switching between them does not change how the connection is configured — only who is responsible for starting it.

Runtime configuration

NervesHubLink also supports runtime configuration via the NervesHubLink.Configurator behavior. This is called during application startup to build the configuration that is to be used for the connection. When implementing the behavior, you'll receive the initial default config read in from the application environment and you can modify it however you need.

This is useful for cases like:

  • selectively choosing which cert/key to use
  • reading a certificate file stored on the device which isn't available during compilation

For example:

defmodule MyApp.Configurator do
  @behaviour NervesHubLink.Configurator

  @impl NervesHubLink.Configurator
  def build(config) do
    ssl = [certfile: "/root/ssl/cert.pem", keyfile: "/root/ssl/key.pem"]
    %{config | ssl: ssl}
  end
end

Then you specify which configurator NervesHubLink should use in config.exs:

config :nerves_hub_link, configurator: MyApp.Configurator

Retrying firmware downloads

Firmware and Archive downloads are resilient to network issues and can be retried automatically.

You can configure how the Downloader handles timeouts, disconnections, and other aspects of the retry logic by adding the following configuration to your application's config file:

config :nerves_hub_link, :retry_config,
  max_disconnects: 20,
  idle_timeout: 75_000,
  max_timeout: 10_800_000

For more information about the configuration options, see the NervesHubLink.Downloader.RetryConfig module.

Choosing how firmware is downloaded and applied

NervesHubLink ships three update strategies. Each one downloads the firmware and hands it to fwup, but they differ in what they do with the bytes on the way through, and therefore in what an interrupted update costs.

StrategyFirmware on diskSurvives a restartVerifies the download
NervesHubLink.UpdateManager.StreamingUpdater (default)nonenofwup signature check only
NervesHubLink.UpdateManager.CachingUpdaterthe whole fileresumes from the size of the partial filefwup signature check only
NervesHubLink.UpdateManager.PartsUpdaterone file per partresumes from the last verified partevery part, as it arrives

The default streams straight into fwup, which needs no spare storage but starts again from nothing whenever a download is interrupted.

CachingUpdater keeps the download so it can be resumed, and trusts the size of what it already has. A part that was half written when the power went out is treated as good and only shows up much later as an fwup error.

PartsUpdater checks each part against the checksums NervesHub sends in the update payload, so a part that arrives corrupted is downloaded again on the spot, and a device that restarts mid-update keeps the parts it had already verified. This is the one to pick for devices on slow or unreliable connections. It needs a NervesHub server new enough to send partials_checksums; older servers fall back to a single unverified download with a warning.

config :nerves_hub_link,
  updater: NervesHubLink.UpdateManager.PartsUpdater

config :nerves_hub_link, NervesHubLink.UpdateManager.PartsUpdater,
  cache_dir: "/data/nerves_hub_link/firmware",
  part_size: 1_048_576,
  max_part_attempts: 3,
  max_concurrent_parts: 1

PartsUpdater can also download from several connections at once. Raising :max_concurrent_parts divides the parts still needed into that many contiguous ranges and fetches each one separately, which helps on links where a single connection can't fill the available bandwidth, such as high latency satellite or cellular. It costs a connection per range on the device and on whatever is serving the firmware, multiplied by every device in a deployment, so it stays at 1 unless you raise it.

See NervesHubLink.UpdateManager.PartsUpdater and NervesHubLink.UpdateManager.CachingUpdater for the options each one takes, and NervesHubLink.UpdateManager.Updater for writing your own.

Conditionally applying updates

It's not always appropriate to apply a firmware update immediately. Custom logic can be added to the device by implementing the NervesHubLink.Client behaviour and telling NervesHubLink to use it. eg.

defmodule MyApp.NervesHubLinkClient do
   @behaviour NervesHubLink.Client

   # May return:
   #  * `:apply` - apply the action immediately
   #  * `:ignore` - don't apply the action, don't ask again.
   #  * `{:reschedule, timeout_in_milliseconds}` - call this function again later.

   @impl NervesHubLink.Client
   def update_available(data) do
    if SomeInternalAPI.is_now_a_good_time_to_update?(data) do
      :apply
    else
      {:reschedule, 60_000}
    end
   end
end

To have NervesHubLink use it, update your config.exs as follows:

config :nerves_hub_link, client: MyApp.NervesHubLinkClient

Reporting update progress

See the previous section for implementing a client behaviour.

defmodule MyApp.NervesHubLinkClient do
  @behaviour NervesHubLink.Client
  #  argument can be:
  #   {:ok, non_neg_integer(), String.t()}
  #   {:warning, non_neg_integer(), String.t()}
  #   {:error, non_neg_integer(), String.t()}
  #   {:progress, 0..100}
  def handle_fwup_message({:ok, _, _}) do
    Logger.error("Firmware update complete")
    :ok
  end

  def handle_fwup_message({:warning, code, message}) do
    Logger.error("Warning while applying firmware update (#{code)}): #{message}")
    :ok
  end

  def handle_fwup_message({:error, _, message}) do
    Logger.error("Error while applying firmware update #(#{code}): {message}")
    :ok
  end

  def handle_fwup_message({:progress, progress}) when rem(progress, 10) do
    Logger.info("Update progress: #{progress}%")
    :ok
  end

  def handle_fwup_message({:progress, _}) do
    :ok
  end
end

Enabling remote IEx access

It's possible to remotely log into your device via the NervesHub web interface. This feature is disabled by default. To enable, add the following to your config.exs:

config :nerves_hub_link, remote_iex: true

The remote IEx process is started on the first data request from NervesHub and is terminated after 5 minutes of inactivity. You can adjust this by setting :remote_iex_timeout value in seconds in your config.exs:

config :nerves_hub_link, remote_iex_timeout: 900 # 15 minutes

You may also need additional permissions on NervesHub to see the device and to use the remote IEx feature.

Alarms

This application can set and clear the following alarms:

  • NervesHubLink.Disconnected
    • set: An issue is preventing a connection to NervesHub or one just hasn't been made yet
    • clear: Currently connected to NervesHub
  • NervesHubLink.UpdateInProgress
    • set: A new firmware update is being downloaded or applied
    • clear: No updates are happening

CA Certificates

The CA certificates installed on the device are used by default.

If you include the CAStore in your project, then that will be selected and used.

Otherwise you can configure nerves_hub_link to use custom CA certificates, which is useful if you are running your own NervesHub instance with self signed SSL certificates. Use the :ca_store option to specify a module with a ca_certs/0 function that returns a list of DER encoded certificates:

config :nerves_hub_link, ca_store: MyModule

Or if you have the certificates in DER format, you can also explicitly set them in the :ssl option:

my_der_list = [<<213, 34, 234, 53, 83, 8, 2, ...>>]
config :nerves_hub_link, ssl: [cacerts: my_der_list]

Similarly, the downloader can be configured to use custom CA certificates to establish the HTTP connection to the firmware download location.

my_der_list = [<<213, 34, 234, 53, 83, 8, 2, ...>>]
config :nerves_hub_link, downloader_ssl: [cacerts: my_der_list]

TLS 1.3 and session resumption

The device offers TLS 1.2 to NervesHub by default. A device talking to a NervesHub that supports TLS 1.3 can offer that as well, and turn on TLS 1.3 session tickets so a reconnection costs a resumed handshake rather than a full one. A resumed handshake sends no certificates and verifies none, which is worth having on a slow or metered connection such as LTE-M, where the bytes and the round trips of a full handshake are the expensive part of reconnecting:

config :nerves_hub_link,
  ssl: [
    versions: [:"tlsv1.3", :"tlsv1.2"],
    session_tickets: :auto
  ]

Both options are needed, as :ssl refuses session_tickets unless :"tlsv1.3" is one of the :versions. Keeping :"tlsv1.2" in the list means a device that reaches a server without TLS 1.3 still connects.

NervesCloud has TLS 1.3 enabled. NervesHub always issues stateless_with_cert session tickets when TLS 1.3 is enabled, so the ticket carries the device's certificate: a resumed handshake exchanges none, and that is what lets the server tell which device has reconnected. Devices authenticating with a certificate (NervesKey, TPM, or a local certificate and key) and devices authenticating with a shared secret can both resume.

Tickets are held in memory by the ssl application, so they outlive the socket reconnecting but not a reboot — the first connection after boot is always a full handshake. A ticket is matched on the server name, which is the host the device connects to, or :sni if you have configured one. Two ssl application variables bound the store, and their defaults (two hours, 1000 tickets) are generous for a device that only talks to one server:

config :ssl,
  client_session_ticket_lifetime: 7200,
  client_session_ticket_store_size: 10

The firmware downloader leaves the TLS version to Mint, which already offers TLS 1.3. It can resume handshakes too, which is worth having when firmware is downloaded in parts or a download is retried:

config :nerves_hub_link, downloader_ssl: [session_tickets: :auto]

HTTP proxy

Devices that have to reach NervesHub through an HTTP proxy can configure one for both the WebSocket connection and the firmware downloader. The option is passed through to Mint.HTTP.connect/4 as :proxy, so any Mint proxy form is accepted:

proxy = {:http, "proxy.example.com", 8080, []}

config :nerves_hub_link,
  socket: [http_opts: [proxy: proxy]],
  downloader_http_opts: [proxy: proxy]

http_opts (under :socket) and :downloader_http_opts forward any extra options that Mint.HTTP.connect/4 accepts — :proxy_headers, :hostname, etc. They merge on top of the SSL configuration described above, so you can combine them:

config :nerves_hub_link,
  ssl: [cacerts: my_der_list],
  socket: [http_opts: [proxy: {:http, "proxy.example.com", 8080, []}]]

Identifying the library to NervesHub

NervesHubLink names itself and its version in the User-Agent of the WebSocket connection and of firmware downloads, for example NHL/2.13.1. NervesHub records it for each connection, which shows you which version of NervesHubLink each device runs. A NervesHub that doesn't read it ignores it.

If the socket headers already have a User-Agent, for example one a custom configurator set, NervesHubLink leaves it alone and doesn't add its own.

The NervesHubLink.Configurator.SharedSecret configurator replaces the socket headers with its signature headers, so a User-Agent set in config :nerves_hub_link, socket: [headers: ...] is dropped and NervesHubLink sends its own. To send a different User-Agent with shared secret authentication, set it in a custom configurator that calls NervesHubLink.Configurator.SharedSecret.build/1 and then adds the header.

Message serialization

Messages between the device and NervesHub are encoded as JSON by default. Devices on metered or low bandwidth connections can use Msgpack instead, which encodes the same messages in fewer bytes and uses less memory doing it.

Msgpack needs the msgpax library, which is an optional dependency:

def deps() do
  [
    {:msgpax, "~> 2.0"}
  ]
end

and then:

config :nerves_hub_link, serializer: :msgpack

Your server has to support it too

The device requests the Msgpack protocol when it connects. A NervesHub server that doesn't support it will reject the connection, and a device that can't connect can't be sent the firmware that would change this setting back. Confirm your server supports Msgpack before rolling this out to a fleet.

If :msgpax is missing, or the configured serializer isn't one it knows, the device logs an error and connects with JSON rather than dropping off the network.

Note that the two formats don't put identical bytes on the wire for every value. Most notably, Msgpack carries timestamps — such as the one in a health report — as its own extension type, where JSON sends an ISO 8601 string.

Verifying network availability

NervesHubLink will attempt to verify that the network is available before initiating the first connection attempt. This is done by checking if the NervesHub host address (config.host) can be resolved. If the network isn't available then the check will be run again in 2 seconds.

You can disable this behaviour with the following config:

config :nerves_hub_link, connect_wait_for_network: false

Providing additional fwup arguments

To pass additional arguments supported by the fwup CLI, you can pass a list of strings via the :fwup_extra_options key.

config :nerves_hub_link, fwup_extra_options: ["--unsafe"],

To disable NervesHubLink connecting to NervesHub when testing, you can add:

config :nerves_hub_link, connect: false

to your config/test.exs