Contiki-Inga 3.x
framer-802154.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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  */
30 
31 /**
32  * \file
33  * MAC framer for IEEE 802.15.4
34  * \author
35  * Niclas Finne <nfi@sics.se>
36  * Joakim Eriksson <joakime@sics.se>
37  */
38 
39 #include "net/mac/framer-802154.h"
40 #include "net/mac/frame802154.h"
41 #include "net/packetbuf.h"
42 #include "lib/random.h"
43 #include <string.h>
44 
45 #define DEBUG 0
46 
47 #if DEBUG
48 #include <stdio.h>
49 #define PRINTF(...) printf(__VA_ARGS__)
50 #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])
51 #else
52 #define PRINTF(...)
53 #define PRINTADDR(addr)
54 #endif
55 
56 /** \brief The sequence number (0x00 - 0xff) added to the transmitted
57  * data or MAC command frame. The default is a random value within
58  * the range.
59  */
60 static uint8_t mac_dsn;
61 
62 static uint8_t initialized = 0;
63 
64 /** \brief The 16-bit identifier of the PAN on which the device is
65  * sending to. If this value is 0xffff, the device is not
66  * associated.
67  */
68 static const uint16_t mac_dst_pan_id = IEEE802154_PANID;
69 
70 /** \brief The 16-bit identifier of the PAN on which the device is
71  * operating. If this value is 0xffff, the device is not
72  * associated.
73  */
74 static const uint16_t mac_src_pan_id = IEEE802154_PANID;
75 
76 /*---------------------------------------------------------------------------*/
77 static int
78 is_broadcast_addr(uint8_t mode, uint8_t *addr)
79 {
80  int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
81  while(i-- > 0) {
82  if(addr[i] != 0xff) {
83  return 0;
84  }
85  }
86  return 1;
87 }
88 /*---------------------------------------------------------------------------*/
89 static int
90 create(void)
91 {
92  frame802154_t params;
93  int len;
94 
95  /* init to zeros */
96  memset(&params, 0, sizeof(params));
97 
98  if(!initialized) {
99  initialized = 1;
100  mac_dsn = random_rand() & 0xff;
101  printf("***mac_dsn: %d\n");
102  }
103 
104  /* Build the FCF. */
105  params.fcf.frame_type = FRAME802154_DATAFRAME;
106  params.fcf.security_enabled = 0;
107  params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
108  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_null)) {
109  params.fcf.ack_required = 0;
110  } else {
111  params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK);
112  }
113  params.fcf.panid_compression = 0;
114 
115  /* Insert IEEE 802.15.4 (2003) version bit. */
116  params.fcf.frame_version = FRAME802154_IEEE802154_2003;
117 
118  /* Increment and set the data sequence number. */
119  if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
120  params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
121  } else {
122  params.seq = mac_dsn++;
123  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq);
124  }
125 /* params.seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); */
126 
127  /* Complete the addressing fields. */
128  /**
129  \todo For phase 1 the addresses are all long. We'll need a mechanism
130  in the rime attributes to tell the mac to use long or short for phase 2.
131  */
132  if(sizeof(linkaddr_t) == 2) {
133  /* Use short address mode if linkaddr size is short. */
134  params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
135  } else {
136  params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
137  }
138  params.dest_pid = mac_dst_pan_id;
139 
140  /*
141  * If the output address is NULL in the Rime buf, then it is broadcast
142  * on the 802.15.4 network.
143  */
144  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_null)) {
145  /* Broadcast requires short address mode. */
146  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
147  params.dest_addr[0] = 0xFF;
148  params.dest_addr[1] = 0xFF;
149 
150  } else {
151  linkaddr_copy((linkaddr_t *)&params.dest_addr,
152  packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
153  /* Use short address mode if linkaddr size is small */
154  if(sizeof(linkaddr_t) == 2) {
155  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
156  } else {
157  params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
158  }
159  }
160 
161  /* Set the source PAN ID to the global variable. */
162  params.src_pid = mac_src_pan_id;
163 
164  /*
165  * Set up the source address using only the long address mode for
166  * phase 1.
167  */
168  linkaddr_copy((linkaddr_t *)&params.src_addr, &linkaddr_node_addr);
169 
170  params.payload = packetbuf_dataptr();
171  params.payload_len = packetbuf_datalen();
172  len = frame802154_hdrlen(&params);
173  if(packetbuf_hdralloc(len)) {
174  frame802154_create(&params, packetbuf_hdrptr(), len);
175 
176  PRINTF("15.4-OUT: %2X", params.fcf.frame_type);
177  PRINTADDR(params.dest_addr);
178  PRINTF("%d %u (%u)\n", len, packetbuf_datalen(), packetbuf_totlen());
179 
180  return len;
181  } else {
182  PRINTF("15.4-OUT: too large header: %u\n", len);
183  return FRAMER_FAILED;
184  }
185 }
186 /*---------------------------------------------------------------------------*/
187 static int
188 parse(void)
189 {
190  frame802154_t frame;
191  int len;
192  len = packetbuf_datalen();
193  if(frame802154_parse(packetbuf_dataptr(), len, &frame) &&
194  packetbuf_hdrreduce(len - frame.payload_len)) {
195  if(frame.fcf.dest_addr_mode) {
196  if(frame.dest_pid != mac_src_pan_id &&
197  frame.dest_pid != FRAME802154_BROADCASTPANDID) {
198  /* Packet to another PAN */
199  PRINTF("15.4: for another pan %u\n", frame.dest_pid);
200  return FRAMER_FAILED;
201  }
202  if(!is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
203  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
204  }
205  }
206  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
207  packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending);
208  /* packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, frame.fcf.ack_required);*/
209  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
210 
211  PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
212  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
213  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
214  PRINTF("%u (%u)\n", packetbuf_datalen(), len);
215 
216  return len - frame.payload_len;
217  }
218  return FRAMER_FAILED;
219 }
220 /*---------------------------------------------------------------------------*/
221 const struct framer framer_802154 = {
222  create, parse
223 };