Contiki 3.x
rmh.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  * Multihop forwarding
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimermh
42  * @{
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 /** @} */
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:172
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
Multihop forwarding header file
#define NULL
The null pointer.
Header file for the Rime stack
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:188