Fix warning on architectures with longer integers.
[bertos.git] / bertos / dt / dnotifier.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Notifier obj (interface).
33  *
34  * \version $Id$
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  * \author Francesco Sacchi <batt@develer.com>
37  */
38
39 #ifndef DT_DNOTIFIER_H
40 #define DT_DNOTIFIER_H
41
42 #include <cfg/debug.h>
43
44 #include <dt/dtag.h>
45 #include <struct/list.h>
46
47 //Fwd declaretion.
48 struct DNotifier;
49 struct DFilter;
50
51 typedef void (* update_func_ptr)(struct DNotifier *, dtag_t, dval_t);
52 typedef void (* update_filter_ptr)(struct DFilter *, dtag_t, dval_t);
53
54 /**
55  * Base object for receive and forward messages.
56  * It contains an update function used to update itslef and a list to
57  * notify other DNotifer eventually connected.
58  */
59 typedef struct DNotifier
60 {
61         /// Receive new attributes from other notifiers.
62         update_func_ptr update;
63
64         /// List of target notifiers to set new attributes to.
65         List targets;
66 } DNotifier;
67
68 /**
69  * Map for messages.
70  * Used to translate src message to dst message.
71  */
72 typedef struct DFilterMap
73 {
74         DTagItem src;
75         DTagItem dst;
76 } DFilterMap;
77
78
79 /**
80  * A filter is an interface between two notifier.
81  * It can translate messages between them through a map (if it is not null).
82  */
83 typedef struct DFilter
84 {
85         /// Allow creating a list of dfilter objects.
86         Node link;
87
88         /// Target of the filter
89         DNotifier *target;
90
91         /// Update function called by the source dnotifier
92         update_filter_ptr update;
93
94         ///Map for translating messages for target
95         const DFilterMap *map;
96
97         ///Used in debug to prevent inserting this filter in more than one list
98         DB(uint8_t magic;)
99 } DFilter;
100
101 /// Type for filter-mask checking
102 typedef unsigned int dfilter_mask_t;
103
104 /// Filter init
105 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target);
106
107 /// Filter update function without masking capabilities.
108 void filter_update(DFilter *f, dtag_t tag, dval_t val);
109
110 /// Filter update function with masking capabilities.
111 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val);
112
113 /// Notifier init
114 void notifier_init(DNotifier *n);
115
116
117 /**
118  * Macro to notify the target object.
119  */
120 INLINE void dnotify(DNotifier *target, dtag_t tag, dval_t val)
121 {
122         if (target)
123                 target->update(target, tag, val);
124 }
125
126 /**
127  * Macro to notify all the targets of \a target object.
128  */
129 INLINE void dnotify_targets(DNotifier *target, dtag_t tag, dval_t val)
130 {
131         DFilter *f;
132         if (!LIST_EMPTY(&target->targets))
133                 FOREACH_NODE(f, &target->targets)
134                         f->update(f, tag, val);
135 }
136
137
138 /**
139  * Macro that connect \a src notifier to \a tgt using \a map and passing \a opt for filtering option.
140  * It declares a static filter to achieve connection and messages translation.
141  * \note Due its static filter declaration, DCONNECT MUST NOT be used inside loops or in functions called multiple times.
142  * Failing to do so will lead to unpredictable connections between notifiers.
143  */
144 #define DCONNECT(src, tgt, map, opt) \
145         do { \
146                 static DFilter _filter_; /* Declare a filter */ \
147                 filter_init(&(_filter_), map, opt, src, tgt); /* Init it. */ \
148         } while (0)
149
150
151 #endif /* DT_DNOTIFIER_H */