Using the Stow in Firmware

Overview

The Stow API allows firmware modules to set, get, and subscribe to items in the Stow. The API is generic meaning the same interface can be used regardless of an item’s data type. Tagged unions are used to ensure type safety alongside runtime type checking. Some data types exist on the stack while others live in reference counted memory blocks. The generic interface abstracts this complexity from the caller.

All Stow operations are validated against the given item’s access controls and value constraints. It can be assumed that values in the Stow are always valid for a given item.

The Stow API is non-blocking, thread-safe, and can be called from interrupt contexts. The permanent storage of persistent items is deferred. A flush occurs CONFIG_STOW_STORAGE_FLUSH_DELAY_MS after a write. Call stow_flush() from a thread context to persist everything pending immediately.

Initialization

The Stow loads persistent values from storage at boot, before application modules run.

Storage

Changes to persistent values are stored using Zephyr’s Settings module. The Zephyr Settings module supports various storage backends to choose from, custom backends can also be defined.

Validation

When saved values are loaded from storage, they are validated against the item’s current constraints. If the saved value is no longer valid, the item is reverted to its default and the saved value is discarded. For structs, all fields must be valid for the saved value to be valid. Notably, if the structure of a struct changes (e.g. fields are added or removed) old saved values are invalidated.

The Stow has no migration mechanism, but the automatic validation should cover most use cases. Prefer atomic data items that never need migrating.

Get Items

Use STOW_GET to read an item’s current value. Always call STOW_RELEASE when finished with the value retrieved by STOW_GET. This allows types that are reference-counted to be released, it is a noop for other types. The Resource Checker will verify that values are released. The same applies to STOW_GET_EXTERNAL and STOW_DECODE. If the call fails nothing was retrieved and there is nothing to release. To keep a value beyond its release, take another reference with STOW_REF and release it with STOW_RELEASE.

data_value_t val = {0};
STOW_GET(STOW_ID_DEVICE_NAME, &val);
LOG_INF("Device name: %s", val.data.string_value);
STOW_RELEASE(STOW_ID_DEVICE_NAME, &val);

Important

The caller MUST NOT modify the value returned by STOW_GET directly. If modifications are needed, make a copy of the value first.

        %%{init: {"theme": "neutral"}}%%
sequenceDiagram
    participant Caller
    participant Stow

    Caller->>Stow: STOW_GET(item_id, &value)
    Stow-->>Caller: Populate value with a reference or copy
    Note over Caller: Use value.data
    Caller->>Stow: STOW_RELEASE(item_id, &value)
    Stow->>Stow: Free the reference or copy
    

Set Items

Use STOW_SET with a data_value_t that includes the correct type tag.

data_value_t val = {
    .type = STOW_ITEM_TYPE_INT,
    .data.int_value = 42,
};
STOW_SET(STOW_ID_TEST_INT, val);
        %%{init: {"theme": "neutral"}}%%
sequenceDiagram
    participant Caller
    participant Stow

    Caller->>Stow: STOW_SET(item_id, value)
    Stow->>Stow: Validate against item constraints
    Stow->>Stow: Set the new value
    Stow->>Stow: Notify subscribers
    Stow->>Caller: SUCCESS or error
    

It may be cumbersome to allocate a buffer for small buffer types. In this case, a STACK_BUFFER can be used to create a stack-allocated buffer_t:

STACK_BUFFER(bytes, 6);
bytes->buf[0] = 0xAB;
data_value_t val = {
    .type = STOW_ITEM_TYPE_BUFFER,
    .data.buffer_value = bytes,
};
STOW_SET(STOW_ID_TEST_BUFFER, val);

Subscriptions

The Stow API allows modules to subscribe to items. The subscription callback will fire whenever the associated item is changed. Subscriptions can be by handle or copy:

STOW_SUBSCRIPTION_HANDLE

