Contiki 2.5
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  * $Id: announcement.c,v 1.6 2010/06/15 19:22:25 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Implementation of the announcement primitive
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "net/rime/announcement.h"
47 #include "lib/list.h"
48 #include "sys/cc.h"
49 
50 LIST(announcements);
51 
52 static void (* listen_callback)(int time);
53 static announcement_observer observer_callback;
54 
55 /*---------------------------------------------------------------------------*/
56 void
58 {
59  list_init(announcements);
60 }
61 /*---------------------------------------------------------------------------*/
62 void
63 announcement_register(struct announcement *a, uint16_t id,
64  announcement_callback_t callback)
65 {
66  a->id = id;
67  a->has_value = 0;
68  a->callback = callback;
69  list_add(announcements, a);
70  if(observer_callback) {
71  observer_callback(a->id, a->has_value,
72  a->value, 0, ANNOUNCEMENT_BUMP);
73  }
74 }
75 /*---------------------------------------------------------------------------*/
76 void
78 {
79  list_remove(announcements, a);
80 }
81 /*---------------------------------------------------------------------------*/
82 void
84 {
85  a->has_value = 0;
86  if(observer_callback) {
87  observer_callback(a->id, 0, 0, 0, ANNOUNCEMENT_NOBUMP);
88  }
89 
90 }
91 /*---------------------------------------------------------------------------*/
92 void
93 announcement_set_value(struct announcement *a, uint16_t value)
94 {
95  uint16_t oldvalue = a->value;
96 
97  a->has_value = 1;
98  a->value = value;
99  if(observer_callback) {
100  observer_callback(a->id, a->has_value,
101  value, oldvalue, ANNOUNCEMENT_NOBUMP);
102  }
103 }
104 /*---------------------------------------------------------------------------*/
105 void
107 {
108  if(observer_callback) {
109  observer_callback(a->id, a->has_value,
110  a->value, a->value, ANNOUNCEMENT_BUMP);
111  }
112 }
113 /*---------------------------------------------------------------------------*/
114 void
116 {
117  if(listen_callback) {
118  listen_callback(time);
119  }
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 announcement_register_listen_callback(void (*callback)(int time))
124 {
125  listen_callback = callback;
126 }
127 /*---------------------------------------------------------------------------*/
128 void
129 announcement_register_observer_callback(announcement_observer callback)
130 {
131  observer_callback = callback;
132 }
133 /*---------------------------------------------------------------------------*/
134 struct announcement *
136 {
137  return list_head(announcements);
138 }
139 /*---------------------------------------------------------------------------*/
140 void
141 announcement_heard(const rimeaddr_t *from, uint16_t id, uint16_t value)
142 {
143  struct announcement *a;
144  for(a = list_head(announcements); a != NULL; a = list_item_next(a)) {
145  if(a->id == id) {
146  if(a->callback != NULL) {
147  a->callback(a, from, id, value);
148  }
149  return;
150  }
151  }
152 }
153 /*---------------------------------------------------------------------------*/
154 /** @} */