Skip to content

Commit 54d638b

Browse files
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 function performs a linear search through all channels using STRUCT_SECTION_FOREACH and compares channel names using strcmp(). It returns NULL if no matching channel is found, maintaining consistency with the existing zbus_chan_from_id() API. 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 <[email protected]>
1 parent 0c9a8c6 commit 54d638b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

include/zephyr/zbus/zbus.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,20 @@ const struct zbus_channel *zbus_chan_from_id(uint32_t channel_id);
690690

691691
#endif
692692

693+
#if defined(CONFIG_ZBUS_CHANNEL_NAME) || defined(__DOXYGEN__)
694+
695+
/**
696+
* @brief Retrieve a zbus channel from its name string
697+
*
698+
* @param name Name of the channel to retrieve.
699+
*
700+
* @retval NULL If channel with name @a name does not exist.
701+
* @retval chan Channel pointer with name @a name otherwise.
702+
*/
703+
const struct zbus_channel *zbus_chan_from_name(const char *name);
704+
705+
#endif
706+
693707
/**
694708
* @brief Get the reference for a channel message directly.
695709
*

subsys/zbus/zbus.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ const struct zbus_channel *zbus_chan_from_id(uint32_t channel_id)
122122

123123
#endif /* CONFIG_ZBUS_CHANNEL_ID */
124124

125+
#if defined(CONFIG_ZBUS_CHANNEL_NAME)
126+
127+
const struct zbus_channel *zbus_chan_from_name(const char *name)
128+
{
129+
if (!name) {
130+
return NULL;
131+
}
132+
133+
STRUCT_SECTION_FOREACH(zbus_channel, chan) {
134+
if (strcmp(chan->name, name) == 0) {
135+
/* Found matching channel */
136+
return chan;
137+
}
138+
}
139+
/* No matching channel exists */
140+
return NULL;
141+
}
142+
143+
#endif /* CONFIG_ZBUS_CHANNEL_NAME */
144+
125145
static inline int _zbus_notify_observer(const struct zbus_channel *chan,
126146
const struct zbus_observer *obs, k_timepoint_t end_time,
127147
struct net_buf *buf)

0 commit comments

Comments
 (0)