Exposing the Stow to Clients
The Stow server lets another device or a PC set, get, subscribe to and describe this device’s items over any link. Each client holds a set of roles, and each item’s permissions decide what those roles may read and write. The Protocol Reference describes what the server and its clients send.
Enabling the Server
CONFIG_STOW_PROTOCOL=y
# Frames Stow messages over a byte stream such as a UART
CONFIG_STOW_COBS_TRANSPORT=y
The server starts its thread at boot. The app opens a connection for each client that connects, and each open connection is one session of the server, up to CONFIG_STOW_PROTOCOL_MAX_SESSIONS.
Serving a UART
A transport carries whole Stow messages over a link. The COBS transport frames them over a byte stream, the framing the Python Stow client’s serial transport uses. The app gives it a pool for messages, a pool for encoded frames and a function that writes a frame, then opens its connection with the roles the client holds, here the schema’s User role:
#include <gantry/stow/stow_cobs_transport.h>
#define MESSAGE_SIZE 512U
#define BUFFER_COUNT 8U
NET_BUF_POOL_DEFINE(message_pool, BUFFER_COUNT, MESSAGE_SIZE, 0, NULL);
NET_BUF_POOL_DEFINE(frame_pool, BUFFER_COUNT, COBS_ENCODE_MAX_SIZE(MESSAGE_SIZE), 0, NULL);
K_MSGQ_DEFINE(tx_queue, sizeof(struct net_buf*), BUFFER_COUNT, sizeof(void*));
static struct stow_cobs_transport uart_link;
static int queue_frame(struct net_buf* frame, void* user_data)
{
ARG_UNUSED(user_data);
return (k_msgq_put(&tx_queue, (const void*)&frame, K_NO_WAIT) == 0) ? SUCCESS : -ENOBUFS;
}
static void uart_link_init(void)
{
const struct stow_cobs_transport_config config = {
.message_pool = &message_pool,
.frame_pool = &frame_pool,
.write = queue_frame,
};
stow_cobs_transport_init(&uart_link, &config);
stow_cobs_transport_open(&uart_link, STOW_ROLE_USER);
}
The UART’s thread feeds the bytes it receives to the transport, and writes out each queued frame:
stow_cobs_transport_feed(&uart_link, data, len);
struct net_buf* frame = NULL;
while (k_msgq_get(&tx_queue, (void*)&frame, K_NO_WAIT) == 0)
{
for (size_t i = 0; i < frame->len; i++)
{
uart_poll_out(uart_dev, frame->data[i]);
}
net_buf_unref(frame);
}
A link that comes and goes, such as a cable or a radio, opens the connection when the client arrives and closes it with stow_cobs_transport_close() when the client leaves. The same connection can also carry this device’s requests to the client’s Stow, see Reading Another Device’s Stow.
Changing Roles
A connection opens with the roles the link grants, such as none for a client that has not authenticated. When the client authenticates in-band, for example by writing a password item, the app grants it new roles with stow_protocol_set_roles():
stow_protocol_set_roles(&uart_link.connection, STOW_ROLE_USER | STOW_ROLE_DEVELOPER);
The server revokes the subscriptions the new roles can no longer read.
Warning
Stow trusts its clients, so only a trusted peer should reach the Stow, for example over a paired radio or a cable only trusted devices use. Roles granted in-band refine what a trusted peer may do. A malicious message of deeply nested containers can overflow the stack and crash the device.
Writing a Transport
A transport for another link embeds a stow_transport, which names the function that sends a message and the pool outgoing messages come from, and a stow_connection for each client it carries. It opens a connection with stow_connection_open() when a client connects, passes each whole message to stow_connection_receive(), and closes the connection with stow_connection_close() when the client leaves. The COBS transport is a good starting point.
API Reference
-
enum stow_runtime_input_kind
What an input tells the side.
Values:
-
enumerator STOW_RUNTIME_OPENED
The session opened; its first input
-
enumerator STOW_RUNTIME_CLOSED
The session closed; only for a session that was OPENED
-
enumerator STOW_RUNTIME_MESSAGE
A message arrived, in stow_runtime_input::buf
-
enumerator STOW_RUNTIME_WORK
Work from stow_runtime_post, in stow_runtime_input::arg and .buf
-
enumerator STOW_RUNTIME_BROADCAST
An event from stow_runtime_broadcast, one input per open session
-
enumerator STOW_RUNTIME_OPENED
-
typedef int (*stow_cobs_write_t)(struct net_buf *frame, void *user_data)
Write one encoded frame to the link.
On success the link takes ownership of
frame. Otherwise ownership stays with the caller and Stow drops the message. Must not block.- Releases:
net_buf frame on-success
- Param frame:
COBS-encoded frame, including the trailing delimiter
- Param user_data:
Opaque pointer from stow_cobs_transport_config
- Return:
SUCCESS when the frame was accepted, otherwise a negative errno
-
int stow_cobs_transport_init(struct stow_cobs_transport *cobs, const struct stow_cobs_transport_config *config)
Initialize a raw COBS transport.
- Parameters:
cobs – Transport instance
config – Instance configuration, copied
- Returns:
SUCCESS when the transport is ready
- Returns:
-EINVAL when the configuration is incomplete
- Returns:
-ENOMEM when the first receive buffer cannot be allocated
-
int stow_cobs_transport_open(struct stow_cobs_transport *cobs, stow_role_t roles)
The link connected; open its connection.
Any partial frame left from an earlier connection is discarded by the next feed.
- Parameters:
cobs – Transport instance
roles – Roles the server grants the peer
- Returns:
the results of stow_connection_open
-
void stow_cobs_transport_close(struct stow_cobs_transport *cobs)
The link disconnected; close its connection.
Any partial frame received on the link is discarded by the next feed.
- Parameters:
cobs – Transport instance
-
int stow_cobs_transport_feed(struct stow_cobs_transport *cobs, const uint8_t *data, size_t len)
Feed bytes received from the link.
Each complete frame is handed to the connection as one message. Call from one thread at a time.
- Parameters:
cobs – Transport instance
data – Received bytes
len – Number of bytes in
data
- Returns:
SUCCESS when the bytes were consumed
- Returns:
-ENOMEM when no receive buffer is available
-
int stow_connection_open(struct stow_connection *connection, struct stow_transport *transport, stow_role_t roles)
The carrier session opened; open the connection, with a server session when the build has a server.
- Parameters:
connection – The connection
transport – Transport that carries the connection’s messages
roles – Roles the server grants the peer
- Returns:
SUCCESS when the connection is open
- Returns:
-EINVAL for a transport stow_transport_usable() rejects
- Returns:
-EALREADY when the connection is open
- Returns:
-ENOMEM when every server session is open or still referenced
-
void stow_connection_close(struct stow_connection *connection)
The carrier session closed; close the client session, if one is open, then the server session.
- Parameters:
connection – The connection
-
void stow_connection_receive(struct stow_connection *connection, struct net_buf *buf)
One complete Stow message arrived.
A request, or a message whose code is unreadable or unknown, goes to the server. Without a server it is answered with an Error. Any other message goes to the client session, or is dropped when none is open. A message on a closed connection is dropped.
- Parameters:
connection – The connection
buf – The message; the caller keeps its reference
-
int stow_msg_peek(const uint8_t *data, size_t len, uint32_t *code_out, uint8_t *tag_out)
Read a message’s code and tag without checking the rest.
- Parameters:
data – The message
len – Length of
datacode_out – [out] Message code, or STOW_MSG_ABSENT when it could not be read
tag_out – [out] Tag, or STOW_TAG_NONE when it is null or could not be read
- Returns:
SUCCESS, or -EBADMSG when the code or tag could not be read
-
int stow_msg_read(struct stow_msg_reader *reader, const uint8_t *data, size_t len, const struct stow_msg_items *items, struct stow_msg *msg)
Check a whole message, then make its list ready to walk.
Every value is decoded through
itemsand released. Unknown items pass; walking reports them. On failure,msgholds the tag if it could be read and stow_msg::error the errno and the item that broke the message.- Parameters:
reader – Reader, which walks the message’s list next
data – The message, which must outlive the walk
len – Length of
dataitems – Resolver of the message’s item IDs
msg – [out] The message’s fixed fields and count
- Returns:
SUCCESS when the message is well formed
- Returns:
-EBADMSG when it is malformed
- Returns:
-EPROTO when its code is unknown
- Returns:
-EPROTONOSUPPORT when it is a Hello Response of another protocol version
- Returns:
-EINVAL when a value breaks its item’s constraints
- Returns:
-ENOMEM when no memory is free to check a value
-
int stow_msg_next_entry(struct stow_msg_reader *reader, struct stow_msg_entry *entry)
Read the next entry; call it exactly stow_msg::count times.
- Acquires:
memory entry on-success
- Parameters:
reader – Reader of a message with entries
entry – [out] The entry; its stow_msg_entry::id is set even on failure
- Returns:
SUCCESS
- Returns:
-ENOENT when this side has no such item
- Returns:
-ENOMEM when no memory is free for the value
-
static inline void stow_msg_entry_release(struct stow_msg_entry *entry)
Release an entry from stow_msg_next_entry; an entry without a value releases nothing.
- Releases:
memory entry
- Parameters:
entry – The entry
-
int stow_msg_next_name(struct stow_msg_reader *reader, struct zcbor_string *name)
Read the next name of a Describe; call it exactly stow_msg::count times.
- Parameters:
reader – Reader of a Describe
name – [out] The name, pointing into the message
- Returns:
SUCCESS
-
int stow_msg_next_description(struct stow_msg_reader *reader, struct stow_msg_description *description)
Read the next description of a Describe Response; call it exactly stow_msg::count times.
- Parameters:
reader – Reader of a Describe Response
description – [out] The description, pointing into the message
- Returns:
SUCCESS
-
void stow_msg_writer_init(struct stow_msg_writer *writer, uint8_t *out, size_t budget)
Start a writer on a byte range.
- Parameters:
writer – The writer
out – Where messages are written
budget – Bytes a message may use
-
int stow_msg_write(struct stow_msg_writer *writer, const struct stow_msg *msg)
Start a message, discarding anything written before.
Writes the fixed fields. A Get, Set, Subscribe, Unsubscribe, Get Response or Update then takes exactly stow_msg::count calls to stow_msg_put_entry. A Describe that names items takes stow_msg_put_name and a Describe Response stow_msg_put_description until one is rejected.
- Parameters:
writer – The writer
msg – The message
- Returns:
SUCCESS, or -E2BIG when the fixed fields do not fit
-
int stow_msg_put_entry(struct stow_msg_writer *writer, const struct stow_msg_entry *entry)
Put the next entry; after a failure, later puts do nothing.
- Parameters:
writer – The writer
entry – The entry, with its codec for a value
- Returns:
SUCCESS, -E2BIG when it does not fit, or the codec’s error
-
int stow_msg_put_name(struct stow_msg_writer *writer, const char *name)
Put the next name of a Describe, which keeps room to continue from any item.
- Parameters:
writer – The writer
name – The name
- Returns:
SUCCESS, or -E2BIG when it or an earlier name was rejected; the message stays valid
-
int stow_msg_put_description(struct stow_msg_writer *writer, const struct stow_item_const_metadata *item)
Put the next description of a Describe Response; the first one rejected becomes the next item.
- Parameters:
writer – The writer
item – The item to describe
- Returns:
SUCCESS, or -E2BIG when it or an earlier description was rejected; the message stays valid
-
int stow_msg_finish(struct stow_msg_writer *writer, size_t *len_out)
Finish the message.
- Parameters:
writer – The writer
len_out – [out] Length of the message, at the start of the byte range
- Returns:
SUCCESS
- Returns:
the first error of the message
- Returns:
-E2BIG when a Describe or Describe Response rejected its first element
-
bool stow_msg_is_request(uint32_t code)
Check whether a message code is a request, which only a server answers.
- Parameters:
code – Message code
- Returns:
true for a request
-
enum stow_message_code stow_msg_answer(enum stow_message_code request)
Get the code of a request’s success response.
- Parameters:
request – A request’s code
- Returns:
The success response’s code
-
int stow_protocol_set_roles(struct stow_connection *connection, stow_role_t roles)
Update the roles of a connection’s server session.
Updates the session’s roles. Active subscriptions that no longer satisfy the new roles are automatically revoked.
Note
A session with no roles has no access to any item.
- Parameters:
connection – The connection whose session to update
roles – The new role bitmask
- Returns:
SUCCESS when the change is queued, or the connection has no server session
- Returns:
-ENOBUFS when the protocol queue is full
-
int stow_runtime_open(struct stow_runtime *runtime, struct stow_connection *connection, const void *context)
Open a connection’s session on an instance.
- Parameters:
runtime – The instance
connection – The connection
context – What the session is opened with, for the side to read from stow_session::context
- Returns:
SUCCESS when the session is open
- Returns:
-EALREADY when the connection has a session open on the instance
- Returns:
-ENOMEM when every session of the instance is open or still referenced
-
void stow_runtime_close(struct stow_runtime *runtime, struct stow_connection *connection)
Close a connection’s session on an instance, if one is open.
- Parameters:
runtime – The instance
connection – The connection
-
void stow_runtime_receive(struct stow_runtime *runtime, struct stow_connection *connection, struct net_buf *buf)
Queue a message for a connection’s session, or drop it when none is open.
- Parameters:
runtime – The instance
connection – The connection
buf – The message; the caller keeps its reference
-
int stow_runtime_next(struct stow_runtime *runtime, k_timeout_t timeout, struct stow_runtime_input *input)
Take the next input, waiting for one until
timeout.One thread per instance calls it. Signals come before queued entries, every close before any open, and a session’s OPENED before anything else for it. An entry for a session that closed since it was queued is released here and never handed out.
- Acquires:
stow_runtime_input input on-success
- Parameters:
runtime – The instance
timeout – Longest to wait
input – [out] The input, for stow_runtime_done
- Returns:
SUCCESS when
inputholds an input- Returns:
-EAGAIN when none came before
timeout
-
void stow_runtime_done(struct stow_runtime_input *input)
Hand back an input, releasing what it borrowed.
After CLOSED this also drops the open session’s reference.
- Releases:
stow_runtime_input input
- Parameters:
input – The input
-
int stow_runtime_post(struct stow_runtime *runtime, struct stow_connection *connection, uint32_t arg, struct net_buf *buf)
Queue work for a connection’s open session, handed out as WORK.
Work for a session that closes first is released without being handed out.
- Releases:
net_buf buf
- Parameters:
runtime – The instance
connection – Connection whose session the work is for
arg – Handed out in stow_runtime_input::arg
buf – Handed out in stow_runtime_input::buf, or NULL; ownership is always taken
- Returns:
SUCCESS when the work is queued
- Returns:
-ENOTCONN when
connectionhas no session open on the instance- Returns:
-ENOBUFS when the queue is full
-
int stow_runtime_broadcast(struct stow_runtime *runtime, event_t *event)
Queue an event, handed out as one BROADCAST for each open session.
- Releases:
memory event
- Parameters:
runtime – The instance
event – The event; ownership is always taken
- Returns:
SUCCESS when the event is queued
- Returns:
-ENOBUFS when the queue is full
-
void stow_session_ref(struct stow_session *session)
Take a reference to a session, keeping its slot from reuse after it closes.
Take it while the session is borrowed or already referenced.
- Acquires:
stow_session session
- Parameters:
session – The session
-
void stow_session_unref(struct stow_session *session)
Release a reference to a session.
- Releases:
stow_session session
- Parameters:
session – The reference to release, NULL does nothing
-
static inline bool stow_transport_usable(const struct stow_transport *transport)
Check that a transport can carry Stow messages: it sends, and its fixed-size buffers outlast its headroom and tailroom.
- Parameters:
transport – The transport, or NULL
- Returns:
true when connections may be opened on it
-
int stow_transport_send(struct stow_connection *connection, struct net_buf *buf)
Send one complete Stow message through an open connection’s transport, dropping it when the transport refuses it.
- Releases:
net_buf buf
- Parameters:
connection – Connection the message belongs to
buf – The message, ownership is always taken
- Returns:
SUCCESS when the transport took the message, otherwise the transport’s error
-
int stow_outgoing_begin(struct stow_outgoing *out, struct stow_connection *connection, uint32_t limit)
Begin an outgoing message on an open connection.
The writer’s budget is the buffer’s room after the transport’s headroom and tailroom, capped at
limit.- Acquires:
stow_outgoing out on-success
- Parameters:
out – [out] The message
connection – The connection the message is for
limit – Largest message the receiver accepts, or 0 for no limit beyond the buffer
- Returns:
SUCCESS when the message is begun
- Returns:
-EBUSY when the transport has no free buffer
-
int stow_outgoing_send(struct stow_outgoing *out)
Finish an outgoing message and hand it to its connection’s transport.
A response that does not fit is sent as an Error carrying the response’s tag and the error. Any other message that does not fit is dropped, as is one the transport refuses.
- Releases:
stow_outgoing out
- Parameters:
out – The message, always consumed
- Returns:
SUCCESS when the transport took the message, otherwise the codec’s or the transport’s error
-
int stow_outgoing_finish(struct stow_outgoing *out, struct net_buf **buf_out)
Finish an outgoing message into a buffer the caller sends later with stow_transport_send.
A response that does not fit becomes an Error, as in stow_outgoing_send.
- Releases:
stow_outgoing out
- Acquires:
net_buf buf_out on-success
- Parameters:
out – The message, always consumed
buf_out – [out] The finished message
- Returns:
SUCCESS, or the codec’s error when the message does not fit
-
void stow_outgoing_drop(struct stow_outgoing *out)
Drop an outgoing message that will not be sent.
- Releases:
stow_outgoing out
- Parameters:
out – The message
-
STOW_TAG_NONE
Marks a tag that is null or could not be read.
-
STOW_MSG_ABSENT
Marks an item, index or next item that is absent from a message, or a code that could not be read.
-
STOW_MSG_READER_STATES
zcbor states a reader holds
A backup for the message’s list, then one for a Hello map or a names list, or one per level of a struct value, plus the two states zcbor needs. The build sets STOW_DEPTH_MAX to the deepest struct nesting it decodes.
-
STOW_MSG_WRITER_STATES
zcbor states a writer holds; definite-length headers need no backups
-
STOW_RUNTIME_DEFINE(_name, _sessions, _member, _queue_size)
Define an instance.
- Parameters:
_name – Name of the instance
_sessions – The side’s array of per-session structs
_member – Name of the stow_session in each struct
_queue_size – Number of entries the queue holds
-
struct stow_cobs_transport_config
- #include <stow_cobs_transport.h>
Configuration for stow_cobs_transport_init.
-
struct stow_cobs_transport
- #include <stow_cobs_transport.h>
Raw COBS transport instance.
-
struct stow_connection
- #include <stow_connection.h>
One carrier session carrying Stow in both directions.
Embedded in the transport’s state for the carrier session, which starts zeroed.
-
struct stow_msg_entry
- #include <stow_msg.h>
One item of a Get, Set, Subscribe, Unsubscribe, Get Response or Update.
-
struct stow_msg_items
- #include <stow_msg.h>
What a wire item ID means on this side.
-
struct stow_msg_hello
- #include <stow_msg.h>
Body of a Hello Response.
-
struct stow_msg_describe
- #include <stow_msg.h>
Body of a Describe.
-
struct stow_msg_describe_response
- #include <stow_msg.h>
Body of a Describe Response.
-
struct stow_msg_error
- #include <stow_msg.h>
Body of an Error, or why a message could not be read.
-
struct stow_msg_description
- #include <stow_msg.h>
The parts of an item description a client checks.
-
struct stow_msg
- #include <stow_msg.h>
Any Stow message.
-
struct stow_msg_reader
- #include <stow_msg.h>
Reads one message and then walks its list; its fields are private.
-
struct stow_msg_writer
- #include <stow_msg.h>
Writes one message at a time into a byte range; its fields are private.
-
struct stow_session
- #include <stow_runtime.h>
A session, embedded in the side’s per-session struct.
The side reads the first two fields; the rest belong to the runtime.
-
struct stow_runtime_input
- #include <stow_runtime.h>
One thing for the side to handle, from stow_runtime_next.
-
struct stow_runtime
- #include <stow_runtime.h>
An instance of the runtime, defined with STOW_RUNTIME_DEFINE.
-
struct stow_transport
- #include <stow_transport.h>
A transport that carries Stow messages for one or more connections.
Transports embed this structure and recover their own state with
CONTAINER_OF. A transport must deliver any message up to the size of its pool’s buffers.
-
struct stow_outgoing
- #include <stow_transport.h>
A Stow message on its way to a transport: a buffer from the transport’s pool and the codec writer filling it.
Begin it with stow_outgoing_begin, write it through stow_outgoing::writer, then hand it on with stow_outgoing_send, stow_outgoing_finish or stow_outgoing_drop.