Contiki 3.x
sicslowmac.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 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 /**
35  * \file
36  * MAC interface for packaging radio packets into 802.15.4 frames
37  *
38  * \author
39  * Adam Dunkels <adam@sics.se>
40  * Eric Gnoske <egnoske@gmail.com>
41  * Blake Leverett <bleverett@gmail.com>
42  * Niclas Finne <nfi@sics.se>
43  * Joakim Eriksson <joakime@sics.se>
44  */
45 
46 #include <string.h>
48 #include "net/mac/frame802154.h"
49 #include "net/packetbuf.h"
50 #include "net/queuebuf.h"
51 #include "net/netstack.h"
52 #include "lib/random.h"
53 
54 #define DEBUG 0
55 
56 #if DEBUG
57 #include <stdio.h>
58 #define PRINTF(...) printf(__VA_ARGS__)
59 #define PRINTADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7])
60 #else
61 #define PRINTF(...)
62 #define PRINTADDR(addr)
63 #endif
64 
65 /** \brief The sequence number (0x00 - 0xff) added to the transmitted
66  * data or MAC command frame. The default is a random value within
67  * the range.
68  */
69 static uint8_t mac_dsn;
70 
71 /** \brief The 16-bit identifier of the PAN on which the device is
72  * sending to. If this value is 0xffff, the device is not
73  * associated.
74  */
75 static uint16_t mac_dst_pan_id = IEEE802154_PANID;
76 
77 /** \brief The 16-bit identifier of the PAN on which the device is
78  * operating. If this value is 0xffff, the device is not
79  * associated.
80  */
81 static uint16_t mac_src_pan_id = IEEE802154_PANID;
82 
83 /*---------------------------------------------------------------------------*/
84 static int
85 is_broadcast_addr(uint8_t mode, uint8_t *addr)
86 {
87  int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
88  while(i-- > 0) {
89  if(addr[i] != 0xff) {
90  return 0;
91  }
92  }
93  return 1;
94 }
95 /*---------------------------------------------------------------------------*/
96 static void
97 send_packet(mac_callback_t sent, void *ptr)
98 {
99  frame802154_t params;
100  uint8_t len;
101 
102  /* init to zeros */
103  memset(&params, 0, sizeof(params));
104 
105  /* Build the FCF. */
106  params.fcf.frame_type = FRAME802154_DATAFRAME;
107  params.fcf.security_enabled = 0;
108  params.fcf.frame_pending = 0;
109  params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_RELIABLE);
110  params.fcf.panid_compression = 0;
111 
112  /* Insert IEEE 802.15.4 (2003) version bit. */
113  params.fcf.frame_version = FRAME802154_IEEE802154_2003;
114 
115  /* Increment and set the data sequence number. */
116  params.seq = mac_dsn++;
117 
118  /* Complete the addressing fields. */
119  /**
120  \todo For phase 1 the addresses are all long. We'll need a mechanism
121  in the rime attributes to tell the mac to use long or short for phase 2.
122  */
123  params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
124  params.dest_pid = mac_dst_pan_id;
125 
126  /*
127  * If the output address is NULL in the Rime buf, then it is broadcast
128  * on the 802.15.4 network.
129  */
130  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_null)) {
131  /* Broadcast requires short address mode. */
132  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
133  params.dest_addr[0] = 0xFF;
134  params.dest_addr[1] = 0xFF;
135 
136  } else {
137  linkaddr_copy((linkaddr_t *)&params.dest_addr,
138  packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
139  params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
140  }
141 
142  /* Set the source PAN ID to the global variable. */
143  params.src_pid = mac_src_pan_id;
144 
145  /*
146  * Set up the source address using only the long address mode for
147  * phase 1.
148  */
149 #if NETSTACK_CONF_BRIDGE_MODE
150  linkaddr_copy((linkaddr_t *)&params.src_addr,packetbuf_addr(PACKETBUF_ADDR_SENDER));
151 #else
152  linkaddr_copy((linkaddr_t *)&params.src_addr, &linkaddr_node_addr);
153 #endif
154 
155  params.payload = packetbuf_dataptr();
156  params.payload_len = packetbuf_datalen();
157  len = frame802154_hdrlen(&params);
158  if(packetbuf_hdralloc(len)) {
159  int ret;
161 
162  PRINTF("6MAC-UT: %2X", params.fcf.frame_type);
163  PRINTADDR(params.dest_addr);
164  PRINTF("%u %u (%u)\n", len, packetbuf_datalen(), packetbuf_totlen());
165 
166  ret = NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
167  if(sent) {
168  switch(ret) {
169  case RADIO_TX_OK:
170  sent(ptr, MAC_TX_OK, 1);
171  break;
172  case RADIO_TX_ERR:
173  sent(ptr, MAC_TX_ERR, 1);
174  break;
175  }
176  }
177  } else {
178  PRINTF("6MAC-UT: too large header: %u\n", len);
179  }
180 }
181 /*---------------------------------------------------------------------------*/
182 void
183 send_list(mac_callback_t sent, void *ptr, struct rdc_buf_list *buf_list)
184 {
185  if(buf_list != NULL) {
186  queuebuf_to_packetbuf(buf_list->buf);
187  send_packet(sent, ptr);
188  }
189 }
190 /*---------------------------------------------------------------------------*/
191 static void
192 input_packet(void)
193 {
194  frame802154_t frame;
195  int len;
196 
197  len = packetbuf_datalen();
198 
199  if(frame802154_parse(packetbuf_dataptr(), len, &frame) &&
200  packetbuf_hdrreduce(len - frame.payload_len)) {
201  if(frame.fcf.dest_addr_mode) {
202  if(frame.dest_pid != mac_src_pan_id &&
203  frame.dest_pid != FRAME802154_BROADCASTPANDID) {
204  /* Not broadcast or for our PAN */
205  PRINTF("6MAC: for another pan %u\n", frame.dest_pid);
206  return;
207  }
208  if(!is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
209  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
210 #if !NETSTACK_CONF_BRIDGE_MODE
211  if(!linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
212  &linkaddr_node_addr)) {
213  /* Not for this node */
214  PRINTF("6MAC: not for us\n");
215  return;
216  }
217 #endif
218  }
219  }
220  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
221 
222  PRINTF("6MAC-IN: %2X", frame.fcf.frame_type);
223  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
224  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
225  PRINTF("%u\n", packetbuf_datalen());
226  NETSTACK_MAC.input();
227  } else {
228  PRINTF("6MAC: failed to parse hdr\n");
229  }
230 }
231 /*---------------------------------------------------------------------------*/
232 static int
233 on(void)
234 {
235  return NETSTACK_RADIO.on();
236 }
237 /*---------------------------------------------------------------------------*/
238 static int
239 off(int keep_radio_on)
240 {
241  if(keep_radio_on) {
242  return NETSTACK_RADIO.on();
243  } else {
244  return NETSTACK_RADIO.off();
245  }
246 }
247 /*---------------------------------------------------------------------------*/
248 static void
249 init(void)
250 {
251  mac_dsn = random_rand() % 256;
252 
253  NETSTACK_RADIO.on();
254 }
255 /*---------------------------------------------------------------------------*/
256 static unsigned short
257 channel_check_interval(void)
258 {
259  return 0;
260 }
261 /*---------------------------------------------------------------------------*/
262 const struct rdc_driver sicslowmac_driver = {
263  "sicslowmac",
264  init,
265  send_packet,
266  send_list,
267  input_packet,
268  on,
269  off,
271 };
272 /*---------------------------------------------------------------------------*/
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:172
uint8_t * payload
Pointer to 802.15.4 payload.
Definition: frame802154.h:186
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
802.15.4 frame creation and parsing functions
uint8_t ack_required
1 bit.
Definition: frame802154.h:137
uint8_t frame_version
2 bit.
Definition: frame802154.h:141
const linkaddr_t linkaddr_null
The null Rime address.
uint8_t frame_type
3 bit.
Definition: frame802154.h:134
int frame802154_hdrlen(frame802154_t *p)
Calculates the length of the frame header.
Definition: frame802154.c:166
Header file for the Rime buffer (packetbuf) management
uint8_t security_enabled
1 bit.
Definition: frame802154.h:135
#define NULL
The null pointer.
int frame802154_create(frame802154_t *p, uint8_t *buf)
Creates a frame for transmission over the air.
Definition: frame802154.c:186
The MAC layer transmission could not be performed because of a fatal error.
Definition: mac.h:93
uint8_t src_addr_mode
2 bit.
Definition: frame802154.h:142
uint8_t panid_compression
1 bit.
Definition: frame802154.h:138
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
MAC interface for packaging radio packets into 802.15.4 frames
Parameters used by the frame802154_create() function.
Definition: frame802154.h:175
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
uint8_t seq
Sequence number.
Definition: frame802154.h:182
int payload_len
Length of payload field.
Definition: frame802154.h:187
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
int(* off)(int keep_radio_on)
Turn the MAC layer off.
Definition: rdc.h:86
Header file for the Rime queue buffer management
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:179
unsigned short(* channel_check_interval)(void)
Returns the channel check interval, expressed in clock_time_t ticks.
Definition: rdc.h:89
The structure of a RDC (radio duty cycling) driver in Contiki.
Definition: rdc.h:67
uint16_t dest_pid
Destination PAN ID.
Definition: frame802154.h:183
void(* init)(void)
Initialize the RDC driver.
Definition: rdc.h:71
uint16_t src_pid
Source PAN ID.
Definition: frame802154.h:184
The MAC layer transmission was OK.
Definition: mac.h:79
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:270
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
int(* on)(void)
Turn the MAC layer on.
Definition: rdc.h:83
uint8_t frame_pending
1 bit.
Definition: frame802154.h:136
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:181
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:188
void(* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list)
Send a packet list.
Definition: rdc.h:77
uint8_t src_addr[8]
Source address.
Definition: frame802154.h:180
Include file for the Contiki low-layer network stack (NETSTACK)
uint8_t dest_addr_mode
&lt; 3 bit.
Definition: frame802154.h:140