utils
utilities in C for microcontrollers
event.h
Go to the documentation of this file.
1 
7 #ifndef EVENT_H_
8 #define EVENT_H_
9 
10 #include "list.h"
11 
15 struct event {
17  struct list handlers;
18 };
19 
23 struct event_handler {
30  void (*fun)(struct event *evt, void *ctx);
33  struct event *evt;
35  unsigned char flags;
36 };
37 
45 #define EVENT_HANDLER_INIT(handler_fun) { \
46  .element = (struct list_element){ NULL, NULL }, \
47  .fun = handler_fun, \
48  .evt = NULL, \
49  .flags = 0 \
50 }
51 
57 int event_handler_init(struct event_handler *handler, void (*fun)(struct event *, void *));
58 
64 int event_init(struct event *evt);
65 
73 #define EVENT_INIT(event) { .handlers = LIST_INIT((event).handlers) }
74 
84 int event_subscribe(struct event *evt, struct event_handler *handler);
85 
94 int event_unsubscribe(struct event *evt, struct event_handler *handler);
95 
104 int event_publish(struct event *evt, void *ctx);
105 
106 #endif /* EVENT_H_ */
struct event * evt
pointer to keep track of which event to subscribe to once handler finishes
Definition: event.h:33
event handler state
Definition: event.h:23
event state
Definition: event.h:15
int event_handler_init(struct event_handler *handler, void(*fun)(struct event *, void *))
initialize an event handler
Definition: event.c:7
int event_unsubscribe(struct event *evt, struct event_handler *handler)
detach an event_handler from an event
Definition: event.c:36
struct list handlers
a list of event handlers that are subscribed to this event
Definition: event.h:17
An intrusive circular doubly linked list.
int event_init(struct event *evt)
initialize a event
Definition: event.c:16
int event_subscribe(struct event *evt, struct event_handler *handler)
attach an event_handler to an event
Definition: event.c:22
void(* fun)(struct event *evt, void *ctx)
handler function pointer
Definition: event.h:30
list element state
Definition: list.h:15
int event_publish(struct event *evt, void *ctx)
publish an event
Definition: event.c:61
struct list_element element
list element for attaching handler to an event
Definition: event.h:25
list state
Definition: list.h:63
unsigned char flags
flag used to indicate if the handler should be removed
Definition: event.h:35