============================ 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 :doc:`protocol` describes what the server and its clients send. Enabling the Server =================== .. code-block:: kconfig 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: .. code-block:: c #include #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: .. code-block:: c 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 :any:`stow_cobs_transport_close` when the client leaves. The same connection can also carry this device's requests to the client's Stow, see :doc:`client`. 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 :any:`stow_protocol_set_roles`: .. code-block:: c 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 :any:`stow_transport`, which names the function that sends a message and the pool outgoing messages come from, and a :any:`stow_connection` for each client it carries. It opens a connection with :any:`stow_connection_open` when a client connects, passes each whole message to :any:`stow_connection_receive`, and closes the connection with :any:`stow_connection_close` when the client leaves. The COBS transport is a good starting point. API Reference ============= .. doxygengroup:: stow_protocol :content-only: