Contiki 2.5
frame.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Additional fixes for AVR contributed by:
6  * Colin O'Flynn coflynn@newae.com
7  * Eric Gnoske egnoske@gmail.com
8  * Blake Leverett bleverett@gmail.com
9  * Mike Vidales mavida404@gmail.com
10  * Kevin Brown kbrown3@uccs.edu
11  * Nate Bohlmann nate@elfwerks.com
12  *
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * * Redistributions of source code must retain the above copyright
19  * notice, this list of conditions and the following disclaimer.
20  * * Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  * * Neither the name of the copyright holders nor the names of
25  * contributors may be used to endorse or promote products derived
26  * from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39 */
40 
41 /**
42  * \addtogroup frame
43  * @{
44  */
45 /**
46  * \file
47  * \brief 802.15.4 frame creation and parsing functions
48  *
49  * This file converts to and from a structure to a packed 802.15.4
50  * frame.
51  *
52  * $Id: frame.h,v 1.2 2008/10/14 18:37:28 c_oflynn Exp $
53 */
54 
55 
56 
57 /* Includes */
58 #ifndef FRAME_UTILS_H
59 #define FRAME_UTILS_H
60 
61 #include "hal.h"
62 
63 /* Macros & Defines */
64 
65 
66 /**
67  * \brief Defines the bitfields of the frame control field (FCF).
68  */
69 typedef union{
70  /** \brief Structure of bitfields for the FCF */
71  struct{
72  uint8_t frameType : 3; /**< Frame type field, see 802.15.4 */
73  bool securityEnabled : 1; /**< True if security is used in this frame */
74  bool framePending : 1; /**< True if sender has more data to send */
75  bool ackRequired : 1; /**< Is an ack frame required? */
76  bool panIdCompression : 1; /**< Is this a compressed header? */
77  uint8_t reserved : 3; /**< Unused bits */
78  uint8_t destAddrMode : 2; /**< Destination address mode, see 802.15.4 */
79  uint8_t frameVersion : 2; /**< 802.15.4 frame version */
80  uint8_t srcAddrMode : 2; /**< Source address mode, see 802.15.4 */
81  };
82  uint16_t word_val; /**< A word-wide value for the entire FCF */
83 }fcf_t;
84 
85 /**
86  * \brief Structure that contains the lengths of the various addressing and security fields
87  * in the 802.15.4 header. This structure is used in \ref frame_tx_create()
88  */
89 typedef struct{
90  uint8_t dest_pid_len; /**< Length (in bytes) of destination PAN ID field */
91  uint8_t dest_addr_len; /**< Length (in bytes) of destination address field */
92  uint8_t src_pid_len; /**< Length (in bytes) of source PAN ID field */
93  uint8_t src_addr_len; /**< Length (in bytes) of source address field */
94  uint8_t aux_sec_len; /**< Length (in bytes) of aux security header field */
95 } field_length_t;
96 
97 /** \brief 802.15.4 security control bitfield. See section 7.6.2.2.1 in 802.15.4 specification */
98 typedef struct{
99  uint8_t security_level : 3; /**< security level */
100  uint8_t key_id_mode : 2; /**< Key identifier mode */
101  uint8_t reserved : 3; /**< Reserved bits */
102 } scf_t;
103 
104 /** \brief 802.15.4 Aux security header */
105 typedef struct{
106  scf_t security_control; /**< Security control bitfield */
107  uint32_t frame_counter; /**< Frame counter, used for security */
108  uint8_t key[9]; /**< The key itself, or an index to the key */
109 } aux_hdr_t;
110 
111 /**
112  * @brief Some constants for frame length calculations.
113  * The IEEE 802.15.4 frame has a number of constant/fixed fields that
114  * can be counted to make frame construction and max payload
115  * calculations easier.
116  *
117  * These include:
118  * 1. FCF - 2 bytes - Fixed
119  * 2. Sequence number - 1 byte - Fixed
120  * 3. Addressing fields - 4 - 20 bytes - Variable
121  * 4. Aux security header - 0 - 14 bytes - Variable
122  * 5. CRC - 2 bytes - Fixed
123 */
124 #define FIXEDFRAMEOVERHEAD (5)
125 
126 /** \brief A union of short and long address types. Although a node can have
127  * both long and short addresses a frame will contain
128  * only one of these. Therefore, a union is appropriate here. */
129 typedef union{
130  uint16_t shortAddr; /**< Short address, two bytes */
131  uint64_t longAddr; /**< Long address, eight bytes */
133 
134 /** \brief Structure containing a PAN ID and an address */
135 typedef struct{
136  uint16_t panId; /**< PAN ID */
137  ADDR_SIZE_SPEC_t addrSpec; /**< A short or long address */
139 
140 /** \brief Structure containing both source and destination addresses */
141 typedef struct{
142  PAN_ID_ADDR_SPEC_t destAddrFields; /**< Destination address */
143  PAN_ID_ADDR_SPEC_t srcAddrFields; /**< Source address */
145 
146 /** \brief Union of both short and long addresses */
147 typedef union{
148  uint16_t addr16; /**< Short address */
149  uint64_t addr64; /**< Long address */
150 } addr_t;
151 
152 /** \brief Strucure used to return that status of the frame create process.
153  * See frame_tx_create() function.*/
154 typedef struct{
155  uint8_t *frame; /**< Pointer to created frame */
156  uint8_t length; /**< Length (in bytes) of created frame */
158 
159 /** \brief Parameters used by the frame_tx_create() function. These
160  * parameters are used in the 802.15.4 frame header. See the 802.15.4
161  * specification for details.
162  */
163 typedef struct{
164  fcf_t fcf; /**< Frame control field */
165  uint8_t seq; /**< Sequence number */
166  uint16_t dest_pid; /**< Destination PAN ID */
167  addr_t dest_addr; /**< Destination address */
168  uint16_t src_pid; /**< Source PAN ID */
169  addr_t src_addr; /**< Source address */
170  aux_hdr_t aux_hdr; /**< Aux security header */
171  uint8_t *payload; /**< Pointer to 802.15.4 frame payload */
172  uint8_t payload_len; /**< Length of payload field */
174 
175 
176 typedef struct{
177  fcf_t * fcf; /**< The FCF of the frame. */
178  uint8_t * seqNum; /**< The sequence number of the frame. */
179  uint16_t * dest_pid; /**< Destination PAN ID. */
180  addr_t * dest_addr; /**< Destination address. */
181  uint16_t * src_pid; /**< PAN ID */
182  addr_t * src_addr; /**< Source address */
183  uint8_t * aux_sec_hdr; /**< 802.15.4 Aux security header */
184  uint8_t * payload; /**< Frame payload */
185  uint8_t payload_length; /**< Length of payload section of frame */
186  uint8_t lqi; /**< Link quality indication value */
187  uint8_t rssi; /**< Received signal strength indication value */
188  uint32_t time; /**< Time stamp of received frame */
189  bool fcs:1; /**< True if checksum has passed */
190  bool in_use:1; /**< Is this frame struct being used? */
191 } parsed_frame_t;
192 
193 /* Globals */
194 
195 //extern FRAME_t rx_frame;
196 
197 /* Protoypes */
198 
200 void frame_rx_callback(uint16_t data);
201 void rx_frame_parse(hal_rx_frame_t *rx_frame, parsed_frame_t *pf);
202 
203 /** @} */
204 #endif /* FRAME_UTILS_H */