Handle subscriptions simply notify a subscriber that an item changed, not what value it changed to. The callback receives only the item ID. Subscribers must read the current value via STOW_GET. Notably, an item’s value may change by the time a subscriber reads the current value. Use this when you only care about the latest value.

STOW_SUBSCRIPTION_COPY

If guaranteed delivery is needed, copy subscriptions can be used. Copy subscriptions notify subscribers whenever an item’s value changes, including the value that the item was changed to. The callback receives a copy of the value at the moment the update occurred. Use this for guaranteed delivery of every value.

void on_update(event_t* event)
{
    const struct stow_update_event_payload* payload = stow_update_payload(event);
    LOG_INF("New value: %d", payload->value_copy.data.int_value);
}

static struct stow_subscription sub = {
    .mode = STOW_SUBSCRIPTION_COPY,
    .cb   = on_update,
};

stow_subscribe(STOW_ID_TEST_INT, &sub);

// later:
stow_unsubscribe(STOW_ID_TEST_INT, &sub);

If a subscription will be alive for the full lifetime of the app, a static subscription can be used via STOW_SUBSCRIPTION_DEFINE. These subscriptions are stored in ROM rather than dynamically allocated.

Important

Subscription callbacks run with interrupts masked, and possibly from an interrupt context. They must complete quickly and may not block. STOW_CALLBACK_DEFINE is recommended for callbacks.

The debug options CONFIG_STOW_CALLBACK_TIME_ASSERT and CONFIG_STOW_CALLBACK_TIME_BUDGET_US can be used to ensure callbacks don’t exceed the defined time budget.

Encode & Decode

Items can be serialized to and from CBOR. Items are encoded when stored in non-volatile storage as well as when transmitted over the Stow Protocol. The value from STOW_DECODE must always be released with STOW_RELEASE.

uint8_t buf[256];
ZCBOR_STATE_E(encoder, 1, buf, sizeof(buf), 1);

data_value_t val = {0};
STOW_GET(STOW_ID_TEST_INT, &val);
stow_encode(encoder, STOW_ID_TEST_INT, val);
STOW_RELEASE(STOW_ID_TEST_INT, &val);

ZCBOR_STATE_D(decoder, 1, buf, sizeof(buf), 1, 0);
data_value_t decoded = {0};
STOW_DECODE(decoder, STOW_ID_TEST_INT, &decoded);
STOW_RELEASE(STOW_ID_TEST_INT, &decoded);

Custom Interface

Items may optionally override the default get, set, and validation. These function overrides are declared in the schema by name and resolved at link time.

Important

custom_get, custom_set, and custom_validate run with interrupts masked. They must be quick and may never block.

custom_validate

Called after the default constraint check as an additional gate. Returns true to accept the value or false to reject it. Use sparingly; clients reading the Stow description will have no way to know about application-specific validation rules.

custom_get

Replaces the default interface get, after the permission check when called via stow_get_external(). This could be used to read a value on demand rather than reading from stored state.

custom_set

Replaces the default interface set, after the permission check (when called via stow_set_external()), constraint validation, and any custom_validate. Use it to intercept writes and apply modifications before saving. custom_set can call the default set implementation after its custom logic to ensure the new value is set in the Stow.

Example:

static int latest_sensor_reading;

int my_item_get(const struct stow_item_const_metadata *item, data_value_t *out_value)
{
    out_value->type = STOW_ITEM_TYPE_INT;
    out_value->data.int_value = latest_sensor_reading;
    return SUCCESS;
}

int my_item_set(const struct stow_item_const_metadata* item, data_value_t value)
{
   // Perform a modification (e.g. divide by 2)
   // Use care to ensure the value is always valid given the item's constraints
   value.data.int_value = value.data.int_value / 2;

   // Call the default set implementation to store the value
   return item->interface->set(item->value_ptr, value);
}

bool my_item_validate(const struct stow_item_const_metadata *item, data_value_t value)
{
   // Only allow even values
    return value.data.int_value % 2 == 0;
}

Authentication

