1 /*!
2  * \file      LoRaMacHeaderTypes.h
3  *
4  * \brief     LoRa MAC layer header type definitions
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013 Semtech
16  *
17  *               ___ _____ _   ___ _  _____ ___  ___  ___ ___
18  *              / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
19  *              \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
20  *              |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
21  *              embedded.connectivity.solutions===============
22  *
23  * \endcode
24  *
25  * \author    Miguel Luis ( Semtech )
26  *
27  * \author    Gregory Cristian ( Semtech )
28  *
29  * \author    Daniel Jaeckle ( STACKFORCE )
30  *
31  * \author    Johannes Bruder ( STACKFORCE )
32  *
33  * addtogroup LORAMAC
34  * \{
35  *
36  */
37 #ifndef __LORAMAC_HEADER_TYPES_H__
38 #define __LORAMAC_HEADER_TYPES_H__
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 #include <stdint.h>
46 
47 /*! MAC header field size */
48 #define LORAMAC_MHDR_FIELD_SIZE             1
49 
50 /*! ReJoinType field size */
51 #define LORAMAC_JOIN_TYPE_FIELD_SIZE        1
52 
53 /*! Join EUI field size */
54 #define LORAMAC_JOIN_EUI_FIELD_SIZE         8
55 
56 /*! Device EUI field size */
57 #define LORAMAC_DEV_EUI_FIELD_SIZE          8
58 
59 /*! End-device nonce field size */
60 #define LORAMAC_DEV_NONCE_FIELD_SIZE        2
61 
62 /*! Join-server nonce field size */
63 #define LORAMAC_JOIN_NONCE_FIELD_SIZE       3
64 
65 /*! RJcount0 field size */
66 #define LORAMAC_RJCOUNT_0_FIELD_SIZE        2
67 
68 /*! RJcount1 field size */
69 #define LORAMAC_RJCOUNT_1_FIELD_SIZE        2
70 
71 /*! Network ID field size */
72 #define LORAMAC_NET_ID_FIELD_SIZE           3
73 
74 /*! Device address field size */
75 #define LORAMAC_DEV_ADDR_FIELD_SIZE         4
76 
77 /*! DLSettings field size */
78 #define LORAMAC_DL_SETTINGS_FIELD_SIZE      1
79 
80 /*! RxDelay field size */
81 #define LORAMAC_RX_DELAY_FIELD_SIZE         1
82 
83 /*! CFList field size */
84 #define LORAMAC_CF_LIST_FIELD_SIZE          16
85 
86 /*! FHDR Device address field size */
87 #define LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE    LORAMAC_DEV_ADDR_FIELD_SIZE
88 
89 /*! FHDR Frame control field size */
90 #define LORAMAC_FHDR_F_CTRL_FIELD_SIZE      1
91 
92 /*! FHDR Frame control field size */
93 #define LORAMAC_FHDR_F_CNT_FIELD_SIZE       2
94 
95 /*! FOpts maximum field size */
96 #define LORAMAC_FHDR_F_OPTS_MAX_FIELD_SIZE  15
97 
98 /*! Port field size */
99 #define LORAMAC_F_PORT_FIELD_SIZE           1
100 
101 /*! Port field size */
102 #define LORAMAC_MAC_PAYLOAD_FIELD_MAX_SIZE  242
103 
104 /*! MIC field size */
105 #define LORAMAC_MIC_FIELD_SIZE              4
106 
107 /*!
108  * JoinRequest frame size
109  *
110  * MHDR(1) + JoinEUI(8) + DevEUI(8) + DevNonce(2) + MIC(4)
111  */
112 #define LORAMAC_JOIN_REQ_MSG_SIZE           ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_EUI_FIELD_SIZE + \
113                                               LORAMAC_DEV_EUI_FIELD_SIZE + LORAMAC_DEV_NONCE_FIELD_SIZE + \
114                                               LORAMAC_MIC_FIELD_SIZE )
115 
116 /*!
117  * ReJoinRequest type 1 frame size
118  *
119  * MHDR(1) + ReJoinType(1) + JoinEUI(8) + DevEUI(8) + RJcount1(2) + MIC(4)
120  */
121 #define LORAMAC_RE_JOIN_1_MSG_SIZE          ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_TYPE_FIELD_SIZE + \
122                                               LORAMAC_JOIN_EUI_FIELD_SIZE + LORAMAC_DEV_EUI_FIELD_SIZE + \
123                                               LORAMAC_RJCOUNT_1_FIELD_SIZE + \
124                                               LORAMAC_MIC_FIELD_SIZE )
125 
126 /*!
127  * ReJoinRequest type 0 or 2 frame size
128  *
129  * MHDR(1) + ReJoinType(1) + NetID(3) + DevEUI(8) + RJcount0(2) + MIC(4)
130  */
131 #define LORAMAC_RE_JOIN_0_2_MSG_SIZE        ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_TYPE_FIELD_SIZE + \
132                                               LORAMAC_NET_ID_FIELD_SIZE + LORAMAC_DEV_EUI_FIELD_SIZE + \
133                                               LORAMAC_RJCOUNT_0_FIELD_SIZE + \
134                                               LORAMAC_MIC_FIELD_SIZE )
135 
136 /*!
137  * JoinAccept frame minimum size
138  *
139  * MHDR(1) + AppNonce(3) + NetID(3) + DevAddr(4) + DLSettings(1) + RxDelay(1) + MIC(4)
140  */
141 #define LORAMAC_JOIN_ACCEPT_FRAME_MIN_SIZE  ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_NONCE_FIELD_SIZE + \
142                                               LORAMAC_NET_ID_FIELD_SIZE + LORAMAC_DEV_ADDR_FIELD_SIZE + \
143                                               LORAMAC_DL_SETTINGS_FIELD_SIZE + LORAMAC_RX_DELAY_FIELD_SIZE + \
144                                               LORAMAC_MIC_FIELD_SIZE )
145 
146 /*!
147  * JoinAccept frame maximum size
148  *
149  * MHDR(1) + AppNonce(3) + NetID(3) + DevAddr(4) + DLSettings(1) + RxDelay(1) + CFList(16) + MIC(4)
150  */
151 #define LORAMAC_JOIN_ACCEPT_FRAME_MAX_SIZE  ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_NONCE_FIELD_SIZE + \
152                                               LORAMAC_NET_ID_FIELD_SIZE + LORAMAC_DEV_ADDR_FIELD_SIZE + \
153                                               LORAMAC_DL_SETTINGS_FIELD_SIZE + LORAMAC_RX_DELAY_FIELD_SIZE + \
154                                               LORAMAC_CF_LIST_FIELD_SIZE + LORAMAC_MIC_FIELD_SIZE )
155 
156 /*!
157  * MIC computation offset
158  * \remark required for 1.1.x support
159  */
160 #define JOIN_ACCEPT_MIC_COMPUTATION_OFFSET                                                   \
161     ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_TYPE_FIELD_SIZE + LORAMAC_JOIN_EUI_FIELD_SIZE + \
162       LORAMAC_DEV_NONCE_FIELD_SIZE )
163 
164 /*!
165  * FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
166  *
167  * Overhead to be used when setting the Radio.SetMaxPayloadLength in RxWindowSetup function.
168  *
169  * MHDR(1) + FHDR(7) + Port(1) + MIC(4)
170  *
171  * Maximum PHYPayload = MaxPayloadOfDatarate + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE
172  */
173 #define LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE ( LORAMAC_MHDR_FIELD_SIZE + ( LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE + \
174                                               LORAMAC_FHDR_F_CTRL_FIELD_SIZE + LORAMAC_FHDR_F_CNT_FIELD_SIZE ) + \
175                                               LORAMAC_F_PORT_FIELD_SIZE + LORAMAC_MIC_FIELD_SIZE )
176 
177 /*!
178  * FRMPayload minimum size
179  *
180  * MHDR(1) + FHDR(7) + MIC(4)
181  */
182 #define LORAMAC_FRAME_PAYLOAD_MIN_SIZE      ( LORAMAC_MHDR_FIELD_SIZE + ( LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE + \
183                                               LORAMAC_FHDR_F_CTRL_FIELD_SIZE + LORAMAC_FHDR_F_CNT_FIELD_SIZE ) + \
184                                               LORAMAC_MIC_FIELD_SIZE )
185 /*!
186  * FRMPayload maximum possible size
187  *
188  * MHDR(1) + FHDR(7) + Port(1) + MACPayload(242) + MIC(4)
189  */
190 #define LORAMAC_FRAME_PAYLOAD_MAX_SIZE      ( LORAMAC_MHDR_FIELD_SIZE + ( LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE + \
191                                               LORAMAC_FHDR_F_CTRL_FIELD_SIZE + LORAMAC_FHDR_F_CNT_FIELD_SIZE ) + \
192                                               LORAMAC_F_PORT_FIELD_SIZE + LORAMAC_MAC_PAYLOAD_FIELD_MAX_SIZE + \
193                                               LORAMAC_MIC_FIELD_SIZE )
194 
195 /*!
196  * LoRaMAC field definition of DLSettings
197  *
198  * LoRaWAN Specification V1.0.2, chapter 5.4
199  */
200 typedef union uLoRaMacDLSettings
201 {
202     /*!
203      * Byte-access to the bits
204      */
205     uint8_t Value;
206     /*!
207      * Structure containing single access to header bits
208      */
209     struct sDLSettingsBits
210     {
211         /*!
212          * Data rate of a downlink using the second receive window
213          */
214         uint8_t RX2DataRate     : 4;
215         /*!
216          * Offset between up and downlink datarate of first reception slot
217          */
218         uint8_t RX1DRoffset     : 3;
219         /*!
220          * Indicates network server LoRaWAN implementation version 1.1 or later.
221          */
222         uint8_t OptNeg          : 1;
223     }Bits;
224 }LoRaMacDLSettings_t;
225 
226 /*!
227  * LoRaMAC header field definition (MHDR field)
228  *
229  * LoRaWAN Specification V1.0.2, chapter 4.2
230  */
231 typedef union uLoRaMacHeader
232 {
233     /*!
234      * Byte-access to the bits
235      */
236     uint8_t Value;
237     /*!
238      * Structure containing single access to header bits
239      */
240     struct sMacHeaderBits
241     {
242         /*!
243          * Major version
244          */
245         uint8_t Major           : 2;
246         /*!
247          * RFU
248          */
249         uint8_t RFU             : 3;
250         /*!
251          * Message type
252          */
253         uint8_t MType           : 3;
254     }Bits;
255 }LoRaMacHeader_t;
256 
257 /*!
258  * LoRaMAC frame control field definition (FCtrl)
259  *
260  * LoRaWAN Specification V1.0.2, chapter 4.3.1
261  */
262 typedef union uLoRaMacFrameCtrl
263 {
264     /*!
265      * Byte-access to the bits
266      */
267     uint8_t Value;
268     /*!
269      * Structure containing single access to bits
270      */
271     struct sCtrlBits
272     {
273         /*!
274          * Frame options length
275          */
276         uint8_t FOptsLen        : 4;
277         /*!
278          * Frame pending bit
279          */
280         uint8_t FPending        : 1;
281         /*!
282          * Message acknowledge bit
283          */
284         uint8_t Ack             : 1;
285         /*!
286          * ADR acknowledgment request bit
287          */
288         uint8_t AdrAckReq       : 1;
289         /*!
290          * ADR control in frame header
291          */
292         uint8_t Adr             : 1;
293     }Bits;
294 }LoRaMacFrameCtrl_t;
295 
296 /*!
297  * LoRaMac Frame header (FHDR)
298  *
299  * LoRaWAN Specification V1.0.2, chapter 4.3.1
300  */
301 typedef struct sLoRaMacFrameHeader
302 {
303     /*!
304      * Device address
305      */
306     uint32_t DevAddr;
307     /*!
308      * Frame control field
309      */
310     LoRaMacFrameCtrl_t FCtrl;
311     /*!
312      * Frame counter
313      */
314     uint16_t FCnt;
315     /*!
316      * FOpts field may transport  MAC commands (opt. 0-15 Bytes)
317      */
318     uint8_t FOpts[LORAMAC_FHDR_F_OPTS_MAX_FIELD_SIZE];
319 }LoRaMacFrameHeader_t;
320 
321 /*! \} addtogroup LORAMAC */
322 
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #endif // __LORAMAC_HEADER_TYPES_H__
328