Contiki 2.5
rmh.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimermh
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: rmh.c,v 1.10 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/rmh.h"
49 
50 struct data_hdr {
51  rimeaddr_t dest;
52  rimeaddr_t originator;
53  uint8_t hops;
54  uint8_t max_rexmits;
55 };
56 
57 #define DEBUG 0
58 #if DEBUG
59 #include <stdio.h>
60 #define PRINTF(...) printf(__VA_ARGS__)
61 #else
62 #define PRINTF(...)
63 #endif
64 
65 /*---------------------------------------------------------------------------*/
66 static void
67 received(struct runicast_conn *uc, const rimeaddr_t *from, uint8_t seqno)
68 {
69  struct rmh_conn *c = (struct rmh_conn *)uc;
70  struct data_hdr *msg = packetbuf_dataptr();
71  rimeaddr_t *nexthop;
72 
73  PRINTF("data_packet_received from %d.%d towards %d.%d len %d\n",
74  from->u8[0], from->u8[1],
75  msg->dest.u8[0], msg->dest.u8[1],
77 
78  if(rimeaddr_cmp(&msg->dest, &rimeaddr_node_addr)) {
79  PRINTF("for us!\n");
80  packetbuf_hdrreduce(sizeof(struct data_hdr));
81  if(c->cb->recv) {
82  c->cb->recv(c, &msg->originator, msg->hops);
83  }
84  } else {
85  nexthop = NULL;
86  if(c->cb->forward) {
87  nexthop = c->cb->forward(c, &msg->originator,
88  &msg->dest, from, msg->hops);
89  }
90  if(nexthop) {
91  PRINTF("forwarding to %d.%d\n", nexthop->u8[0], nexthop->u8[1]);
92  msg->hops++;
93  runicast_send(&c->c, nexthop, c->num_rexmit);
94  }
95  }
96 }
97 /*---------------------------------------------------------------------------*/
98 static void
99 sent(struct runicast_conn *c, const rimeaddr_t *to, uint8_t retransmissions)
100 {
101 
102 }
103 /*---------------------------------------------------------------------------*/
104 static void
105 timedout(struct runicast_conn *c, const rimeaddr_t *to, uint8_t retransmissions)
106 {
107 
108 }
109 /*---------------------------------------------------------------------------*/
110 static const struct runicast_callbacks data_callbacks = { received ,
111  sent,
112  timedout};
113 /*---------------------------------------------------------------------------*/
114 void
115 rmh_open(struct rmh_conn *c, uint16_t channel,
116  const struct rmh_callbacks *callbacks)
117 {
118  runicast_open(&c->c, channel, &data_callbacks);
119  c->cb = callbacks;
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 rmh_close(struct rmh_conn *c)
124 {
125  runicast_close(&c->c);
126 }
127 /*---------------------------------------------------------------------------*/
128 int
129 rmh_send(struct rmh_conn *c, rimeaddr_t *to, uint8_t num_rexmit, uint8_t max_hops)
130 {
131  rimeaddr_t *nexthop;
132  struct data_hdr *hdr;
133 
134  c->num_rexmit = num_rexmit;
135 
136  if(c->cb->forward == NULL) {
137  return 0;
138  }
139 
140  nexthop = c->cb->forward(c, &rimeaddr_node_addr, to, NULL, 0);
141  if(nexthop == NULL) {
142  PRINTF("rmh_send: no route\n");
143  return 0;
144  } else {
145  PRINTF("rmh_send: sending data\n");
146 
147 
148  if(packetbuf_hdralloc(sizeof(struct data_hdr))) {
149  hdr = packetbuf_hdrptr();
150  rimeaddr_copy(&hdr->dest, to);
151  rimeaddr_copy(&hdr->originator, &rimeaddr_node_addr);
152  hdr->hops = 1;
153  hdr->max_rexmits = num_rexmit;
154  runicast_send(&c->c, nexthop, num_rexmit);
155  }
156  return 1;
157  }
158 }
159 /*---------------------------------------------------------------------------*/
160 /** @} */