Each Stow item has access controls for reading and writing values. The internal API (stow_set(), stow_get(), stow_subscribe()) takes no role parameter and performs no permission check. Internal firmware modules always have full access.

Callers that must be checked against an item’s permissions use the external variants instead: stow_set_external(), stow_get_external(), and stow_subscribe_external(). The Stow server is the primary example, with the roles each client’s connection holds (see Exposing the Stow to Clients). Each function takes a bitfield of the caller’s current roles, checks it against the item’s permission mask, and delegates to the corresponding core function on success.

Each item’s permissions are defined by a bitfield of roles. Roles can be defined in the schema. If the caller has any of the allowed roles, it has access. A permission mask of STOW_ROLE_INTERNAL (0) marks an item as internal-only, so no external caller can access it.

Configuration

# Stow dependencies
CONFIG_ZCBOR=y

# Enable the Stow library
CONFIG_GANTRY_STOW=y

# Maximum encoded size for items in persistent storage (bytes)
CONFIG_STOW_ITEM_STORAGE_SIZE_MAX=1024

# Delay before persistent items are saved to storage
CONFIG_STOW_STORAGE_FLUSH_DELAY_MS=1000

# Debug option to assert when callbacks exceed the time budget
CONFIG_STOW_CALLBACK_TIME_ASSERT=y
CONFIG_STOW_CALLBACK_TIME_BUDGET_US=200

API Reference

enum stow_subscription_mode

Subscription mode for stow item subscriptions.

Values:

enumerator STOW_SUBSCRIPTION_HANDLE

Subscribers are notified of which value has been updated, the subscriber must read the current value from the stow.

enumerator STOW_SUBSCRIPTION_COPY

Used for guaranteed delivery of every value. The notification contains a copy of the value at the time when the notification occurred.

enumerator STOW_SUBSCRIPTION_COUNT
typedef void (*stow_subscription_cb)(event_t *event)

Callback used to notify a subscriber when the subscribed value changes.

Note

Runs with interrupts masked, must be interrupt safe, any may not block.

Param event:

Pointer to a stow update event

typedef uint16_t stow_role_t

Role bitmask type for access control.

Each bit position represents one role from the app’s Stow specification. Permission checks are a bitwise AND between a caller’s role bitmask and an item’s permission mask.

Note

STOW_ROLE_INTERNAL marks and item as internal-only since no external role bit mask could pass the check.

const event_type_t stow_update_event

Stow update event declaration.

const struct stow_item_interface stow_buffer_interface

Stow interface for buffer items.

Operations expect to receive the address of a buffer_t pointer

const struct stow_item_interface stow_byte_array_interface

Stow interface for byte array items.

Operations expect to receive the address of a buffer_t pointer

const struct stow_item_interface stow_enum_interface

Stow interface for enum items.

Operations expect to receive the address of an int

const struct stow_item_interface stow_float_interface

Stow interface for float items.

Operations expect to receive the address of a float

const struct stow_item_interface stow_int_interface

Stow interface for int items.

Operations expect to receive the address of an integer

const struct stow_item_interface stow_string_interface

Stow interface for string items.

Operations expect to receive the address of a char pointer

void stow_init(void)

Initialize the stow.

Resets default values and subscriber state, then loads persistent stow item values. Called automatically at boot before any application modules run.

Note

This function is public and idempotent so it can be used to reset state in testing.

bool stow_is_id_valid(uint32_t id)

Check if a numeric ID is a valid stow item ID.

Parameters:

id – the numeric ID to check

Returns:

true If the ID can be cast to a stow_item_id

Returns:

false If the ID is not a valid item ID

bool stow_can_read(stow_role_t roles, enum stow_item_id id)

Check whether a caller’s roles may read an item.

Parameters:
  • roles – The caller’s role bitmask

  • id – Item ID to check

Returns:

true when roles overlap the item’s read permissions

const struct stow_item_const_metadata *stow_item_find(const char *name, size_t len)

Find an item by name.

