Contiki 2.5
nullrdc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 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  * $Id: nullrdc.c,v 1.4 2010/11/23 18:11:00 nifi Exp $
32  */
33 
34 /**
35  * \file
36  * A null RDC implementation that uses framer for headers.
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  * Niclas Finne <nfi@sics.se>
40  */
41 
42 #include "net/mac/nullrdc.h"
43 #include "net/packetbuf.h"
44 #include "net/netstack.h"
45 #include <string.h>
46 
47 #define DEBUG 0
48 #if DEBUG
49 #include <stdio.h>
50 #define PRINTF(...) printf(__VA_ARGS__)
51 #else
52 #define PRINTF(...)
53 #endif
54 
55 #ifdef NULLRDC_CONF_ADDRESS_FILTER
56 #define NULLRDC_ADDRESS_FILTER NULLRDC_CONF_ADDRESS_FILTER
57 #else
58 #define NULLRDC_ADDRESS_FILTER 1
59 #endif /* NULLRDC_CONF_ADDRESS_FILTER */
60 #define NULLRDC_802154_AUTOACK 1
61 
62 #ifndef NULLRDC_802154_AUTOACK
63 #ifdef NULLRDC_CONF_802154_AUTOACK
64 #define NULLRDC_802154_AUTOACK NULLRDC_CONF_802154_AUTOACK
65 #else
66 #define NULLRDC_802154_AUTOACK 0
67 #endif /* NULLRDC_CONF_802154_AUTOACK */
68 #endif /* NULLRDC_802154_AUTOACK */
69 
70 #ifndef NULLRDC_802154_AUTOACK_HW
71 #ifdef NULLRDC_CONF_802154_AUTOACK_HW
72 #define NULLRDC_802154_AUTOACK_HW NULLRDC_CONF_802154_AUTOACK_HW
73 #else
74 #define NULLRDC_802154_AUTOACK_HW 0
75 #endif /* NULLRDC_CONF_802154_AUTOACK_HW */
76 #endif /* NULLRDC_802154_AUTOACK_HW */
77 
78 #if NULLRDC_802154_AUTOACK
79 #include "sys/rtimer.h"
80 #include "dev/watchdog.h"
81 
82 #define ACK_WAIT_TIME RTIMER_SECOND / 2500
83 #define AFTER_ACK_DETECTED_WAIT_TIME RTIMER_SECOND / 1500
84 #define ACK_LEN 3
85 #endif /* NULLRDC_802154_AUTOACK */
86 
87 #if NULLRDC_802154_AUTOACK || NULLRDC_802154_AUTOACK_HW
88 struct seqno {
89  rimeaddr_t sender;
90  uint8_t seqno;
91 };
92 
93 #ifdef NETSTACK_CONF_MAC_SEQNO_HISTORY
94 #define MAX_SEQNOS NETSTACK_CONF_MAC_SEQNO_HISTORY
95 #else /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
96 #define MAX_SEQNOS 16
97 #endif /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
98 
99 static struct seqno received_seqnos[MAX_SEQNOS];
100 #endif /* NULLRDC_802154_AUTOACK || NULLRDC_802154_AUTOACK_HW */
101 
102 /*---------------------------------------------------------------------------*/
103 static void
104 send_packet(mac_callback_t sent, void *ptr)
105 {
106  int ret;
107  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
108 #if NULLRDC_802154_AUTOACK || NULLRDC_802154_AUTOACK_HW
109  packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
110 #endif /* NULLRDC_802154_AUTOACK || NULLRDC_802154_AUTOACK_HW */
111 
112  if(NETSTACK_FRAMER.create() == 0) {
113  /* Failed to allocate space for headers */
114  PRINTF("nullrdc: send failed, too large header\n");
115  ret = MAC_TX_ERR_FATAL;
116  } else {
117 
118 #if NULLRDC_802154_AUTOACK
119  int is_broadcast;
120  uint8_t dsn;
121  dsn = ((uint8_t *)packetbuf_hdrptr())[2] & 0xff;
122 
123  NETSTACK_RADIO.prepare(packetbuf_hdrptr(), packetbuf_totlen());
124 
125  is_broadcast = rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
126  &rimeaddr_null);
127 
128  if(NETSTACK_RADIO.receiving_packet() ||
129  (!is_broadcast && NETSTACK_RADIO.pending_packet())) {
130 
131  /* Currently receiving a packet over air or the radio has
132  already received a packet that needs to be read before
133  sending with auto ack. */
134  ret = MAC_TX_COLLISION;
135 
136  } else {
137  switch(NETSTACK_RADIO.transmit(packetbuf_totlen())) {
138  case RADIO_TX_OK:
139  if(is_broadcast) {
140  ret = MAC_TX_OK;
141  } else {
142  rtimer_clock_t wt;
143 
144  /* Check for ack */
145  wt = RTIMER_NOW();
146  watchdog_periodic();
147  while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + ACK_WAIT_TIME));
148 
149  ret = MAC_TX_NOACK;
150  if(NETSTACK_RADIO.receiving_packet() ||
151  NETSTACK_RADIO.pending_packet() ||
152  NETSTACK_RADIO.channel_clear() == 0) {
153  int len;
154  uint8_t ackbuf[ACK_LEN];
155 
156  wt = RTIMER_NOW();
157  watchdog_periodic();
158  while(RTIMER_CLOCK_LT(RTIMER_NOW(),
159  wt + AFTER_ACK_DETECTED_WAIT_TIME));
160 
161  if(NETSTACK_RADIO.pending_packet()) {
162  len = NETSTACK_RADIO.read(ackbuf, ACK_LEN);
163  if(len == ACK_LEN && ackbuf[2] == dsn) {
164  /* Ack received */
165  ret = MAC_TX_OK;
166  } else {
167  /* Not an ack or ack not for us: collision */
168  ret = MAC_TX_COLLISION;
169  }
170  }
171  }
172  }
173  break;
174  case RADIO_TX_COLLISION:
175  ret = MAC_TX_COLLISION;
176  break;
177  default:
178  ret = MAC_TX_ERR;
179  break;
180  }
181  }
182 
183 #else /* ! NULLRDC_802154_AUTOACK */
184 
185  switch(NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen())) {
186  case RADIO_TX_OK:
187  ret = MAC_TX_OK;
188  break;
189  case RADIO_TX_COLLISION:
190  ret = MAC_TX_COLLISION;
191  break;
192  case RADIO_TX_NOACK:
193  ret = MAC_TX_NOACK;
194  break;
195  default:
196  ret = MAC_TX_ERR;
197  break;
198  }
199 
200 #endif /* ! NULLRDC_802154_AUTOACK */
201  }
202  mac_call_sent_callback(sent, ptr, ret, 1);
203 }
204 /*---------------------------------------------------------------------------*/
205 static void
206 packet_input(void)
207 {
208  PRINTF("nullrdc: got packet\n");
209 #if NULLRDC_802154_AUTOACK
210  if(packetbuf_datalen() == ACK_LEN) {
211  /* Ignore ack packets */
212  /* PRINTF("nullrdc: ignored ack\n"); */
213  } else
214 #endif /* NULLRDC_802154_AUTOACK */
215  if(NETSTACK_FRAMER.parse() == 0) {
216  PRINTF("nullrdc: failed to parse %u\n", packetbuf_datalen());
217 #if NULLRDC_ADDRESS_FILTER
218  } else if(!rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
219  &rimeaddr_node_addr) &&
220  !rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
221  &rimeaddr_null)) {
222  PRINTF("nullrdc: not for us\n");
223 #endif /* NULLRDC_ADDRESS_FILTER */
224  } else {
225 #if NULLRDC_802154_AUTOACK || NULLRDC_802154_AUTOACK_HW
226  /* Check for duplicate packet by comparing the sequence number
227  of the incoming packet with the last few ones we saw. */
228  int i;
229  for(i = 0; i < MAX_SEQNOS; ++i) {
230  if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == received_seqnos[i].seqno &&
231  rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
232  &received_seqnos[i].sender)) {
233  /* Drop the packet. */
234  PRINTF("nullrdc: drop duplicate link layer packet %u\n",
235  packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
236  return;
237  }
238  }
239  for(i = MAX_SEQNOS - 1; i > 0; --i) {
240  memcpy(&received_seqnos[i], &received_seqnos[i - 1],
241  sizeof(struct seqno));
242  }
243  received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
244  rimeaddr_copy(&received_seqnos[0].sender,
245  packetbuf_addr(PACKETBUF_ADDR_SENDER));
246 #endif /* NULLRDC_802154_AUTOACK */
247  NETSTACK_MAC.input();
248  }
249 }
250 /*---------------------------------------------------------------------------*/
251 static int
252 on(void)
253 {
254  return NETSTACK_RADIO.on();
255 }
256 /*---------------------------------------------------------------------------*/
257 static int
258 off(int keep_radio_on)
259 {
260  if(keep_radio_on) {
261  return NETSTACK_RADIO.on();
262  } else {
263  return NETSTACK_RADIO.off();
264  }
265 }
266 /*---------------------------------------------------------------------------*/
267 static unsigned short
268 channel_check_interval(void)
269 {
270  return 0;
271 }
272 /*---------------------------------------------------------------------------*/
273 static void
274 init(void)
275 {
276  on();
277 }
278 /*---------------------------------------------------------------------------*/
279 const struct rdc_driver nullrdc_driver = {
280  "nullrdc",
281  init,
282  send_packet,
283  packet_input,
284  on,
285  off,
287 };
288 /*---------------------------------------------------------------------------*/