6#ifndef ZEPHYR_INCLUDE_ZBUS_H_
7#define ZEPHYR_INCLUDE_ZBUS_H_
32#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
59#if (CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE > 0) || defined(__DOXYGEN__)
88#if defined(CONFIG_ZBUS_OBSERVER_NAME) || defined(__DOXYGEN__)
103#if defined(CONFIG_ZBUS_ASSERT_MOCK)
104#define _ZBUS_ASSERT(_cond, _fmt, ...) \
107 printk("ZBUS ASSERT: "); \
108 printk(_fmt, ##__VA_ARGS__); \
114#define _ZBUS_ASSERT(_cond, _fmt, ...) __ASSERT(_cond, _fmt, ##__VA_ARGS__)
117#if defined(CONFIG_ZBUS_CHANNEL_NAME)
118#define ZBUS_CHANNEL_NAME_INIT(_name) .name = #_name,
120#define ZBUS_CHANNEL_NAME_INIT(_name)
123#if defined(CONFIG_ZBUS_OBSERVER_NAME)
124#define ZBUS_OBSERVER_NAME_INIT(_name) .name = #_name,
125#define _ZBUS_OBS_NAME(_obs) (_obs)->name
127#define ZBUS_OBSERVER_NAME_INIT(_name)
128#define _ZBUS_OBS_NAME(_obs) ""
131#if CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE > 0
132#define ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_slist_name) static sys_slist_t _slist_name
133#define ZBUS_RUNTIME_OBSERVERS_LIST_INIT(_slist_name) .runtime_observers = &_slist_name,
135#define ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_slist_name)
136#define ZBUS_RUNTIME_OBSERVERS_LIST_INIT(_slist_name)
139#if defined(CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS)
140#define _ZBUS_STRUCT_DECLARE(_type, _name) STRUCT_SECTION_ITERABLE(_type, _name)
142#define _ZBUS_STRUCT_DECLARE(_type, _name) struct _type _name
145#define _ZBUS_OBS_EXTERN(_name) extern struct zbus_observer _name
147#define _ZBUS_CHAN_EXTERN(_name) extern const struct zbus_channel _name
149#define ZBUS_REF(_value) &(_value)
159#define ZBUS_OBS_DECLARE(...) FOR_EACH(_ZBUS_OBS_EXTERN, (;), __VA_ARGS__)
166#define ZBUS_CHAN_DECLARE(...) FOR_EACH(_ZBUS_CHAN_EXTERN, (;), __VA_ARGS__)
172#define ZBUS_OBSERVERS_EMPTY
179#define ZBUS_OBSERVERS(...) __VA_ARGS__
196#define ZBUS_CHAN_DEFINE(_name, _type, _validator, _user_data, _observers, _init_val) \
197 static _type _CONCAT(_zbus_message_, _name) = _init_val; \
198 static K_MUTEX_DEFINE(_CONCAT(_zbus_mutex_, _name)); \
199 ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_CONCAT(_runtime_observers_, _name)); \
200 FOR_EACH_NONEMPTY_TERM(_ZBUS_OBS_EXTERN, (;), _observers) \
201 static const struct zbus_observer *const _CONCAT(_zbus_observers_, _name)[] = { \
202 FOR_EACH_NONEMPTY_TERM(ZBUS_REF, (,), _observers) NULL}; \
203 const _ZBUS_STRUCT_DECLARE(zbus_channel, _name) = { \
204 ZBUS_CHANNEL_NAME_INIT(_name) \
205 .message_size = sizeof(_type), \
206 .user_data = _user_data, \
207 .message = &_CONCAT(_zbus_message_, _name), \
208 .validator = (_validator), \
209 .mutex = &_CONCAT(_zbus_mutex_, _name), \
210 ZBUS_RUNTIME_OBSERVERS_LIST_INIT( \
211 _CONCAT(_runtime_observers_, _name)) \
212 .observers = _CONCAT(_zbus_observers_, _name)}
223#define ZBUS_MSG_INIT(_val, ...) \
225 _val, ##__VA_ARGS__ \
238#define ZBUS_SUBSCRIBER_DEFINE(_name, _queue_size) \
239 K_MSGQ_DEFINE(_zbus_observer_queue_##_name, sizeof(const struct zbus_channel *), \
240 _queue_size, sizeof(const struct zbus_channel *)); \
241 _ZBUS_STRUCT_DECLARE(zbus_observer, \
242 _name) = {ZBUS_OBSERVER_NAME_INIT(_name) \
244 .queue = &_zbus_observer_queue_##_name, .callback = NULL}
256#define ZBUS_LISTENER_DEFINE(_name, _cb) \
257 _ZBUS_STRUCT_DECLARE(zbus_observer, \
258 _name) = {ZBUS_OBSERVER_NAME_INIT(_name) \
260 .queue = NULL, .callback = (_cb)}
365#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
378 __ASSERT(chan != NULL,
"chan is required");
399 __ASSERT(chan != NULL,
"chan is required");
420 __ASSERT(chan != NULL,
"chan is required");
436 __ASSERT(chan != NULL,
"chan is required");
452 __ASSERT(chan != NULL,
"chan is required");
457#if (CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE > 0) || defined(__DOXYGEN__)
509struct zbus_observer_node {
533 _ZBUS_ASSERT(obs != NULL,
"obs is required");
540#if defined(CONFIG_ZBUS_OBSERVER_NAME) || defined(__DOXYGEN__)
553 __ASSERT(obs != NULL,
"obs is required");
581#if defined(CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS) || defined(__DOXYGEN__)
ZTEST_BMEM int timeout
Definition: main.c:31
int zbus_chan_claim(const struct zbus_channel *chan, k_timeout_t timeout)
Claim a channel.
static const char * zbus_chan_name(const struct zbus_channel *chan)
Get the channel's name.
Definition: zbus.h:376
struct k_mem_slab * zbus_runtime_obs_pool(void)
Get zbus runtime observers pool.
bool zbus_iterate_over_observers(bool(*iterator_func)(const struct zbus_observer *obs))
Iterate over observers.
static const char * zbus_obs_name(const struct zbus_observer *obs)
Get the observer's name.
Definition: zbus.h:551
bool zbus_iterate_over_channels(bool(*iterator_func)(const struct zbus_channel *chan))
Iterate over channels.
int zbus_chan_notify(const struct zbus_channel *chan, k_timeout_t timeout)
Force a channel notification.
int zbus_chan_finish(const struct zbus_channel *chan)
Finish a channel claim.
int zbus_chan_read(const struct zbus_channel *chan, void *msg, k_timeout_t timeout)
Read a channel.
int zbus_sub_wait(const struct zbus_observer *sub, const struct zbus_channel **chan, k_timeout_t timeout)
Wait for a channel notification.
static uint16_t zbus_chan_msg_size(const struct zbus_channel *chan)
Get the channel's message size.
Definition: zbus.h:434
static void * zbus_chan_msg(const struct zbus_channel *chan)
Get the reference for a channel message directly.
Definition: zbus.h:397
static void * zbus_chan_user_data(const struct zbus_channel *chan)
Get the channel's user data.
Definition: zbus.h:450
static int zbus_obs_set_enable(struct zbus_observer *obs, bool enabled)
Change the observer state.
Definition: zbus.h:531
int zbus_chan_add_obs(const struct zbus_channel *chan, const struct zbus_observer *obs, k_timeout_t timeout)
Add an observer to a channel.
int zbus_chan_pub(const struct zbus_channel *chan, const void *msg, k_timeout_t timeout)
Publish to a channel.
int zbus_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_observer *obs, k_timeout_t timeout)
Remove an observer from a channel.
static const void * zbus_chan_const_msg(const struct zbus_channel *chan)
Get a constant reference for a channel message directly.
Definition: zbus.h:418
struct _slist sys_slist_t
Definition: slist.h:40
struct _snode sys_snode_t
Definition: slist.h:33
#define bool
Definition: stdbool.h:13
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Message Queue Structure.
Definition: kernel.h:4306
Definition: kernel.h:2822
Kernel timeout type.
Definition: sys_clock.h:65
Type used to represent a channel.
Definition: zbus.h:31
bool(*const validator)(const void *msg, size_t msg_size)
Definition: zbus.h:53
sys_slist_t * runtime_observers
Definition: zbus.h:63
const uint16_t message_size
Definition: zbus.h:37
const struct zbus_observer *const * observers
Definition: zbus.h:69
void *const message
Definition: zbus.h:47
const char *const name
Definition: zbus.h:34
void *const user_data
Definition: zbus.h:42
struct k_mutex * mutex
Definition: zbus.h:58
Type used to represent an observer.
Definition: zbus.h:87
void(*const callback)(const struct zbus_channel *chan)
Definition: zbus.h:98
struct k_msgq *const queue
Definition: zbus.h:95
bool enabled
Definition: zbus.h:93
const char *const name
Definition: zbus.h:90
static void msg(uint64_t c64)
Definition: main.c:17