Skip to content

Commit a9626ff

Browse files
JordanYatesjhedberg
authored andcommitted
zbus: helper to query channel data age
Add a helper function to query how old the data in a channel is. Output is converted to milliseconds as `k_ticks` objects cannot be compared directly. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent dfe2c26 commit a9626ff

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

include/zephyr/zbus/zbus.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,22 @@ static inline uint32_t zbus_chan_pub_stats_avg_period(const struct zbus_channel
855855
return k_uptime_get() / chan->data->publish_count;
856856
}
857857

858+
/**
859+
* @brief Get the age of a message in a channel
860+
*
861+
* @param chan The channel's reference.
862+
*
863+
* @retval UINT64_MAX if channel has never been published to
864+
* @retval age_ms Message age in milliseconds otherwise
865+
*/
866+
static inline uint64_t zbus_chan_pub_stats_msg_age(const struct zbus_channel *chan)
867+
{
868+
if (zbus_chan_pub_stats_count(chan) == 0) {
869+
return UINT64_MAX;
870+
}
871+
return k_ticks_to_ms_floor64(k_uptime_ticks() - zbus_chan_pub_stats_last_time(chan));
872+
}
873+
858874
#else
859875

860876
static inline void zbus_chan_pub_stats_update(const struct zbus_channel *chan)

0 commit comments

Comments
 (0)