deferred_callback.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
24 #include <assert.h>
25 #include "event/callback.h"
26 #include "ztimer.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
35 typedef struct {
40 
45 static inline void _event_deferred_post(void *arg)
46 {
47  event_deferred_callback_t *event_timeout = arg;
48 
49  event_post(event_timeout->queue, arg);
50 }
51 
65  event_queue_t *queue,
66  ztimer_clock_t *clock, uint32_t timeout,
67  void (*callback)(void *), void *arg)
68 {
69  /* cancel the event if it has already been queued */
70  ztimer_remove(clock, &event->timer);
71  event_cancel(queue, &event->event.super);
72 
73  event->event = (event_callback_t) {
74  .super.handler = _event_callback_handler,
75  .callback = callback,
76  .arg = arg,
77  };
78  event->timer = (ztimer_t) {
79  .callback = _event_deferred_post,
80  .arg = event,
81  };
82  event->queue = queue;
83 
84  ztimer_set(clock, &event->timer, timeout);
85 }
86 
94  ztimer_clock_t *clock)
95 {
96  ztimer_remove(clock, &event->timer);
97  event_cancel(event->queue, (event_t *)event);
98 }
99 
100 #ifdef __cplusplus
101 }
102 #endif
POSIX.1-2008 compliant version of the assert macro.
Provides a callback-with-argument event type.
void _event_callback_handler(event_t *event)
event callback handler function (used internally)
static void _event_deferred_post(void *arg)
Internal helper function for ztimer callback.
static void event_deferred_callback_cancel(event_deferred_callback_t *event, ztimer_clock_t *clock)
Cancel a callback function if it has not been executed yet.
static void event_deferred_callback_post(event_deferred_callback_t *event, event_queue_t *queue, ztimer_clock_t *clock, uint32_t timeout, void(*callback)(void *), void *arg)
Execute a callback function in the event thread after a timeout.
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
void event_cancel(event_queue_t *queue, event_t *event)
Cancel a queued event.
uint32_t ztimer_set(ztimer_clock_t *clock, ztimer_t *timer, uint32_t val)
Set a timer on a clock.
bool ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer)
Remove a timer from a clock.
event queue structure
Definition: event.h:150
Callback Event structure definition.
Definition: callback.h:45
Deferred Callback Event structure.
ztimer_t timer
ztimer object used for timeout
event_callback_t event
callback event portion
event_queue_t * queue
event queue to post event to
event structure
Definition: event.h:142
ztimer device structure
Definition: ztimer.h:367
ztimer structure
Definition: ztimer.h:316
ztimer API