Channels

Asset Tracker Template

tags
Asset Tracker Template

In the Asset Tracker Template, each module declares and defines a channel using zbus macros. For example, in the app/src/modules/network/network.h file, the Network module declares the network_chan channel:

/* Channels provided by this module */
ZBUS_CHAN_DECLARE(
    network_chan
);

The channel is defined in app/src/modules/network/network.c:

/* Define channels provided by this module */
ZBUS_CHAN_DEFINE(network_chan, /* Channel name, derived from module name */
        struct network_msg,    /* Message data type */
        NULL,                  /* Optional validator function */
        NULL,                  /* Optional pointer to user data */
        ZBUS_OBSERVERS_EMPTY,  /* Initial observers */
        ZBUS_MSG_INIT(0)       /* Message initialization */
);

In the Asset Tracker Template, the fields of the ZBUS_CHAN_DEFINE macro are populated in a similar way across modules:

  • Channel name: The name of the channel, derived from the module name.
  • Message data type: The data type used to hold message data. The name comes from the channel name.
  • Validator function and User data: Not used.
  • Initial observers: The initial observer list is empty. Observers are added in the relevant modules later.
  • Message initialization: The initial value stored in the channel. Not used and therefore set to ZBUS_MSG_INIT(0).

Important

In the context of zbus, you can use the term "message type" to refer to the data type or structure containing the message data for a given channel, in this case struct network_msg. In the Asset Tracker Template architecture, "message type" is used to mean the enumerated value that distinguishes different kinds of messages sent on the same channel, in this case the type field of struct network_msg.