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