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