Contiki 3.x
mesh.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 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  * A mesh routing protocol
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimemesh
42  * @{
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 /** @} */
int mesh_send(struct mesh_conn *c, const linkaddr_t *to)
Send a mesh packet.
Definition: mesh.c:184
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
#define NULL
The null pointer.
void mesh_close(struct mesh_conn *c)
Close an mesh connection.
Definition: mesh.c:177
Header file for the Rime stack
Mesh callbacks.
Definition: mesh.h:73
int mesh_ready(struct mesh_conn *c)
Test if mesh is ready to send a packet (or packet is queued)
Definition: mesh.c:205
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void mesh_open(struct mesh_conn *c, uint16_t channels, const struct mesh_callbacks *callbacks)
Open a mesh connection.
Definition: mesh.c:164
Header file for the Rime mesh routing protocol
Header file for the Rime route table
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82