1 /*
2  * Copyright (c) 2017 - 2023, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 /**
36  * @brief Module that contains definitions of constant values used by the nRF 802.15.4 driver.
37  *
38  */
39 
40 #ifndef NRF_802154_CONST_H_
41 #define NRF_802154_CONST_H_
42 
43 #define RAW_LENGTH_OFFSET               0                                            ///< Byte containing the frame length in a raw frame.
44 #define RAW_PAYLOAD_OFFSET              1                                            ///< Offset of the frame payload in a raw frame
45 
46 #define ACK_HEADER_WITH_PENDING         0x12                                         ///< The first byte of an ACK frame containing a pending bit.
47 #define ACK_HEADER_WITHOUT_PENDING      0x02                                         ///< The first byte of an ACK frame without a pending bit.
48 
49 #define ACK_REQUEST_OFFSET              1                                            ///< Byte containing the ACK request bit (+1 for the frame length byte).
50 #define ACK_REQUEST_BIT                 (1 << 5)                                     ///< ACK request bit.
51 
52 #define ASN_IN_NONCE_BIT                0x40                                         ///< Bit containing the ASN in Nonce field.
53 
54 #define DEST_ADDR_TYPE_OFFSET           2                                            ///< Byte containing the destination address type (+1 for the frame length byte).
55 #define DEST_ADDR_TYPE_MASK             0x0c                                         ///< Mask of bits containing the destination address type.
56 #define DEST_ADDR_TYPE_EXTENDED         0x0c                                         ///< Bits containing the extended destination address type.
57 #define DEST_ADDR_TYPE_NONE             0x00                                         ///< Bits containing a not-present destination address type.
58 #define DEST_ADDR_TYPE_SHORT            0x08                                         ///< Bits containing the short destination address type.
59 #define DEST_ADDR_OFFSET                6                                            ///< Offset of the destination address in the Data frame (+1 for the frame length byte).
60 
61 #define DSN_OFFSET                      3                                            ///< Byte containing the DSN value (+1 for the frame length byte).
62 #define DSN_SUPPRESS_OFFSET             2                                            ///< Byte containing the DSN suppression field.
63 #define DSN_SUPPRESS_BIT                0x01                                         ///< Bits containing the DSN suppression field.
64 
65 #define FRAME_COUNTER_SUPPRESS_BIT      0x20                                         ///< Bit containing the Frame Counter Suppression field.
66 
67 #define FRAME_PENDING_OFFSET            1                                            ///< Byte containing a pending bit (+1 for the frame length byte).
68 #define FRAME_PENDING_BIT               (1 << 4)                                     ///< Pending bit.
69 
70 #define FRAME_TYPE_OFFSET               1                                            ///< Byte containing the frame type bits (+1 for the frame length byte).
71 #define FRAME_TYPE_MASK                 0x07                                         ///< Mask of bits containing the frame type.
72 #define FRAME_TYPE_ACK                  0x02                                         ///< Bits containing the ACK frame type.
73 #define FRAME_TYPE_BEACON               0x00                                         ///< Bits containing the Beacon frame type.
74 #define FRAME_TYPE_COMMAND              0x03                                         ///< Bits containing the Command frame type.
75 #define FRAME_TYPE_DATA                 0x01                                         ///< Bits containing the Data frame type.
76 #define FRAME_TYPE_EXTENDED             0x07                                         ///< Bits containing the Extended frame type.
77 #define FRAME_TYPE_FRAGMENT             0x06                                         ///< Bits containing the Fragment or the Frak frame type.
78 #define FRAME_TYPE_MULTIPURPOSE         0x05                                         ///< Bits containing the Multipurpose frame type.
79 
80 #define FRAME_VERSION_OFFSET            2                                            ///< Byte containing the frame version bits (+1 for the frame length byte).
81 #define FRAME_VERSION_MASK              0x30                                         ///< Mask of bits containing the frame version.
82 #define FRAME_VERSION_0                 0x00                                         ///< Bits containing the frame version 0b00.
83 #define FRAME_VERSION_1                 0x10                                         ///< Bits containing the frame version 0b01.
84 #define FRAME_VERSION_2                 0x20                                         ///< Bits containing the frame version 0b10.
85 #define FRAME_VERSION_3                 0x30                                         ///< Bits containing the frame version 0b11.
86 
87 #define IE_HEADER_LENGTH_MASK           0x3f                                         ///< Mask of bits containing the length of an IE header content.
88 #define IE_PRESENT_OFFSET               2                                            ///< Byte containing the IE Present bit.
89 #define IE_PRESENT_BIT                  0x02                                         ///< Bits containing the IE Present field.
90 
91 #define KEY_ID_MODE_0                   0                                            ///< Value of the 0x00 Key Identifier Mode.
92 #define KEY_ID_MODE_1                   1                                            ///< Value of the 0x01 Key Identifier Mode.
93 #define KEY_ID_MODE_2                   2                                            ///< Value of the 0x10 Key Identifier Mode.
94 #define KEY_ID_MODE_3                   3                                            ///< Value of the 0x11 Key Identifier Mode.
95 
96 #define KEY_ID_MODE_MASK                0x18                                         ///< Mask of bits containing Key Identifier Mode in the Security Control field.
97 #define KEY_ID_MODE_BIT_OFFSET          3                                            ///< Number of bits the Key Identifier Mode is offset in the Security Control field.
98 #define KEY_ID_MODE_0_MASK              0                                            ///< Bits containing the 0x00 Key Identifier Mode.
99 #define KEY_ID_MODE_1_MASK              0x08                                         ///< Bits containing the 0x01 Key Identifier Mode.
100 #define KEY_ID_MODE_2_MASK              0x10                                         ///< Bits containing the 0x10 Key Identifier Mode.
101 #define KEY_ID_MODE_3_MASK              0x18                                         ///< Bits containing the 0x11 Key Identifier Mode.
102 
103 #define KEY_SRC_KEY_ID_MODE_0_SIZE      0                                            ///< Size of the Key Source field when Key Identifier Mode equals 0.
104 #define KEY_SRC_KEY_ID_MODE_1_SIZE      0                                            ///< Size of the Key Source field when Key Identifier Mode equals 1.
105 #define KEY_SRC_KEY_ID_MODE_2_SIZE      4                                            ///< Size of the Key Source field when Key Identifier Mode equals 2.
106 #define KEY_SRC_KEY_ID_MODE_3_SIZE      8                                            ///< Size of the Key Source field when Key Identifier Mode equals 3.
107 
108 #define KEY_IDX_SIZE                    1                                            ///< Size of the Key Index field
109 
110 #define MAC_CMD_COMMAND_ID_SIZE         1                                            ///< Size of the MAC Command ID field.
111 #define MAC_CMD_ASSOC_REQ               0x01                                         ///< Command frame identifier for MAC Association request.
112 #define MAC_CMD_ASSOC_RESP              0x02                                         ///< Command frame identifier for MAC Association response.
113 #define MAC_CMD_DISASSOC_NOTIFY         0x03                                         ///< Command frame identifier for MAC Disaccociation notification.
114 #define MAC_CMD_DATA_REQ                0x04                                         ///< Command frame identifier for MAC Data Requests.
115 #define MAC_CMD_PANID_CONFLICT          0x05                                         ///< Command frame identifier for MAC PAN ID conflict notification.
116 #define MAC_CMD_ORPHAN_NOTIFY           0x06                                         ///< Command frame identifier for MAC Orphan notification.
117 #define MAC_CMD_BEACON_REQ              0x07                                         ///< Command frame identifier for MAC Beacon.
118 #define MAC_CMD_COORD_REALIGN           0x08                                         ///< Command frame identifier for MAC Coordinator realignment.
119 #define MAC_CMD_GTS_REQUEST             0x09                                         ///< Command frame identifier for MAC GTS request.
120 
121 #define PAN_ID_COMPR_OFFSET             1                                            ///< Byte containing the PAN ID compression bit (+1 for the frame length byte).
122 #define PAN_ID_COMPR_MASK               0x40                                         ///< PAN ID compression bit.
123 
124 #define PAN_ID_OFFSET                   4                                            ///< Offset of PAN ID in the Data frame (+1 for the frame length byte).
125 
126 #define PHR_OFFSET                      0                                            ///< Offset of the PHY header in a frame.
127 #define PHR_LENGTH_MASK                 0x7f                                         ///< Mask of the PHR length field
128 
129 #define PSDU_OFFSET                     1                                            ///< Offset of the PHY payload.
130 
131 #define SECURITY_ENABLED_OFFSET         1                                            ///< Byte containing the Security Enabled bit.
132 #define SECURITY_ENABLED_BIT            0x08                                         ///< Bits containing the Security Enabled field.
133 #define SECURITY_LEVEL_MASK             0x07                                         ///< Mask of bits containing the Security level field.
134 #define SECURITY_LEVEL_NONE             0x00                                         ///< Bits indicating a frame with no security attributes (0b000).
135 #define SECURITY_LEVEL_MIC_32           0x01                                         ///< Bits containing the 32-bit Message Integrity Code (0b001).
136 #define SECURITY_LEVEL_MIC_64           0x02                                         ///< Bits containing the 64-bit Message Integrity Code (0b010).
137 #define SECURITY_LEVEL_MIC_128          0x03                                         ///< Bits containing the 128-bit Message Integrity Code (0b011).
138 #define SECURITY_LEVEL_ENC_MIC_32       0x05                                         ///< Bits containing the 32-bit Encrypted Message Integrity Code (0b101).
139 #define SECURITY_LEVEL_ENC_MIC_64       0x06                                         ///< Bits containing the 64-bit Encrypted Message Integrity Code (0b110).
140 #define SECURITY_LEVEL_ENC_MIC_128      0x07                                         ///< Bits containing the 128-bit Encrypted Message Integrity Code (0b111).
141 #define SECURITY_LEVEL_MIC_LEVEL_MASK   0x03                                         ///< Mask of bits encoding the Message Integrity Code length.
142 
143 #define SRC_ADDR_TYPE_EXTENDED          0xc0                                         ///< Bits containing the extended source address type.
144 #define SRC_ADDR_TYPE_NONE              0x00                                         ///< Bits containing a not-present source address type.
145 #define SRC_ADDR_TYPE_MASK              0xc0                                         ///< Mask of bits containing the source address type.
146 #define SRC_ADDR_TYPE_OFFSET            2                                            ///< Byte containing the source address type (+1 for the frame length byte).
147 #define SRC_ADDR_TYPE_SHORT             0x80                                         ///< Bits containing the short source address type.
148 
149 #define SRC_ADDR_OFFSET_SHORT_DST       8                                            ///< Offset of the source address in the Data frame if the destination address is short.
150 #define SRC_ADDR_OFFSET_EXTENDED_DST    14                                           ///< Offset of the source address in the Data frame if the destination address is extended.
151 
152 #define DSN_SIZE                        1                                            ///< Size of the Sequence Number field.
153 #define FCF_SIZE                        2                                            ///< Size of the FCF field.
154 #define FCS_SIZE                        2                                            ///< Size of the FCS field.
155 #define FRAME_COUNTER_SIZE              4                                            ///< Size of the Frame Counter field.
156 #define IE_HEADER_SIZE                  2                                            ///< Size of the obligatory IE Header field elements.
157 #define IMM_ACK_LENGTH                  5                                            ///< Length of the ACK frame.
158 #define KEY_ID_MODE_1_SIZE              1                                            ///< Size of the 0x01 Key Identifier Mode field.
159 #define KEY_ID_MODE_2_SIZE              5                                            ///< Size of the 0x10 Key Identifier Mode field.
160 #define KEY_ID_MODE_3_SIZE              9                                            ///< Size of the 0x11 Key Identifier Mode field.
161 #define MAX_PACKET_SIZE                 127                                          ///< Maximum size of the radio packet.
162 #define MIC_32_SIZE                     4                                            ///< Size of MIC with the MIC-32 and ENC-MIC-32 security attributes.
163 #define MIC_64_SIZE                     8                                            ///< Size of MIC with the MIC-64 and ENC-MIC-64 security attributes.
164 #define MIC_128_SIZE                    16                                           ///< Size of MIC with the MIC-128 and ENC-MIC-128 security attributes.
165 #define PAN_ID_SIZE                     2                                            ///< Size of the PAN ID.
166 #define PHR_SIZE                        1                                            ///< Size of the PHR field.
167 #define SECURITY_CONTROL_SIZE           1                                            ///< Size of the Security Control field.
168 
169 #define AES_CCM_KEY_SIZE                16                                           ///< Size of AES CCM Key.
170 
171 #define EXTENDED_ADDRESS_SIZE           8                                            ///< Size of the Extended Mac Address.
172 #define SHORT_ADDRESS_SIZE              2                                            ///< Size of the Short Mac Address.
173 
174 #define TURNAROUND_TIME                 192UL                                        ///< RX-to-TX or TX-to-RX turnaround time (aTurnaroundTime), in microseconds (us).
175 #define CCA_TIME                        128UL                                        ///< Time required to perform CCA detection (aCcaTime), in microseconds (us).
176 #define ACK_IFS                         TURNAROUND_TIME                              ///< Ack Inter Frame Spacing [us] - delay between last symbol of received frame and first symbol of transmitted Ack
177 #define UNIT_BACKOFF_PERIOD             (TURNAROUND_TIME + CCA_TIME)                 ///< Number of symbols in the basic time period used by CSMA-CA algorithm (aUnitBackoffPeriod), in (us).
178 
179 #define PHY_US_PER_SYMBOL               16                                           ///< Duration of a single symbol in microseconds (us).
180 #define PHY_SYMBOLS_PER_OCTET           2                                            ///< Number of symbols in a single byte (octet).
181 #define PHY_SHR_SYMBOLS                 10                                           ///< Number of symbols in the Synchronization Header (SHR).
182 
183 #define PHY_MIN_RECEIVER_SENSITIVITY    -85                                          ///< Lowest receiver sensitivity level in dBm according to 802.15.4-2020 specification, chapter 12.3.4
184 
185 #define ED_RESULT_MAX                   0xff                                         ///< Maximal ED result.
186 
187 #define BROADCAST_ADDRESS               ((uint8_t[SHORT_ADDRESS_SIZE]) {0xff, 0xff}) ///< Broadcast short address.
188 
189 #define MIN_SIFS_PERIOD_US              192                                          ///< Minimum Short IFS period default value in us.
190 #define MIN_LIFS_PERIOD_US              640                                          ///< Minimum Long IFS period default value in us.
191 #define MAX_SIFS_FRAME_SIZE             18                                           ///< Maximum frame length which can be followed by the Short Interframe Space.
192 
193 #define NRF_802154_RESERVED_CSMACA_ID   (UINT32_MAX - 2)                             ///< Delayed timeslot identifier reserved for CSMA/CA procedure.
194 #define NRF_802154_RESERVED_DTX_ID      (UINT32_MAX - 3)                             ///< Delayed timeslot identifier reserved for delayed transmissions.
195 
196 #define IE_VENDOR_ID                    0x00                                         ///< Vendor-specific IE identifier
197 #define IE_VENDOR_SIZE_MIN              3                                            ///< Vendor-specific IE minimum length
198 #define IE_VENDOR_OUI_OFFSET            0                                            ///< Vendor-specific IE OUI offset
199 
200 #define IE_VENDOR_THREAD_SUBTYPE_OFFSET 3                                            ///< Thread Vendor-specific IE subtype offset
201 #define IE_VENDOR_THREAD_DATA_OFFSET    4                                            ///< Thread Vendor-specific IE DATA offset
202 #define IE_VENDOR_THREAD_OUI            0xeab89b                                     ///< Thread Vendor-specific IE OUI
203 #define IE_VENDOR_THREAD_SIZE_MIN       4                                            ///< Thread Vendor-specific IE minimum length
204 
205 #define IE_VENDOR_THREAD_ACK_PROBING_ID 0x00                                         ///< Thread Vendor-specific ACK Probing IE subtype ID
206 #define IE_VENDOR_THREAD_ACK_SIZE_MIN   5                                            ///< Thread Vendor-specific ACK Probing IE minimum size
207 #define IE_VENDOR_THREAD_ACK_SIZE_MAX   6                                            ///< Thread Vendor-specific ACK Probing IE maximum size
208 #define IE_VENDOR_THREAD_RSSI_TOKEN     0x01                                         ///< Thread Vendor-specific ACK Probing IE RSSI value placeholder
209 #define IE_VENDOR_THREAD_MARGIN_TOKEN   0x02                                         ///< Thread Vendor-specific ACK Probing IE Link margin value placeholder
210 #define IE_VENDOR_THREAD_LQI_TOKEN      0x03                                         ///< Thread Vendor-specific ACK Probing IE LQI value placeholder
211 #define IE_VENDOR_THREAD_RSSI_FLOOR     -130                                         ///< Thread Vendor-specific ACK Probing RSSI floor value used for scaling
212 #define IE_VENDOR_THREAD_MARGIN_FLOOR   0                                            ///< Thread Vendor-specific ACK Probing margin floor value used for scaling
213 #define IE_VENDOR_THREAD_RSSI_CEIL      0                                            ///< Thread Vendor-specific ACK Probing RSSI ceil value used for scaling
214 #define IE_VENDOR_THREAD_MARGIN_CEIL    130                                          ///< Thread Vendor-specific ACK Probing margin ceil value used for scaling
215 #define IE_CSL_SYMBOLS_PER_UNIT         10                                           ///< Number of symbols per phase/period unit
216 #define IE_CSL_PERIOD_MAX               0xffff                                       ///< Maximum CSL IE phase/period value
217 #define IE_CSL_SIZE_MIN                 4                                            ///< Minimal size of the CSL IE
218 #define IE_CSL_ID                       0x1a                                         ///< CSL IE identifier
219 
220 #define IE_HT1                          0x7e                                         ///< Information Element Header Termination type 1
221 #define IE_HT2                          0x7f                                         ///< Information Element Header Termination type 2
222 #define IE_LENGTH_MASK                  0x7f                                         ///< Information element length mask
223 #define IE_LENGTH_OFFSET                0x00                                         ///< Information element length offset
224 #define IE_ID_OFFSET_0                  0x00                                         ///< Offset of the octet containing the first part of the IE identifier.
225 #define IE_ID_OFFSET_1                  0x01                                         ///< Offset of the octed containing the second part of the IE identifier.
226 #define IE_DATA_OFFSET                  0x02                                         ///< Information element data offset
227 #define IE_HEADER_ELEMENT_ID_OFFSET     0x07                                         ///< Bit offset of Element ID field in a Header IE header.
228 
229 typedef enum
230 {
231     REQ_ORIG_HIGHER_LAYER,
232     REQ_ORIG_CORE,
233     REQ_ORIG_RSCH,
234     REQ_ORIG_CSMA_CA,
235     REQ_ORIG_ACK_TIMEOUT,
236     REQ_ORIG_DELAYED_TRX,
237     REQ_ORIG_IFS,
238 } req_originator_t;
239 
240 #endif // NRF_802154_CONST_H_
241