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