Avoid strlen().
[bertos.git] / kern / 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.10  2005/01/24 04:22:02  bernie
22  *#* Update copyright information.
23  *#*
24  *#* Revision 1.9  2005/01/24 04:19:55  bernie
25  *#* Remove obsolete names.
26  *#*
27  *#* Revision 1.8  2005/01/24 04:19:05  bernie
28  *#* Function pointer based event dispatching.
29  *#*
30  *#* Revision 1.7  2004/08/25 14:12:09  rasky
31  *#* Aggiornato il comment block dei log RCS
32  *#*
33  *#* Revision 1.6  2004/08/14 19:37:57  rasky
34  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
35  *#*
36  *#* Revision 1.5  2004/07/30 14:30:27  rasky
37  *#* Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
38  *#* Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
39  *#*
40  *#* Revision 1.4  2004/06/07 15:58:00  aleph
41  *#* Add function prototypes
42  *#*
43  *#* Revision 1.3  2004/06/06 18:25:44  bernie
44  *#* Rename event macros to look like regular functions.
45  *#*
46  *#* Revision 1.2  2004/06/03 11:27:09  bernie
47  *#* Add dual-license information.
48  *#*
49  *#* Revision 1.1  2004/05/23 17:27:00  bernie
50  *#* Import kern/ subdirectory.
51  *#*
52  *#*/
53 #ifndef KERN_EVENT_H
54 #define KERN_EVENT_H
55
56 #include <config.h>
57
58 #if CONFIG_KERNEL
59         #include "config_kern.h"
60         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
61                 #include "signal.h"
62         #endif
63 #endif
64
65
66 /* Forward decl */
67 struct Process;
68
69 //! User defined callback type
70 typedef void (*Hook)(void *);
71
72 typedef struct Event
73 {
74         void (*action)(struct Event *);
75         union
76         {
77 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
78                 struct
79                 {
80                         struct Process *sig_proc;  /* Process to be signalled */
81                         sig_t           sig_bit;   /* Signal to send */
82                 } Sig;
83 #endif
84                 struct
85                 {
86                         Hook  func;         /* Pointer to softint hook */
87                         void *user_data;    /* Data to be passed back to user hook */
88                 } Int;
89         } Ev;
90 } Event;
91
92 void event_hook_ignore(Event *event);
93 void event_hook_signal(Event *event);
94 void event_hook_softint(Event *event);
95
96 /*! Initialize the event \a e as a no-op */
97 #define event_initNone(e) \
98         ((e)->action = event_hook_ignore)
99
100 /*! Same as event_initNone(), but returns the initialized event */
101 INLINE Event event_createNone(void);
102 INLINE Event event_createNone(void)
103 {
104         Event e;
105         e.action = event_hook_ignore;
106         return e;
107 }
108
109 /*! Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
110 #define event_initSoftInt(e,f,u) \
111         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
112
113 /*! Same as event_initSoftInt(), but returns the initialized event */
114 INLINE Event event_createSoftInt(Hook func, void* user_data)
115 {
116         Event e;
117         e.action = event_hook_softint;
118         e.Ev.Int.func = func;
119         e.Ev.Int.user_data = user_data;
120         return e;
121 }
122
123
124 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
125
126 /*! Initialize the event \a e with a signal (send signal \a s to process \a p) */
127 #define event_initSignal(e,p,s) \
128         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
129
130 /*! Same as event_initSignal(), but returns the initialized event */
131 INLINE Event event_createSignal(struct Process* proc, sig_t bit)
132 {
133         Event e;
134         e.action = event_hook_signal;
135         e.Ev.Sig.sig_proc = proc;
136         e.Ev.Sig.sig_bit = bit;
137         return e;
138 }
139
140 #endif
141
142 /*! Trigger an event */
143 INLINE void event_do(struct Event* e)
144 {
145         e->action(e);
146 }
147
148 #endif /* KERN_EVENT_H */