NervesHubLink.Extensions behaviour (nerves_hub_link v2.14.0)

Copy Markdown View Source

Extensions are a mechanism for transmitting messages for non-critical functionality over the existing NervesHub Socket. An extension will only attach if the server-side requests it from the device to ensure it will not disrupt regular operation.

This module provides a behaviour with a macro to use for implementing an Extension.

Extensions are started as separate GenServers under a DynamicSupervisor and any messages namespaced for a specific extension will be forwarded to that extension's GenServer.

Summary

Callbacks

Invoked when routing an Extension event

Functions

Attach specified extensions

Returns a specification to start this module under a supervisor.

The extension modules this device is configured with.

Detach specified extensions

List extensions currently available

What to offer on the extensions join, given what the platform says it has.

Callbacks

handle_event(t, map, state)

@callback handle_event(String.t(), map(), state :: term()) ::
  {:noreply, new_state}
  | {:noreply, new_state,
     timeout() | :hibernate | {:continue, continue_arg :: term()}}
  | {:stop, reason :: term(), new_state}
when new_state: term()

Invoked when routing an Extension event

Behaves the same as GenServer.handle_info/2

Functions

attach(extension)

@spec attach(String.t() | [String.t()] | :all) :: :ok

Attach specified extensions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

configured_modules()

@spec configured_modules() :: [module()]

The extension modules this device is configured with.

The configured list replaces the defaults rather than adding to them, which is what makes this worth asking for rather than reading the config directly.

detach(extension)

@spec detach(String.t() | [String.t()] | :all) :: :ok

Detach specified extensions

Also supports :all as an argument for cases NervesHubLink may want to detach all of them at once

handle_event(event, message)

@spec handle_event(String.t(), map()) :: :ok

list()

@spec list() :: [
  %{
    required(String.t()) => %{
      attached?: boolean(),
      attach_ref: String.t(),
      module: module(),
      version: boolean()
    }
  }
]

List extensions currently available

offer(advertisement)

@spec offer(%{required(String.t()) => [String.t()]} | nil) :: %{
  required(String.t()) => String.t()
}

What to offer on the extensions join, given what the platform says it has.

The platform sends its side in extensions:get, as %{name => versions}. This picks, for each extension both sides have, the version to declare, and returns the %{name => version} map the join carries.

Pass nil when the server asked without naming anything, which is what every NervesHub does until it learns to advertise. It still serves the versions it always did, so everything is offered at the version this client implements rather than nothing being offered at all.

Nothing is offered unless the server asks. Joining the extensions topic uninvited would be the device deciding it should be reporting, and that decision is the platform's.

An extension the platform did not name is left out. It either does not implement it or has it switched off, and either way there is nothing to attach.

One version per extension

This client implements exactly one version of each extension, so choosing is only ever a question of whether the platform has that one. Giving an extension a second version means keying the registry by name and version rather than by name alone, since find_extensions/0 would otherwise keep whichever module it saw last.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()