Contiki 2.6

frame802154.h

Go to the documentation of this file.
00001 /*
00002  *  Copyright (c) 2008, Swedish Institute of Computer Science
00003  *  All rights reserved.
00004  *
00005  *  Additional fixes for AVR contributed by:
00006  *        Colin O'Flynn coflynn@newae.com
00007  *        Eric Gnoske egnoske@gmail.com
00008  *        Blake Leverett bleverett@gmail.com
00009  *        Mike Vidales mavida404@gmail.com
00010  *        Kevin Brown kbrown3@uccs.edu
00011  *        Nate Bohlmann nate@elfwerks.com
00012  *
00013  *  Additional fixes for MSP430 contributed by:
00014  *        Joakim Eriksson
00015  *        Niclas Finne
00016  *        Nicolas Tsiftes
00017  *
00018  *   All rights reserved.
00019  *
00020  *   Redistribution and use in source and binary forms, with or without
00021  *   modification, are permitted provided that the following conditions are met:
00022  *
00023  *   * Redistributions of source code must retain the above copyright
00024  *     notice, this list of conditions and the following disclaimer.
00025  *   * Redistributions in binary form must reproduce the above copyright
00026  *     notice, this list of conditions and the following disclaimer in
00027  *     the documentation and/or other materials provided with the
00028  *     distribution.
00029  *   * Neither the name of the copyright holders nor the names of
00030  *     contributors may be used to endorse or promote products derived
00031  *     from this software without specific prior written permission.
00032  *
00033  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00034  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00035  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00036  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00037  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00038  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00039  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00040  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00041  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00042  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00043  *  POSSIBILITY OF SUCH DAMAGE.
00044 */
00045 
00046 /**
00047  *    \addtogroup frame802154
00048  *    @{
00049  */
00050 /**
00051  *  \file
00052  *  \brief 802.15.4 frame creation and parsing functions
00053  *
00054  *  This file converts to and from a structure to a packed 802.15.4
00055  *  frame.
00056  *
00057  *    $Id: frame802154.h,v 1.3 2010/02/18 21:00:28 adamdunkels Exp $
00058 */
00059 
00060 
00061 /* Includes */
00062 #ifndef FRAME_802154_H
00063 #define FRAME_802154_H
00064 
00065 #include "contiki-conf.h"
00066 #include "net/rime/rimeaddr.h"
00067 
00068 #ifdef IEEE802154_CONF_PANID
00069 #define IEEE802154_PANID           IEEE802154_CONF_PANID
00070 #else
00071 #define IEEE802154_PANID           0xABCD
00072 #endif
00073 
00074 /* Macros & Defines */
00075 
00076 /** \brief These are some definitions of values used in the FCF.  See the 802.15.4 spec for details.
00077  *  \name FCF element values definitions
00078  *  @{
00079  */
00080 #define FRAME802154_BEACONFRAME     (0x00)
00081 #define FRAME802154_DATAFRAME       (0x01)
00082 #define FRAME802154_ACKFRAME        (0x02)
00083 #define FRAME802154_CMDFRAME        (0x03)
00084 
00085 #define FRAME802154_BEACONREQ       (0x07)
00086 
00087 #define FRAME802154_IEEERESERVED    (0x00)
00088 #define FRAME802154_NOADDR          (0x00)      /**< Only valid for ACK or Beacon frames. */
00089 #define FRAME802154_SHORTADDRMODE   (0x02)
00090 #define FRAME802154_LONGADDRMODE    (0x03)
00091 
00092 #define FRAME802154_NOBEACONS       (0x0F)
00093 
00094 #define FRAME802154_BROADCASTADDR   (0xFFFF)
00095 #define FRAME802154_BROADCASTPANDID (0xFFFF)
00096 
00097 #define FRAME802154_IEEE802154_2003 (0x00)
00098 #define FRAME802154_IEEE802154_2006 (0x01)
00099 
00100 #define FRAME802154_SECURITY_LEVEL_NONE (0)
00101 #define FRAME802154_SECURITY_LEVEL_128  (3)
00102 
00103 
00104 /**
00105  *    @brief  The IEEE 802.15.4 frame has a number of constant/fixed fields that
00106  *            can be counted to make frame construction and max payload
00107  *            calculations easier.
00108  *
00109  *            These include:
00110  *            1. FCF                  - 2 bytes       - Fixed
00111  *            2. Sequence number      - 1 byte        - Fixed
00112  *            3. Addressing fields    - 4 - 20 bytes  - Variable
00113  *            4. Aux security header  - 0 - 14 bytes  - Variable
00114  *            5. CRC                  - 2 bytes       - Fixed
00115 */
00116 
00117 /**
00118  * \brief Defines the bitfields of the frame control field (FCF).
00119  */
00120 typedef struct {
00121   uint8_t frame_type;        /**< 3 bit. Frame type field, see 802.15.4 */
00122   uint8_t security_enabled;  /**< 1 bit. True if security is used in this frame */
00123   uint8_t frame_pending;     /**< 1 bit. True if sender has more data to send */
00124   uint8_t ack_required;      /**< 1 bit. Is an ack frame required? */
00125   uint8_t panid_compression; /**< 1 bit. Is this a compressed header? */
00126   /*   uint8_t reserved; */  /**< 3 bit. Unused bits */
00127   uint8_t dest_addr_mode;    /**< 2 bit. Destination address mode, see 802.15.4 */
00128   uint8_t frame_version;     /**< 2 bit. 802.15.4 frame version */
00129   uint8_t src_addr_mode;     /**< 2 bit. Source address mode, see 802.15.4 */
00130 } frame802154_fcf_t;
00131 
00132 /** \brief 802.15.4 security control bitfield.  See section 7.6.2.2.1 in 802.15.4 specification */
00133 typedef struct {
00134   uint8_t  security_level; /**< 3 bit. security level      */
00135   uint8_t  key_id_mode;    /**< 2 bit. Key identifier mode */
00136   uint8_t  reserved;       /**< 3 bit. Reserved bits       */
00137 } frame802154_scf_t;
00138 
00139 /** \brief 802.15.4 Aux security header */
00140 typedef struct {
00141   frame802154_scf_t security_control;  /**< Security control bitfield */
00142   uint32_t frame_counter;   /**< Frame counter, used for security */
00143   uint8_t  key[9];          /**< The key itself, or an index to the key */
00144 } frame802154_aux_hdr_t;
00145 
00146 /** \brief Parameters used by the frame802154_create() function.  These
00147  *  parameters are used in the 802.15.4 frame header.  See the 802.15.4
00148  *  specification for details.
00149  */
00150 typedef struct {
00151   frame802154_fcf_t fcf;            /**< Frame control field  */
00152   uint8_t seq;          /**< Sequence number */
00153   uint16_t dest_pid;    /**< Destination PAN ID */
00154   uint8_t dest_addr[8];     /**< Destination address */
00155   uint16_t src_pid;     /**< Source PAN ID */
00156   uint8_t src_addr[8];      /**< Source address */
00157   frame802154_aux_hdr_t aux_hdr;    /**< Aux security header */
00158   uint8_t *payload;     /**< Pointer to 802.15.4 frame payload */
00159   uint8_t payload_len;  /**< Length of payload field */
00160 } frame802154_t;
00161 
00162 /* Prototypes */
00163 
00164 uint8_t frame802154_hdrlen(frame802154_t *p);
00165 uint8_t frame802154_create(frame802154_t *p, uint8_t *buf, uint8_t buf_len);
00166 uint8_t frame802154_parse(uint8_t *data, uint8_t length, frame802154_t *pf);
00167 
00168 /** @} */
00169 #endif /* FRAME_802154_H */