Parameters:
  • name – The name to look up, which need not be NULL terminated

  • len – Length of name

Returns:

The item’s metadata, or NULL when no item has that name

int stow_flush(void)

Write modified persistent items to storage.

Writes are normally flushed by a background job. Call this to persist pending writes immediately, for example before a deliberate reset.

Note

Not interrupt safe.

Returns:

SUCCESS when every dirty item was saved

Returns:

the first error encountered, having attempted every dirty item

int stow_set(enum stow_item_id id, data_value_t value)

Set the value of a data item.

Note

Safe to call from interrupt context. Persistence is deferred.

Parameters:
  • id – Item ID to modify

  • value – The desired value

Returns:

SUCCESS when the value is set

Returns:

-EALREADY when the item is TOFU and has already been modified

Returns:

-EINVAL when the provided value is invalid

Returns:

-ENOMEM when the value cannot be stored

int stow_set_external(stow_role_t roles, enum stow_item_id id, data_value_t value)

Set the value of a data item on behalf of an external client.

Checks roles against the item’s write permissions before delegating to stow_set.

Parameters:
  • roles – The caller’s role bitmask

  • id – Item ID to modify

  • value – The desired value

Returns:

SUCCESS when the value is set

Returns:

-EACCES when the caller’s role has no overlap with the item’s write permission mask

Returns:

-EALREADY when the item is TOFU and has already been modified

Returns:

-EINVAL when the provided value is invalid

Returns:

-ENOMEM when the value cannot be stored

int stow_get(enum stow_item_id id, data_value_t *out_value)

Get the current value of a data item.

Acquires:

memory out_value on-success

Note

Safe to call from interrupt context.

Parameters:
  • id – [in] Item ID to retrieve

  • out_value – [out] Pointer to be populated with the item’s current value

Returns:

SUCCESS when the value was retrieved

Returns:

-EINVAL when the output pointer is NULL

Returns:

-ENOMEM when the value cannot be retrieved

int stow_get_external(stow_role_t roles, enum stow_item_id id, data_value_t *out_value)

Get the current value of a data item on behalf of an external client.

Checks roles against the item’s read permissions before delegating to stow_get.

Acquires:

memory out_value on-success

Parameters:
  • roles – [in] The caller’s role bitmask

  • id – [in] Item ID to retrieve

  • out_value – [out] Pointer to be populated with the item’s current value

Returns:

SUCCESS when the value was retrieved

Returns:

-EACCES when the caller’s role has no overlap with the item’s read permission mask

Returns:

-EINVAL when the output pointer is NULL

Returns:

-ENOMEM when the value cannot be retrieved

void stow_release(enum stow_item_id id, data_value_t *value)

Release a previously retrieved data item value.

stow_get may return a pointer to a memory block depending on the item’s data type. As a result, item values retrieved with stow_get should always be released.

Releases:

memory value

Parameters:
  • id – [in] Item ID, used to determine the data type

  • value – [inout] Pointer to the value to release.

void stow_ref(enum stow_item_id id, data_value_t *value)

Add a reference to a retrieved data item value; release it with stow_release.

Acquires:

memory value

Parameters:
  • id – [in] Item ID, used to determine the data type

  • value – [in] Pointer to the value to keep

int stow_encode(zcbor_state_t *encoder, enum stow_item_id id, data_value_t value)

Encode a data item value as CBOR.

Parameters:
  • encoder – CBOR encoder instance

  • id – Item ID, used to determine the data type

  • value – The value to encode

Returns:

SUCCESS when the value was encoded

Returns:

-ENOMEM when the encoder lacks room to encode the value

int stow_decode(zcbor_state_t *decoder, enum stow_item_id id, data_value_t *out_value)

Decode a data item value from CBOR.

Acquires:

memory out_value on-success

Parameters:
  • decoder – [in] CBOR decoder instance

  • id – [in] Item ID, used to determine the data type

  • out_value – [out] Pointer populated with the decoded value

