Contiki-Inga 3.x
announcement.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimeannouncement
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2008, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  */
37 
38 /**
39  * \file
40  * Implementation of the announcement primitive
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "net/rime/announcement.h"
46 #include "lib/list.h"
47 #include "sys/cc.h"
48 
49 LIST(announcements);
50 
51 static void (* listen_callback)(int time);
52 static announcement_observer observer_callback;
53 
54 /*---------------------------------------------------------------------------*/
55 void
57 {
58  list_init(announcements);
59 }
60 /*---------------------------------------------------------------------------*/
61 void
62 announcement_register(struct announcement *a, uint16_t id,
63  announcement_callback_t callback)
64 {
65  a->id = id;
66  a->has_value = 0;
67  a->callback = callback;
68  list_add(announcements, a);
69  if(observer_callback) {
70  observer_callback(a->id, a->has_value,
71  a->value, 0, ANNOUNCEMENT_BUMP);
72  }
73 }
74 /*---------------------------------------------------------------------------*/
75 void
77 {
78  list_remove(announcements, a);
79 }
80 /*---------------------------------------------------------------------------*/
81 void
83 {
84  a->has_value = 0;
85  if(observer_callback) {
86  observer_callback(a->id, 0, 0, 0, ANNOUNCEMENT_NOBUMP);
87  }
88 
89 }
90 /*---------------------------------------------------------------------------*/
91 void
92 announcement_set_value(struct announcement *a, uint16_t value)
93 {
94  uint16_t oldvalue = a->value;
95 
96  a->has_value = 1;
97  a->value = value;
98  if(observer_callback) {
99  observer_callback(a->id, a->has_value,
100  value, oldvalue, ANNOUNCEMENT_NOBUMP);
101  }
102 }
103 /*---------------------------------------------------------------------------*/
104 void
106 {
107  if(observer_callback) {
108  observer_callback(a->id, a->has_value,
109  a->value, a->value, ANNOUNCEMENT_BUMP);
110  }
111 }
112 /*---------------------------------------------------------------------------*/
113 void
115 {
116  if(listen_callback) {
117  listen_callback(time);
118  }
119 }
120 /*---------------------------------------------------------------------------*/
121 void
122 announcement_register_listen_callback(void (*callback)(int time))
123 {
124  listen_callback = callback;
125 }
126 /*---------------------------------------------------------------------------*/
127 void
128 announcement_register_observer_callback(announcement_observer callback)
129 {
130  observer_callback = callback;
131 }
132 /*---------------------------------------------------------------------------*/
133 struct announcement *
135 {
136  return list_head(announcements);
137 }
138 /*---------------------------------------------------------------------------*/
139 void
140 announcement_heard(const linkaddr_t *from, uint16_t id, uint16_t value)
141 {
142  struct announcement *a;
143  for(a = list_head(announcements); a != NULL; a = list_item_next(a)) {
144  if(a->id == id) {
145  if(a->callback != NULL) {
146  a->callback(a, from, id, value);
147  }
148  return;
149  }
150  }
151 }
152 /*---------------------------------------------------------------------------*/
153 /** @} */