framer-802154.c

Go to the documentation of this file.
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 static uint8_t mac_dsn;
00058 static uint8_t initialized = 0;
00059 static const uint16_t mac_dst_pan_id = IEEE802154_PANID;
00060 static const uint16_t mac_src_pan_id = IEEE802154_PANID;
00061 
00062 /*---------------------------------------------------------------------------*/
00063 static int
00064 is_broadcast_addr(uint8_t mode, uint8_t *addr)
00065 {
00066   int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
00067   while(i-- > 0) {
00068     if(addr[i] != 0xff) {
00069       return 0;
00070     }
00071   }
00072   return 1;
00073 }
00074 /*---------------------------------------------------------------------------*/
00075 static int
00076 create(void)
00077 {
00078   frame802154_t params;
00079   uint8_t len;
00080 
00081   /* init to zeros */
00082   memset(&params, 0, sizeof(params));
00083 
00084   if(!initialized) {
00085     initialized = 1;
00086     mac_dsn = random_rand() & 0xff;
00087   }
00088 
00089   /* Build the FCF. */
00090   params.fcf.frame_type = FRAME802154_DATAFRAME;
00091   params.fcf.security_enabled = 0;
00092   params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
00093   if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) {
00094     params.fcf.ack_required = 0;
00095   } else {
00096     params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK);
00097   }
00098   params.fcf.panid_compression = 0;
00099 
00100   /* Insert IEEE 802.15.4 (2003) version bit. */
00101   params.fcf.frame_version = FRAME802154_IEEE802154_2003;
00102 
00103   /* Increment and set the data sequence number. */
00104   if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
00105     params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
00106   } else {
00107     params.seq = mac_dsn++;
00108     packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq);
00109   }
00110 /*   params.seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); */
00111 
00112   /* Complete the addressing fields. */
00113   /**
00114      \todo For phase 1 the addresses are all long. We'll need a mechanism
00115      in the rime attributes to tell the mac to use long or short for phase 2.
00116   */
00117   if(sizeof(rimeaddr_t) == 2) {
00118     /* Use short address mode if rimeaddr size is short. */
00119     params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
00120   } else {
00121     params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
00122   }
00123   params.dest_pid = mac_dst_pan_id;
00124 
00125   /*
00126    *  If the output address is NULL in the Rime buf, then it is broadcast
00127    *  on the 802.15.4 network.
00128    */
00129   if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) {
00130     /* Broadcast requires short address mode. */
00131     params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
00132     params.dest_addr[0] = 0xFF;
00133     params.dest_addr[1] = 0xFF;
00134 
00135   } else {
00136     rimeaddr_copy((rimeaddr_t *)&params.dest_addr,
00137                   packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
00138     /* Use short address mode if rimeaddr size is small */
00139     if(sizeof(rimeaddr_t) == 2) {
00140       params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
00141     } else {
00142       params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
00143     }
00144   }
00145 
00146   /* Set the source PAN ID to the global variable. */
00147   params.src_pid = mac_src_pan_id;
00148   
00149   /*
00150    * Set up the source address using only the long address mode for
00151    * phase 1.
00152    */
00153   rimeaddr_copy((rimeaddr_t *)&params.src_addr, &rimeaddr_node_addr);
00154 
00155   params.payload = packetbuf_dataptr();
00156   params.payload_len = packetbuf_datalen();
00157   len = frame802154_hdrlen(&params);
00158   if(packetbuf_hdralloc(len)) {
00159     frame802154_create(&params, packetbuf_hdrptr(), len);
00160 
00161     PRINTF("15.4-OUT: %2X", params.fcf.frame_type);
00162     PRINTADDR(params.dest_addr.u8);
00163     PRINTF("%u %u (%u)\n", len, packetbuf_datalen(), packetbuf_totlen());
00164 
00165     return len;
00166   } else {
00167     PRINTF("15.4-OUT: too large header: %u\n", len);
00168     return 0;
00169   }
00170 }
00171 /*---------------------------------------------------------------------------*/
00172 static int
00173 parse(void)
00174 {
00175   frame802154_t frame;
00176   int len;
00177   len = packetbuf_datalen();
00178   if(frame802154_parse(packetbuf_dataptr(), len, &frame) &&
00179      packetbuf_hdrreduce(len - frame.payload_len)) {
00180     if(frame.fcf.dest_addr_mode) {
00181       if(frame.dest_pid != mac_src_pan_id &&
00182          frame.dest_pid != FRAME802154_BROADCASTPANDID) {
00183         /* Packet to another PAN */
00184         PRINTF("15.4: for another pan %u\n", frame.dest_pid);
00185         return 0;
00186       }
00187       if(!is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
00188         packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (rimeaddr_t *)&frame.dest_addr);
00189       }
00190     }
00191     packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (rimeaddr_t *)&frame.src_addr);
00192     packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending);
00193     /*    packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, frame.fcf.ack_required);*/
00194     packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
00195 
00196     PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
00197     PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
00198     PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
00199     PRINTF("%u (%u)\n", packetbuf_datalen(), len);
00200 
00201     return len - frame.payload_len;
00202   }
00203   return 0;
00204 }
00205 /*---------------------------------------------------------------------------*/
00206 const struct framer framer_802154 = {
00207   create, parse
00208 };

Generated on Mon Apr 11 14:23:29 2011 for Contiki 2.5 by  doxygen 1.6.1