diff --git a/sys/event/timeout.c b/sys/event/timeout.c index c26e63f0919049328c0faf162539239a669fb581..e7d4d0a4d3347885c33732444c289a808cb2563f 100644 --- a/sys/event/timeout.c +++ b/sys/event/timeout.c @@ -26,3 +26,8 @@ void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout) { xtimer_set(&event_timeout->timer, timeout); } + +void event_timeout_clear(event_timeout_t *event_timeout) +{ + xtimer_remove(&event_timeout->timer); +} diff --git a/sys/include/event/timeout.h b/sys/include/event/timeout.h index 0d13e65ea789eba7ed54395e88e2937f925a69e6..cbd65a18f334fa48cecda18569c4c7814c8aa4bb 100644 --- a/sys/include/event/timeout.h +++ b/sys/include/event/timeout.h @@ -54,11 +54,12 @@ typedef struct { /** * @brief Initialize timeout event object * - * @param[in] event_timeout event_timeout object to initilalize + * @param[in] event_timeout event_timeout object to initialize * @param[in] queue queue that the timed-out event will be added to * @param[in] event event to add to queue after timeout */ -void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event); +void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, + event_t *event); /** * @brief Set a timeout @@ -69,11 +70,23 @@ void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, ev * @note: the used event_timeout struct must stay valid until after the timeout * event has been processed! * - * @param[in] event_timeout event_timout context onject to use + * @param[in] event_timeout event_timout context object to use * @param[in] timeout timeout in miliseconds */ void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout); +/** + * @brief Clear a timeout event + * + * Calling this function will cancel the timeout by removing its underlying + * timer. If the timer has already fired before calling this function, the + * connected event will be put already into the given event queue and this + * function does not have any effect. + * + * @param[in] event_timeout event_timeout context object to use + */ +void event_timeout_clear(event_timeout_t *event_timeout); + #ifdef __cplusplus } #endif diff --git a/tests/events/main.c b/tests/events/main.c index d25212d81894472a122eed6e51156a56fabf8a71..6c6e94b2a02980a665282eac72048a6454999712 100644 --- a/tests/events/main.c +++ b/tests/events/main.c @@ -31,6 +31,7 @@ static uint32_t before; static void callback(event_t *arg); static void custom_callback(event_t *event); static void timed_callback(void *arg); +static void forbidden_callback(void *arg); static event_t event = { .handler = callback }; @@ -51,6 +52,7 @@ typedef struct { static custom_event_t custom_event = { .super.handler = custom_callback, .text = "CUSTOM CALLBACK" }; static event_callback_t event_callback = EVENT_CALLBACK_INIT(timed_callback, 0x12345678); +static event_callback_t noevent_callback = EVENT_CALLBACK_INIT(forbidden_callback, 0); static void custom_callback(event_t *event) { @@ -69,7 +71,18 @@ static void timed_callback(void *arg) uint32_t now = xtimer_now_usec(); assert((now - before >= 100000LU)); printf("triggered timed callback with arg 0x%08x after %" PRIu32 "us\n", (unsigned)arg, now - before); - printf("[SUCCESS]\n"); + puts("[SUCCESS]"); +} + +static void forbidden_callback(void *arg) +{ + (void)arg; + /* this callback should never be triggered! */ + puts("call to forbidden callback"); + puts("[FAILED]"); + while (1) { + assert(false); + } } int main(void) @@ -85,17 +98,25 @@ int main(void) printf("canceling 0x%08x\n", (unsigned)&event2); event_cancel(&queue, &event2); - printf("posting custom event\n"); + puts("posting custom event"); event_post(&queue, (event_t *)&custom_event); event_timeout_t event_timeout; - printf("posting timed callback with timeout 1sec\n"); + puts("posting timed callback with timeout 1sec"); event_timeout_init(&event_timeout, &queue, (event_t *)&event_callback); before = xtimer_now_usec(); - event_timeout_set(&event_timeout, 100000LU); + event_timeout_set(&event_timeout, (1 * US_PER_SEC)); + + event_timeout_t event_timeout_canceled; + + puts("posting timed callback with timeout 0.5sec and canceling it again"); + event_timeout_init(&event_timeout_canceled, &queue, + (event_t *)&noevent_callback); + event_timeout_set(&event_timeout_canceled, 500 * US_PER_MS); + event_timeout_clear(&event_timeout_canceled); - printf("launching event queue\n"); + puts("launching event queue"); event_loop(&queue); return 0;