Contiki 3.x
frame802154.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  * Additional fixes for MSP430 contributed by:
14  * Joakim Eriksson
15  * Niclas Finne
16  * Nicolas Tsiftes
17  *
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions are met:
22  *
23  * * Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimer.
25  * * Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimer in
27  * the documentation and/or other materials provided with the
28  * distribution.
29  * * Neither the name of the copyright holders nor the names of
30  * contributors may be used to endorse or promote products derived
31  * from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
37  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  * POSSIBILITY OF SUCH DAMAGE.
44 */
45 
46 /**
47  * \addtogroup net 802.15.4 frame creation and parsing
48  * @{
49  */
50 
51 /**
52  * \defgroup frame802154 802.15.4 frame creation and parsing
53  * @{
54  */
55 /**
56  * \file
57  * \brief 802.15.4 frame creation and parsing functions
58  *
59  * This file converts to and from a structure to a packed 802.15.4
60  * frame.
61  *
62 */
63 
64 
65 /* Includes */
66 #ifndef FRAME_802154_H
67 #define FRAME_802154_H
68 
69 #include "contiki-conf.h"
70 
71 #ifdef IEEE802154_CONF_PANID
72 #define IEEE802154_PANID IEEE802154_CONF_PANID
73 #else /* IEEE802154_CONF_PANID */
74 #define IEEE802154_PANID 0xABCD
75 #endif /* IEEE802154_CONF_PANID */
76 
77 /* Macros & Defines */
78 
79 /** \brief These are some definitions of values used in the FCF. See the 802.15.4 spec for details.
80  * \name FCF element values definitions
81  * @{
82  */
83 #define FRAME802154_BEACONFRAME (0x00)
84 #define FRAME802154_DATAFRAME (0x01)
85 #define FRAME802154_ACKFRAME (0x02)
86 #define FRAME802154_CMDFRAME (0x03)
87 
88 #define FRAME802154_BEACONREQ (0x07)
89 
90 #define FRAME802154_IEEERESERVED (0x00)
91 #define FRAME802154_NOADDR (0x00) /**< Only valid for ACK or Beacon frames. */
92 #define FRAME802154_SHORTADDRMODE (0x02)
93 #define FRAME802154_LONGADDRMODE (0x03)
94 
95 #define FRAME802154_NOBEACONS (0x0F)
96 
97 #define FRAME802154_BROADCASTADDR (0xFFFF)
98 #define FRAME802154_BROADCASTPANDID (0xFFFF)
99 
100 #define FRAME802154_IEEE802154_2003 (0x00)
101 #define FRAME802154_IEEE802154_2006 (0x01)
102 
103 #define FRAME802154_SECURITY_LEVEL_NONE (0)
104 #define FRAME802154_SECURITY_LEVEL_MIC_32 (1)
105 #define FRAME802154_SECURITY_LEVEL_MIC_64 (2)
106 #define FRAME802154_SECURITY_LEVEL_MIC_128 (3)
107 #define FRAME802154_SECURITY_LEVEL_ENC (4)
108 #define FRAME802154_SECURITY_LEVEL_ENC_MIC_32 (5)
109 #define FRAME802154_SECURITY_LEVEL_ENC_MIC_64 (6)
110 #define FRAME802154_SECURITY_LEVEL_ENC_MIC_128 (7)
111 
112 #define FRAME802154_IMPLICIT_KEY (0)
113 #define FRAME802154_1_BYTE_KEY_ID_MODE (1)
114 #define FRAME802154_5_BYTE_KEY_ID_MODE (2)
115 #define FRAME802154_9_BYTE_KEY_ID_MODE (3)
116 
117 /**
118  * @brief The IEEE 802.15.4 frame has a number of constant/fixed fields that
119  * can be counted to make frame construction and max payload
120  * calculations easier.
121  *
122  * These include:
123  * 1. FCF - 2 bytes - Fixed
124  * 2. Sequence number - 1 byte - Fixed
125  * 3. Addressing fields - 4 - 20 bytes - Variable
126  * 4. Aux security header - 0 - 14 bytes - Variable
127  * 5. CRC - 2 bytes - Fixed
128 */
129 
130 /**
131  * \brief Defines the bitfields of the frame control field (FCF).
132  */
133 typedef struct {
134  uint8_t frame_type; /**< 3 bit. Frame type field, see 802.15.4 */
135  uint8_t security_enabled; /**< 1 bit. True if security is used in this frame */
136  uint8_t frame_pending; /**< 1 bit. True if sender has more data to send */
137  uint8_t ack_required; /**< 1 bit. Is an ack frame required? */
138  uint8_t panid_compression; /**< 1 bit. Is this a compressed header? */
139  /* uint8_t reserved; */ /**< 3 bit. Unused bits */
140  uint8_t dest_addr_mode; /**< 2 bit. Destination address mode, see 802.15.4 */
141  uint8_t frame_version; /**< 2 bit. 802.15.4 frame version */
142  uint8_t src_addr_mode; /**< 2 bit. Source address mode, see 802.15.4 */
144 
145 /** \brief 802.15.4 security control bitfield. See section 7.6.2.2.1 in 802.15.4 specification */
146 typedef struct {
147  uint8_t security_level; /**< 3 bit. security level */
148  uint8_t key_id_mode; /**< 2 bit. Key identifier mode */
149  uint8_t reserved; /**< 3 bit. Reserved bits */
151 
152 typedef union {
153  uint32_t u32;
154  uint16_t u16[2];
155  uint8_t u8[4];
156 } frame802154_frame_counter_t;
157 
158 typedef union {
159  uint16_t u16[4];
160  uint8_t u8[8];
161 } frame802154_key_source_t;
162 
163 /** \brief 802.15.4 Aux security header */
164 typedef struct {
165  frame802154_scf_t security_control; /**< Security control bitfield */
166  frame802154_frame_counter_t frame_counter; /**< Frame counter, used for security */
167  frame802154_key_source_t key_source; /**< Key Source subfield */
168  uint8_t key_index; /**< Key Index subfield */
170 
171 /** \brief Parameters used by the frame802154_create() function. These
172  * parameters are used in the 802.15.4 frame header. See the 802.15.4
173  * specification for details.
174  */
175 typedef struct {
176  /* The fields dest_addr and src_addr must come first to ensure they are aligned to the
177  * CPU word size. Needed as they are accessed directly as linkaddr_t*. Note we cannot use
178  * the type linkaddr_t directly here, as we always need 8 bytes, not LINKADDR_SIZE bytes. */
179  uint8_t dest_addr[8]; /**< Destination address */
180  uint8_t src_addr[8]; /**< Source address */
181  frame802154_fcf_t fcf; /**< Frame control field */
182  uint8_t seq; /**< Sequence number */
183  uint16_t dest_pid; /**< Destination PAN ID */
184  uint16_t src_pid; /**< Source PAN ID */
185  frame802154_aux_hdr_t aux_hdr; /**< Aux security header */
186  uint8_t *payload; /**< Pointer to 802.15.4 payload */
187  int payload_len; /**< Length of payload field */
188 } frame802154_t;
189 
190 /* Prototypes */
191 
193 int frame802154_create(frame802154_t *p, uint8_t *buf);
194 int frame802154_parse(uint8_t *data, int length, frame802154_t *pf);
195 
196 /** @} */
197 #endif /* FRAME_802154_H */
198 /** @} */
199 /** @} */
frame802154_frame_counter_t frame_counter
Frame counter, used for security.
Definition: frame802154.h:166
The IEEE 802.15.4 frame has a number of constant/fixed fields that can be counted to make frame const...
Definition: frame802154.h:133
uint8_t * payload
Pointer to 802.15.4 payload.
Definition: frame802154.h:186
uint8_t ack_required
1 bit.
Definition: frame802154.h:137
frame802154_key_source_t key_source
Key Source subfield.
Definition: frame802154.h:167
uint8_t frame_version
2 bit.
Definition: frame802154.h:141
uint8_t frame_type
3 bit.
Definition: frame802154.h:134
int frame802154_hdrlen(frame802154_t *p)
Calculates the length of the frame header.
Definition: frame802154.c:166
uint8_t security_enabled
1 bit.
Definition: frame802154.h:135
int frame802154_create(frame802154_t *p, uint8_t *buf)
Creates a frame for transmission over the air.
Definition: frame802154.c:186
uint8_t src_addr_mode
2 bit.
Definition: frame802154.h:142
uint8_t panid_compression
1 bit.
Definition: frame802154.h:138
802.15.4 Aux security header
Definition: frame802154.h:164
uint8_t key_id_mode
2 bit.
Definition: frame802154.h:148
Parameters used by the frame802154_create() function.
Definition: frame802154.h:175
uint8_t seq
Sequence number.
Definition: frame802154.h:182
int payload_len
Length of payload field.
Definition: frame802154.h:187
frame802154_scf_t security_control
Security control bitfield.
Definition: frame802154.h:165
uint8_t reserved
3 bit.
Definition: frame802154.h:149
802.15.4 security control bitfield.
Definition: frame802154.h:146
uint8_t key_index
Key Index subfield.
Definition: frame802154.h:168
uint16_t dest_pid
Destination PAN ID.
Definition: frame802154.h:183
uint16_t src_pid
Source PAN ID.
Definition: frame802154.h:184
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:270
uint8_t frame_pending
1 bit.
Definition: frame802154.h:136
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:181
uint8_t security_level
3 bit.
Definition: frame802154.h:147
frame802154_aux_hdr_t aux_hdr
Aux security header.
Definition: frame802154.h:185
uint8_t dest_addr_mode
&lt; 3 bit.
Definition: frame802154.h:140