Contiki 2.5
route-discovery.c
Go to the documentation of this file.
1 /**
2  * \addtogroup routediscovery
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2007, 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: route-discovery.c,v 1.20 2010/01/27 08:12:56 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Route discovery protocol
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "contiki.h"
47 #include "net/rime.h"
48 #include "net/rime/route.h"
50 
51 #include <stddef.h> /* For offsetof */
52 #include <stdio.h>
53 
54 struct route_msg {
55  rimeaddr_t dest;
56  uint8_t rreq_id;
57  uint8_t pad;
58 };
59 
60 struct rrep_hdr {
61  uint8_t rreq_id;
62  uint8_t hops;
63  rimeaddr_t dest;
64  rimeaddr_t originator;
65 };
66 
67 #if CONTIKI_TARGET_NETSIM
68 #include "ether.h"
69 #endif
70 
71 
72 #define DEBUG 0
73 #if DEBUG
74 #include <stdio.h>
75 #define PRINTF(...) printf(__VA_ARGS__)
76 #else
77 #define PRINTF(...)
78 #endif
79 
80 /*---------------------------------------------------------------------------*/
81 static char rrep_pending; /* A reply for a request is pending. */
82 /*---------------------------------------------------------------------------*/
83 static void
84 send_rreq(struct route_discovery_conn *c, const rimeaddr_t *dest)
85 {
86  rimeaddr_t dest_copy;
87  struct route_msg *msg;
88 
89  rimeaddr_copy(&dest_copy, dest);
90  dest = &dest_copy;
91 
93  msg = packetbuf_dataptr();
94  packetbuf_set_datalen(sizeof(struct route_msg));
95 
96  msg->pad = 0;
97  msg->rreq_id = c->rreq_id;
98  rimeaddr_copy(&msg->dest, dest);
99 
100  netflood_send(&c->rreqconn, c->rreq_id);
101  c->rreq_id++;
102 }
103 /*---------------------------------------------------------------------------*/
104 static void
105 send_rrep(struct route_discovery_conn *c, const rimeaddr_t *dest)
106 {
107  struct rrep_hdr *rrepmsg;
108  struct route_entry *rt;
109  rimeaddr_t saved_dest;
110 
111  rimeaddr_copy(&saved_dest, dest);
112 
113  packetbuf_clear();
114  dest = &saved_dest;
115  rrepmsg = packetbuf_dataptr();
116  packetbuf_set_datalen(sizeof(struct rrep_hdr));
117  rrepmsg->hops = 0;
118  rimeaddr_copy(&rrepmsg->dest, dest);
119  rimeaddr_copy(&rrepmsg->originator, &rimeaddr_node_addr);
120  rt = route_lookup(dest);
121  if(rt != NULL) {
122  PRINTF("%d.%d: send_rrep to %d.%d via %d.%d\n",
124  dest->u8[0],dest->u8[1],
125  rt->nexthop.u8[0],rt->nexthop.u8[1]);
126  unicast_send(&c->rrepconn, &rt->nexthop);
127  } else {
128  PRINTF("%d.%d: no route for rrep to %d.%d\n",
130  dest->u8[0],dest->u8[1]);
131  }
132 }
133 /*---------------------------------------------------------------------------*/
134 static void
135 insert_route(const rimeaddr_t *originator, const rimeaddr_t *last_hop,
136  uint8_t hops)
137 {
138  PRINTF("%d.%d: Inserting %d.%d into routing table, next hop %d.%d, hop count %d\n",
140  originator->u8[0], originator->u8[1],
141  last_hop->u8[0], last_hop->u8[1],
142  hops);
143 
144  route_add(originator, last_hop, hops, 0);
145  /*
146  struct route_entry *rt;
147 
148  rt = route_lookup(originator);
149  if(rt == NULL || hops < rt->hop_count) {
150  PRINTF("%d.%d: Inserting %d.%d into routing table, next hop %d.%d, hop count %d\n",
151  rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
152  originator->u8[0], originator->u8[1],
153  last_hop->u8[0], last_hop->u8[1],
154  hops);
155  route_add(originator, last_hop, hops, 0);
156 #if CONTIKI_TARGET_NETSIM
157  ether_set_line(last_hop->u8[0], last_hop->u8[1]);
158 #endif
159 
160 }*/
161 }
162 /*---------------------------------------------------------------------------*/
163 static void
164 rrep_packet_received(struct unicast_conn *uc, const rimeaddr_t *from)
165 {
166  struct rrep_hdr *msg = packetbuf_dataptr();
167  struct route_entry *rt;
168  rimeaddr_t dest;
169  struct route_discovery_conn *c = (struct route_discovery_conn *)
170  ((char *)uc - offsetof(struct route_discovery_conn, rrepconn));
171 
172  PRINTF("%d.%d: rrep_packet_received from %d.%d towards %d.%d len %d\n",
174  from->u8[0],from->u8[1],
175  msg->dest.u8[0],msg->dest.u8[1],
177 
178  PRINTF("from %d.%d hops %d rssi %d lqi %d\n",
179  from->u8[0], from->u8[1],
180  msg->hops,
181  packetbuf_attr(PACKETBUF_ATTR_RSSI),
182  packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
183 
184  insert_route(&msg->originator, from, msg->hops);
185 
186  if(rimeaddr_cmp(&msg->dest, &rimeaddr_node_addr)) {
187  PRINTF("rrep for us!\n");
188  rrep_pending = 0;
189  ctimer_stop(&c->t);
190  if(c->cb->new_route) {
191  rimeaddr_t originator;
192 
193  /* If the callback modifies the packet, the originator address
194  will be lost. Therefore, we need to copy it into a local
195  variable before calling the callback. */
196  rimeaddr_copy(&originator, &msg->originator);
197  c->cb->new_route(c, &originator);
198  }
199 
200  } else {
201  rimeaddr_copy(&dest, &msg->dest);
202 
203  rt = route_lookup(&msg->dest);
204  if(rt != NULL) {
205  PRINTF("forwarding to %d.%d\n", rt->nexthop.u8[0], rt->nexthop.u8[1]);
206  msg->hops++;
207  unicast_send(&c->rrepconn, &rt->nexthop);
208  } else {
209  PRINTF("%d.%d: no route to %d.%d\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], msg->dest.u8[0], msg->dest.u8[1]);
210  }
211  }
212 }
213 /*---------------------------------------------------------------------------*/
214 static int
215 rreq_packet_received(struct netflood_conn *nf, const rimeaddr_t *from,
216  const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
217 {
218  struct route_msg *msg = packetbuf_dataptr();
219  struct route_discovery_conn *c = (struct route_discovery_conn *)
220  ((char *)nf - offsetof(struct route_discovery_conn, rreqconn));
221 
222  PRINTF("%d.%d: rreq_packet_received from %d.%d hops %d rreq_id %d last %d.%d/%d\n",
224  from->u8[0], from->u8[1],
225  hops, msg->rreq_id,
226  c->last_rreq_originator.u8[0],
227  c->last_rreq_originator.u8[1],
228  c->last_rreq_id);
229 
230  if(!(rimeaddr_cmp(&c->last_rreq_originator, originator) &&
231  c->last_rreq_id == msg->rreq_id)) {
232 
233  PRINTF("%d.%d: rreq_packet_received: request for %d.%d originator %d.%d / %d\n",
235  msg->dest.u8[0], msg->dest.u8[1],
236  originator->u8[0], originator->u8[1],
237  msg->rreq_id);
238 
239  rimeaddr_copy(&c->last_rreq_originator, originator);
240  c->last_rreq_id = msg->rreq_id;
241 
242  if(rimeaddr_cmp(&msg->dest, &rimeaddr_node_addr)) {
243  PRINTF("%d.%d: route_packet_received: route request for our address\n",
245  PRINTF("from %d.%d hops %d rssi %d lqi %d\n",
246  from->u8[0], from->u8[1],
247  hops,
248  packetbuf_attr(PACKETBUF_ATTR_RSSI),
249  packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
250 
251  insert_route(originator, from, hops);
252 
253  /* Send route reply back to source. */
254  send_rrep(c, originator);
255  return 0; /* Don't continue to flood the rreq packet. */
256  } else {
257  /* PRINTF("route request for %d\n", msg->dest_id);*/
258  PRINTF("from %d.%d hops %d rssi %d lqi %d\n",
259  from->u8[0], from->u8[1],
260  hops,
261  packetbuf_attr(PACKETBUF_ATTR_RSSI),
262  packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
263  insert_route(originator, from, hops);
264  }
265 
266  return 1;
267  }
268  return 0; /* Don't forward packet. */
269 }
270 /*---------------------------------------------------------------------------*/
271 static const struct unicast_callbacks rrep_callbacks = {rrep_packet_received};
272 static const struct netflood_callbacks rreq_callbacks = {rreq_packet_received, NULL, NULL};
273 /*---------------------------------------------------------------------------*/
274 void
275 route_discovery_open(struct route_discovery_conn *c,
276  clock_time_t time,
277  uint16_t channels,
278  const struct route_discovery_callbacks *callbacks)
279 {
280  netflood_open(&c->rreqconn, time, channels + 0, &rreq_callbacks);
281  unicast_open(&c->rrepconn, channels + 1, &rrep_callbacks);
282  c->cb = callbacks;
283 }
284 /*---------------------------------------------------------------------------*/
285 void
286 route_discovery_close(struct route_discovery_conn *c)
287 {
288  unicast_close(&c->rrepconn);
289  netflood_close(&c->rreqconn);
290  ctimer_stop(&c->t);
291 }
292 /*---------------------------------------------------------------------------*/
293 static void
294 timeout_handler(void *ptr)
295 {
296  struct route_discovery_conn *c = ptr;
297  PRINTF("route_discovery: timeout, timed out\n");
298  rrep_pending = 0;
299  if(c->cb->timedout) {
300  c->cb->timedout(c);
301  }
302 }
303 /*---------------------------------------------------------------------------*/
304 int
305 route_discovery_discover(struct route_discovery_conn *c, const rimeaddr_t *addr,
306  clock_time_t timeout)
307 {
308  if(rrep_pending) {
309  PRINTF("route_discovery_send: ignoring request because of pending response\n");
310  return 0;
311  }
312 
313  PRINTF("route_discovery_send: sending route request\n");
314  ctimer_set(&c->t, timeout, timeout_handler, c);
315  rrep_pending = 1;
316  send_rreq(c, addr);
317  return 1;
318 }
319 /*---------------------------------------------------------------------------*/
320 /** @} */