Contiki 3.x
broadcast-announcement.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 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  * \addtogroup rimebroadcastannouncement
35  * @{
36  */
37 
38 /**
39  * \file
40  * An example announcement back-end, based on the broadcast primitive
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "contiki.h"
46 
47 #include "net/rime/rime.h"
48 #include "net/rime/announcement.h"
49 #include "net/rime/broadcast.h"
50 #include "lib/random.h"
51 #include "lib/list.h"
52 
53 #if NETSIM
54 #include "ether.h"
55 #endif
56 
57 #include <string.h>
58 #include <stddef.h>
59 
60 struct announcement_data {
61  uint16_t id;
62  uint16_t value;
63 };
64 
65 #ifdef BROADCAST_ANNOUNCEMENT_CONF_MAX_DUPS
66 #define NUM_DUPS BROADCAST_ANNOUNCEMENT_CONF_MAX_DUPS
67 #else /* BROADCAST_ANNOUNCEMENT_CONF_MAX_DUPS */
68 #define NUM_DUPS 5
69 #endif /* BROADCAST_ANNOUNCEMENT_CONF_MAX_DUPS */
70 
71 #define ANNOUNCEMENT_MSG_HEADERLEN 2
72 struct announcement_msg {
73  uint16_t num;
74  struct announcement_data data[];
75 };
76 
77 
78 static struct broadcast_announcement_state {
79  struct broadcast_conn c;
80  struct ctimer send_timer, interval_timer;
81  clock_time_t initial_interval, min_interval, max_interval;
82  clock_time_t current_interval;
83  uint16_t val;
84 } c;
85 
86 
87 #define DEBUG 0
88 #if DEBUG
89 #include <stdio.h>
90 #define PRINTF(...) printf(__VA_ARGS__)
91 #else
92 #define PRINTF(...)
93 #endif
94 
95 /*---------------------------------------------------------------------------*/
96 static void
97 send_adv(void *ptr)
98 {
99  struct announcement_msg *adata;
100  struct announcement *a;
101 
102  packetbuf_clear();
103  adata = packetbuf_dataptr();
104  adata->num = 0;
105  for(a = announcement_list(); a != NULL && a->has_value; a = list_item_next(a)) {
106  adata->data[adata->num].id = a->id;
107  adata->data[adata->num].value = a->value;
108  adata->num++;
109  }
110 
111  packetbuf_set_datalen(ANNOUNCEMENT_MSG_HEADERLEN +
112  sizeof(struct announcement_data) * adata->num);
113 
114  PRINTF("%d.%d: sending neighbor advertisement with %d announcements\n",
115  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], adata->num);
116 
117  if(adata->num > 0) {
118  /* Send the packet only if it contains more than zero announcements. */
119  broadcast_send(&c.c);
120  }
121  PRINTF("%d.%d: sending neighbor advertisement with val %d\n",
123  c.val);
124 }
125 /*---------------------------------------------------------------------------*/
126 static void
127 adv_packet_received(struct broadcast_conn *ibc, const linkaddr_t *from)
128 {
129  struct announcement_msg adata;
130  struct announcement_data data;
131  uint8_t *ptr;
132  int i;
133 
134  ptr = packetbuf_dataptr();
135 
136  /* Copy number of announcements */
137  memcpy(&adata, ptr, sizeof(struct announcement_msg));
138  PRINTF("%d.%d: adv_packet_received from %d.%d with %d announcements\n",
140  from->u8[0], from->u8[1], adata.num);
141 
142  if(ANNOUNCEMENT_MSG_HEADERLEN + adata.num * sizeof(struct announcement_data) > packetbuf_datalen()) {
143  /* The number of announcements is too large - corrupt packet has
144  been received. */
145  PRINTF("adata.num way out there: %d\n", adata.num);
146  return;
147  }
148 
149  ptr += ANNOUNCEMENT_MSG_HEADERLEN;
150  for(i = 0; i < adata.num; ++i) {
151  /* Copy announcements */
152  memcpy(&data, ptr, sizeof(struct announcement_data));
153  announcement_heard(from, data.id, data.value);
154  ptr += sizeof(struct announcement_data);
155  }
156 }
157 /*---------------------------------------------------------------------------*/
158 static void
159 adv_packet_sent(struct broadcast_conn *bc, int status, int num_tx)
160 {
161 }
162 /*---------------------------------------------------------------------------*/
163 static void send_timer(void *ptr);
164 
165 static void
166 set_timers(void)
167 {
168  ctimer_set(&c.interval_timer, c.current_interval, send_timer, NULL);
169  ctimer_set(&c.send_timer, random_rand() % c.current_interval,
170  send_adv, NULL);
171 }
172 /*---------------------------------------------------------------------------*/
173 static void
174 send_timer(void *ptr)
175 {
176  clock_time_t interval;
177 
178  interval = c.current_interval * 2;
179 
180  if(interval > c.max_interval) {
181  interval = c.max_interval;
182  }
183 
184  c.current_interval = interval;
185 
186  /* printf("current_interval %lu\n", (long unsigned int) interval);*/
187 
188  set_timers();
189 }
190 /*---------------------------------------------------------------------------*/
191 static void
192 new_announcement(uint16_t id, uint8_t has_value,
193  uint16_t newval, uint16_t oldval, uint8_t bump)
194 {
195  if(bump == ANNOUNCEMENT_BUMP) {
196  c.current_interval = c.initial_interval;
197  set_timers();
198  /* } else if(newval != oldval) {
199  c.current_interval = c.min_interval;
200  set_timers();*/
201  }
202 }
203 /*---------------------------------------------------------------------------*/
205  {adv_packet_received, adv_packet_sent };
206 /*---------------------------------------------------------------------------*/
207 void
208 broadcast_announcement_init(uint16_t channel,
209  clock_time_t initial,
210  clock_time_t min,
211  clock_time_t max)
212 {
213  broadcast_open(&c.c, channel, &broadcast_callbacks);
214  c.initial_interval = initial;
215  c.min_interval = min;
216  c.max_interval = max;
217 
218  announcement_register_observer_callback(new_announcement);
219 }
220 /*---------------------------------------------------------------------------*/
221 void
222 broadcast_announcement_stop(void)
223 {
224  ctimer_stop(&c.interval_timer);
225  ctimer_stop(&c.send_timer);
226  broadcast_close(&c.c);
227 }
228 /*---------------------------------------------------------------------------*/
229 clock_time_t
230 broadcast_announcement_beacon_interval(void)
231 {
232  return c.current_interval;
233 }
234 /*---------------------------------------------------------------------------*/
235 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
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
Header file for the announcement primitive
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:76
void broadcast_open(struct broadcast_conn *c, uint16_t channel, const struct broadcast_callbacks *u)
Set up an identified best-effort broadcast connection.
Definition: broadcast.c:96
void broadcast_close(struct broadcast_conn *c)
Close a broadcast connection.
Definition: broadcast.c:105
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
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
Callback structure for broadcast.
Definition: broadcast.h:80
Header file for the Rime stack
#define NULL
The null pointer.
struct announcement * announcement_list(void)
Get the list of registered announcements.
Definition: announcement.c:134
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:47
Linked list manipulation routines.
int broadcast_send(struct broadcast_conn *c)
Send an identified best-effort broadcast packet.
Definition: broadcast.c:111
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:151
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
Header file for identified best-effort local area broadcast
#define CC_CONST_FUNCTION
Configure if the C compiler have problems with const function pointers.
Definition: cc.h:77