Contiki 3.x
polite-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  * \file
35  * An example announcement back-end, based on the polite primitive
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimepoliteannouncement
42  * @{
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 /** @} */
A structure with callback functions for an ipolite connection.
Definition: ipolite.h:113
Linked list manipulation routines.
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
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_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.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:200
Header file for the Rime stack
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
struct announcement * announcement_list(void)
Get the list of registered announcements.
Definition: announcement.c:134
Header file for the announcement primitive
void ipolite_close(struct ipolite_conn *c)
Close an ipolite connection.
Definition: ipolite.c:133
void ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups, const struct ipolite_callbacks *cb)
Open an ipolite connection.
Definition: ipolite.c:123
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
Header file for Ipolite best effort local Broadcast (ipolite)
An opaque structure with no user-visible elements that holds the state of an ipolite connection...
Definition: ipolite.h:135
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
Representation of an announcement.
Definition: announcement.h:83
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
int ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
Send a packet on an ipolite connection.
Definition: ipolite.c:144
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:142