Contiki 2.5
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 frame802154
48  * @{
49  */
50 /**
51  * \file
52  * \brief 802.15.4 frame creation and parsing functions
53  *
54  * This file converts to and from a structure to a packed 802.15.4
55  * frame.
56  *
57  * $Id: frame802154.h,v 1.3 2010/02/18 21:00:28 adamdunkels Exp $
58 */
59 
60 
61 /* Includes */
62 #ifndef FRAME_802154_H
63 #define FRAME_802154_H
64 
65 #include "contiki-conf.h"
66 #include "net/rime/rimeaddr.h"
67 
68 #ifdef IEEE802154_CONF_PANID
69 #define IEEE802154_PANID IEEE802154_CONF_PANID
70 #else
71 #define IEEE802154_PANID 0xABCD
72 #endif
73 
74 /* Macros & Defines */
75 
76 /** \brief These are some definitions of values used in the FCF. See the 802.15.4 spec for details.
77  * \name FCF element values definitions
78  * @{
79  */
80 #define FRAME802154_BEACONFRAME (0x00)
81 #define FRAME802154_DATAFRAME (0x01)
82 #define FRAME802154_ACKFRAME (0x02)
83 #define FRAME802154_CMDFRAME (0x03)
84 
85 #define FRAME802154_BEACONREQ (0x07)
86 
87 #define FRAME802154_IEEERESERVED (0x00)
88 #define FRAME802154_NOADDR (0x00) /**< Only valid for ACK or Beacon frames. */
89 #define FRAME802154_SHORTADDRMODE (0x02)
90 #define FRAME802154_LONGADDRMODE (0x03)
91 
92 #define FRAME802154_NOBEACONS (0x0F)
93 
94 #define FRAME802154_BROADCASTADDR (0xFFFF)
95 #define FRAME802154_BROADCASTPANDID (0xFFFF)
96 
97 #define FRAME802154_IEEE802154_2003 (0x00)
98 #define FRAME802154_IEEE802154_2006 (0x01)
99 
100 #define FRAME802154_SECURITY_LEVEL_NONE (0)
101 #define FRAME802154_SECURITY_LEVEL_128 (3)
102 
103 
104 /**
105  * @brief The IEEE 802.15.4 frame has a number of constant/fixed fields that
106  * can be counted to make frame construction and max payload
107  * calculations easier.
108  *
109  * These include:
110  * 1. FCF - 2 bytes - Fixed
111  * 2. Sequence number - 1 byte - Fixed
112  * 3. Addressing fields - 4 - 20 bytes - Variable
113  * 4. Aux security header - 0 - 14 bytes - Variable
114  * 5. CRC - 2 bytes - Fixed
115 */
116 
117 /**
118  * \brief Defines the bitfields of the frame control field (FCF).
119  */
120 typedef struct {
121  uint8_t frame_type; /**< 3 bit. Frame type field, see 802.15.4 */
122  uint8_t security_enabled; /**< 1 bit. True if security is used in this frame */
123  uint8_t frame_pending; /**< 1 bit. True if sender has more data to send */
124  uint8_t ack_required; /**< 1 bit. Is an ack frame required? */
125  uint8_t panid_compression; /**< 1 bit. Is this a compressed header? */
126  /* uint8_t reserved; */ /**< 3 bit. Unused bits */
127  uint8_t dest_addr_mode; /**< 2 bit. Destination address mode, see 802.15.4 */
128  uint8_t frame_version; /**< 2 bit. 802.15.4 frame version */
129  uint8_t src_addr_mode; /**< 2 bit. Source address mode, see 802.15.4 */
131 
132 /** \brief 802.15.4 security control bitfield. See section 7.6.2.2.1 in 802.15.4 specification */
133 typedef struct {
134  uint8_t security_level; /**< 3 bit. security level */
135  uint8_t key_id_mode; /**< 2 bit. Key identifier mode */
136  uint8_t reserved; /**< 3 bit. Reserved bits */
138 
139 /** \brief 802.15.4 Aux security header */
140 typedef struct {
141  frame802154_scf_t security_control; /**< Security control bitfield */
142  uint32_t frame_counter; /**< Frame counter, used for security */
143  uint8_t key[9]; /**< The key itself, or an index to the key */
145 
146 /** \brief Parameters used by the frame802154_create() function. These
147  * parameters are used in the 802.15.4 frame header. See the 802.15.4
148  * specification for details.
149  */
150 typedef struct {
151  frame802154_fcf_t fcf; /**< Frame control field */
152  uint8_t seq; /**< Sequence number */
153  uint16_t dest_pid; /**< Destination PAN ID */
154  uint8_t dest_addr[8]; /**< Destination address */
155  uint16_t src_pid; /**< Source PAN ID */
156  uint8_t src_addr[8]; /**< Source address */
157  frame802154_aux_hdr_t aux_hdr; /**< Aux security header */
158  uint8_t *payload; /**< Pointer to 802.15.4 frame payload */
159  uint8_t payload_len; /**< Length of payload field */
160 } frame802154_t;
161 
162 /* Prototypes */
163 
165 uint8_t frame802154_create(frame802154_t *p, uint8_t *buf, uint8_t buf_len);
166 uint8_t frame802154_parse(uint8_t *data, uint8_t length, frame802154_t *pf);
167 
168 /** @} */
169 #endif /* FRAME_802154_H */