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: Not used.
- 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. This field is not used, so it is set to
ZBUS_MSG_INIT(0).
Note
In zbus, "message type" can refer to the data type or structure that holds the message data for a channel, in this case struct network_msg. In the Asset Tracker Template, "message type" means the enumerated value that distinguishes the kinds of messages sent on one channel, in this case the type field of struct network_msg.