Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2009, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * $Id: framer-802154.c,v 1.12 2010/06/14 19:19:16 adamdunkels Exp $ 00030 */ 00031 00032 /** 00033 * \file 00034 * MAC framer for IEEE 802.15.4 00035 * \author 00036 * Niclas Finne <nfi@sics.se> 00037 * Joakim Eriksson <joakime@sics.se> 00038 */ 00039 00040 #include "net/mac/framer-802154.h" 00041 #include "net/mac/frame802154.h" 00042 #include "net/packetbuf.h" 00043 #include "lib/random.h" 00044 #include <string.h> 00045 00046 #define DEBUG 0 00047 00048 #if DEBUG 00049 #include <stdio.h> 00050 #define PRINTF(...) printf(__VA_ARGS__) 00051 #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]) 00052 #else 00053 #define PRINTF(...) 00054 #define PRINTADDR(addr) 00055 #endif 00056 00057 /** \brief The sequence number (0x00 - 0xff) added to the transmitted 00058 * data or MAC command frame. The default is a random value within 00059 * the range. 00060 */ 00061 static uint8_t mac_dsn; 00062 00063 static uint8_t initialized = 0; 00064 00065 /** \brief The 16-bit identifier of the PAN on which the device is 00066 * sending to. If this value is 0xffff, the device is not 00067 * associated. 00068 */ 00069 static const uint16_t mac_dst_pan_id = IEEE802154_PANID; 00070 00071 /** \brief The 16-bit identifier of the PAN on which the device is 00072 * operating. If this value is 0xffff, the device is not 00073 * associated. 00074 */ 00075 static const uint16_t mac_src_pan_id = IEEE802154_PANID; 00076 00077 /*---------------------------------------------------------------------------*/ 00078 static int 00079 is_broadcast_addr(uint8_t mode, uint8_t *addr) 00080 { 00081 int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8; 00082 while(i-- > 0) { 00083 if(addr[i] != 0xff) { 00084 return 0; 00085 } 00086 } 00087 return 1; 00088 } 00089 /*---------------------------------------------------------------------------*/ 00090 static int 00091 create(void) 00092 { 00093 frame802154_t params; 00094 uint8_t len; 00095 00096 /* init to zeros */ 00097 memset(¶ms, 0, sizeof(params)); 00098 00099 if(!initialized) { 00100 initialized = 1; 00101 mac_dsn = random_rand() & 0xff; 00102 } 00103 00104 /* Build the FCF. */ 00105 params.fcf.frame_type = FRAME802154_DATAFRAME; 00106 params.fcf.security_enabled = 0; 00107 params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING); 00108 if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) { 00109 params.fcf.ack_required = 0; 00110 } else { 00111 params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK); 00112 } 00113 params.fcf.panid_compression = 0; 00114 00115 /* Insert IEEE 802.15.4 (2003) version bit. */ 00116 params.fcf.frame_version = FRAME802154_IEEE802154_2003; 00117 00118 /* Increment and set the data sequence number. */ 00119 if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) { 00120 params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO); 00121 } else { 00122 params.seq = mac_dsn++; 00123 packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq); 00124 } 00125 /* params.seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); */ 00126 00127 /* Complete the addressing fields. */ 00128 /** 00129 \todo For phase 1 the addresses are all long. We'll need a mechanism 00130 in the rime attributes to tell the mac to use long or short for phase 2. 00131 */ 00132 if(sizeof(rimeaddr_t) == 2) { 00133 /* Use short address mode if rimeaddr size is short. */ 00134 params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE; 00135 } else { 00136 params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE; 00137 } 00138 params.dest_pid = mac_dst_pan_id; 00139 00140 /* 00141 * If the output address is NULL in the Rime buf, then it is broadcast 00142 * on the 802.15.4 network. 00143 */ 00144 if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) { 00145 /* Broadcast requires short address mode. */ 00146 params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE; 00147 params.dest_addr[0] = 0xFF; 00148 params.dest_addr[1] = 0xFF; 00149 00150 } else { 00151 rimeaddr_copy((rimeaddr_t *)¶ms.dest_addr, 00152 packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); 00153 /* Use short address mode if rimeaddr size is small */ 00154 if(sizeof(rimeaddr_t) == 2) { 00155 params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE; 00156 } else { 00157 params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE; 00158 } 00159 } 00160 00161 /* Set the source PAN ID to the global variable. */ 00162 params.src_pid = mac_src_pan_id; 00163 00164 /* 00165 * Set up the source address using only the long address mode for 00166 * phase 1. 00167 */ 00168 rimeaddr_copy((rimeaddr_t *)¶ms.src_addr, &rimeaddr_node_addr); 00169 00170 params.payload = packetbuf_dataptr(); 00171 params.payload_len = packetbuf_datalen(); 00172 len = frame802154_hdrlen(¶ms); 00173 if(packetbuf_hdralloc(len)) { 00174 frame802154_create(¶ms, packetbuf_hdrptr(), len); 00175 00176 PRINTF("15.4-OUT: %2X", params.fcf.frame_type); 00177 PRINTADDR(params.dest_addr.u8); 00178 PRINTF("%u %u (%u)\n", len, packetbuf_datalen(), packetbuf_totlen()); 00179 00180 return len; 00181 } else { 00182 PRINTF("15.4-OUT: too large header: %u\n", len); 00183 return FRAMER_FAILED; 00184 } 00185 } 00186 /*---------------------------------------------------------------------------*/ 00187 static int 00188 parse(void) 00189 { 00190 frame802154_t frame; 00191 int len; 00192 len = packetbuf_datalen(); 00193 if(frame802154_parse(packetbuf_dataptr(), len, &frame) && 00194 packetbuf_hdrreduce(len - frame.payload_len)) { 00195 if(frame.fcf.dest_addr_mode) { 00196 if(frame.dest_pid != mac_src_pan_id && 00197 frame.dest_pid != FRAME802154_BROADCASTPANDID) { 00198 /* Packet to another PAN */ 00199 PRINTF("15.4: for another pan %u\n", frame.dest_pid); 00200 return FRAMER_FAILED; 00201 } 00202 if(!is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) { 00203 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (rimeaddr_t *)&frame.dest_addr); 00204 } 00205 } 00206 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (rimeaddr_t *)&frame.src_addr); 00207 packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending); 00208 /* packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, frame.fcf.ack_required);*/ 00209 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq); 00210 00211 PRINTF("15.4-IN: %2X", frame.fcf.frame_type); 00212 PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER)); 00213 PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); 00214 PRINTF("%u (%u)\n", packetbuf_datalen(), len); 00215 00216 return len - frame.payload_len; 00217 } 00218 return FRAMER_FAILED; 00219 } 00220 /*---------------------------------------------------------------------------*/ 00221 const struct framer framer_802154 = { 00222 create, parse 00223 };