09a2c3487181c4c59b66d48adb771b415978055b
[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 Bernardo Innocenti <bernie@develer.com>
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 #include <cfg/macros.h>
44
45 #include <dt/dtag.h>
46 #include <mware/list.h>
47
48 //Fwd declaretion.
49 struct DNotifier;
50 struct DFilter;
51
52 typedef void (* update_func_ptr)(struct DNotifier *, dtag_t, dval_t);
53 typedef void (* update_filter_ptr)(struct DFilter *, dtag_t, dval_t);
54
55 /**
56  * Base object for receive and forward messages.
57  * It contains an update function used to update itslef and a list to
58  * notify other DNotifer eventually connected.
59  */
60 typedef struct DNotifier
61 {
62         /// Receive new attributes from other notifiers.
63         update_func_ptr update;
64
65         /// List of target notifiers to set new attributes to.
66         List targets;
67 } DNotifier;
68
69 /**
70  * Map for messages.
71  * Used to translate src message to dst message.
72  */
73 typedef struct DFilterMap
74 {
75         DTagItem src;
76         DTagItem dst;
77 } DFilterMap;
78
79
80 /**
81  * A filter is an interface between two notifier.
82  * It can translate messages between them through a map (if it is not null).
83  */
84 typedef struct DFilter
85 {
86         /// Allow creating a list of dfilter objects.
87         Node link;
88
89         /// Target of the filter
90         DNotifier *target;
91
92         /// Update function called by the source dnotifier
93         update_filter_ptr update;
94
95         ///Map for translating messages for target
96         const DFilterMap *map;
97
98         ///Used in debug to prevent inserting this filter in more than one list
99         DB(uint8_t magic;)
100 } DFilter;
101
102 /// Type for filter-mask checking
103 typedef uint16_t dfilter_mask_t;
104
105 /// Filter init
106 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target);
107
108 /// Filter update function without masking capabilities.
109 void filter_update(DFilter *f, dtag_t tag, dval_t val);
110
111 /// Filter update function with masking capabilities.
112 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val);
113
114 /// Notifier init
115 void notifier_init(DNotifier *n);
116
117
118 /**
119  * Macro to notify the target object.
120  */
121 INLINE void dnotify(DNotifier *target, dtag_t tag, dval_t val)
122 {
123         if (target)
124                 target->update(target, tag, val);
125 }
126
127 /**
128  * Macro to notify all the targets of \a target object.
129  */
130 INLINE void dnotify_targets(DNotifier *target, dtag_t tag, dval_t val)
131 {
132         DFilter *f;
133         if (!ISLISTEMPTY(&target->targets))
134                 FOREACHNODE(f, &target->targets)
135                         f->update(f, tag, val);
136 }
137
138
139 /**
140  * Macro that connect \a src notifier to \a tgt using \a map and passing \a opt for filtering option.
141  * It declares a static filter to achieve connection and messages translation.
142  * \note Due its static filter declaration, DCONNECT MUST NOT be used inside loops or in functions called multiple times.
143  * Failing to do so will lead to unpredictable connections between notifiers.
144  */
145 #define DCONNECT(src, tgt, map, opt) \
146         do { \
147                 static DFilter _filter_; /* Declare a filter */ \
148                 filter_init(&(_filter_), map, opt, src, tgt); /* Init it. */ \
149         } while (0)
150
151
152 #endif /* DT_DNOTIFIER_H */