From 18e6eaca00e66ca0610ce487c09772fb744716d6 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 29 Aug 2024 17:19:13 +0200 Subject: [PATCH 1/5] sys/event: add assertion that event has a handler --- sys/event/event.c | 1 + 1 file changed, 1 insertion(+) diff --git a/sys/event/event.c b/sys/event/event.c index c9a615de51aa..27ef8430fd2d 100644 --- a/sys/event/event.c +++ b/sys/event/event.c @@ -36,6 +36,7 @@ void event_post(event_queue_t *queue, event_t *event) { assert(queue && event); + assert(event->handler); unsigned state = irq_disable(); if (!event->list_node.next) { From 10f28b873e8f77f6c86333a0aeeed6d66784e7f3 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 29 Aug 2024 17:19:43 +0200 Subject: [PATCH 2/5] sys/event_callback: add event_callback_post() --- sys/include/event/callback.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sys/include/event/callback.h b/sys/include/event/callback.h index eb46a815308a..ffed28db3b3a 100644 --- a/sys/include/event/callback.h +++ b/sys/include/event/callback.h @@ -36,6 +36,7 @@ #ifndef EVENT_CALLBACK_H #define EVENT_CALLBACK_H +#include #include "event.h" #ifdef __cplusplus @@ -60,6 +61,25 @@ typedef struct { */ void event_callback_init(event_callback_t *event_callback, void (*callback)(void *), void *arg); +/** + * @brief Queue an event + * + * The given event will be posted on the given @p queue. If the event is already + * queued when calling this function, the event will not be touched and remain + * in the previous position on the queue. So reposting an event while it is + * already on the queue will have no effect. + * + * @pre queue should be initialized + * + * @param[in] queue event queue to queue event in + * @param[in] event event to queue in event queue + */ +static inline void event_callback_post(event_queue_t *queue, event_callback_t *event) +{ + assert(event->callback); + event_post(queue, &event->super); +} + /** * @brief Generate a one-shot callback event on @p queue * From 61773807621dae2cc6524756aadb8328b96fbe40 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 29 Aug 2024 17:20:16 +0200 Subject: [PATCH 3/5] event_periodic_callback: add assertion that event has a handler --- sys/include/event/periodic_callback.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sys/include/event/periodic_callback.h b/sys/include/event/periodic_callback.h index 26ff293f9f74..81a48aba7a79 100644 --- a/sys/include/event/periodic_callback.h +++ b/sys/include/event/periodic_callback.h @@ -25,6 +25,7 @@ #ifndef EVENT_PERIODIC_CALLBACK_H #define EVENT_PERIODIC_CALLBACK_H +#include #include "event/callback.h" #include "event/periodic.h" @@ -94,6 +95,7 @@ static inline void event_periodic_callback_init(event_periodic_callback_t *event static inline void event_periodic_callback_start(event_periodic_callback_t *event, uint32_t interval) { + assert(event->event.callback); event_periodic_start(&event->periodic, interval); } From 48850e6331c192b31ed84b6809abeb8090437574 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 29 Aug 2024 17:23:30 +0200 Subject: [PATCH 4/5] examples/twr_aloha: make use of event_callback_post() --- examples/twr_aloha/control.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/twr_aloha/control.c b/examples/twr_aloha/control.c index 42d1332d04e1..5672f6a9060f 100644 --- a/examples/twr_aloha/control.c +++ b/examples/twr_aloha/control.c @@ -146,7 +146,7 @@ static bool _complete_cb(struct uwb_dev *inst, struct uwb_mac_interface *cbs) if (inst->fctrl != FCNTL_IEEE_RANGE_16 && inst->fctrl != (FCNTL_IEEE_RANGE_16 | UWB_FCTRL_ACK_REQUESTED)) { /* on completion of a range request setup an event to keep listening */ - event_post(uwb_core_get_eventq(), &_rng_listen_event.super); + event_callback_post(uwb_core_get_eventq(), &_rng_listen_event); return false; } @@ -163,7 +163,7 @@ static bool _complete_cb(struct uwb_dev *inst, struct uwb_mac_interface *cbs) if (IS_ACTIVE(CONFIG_TWR_PRINTF_INITIATOR_ONLY) && ((frame->code < UWB_DATA_CODE_DS_TWR && frame->src_address != _udev->uid) || (frame->code >= UWB_DATA_CODE_DS_TWR && frame->dst_address != _udev->uid))) { - event_post(uwb_core_get_eventq(), &_rng_listen_event.super); + event_callback_post(uwb_core_get_eventq(), &_rng_listen_event); return true; } @@ -183,9 +183,9 @@ static bool _complete_cb(struct uwb_dev *inst, struct uwb_mac_interface *cbs) #endif /* offload range data printing */ memcpy(&_rng_data, &data, sizeof(uwb_core_rng_data_t)); - event_post(uwb_core_get_eventq(), &_print_rng_data_event.super); + event_callback_post(uwb_core_get_eventq(), &_print_rng_data_event); /* on completion of a range request setup an event to keep listening */ - event_post(uwb_core_get_eventq(), &_rng_listen_event.super); + event_callback_post(uwb_core_get_eventq(), &_rng_listen_event); return true; } @@ -201,7 +201,7 @@ static bool _rx_timeout_cb(struct uwb_dev *inst, struct uwb_mac_interface *cbs) { (void)inst; (void)cbs; - event_post(uwb_core_get_eventq(), &_rng_listen_event.super); + event_callback_post(uwb_core_get_eventq(), &_rng_listen_event); return true; } @@ -238,7 +238,7 @@ void uwb_core_rng_listen_enable(void) { _status |= TWR_STATUS_RESPONDER; /* post event to start listening */ - event_post(uwb_core_get_eventq(), &_rng_listen_event.super); + event_callback_post(uwb_core_get_eventq(), &_rng_listen_event); } void uwb_core_rng_listen_disable(void) From 16c447afb1d1fbb540829aa43159aa235ac2cdcd Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 29 Aug 2024 17:35:54 +0200 Subject: [PATCH 5/5] gcoap: make use of event_callback_post() --- sys/net/application_layer/gcoap/gcoap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index b316371396c4..d53fc8532af1 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -1842,7 +1842,7 @@ ssize_t gcoap_req_send(const uint8_t *buf, size_t len, event_callback_init(&_receive_from_cache, _receive_from_cache_cb, memo); - event_post(&_queue, &_receive_from_cache.super); + event_callback_post(&_queue, &_receive_from_cache); return len; } }