Reading Another Device’s Stow
The Stow client lets firmware get, set and subscribe to the items of a peer device’s Stow, over a connection to that peer’s Stow server. Values are the same data_value_t as local Stow items.
The client is enabled with CONFIG_STOW_CLIENT, independently of the local Stow (CONFIG_GANTRY_STOW) and the server (CONFIG_STOW_PROTOCOL). A client-only build, with CONFIG_GANTRY_STOW=n, needs no stow.yaml of its own.
Building Against a Peer
A client is built against the peer’s schema, or only the items it uses. The app’s CMakeLists.txt names the peer’s schema and a prefix:
gantry_stow_client_generate(
PREFIX device
YAML ${DEVICE_DIR}/stow.yaml
FRAGMENTS ${DEVICE_DIR}/ble.stow.yaml
ITEMS BatteryLevel LedMode
)
The peer’s schema is composed and validated as in the peer’s own build. The app includes generated_device_stow_client.h, which holds an ID per item (DEVICE_ID_BATTERY_LEVEL), the schema device_stow_client_schema, and the peer’s enums and structs, all under the prefix. A peer enum or struct identical to one in the local Stow is the local type, so device_Point_t is Point_t. A client of more than CONFIG_STOW_CLIENT_MAX_ITEMS items fails to build.
Connecting
A client is defined once, with the schema its peers are checked against. The app opens a client session on an open connection to the peer, and each session is a peer. Here the connection is the COBS transport’s, opened with no roles, so the peer can use none of this device’s items (see Exposing the Stow to Clients):
STOW_CLIENT_DEFINE(device_client, &device_stow_client_schema, 0);
stow_cobs_transport_open(&device_link, 0);
stow_client_open(&device_client, &device_link.connection);
When the session opens, the client checks the peer’s items by name, type and constraints. stow_client_availability() says whether an item can be used, and the peer’s schema should follow the compatibility rule.
The client reports each peer to the hooks registered with STOW_CLIENT_CB_DEFINE. A hook records the peer state its module needs, and anything it queues about a peer is only a wake-up, since a full queue drops it. Here the connected hook keeps a reference to the peer and wakes the module, and disconnected drops it:
K_MUTEX_DEFINE(device_lock);
K_SEM_DEFINE(device_wake, 0, 1);
static struct stow_client_peer* device;
static atomic_t device_changed;
static void on_connected(struct stow_client_peer* peer, int err)
{
if (err != SUCCESS)
{
stow_client_close(stow_client_peer_connection(peer));
return;
}
k_mutex_lock(&device_lock, K_FOREVER);
stow_client_peer_unref(device);
stow_client_peer_ref(peer);
device = peer;
PASS_OWNERSHIP(peer);
k_mutex_unlock(&device_lock);
atomic_set(&device_changed, 1);
k_sem_give(&device_wake);
}
static void on_disconnected(struct stow_client_peer* peer)
{
k_mutex_lock(&device_lock, K_FOREVER);
if (device == peer)
{
stow_client_peer_unref(device);
device = NULL;
}
k_mutex_unlock(&device_lock);
}
STOW_CLIENT_CB_DEFINE(device_hooks) = {
.connected = on_connected,
.disconnected = on_disconnected,
};
When it wakes and finds device_changed set, the module’s thread takes its own reference to device under the lock, makes its requests on that reference, and then releases it with stow_client_peer_unref(). The examples below call that reference peer.
Making Requests
Get, Set, Subscribe and Unsubscribe take one or more items, and each has a _one form for a single item. The blocking forms return when the peer answers:
struct stow_client_value level;
if (stow_client_get_one(peer, DEVICE_ID_BATTERY_LEVEL, &level) == SUCCESS)
{
LOG_INF("Battery %d%%", level.value.data.int_value);
STOW_CLIENT_RELEASE(&device_client, &level);
}
const data_value_t on = { .type = STOW_ITEM_TYPE_ENUM, .data.int_value = device_LedMode_ON };
stow_client_set_one(peer, DEVICE_ID_LED_MODE, on);
Each call also has an _async form, which returns at once and calls its callback on the client thread with a result event:
static void on_level(event_t* event, void* user_data)
{
const struct stow_client_result_payload* result = (const void*)event->data.buf;
if (result->err == SUCCESS)
{
LOG_INF("Battery %d%%", result->values[0].value.data.int_value);
}
}
stow_client_get_one_async(peer, DEVICE_ID_BATTERY_LEVEL, on_level, NULL);
Updates
After stow_client_subscribe(), each Update for the item reaches every update hook as a stow_client_update_event. Here the hook posts it to its module’s queue:
static void on_update(event_t* event)
{
EVENT_REF(event);
if (MSGQ_PUT_OWNED(&device_queue, &event, event) != 0)
{
EVENT_UNREF(event);
}
}
STOW_CLIENT_CB_DEFINE(device_updates) = {
.update = on_update,
};
Update and result events come from the memory pools, so size the pools for the events in flight. Updates are best effort, as the protocol reference describes, so a module may lose one to a full queue.
API Reference
-
enum stow_client_item_availability
Whether a connected peer has an expected item.
Values:
-
enumerator STOW_CLIENT_MISSING
The peer has no item with the expected name
-
enumerator STOW_CLIENT_INCOMPATIBLE
The peer’s item differs in type or constraints
-
enumerator STOW_CLIENT_AVAILABLE
The peer’s item matches
-
enumerator STOW_CLIENT_MISSING
-
typedef void (*stow_client_cb_t)(event_t *event, void *user_data)
Called on the client thread with the stow_client_result_event of an asynchronous request.
Keep the event with EVENT_REF; the client releases its own reference when this returns.
-
const event_type_t stow_client_update_event
Update event, with a stow_client_update_payload.
-
const event_type_t stow_client_result_event
Result event, with a stow_client_result_payload.
-
int stow_client_open(const struct stow_client *client, struct stow_connection *connection)
Open a client session on a connection, to the peer’s server.
- Parameters:
client – Client the peer is checked against
connection – An open connection
- Returns:
SUCCESS when the session is open; its peer is reported through the stow_client_cb::connected hooks
- Returns:
-ENOTCONN when the connection is not open
- Returns:
-EALREADY when the connection has a client session
- Returns:
-ENOMEM when every one of the CONFIG_STOW_CLIENT_MAX_PEERS peers is open or still referenced
-
void stow_client_close(struct stow_connection *connection)
Close a connection’s client session, if one is open.
- Parameters:
connection – The connection
-
void stow_client_peer_ref(struct stow_client_peer *peer)
Take a reference to a peer, keeping it valid after a hook or callback returns.
Take it while the peer is borrowed or already referenced.
- Acquires:
stow_client_peer peer
- Parameters:
peer – The peer
-
void stow_client_peer_unref(struct stow_client_peer *peer)
Release a reference to a peer.
- Releases:
stow_client_peer peer
- Parameters:
peer – The reference to release, NULL does nothing
-
const struct stow_client *stow_client_peer_client(struct stow_client_peer *peer)
Get the client a peer’s session belongs to.
- Parameters:
peer – The peer
- Returns:
The client
-
struct stow_connection *stow_client_peer_connection(struct stow_client_peer *peer)
Get the connection a peer’s session is on.
- Parameters:
peer – The peer
- Returns:
The connection
-
int stow_client_peer_info(struct stow_client_peer *peer, struct stow_client_peer_hello *info_out)
Read what a connected peer reported in its Hello Response.
- Parameters:
peer – The peer
info_out – [out] Populated with the peer’s limit and the session’s roles
- Returns:
SUCCESS when the peer is connected
- Returns:
-ENOTCONN when it is not
-
enum stow_client_item_availability stow_client_availability(struct stow_client_peer *peer, uint16_t id)
Check whether a connected peer has an expected item.
- Parameters:
peer – The peer
id – Generated item ID
- Returns:
The item’s availability, STOW_CLIENT_MISSING while the peer is not connected
-
int stow_client_get(struct stow_client_peer *peer, const uint16_t *ids, size_t count, struct stow_client_value *values_out)
Get the values of one or more items, blocking until the response.
Must not be called from an interrupt or the client thread. Release each value with stow_client_release.
- Acquires:
memory values_out on-success
- Parameters:
peer – The peer
ids – Generated item IDs
count – Number of items
values_out – [out] Each item and its value, in the order requested
- Returns:
SUCCESS when every value was read
- Returns:
-EINVAL when no or more than UINT16_MAX items are given, or an ID is unknown
- Returns:
-ENOTCONN when the peer is not connected, or its session closed before the response
- Returns:
-ENOTSUP when an item is missing or incompatible on the peer
- Returns:
-EMSGSIZE when the request exceeds the smaller of the peer’s max_message and the client’s buffers
- Returns:
-EBUSY when no request buffer was free, or the transport or the client’s queue refused the request
- Returns:
-ETIMEDOUT when no response arrived in time
- Returns:
-ENOMSG when the response was malformed or did not match the request
- Returns:
-ENOBUFS when the client had no memory for the request or response
- Returns:
-EWOULDBLOCK when called from an interrupt or the client thread
- Returns:
-EBADMSG, -EPROTO, -ENOENT, -ENOMEM, -EACCES, -E2BIG or -EIO when the peer answered with an Error
-
int stow_client_set(struct stow_client_peer *peer, const uint16_t *ids, const data_value_t *values, size_t count)
Set the values of one or more items, blocking until the response.
Items are set in order and the first failure stops the rest. Which entry failed is not reported; use stow_client_set_async when it matters.
- Parameters:
peer – The peer
ids – Generated item IDs
values – Value of each item
count – Number of items
- Returns:
SUCCESS when every value was set
- Returns:
-EINVAL when a value breaks the item’s constraints, locally or on the peer
- Returns:
-EALREADY when a trust-on-first-use item is already set
- Returns:
a codec’s error when a value cannot be encoded
- Returns:
the other errors of stow_client_get
-
int stow_client_subscribe(struct stow_client_peer *peer, const uint16_t *ids, size_t count)
Subscribe to one or more items, blocking until the response.
Updates arrive through the stow_client_cb::update hooks. They are best effort, so Get an item after subscribing and after reconnecting, and poll items where a stale value matters. Subscriptions end with the session.
- Parameters:
peer – The peer
ids – Generated item IDs
count – Number of items
- Returns:
the results of stow_client_get
-
int stow_client_unsubscribe(struct stow_client_peer *peer, const uint16_t *ids, size_t count)
Unsubscribe from one or more items, blocking until the response.
- Parameters:
peer – The peer
ids – Generated item IDs
count – Number of items
- Returns:
the results of stow_client_get
-
int stow_client_get_async(struct stow_client_peer *peer, const uint16_t *ids, size_t count, stow_client_cb_t cb, void *user_data)
Get the values of one or more items without blocking.
- Parameters:
peer – The peer
ids – Generated item IDs
count – Number of items
cb – Called with the result event, not NULL
user_data – Passed to
cb
- Returns:
SUCCESS when
cbwill be called- Returns:
-EAGAIN when no request slot or tag is free
- Returns:
-ENOBUFS when no event is free for the result
- Returns:
the errors of stow_client_get found before sending
-
int stow_client_set_async(struct stow_client_peer *peer, const uint16_t *ids, const data_value_t *values, size_t count, stow_client_cb_t cb, void *user_data)
Set the values of one or more items without blocking.
- Parameters:
peer – The peer
ids – Generated item IDs
values – Value of each item, encoded before this returns
count – Number of items
cb – Called with the result event, not NULL
user_data – Passed to
cb
- Returns:
the results of stow_client_get_async
-
int stow_client_subscribe_async(struct stow_client_peer *peer, const uint16_t *ids, size_t count, stow_client_cb_t cb, void *user_data)
Subscribe to one or more items without blocking.
- Parameters:
peer – The peer
ids – Generated item IDs
count – Number of items
cb – Called with the result event, not NULL
user_data – Passed to
cb
- Returns:
the results of stow_client_get_async
-
int stow_client_unsubscribe_async(struct stow_client_peer *peer, const uint16_t *ids, size_t count, stow_client_cb_t cb, void *user_data)
Unsubscribe from one or more items without blocking.
- Parameters:
peer – The peer
ids – Generated item IDs
count – Number of items
cb – Called with the result event, not NULL
user_data – Passed to
cb
- Returns:
the results of stow_client_get_async
-
void stow_client_release(const struct stow_client *client, struct stow_client_value *value)
Release a value returned by stow_client_get or kept with stow_client_ref.
- Releases:
memory value
- Parameters:
client – Client of the peer the value came from
value – [inout] The item and value to release; its item must be in
client'sschema
-
void stow_client_ref(const struct stow_client *client, struct stow_client_value *value)
Add a reference to a value; release it with stow_client_release.
- Acquires:
memory value
- Parameters:
client – Client of the peer the value came from
value – [in] The item and value to keep; its item must be in
client'sschema
-
static inline int stow_client_get_one(struct stow_client_peer *peer, uint16_t id, struct stow_client_value *value_out)
Get the value of one item, see stow_client_get.
- Acquires:
memory value_out on-success
-
static inline int stow_client_set_one(struct stow_client_peer *peer, uint16_t id, data_value_t value)
Set the value of one item, see stow_client_set.
-
static inline int stow_client_subscribe_one(struct stow_client_peer *peer, uint16_t id)
Subscribe to one item, see stow_client_subscribe.
-
static inline int stow_client_unsubscribe_one(struct stow_client_peer *peer, uint16_t id)
Unsubscribe from one item, see stow_client_unsubscribe.
-
static inline int stow_client_get_one_async(struct stow_client_peer *peer, uint16_t id, stow_client_cb_t cb, void *user_data)
Get the value of one item without blocking, see stow_client_get_async.
-
static inline int stow_client_set_one_async(struct stow_client_peer *peer, uint16_t id, data_value_t value, stow_client_cb_t cb, void *user_data)
Set the value of one item without blocking, see stow_client_set_async.
-
static inline int stow_client_subscribe_one_async(struct stow_client_peer *peer, uint16_t id, stow_client_cb_t cb, void *user_data)
Subscribe to one item without blocking, see stow_client_subscribe_async.
-
static inline int stow_client_unsubscribe_one_async(struct stow_client_peer *peer, uint16_t id, stow_client_cb_t cb, void *user_data)
Unsubscribe from one item without blocking, see stow_client_unsubscribe_async.
-
STOW_CLIENT_NO_INDEX
Marks a result that names no failing entry.
-
EVENT_ID_STOW_CLIENT_UPDATE
Event ID for Updates from a peer.
-
EVENT_ID_STOW_CLIENT_RESULT
Event ID for the results of asynchronous requests.
-
STOW_CLIENT_CB_DEFINE(_name)
Statically register client hooks.
- Parameters:
_name – Name of the hook instance
-
STOW_CLIENT_DEFINE(_name, _schema, _timeout_ms)
Define a client for peers with the given schema.
- Parameters:
_name – Name of the Stow Client
_schema – The generated schema, for example
&device_stow_client_schema_timeout_ms – Request timeout, or 0 for CONFIG_STOW_CLIENT_TIMEOUT_MS
-
STOW_CLIENT_RELEASE(client, value)
Convenience macro for stow_client_release with debug tracing.
- Releases:
memory value
-
STOW_CLIENT_REF(client, value)
Convenience macro for stow_client_ref with debug tracing.
- Acquires:
memory value
-
struct stow_client_item
- #include <stow_client.h>
An item the client expects a peer to have.
-
struct stow_client_schema
- #include <stow_client.h>
The items a client expects a peer to have, generated from the peer’s schema.
-
struct stow_client
- #include <stow_client.h>
A client, defined with STOW_CLIENT_DEFINE.
Each session opened with it by stow_client_open is a peer.
-
struct stow_client_value
- #include <stow_client.h>
An item and its value.
-
struct stow_client_update_payload
- #include <stow_client.h>
Payload of a stow_client_update_event.
The event owns the value and a reference to the peer.
-
struct stow_client_result_payload
- #include <stow_client.h>
Payload of a stow_client_result_event, the outcome of an asynchronous request.
The event owns the values and a reference to the peer.
-
struct stow_client_peer_hello
- #include <stow_client.h>
What the peer reported in its Hello Response.
-
struct stow_client_cb
- #include <stow_client.h>
Hooks called on the client thread, registered with STOW_CLIENT_CB_DEFINE.
Any hook may be NULL. The peer is borrowed for the duration of a hook; take a reference to keep it. Record what the module needs in the hook itself, such as the peer reference or a flag, and treat anything queued from a hook as a wake-up, since a full queue drops it.