From e0173d9b8e4ea4e9f46f051031c0348d87c72608 Mon Sep 17 00:00:00 2001 From: Brandon Allen Date: Fri, 17 Jan 2025 14:21:53 -0500 Subject: [PATCH] zbus: assert when inside an ISR and time out is not zero Currently various zbus functions silently change the timeout to zero when inside an ISR. If a developer is not aware of this it could lead to unexpected behaviour or a publish/read failing. Also in the zbus docs it states to only use a timeout of zero when inside a ISR multiple times. Signed-off-by: Brandon Allen --- subsys/zbus/zbus.c | 8 ++++++++ tests/subsys/zbus/unittests/src/main.c | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/subsys/zbus/zbus.c b/subsys/zbus/zbus.c index e4defffbc57..0fff534c248 100644 --- a/subsys/zbus/zbus.c +++ b/subsys/zbus/zbus.c @@ -380,6 +380,8 @@ int zbus_chan_pub(const struct zbus_channel *chan, const void *msg, k_timeout_t _ZBUS_ASSERT(chan != NULL, "chan is required"); _ZBUS_ASSERT(msg != NULL, "msg is required"); + _ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true, + "inside an ISR, the timeout must be K_NO_WAIT"); if (k_is_in_isr()) { timeout = K_NO_WAIT; @@ -416,6 +418,8 @@ int zbus_chan_read(const struct zbus_channel *chan, void *msg, k_timeout_t timeo { _ZBUS_ASSERT(chan != NULL, "chan is required"); _ZBUS_ASSERT(msg != NULL, "msg is required"); + _ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true, + "inside an ISR, the timeout must be K_NO_WAIT"); if (k_is_in_isr()) { timeout = K_NO_WAIT; @@ -438,6 +442,8 @@ int zbus_chan_notify(const struct zbus_channel *chan, k_timeout_t timeout) int err; _ZBUS_ASSERT(chan != NULL, "chan is required"); + _ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true, + "inside an ISR, the timeout must be K_NO_WAIT"); if (k_is_in_isr()) { timeout = K_NO_WAIT; @@ -462,6 +468,8 @@ int zbus_chan_notify(const struct zbus_channel *chan, k_timeout_t timeout) int zbus_chan_claim(const struct zbus_channel *chan, k_timeout_t timeout) { _ZBUS_ASSERT(chan != NULL, "chan is required"); + _ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true, + "inside an ISR, the timeout must be K_NO_WAIT"); if (k_is_in_isr()) { timeout = K_NO_WAIT; diff --git a/tests/subsys/zbus/unittests/src/main.c b/tests/subsys/zbus/unittests/src/main.c index 69659d6b825..c94d0c28b0e 100644 --- a/tests/subsys/zbus/unittests/src/main.c +++ b/tests/subsys/zbus/unittests/src/main.c @@ -336,14 +336,14 @@ ZTEST(basic, test_specification_based__zbus_chan) /* Trying to call the zbus functions in a ISR context. None must work */ ISR_OP(PUB_ISR, 0); - ISR_OP(PUB_ISR_INVAL, 0); + ISR_OP(PUB_ISR_INVAL, -EFAULT); ISR_OP(READ_ISR, 0); - ISR_OP(READ_ISR_INVAL, 0); + ISR_OP(READ_ISR_INVAL, -EFAULT); ISR_OP(NOTIFY_ISR, 0); - ISR_OP(NOTIFY_ISR_INVAL, 0); + ISR_OP(NOTIFY_ISR_INVAL, -EFAULT); ISR_OP(CLAIM_ISR, 0); ISR_OP(FINISH_ISR, 0); - ISR_OP(CLAIM_ISR_INVAL, 0); + ISR_OP(CLAIM_ISR_INVAL, -EFAULT); ISR_OP(FINISH_ISR, 0); ISR_OP(FINISH_ISR_INVAL, -EFAULT); ISR_OP(ADD_OBS_ISR, -EFAULT);