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 <trond.christiansen@nordicsemi.no>
This commit is contained in:
Trond F. Christiansen
2025-08-19 10:48:14 +02:00
committed by Anas Nashif
parent 5c152e19fa
commit 46eca1e3a9
2 changed files with 34 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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)