Contiki 2.5
multihop.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimemh
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: multihop.c,v 1.7 2009/11/08 19:40:17 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Multihop forwarding
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "contiki.h"
47 #include "net/rime.h"
48 #include "net/rime/multihop.h"
49 #include "net/rime/route.h"
50 
51 #include <string.h>
52 
53 static const struct packetbuf_attrlist attributes[] =
54  {
55  MULTIHOP_ATTRIBUTES
56  PACKETBUF_ATTR_LAST
57  };
58 
59 #define DEBUG 0
60 #if DEBUG
61 #include <stdio.h>
62 #define PRINTF(...) printf(__VA_ARGS__)
63 #else
64 #define PRINTF(...)
65 #endif
66 
67 /*---------------------------------------------------------------------------*/
68 void
69 data_packet_received(struct unicast_conn *uc, const rimeaddr_t *from)
70 {
71  struct multihop_conn *c = (struct multihop_conn *)uc;
72  rimeaddr_t *nexthop;
73  rimeaddr_t sender, receiver;
74 
75  /* Copy the packet attributes to avoid them being overwritten or
76  cleared by an application program that uses the packet buffer for
77  its own needs. */
78  rimeaddr_copy(&sender, packetbuf_addr(PACKETBUF_ADDR_ESENDER));
79  rimeaddr_copy(&receiver, packetbuf_addr(PACKETBUF_ADDR_ERECEIVER));
80 
81  PRINTF("data_packet_received from %d.%d towards %d.%d len %d\n",
82  from->u8[0], from->u8[1],
83  packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)->u8[0],
84  packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)->u8[1],
86 
87  if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_ERECEIVER),
89  PRINTF("for us!\n");
90  if(c->cb->recv) {
91  c->cb->recv(c, &sender, from,
92  packetbuf_attr(PACKETBUF_ATTR_HOPS));
93  }
94  } else {
95  nexthop = NULL;
96  if(c->cb->forward) {
97  packetbuf_set_attr(PACKETBUF_ATTR_HOPS,
98  packetbuf_attr(PACKETBUF_ATTR_HOPS) + 1);
99  nexthop = c->cb->forward(c, &sender, &receiver,
100  from, packetbuf_attr(PACKETBUF_ATTR_HOPS) - 1);
101  }
102  if(nexthop) {
103  PRINTF("forwarding to %d.%d\n", nexthop->u8[0], nexthop->u8[1]);
104  unicast_send(&c->c, nexthop);
105  }
106  }
107 }
108 /*---------------------------------------------------------------------------*/
109 static const struct unicast_callbacks data_callbacks = { data_packet_received };
110 /*---------------------------------------------------------------------------*/
111 void
112 multihop_open(struct multihop_conn *c, uint16_t channel,
113  const struct multihop_callbacks *callbacks)
114 {
115  unicast_open(&c->c, channel, &data_callbacks);
116  channel_set_attributes(channel, attributes);
117  c->cb = callbacks;
118 }
119 /*---------------------------------------------------------------------------*/
120 void
121 multihop_close(struct multihop_conn *c)
122 {
123  unicast_close(&c->c);
124 }
125 /*---------------------------------------------------------------------------*/
126 int
127 multihop_send(struct multihop_conn *c, const rimeaddr_t *to)
128 {
129  rimeaddr_t *nexthop;
130 
131  if(c->cb->forward == NULL) {
132  return 0;
133  }
135  packetbuf_set_addr(PACKETBUF_ADDR_ERECEIVER, to);
136  packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &rimeaddr_node_addr);
137  packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
138  nexthop = c->cb->forward(c, &rimeaddr_node_addr, to, NULL, 0);
139 
140  if(nexthop == NULL) {
141  PRINTF("multihop_send: no route\n");
142  return 0;
143  } else {
144  PRINTF("multihop_send: sending data towards %d.%d\n",
145  nexthop->u8[0], nexthop->u8[1]);
146  unicast_send(&c->c, nexthop);
147  return 1;
148  }
149 }
150 /*---------------------------------------------------------------------------*/
151 void
152 multihop_resend(struct multihop_conn *c, const rimeaddr_t *nexthop)
153 {
154  unicast_send(&c->c, nexthop);
155 }
156 /*---------------------------------------------------------------------------*/
157 /** @} */