6#ifndef ZEPHYR_INCLUDE_ZBUS_H_
7#define ZEPHYR_INCLUDE_ZBUS_H_
30#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
57#if (CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE > 0) || defined(__DOXYGEN__)
86#if defined(CONFIG_ZBUS_OBSERVER_NAME) || defined(__DOXYGEN__)
101#if defined(CONFIG_ZBUS_ASSERT_MOCK)
102#define _ZBUS_ASSERT(_cond, _fmt, ...) \
105 printk("ZBUS ASSERT: "); \
106 printk(_fmt, ##__VA_ARGS__); \
112#define _ZBUS_ASSERT(_cond, _fmt, ...) __ASSERT(_cond, _fmt, ##__VA_ARGS__)
115#if defined(CONFIG_ZBUS_CHANNEL_NAME)
116#define ZBUS_CHANNEL_NAME_INIT(_name) .name = #_name,
118#define ZBUS_CHANNEL_NAME_INIT(_name)
121#if defined(CONFIG_ZBUS_OBSERVER_NAME)
122#define ZBUS_OBSERVER_NAME_INIT(_name) .name = #_name,
123#define _ZBUS_OBS_NAME(_obs) (_obs)->name
125#define ZBUS_OBSERVER_NAME_INIT(_name)
126#define _ZBUS_OBS_NAME(_obs) ""
129#if CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE > 0
130#define ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_slist_name) static sys_slist_t _slist_name
131#define ZBUS_RUNTIME_OBSERVERS_LIST_INIT(_slist_name) .runtime_observers = &_slist_name,
133#define ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_slist_name)
134#define ZBUS_RUNTIME_OBSERVERS_LIST_INIT(_slist_name)
137#if defined(CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS)
138#define _ZBUS_STRUCT_DECLARE(_type, _name) STRUCT_SECTION_ITERABLE(_type, _name)
140#define _ZBUS_STRUCT_DECLARE(_type, _name) struct _type _name
143#define _ZBUS_OBS_EXTERN(_name) extern struct zbus_observer _name
145#define _ZBUS_CHAN_EXTERN(_name) extern const struct zbus_channel _name
147#define ZBUS_REF(_value) &(_value)
157#define ZBUS_OBS_DECLARE(...) FOR_EACH(_ZBUS_OBS_EXTERN, (;), __VA_ARGS__)
164#define ZBUS_CHAN_DECLARE(...) FOR_EACH(_ZBUS_CHAN_EXTERN, (;), __VA_ARGS__)
170#define ZBUS_OBSERVERS_EMPTY
177#define ZBUS_OBSERVERS(...) __VA_ARGS__
194#define ZBUS_CHAN_DEFINE(_name, _type, _validator, _user_data, _observers, _init_val) \
195 static _type _CONCAT(_zbus_message_, _name) = _init_val; \
196 static K_MUTEX_DEFINE(_CONCAT(_zbus_mutex_, _name)); \
197 ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_CONCAT(_runtime_observers_, _name)); \
198 FOR_EACH_NONEMPTY_TERM(_ZBUS_OBS_EXTERN, (;), _observers) \
199 static const struct zbus_observer *const _CONCAT(_zbus_observers_, _name)[] = { \
200 FOR_EACH_NONEMPTY_TERM(ZBUS_REF, (,), _observers) NULL}; \
201 const _ZBUS_STRUCT_DECLARE(zbus_channel, _name) = { \
202 ZBUS_CHANNEL_NAME_INIT(_name) \
203 .message_size = sizeof(_type), \
204 .user_data = _user_data, \
205 .message = &_CONCAT(_zbus_message_, _name), \
206 .validator = (_validator), \
207 .mutex = &_CONCAT(_zbus_mutex_, _name), \
208 ZBUS_RUNTIME_OBSERVERS_LIST_INIT( \
209 _CONCAT(_runtime_observers_, _name)) \
210 .observers = _CONCAT(_zbus_observers_, _name)}
221#define ZBUS_MSG_INIT(_val, ...) \
223 _val, ##__VA_ARGS__ \
236#define ZBUS_SUBSCRIBER_DEFINE(_name, _queue_size) \
237 K_MSGQ_DEFINE(_zbus_observer_queue_##_name, sizeof(const struct zbus_channel *), \
238 _queue_size, sizeof(const struct zbus_channel *)); \
239 _ZBUS_STRUCT_DECLARE(zbus_observer, \
240 _name) = {ZBUS_OBSERVER_NAME_INIT(_name) \
242 .queue = &_zbus_observer_queue_##_name, .callback = NULL}
254#define ZBUS_LISTENER_DEFINE(_name, _cb) \
255 _ZBUS_STRUCT_DECLARE(zbus_observer, \
256 _name) = {ZBUS_OBSERVER_NAME_INIT(_name) \
258 .queue = NULL, .callback = (_cb)}
363#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
376 __ASSERT(chan != NULL,
"chan is required");
397 __ASSERT(chan != NULL,
"chan is required");
418 __ASSERT(chan != NULL,
"chan is required");
434 __ASSERT(chan != NULL,
"chan is required");
450 __ASSERT(chan != NULL,
"chan is required");
455#if (CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE > 0) || defined(__DOXYGEN__)
507struct zbus_observer_node {
531 _ZBUS_ASSERT(obs != NULL,
"obs is required");
538#if defined(CONFIG_ZBUS_OBSERVER_NAME) || defined(__DOXYGEN__)
551 __ASSERT(obs != NULL,
"obs is required");
579#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:374
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:549
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:432
static void * zbus_chan_msg(const struct zbus_channel *chan)
Get the reference for a channel message directly.
Definition: zbus.h:395
static void * zbus_chan_user_data(const struct zbus_channel *chan)
Get the channel's user data.
Definition: zbus.h:448
static int zbus_obs_set_enable(struct zbus_observer *obs, bool enabled)
Change the observer state.
Definition: zbus.h:529
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:416
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:4248
Definition: kernel.h:2764
Kernel timeout type.
Definition: sys_clock.h:65
Type used to represent a channel.
Definition: zbus.h:29
bool(*const validator)(const void *msg, size_t msg_size)
Definition: zbus.h:51
sys_slist_t * runtime_observers
Definition: zbus.h:61
const uint16_t message_size
Definition: zbus.h:35
const struct zbus_observer *const * observers
Definition: zbus.h:67
void *const message
Definition: zbus.h:45
const char *const name
Definition: zbus.h:32
void *const user_data
Definition: zbus.h:40
struct k_mutex * mutex
Definition: zbus.h:56
Type used to represent an observer.
Definition: zbus.h:85
void(*const callback)(const struct zbus_channel *chan)
Definition: zbus.h:96
struct k_msgq *const queue
Definition: zbus.h:93
bool enabled
Definition: zbus.h:91
const char *const name
Definition: zbus.h:88
static void msg(uint64_t c64)
Definition: main.c:17