11ba5c40c5a3257fe3839ff24a24e099638b3add
[bertos.git] / mware / event.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2001, 2003 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \brief Events handling
10  *
11  * This module implements a common system for executing
12  * a user defined action calling a hook function.
13  *
14  * \version $Id$
15  *
16  * \author Bernardo Innocenti <bernie@develer.com>
17  */
18
19 /*#*
20  *#* $Log$
21  *#* Revision 1.8  2006/07/19 12:56:27  bernie
22  *#* Convert to new Doxygen style.
23  *#*
24  *#* Revision 1.7  2006/03/20 17:52:22  bernie
25  *#* Add missing forward declaration.
26  *#*
27  *#* Revision 1.6  2006/02/24 01:17:44  bernie
28  *#* Update for new emulator.
29  *#*
30  *#* Revision 1.5  2006/02/24 00:26:21  bernie
31  *#* Fix header name.
32  *#*
33  *#* Revision 1.4  2006/02/10 12:24:42  bernie
34  *#* Fix standalone build.
35  *#*
36  *#* Revision 1.3  2006/01/16 03:27:49  bernie
37  *#* Rename sig_t to sigbit_t to avoid clash with POSIX.
38  *#*
39  *#* Revision 1.2  2005/11/27 03:02:55  bernie
40  *#* Convert to appconfig.h.
41  *#*
42  *#* Revision 1.1  2005/11/27 01:39:48  bernie
43  *#* Move event.[ch] from kern/ to mware/.
44  *#*
45  *#* Revision 1.11  2005/04/11 19:10:28  bernie
46  *#* Include top-level headers from cfg/ subdir.
47  *#*
48  *#* Revision 1.10  2005/01/24 04:22:02  bernie
49  *#* Update copyright information.
50  *#*
51  *#* Revision 1.9  2005/01/24 04:19:55  bernie
52  *#* Remove obsolete names.
53  *#*
54  *#* Revision 1.8  2005/01/24 04:19:05  bernie
55  *#* Function pointer based event dispatching.
56  *#*
57  *#* Revision 1.7  2004/08/25 14:12:09  rasky
58  *#* Aggiornato il comment block dei log RCS
59  *#*
60  *#* Revision 1.6  2004/08/14 19:37:57  rasky
61  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
62  *#*
63  *#* Revision 1.5  2004/07/30 14:30:27  rasky
64  *#* Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
65  *#* Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
66  *#*
67  *#* Revision 1.4  2004/06/07 15:58:00  aleph
68  *#* Add function prototypes
69  *#*
70  *#* Revision 1.3  2004/06/06 18:25:44  bernie
71  *#* Rename event macros to look like regular functions.
72  *#*
73  *#* Revision 1.2  2004/06/03 11:27:09  bernie
74  *#* Add dual-license information.
75  *#*
76  *#* Revision 1.1  2004/05/23 17:27:00  bernie
77  *#* Import kern/ subdirectory.
78  *#*
79  *#*/
80 #ifndef KERN_EVENT_H
81 #define KERN_EVENT_H
82
83 #include <cfg/compiler.h>
84 #include <appconfig.h>
85
86 #if CONFIG_KERNEL
87         #include <config_kern.h>
88         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
89                 #include <kern/signal.h>
90         #endif
91
92         /* Forward decl */
93         struct Process;
94 #endif
95
96
97 /// User defined callback type
98 typedef void (*Hook)(void *);
99
100 typedef struct Event
101 {
102         void (*action)(struct Event *);
103         union
104         {
105 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
106                 struct
107                 {
108                         struct Process *sig_proc;  /* Process to be signalled */
109                         sigbit_t        sig_bit;   /* Signal to send */
110                 } Sig;
111 #endif
112                 struct
113                 {
114                         Hook  func;         /* Pointer to softint hook */
115                         void *user_data;    /* Data to be passed back to user hook */
116                 } Int;
117         } Ev;
118 } Event;
119
120 void event_hook_ignore(Event *event);
121 void event_hook_signal(Event *event);
122 void event_hook_softint(Event *event);
123
124 /** Initialize the event \a e as a no-op */
125 #define event_initNone(e) \
126         ((e)->action = event_hook_ignore)
127
128 /** Same as event_initNone(), but returns the initialized event */
129 INLINE Event event_createNone(void);
130 INLINE Event event_createNone(void)
131 {
132         Event e;
133         e.action = event_hook_ignore;
134         return e;
135 }
136
137 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
138 #define event_initSoftInt(e,f,u) \
139         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
140
141 /** Same as event_initSoftInt(), but returns the initialized event */
142 INLINE Event event_createSoftInt(Hook func, void *user_data)
143 {
144         Event e;
145         e.action = event_hook_softint;
146         e.Ev.Int.func = func;
147         e.Ev.Int.user_data = user_data;
148         return e;
149 }
150
151
152 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
153
154 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
155 #define event_initSignal(e,p,s) \
156         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
157
158 /** Same as event_initSignal(), but returns the initialized event */
159 INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
160 {
161         Event e;
162         e.action = event_hook_signal;
163         e.Ev.Sig.sig_proc = proc;
164         e.Ev.Sig.sig_bit = bit;
165         return e;
166 }
167
168 #endif
169
170 /** Trigger an event */
171 INLINE void event_do(struct Event *e)
172 {
173         e->action(e);
174 }
175
176 #endif /* KERN_EVENT_H */