============================= 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: .. code-block:: cmake 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 :doc:`server`): .. code-block:: c 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. :any:`stow_client_availability` says whether an item can be used, and the peer's schema should follow the :ref:`compatibility rule `. The client reports each peer to the hooks registered with :any:`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: .. code-block:: c 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 :any:`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: .. code-block:: c 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: .. code-block:: c 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 :any:`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: .. code-block:: c 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 :ref:`protocol reference ` describes, so a module may lose one to a full queue. API Reference ============= .. doxygengroup:: stow_client :content-only: