Contiki 3.x
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 */
53 
54 
55 
56 /* Includes */
57 #ifndef FRAME_UTILS_H
58 #define FRAME_UTILS_H
59 
60 #include "hal.h"
61 
62 /* Macros & Defines */
63 
64 
65 /**
66  * \brief Defines the bitfields of the frame control field (FCF).
67  */
68 typedef union{
69  /** \brief Structure of bitfields for the FCF */
70  struct{
71  uint8_t frameType : 3; /**< Frame type field, see 802.15.4 */
72  bool securityEnabled : 1; /**< True if security is used in this frame */
73  bool framePending : 1; /**< True if sender has more data to send */
74  bool ackRequired : 1; /**< Is an ack frame required? */
75  bool panIdCompression : 1; /**< Is this a compressed header? */
76  uint8_t reserved : 3; /**< Unused bits */
77  uint8_t destAddrMode : 2; /**< Destination address mode, see 802.15.4 */
78  uint8_t frameVersion : 2; /**< 802.15.4 frame version */
79  uint8_t srcAddrMode : 2; /**< Source address mode, see 802.15.4 */
80  };
81  uint16_t word_val; /**< A word-wide value for the entire FCF */
82 }fcf_t;
83 
84 /**
85  * \brief Structure that contains the lengths of the various addressing and security fields
86  * in the 802.15.4 header. This structure is used in \ref frame_tx_create()
87  */
88 typedef struct{
89  uint8_t dest_pid_len; /**< Length (in bytes) of destination PAN ID field */
90  uint8_t dest_addr_len; /**< Length (in bytes) of destination address field */
91  uint8_t src_pid_len; /**< Length (in bytes) of source PAN ID field */
92  uint8_t src_addr_len; /**< Length (in bytes) of source address field */
93  uint8_t aux_sec_len; /**< Length (in bytes) of aux security header field */
94 } field_length_t;
95 
96 /** \brief 802.15.4 security control bitfield. See section 7.6.2.2.1 in 802.15.4 specification */
97 typedef struct{
98  uint8_t security_level : 3; /**< security level */
99  uint8_t key_id_mode : 2; /**< Key identifier mode */
100  uint8_t reserved : 3; /**< Reserved bits */
101 } scf_t;
102 
103 /** \brief 802.15.4 Aux security header */
104 typedef struct{
105  scf_t security_control; /**< Security control bitfield */
106  uint32_t frame_counter; /**< Frame counter, used for security */
107  uint8_t key[9]; /**< The key itself, or an index to the key */
108 } aux_hdr_t;
109 
110 /**
111  * @brief Some constants for frame length calculations.
112  * The IEEE 802.15.4 frame has a number of constant/fixed fields that
113  * can be counted to make frame construction and max payload
114  * calculations easier.
115  *
116  * These include:
117  * 1. FCF - 2 bytes - Fixed
118  * 2. Sequence number - 1 byte - Fixed
119  * 3. Addressing fields - 4 - 20 bytes - Variable
120  * 4. Aux security header - 0 - 14 bytes - Variable
121  * 5. CRC - 2 bytes - Fixed
122 */
123 #define FIXEDFRAMEOVERHEAD (5)
124 
125 /** \brief A union of short and long address types. Although a node can have
126  * both long and short addresses a frame will contain
127  * only one of these. Therefore, a union is appropriate here. */
128 typedef union{
129  uint16_t shortAddr; /**< Short address, two bytes */
130  uint64_t longAddr; /**< Long address, eight bytes */
132 
133 /** \brief Structure containing a PAN ID and an address */
134 typedef struct{
135  uint16_t panId; /**< PAN ID */
136  ADDR_SIZE_SPEC_t addrSpec; /**< A short or long address */
138 
139 /** \brief Structure containing both source and destination addresses */
140 typedef struct{
141  PAN_ID_ADDR_SPEC_t destAddrFields; /**< Destination address */
142  PAN_ID_ADDR_SPEC_t srcAddrFields; /**< Source address */
144 
145 /** \brief Union of both short and long addresses */
146 typedef union{
147  uint16_t addr16; /**< Short address */
148  uint64_t addr64; /**< Long address */
149 } addr_t;
150 
151 /** \brief Strucure used to return that status of the frame create process.
152  * See frame_tx_create() function.*/
153 typedef struct{
154  uint8_t *frame; /**< Pointer to created frame */
155  uint8_t length; /**< Length (in bytes) of created frame */
157 
158 /** \brief Parameters used by the frame_tx_create() function. These
159  * parameters are used in the 802.15.4 frame header. See the 802.15.4
160  * specification for details.
161  */
162 typedef struct{
163  fcf_t fcf; /**< Frame control field */
164  uint8_t seq; /**< Sequence number */
165  uint16_t dest_pid; /**< Destination PAN ID */
166  addr_t dest_addr; /**< Destination address */
167  uint16_t src_pid; /**< Source PAN ID */
168  addr_t src_addr; /**< Source address */
169  aux_hdr_t aux_hdr; /**< Aux security header */
170  uint8_t *payload; /**< Pointer to 802.15.4 frame payload */
171  uint8_t payload_len; /**< Length of payload field */
173 
174 
175 typedef struct{
176  fcf_t * fcf; /**< The FCF of the frame. */
177  uint8_t * seqNum; /**< The sequence number of the frame. */
178  uint16_t * dest_pid; /**< Destination PAN ID. */
179  addr_t * dest_addr; /**< Destination address. */
180  uint16_t * src_pid; /**< PAN ID */
181  addr_t * src_addr; /**< Source address */
182  uint8_t * aux_sec_hdr; /**< 802.15.4 Aux security header */
183  uint8_t * payload; /**< Frame payload */
184  uint8_t payload_length; /**< Length of payload section of frame */
185  uint8_t lqi; /**< Link quality indication value */
186  uint8_t rssi; /**< Received signal strength indication value */
187  uint32_t time; /**< Time stamp of received frame */
188  bool fcs:1; /**< True if checksum has passed */
189  bool in_use:1; /**< Is this frame struct being used? */
190 } parsed_frame_t;
191 
192 /* Globals */
193 
194 //extern FRAME_t rx_frame;
195 
196 /* Protoypes */
197 
199 void frame_rx_callback(uint16_t data);
200 void rx_frame_parse(hal_rx_frame_t *rx_frame, parsed_frame_t *pf);
201 
202 /** @} */
203 #endif /* FRAME_UTILS_H */
PAN_ID_ADDR_SPEC_t destAddrFields
Destination address.
Definition: frame.h:141
uint8_t length
Length (in bytes) of created frame.
Definition: frame.h:155
ADDR_SIZE_SPEC_t addrSpec
A short or long address.
Definition: frame.h:136
uint16_t src_pid
Source PAN ID.
Definition: frame.h:167
uint8_t seq
Sequence number.
Definition: frame.h:164
uint8_t payload_len
Length of payload field.
Definition: frame.h:171
Structure containing a PAN ID and an address.
Definition: frame.h:134
PAN_ID_ADDR_SPEC_t srcAddrFields
Source address.
Definition: frame.h:142
void rx_frame_parse(hal_rx_frame_t *rx_frame, parsed_frame_t *pf)
Parses an input frame.
Definition: frame.c:255
Structure containing both source and destination addresses.
Definition: frame.h:140
scf_t security_control
Security control bitfield.
Definition: frame.h:105
uint64_t longAddr
Long address, eight bytes.
Definition: frame.h:130
void frame_tx_create(frame_create_params_t *p, frame_result_t *frame_result)
Creates a frame for transmission over the air.
Definition: frame.c:119
This file contains low-level radio driver code.
This struct defines the rx data container.
Definition: hal.h:351
uint32_t frame_counter
Frame counter, used for security.
Definition: frame.h:106
uint16_t panId
PAN ID.
Definition: frame.h:135
uint8_t * frame
Pointer to created frame.
Definition: frame.h:154
Strucure used to return that status of the frame create process.
Definition: frame.h:153
aux_hdr_t aux_hdr
Aux security header.
Definition: frame.h:169
uint16_t shortAddr
Short address, two bytes.
Definition: frame.h:129
addr_t src_addr
Source address.
Definition: frame.h:168
uint64_t addr64
Long address.
Definition: frame.h:148
uint16_t dest_pid
Destination PAN ID.
Definition: frame.h:165
uint16_t addr16
Short address.
Definition: frame.h:147
addr_t dest_addr
Destination address.
Definition: frame.h:166
Union of both short and long addresses.
Definition: frame.h:146
Parameters used by the frame_tx_create() function.
Definition: frame.h:162
802.15.4 Aux security header
Definition: frame.h:104
802.15.4 security control bitfield.
Definition: frame.h:97
uint16_t word_val
A word-wide value for the entire FCF.
Definition: frame.h:81
fcf_t fcf
Frame control field.
Definition: frame.h:163
A union of short and long address types.
Definition: frame.h:128
uint8_t * payload
Pointer to 802.15.4 frame payload.
Definition: frame.h:170
Defines the bitfields of the frame control field (FCF).
Definition: frame.h:68