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  *
135  * This function sets the value of an announcement that
136  * has previously been registered with
137  * announcement_register().
138  *
139  */
140 void announcement_set_value(struct announcement *a, uint16_t value);
141 
142 /**
143  * \brief Remove the value of an announcement
144  * \param a A pointer to a struct announcement that has
145  * previously been registered
146  *
147  * This function removes the value of an announcement that
148  * has previously been registered with
149  * announcement_register().
150  *
151  */
153 
154 
155 /**
156  * \brief Bump an announcement
157  * \param a A pointer to a struct announcement that has
158  * previously been registered
159  *
160  * This function is called to inform the announcement
161  * module that a particular announcement has changed in a
162  * way that it should be bumped. When an announcement is
163  * bumped, the announcement back-end may send out a new
164  * announcement to neighbors.
165  *
166  */
167 void announcement_bump(struct announcement *a);
168 
169 /**
170  * \brief Listen for announcements for a specific amount of
171  * announcement periods
172  * \param periods The number of periods to listen for announcement
173  *
174  * This function starts to listen for announcements for
175  * the specified amount of announcement periods. This
176  * function is called to ensure that the announcement
177  * module hears announcements from neighbors. The
178  * announcement module may hear announcements even if
179  * listening is not explicitly enabled, but with listening
180  * enabled, more announcements will be heard.
181  *
182  */
183 void announcement_listen(int periods);
184 /**
185  * @}
186  */
187 
188 /**
189  * \name System API
190  * @{
191  */
192 
193 /**
194  * \brief Initialize the announcement module
195  *
196  * This function initializes the announcement module, and
197  * is called by the system at boot up.
198  */
199 void announcement_init(void);
200 
201 /**
202  * \brief Get the list of registered announcements
203  * \return The list of registered announcements
204  *
205  * This function returns the list of registered
206  * announcements. This function is used by the back-end to
207  * compile announcement packets from the registered
208  * announcements.
209  *
210  * The announcement list is an ordinary Contiki list, as
211  * defined by the \ref list "list module".
212  *
213  */
214 struct announcement *announcement_list(void);
215 
216 /**
217  * \brief Inform the announcement module of an incoming announcement
218  * \param from The address of the sender of the announcement
219  * \param id The identifier of the announcement
220  * \param value The value of the announcement
221  *
222  * This function is called by the back-end to inform the
223  * announcement module that an announcement from a
224  * neighbor has been heard.
225  *
226  */
227 void announcement_heard(const linkaddr_t *from, uint16_t id, uint16_t value);
228 
229 /**
230  * \brief Register a listen callback with the announcement module
231  * \param callback A pointer to a callback function
232  *
233  * This function is called by the back-end to register a
234  * listen callback with the announcement module. The
235  * listen callback function is called by the announcement
236  * module as part of the announcement_listen() function.
237  *
238  */
239 void announcement_register_listen_callback(void (*callback)(int time));
240 
241 enum {
242  ANNOUNCEMENT_NOBUMP,
243  ANNOUNCEMENT_BUMP,
244 };
245 
246 typedef void (* announcement_observer)(uint16_t id, uint8_t has_value,
247  uint16_t newvalue, uint16_t oldvalue,
248  uint8_t bump);
249 
250 /**
251  * \brief Register an observer callback with the announcement module
252  * \param observer A pointer to an observer function
253  *
254  * This function is callback by the back-end to register
255  * an observer callback with the announcement module. The
256  * observer callback is called by the announcement module
257  * when an announcement is registered, removed, or have
258  * its identifier or value updated.
259  *
260  * The back-end may chose to send out a new announcement
261  * message with the updated values.
262  *
263  */
264 void announcement_register_observer_callback(announcement_observer observer);
265 
266 /**
267  * @}
268  */
269 
270 #endif /* ANNOUNCE_H_ */
271 
272 /** @} */
273 /** @} */
void announcement_remove(struct announcement *a)
Remove a previously registered announcement.
Definition: announcement.c:76
void announcement_register(struct announcement *a, uint16_t id, announcement_callback_t callback)
Register an announcement.
Definition: announcement.c:62
void announcement_register_observer_callback(announcement_observer callback)
Register an observer callback with the announcement module.
Definition: announcement.c:128
void announcement_remove_value(struct announcement *a)
Remove the value of an announcement.
Definition: announcement.c:82
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
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_register_listen_callback(void(*callback)(int time))
Register a listen callback with the announcement module.
Definition: announcement.c:122
void announcement_init(void)
Initialize the announcement module.
Definition: announcement.c:56
Representation of an announcement.
Definition: announcement.h:83
void announcement_bump(struct announcement *a)
Bump an announcement.
Definition: announcement.c:105
void announcement_set_value(struct announcement *a, uint16_t value)
Set the value of an announcement.
Definition: announcement.c:92
Header file for the Rime address representation