Contiki 2.6

frame.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  *   All rights reserved.
00014  *
00015  *   Redistribution and use in source and binary forms, with or without
00016  *   modification, are permitted provided that the following conditions are met:
00017  *
00018  *   * Redistributions of source code must retain the above copyright
00019  *     notice, this list of conditions and the following disclaimer.
00020  *   * Redistributions in binary form must reproduce the above copyright
00021  *     notice, this list of conditions and the following disclaimer in
00022  *     the documentation and/or other materials provided with the
00023  *     distribution.
00024  *   * Neither the name of the copyright holders nor the names of
00025  *     contributors may be used to endorse or promote products derived
00026  *     from this software without specific prior written permission.
00027  *
00028  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00032  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00033  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00034  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00035  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00036  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00037  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038  *  POSSIBILITY OF SUCH DAMAGE.
00039 */
00040 
00041 /**
00042  *    \addtogroup frame
00043  *    @{
00044  */
00045 /**
00046  *  \file
00047  *  \brief 802.15.4 frame creation and parsing functions
00048  *
00049  *  This file converts to and from a structure to a packed 802.15.4
00050  *  frame.
00051  *
00052  *    $Id: frame.h,v 1.2 2008/10/14 18:37:28 c_oflynn Exp $
00053 */
00054 
00055 
00056 
00057 /* Includes */
00058 #ifndef FRAME_UTILS_H
00059 #define FRAME_UTILS_H
00060 
00061 #include "hal.h"
00062 
00063 /* Macros & Defines */
00064 
00065 
00066 /**
00067  * \brief Defines the bitfields of the frame control field (FCF).
00068  */
00069 typedef union{
00070     /** \brief Structure of bitfields for the FCF */
00071     struct{
00072         uint8_t frameType : 3;          /**< Frame type field, see 802.15.4 */
00073         bool    securityEnabled : 1;    /**< True if security is used in this frame */
00074         bool    framePending : 1;       /**< True if sender has more data to send */
00075         bool    ackRequired : 1;        /**< Is an ack frame required? */
00076         bool    panIdCompression : 1;   /**< Is this a compressed header? */
00077         uint8_t reserved : 3;           /**< Unused bits */
00078         uint8_t destAddrMode : 2;       /**< Destination address mode, see 802.15.4 */
00079         uint8_t frameVersion : 2;       /**< 802.15.4 frame version */
00080         uint8_t srcAddrMode : 2;        /**< Source address mode, see 802.15.4 */
00081     };
00082     uint16_t word_val; /**< A word-wide value for the entire FCF */
00083 }fcf_t;
00084 
00085 /**
00086  *  \brief Structure that contains the lengths of the various addressing and security fields
00087  *  in the 802.15.4 header.  This structure is used in \ref frame_tx_create()
00088  */
00089 typedef struct{
00090     uint8_t dest_pid_len;    /**<  Length (in bytes) of destination PAN ID field */
00091     uint8_t dest_addr_len;   /**<  Length (in bytes) of destination address field */
00092     uint8_t src_pid_len;     /**<  Length (in bytes) of source PAN ID field */
00093     uint8_t src_addr_len;    /**<  Length (in bytes) of source address field */
00094     uint8_t aux_sec_len;     /**<  Length (in bytes) of aux security header field */
00095 } field_length_t;
00096 
00097 /** \brief 802.15.4 security control bitfield.  See section 7.6.2.2.1 in 802.15.4 specification */
00098 typedef struct{
00099     uint8_t  security_level : 3; /**< security level      */
00100     uint8_t  key_id_mode    : 2; /**< Key identifier mode */
00101     uint8_t  reserved       : 3; /**< Reserved bits       */
00102 } scf_t;
00103 
00104 /** \brief 802.15.4 Aux security header */
00105 typedef struct{
00106     scf_t  security_control;  /**< Security control bitfield */
00107     uint32_t frame_counter;   /**< Frame counter, used for security */
00108     uint8_t  key[9];          /**< The key itself, or an index to the key */
00109 } aux_hdr_t;
00110 
00111 /**
00112  *    @brief  Some constants for frame length calculations.
00113  *            The IEEE 802.15.4 frame has a number of constant/fixed fields that
00114  *            can be counted to make frame construction and max payload
00115  *            calculations easier.
00116  *
00117  *            These include:
00118  *            1. FCF                  - 2 bytes       - Fixed
00119  *            2. Sequence number      - 1 byte        - Fixed
00120  *            3. Addressing fields    - 4 - 20 bytes  - Variable
00121  *            4. Aux security header  - 0 - 14 bytes  - Variable
00122  *            5. CRC                  - 2 bytes       - Fixed
00123 */
00124 #define FIXEDFRAMEOVERHEAD  (5)
00125 
00126 /** \brief A union of short and long address types. Although a node can have
00127  * both long and short addresses a frame will contain
00128  * only one of these. Therefore, a union is appropriate here. */
00129 typedef union{
00130     uint16_t shortAddr;     /**< Short address, two bytes */
00131     uint64_t longAddr;      /**< Long address, eight bytes */
00132 }ADDR_SIZE_SPEC_t;
00133 
00134 /** \brief Structure containing a PAN ID and an address */
00135 typedef struct{
00136     uint16_t  panId;            /**< PAN ID */
00137     ADDR_SIZE_SPEC_t addrSpec;  /**< A short or long address */
00138 }PAN_ID_ADDR_SPEC_t;
00139 
00140 /** \brief Structure containing both source and destination addresses */
00141 typedef struct{
00142     PAN_ID_ADDR_SPEC_t  destAddrFields; /**< Destination address */
00143     PAN_ID_ADDR_SPEC_t  srcAddrFields;  /**< Source address */
00144 }ADDR_FIELD_SPEC_t;
00145 
00146 /** \brief Union of both short and long addresses */
00147 typedef union{
00148     uint16_t addr16;  /**< Short address */
00149     uint64_t addr64;  /**< Long address */
00150 } addr_t;
00151 
00152 /** \brief Strucure used to return that status of the frame create process.
00153  *   See frame_tx_create() function.*/
00154 typedef struct{
00155     uint8_t *frame; /**< Pointer to created frame */
00156     uint8_t length; /**< Length (in bytes) of created frame */
00157 } frame_result_t;
00158 
00159 /** \brief Parameters used by the frame_tx_create() function.  These
00160  *  parameters are used in the 802.15.4 frame header.  See the 802.15.4
00161  *  specification for details.
00162  */
00163 typedef struct{
00164     fcf_t fcf;            /**< Frame control field  */
00165     uint8_t seq;          /**< Sequence number */
00166     uint16_t dest_pid;    /**< Destination PAN ID */
00167     addr_t dest_addr;     /**< Destination address */
00168     uint16_t src_pid;     /**< Source PAN ID */
00169     addr_t src_addr;      /**< Source address */
00170     aux_hdr_t aux_hdr;    /**< Aux security header */
00171     uint8_t *payload;     /**< Pointer to 802.15.4 frame payload */
00172     uint8_t payload_len;  /**< Length of payload field */
00173 } frame_create_params_t;
00174 
00175 
00176 typedef struct{
00177     fcf_t      * fcf;               /**< The FCF of the frame. */
00178     uint8_t    * seqNum;            /**< The sequence number of the frame. */
00179     uint16_t   * dest_pid;          /**< Destination PAN ID. */
00180     addr_t     * dest_addr;         /**< Destination address. */
00181     uint16_t   * src_pid;           /**< PAN ID */
00182     addr_t     * src_addr;          /**< Source address */
00183     uint8_t    * aux_sec_hdr;       /**< 802.15.4 Aux security header */
00184     uint8_t    * payload;           /**< Frame payload */
00185     uint8_t      payload_length;    /**< Length of payload section of frame */
00186     uint8_t     lqi;                /**< Link quality indication value */
00187     uint8_t     rssi;               /**< Received signal strength indication value */
00188     uint32_t    time;               /**< Time stamp of received frame */
00189     bool   fcs:1;                   /**< True if checksum has passed */
00190     bool   in_use:1;                /**< Is this frame struct being used? */
00191 } parsed_frame_t;
00192 
00193 /* Globals */
00194 
00195 //extern FRAME_t rx_frame;
00196 
00197 /* Protoypes */
00198 
00199 void frame_tx_create(frame_create_params_t *p,frame_result_t *frame_result);
00200 void frame_rx_callback(uint16_t data);
00201 void rx_frame_parse(hal_rx_frame_t *rx_frame, parsed_frame_t *pf);
00202 
00203 /** @} */
00204 #endif /* FRAME_UTILS_H */