From 46eca1e3a9373e07a933ae2dc87fbeb03d204f58 Mon Sep 17 00:00:00 2001 From: "Trond F. Christiansen" Date: Tue, 19 Aug 2025 10:48:14 +0200 Subject: [PATCH] zbus: add zbus_chan_from_name() function Add a new API function zbus_chan_from_name() that allows retrieving a zbus channel by its name string. This complements the existing zbus_chan_from_id() function and provides more flexibility for channel lookup operations. The implementation is conditionally compiled when CONFIG_ZBUS_CHANNEL_NAME is enabled, ensuring it's only available when channel names are configured in the system. Signed-off-by: Trond F. Christiansen --- include/zephyr/zbus/zbus.h | 14 ++++++++++++++ subsys/zbus/zbus.c | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/zephyr/zbus/zbus.h b/include/zephyr/zbus/zbus.h index 13368b4841a..f7ac22d1682 100644 --- a/include/zephyr/zbus/zbus.h +++ b/include/zephyr/zbus/zbus.h @@ -807,6 +807,20 @@ const struct zbus_channel *zbus_chan_from_id(uint32_t channel_id); #endif +#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__) + +/** + * @brief Retrieve a zbus channel from its name string + * + * @param name Name of the channel to retrieve. + * + * @retval NULL If channel with name @a name does not exist. + * @retval chan Channel pointer with name @a name otherwise. + */ +const struct zbus_channel *zbus_chan_from_name(const char *name); + +#endif + /** * @brief Get the reference for a channel message directly. * diff --git a/subsys/zbus/zbus.c b/subsys/zbus/zbus.c index 448d7502491..16d5e387542 100644 --- a/subsys/zbus/zbus.c +++ b/subsys/zbus/zbus.c @@ -156,6 +156,26 @@ const struct zbus_channel *zbus_chan_from_id(uint32_t channel_id) #endif /* CONFIG_ZBUS_CHANNEL_ID */ +#if defined(CONFIG_ZBUS_CHANNEL_NAME) + +const struct zbus_channel *zbus_chan_from_name(const char *name) +{ + CHECKIF(name == NULL) { + return NULL; + } + + STRUCT_SECTION_FOREACH(zbus_channel, chan) { + if (strcmp(chan->name, name) == 0) { + /* Found matching channel */ + return chan; + } + } + /* No matching channel exists */ + return NULL; +} + +#endif /* CONFIG_ZBUS_CHANNEL_NAME */ + static inline int _zbus_notify_observer(const struct zbus_channel *chan, const struct zbus_observer *obs, k_timepoint_t end_time, struct net_buf *buf)