Contiki 3.x
announcement.c
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  * Implementation of the announcement primitive
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimeannouncement
42  * @{
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 /** @} */
void announcement_remove(struct announcement *a)
Remove a previously registered announcement.
Definition: announcement.c:76
Linked list manipulation routines.
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 * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
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
#define NULL
The null pointer.
Default definitions of C compiler quirk work-arounds.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
struct announcement * announcement_list(void)
Get the list of registered announcements.
Definition: announcement.c:134
Header file for the announcement primitive
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
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
#define LIST(name)
Declare a linked list.
Definition: list.h:86
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