64 rimeaddr_t originator;
67 #if CONTIKI_TARGET_NETSIM
75 #define PRINTF(...) printf(__VA_ARGS__)
81 static char rrep_pending;
84 send_rreq(
struct route_discovery_conn *c,
const rimeaddr_t *dest)
87 struct route_msg *msg;
97 msg->rreq_id = c->rreq_id;
100 netflood_send(&c->rreqconn, c->rreq_id);
105 send_rrep(
struct route_discovery_conn *c,
const rimeaddr_t *dest)
107 struct rrep_hdr *rrepmsg;
108 struct route_entry *rt;
109 rimeaddr_t saved_dest;
120 rt = route_lookup(dest);
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);
128 PRINTF(
"%d.%d: no route for rrep to %d.%d\n",
130 dest->u8[0],dest->u8[1]);
135 insert_route(
const rimeaddr_t *originator,
const rimeaddr_t *last_hop,
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],
144 route_add(originator, last_hop, hops, 0);
164 rrep_packet_received(
struct unicast_conn *uc,
const rimeaddr_t *from)
167 struct route_entry *rt;
169 struct route_discovery_conn *c = (
struct route_discovery_conn *)
170 ((
char *)uc - offsetof(
struct route_discovery_conn, rrepconn));
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],
178 PRINTF(
"from %d.%d hops %d rssi %d lqi %d\n",
179 from->u8[0], from->u8[1],
181 packetbuf_attr(PACKETBUF_ATTR_RSSI),
182 packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
184 insert_route(&msg->originator, from, msg->hops);
187 PRINTF(
"rrep for us!\n");
190 if(c->cb->new_route) {
191 rimeaddr_t originator;
197 c->cb->new_route(c, &originator);
203 rt = route_lookup(&msg->dest);
205 PRINTF(
"forwarding to %d.%d\n", rt->nexthop.u8[0], rt->nexthop.u8[1]);
207 unicast_send(&c->rrepconn, &rt->nexthop);
215 rreq_packet_received(
struct netflood_conn *nf,
const rimeaddr_t *from,
216 const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
219 struct route_discovery_conn *c = (
struct route_discovery_conn *)
220 ((
char *)nf - offsetof(
struct route_discovery_conn, rreqconn));
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],
226 c->last_rreq_originator.u8[0],
227 c->last_rreq_originator.u8[1],
230 if(!(
rimeaddr_cmp(&c->last_rreq_originator, originator) &&
231 c->last_rreq_id == msg->rreq_id)) {
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],
240 c->last_rreq_id = msg->rreq_id;
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],
248 packetbuf_attr(PACKETBUF_ATTR_RSSI),
249 packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
251 insert_route(originator, from, hops);
254 send_rrep(c, originator);
258 PRINTF(
"from %d.%d hops %d rssi %d lqi %d\n",
259 from->u8[0], from->u8[1],
261 packetbuf_attr(PACKETBUF_ATTR_RSSI),
262 packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
263 insert_route(originator, from, hops);
271 static const struct unicast_callbacks rrep_callbacks = {rrep_packet_received};
272 static const struct netflood_callbacks rreq_callbacks = {rreq_packet_received,
NULL, NULL};
275 route_discovery_open(
struct route_discovery_conn *c,
278 const struct route_discovery_callbacks *callbacks)
280 netflood_open(&c->rreqconn, time, channels + 0, &rreq_callbacks);
281 unicast_open(&c->rrepconn, channels + 1, &rrep_callbacks);
286 route_discovery_close(
struct route_discovery_conn *c)
288 unicast_close(&c->rrepconn);
289 netflood_close(&c->rreqconn);
294 timeout_handler(
void *ptr)
296 struct route_discovery_conn *c = ptr;
297 PRINTF(
"route_discovery: timeout, timed out\n");
299 if(c->cb->timedout) {
305 route_discovery_discover(
struct route_discovery_conn *c,
const rimeaddr_t *addr,
306 clock_time_t timeout)
309 PRINTF(
"route_discovery_send: ignoring request because of pending response\n");
313 PRINTF(
"route_discovery_send: sending route request\n");
314 ctimer_set(&c->t, timeout, timeout_handler, c);