Returns:

SUCCESS when the value was decoded

Returns:

-EBADMSG when the CBOR value could not be decoded

Returns:

-ENOMEM when memory cannot be allocated for the decoded value

int stow_subscribe(enum stow_item_id id, struct stow_subscription *subscription)

Subscribe to a data item.

Note

Safe to call from interrupt context.

Parameters:
  • id – Item ID to subscribe to

  • subscription – The stow subscription

Returns:

SUCCESS when the subscription is added

Returns:

-EALREADY when the requested subscription already exists

Returns:

-ENOMEM when there is no memory to create a subscription

int stow_subscribe_external(stow_role_t roles, enum stow_item_id id, struct stow_subscription *subscription)

Subscribe to a data item on behalf of an external client.

Checks roles against the item’s read permissions before delegating to stow_subscribe.

Parameters:
  • roles – The caller’s role bitmask

  • id – Item ID to subscribe to

  • subscription – The stow subscription

Returns:

SUCCESS when the subscription is added

Returns:

-EACCES when the caller’s role has no overlap with the item’s read permission mask

Returns:

-EALREADY when the requested subscription already exists

Returns:

-ENOMEM when there is no memory to create a subscription

int stow_unsubscribe(enum stow_item_id id, struct stow_subscription *subscription)

Unsubscribe from a data item.

Note

Safe to call from interrupt context.

Parameters:
  • id – Item ID to unsubscribe from

  • subscription – The subscription to remove

Returns:

SUCCESS when the subscription is removed from the data item

Returns:

-ENOENT when the subscription did not exist for the given data item

static inline bool stow_cbor_container_start(zcbor_state_t *encoder, zcbor_major_type_t major_type, uint32_t count)

Encode a definite-length container header.

Parameters:
  • encoder – CBOR encoder

  • major_type – Major type of the container

  • count – Number of elements, or of key-value pairs for a map

Returns:

true when the header was encoded

static inline bool stow_cbor_list_start(zcbor_state_t *encoder, uint32_t count)

Encode the header of a definite-length array.

Parameters:
  • encoder – CBOR encoder

  • count – Number of elements that follow

Returns:

true when the header was encoded

static inline bool stow_cbor_map_start(zcbor_state_t *encoder, uint32_t count)

Encode the header of a definite-length map.

Parameters:
  • encoder – CBOR encoder

  • count – Number of key-value pairs that follow

Returns:

true when the header was encoded

const struct stow_update_event_payload *stow_update_payload(const event_t *event)

Get the payload of a Stow update event.

Parameters:

event – A stow_update_event

Returns:

The event’s payload

void stow_event_on_free(event_t *event)

Release the data copy within a stow_update_event_payload.

Parameters:

event – Event being freed

int stow_storage_load(void)

Load data items from storage.

This should be called during stow initialization

Returns:

SUCCESS when the stow has been loaded from storage, non-zero on error

int stow_storage_save_item(const struct stow_item_const_metadata *item)

Save an item to persistent storage.

The value is only saved if it has changed.

Parameters:

item – Data item to save

Returns:

SUCCESS when the item is saved

Returns:

-ENOMEM when memory is not available

Returns:

result of settings_save_one on failure

int enum_get_name_from_value(const union stow_constraints *constraints, int value, char **out_name)

Get the enum value’s name from its numeric value.

Parameters:
  • constraints – [in] the enum’s constraints

  • value – [in] the numeric enum value

  • out_name – [out] the name of the enum value, only populated on success

Returns:

int SUCCESS if the value existed

Returns:

-EINVAL when the provided value was invalid

int enum_get_value_from_name(const union stow_constraints *constraints, char *name, int *out_value)

Get the enum value’s numeric value from its name.

Parameters:
  • constraints – [in] the enum’s constraints

  • name – [in] the name of the enum value

  • out_value – [out] numeric value of the enum value, only populated on success

Returns:

int SUCCESS if the name was valid

Returns:

