Skip to content

Commit

Permalink
Merge pull request #20847 from benpicco/event_assert
Browse files Browse the repository at this point in the history
sys/event: add assertion that event has a handler
  • Loading branch information
Teufelchen1 authored Oct 25, 2024
2 parents ba83fef + 16c447a commit b376bec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
12 changes: 6 additions & 6 deletions examples/twr_aloha/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions sys/event/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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) {
Expand Down
20 changes: 20 additions & 0 deletions sys/include/event/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#ifndef EVENT_CALLBACK_H
#define EVENT_CALLBACK_H

#include <assert.h>
#include "event.h"

#ifdef __cplusplus
Expand All @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions sys/include/event/periodic_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef EVENT_PERIODIC_CALLBACK_H
#define EVENT_PERIODIC_CALLBACK_H

#include <assert.h>
#include "event/callback.h"
#include "event/periodic.h"

Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,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;
}
}
Expand Down

0 comments on commit b376bec

Please sign in to comment.