summaryrefslogtreecommitdiff
path: root/firmware/events.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/events.c')
-rw-r--r--firmware/events.c61
1 files changed, 49 insertions, 12 deletions
diff --git a/firmware/events.c b/firmware/events.c
index 74172e1fa0..4a51e7ae87 100644
--- a/firmware/events.c
+++ b/firmware/events.c
@@ -28,30 +28,41 @@
28struct sysevent { 28struct sysevent {
29 unsigned short id; 29 unsigned short id;
30 bool oneshot; 30 bool oneshot;
31 void (*callback)(void *data); 31 bool has_user_data;
32 union {
33 void (*callback)(unsigned short id, void *event_data);
34 struct {
35 void (*callback2)(unsigned short id, void *event_data, void *user_data);
36 void *user_data;
37 };
38 } handler;
32}; 39};
33 40
34static struct sysevent events[MAX_SYS_EVENTS]; 41static struct sysevent events[MAX_SYS_EVENTS];
35 42
36bool add_event(unsigned short id, bool oneshot, void (*handler)(void *data)) 43static bool do_add_event(unsigned short id, bool oneshot, bool user_data_valid,
44 void *handler, void *user_data)
37{ 45{
38 int i; 46 int i;
39 47
40 /* Check if the event already exists. */ 48 /* Check if the event already exists. */
41 for (i = 0; i < MAX_SYS_EVENTS; i++) 49 for (i = 0; i < MAX_SYS_EVENTS; i++)
42 { 50 {
43 if (events[i].callback == handler && events[i].id == id) 51 if (events[i].handler.callback == handler && events[i].id == id
52 && (!user_data_valid || (user_data == events[i].handler.callback)))
44 return false; 53 return false;
45 } 54 }
46 55
47 /* Try to find a free slot. */ 56 /* Try to find a free slot. */
48 for (i = 0; i < MAX_SYS_EVENTS; i++) 57 for (i = 0; i < MAX_SYS_EVENTS; i++)
49 { 58 {
50 if (events[i].callback == NULL) 59 if (events[i].handler.callback == NULL)
51 { 60 {
52 events[i].id = id; 61 events[i].id = id;
53 events[i].oneshot = oneshot; 62 events[i].oneshot = oneshot;
54 events[i].callback = handler; 63 if ((events[i].has_user_data = user_data_valid))
64 events[i].handler.user_data = user_data;
65 events[i].handler.callback = handler;
55 return true; 66 return true;
56 } 67 }
57 } 68 }
@@ -60,33 +71,59 @@ bool add_event(unsigned short id, bool oneshot, void (*handler)(void *data))
60 return false; 71 return false;
61} 72}
62 73
63void remove_event(unsigned short id, void (*handler)(void *data)) 74bool add_event(unsigned short id, void (*handler)(unsigned short id, void *data))
75{
76 return do_add_event(id, false, false, handler, NULL);
77}
78
79bool add_event_ex(unsigned short id, bool oneshot, void (*handler)(unsigned short id, void *event_data, void *user_data), void *user_data)
80{
81 return do_add_event(id, oneshot, true, handler, user_data);
82}
83
84void do_remove_event(unsigned short id, bool user_data_valid,
85 void *handler, void *user_data)
64{ 86{
65 int i; 87 int i;
66 88
67 for (i = 0; i < MAX_SYS_EVENTS; i++) 89 for (i = 0; i < MAX_SYS_EVENTS; i++)
68 { 90 {
69 if (events[i].id == id && events[i].callback == handler) 91 if (events[i].id == id && events[i].handler.callback == handler
92 && (!user_data_valid || (user_data == events[i].handler.callback)))
70 { 93 {
71 events[i].callback = NULL; 94 events[i].handler.callback = NULL;
72 return; 95 return;
73 } 96 }
74 } 97 }
75} 98}
76 99
100void remove_event(unsigned short id, void (*handler)(unsigned short id, void *data))
101{
102 do_remove_event(id, false, handler, NULL);
103}
104
105void remove_event_ex(unsigned short id,
106 void (*handler)(unsigned short id, void *event_data, void *user_data),
107 void *user_data)
108{
109 do_remove_event(id, true, handler, user_data);
110}
111
77void send_event(unsigned short id, void *data) 112void send_event(unsigned short id, void *data)
78{ 113{
79 int i; 114 int i;
80 115
81 for (i = 0; i < MAX_SYS_EVENTS; i++) 116 for (i = 0; i < MAX_SYS_EVENTS; i++)
82 { 117 {
83 if (events[i].id == id && events[i].callback != NULL) 118 if (events[i].id == id && events[i].handler.callback != NULL)
84 { 119 {
85 events[i].callback(data); 120 if (events[i].has_user_data)
121 events[i].handler.callback2(id, data, events[i].handler.user_data);
122 else
123 events[i].handler.callback(id, data);
86 124
87 if (events[i].oneshot) 125 if (events[i].oneshot)
88 events[i].callback = NULL; 126 events[i].handler.callback = NULL;
89 } 127 }
90 } 128 }
91} 129}
92