Contiki-Inga 3.x
mesh.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimemesh
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  */
37 
38 /**
39  * \file
40  * A mesh routing protocol
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "contiki.h"
46 #include "net/rime/rime.h"
47 #include "net/rime/route.h"
48 #include "net/rime/mesh.h"
49 
50 #include <stddef.h> /* For offsetof */
51 
52 #define PACKET_TIMEOUT (CLOCK_SECOND * 10)
53 
54 #define DEBUG 0
55 #if DEBUG
56 #include <stdio.h>
57 #define PRINTF(...) printf(__VA_ARGS__)
58 #else
59 #define PRINTF(...)
60 #endif
61 
62 /*---------------------------------------------------------------------------*/
63 static void
64 data_packet_received(struct multihop_conn *multihop,
65  const linkaddr_t *from,
66  const linkaddr_t *prevhop, uint8_t hops)
67 {
68  struct mesh_conn *c = (struct mesh_conn *)
69  ((char *)multihop - offsetof(struct mesh_conn, multihop));
70 
71  struct route_entry *rt;
72 
73  /* Refresh the route when we hear a packet from a neighbor. */
74  rt = route_lookup(from);
75  if(rt != NULL) {
76  route_refresh(rt);
77  }
78 
79  if(c->cb->recv) {
80  c->cb->recv(c, from, hops);
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 static linkaddr_t *
85 data_packet_forward(struct multihop_conn *multihop,
86  const linkaddr_t *originator,
87  const linkaddr_t *dest,
88  const linkaddr_t *prevhop, uint8_t hops)
89 {
90  struct route_entry *rt;
91  struct mesh_conn *c = (struct mesh_conn *)
92  ((char *)multihop - offsetof(struct mesh_conn, multihop));
93 
94  rt = route_lookup(dest);
95  if(rt == NULL) {
96  if(c->queued_data != NULL) {
97  queuebuf_free(c->queued_data);
98  }
99 
100  PRINTF("data_packet_forward: queueing data, sending rreq\n");
101  c->queued_data = queuebuf_new_from_packetbuf();
102  linkaddr_copy(&c->queued_data_dest, dest);
103  route_discovery_discover(&c->route_discovery_conn, dest, PACKET_TIMEOUT);
104 
105  return NULL;
106  } else {
107  route_refresh(rt);
108  }
109 
110  return &rt->nexthop;
111 }
112 /*---------------------------------------------------------------------------*/
113 static void
114 found_route(struct route_discovery_conn *rdc, const linkaddr_t *dest)
115 {
116  struct route_entry *rt;
117  struct mesh_conn *c = (struct mesh_conn *)
118  ((char *)rdc - offsetof(struct mesh_conn, route_discovery_conn));
119 
120  PRINTF("found_route\n");
121 
122  if(c->queued_data != NULL &&
123  linkaddr_cmp(dest, &c->queued_data_dest)) {
124  queuebuf_to_packetbuf(c->queued_data);
125  queuebuf_free(c->queued_data);
126  c->queued_data = NULL;
127 
128  rt = route_lookup(dest);
129  if(rt != NULL) {
130  multihop_resend(&c->multihop, &rt->nexthop);
131  if(c->cb->sent != NULL) {
132  c->cb->sent(c);
133  }
134  } else {
135  if(c->cb->timedout != NULL) {
136  c->cb->timedout(c);
137  }
138  }
139  }
140 }
141 /*---------------------------------------------------------------------------*/
142 static void
143 route_timed_out(struct route_discovery_conn *rdc)
144 {
145  struct mesh_conn *c = (struct mesh_conn *)
146  ((char *)rdc - offsetof(struct mesh_conn, route_discovery_conn));
147 
148  if(c->queued_data != NULL) {
149  queuebuf_free(c->queued_data);
150  c->queued_data = NULL;
151  }
152 
153  if(c->cb->timedout) {
154  c->cb->timedout(c);
155  }
156 }
157 /*---------------------------------------------------------------------------*/
158 static const struct multihop_callbacks data_callbacks = { data_packet_received,
159  data_packet_forward };
160 static const struct route_discovery_callbacks route_discovery_callbacks =
161  { found_route, route_timed_out };
162 /*---------------------------------------------------------------------------*/
163 void
164 mesh_open(struct mesh_conn *c, uint16_t channels,
165  const struct mesh_callbacks *callbacks)
166 {
167  route_init();
168  multihop_open(&c->multihop, channels, &data_callbacks);
169  route_discovery_open(&c->route_discovery_conn,
170  CLOCK_SECOND * 2,
171  channels + 1,
172  &route_discovery_callbacks);
173  c->cb = callbacks;
174 }
175 /*---------------------------------------------------------------------------*/
176 void
177 mesh_close(struct mesh_conn *c)
178 {
179  multihop_close(&c->multihop);
180  route_discovery_close(&c->route_discovery_conn);
181 }
182 /*---------------------------------------------------------------------------*/
183 int
184 mesh_send(struct mesh_conn *c, const linkaddr_t *to)
185 {
186  int could_send;
187 
188  PRINTF("%d.%d: mesh_send to %d.%d\n",
190  to->u8[0], to->u8[1]);
191 
192  could_send = multihop_send(&c->multihop, to);
193 
194  if(!could_send) {
195  PRINTF("mesh_send: could not send\n");
196  return 0;
197  }
198  if(c->cb->sent != NULL) {
199  c->cb->sent(c);
200  }
201  return 1;
202 }
203 /*---------------------------------------------------------------------------*/
204 int
205 mesh_ready(struct mesh_conn *c)
206 {
207  return (c->queued_data == NULL);
208 }
209 
210 
211 /** @} */