Zephyr Project API 4.1.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
zbus.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Rodrigo Peixoto <rodrigopex@gmail.com>
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#ifndef ZEPHYR_INCLUDE_ZBUS_H_
7#define ZEPHYR_INCLUDE_ZBUS_H_
8
9#include <string.h>
10
11#include <zephyr/kernel.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
37
42
46 struct k_sem sem;
47
48#if defined(CONFIG_ZBUS_PRIORITY_BOOST)
52 int highest_observer_priority;
53#endif /* CONFIG_ZBUS_PRIORITY_BOOST */
54
55#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS) || defined(__DOXYGEN__)
60#endif /* CONFIG_ZBUS_RUNTIME_OBSERVERS */
61
62#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION) || defined(__DOXYGEN__)
66#endif /* ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION */
67
68#if defined(CONFIG_ZBUS_CHANNEL_PUBLISH_STATS) || defined(__DOXYGEN__)
73#endif /* CONFIG_ZBUS_CHANNEL_PUBLISH_STATS */
74};
75
83#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
85 const char *name;
86#endif
87#if defined(CONFIG_ZBUS_CHANNEL_ID) || defined(__DOXYGEN__)
90#endif
94 void *message;
95
98
103
108 bool (*validator)(const void *msg, size_t msg_size);
109
112};
113
124
128
129#if defined(CONFIG_ZBUS_PRIORITY_BOOST)
131 int priority;
132#endif /* CONFIG_ZBUS_PRIORITY_BOOST */
133};
134
151#if defined(CONFIG_ZBUS_OBSERVER_NAME) || defined(__DOXYGEN__)
153 const char *name;
154#endif
157
160
161 union {
163 struct k_msgq *queue;
164
166 void (*callback)(const struct zbus_channel *chan);
167
168#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER) || defined(__DOXYGEN__)
173#endif /* CONFIG_ZBUS_MSG_SUBSCRIBER */
174 };
175};
176
178struct zbus_channel_observation_mask {
179 bool enabled;
180};
181
185struct zbus_channel_observation {
186 const struct zbus_channel *chan;
187 const struct zbus_observer *obs;
188};
189
190#ifdef __cplusplus
191#define _ZBUS_CPP_EXTERN extern
192#else
193#define _ZBUS_CPP_EXTERN
194#endif /* __cplusplus */
195
196#define ZBUS_MIN_THREAD_PRIORITY (CONFIG_NUM_PREEMPT_PRIORITIES - 1)
197
198#if defined(CONFIG_ZBUS_ASSERT_MOCK)
199#define _ZBUS_ASSERT(_cond, _fmt, ...) \
200 do { \
201 if (!(_cond)) { \
202 printk("ZBUS ASSERT: "); \
203 printk(_fmt, ##__VA_ARGS__); \
204 printk("\n"); \
205 return -EFAULT; \
206 } \
207 } while (0)
208#else
209#define _ZBUS_ASSERT(_cond, _fmt, ...) __ASSERT(_cond, _fmt, ##__VA_ARGS__)
210#endif
211
212#if defined(CONFIG_ZBUS_CHANNEL_NAME)
213#define ZBUS_CHANNEL_NAME_INIT(_name) .name = #_name,
214#define _ZBUS_CHAN_NAME(_chan) (_chan)->name
215#else
216#define ZBUS_CHANNEL_NAME_INIT(_name)
217#define _ZBUS_CHAN_NAME(_chan) ""
218#endif
219
220#if defined(CONFIG_ZBUS_OBSERVER_NAME)
221#define ZBUS_OBSERVER_NAME_INIT(_name) .name = #_name,
222#define _ZBUS_OBS_NAME(_obs) (_obs)->name
223#else
224#define ZBUS_OBSERVER_NAME_INIT(_name)
225#define _ZBUS_OBS_NAME(_obs) ""
226#endif
227
228#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS)
229#define ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_slist_name) static sys_slist_t _slist_name
230#define ZBUS_RUNTIME_OBSERVERS_LIST_INIT(_slist_name) .runtime_observers = &_slist_name,
231#else
232#define ZBUS_RUNTIME_OBSERVERS_LIST_DECL(_slist_name)
233#define ZBUS_RUNTIME_OBSERVERS_LIST_INIT(_slist_name) /* No runtime observers */
234#endif
235
236#define _ZBUS_OBS_EXTERN(_name) extern const struct zbus_observer _name
237
238#define _ZBUS_CHAN_EXTERN(_name) extern const struct zbus_channel _name
239
240#define ZBUS_REF(_value) &(_value)
241
242#define FOR_EACH_FIXED_ARG_NONEMPTY_TERM(F, sep, fixed_arg, ...) \
243 COND_CODE_0(/* are there zero non-empty arguments ? */ \
244 NUM_VA_ARGS_LESS_1( \
245 LIST_DROP_EMPTY(__VA_ARGS__, _)), /* if so, expand to nothing */ \
246 (), /* otherwise, expand to: */ \
247 (FOR_EACH_IDX_FIXED_ARG( \
248 F, sep, fixed_arg, \
249 LIST_DROP_EMPTY(__VA_ARGS__)) /* plus a final terminator */ \
250 __DEBRACKET sep))
251
252#define _ZBUS_OBSERVATION_PREFIX(_idx) \
253 GET_ARG_N(_idx, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, \
254 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, \
255 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, \
256 58, 59, 60, 61, 62, 63)
257
258#define _ZBUS_CHAN_OBSERVATION(_idx, _obs, _chan) \
259 const STRUCT_SECTION_ITERABLE( \
260 zbus_channel_observation, \
261 _CONCAT(_chan, _ZBUS_OBSERVATION_PREFIX(UTIL_INC(_idx)))) = {.chan = &_chan, \
262 .obs = &_obs}; \
263 STRUCT_SECTION_ITERABLE(zbus_channel_observation_mask, \
264 _CONCAT(_CONCAT(_chan, _ZBUS_OBSERVATION_PREFIX(UTIL_INC(_idx))), \
265 _mask)) = {.enabled = false};
266
267#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS) || defined(__DOXYGEN__)
268#define _ZBUS_RUNTIME_OBSERVERS(_name) .observers = &(_CONCAT(_observers_, _name)),
269#define _ZBUS_RUNTIME_OBSERVERS_DECL(_name) static sys_slist_t _CONCAT(_observers_, _name);
270#else
271#define _ZBUS_RUNTIME_OBSERVERS(_name)
272#define _ZBUS_RUNTIME_OBSERVERS_DECL(_name)
273#endif /* CONFIG_ZBUS_RUNTIME_OBSERVERS */
274
275#define _ZBUS_MESSAGE_NAME(_name) _CONCAT(_zbus_message_, _name)
276
277/* clang-format off */
278#define _ZBUS_CHAN_DEFINE(_name, _id, _type, _validator, _user_data) \
279 static struct zbus_channel_data _CONCAT(_zbus_chan_data_, _name) = { \
280 .observers_start_idx = -1, \
281 .observers_end_idx = -1, \
282 .sem = Z_SEM_INITIALIZER(_CONCAT(_zbus_chan_data_, _name).sem, 1, 1), \
283 IF_ENABLED(CONFIG_ZBUS_PRIORITY_BOOST, \
284 (.highest_observer_priority = ZBUS_MIN_THREAD_PRIORITY,)) \
285 IF_ENABLED(CONFIG_ZBUS_RUNTIME_OBSERVERS, \
286 (.observers = SYS_SLIST_STATIC_INIT( \
287 &_CONCAT(_zbus_chan_data_, _name).observers),)) \
288 }; \
289 static K_MUTEX_DEFINE(_CONCAT(_zbus_mutex_, _name)); \
290 _ZBUS_CPP_EXTERN const STRUCT_SECTION_ITERABLE(zbus_channel, _name) = { \
291 ZBUS_CHANNEL_NAME_INIT(_name) /* Maybe removed */ \
292 IF_ENABLED(CONFIG_ZBUS_CHANNEL_ID, (.id = _id,)) \
293 .message = &_ZBUS_MESSAGE_NAME(_name), \
294 .message_size = sizeof(_type), \
295 .user_data = _user_data, \
296 .validator = _validator, \
297 .data = &_CONCAT(_zbus_chan_data_, _name), \
298 IF_ENABLED(ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION, \
299 (.msg_subscriber_pool = &_zbus_msg_subscribers_pool,)) \
300 }
301/* clang-format on */
302
305/* clang-format off */
306
318#define ZBUS_CHAN_ADD_OBS_WITH_MASK(_chan, _obs, _masked, _prio) \
319 const STRUCT_SECTION_ITERABLE(zbus_channel_observation, \
320 _CONCAT(_CONCAT(_chan, zz), _CONCAT(_prio, _obs))) = { \
321 .chan = &_chan, \
322 .obs = &_obs, \
323 }; \
324 STRUCT_SECTION_ITERABLE(zbus_channel_observation_mask, \
325 _CONCAT(_CONCAT(_CONCAT(_chan, zz), _CONCAT(_prio, _obs)), \
326 _mask)) = {.enabled = _masked}
327/* clang-format on */
328
339#define ZBUS_CHAN_ADD_OBS(_chan, _obs, _prio) ZBUS_CHAN_ADD_OBS_WITH_MASK(_chan, _obs, false, _prio)
340
346#define ZBUS_OBS_DECLARE(...) FOR_EACH_NONEMPTY_TERM(_ZBUS_OBS_EXTERN, (;), __VA_ARGS__)
347
353#define ZBUS_CHAN_DECLARE(...) FOR_EACH(_ZBUS_CHAN_EXTERN, (;), __VA_ARGS__)
354
359#define ZBUS_OBSERVERS_EMPTY
360
366#define ZBUS_OBSERVERS(...) __VA_ARGS__
367
372#define ZBUS_CHAN_ID_INVALID UINT32_MAX
373
389#define ZBUS_CHAN_DEFINE(_name, _type, _validator, _user_data, _observers, _init_val) \
390 static _type _ZBUS_MESSAGE_NAME(_name) = _init_val; \
391 _ZBUS_CHAN_DEFINE(_name, ZBUS_CHAN_ID_INVALID, _type, _validator, _user_data); \
392 /* Extern declaration of observers */ \
393 ZBUS_OBS_DECLARE(_observers); \
394 /* Create all channel observations from observers list */ \
395 FOR_EACH_FIXED_ARG_NONEMPTY_TERM(_ZBUS_CHAN_OBSERVATION, (;), _name, _observers)
396
413#define ZBUS_CHAN_DEFINE_WITH_ID(_name, _id, _type, _validator, _user_data, _observers, _init_val) \
414 static _type _ZBUS_MESSAGE_NAME(_name) = _init_val; \
415 _ZBUS_CHAN_DEFINE(_name, _id, _type, _validator, _user_data); \
416 /* Extern declaration of observers */ \
417 ZBUS_OBS_DECLARE(_observers); \
418 /* Create all channel observations from observers list */ \
419 FOR_EACH_FIXED_ARG_NONEMPTY_TERM(_ZBUS_CHAN_OBSERVATION, (;), _name, _observers)
420
430#define ZBUS_MSG_INIT(_val, ...) {_val, ##__VA_ARGS__}
431
432/* clang-format off */
433
445#define ZBUS_SUBSCRIBER_DEFINE_WITH_ENABLE(_name, _queue_size, _enable) \
446 K_MSGQ_DEFINE(_zbus_observer_queue_##_name, \
447 sizeof(struct zbus_channel *), \
448 _queue_size, sizeof(struct zbus_channel *) \
449 ); \
450 static struct zbus_observer_data _CONCAT(_zbus_obs_data_, _name) = { \
451 .enabled = _enable, \
452 IF_ENABLED(CONFIG_ZBUS_PRIORITY_BOOST, ( \
453 .priority = ZBUS_MIN_THREAD_PRIORITY, \
454 )) \
455 }; \
456 _ZBUS_CPP_EXTERN const STRUCT_SECTION_ITERABLE(zbus_observer, _name) = { \
457 ZBUS_OBSERVER_NAME_INIT(_name) /* Name field */ \
458 .type = ZBUS_OBSERVER_SUBSCRIBER_TYPE, \
459 .data = &_CONCAT(_zbus_obs_data_, _name), \
460 .queue = &_zbus_observer_queue_##_name, \
461 }
462/* clang-format on */
463
475#define ZBUS_SUBSCRIBER_DEFINE(_name, _queue_size) \
476 ZBUS_SUBSCRIBER_DEFINE_WITH_ENABLE(_name, _queue_size, true)
477
478/* clang-format off */
479
491#define ZBUS_LISTENER_DEFINE_WITH_ENABLE(_name, _cb, _enable) \
492 static struct zbus_observer_data _CONCAT(_zbus_obs_data_, _name) = { \
493 .enabled = _enable, \
494 IF_ENABLED(CONFIG_ZBUS_PRIORITY_BOOST, ( \
495 .priority = ZBUS_MIN_THREAD_PRIORITY, \
496 )) \
497 }; \
498 _ZBUS_CPP_EXTERN const STRUCT_SECTION_ITERABLE(zbus_observer, _name) = { \
499 ZBUS_OBSERVER_NAME_INIT(_name) /* Name field */ \
500 .type = ZBUS_OBSERVER_LISTENER_TYPE, \
501 .data = &_CONCAT(_zbus_obs_data_, _name), \
502 .callback = (_cb) \
503 }
504/* clang-format on */
505
516#define ZBUS_LISTENER_DEFINE(_name, _cb) ZBUS_LISTENER_DEFINE_WITH_ENABLE(_name, _cb, true)
517
518/* clang-format off */
519
530#define ZBUS_MSG_SUBSCRIBER_DEFINE_WITH_ENABLE(_name, _enable) \
531 static K_FIFO_DEFINE(_zbus_observer_fifo_##_name); \
532 static struct zbus_observer_data _CONCAT(_zbus_obs_data_, _name) = { \
533 .enabled = _enable, \
534 IF_ENABLED(CONFIG_ZBUS_PRIORITY_BOOST, ( \
535 .priority = ZBUS_MIN_THREAD_PRIORITY, \
536 )) \
537 }; \
538 _ZBUS_CPP_EXTERN const STRUCT_SECTION_ITERABLE(zbus_observer, _name) = { \
539 ZBUS_OBSERVER_NAME_INIT(_name) /* Name field */ \
540 .type = ZBUS_OBSERVER_MSG_SUBSCRIBER_TYPE, \
541 .data = &_CONCAT(_zbus_obs_data_, _name), \
542 .message_fifo = &_zbus_observer_fifo_##_name, \
543 }
544/* clang-format on */
545
557#define ZBUS_MSG_SUBSCRIBER_DEFINE(_name) ZBUS_MSG_SUBSCRIBER_DEFINE_WITH_ENABLE(_name, true)
579int zbus_chan_pub(const struct zbus_channel *chan, const void *msg, k_timeout_t timeout);
580
598int zbus_chan_read(const struct zbus_channel *chan, void *msg, k_timeout_t timeout);
599
621int zbus_chan_claim(const struct zbus_channel *chan, k_timeout_t timeout);
622
637int zbus_chan_finish(const struct zbus_channel *chan);
638
657int zbus_chan_notify(const struct zbus_channel *chan, k_timeout_t timeout);
658
659#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
660
670static inline const char *zbus_chan_name(const struct zbus_channel *chan)
671{
672 __ASSERT(chan != NULL, "chan is required");
673
674 return chan->name;
675}
676
677#endif
678
679#if defined(CONFIG_ZBUS_CHANNEL_ID) || defined(__DOXYGEN__)
680
689const struct zbus_channel *zbus_chan_from_id(uint32_t channel_id);
690
691#endif
692
705static inline void *zbus_chan_msg(const struct zbus_channel *chan)
706{
707 __ASSERT(chan != NULL, "chan is required");
708
709 return chan->message;
710}
711
726static inline const void *zbus_chan_const_msg(const struct zbus_channel *chan)
727{
728 __ASSERT(chan != NULL, "chan is required");
729
730 return chan->message;
731}
732
742static inline uint16_t zbus_chan_msg_size(const struct zbus_channel *chan)
743{
744 __ASSERT(chan != NULL, "chan is required");
745
746 return chan->message_size;
747}
748
758static inline void *zbus_chan_user_data(const struct zbus_channel *chan)
759{
760 __ASSERT(chan != NULL, "chan is required");
761
762 return chan->user_data;
763}
764
765#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION) || defined(__DOXYGEN__)
766
773static inline void zbus_chan_set_msg_sub_pool(const struct zbus_channel *chan,
774 struct net_buf_pool *pool)
775{
776 __ASSERT(chan != NULL, "chan is required");
777 __ASSERT(pool != NULL, "pool is required");
778
779 chan->data->msg_subscriber_pool = pool;
780}
781
782#endif /* ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION */
783
784#if defined(CONFIG_ZBUS_CHANNEL_PUBLISH_STATS) || defined(__DOXYGEN__)
785
797static inline void zbus_chan_pub_stats_update(const struct zbus_channel *chan)
798{
799 __ASSERT(chan != NULL, "chan is required");
800
802 chan->data->publish_count += 1;
803}
804
814static inline k_ticks_t zbus_chan_pub_stats_last_time(const struct zbus_channel *chan)
815{
816 __ASSERT(chan != NULL, "chan is required");
817
818 return chan->data->publish_timestamp;
819}
820
830static inline uint32_t zbus_chan_pub_stats_count(const struct zbus_channel *chan)
831{
832 __ASSERT(chan != NULL, "chan is required");
833
834 return chan->data->publish_count;
835}
836
846static inline uint32_t zbus_chan_pub_stats_avg_period(const struct zbus_channel *chan)
847{
848 __ASSERT(chan != NULL, "chan is required");
849
850 /* Not yet published, period = 0ms */
851 if (chan->data->publish_count == 0) {
852 return 0;
853 }
854 /* Average period across application runtime */
855 return k_uptime_get() / chan->data->publish_count;
856}
857
858#else
859
860static inline void zbus_chan_pub_stats_update(const struct zbus_channel *chan)
861{
862 (void)chan;
863}
864
865#endif /* CONFIG_ZBUS_CHANNEL_PUBLISH_STATS */
866
867#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS) || defined(__DOXYGEN__)
868
875 const struct zbus_observer *obs;
876#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE)
877 const struct zbus_channel *chan;
878#endif
879};
880
881#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE) || defined(__DOXYGEN__)
901int zbus_chan_add_obs_with_node(const struct zbus_channel *chan, const struct zbus_observer *obs,
902 struct zbus_observer_node *node, k_timeout_t timeout);
903#else
904static inline int zbus_chan_add_obs_with_node(const struct zbus_channel *chan,
905 const struct zbus_observer *obs,
906 struct zbus_observer_node *node, k_timeout_t timeout)
907{
908 ARG_UNUSED(chan);
909 ARG_UNUSED(obs);
910 ARG_UNUSED(node);
911 ARG_UNUSED(timeout);
912
913 return -ENOTSUP;
914}
915#endif /* CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE */
916
917#if !defined(CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE) || defined(__DOXYGEN__)
938int zbus_chan_add_obs(const struct zbus_channel *chan, const struct zbus_observer *obs,
939 k_timeout_t timeout);
940#else
941static inline int zbus_chan_add_obs(const struct zbus_channel *chan,
942 const struct zbus_observer *obs, k_timeout_t timeout)
943{
944 ARG_UNUSED(chan);
945 ARG_UNUSED(obs);
946 ARG_UNUSED(timeout);
947
948 return -ENOTSUP;
949}
950
951#endif /* !CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE */
967int zbus_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_observer *obs,
968 k_timeout_t timeout);
969
970#endif /* CONFIG_ZBUS_RUNTIME_OBSERVERS */
971
985int zbus_obs_set_enable(const struct zbus_observer *obs, bool enabled);
986
997static inline int zbus_obs_is_enabled(const struct zbus_observer *obs, bool *enable)
998{
999 _ZBUS_ASSERT(obs != NULL, "obs is required");
1000 _ZBUS_ASSERT(enable != NULL, "enable is required");
1001
1002 *enable = obs->data->enabled;
1003
1004 return 0;
1005}
1006
1022 const struct zbus_channel *chan, bool masked);
1023
1037 const struct zbus_channel *chan, bool *masked);
1038
1039#if defined(CONFIG_ZBUS_OBSERVER_NAME) || defined(__DOXYGEN__)
1040
1050static inline const char *zbus_obs_name(const struct zbus_observer *obs)
1051{
1052 __ASSERT(obs != NULL, "obs is required");
1053
1054 return obs->name;
1055}
1056
1057#endif
1058
1059#if defined(CONFIG_ZBUS_PRIORITY_BOOST) || defined(__DOXYGEN__)
1060
1071
1082
1083#endif /* CONFIG_ZBUS_PRIORITY_BOOST */
1084
1103int zbus_sub_wait(const struct zbus_observer *sub, const struct zbus_channel **chan,
1104 k_timeout_t timeout);
1105
1106#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER) || defined(__DOXYGEN__)
1107
1126int zbus_sub_wait_msg(const struct zbus_observer *sub, const struct zbus_channel **chan, void *msg,
1127 k_timeout_t timeout);
1128
1129#endif /* CONFIG_ZBUS_MSG_SUBSCRIBER */
1130
1144bool zbus_iterate_over_channels(bool (*iterator_func)(const struct zbus_channel *chan));
1160 bool (*iterator_func)(const struct zbus_channel *chan, void *user_data), void *user_data);
1161
1175bool zbus_iterate_over_observers(bool (*iterator_func)(const struct zbus_observer *obs));
1191 bool (*iterator_func)(const struct zbus_observer *obs, void *user_data), void *user_data);
1192
1197#ifdef __cplusplus
1198}
1199#endif
1200
1201#endif /* ZEPHYR_INCLUDE_ZBUS_H_ */
int64_t k_uptime_ticks(void)
Get system uptime, in system ticks.
uint32_t k_ticks_t
Tick precision used in timeout APIs.
Definition sys_clock.h:48
static int64_t k_uptime_get(void)
Get system uptime.
Definition kernel.h:1883
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
#define ENOTSUP
Unsupported value.
Definition errno.h:114
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:670
static uint32_t zbus_chan_pub_stats_avg_period(const struct zbus_channel *chan)
Get the average period between publishes to a channel.
Definition zbus.h:846
bool zbus_iterate_over_observers_with_user_data(bool(*iterator_func)(const struct zbus_observer *obs, void *user_data), void *user_data)
Iterate over observers with user data.
bool zbus_iterate_over_observers(bool(*iterator_func)(const struct zbus_observer *obs))
Iterate over observers.
static int zbus_obs_is_enabled(const struct zbus_observer *obs, bool *enable)
Get the observer state.
Definition zbus.h:997
static void zbus_chan_set_msg_sub_pool(const struct zbus_channel *chan, struct net_buf_pool *pool)
Set the channel's msg subscriber net_buf pool.
Definition zbus.h:773
int zbus_obs_is_chan_notification_masked(const struct zbus_observer *obs, const struct zbus_channel *chan, bool *masked)
Get the notifications masking state from a channel to an observer.
int zbus_obs_detach_from_thread(const struct zbus_observer *obs)
Clear the observer thread priority by detaching it from a thread.
static uint32_t zbus_chan_pub_stats_count(const struct zbus_channel *chan)
Get the number of times a channel has been published to.
Definition zbus.h:830
static const char * zbus_obs_name(const struct zbus_observer *obs)
Get the observer's name.
Definition zbus.h:1050
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_add_obs_with_node(const struct zbus_channel *chan, const struct zbus_observer *obs, struct zbus_observer_node *node, k_timeout_t timeout)
Add an observer to a channel.
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.
zbus_observer_type
Type used to represent an observer type.
Definition zbus.h:119
static uint16_t zbus_chan_msg_size(const struct zbus_channel *chan)
Get the channel's message size.
Definition zbus.h:742
int zbus_obs_set_chan_notification_mask(const struct zbus_observer *obs, const struct zbus_channel *chan, bool masked)
Mask notifications from a channel to an observer.
int zbus_obs_set_enable(const struct zbus_observer *obs, bool enabled)
Change the observer state.
static void * zbus_chan_msg(const struct zbus_channel *chan)
Get the reference for a channel message directly.
Definition zbus.h:705
bool zbus_iterate_over_channels_with_user_data(bool(*iterator_func)(const struct zbus_channel *chan, void *user_data), void *user_data)
Iterate over channels with user data.
int zbus_obs_attach_to_thread(const struct zbus_observer *obs)
Set the observer thread priority by attaching it to a thread.
static void * zbus_chan_user_data(const struct zbus_channel *chan)
Get the channel's user data.
Definition zbus.h:758
static k_ticks_t zbus_chan_pub_stats_last_time(const struct zbus_channel *chan)
Get the time a channel was last published to.
Definition zbus.h:814
const struct zbus_channel * zbus_chan_from_id(uint32_t channel_id)
Retrieve a zbus channel from its numeric identifier.
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 void zbus_chan_pub_stats_update(const struct zbus_channel *chan)
Update the publishing statistics for a channel.
Definition zbus.h:797
int zbus_sub_wait_msg(const struct zbus_observer *sub, const struct zbus_channel **chan, void *msg, k_timeout_t timeout)
Wait for a channel message.
static const void * zbus_chan_const_msg(const struct zbus_channel *chan)
Get a constant reference for a channel message directly.
Definition zbus.h:726
@ ZBUS_OBSERVER_LISTENER_TYPE
Definition zbus.h:120
@ ZBUS_OBSERVER_SUBSCRIBER_TYPE
Definition zbus.h:121
@ ZBUS_OBSERVER_MSG_SUBSCRIBER_TYPE
Definition zbus.h:122
#define NULL
Definition iar_missing_defs.h:20
Public kernel APIs.
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT16_TYPE__ int16_t
Definition stdint.h:73
Definition kernel.h:2540
Message Queue Structure.
Definition kernel.h:4640
Semaphore structure.
Definition kernel.h:3275
Kernel timeout type.
Definition sys_clock.h:65
Network buffer pool representation.
Definition net_buf.h:1078
Type used to represent a channel mutable data.
Definition zbus.h:32
k_ticks_t publish_timestamp
Kernel timestamp of the last publish action on this channel.
Definition zbus.h:70
struct net_buf_pool * msg_subscriber_pool
Net buf pool for message subscribers.
Definition zbus.h:65
uint32_t publish_count
Number of times data has been published to this channel.
Definition zbus.h:72
int16_t observers_end_idx
Static channel observer list end index.
Definition zbus.h:41
int16_t observers_start_idx
Static channel observer list start index.
Definition zbus.h:36
struct k_sem sem
Access control semaphore.
Definition zbus.h:46
sys_slist_t observers
Channel observer list.
Definition zbus.h:59
Type used to represent a channel.
Definition zbus.h:82
void * user_data
User data available to extend zbus features.
Definition zbus.h:102
uint32_t id
Unique numeric channel identifier.
Definition zbus.h:89
struct zbus_channel_data * data
Mutable channel data struct.
Definition zbus.h:111
bool(* validator)(const void *msg, size_t msg_size)
Message validator.
Definition zbus.h:108
size_t message_size
Message size.
Definition zbus.h:97
const char * name
Channel name.
Definition zbus.h:85
void * message
Message reference.
Definition zbus.h:94
Definition zbus.h:125
bool enabled
Enabled flag.
Definition zbus.h:127
Structure used to register runtime obeservers.
Definition zbus.h:873
const struct zbus_observer * obs
Definition zbus.h:875
sys_snode_t node
Definition zbus.h:874
Type used to represent an observer.
Definition zbus.h:150
enum zbus_observer_type type
Type indication.
Definition zbus.h:156
struct k_fifo * message_fifo
Observer message FIFO.
Definition zbus.h:172
struct zbus_observer_data * data
Mutable observer data struct.
Definition zbus.h:159
struct k_msgq * queue
Observer message queue.
Definition zbus.h:163
const char * name
Observer name.
Definition zbus.h:153