Contiki 3.x
announcement.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Header file for the announcement primitive
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rime
42  * @{
43  */
44 
45 /**
46  * \defgroup rimeannouncement Announcements
47  * @{
48  *
49  * The Announcement primitive does local area announcements. An
50  * announcement is an (ID, value) tuple that is disseminated to local
51  * area neighbors. An application or protocol can explicitly listen to
52  * announcements from neighbors. When an announcement is heard, a
53  * callback is invoked.
54  *
55  * Announcements can be used for a variety of network mechanisms such
56  * as neighbor discovery, node-level service discovery, or routing
57  * metric dissemination.
58  *
59  * Application programs and protocols register announcements with the
60  * announcement module. An announcement back-end, implemented by the
61  * system, takes care of sending out announcements over the radio, as
62  * well as collecting announcements heard from neighbors.
63  *
64  */
65 
66 #ifndef ANNOUNCEMENT_H_
67 #define ANNOUNCEMENT_H_
68 
69 #include "net/linkaddr.h"
70 
71 struct announcement;
72 
73 typedef void (*announcement_callback_t)(struct announcement *a,
74  const linkaddr_t *from,
75  uint16_t id, uint16_t val);
76 
77 /**
78  * \brief Representation of an announcement.
79  *
80  * This structure holds the state of an announcement. It
81  * is an opaque structure with no user-visible elements.
82  */
83 struct announcement {
84  struct announcement *next;
85  uint16_t id;
86  uint16_t value;
87  announcement_callback_t callback;
88  uint8_t has_value;
89 };
90 
91 /**
92  * \name Application API
93  * @{
94  */
95 /**
96  * \brief Register an announcement
97  * \param a A pointer to a struct announcement
98  * \param id The identifying number of the announcement
99  * \param callback A pointer to a callback function that is called
100  * when an announcement is heard
101  *
102  * This function registers an announcement with the
103  * announcement module. The state of the announcement is
104  * held in a struct announcement variable, which is passed
105  * as an argument to this function. This variable must be
106  * allocated by the caller. An announcement is identified
107  * with a 16-bit number, which is passed as a parameter to
108  * the function. The announcement also has an initial
109  * value, that can later be changed with
110  * announcement_set_value().
111  *
112  */
113 void announcement_register(struct announcement *a,
114  uint16_t id,
115  announcement_callback_t callback);
116 
117 /**
118  * \brief Remove a previously registered announcement
119  * \param a A pointer to a struct announcement that has
120  * previously been registered
121  *
122  * This function removes an announcement that has
123  * previously been registered with
124  * announcement_register().
125  *
126  */
127 void announcement_remove(struct announcement *a);
128 
129 
130 /**
131  * \brief Set the value of an announcement
132  * \param a A pointer to a struct announcement that has
133  * previously been registered
134  * \param value The new value
135  *
136  * This function sets the value of an announcement that
137  * has previously been registered with
138  * announcement_register().
139  *
140  */
141 void announcement_set_value(struct announcement *a, uint16_t value);
142 
143 /**
144  * \brief Remove the value of an announcement
145  * \param a A pointer to a struct announcement that has
146  * previously been registered
147  *
148  * This function removes the value of an announcement that
149  * has previously been registered with
150  * announcement_register().
151  *
152  */
154 
155 
156 /**
157  * \brief Bump an announcement
158  * \param a A pointer to a struct announcement that has
159  * previously been registered
160  *
161  * This function is called to inform the announcement
162  * module that a particular announcement has changed in a
163  * way that it should be bumped. When an announcement is
164  * bumped, the announcement back-end may send out a new
165  * announcement to neighbors.
166  *
167  */
168 void announcement_bump(struct announcement *a);
169 
170 /**
171  * \brief Listen for announcements for a specific amount of
172  * announcement periods
173  * \param periods The number of periods to listen for announcement
174  *
175  * This function starts to listen for announcements for
176  * the specified amount of announcement periods. This
177  * function is called to ensure that the announcement
178  * module hears announcements from neighbors. The
179  * announcement module may hear announcements even if
180  * listening is not explicitly enabled, but with listening
181  * enabled, more announcements will be heard.
182  *
183  */
184 void announcement_listen(int periods);
185 /**
186  * @}
187  */
188 
189 /**
190  * \name System API
191  * @{
192  */
193 
194 /**
195  * \brief Initialize the announcement module
196  *
197  * This function initializes the announcement module, and
198  * is called by the system at boot up.
199  */
200 void announcement_init(void);
201 
202 /**
203  * \brief Get the list of registered announcements
204  * \return The list of registered announcements
205  *
206  * This function returns the list of registered
207  * announcements. This function is used by the back-end to
208  * compile announcement packets from the registered
209  * announcements.
210  *
211  * The announcement list is an ordinary Contiki list, as
212  * defined by the \ref list "list module".
213  *
214  */
215 struct announcement *announcement_list(void);
216 
217 /**
218  * \brief Inform the announcement module of an incoming announcement
219  * \param from The address of the sender of the announcement
220  * \param id The identifier of the announcement
221  * \param value The value of the announcement
222  *
223  * This function is called by the back-end to inform the
224  * announcement module that an announcement from a
225  * neighbor has been heard.
226  *
227  */
228 void announcement_heard(const linkaddr_t *from, uint16_t id, uint16_t value);
229 
230 /**
231  * \brief Register a listen callback with the announcement module
232  * \param callback A pointer to a callback function
233  *
234  * This function is called by the back-end to register a
235  * listen callback with the announcement module. The
236  * listen callback function is called by the announcement
237  * module as part of the announcement_listen() function.
238  *
239  */
240 void announcement_register_listen_callback(void (*callback)(int time));
241 
242 enum {
243  ANNOUNCEMENT_NOBUMP,
244  ANNOUNCEMENT_BUMP,
245 };
246 
247 typedef void (* announcement_observer)(uint16_t id, uint8_t has_value,
248  uint16_t newvalue, uint16_t oldvalue,
249  uint8_t bump);
250 
251 /**
252  * \brief Register an observer callback with the announcement module
253  * \param observer A pointer to an observer function
254  *
255  * This function is callback by the back-end to register
256  * an observer callback with the announcement module. The
257  * observer callback is called by the announcement module
258  * when an announcement is registered, removed, or have
259  * its identifier or value updated.
260  *
261  * The back-end may chose to send out a new announcement
262  * message with the updated values.
263  *
264  */
265 void announcement_register_observer_callback(announcement_observer observer);
266 
267 /**
268  * @}
269  */
270 
271 #endif /* ANNOUNCE_H_ */
272 
273 /** @} */
274 /** @} */
void announcement_bump(struct announcement *a)
Bump an announcement.
Definition: announcement.c:105
void announcement_register_observer_callback(announcement_observer callback)
Register an observer callback with the announcement module.
Definition: announcement.c:128
Representation of an announcement.
Definition: announcement.h:83
void announcement_set_value(struct announcement *a, uint16_t value)
Set the value of an announcement.
Definition: announcement.c:92
void announcement_remove_value(struct announcement *a)
Remove the value of an announcement.
Definition: announcement.c:82
void announcement_register(struct announcement *a, uint16_t id, announcement_callback_t callback)
Register an announcement.
Definition: announcement.c:62
void announcement_heard(const linkaddr_t *from, uint16_t id, uint16_t value)
Inform the announcement module of an incoming announcement.
Definition: announcement.c:140
Header file for the Rime address representation
struct announcement * announcement_list(void)
Get the list of registered announcements.
Definition: announcement.c:134
void announcement_listen(int time)
Listen for announcements for a specific amount of announcement periods.
Definition: announcement.c:114
void announcement_init(void)
Initialize the announcement module.
Definition: announcement.c:56
void announcement_register_listen_callback(void(*callback)(int time))
Register a listen callback with the announcement module.
Definition: announcement.c:122
void announcement_remove(struct announcement *a)
Remove a previously registered announcement.
Definition: announcement.c:76