A NervesHub device agent for AtomVM, targeting the ESP32.
Two working devices are in examples, one written in Erlang and one in Elixir.
A device does not run a stock AtomVM. The WebSocket transport is an ESP-IDF component, so a stock build cannot reach NervesHub at all, and building the VM from source is a prerequisite for everything below rather than for one feature.
Two edits beyond the transport, each buying one thing:
| Edit | Needed for |
|---|---|
Two packbeam partitions, main.avm and alt.avm |
Over-the-air updates |
AVM_USE_LIBSODIUM=ON, and a 16K main task stack |
Verifying firmware signatures |
Without the partitions a device still connects, reports what it is running,
answers the console and carries the extensions. It just has nowhere to put an
update that is not the partition it is executing from. Without libsodium a
device configured with firmware_keys reports verification_unavailable
rather than accepting an update it cannot check. Leaving either out is a
supported choice.
The three files those edits need ship in priv/atomvm.
After rebar3 compile the files are at
_build/default/lib/nerves_hub_link_atomvm_esp32/priv/atomvm:
priv=_build/default/lib/nerves_hub_link_atomvm_esp32/priv/atomvm
esp32=<AtomVM>/src/platforms/esp32
cp $priv/partitions.csv $esp32/partitions.csv
cp $priv/idf_component.yml $esp32/components/libatomvm/
cd $esp32
idf.py -DAVM_USE_LIBSODIUM=ON \
-DEXTRA_COMPONENT_DIRS=<transport> \
-DSDKCONFIG_DEFAULTS="sdkconfig.defaults;$priv/sdkconfig.defaults" \
set-target esp32
idf.py build
set-target rather than reconfigure, because it clears the build directory
and the generated sdkconfig first. CMake caches its component list, and a
plain idf.py build after adding a component reports success without ever
compiling it. That failure is silent, which is the one step worth not
improvising.
ESP-IDF v5.2 to v5.5 for either route. AtomVM does not build against v6:
mbedTLS 4.x moved mbedtls/ctr_drbg.h, and GCC 15 rejects the gperf-generated
tables.
cd <AtomVM>/src/platforms/esp32
idf.py -p /dev/ttyUSB0 flash
idf.py -p /dev/ttyUSB0 flash-elixir # or flash-erlang
Then the application, into main.avm. Read the offset off the device rather
than from a checkout, because writing to one from a stale copy of the table
lands the application inside boot.avm, and the only symptom is
Failed app start: invalid_avm:
esptool.py --chip esp32 --port /dev/ttyUSB0 read_flash 0x8000 0xC00 ptable.bin
gen_esp32part.py ptable.bin
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x1D0000,
boot.avm, data, phy, 0x1E0000, 0x90000,
main.avm, data, phy, 0x270000, 0xC8000,
alt.avm, data, phy, 0x338000, 0xC8000,
main.avm and alt.avm must stay the same size as each other, since either
has to hold the archive. The names are not symmetric on purpose: esp32init
falls back to /dev/partition/by-name/main.avm when NVS holds no boot path, so
a device with nothing provisioned still boots. Naming them a and b would
mean writing NVS before a device would start at all.
boot.avm is 576K rather than the stock 512K because an Elixir device needs
elixir_esp32boot.avm, which is 530,784 bytes. factory is 1856K rather than
1792K because libsodium adds around 140K and the stock size overflows by
0x5130. An Erlang device without signature verification can have both back.
priv/atomvm/partitions.csv carries the same table with those reasons beside
each line.
{deps, [
{nerves_hub_link_atomvm_esp32, "~> 0.2"},
{atomvm_websocket_client,
{git, "https://github.com/nerves-hub/atomvm_websocket_client.git",
{branch, "main"}}}
]}.The transport is a separate dependency and is not optional: it is what the agent talks to NervesHub over. It is also an ESP-IDF component, so it has to be compiled into the VM as well as listed here. See building the VM.
It stays a git dependency because it is not on Hex: it is an ESP-IDF component first and an Erlang library second, and the half that matters is compiled into the VM rather than fetched by rebar3.
{ok, _Pid} = nerves_hub_link:start(#{
identifier => <<"my-device">>,
shared_secret => {Key, Secret}
}).The calling process receives {nerves_hub, Event}:
| Event | Meaning |
|---|---|
{joined, Response} |
The device channel is live |
{join_error, Reason} |
The server refused the join |
{message, Event, Payload} |
A server message this library does not handle itself |
{update_started, Pid} |
An update is downloading |
{download_socket, open | closed, Largest} |
With keep_open_above: whether the socket stays open for the download |
{download_resumed, Offset} |
The download reconnected and asked for the rest from Offset |
{update_ready, Slot} |
Written and armed; reboot when convenient. Heard again from an agent started before the reboot |
{update_failed, Reason} |
Refused, or the write failed |
{update_ignored, armed} |
An offer while an update waits for the reboot |
{update_ignored, backing_off} |
The firmware that just failed, offered again before update_retry_backoff is over |
{update_decision_ignored, updating | armed} |
apply_update/2 while one downloads or waits for the reboot |
{firmware_committed, Slot} |
The running update proved itself |
{firmware_trial, Slot, Boot} |
Booted firmware that has not proved itself yet |
{firmware_reverted, Slot} |
It did not; the device boots Slot next |
{update_mode, Mode, Allowed} |
NervesHub reported the update mode |
{update_rejected, Reason} |
NervesHub refused request_update/1 |
identify |
Blink something |
reboot_requested |
Only with reboot => manual |
console_joined |
Someone opened the console |
{extensions_attached, Names} |
Which extensions NervesHub turned on |
{extension_detached, Name} |
NervesHub stopped one it does not know |
{disconnected, Reason} |
The socket dropped; the agent reconnects |
{transport_error, Reason} |
TLS or network failure |
And reports back:
nerves_hub_link:update_progress(Pid, 42, <<"downloading">>),
nerves_hub_link:firmware_validated(Pid),
nerves_hub_link:update_failed(Pid, <<"flash write failed">>).| Config | Result |
|---|---|
| (nothing) | wss://devices.nervescloud.com/device-socket/websocket?vsn=2.0.0 |
host => "nh.example.com" |
wss://nh.example.com/device-socket/websocket?vsn=2.0.0 |
url => "ws://192.168.1.10:4000" |
ws://192.168.1.10:4000/device-socket/websocket?vsn=2.0.0 |
Anything already written is kept, so a URL given in full passes through untouched
and an unusual mount point survives. Giving both url and host will raise an error.
Either shared_secret => {Key, Secret} or client_cert => {CertPem, KeyPem},
an organization's choice, and NervesHub accepts both. One of them is required.
A shared-secret signature is time-bound: NervesHub refuses one signed more than
90 seconds ago. An ESP32 boots at the epoch, so the clock has to be set before
connecting or every signature is decades stale. start/1 refuses with
{error, {clock_not_set, Now}} rather than letting the socket answer a bare
401.
| Option | Default | |
|---|---|---|
identifier |
required | The device's name on NervesHub |
shared_secret |
{Key, Secret} |
|
client_cert |
{CertPem, KeyPem} |
|
host |
devices.nervescloud.com |
|
url |
A URL in as much detail as you like | |
verify |
crt_bundle |
Or {cacert_pem, Pem}, or none |
firmware |
boot |
Where the running firmware's description comes from |
firmware_keys |
none | Public keys; configuring any requires signatures |
request_firmware_keys |
false |
Also ask the server for the org's keys |
updates |
auto |
manual reports the offer and does nothing |
reboot |
auto |
manual reports reboot_requested instead |
console |
false |
Join NervesHub's console channel |
console_commands |
[] |
The application's own console commands. See the console |
extensions |
none | all, or any of health, geo, logging |
location |
none | geo's answer, a function returning the payload (or undefined) instead of a GeoIP lookup |
capture_io |
false |
Send what the application prints |
register |
none | Register the agent under a name |
handler |
the caller | Where {nerves_hub, Event} goes |
heartbeat_ms |
30000 | |
reconnect_backoff |
{1000, 60000} |
First and longest wait between reconnects, in ms |
firmware_trial |
#{boot_attempts => 3, join_timeout_ms => 300000} |
Or off. See on trial |
network_interface |
<<"wlan0">> |
What the device reports it connects over, or a function asked on each join |
log_flush_ms |
10000 | How long batched log lines wait |
log_buffer |
100 | Log lines held before the oldest are dropped |
ping_interval_ms |
60000 | WebSocket pings; the IDF on its own sends them every 10 s |
connect_timeout_ms |
30000 | How long a connection may take to come up |
download_idle_ms |
120000 | How long the agent waits to hear from a download before giving up on it. Never less than the downloader's own resumes need |
download_resume_after_ms |
10000 | How long a download connection may stay quiet before it is resumed. See updates |
download_resumes |
3 | Download connections in a row that bring nothing before giving up |
keep_open_above |
unset: open | Close the socket while an update downloads unless this many bytes are free in one block, or never to always close. See updates |
before_update |
none | A function run before a download starts, to free memory |
resume_update |
none | An offer kept from before a restart, downloaded once joined. See updates |
update_retry_backoff |
{30000, 3600000} |
First and longest wait before joining again after a failed update, in ms |
transport |
websocket_client |
The numbers are checked at start: one that is not a positive integer (or, for
the backoffs, a {Min, Max} pair of them with Min =< Max; for
keep_open_above, a non-negative integer or never) is
{error, {invalid_config, Key, Value}} from start_link, and so is a
resume_update that is not a map.
updates => auto downloads what NervesHub offers into whichever of the two
packbeam slots the device is not running from, arms it, and reports
{update_ready, Slot}. Rebooting is left to the application, because only it
knows whether the device is in the middle of something.
Writing to the inactive slot is what makes a failed update survivable: the running archive is never overwritten, so a refused or corrupt download leaves the device running what it had.
NervesHub hears how it goes as it goes: received when the update is accepted,
started as each download connection begins, progress while it downloads,
completed once it is armed, and failed with a reason if it is not.
An ESP32 without PSRAM may have room for one TLS session, not two: with the
socket open, the download's handshake can leave too little heap for the next
record on either connection, and the device aborts. keep_open_above => Bytes
closes the socket while an update downloads, unless the largest free block of
heap is at least Bytes as the download starts, and reopens it afterwards;
never always closes it. The owner hears {download_socket, open | closed, LargestFreeBlock}, and what NervesHub would have heard while the socket was
closed is sent, in order, once the device has joined again: the latest of each
status, so a download resumed many times is one started. Only a socket closed
for the download opens again at once; one lost during it keeps its reconnect
backoff. before_update runs before any of this, so the application can free
memory of its own first; if it returns anything but ok, or raises, the update
fails ({update_failed, {before_update, Result}}) and nothing is downloaded.
NervesHub offers an update again only when the device joins. After a failed
update the device joins again, after update_retry_backoff: 30 s, doubling
with each failure in a row up to an hour (plus up to half again, so devices
do not all come back at once), back to the start once an update succeeds.
Until then the firmware that failed is held: offered again sooner -- a socket
closed for the download opens again at once, so that the device stays
reachable, and NervesHub offers it on that join -- it is not downloaded, and
the owner hears {update_ignored, backing_off}. NervesHub hears of the failure
on that join. A different firmware is not held, and nor is an update the
application asks for with request_update/1.
An update armed and waiting for the reboot counts as busy: NervesHub offers it
again on the next join, and downloading it would write over a slot the device
needs. Until the reboot, an offer is ignored and the owner hears
{update_ignored, armed}; apply_update/2 is answered with
{update_decision_ignored, armed}, and request_update/1 with
{error, armed}. An agent started again before the reboot knows it too: it
does not take the armed update for one the device has booted (counting a boot,
committing it on the first join), and the owner hears {update_ready, Slot}
again. That takes a small registered process, nh_armed, which waits from the
arming until the reboot ends it.
NervesHub keeps a record of each update it sends, and ends it as abandoned
when the device joins without currently_downloading_uuid. So for as long as
NervesHub counts the update as inflight, every join carries the UUID of the
firmware being installed (the offer's firmware_meta UUID, as nerves_hub_link
sends): from the offer being taken until NervesHub has heard the update failed,
or, for one written and armed, until the device joins running it. That covers
the socket opening again after a download, a reconnect while the download runs
on its own connection, and an agent started again before the reboot (nh_armed
keeps the UUID). A socket closed for a download that failed still sends it on
its first join, which comes before the failure it reports.
A NervesHub that names its updates sends each offer with an update_id. Every
report about that update sends it back: received, started, progress,
completed and failed, including those held for the next join. The joins
above send it too, as current_update_id, and nh_armed keeps it with the
UUID. NervesHub then applies a report only to the update it names, so one that
arrives late cannot land on a newer update, and a join keeps that update in
particular, or reopens it if NervesHub had stopped waiting for it. An offer
without one, from an older NervesHub, leaves the key out of the reports and
the joins.
NervesHub sends an offer once, and not again while the update it started is in
progress. A device that restarts to download, with only the agent running so
the download has the heap to itself, needs the offer it was sent: keep the
Payload of {message, <<"update">>, Payload} and start the agent with
resume_update => Payload. The first join names it, so NervesHub keeps that
update (or reopens it, if it had stopped waiting), and once joined the agent
downloads it without waiting for an offer, as apply_update/2 would. An
update armed for the next boot comes first: the join names that one, and the
kept offer is refused with {update_decision_ignored, armed}. The signed URL
in an offer is good for a day.
A connection that goes quiet for resume_after_ms (10 s), fails or closes
short is dropped, and the rest is asked for from the last byte received with a
Range request. The download gives up only after resumes (3) connections in
a row that brought no new byte; one that brought any starts the count again, so
a slow link that keeps moving takes as many connections as it needs. After one
that brought nothing it waits resume_after_ms times that count. A 5xx counts
as a connection that brought nothing; a failure another connection would not
fix, such as a 404 or a flash write that failed, ends it at once. The agent
passes download_resume_after_ms and download_resumes from its config as
these two. The deadline covers each connection's DNS lookup, TCP connect, TLS
handshake and first byte as well as the reads: on a link whose handshake often
takes longer than 10 s, raise download_resume_after_ms. A download the agent
hears nothing from for download_idle_ms (two minutes) is given up on; a
resume counts as hearing from it. It never gives up sooner than the download's
own resumes allow, (download_resumes + 1) * download_resume_after_ms, so
raising the deadline for a slow link does not have the agent end a download
that is waiting to reconnect.
The connection is read by a process of its own, and a quiet one is ended by
killing that process: AtomVM's ssl:recv/2 has no deadline and cannot be
woken. Its socket and TLS session go with it, since AtomVM frees a process's
resources when it dies, so the next connection does not wait for room.
updates => manual reports the offer as {message, <<"update">>, Payload} and
waits. Answer it with one of:
nerves_hub_link:apply_update(Pid, Payload),
nerves_hub_link:ignore_update(Pid, <<"on battery">>),
nerves_hub_link:reschedule_update(Pid, 60, <<"in use">>).Ignoring holds further offers back for the deployment's penalty timeout, and
rescheduling asks for the same offer again after the delay, in whole minutes.
A delay under 5 minutes is sent as 5, as nerves_hub_link does. Either answer
names the latest offer by its update_id, when it has one.
A product can let its devices choose when to update. A device in
device_managed mode gets no pushes, and asks instead:
{ok, #{mode := device_managed}} = nerves_hub_link:set_update_mode(Pid, device_managed),
{ok, #{available := true}} = nerves_hub_link:check_for_update(Pid),
ok = nerves_hub_link:request_update(Pid).request_update/1 returns once NervesHub has sent the update and the download
has begun, and the result arrives as {update_ready, Slot} or
{update_failed, Reason} as for any other update. It installs even with
updates => manual, since asking was the decision. NervesHub refuses with
{error, no_update}, {error, no_deployment_group} and the like, and
set_update_mode/2 with {error, not_permitted} for a product that does not
allow it. update_mode/1 returns the mode NervesHub last reported without
asking again.
A new firmware is on trial until it reaches NervesHub. It has three boots to join, counted in NVS where a crash cannot reset the count, and five minutes on each. If it runs out of either, the device points back at the firmware it was running before and restarts into it. That firmware then tells NervesHub it is running because an update was reverted.
The count is kept when nerves_hub_link:start/1 runs, so a firmware that
crashes before calling it at all is not caught. Start the agent early, before
anything that could fail. firmware_trial => off turns all of this off;
reboot => manual reverts but leaves the restart to the application.
Updates need two packbeam partitions, main.avm and alt.avm. Stock AtomVM
has one, so a device built from the default table cannot be updated: there is
nowhere to write that is not the partition it is executing from.
The table is compiled into the AtomVM firmware, so it is decided when the VM is built and an application cannot change it later. See building the VM.
Configuring firmware_keys is what asks for signatures. A device with keys
refuses any update they do not cover, including one carrying no signature at
all, and refuses it before the boot path moves, so a rejected archive sits in
a slot nothing boots from. A device with no keys installs what it is sent.
The keys are the organization's existing fwup public keys. An fwup private key
is a 32-byte Ed25519 seed followed by its public key, and that trailing half is
byte for byte the .pub NervesHub already stores, so signing packbeams needs no
new key management.
Packbeam has no signature format of its own, so this defines one: an entry named
nerves_hub/signature appended last, carrying "NH1", a version, and 64 bytes.
The signed range is every byte before that entry begins. Because it is appended,
nothing before it moves, and both ends compute the same range without agreeing
on anything else. It is a data file, the class AtomVM skips when looking for
code, so a signed archive still boots on a stock VM.
nh-avm signs and verifies:
nh-avm sign --key fwup-key.priv --in app.avm --out app-signed.avm
nh-avm verify --key fwup-key.pub --in app-signed.avm
nh-avm keygen --priv my.priv --pub my.pub
Give it a path, never the key itself: a key in an environment variable is readable in process listings and tends to end up in CI logs.
nh_signature:sign/2 and nh_signature:private_key/1 are the same thing
without the command line, for anything that would rather not shell out.
Verification on the device needs an AtomVM built with AVM_USE_LIBSODIUM=ON,
which is off by default. Without it a device configured with keys reports
verification_unavailable rather than quietly accepting the update.
console => true joins NervesHub's console channel. On Nerves, nerves_hub_link
answers that channel with an IEx session. AtomVM has no shell, so this answers
with a fixed set of commands instead: help, info, firmware, memory,
partitions, net, geo, signature, uptime, reboot, clear. The up
and down arrows walk through the last lines entered.
console_commands adds the application's own:
console_commands => [
{<<"relays">>, <<"the relay states">>, fun(_Args) -> my_app:relays() end},
{<<"io">>, <<"di">>, <<"the digital inputs">>, fun(Args) -> my_app:inputs(Args) end}
]Fun gets the line's arguments and returns the output (iodata); a line end is
added if it has none. One that raises prints the error instead of taking the
console down. help lists the commands by section: the built-in ones are in
device and network, an application's goes in the section it names or in
application. One with a built-in's name replaces it; help itself cannot be
replaced. Names, sections and descriptions are binaries, and an entry that is
not is refused at start with {error, {invalid_console_command, Entry}}.
Support scripts run the built-in commands only, and clear prints nothing
there.
Fun runs in the agent's process. While it runs, the agent sends no heartbeat
and reads no frame, so it must be quick. It must not call nerves_hub_link:
those calls go to the agent and would wait on themselves until they time out.
A line is at most 256 bytes; what is typed past that is not kept.
It reports, and it reboots. It will not evaluate Erlang, and it is not a way in to a running system.
NervesHub's support scripts, and the connecting code it runs on every join, are Elixir on Nerves. There is nothing to evaluate them with here, so a script is console commands, one per line:
# What is this device running?
firmware
memory
Blank lines and # lines are skipped, and the first line that is not a command
fails the script, so one written for Nerves says it cannot run rather than
reporting nothing. reboot is refused in a script: in connecting code it would
restart the device every time it connected.
extensions => all attaches the five NervesHub extensions: health reports
memory and uptime, geo reports a location, logging carries log lines,
alarms sends the application's alarms as they are raised and cleared, and
error_reports sends crashes as structured reports.
After the device joins, NervesHub says which versions of each it speaks and the device joins with the newest both know. An operator can turn one off or on while the device is connected, and it stops or starts answering.
Logging speaks both formats. Against a NervesHub that has version 0.1.0, lines
are held and sent in batches of up to 100, every log_flush_ms or as soon as a
batch fills, which keeps a burst at boot inside NervesHub's rate limit. Lines
logged while disconnected are held too, up to log_buffer, and the first batch
after dropping any says how many went.
nh_logger is a logger handler that sends everything logged, and
nerves_hub_link:send_log/3 sends one line directly.
AtomVM's logger accepts a list or a map and raises badarg on anything else,
before any handler sees the event. A binary message does not produce a
mangled log line; it takes down the process that logged it.
logger:info("started"), %% ok, a list
logger:info(#{event => started}), %% ok, a map
logger:info(<<"started">>). %% badarg, and the caller diesThis bites Elixir hardest, because Logger.info("...") is muscle memory and
Elixir strings are binaries. Elixir has no Logger on AtomVM, so Elixir code
calls :logger directly and hits this on the first line it writes. Use a
charlist (~c"started") or a map, or call nerves_hub_link:send_log/3, which
takes a binary and is the safe path.
capture_io => true also sends what the application prints, which on AtomVM
is most of how code reports anything. It makes a capture process the group
leader, so io:format/2, IO.puts/1 and IO.inspect/1 reach NervesHub as well
as the console. Read nh_io_capture before turning it on: the failure mode of a
group leader is a printing process that waits forever, and there is one
interaction with logger_std_h that duplicates every logged line.
alarms => Fun returns the complete set raised right now, #{Name => Description}. Without the alarms extension it goes on the health report,
which NervesHub asks for hourly on an idle device. With it, the agent calls
Fun every alarms_interval_ms (30 s by default) and sends only what changed,
stamped with the time the alarm appeared; nh_ext_alarms has the details.
A Fun of one argument is given what it returned last time, as it returned
it (#{} the first time), so an alarm can clear at a different level from the
one that raised it:
alarms => fun(Raised) ->
Block = erlang:system_info(esp32_largest_free_block),
Limit = case maps:is_key(<<"LowMemory">>, Raised) of true -> 36000; false -> 32768 end,
case Block < Limit of
true -> #{<<"LowMemory">> => <<"heap fragmented">>};
false -> #{}
end
endKeep descriptions stable: a new description is sent as the alarm raised again.
A description may be undefined, and is then left out. Names and descriptions
are sent as strings, so low_memory and <<"low_memory">> are one alarm; a
set holding both sends only one of them.
Fun runs in the agent's process, for every health report and every
alarms_interval_ms. Keep it quick: while it runs, the agent sends no
heartbeat and reads no frame. It must not call nerves_hub_link, whose calls
go to the agent and would wait on themselves until they time out.
With error_reports, every crash nh_logger sees -- the crash report
proc_lib logs when a process exits abnormally -- also goes to NervesHub as a
report: kind, reason, frames and the device's free heap at that moment. The
platform groups reports of one bug into one issue and keeps them for 30 days,
where a log line is text kept for three. nh_logger has to be a logger
handler for this, as it is for logging.
An error the application caught and still wants seen goes with
nerves_hub_link:report_error/4:
nerves_hub_link:report_error(nerves_hub_link, error, {timeout, 5000}, #{
context => #{queue => uploads},
fingerprint => <<"payment-gateway">>
}).Reports are held, up to report_buffer (10 by default, the oldest dropped and
the drop reported), and sent in a batch at most once a second, NervesHub's
rate. Held through a disconnect, they go as soon as the extension is attached
again. NervesHub has the extension off by default; turn it on for the product.
The firmware NervesHub manages is the packbeam, not the ESP-IDF image underneath. That image is AtomVM itself, and it is replaced by a different mechanism on a different schedule.
firmware says where the description comes from:
| Value | Behaviour |
|---|---|
boot |
The packbeam AtomVM booted. Default |
{partition, Label} |
A named partition instead |
{metadata, Map} |
Supply it directly |
none |
Join without describing the firmware |
boot can be the default because it is not a guess. AtomVM's esp32init
records where it booted from in NVS under atomvm/boot_path, so the agent
reads the answer and stays right after an update that switched slots.
A device that cannot read its firmware description refuses to start rather than connecting without one, which would look like a healthy device that never needs updating.
nh_channel is the Phoenix channel protocol as a pure state machine: no
processes, no timers, no socket. Each call returns a new state and a list of
actions:
State0 = nh_channel:new(JoinParams),
{State1, Actions} = nh_channel:connected(State0), %% on every connection
{State2, Actions1} = nh_channel:handle_text(Frame, State1).{send, Binary} %% write this to the socket
{event, Term} %% tell the application this happened
A channel does not survive a socket reconnect, so connected/1 must be called
on every connection. A client that reconnects the socket without rejoining looks
healthy and silently stops receiving updates.
| Parameter | Source |
|---|---|
atomvm_app_name |
the packbeam's application name |
atomvm_app_version |
its vsn |
atomvm_avm_sha256 |
SHA-256 of the packbeam |
atomvm_version |
erlang:system_info(atomvm_version) |
device_api_version |
2.4.0, the NervesHub protocol it speaks |
meta.firmware_validated |
false while the firmware is on trial |
meta.firmware_auto_revert_detected |
true after a revert, until the next update proves itself |
currently_downloading_uuid |
the offer's firmware_meta UUID while an update is inflight; left out otherwise |
current_update_id |
the offer's update_id at the same times, when it has one; left out otherwise |
After joining it also reports report_network_interface, from
network_interface. A function there is asked on each join and as a download
starts, so a device that falls back from Ethernet to Wi-Fi or a modem reports
the network it is on now.
So the device reports what is actually running rather than a constant it was compiled with. There is no UUID: NervesHub derives one from the digest using the same rule it applied to the uploaded archive, which keeps the rule in one place where an agent cannot get it subtly wrong.
atomvm_app_version is the firmware. atomvm_version is the VM it runs on,
which no packbeam can know.
The digest has to cover the archive and nothing else, because that is what NervesHub hashed on upload, and a partition is much larger than the archive written into it.
The archive's own length is recoverable exactly. packbeam_api:write_packbeam/2
ends every archive with create_header(0, 0, <<"end">>), a zeroed 12-byte
header followed by "end\0", so the archive ends 16 bytes after the terminator
starts. nh_packbeam:byte_length/1 walks to it.
nh_flash does that walk against flash a chunk at a time, hashing as it goes
and keeping only the one entry it needs, so the archive is never held in memory.
nh_agent is the only long-lived process: it opens the socket, joins on every
connected, heartbeats on a deadline, and surfaces messages to its owner as
{nerves_hub, Event}. Downloads, location lookups and scripts each run in a
process of their own and report back to it.
It reconnects rather than leaving that to the transport. A shared-secret signature is good for 90 seconds, and a transport that reconnects by itself replays the headers it was opened with, so every reconnect after the first minute and a half would be refused. The agent opens the transport with its own reconnection off, and when the connection drops it waits a backoff with jitter and opens a new one, signed at that moment.
{ok, _} = nh_agent:start(#{
url => "wss://nh.example.com",
identifier => <<"my-device">>,
shared_secret => {Key, Secret},
metadata => Metadata,
verify => crt_bundle
}).The transport is a module rather than a hard dependency, which is what let the
agent be tested against a real NervesHub from a desktop before any of it ran on
a chip. websocket_client from
atomvm_websocket_client
is the one it uses on a device.
✅ means tested on an ESP32 against NervesHub.
| Shared-secret authentication | ✅ |
| Working the URL out from a host | ✅ |
| Channel join and heartbeat | ✅ |
| Packbeam parsing | ✅ cross-checked against NervesHub's own parser |
| Reading metadata from flash | ✅ digest matches what the server derived |
| Over-the-air updates | ✅ installed into the inactive slot, both directions |
| Signature verification | ✅ a signed archive installs, a tampered one is refused |
| Remote console | ✅ a fixed set of commands, since AtomVM has no shell |
| Health, geo and logging extensions | ✅ |
identify and reboot |
✅ |
| Reconnecting with fresh signatures | tested against fakes only |
| Resuming a dropped download | tested against fakes only |
| Reverting firmware that does not join | tested against fakes only |
| Support scripts | tested against fakes only |
| Device-managed updates | tested against fakes only |
| Extension negotiation and batched logging | tested against fakes only |
Capturing io:format and IO.puts |
✅ |
| Client certificates | accepted in config, never run on a device |
Transport comes from atomvm_websocket_client.
Apache-2.0 OR LGPL-2.1-or-later.