Protocol Reference
Overview
The Stow Protocol lets external clients discover and interact with the items in a device’s Stow. The firmware is the server (see Exposing the Stow to Clients), and this page describes what a client sends and receives. Each message is one CBOR array, and a transport carries each one whole, for example as a COBS frame over a serial link. The Python Stow client is a complete client implementation.
Key Features
Notably, the Stow Protocol implementation supports:
Set, Get, & Subscribe to Stow items, one or several per request
Retrieving a full description of the Stow, or of selected items (so that client need not be aware of the
stow.yamlin use by the firmware)Multiple concurrent external client sessions, with several requests in flight per session
Role-based access controls at the item level
Easy integration with arbitrary transport mediums and protocols
Messages
Every message is an array [CODE, TAG, ...]. Hello (code 0) and the version at the start of its response are fixed across protocol versions; everything else may change. Every code has a fixed direction, requests from client to server and the rest from server to client, and new codes must keep that so one stream can carry a session in each direction.
The Stow Protocol mostly follows a request-response pattern. However, Update messages arrive unsolicited.
The definition at the end of this page gives every message, error code and item description exactly, in CDDL; the sections below explain how they behave. Examples use CBOR diagnostic notation, which reads like JSON; use cbor.me to view the CBOR representation.
Message |
Description |
|---|---|
Hello |
Request the protocol version, the server’s limits and the session’s roles. |
Hello Response |
Returns the protocol version (currently |
Describe |
Request a chunk of the Stow description starting at a given item ID, optionally for named items only. |
Describe Response |
A chunk of CBOR-encoded item descriptions and the item to continue from. |
Get |
Get the current values of one or more items. |
Get Response |
Returns the ID and current value of each requested item. |
Set |
Set the values of one or more items. |
Subscribe |
Subscribe to value notifications for one or more items. |
Unsubscribe |
Unsubscribe from notifications for one or more items. |
Update |
Sent to the client when a subscribed item is set. |
OK |
Success response for operations without a dedicated response. |
Error |
Failure response with an error code, and the failing item when known. |
Delivery
Every request the server handles gets exactly one response, either its dedicated response, OK or Error.
The server does not acknowledge delivery or retry anything. A request is dropped without a response when the server is too busy to take it, for example when its queue or the transport’s buffers are full, so clients should apply a timeout to every request. A response or Update the transport cannot send is dropped as well.
Updates are best effort. A client that relies on an item’s value should Subscribe and then Get it, Get it again after a reconnect, and poll items where a stale value would matter.
Hello
Request the protocol version, the server’s limits and the roles of the session. The client sends it once when the session starts, before any other request.
The response starts with the protocol version, currently 1; a client that does not support it should stop. The map that follows holds:
hello-max-message: the largest message, in bytes, the server can send on this session. Clients keep every request within it.hello-stow-hash: theStowHashitem’s value, used to cache the Stow description.hello-roles: the bitmask of the roles the session holds.hello-role-names: the name of every role, indexed by its bit.
Clients should ignore keys they do not recognize.
Describe
Request a chunk of the Stow description starting at item start. Pass 0 to start from the beginning, then the next from each Describe Response. The description is complete when next is null.
The optional names list restricts the description to the named items. Unknown names are ignored, and a list that matches no item returns an empty chunk.
Any session can describe every item; permissions are enforced when an item is accessed.
The response’s chunk holds whole item descriptions as a CBOR sequence, so each chunk can be decoded on its own. The number of items per chunk depends on the size of the session’s buffers. When the next item cannot fit an otherwise empty response, the server answers err-too-large naming that item.
Get
Request the current values of one or more items. Returns a Get Response with each item and its value, in the order requested, or an Error for the first item that could not be read.
Set
Set the values of one or more items. Returns OK if every item was set. Items are set in order and the first failure stops processing with an Error; every item before the failing one was set and the rest were not.
Subscribe
Subscribe to receive Update messages when one or more items are set. Returns OK, or an Error for the first item that could not be subscribed; the subscriptions before it are active. Subscribing to an item twice is harmless.
Unsubscribe
Unsubscribe to stop receiving Update messages for one or more items. Returns OK or Error. Unsubscribing from an item that is not subscribed is harmless.
Error
Sent in response to failed operations. See Error Codes for possible errors.
item names the item that failed, when there is one. index is the zero-based position of an entry that failed as it was applied; the entries before it took effect and the rest did not. A request that fails before anything is applied, for example because it is malformed, carries no index and took no effect. Optional elements that do not apply are omitted. The tag is null when the server could not read the request’s tag.
A request that doesn’t match the CDDL is err-malformed-msg, before any check of its meaning, such as an unknown item, an unknown Describe start, a permission or a value’s constraints. A message that isn’t a request is err-unknown-msg when its tag is 0 to 23, and err-malformed-msg with a null tag otherwise.
Error Codes
Name |
Description |
|---|---|
|
The message was malformed. |
|
The message code was not recognized. |
|
The item ID was invalid. |
|
Not enough memory to handle the message. |
|
The client’s role(s) were insufficient. |
|
An unknown error occurred (should never occur). |
|
The value broke the item’s constraints or custom validator. |
|
The trust-on-first-use item has already been set. |
|
The response does not fit the server’s buffers. |
Client Interaction
Connecting to the Stow
External clients send Hello to verify the protocol version and learn the server’s limits, the session’s roles and the StowHash. The hash tells the client whether it already has the Stow description cached.
%%{init: {"theme": "neutral"}}%%
sequenceDiagram
participant C as Client
participant S as Stow Server
C->>S: Hello [0, 1]
S->>C: Hello Response [1, 1, 1, {..., 1: hash, ...}]
C->>C: Verify the version is 1
C->>C: Check if the Stow is cached based on the hash
If the description is not yet cached, the client should retrieve the Stow description.
Describing the Stow
The client pages through the Stow description by repeatedly sending Describe with the next returned by each response. The loop ends when next is null.
%%{init: {"theme": "neutral"}}%%
sequenceDiagram
participant C as Client
participant S as Stow Server
C->>S: Describe [2, 2, 0]
S->>C: Describe Response [3, 2, next_id, chunk_1]
C->>S: Describe [2, 3, next_id]
S->>C: Describe Response [3, 3, null, chunk_N]
Note over C: Decode the item descriptions in each chunk
Getting and Setting Items
%%{init: {"theme": "neutral"}}%%
sequenceDiagram
participant C as Client
participant S as Stow Server
C->>S: Get [4, 4, id_1, id_2]
S->>C: Get Response [5, 4, id_1, value_1, id_2, value_2]
C->>S: Set [6, 5, id_1, new_value]
S->>C: OK [10, 5]
C->>S: Set [6, 6, id_1, value_1, id_2, bad_value]
S->>C: Error [11, 6, err-invalid-value, id_2, 1]
Subscribing and Receiving Updates
Once subscribed, the server pushes Update messages to the client whenever the item is set. The client must unsubscribe explicitly, or close the session.
%%{init: {"theme": "neutral"}}%%
sequenceDiagram
participant C as Client
participant S as Stow Server
C->>S: Subscribe [7, 7, id_1, id_2]
S->>C: OK [10, 7]
Note over S: Item value is set
S-->>C: Update [9, null, id_1, new_value]
C->>S: Unsubscribe [8, 8, id_1, id_2]
S->>C: OK [10, 8]
Note over S: Item value is set again, no update sent
Golden Vectors
The golden vectors are recorded exchanges between a client and a server, each message given as bytes and in diagnostic notation. Export them with protocol-spec json protocols/stow --output vectors.json.
Stow Description
Each Describe Response chunk holds a sequence of item description maps. An item’s desc-type sets how its desc-default and its values are encoded and the shape of its desc-constraints.
desc-id is the item ID used in Get, Set, Subscribe and Update messages. IDs may change across firmware versions; use desc-name as the stable identifier.
Bit n of desc-read-perm and desc-write-perm is the role at index n of the Hello Response’s hello-role-names. A client whose session holds any role in the mask is granted access. A mask of 0 means the item is firmware-only and cannot be accessed by any external client.
Definition
This definition is normative.
; Stow protocol, version 1. Every message is [code, tag, ...].
; A node answers a misdirected non-request with nothing
message = request / response
request = hello / describe / get / set / subscribe / unsubscribe
response = hello-response / describe-response / get-response / update / ok / error
protocol-version = 1
; Chosen by the client and echoed by the response, so it encodes in one byte
tag = 0..23
item-id = uint
; Bit n is the role at index n of role-names
role-mask = uint
int32 = -2147483648..2147483647
; Encoded as the item's type: Enum and Int as int32, Float as float32,
; String as tstr, Byte Array and Buffer as bstr, Struct as its field values
value = int32 / float32 / tstr / bstr / [* value]
; A Set may carry a Float item's value in any float width
set-value = int32 / float / tstr / bstr / [* set-value]
msg-hello = 0
msg-hello-response = 1
msg-describe = 2
msg-describe-response = 3
msg-get = 4
msg-get-response = 5
msg-set = 6
msg-subscribe = 7
msg-unsubscribe = 8
msg-update = 9
msg-ok = 10
msg-error = 11
hello = [msg-hello, tag]
hello-response = [msg-hello-response, tag, version: protocol-version, hello-info]
hello-info = {
hello-max-message => uint,
hello-stow-hash => tstr,
hello-roles => role-mask,
hello-role-names => [* tstr],
* uint => any,
}
hello-max-message = 0
hello-stow-hash = 1
hello-roles = 2
hello-role-names = 3
describe = [msg-describe, tag, start: item-id, ? names: [* tstr]]
describe-response = [
msg-describe-response, tag,
next: item-id / null,
chunk: bstr .cborseq description-sequence,
]
get = [msg-get, tag, + item-id]
get-response = [msg-get-response, tag, + (item-id, value)]
set = [msg-set, tag, + (item-id, set-value)]
subscribe = [msg-subscribe, tag, + item-id]
unsubscribe = [msg-unsubscribe, tag, + item-id]
update = [msg-update, null, item-id, value]
ok = [msg-ok, tag]
; The tag is null when the request's tag could not be read, and the item
; when an index is known without one
error = [msg-error, tag / null, error-code, ? (item: item-id / null, ? index: uint)]
error-code = err-malformed-msg / err-unknown-msg / err-invalid-item /
err-out-of-memory / err-permission-denied / err-unknown /
err-invalid-value / err-already-set / err-too-large
err-malformed-msg = 1
err-unknown-msg = 2
err-invalid-item = 3
err-out-of-memory = 4
err-permission-denied = 5
err-unknown = 6
err-invalid-value = 7
err-already-set = 8
err-too-large = 9
description-sequence = [* description]
description = enum-description / int-description / float-description /
string-description / bytes-description / struct-description
item-common = (
desc-id => item-id,
desc-name => tstr,
desc-categories => [* tstr],
desc-storage => storage,
desc-read-perm => role-mask,
desc-write-perm => role-mask,
)
enum-description = {
item-common,
desc-type => type-enum,
desc-default => int32,
desc-constraints => enum-constraints,
}
int-description = {
item-common,
desc-type => type-int,
desc-default => int32,
desc-constraints => int-constraints,
}
float-description = {
item-common,
desc-type => type-float,
desc-default => float32,
desc-constraints => float-constraints,
}
string-description = {
item-common,
desc-type => type-string,
desc-default => tstr,
desc-constraints => length-constraints,
}
bytes-description = {
item-common,
desc-type => type-byte-array / type-buffer,
desc-default => bstr,
desc-constraints => length-constraints,
}
struct-description = {
item-common,
desc-type => type-struct,
desc-default => [* value],
desc-constraints => struct-constraints,
}
desc-id = 0
desc-name = 1
desc-categories = 2
desc-storage = 3
desc-read-perm = 4
desc-write-perm = 5
desc-type = 6
desc-default = 7
desc-constraints = 8
storage = storage-ephemeral / storage-persistent / storage-tofu
storage-ephemeral = 0
storage-persistent = 1
storage-tofu = 2
item-type = type-enum / type-int / type-float / type-string /
type-byte-array / type-buffer / type-struct
type-enum = 0
type-int = 1
type-float = 2
type-string = 3
type-byte-array = 4
type-buffer = 5
type-struct = 6
constraints = enum-constraints / int-constraints / float-constraints /
length-constraints / struct-constraints
enum-constraints = [* {enum-value => int32, enum-name => tstr}]
enum-value = 0
enum-name = 1
int-constraints = {constraint-min => int32, constraint-max => int32}
float-constraints = {constraint-min => float32, constraint-max => float32}
length-constraints = {constraint-min => uint, constraint-max => uint}
constraint-min = 0
constraint-max = 1
; Struct fields in order; a field's constraints follow its type, so structs nest
struct-constraints = [* {field-name => tstr, field-type => item-type, field-constraints => constraints}]
field-name = 0
field-type = 1
field-constraints = 2