Contiki-Inga 3.x
polite-announcement.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimeexamples
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2006, 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  * An example announcement back-end, based on the polite primitive
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "contiki.h"
46 
47 #include "lib/list.h"
48 #include "net/rime/rime.h"
49 #include "net/rime/announcement.h"
50 #include "net/rime/ipolite.h"
51 
52 #if NETSIM
53 #include "ether.h"
54 #endif
55 
56 #include <string.h>
57 #include <stdio.h>
58 #include <stddef.h>
59 
60 struct announcement_data {
61  uint16_t id;
62  uint16_t value;
63 };
64 
65 #ifdef POLITE_ANNOUNCEMENT_CONF_MAX_DUPS
66 #define NUM_DUPS POLITE_ANNOUNCEMENT_CONF_MAX_DUPS
67 #else /* POLITE_ANNOUNCEMENT_CONF_MAX_DUPS */
68 #define NUM_DUPS 5
69 #endif /* POLITE_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 polite_announcement_state {
79  struct ipolite_conn c;
80  struct ctimer t;
81  clock_time_t interval;
82  clock_time_t min_interval, max_interval;
83 } c;
84 
85 #define DEBUG 0
86 #if DEBUG
87 #include <stdio.h>
88 #define PRINTF(...) printf(__VA_ARGS__)
89 #else
90 #define PRINTF(...)
91 #endif
92 
93 #define MIN(a, b) ((a)<(b)?(a):(b))
94 
95 /*---------------------------------------------------------------------------*/
96 static void
97 send_adv(clock_time_t interval)
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 = 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  ipolite_send(&c.c, interval, packetbuf_datalen());
120  }
121 }
122 /*---------------------------------------------------------------------------*/
123 static void
124 adv_packet_received(struct ipolite_conn *ipolite, const linkaddr_t *from)
125 {
126  struct announcement_msg adata;
127  struct announcement_data data;
128  uint8_t *ptr;
129  int i;
130 
131  ptr = packetbuf_dataptr();
132 
133  /* Copy number of announcements */
134  memcpy(&adata, ptr, sizeof(struct announcement_msg));
135  PRINTF("%d.%d: adv_packet_received from %d.%d with %d announcements\n",
137  from->u8[0], from->u8[1], adata.num);
138 
139  if(ANNOUNCEMENT_MSG_HEADERLEN + adata.num * sizeof(struct announcement_data) > packetbuf_datalen()) {
140  /* The number of announcements is too large - corrupt packet has
141  been received. */
142  PRINTF("adata.num way out there: %d\n", adata.num);
143  return;
144  }
145 
146  ptr += ANNOUNCEMENT_MSG_HEADERLEN;
147  for(i = 0; i < adata.num; ++i) {
148  /* Copy announcements */
149  memcpy(&data, ptr, sizeof(struct announcement_data));
150  announcement_heard(from, data.id, data.value);
151  ptr += sizeof(struct announcement_data);
152  }
153 }
154 /*---------------------------------------------------------------------------*/
155 static void
156 send_timer(void *ptr)
157 {
158  send_adv(c.interval);
159  ctimer_set(&c.t,
160  c.interval,
161  send_timer, &c);
162 
163  c.interval = MIN(c.interval * 2, c.max_interval);
164 }
165 /*---------------------------------------------------------------------------*/
166 static void
167 new_announcement(uint16_t id, uint8_t has_value, uint16_t newval,
168  uint16_t oldval, uint8_t bump)
169 {
170  if(newval != oldval) {
171  c.interval = c.min_interval;
172  send_timer(&c);
173  }
174 }
175 /*---------------------------------------------------------------------------*/
176 static const struct ipolite_callbacks ipolite_callbacks =
177  {adv_packet_received, NULL, NULL};
178 /*---------------------------------------------------------------------------*/
179 void
180 polite_announcement_init(uint16_t channel,
181  clock_time_t min,
182  clock_time_t max)
183 {
184  ipolite_open(&c.c, channel, NUM_DUPS, &ipolite_callbacks);
185 
186  c.min_interval = min;
187  c.max_interval = max;
188 
189  announcement_register_observer_callback(new_announcement);
190 }
191 /*---------------------------------------------------------------------------*/
192 void
193 polite_announcement_stop(void)
194 {
195  ctimer_stop(&c.t);
196  ipolite_close(&c.c);
197 }
198 /*---------------------------------------------------------------------------*/
199 /** @} */