-EINVAL when the provided name was invalid

static inline void stow_value_ref_block(data_value_t *value)

The stow_item_interface ref of values held in a memory block, such as strings and buffers.

Parameters:

value – Value whose block gains a reference, or that holds NULL

static inline void stow_value_ref_none(data_value_t *value)

The stow_item_interface ref of values held inline, such as ints, floats and enums.

Parameters:

value – Unused

STOW_SET(id, value)

Convenience macro for stow_set with memory tracing.

STOW_SET_EXTERNAL(roles, id, value)

Convenience macro for stow_set_external with memory tracing.

STOW_GET(id, out_value)

Convenience macro for stow_get with memory tracing.

Acquires:

memory out_value on-success

STOW_GET_EXTERNAL(roles, id, out_value)

Convenience macro for stow_get_external with memory tracing.

Acquires:

memory out_value on-success

STOW_RELEASE(id, value)

Convenience macro for stow_release with debug tracing.

Releases:

memory value

STOW_REF(id, value)

Convenience macro for stow_ref with debug tracing.

Acquires:

memory value

STOW_DECODE(decoder, id, out_value)

Convenience macro for stow_decode with debug tracing.

Acquires:

memory out_value on-success

STOW_SUBSCRIPTION_DEFINE(_name, _mode, _cb, ...)

Statically define stow subscriptions.

Use this instead of a runtime stow_subscribe call for subscriptions that live for the entire process lifetime.

Parameters:
  • _name – Base name for subscription, must be unique within the translation unit

  • _mode – Subscription mode, see stow_subscription_mode

  • _cb – Callback invoked when any listed item changes

  • ... – One or more item IDs

EVENT_ID_STOW_UPDATE

Event ID for stow update events.

STOW_ROLE_INTERNAL

Internal-only; no external client access

STOW_ROLE_ANY

Any external client role has access

struct stow_subscription
#include <stow.h>

Stow subscription.

Defines the subscription mode and callback to be called when the item is updated.

struct stow_item_dynamic_metadata
#include <stow.h>

Dynamic metadata for stow items.

struct stow_static_subscription
#include <stow.h>

A static stow subscription.

Created by STOW_SUBSCRIPTION_DEFINE. These subscriptions exist for the full lifetime of the app.

struct stow_update_event_payload
#include <stow_event.h>

Event payload for stow update events.

union raw_data_value_t
#include <stow_types.h>

Union of raw values for each data type.

Public Members

int int_value

Value for ints

float float_value

Value for floats

char *string_value

Value for strings

buffer_t *buffer_value

Value for buffers

void *raw_value

Raw pointer value for structs

struct data_value_t
#include <stow_types.h>

Tagged union representing a data item value.

struct stow_permissions
#include <stow_types.h>

Permission bitmasks required to read or write an associated data item.

struct data_enum_value
#include <stow_types.h>

Value and name pair for an enum value.

struct stow_enum_constraints
#include <stow_types.h>

Constraints for enum items.

struct stow_int_constraints
#include <stow_types.h>

Constraints for int items.

struct stow_float_constraints
#include <stow_types.h>

Constraints for float items.

struct stow_buffer_constraints
#include <stow_types.h>

Constraints for variable length buffer items.

union stow_constraints
#include <stow_types.h>

Constraints for item value.

Public Members

struct stow_enum_constraints enum_constraints

Constraints for enum items

struct stow_int_constraints int_constraints

Constraints for int items

struct stow_float_constraints float_constraints

Constraints for float items

struct stow_buffer_constraints buffer_constraints

Constraints for variable length buffer types (string, bytes, buffers)

struct stow_item_custom_interface
#include <stow_types.h>

Optional application-provided interface overrides for an item.

Each field may be NULL indicating that the function has no custom implementation.

struct stow_item_interface
#include <stow_types.h>

Common interface for each stow item, implemented for each item type.

struct stow_item_const_metadata
#include <stow_types.h>

Constant metadata for stow items.