1 /*
2 / _____) _ | |
3 ( (____ _____ ____ _| |_ _____ ____| |__
4 \____ \| ___ | (_ _) ___ |/ ___) _ \
5 _____) ) ____| | | || |_| ____( (___| | | |
6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
7 (C)2013 Semtech
8 ___ _____ _ ___ _ _____ ___ ___ ___ ___
9 / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
10 \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
11 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
12 embedded.connectivity.solutions===============
13
14 Description: LoRa MAC layer message serializer functionality implementation
15
16 License: Revised BSD License, see LICENSE.TXT file include in the project
17
18 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ),
19 Daniel Jaeckle ( STACKFORCE ), Johannes Bruder ( STACKFORCE )
20 */
21 #include "LoRaMacSerializer.h"
22 #include "utilities.h"
23
LoRaMacSerializerJoinRequest(LoRaMacMessageJoinRequest_t * macMsg)24 LoRaMacSerializerStatus_t LoRaMacSerializerJoinRequest( LoRaMacMessageJoinRequest_t* macMsg )
25 {
26 if( ( macMsg == 0 ) || ( macMsg->Buffer == 0 ) )
27 {
28 return LORAMAC_SERIALIZER_ERROR_NPE;
29 }
30
31 uint16_t bufItr = 0;
32
33 // Check macMsg->BufSize
34 if( macMsg->BufSize < LORAMAC_JOIN_REQ_MSG_SIZE )
35 {
36 return LORAMAC_SERIALIZER_ERROR_BUF_SIZE;
37 }
38
39 macMsg->Buffer[bufItr++] = macMsg->MHDR.Value;
40
41 memcpyr( &macMsg->Buffer[bufItr], macMsg->JoinEUI, LORAMAC_JOIN_EUI_FIELD_SIZE );
42 bufItr += LORAMAC_JOIN_EUI_FIELD_SIZE;
43
44 memcpyr( &macMsg->Buffer[bufItr], macMsg->DevEUI, LORAMAC_DEV_EUI_FIELD_SIZE );
45 bufItr += LORAMAC_DEV_EUI_FIELD_SIZE;
46
47 macMsg->Buffer[bufItr++] = macMsg->DevNonce & 0xFF;
48 macMsg->Buffer[bufItr++] = ( macMsg->DevNonce >> 8 ) & 0xFF;
49
50 macMsg->Buffer[bufItr++] = macMsg->MIC & 0xFF;
51 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 8 ) & 0xFF;
52 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 16 ) & 0xFF;
53 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 24 ) & 0xFF;
54
55 macMsg->BufSize = bufItr;
56
57 return LORAMAC_SERIALIZER_SUCCESS;
58 }
59
LoRaMacSerializerReJoinType1(LoRaMacMessageReJoinType1_t * macMsg)60 LoRaMacSerializerStatus_t LoRaMacSerializerReJoinType1( LoRaMacMessageReJoinType1_t* macMsg )
61 {
62 if( ( macMsg == 0 ) || ( macMsg->Buffer == 0 ) )
63 {
64 return LORAMAC_SERIALIZER_ERROR_NPE;
65 }
66
67 uint16_t bufItr = 0;
68
69 // Check macMsg->BufSize
70 if( macMsg->BufSize < LORAMAC_RE_JOIN_1_MSG_SIZE )
71 {
72 return LORAMAC_SERIALIZER_ERROR_BUF_SIZE;
73 }
74
75 macMsg->Buffer[bufItr++] = macMsg->MHDR.Value;
76 macMsg->Buffer[bufItr++] = macMsg->ReJoinType;
77
78 memcpyr( &macMsg->Buffer[bufItr], macMsg->JoinEUI, LORAMAC_JOIN_EUI_FIELD_SIZE );
79 bufItr += LORAMAC_JOIN_EUI_FIELD_SIZE;
80
81 memcpyr( &macMsg->Buffer[bufItr], macMsg->DevEUI, LORAMAC_DEV_EUI_FIELD_SIZE );
82 bufItr += LORAMAC_DEV_EUI_FIELD_SIZE;
83
84 macMsg->Buffer[bufItr++] = macMsg->RJcount1 & 0xFF;
85 macMsg->Buffer[bufItr++] = ( macMsg->RJcount1 >> 8 ) & 0xFF;
86
87 macMsg->Buffer[bufItr++] = macMsg->MIC & 0xFF;
88 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 8 ) & 0xFF;
89 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 16 ) & 0xFF;
90 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 24 ) & 0xFF;
91
92 macMsg->BufSize = bufItr;
93
94 return LORAMAC_SERIALIZER_SUCCESS;
95 }
96
LoRaMacSerializerReJoinType0or2(LoRaMacMessageReJoinType0or2_t * macMsg)97 LoRaMacSerializerStatus_t LoRaMacSerializerReJoinType0or2( LoRaMacMessageReJoinType0or2_t* macMsg )
98 {
99 if( ( macMsg == 0 ) || ( macMsg->Buffer == 0 ) )
100 {
101 return LORAMAC_SERIALIZER_ERROR_NPE;
102 }
103
104 uint16_t bufItr = 0;
105
106 // Check macMsg->BufSize
107 if( macMsg->BufSize < LORAMAC_RE_JOIN_0_2_MSG_SIZE )
108 {
109 return LORAMAC_SERIALIZER_ERROR_BUF_SIZE;
110 }
111
112 macMsg->Buffer[bufItr++] = macMsg->MHDR.Value;
113 macMsg->Buffer[bufItr++] = macMsg->ReJoinType;
114
115 memcpy1( &macMsg->Buffer[bufItr], macMsg->NetID, LORAMAC_NET_ID_FIELD_SIZE );
116 bufItr += LORAMAC_NET_ID_FIELD_SIZE;
117
118 memcpyr( &macMsg->Buffer[bufItr], macMsg->DevEUI, LORAMAC_DEV_EUI_FIELD_SIZE );
119 bufItr += LORAMAC_DEV_EUI_FIELD_SIZE;
120
121 macMsg->Buffer[bufItr++] = macMsg->RJcount0 & 0xFF;
122 macMsg->Buffer[bufItr++] = ( macMsg->RJcount0 >> 8 ) & 0xFF;
123
124 macMsg->Buffer[bufItr++] = macMsg->MIC & 0xFF;
125 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 8 ) & 0xFF;
126 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 16 ) & 0xFF;
127 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 24 ) & 0xFF;
128
129 macMsg->BufSize = bufItr;
130
131 return LORAMAC_SERIALIZER_SUCCESS;
132 }
133
LoRaMacSerializerData(LoRaMacMessageData_t * macMsg)134 LoRaMacSerializerStatus_t LoRaMacSerializerData( LoRaMacMessageData_t* macMsg )
135 {
136 if( ( macMsg == 0 ) || ( macMsg->Buffer == 0 ) )
137 {
138 return LORAMAC_SERIALIZER_ERROR_NPE;
139 }
140
141 uint16_t bufItr = 0;
142
143 // Check macMsg->BufSize
144 uint16_t computedBufSize = LORAMAC_MHDR_FIELD_SIZE
145 + LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE
146 + LORAMAC_FHDR_F_CTRL_FIELD_SIZE
147 + LORAMAC_FHDR_F_CNT_FIELD_SIZE;
148
149 computedBufSize += macMsg->FHDR.FCtrl.Bits.FOptsLen;
150
151 if( macMsg->FRMPayloadSize > 0 )
152 {
153 computedBufSize += LORAMAC_F_PORT_FIELD_SIZE;
154 }
155
156 computedBufSize += macMsg->FRMPayloadSize;
157 computedBufSize += LORAMAC_MIC_FIELD_SIZE;
158
159 if( macMsg->BufSize < computedBufSize )
160 {
161 return LORAMAC_SERIALIZER_ERROR_BUF_SIZE;
162 }
163
164 macMsg->Buffer[bufItr++] = macMsg->MHDR.Value;
165
166 macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr ) & 0xFF;
167 macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr >> 8 ) & 0xFF;
168 macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr >> 16 ) & 0xFF;
169 macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr >> 24 ) & 0xFF;
170
171 macMsg->Buffer[bufItr++] = macMsg->FHDR.FCtrl.Value;
172
173 macMsg->Buffer[bufItr++] = macMsg->FHDR.FCnt & 0xFF;
174 macMsg->Buffer[bufItr++] = ( macMsg->FHDR.FCnt >> 8 ) & 0xFF;
175
176 memcpy1( &macMsg->Buffer[bufItr], macMsg->FHDR.FOpts, macMsg->FHDR.FCtrl.Bits.FOptsLen );
177 bufItr = bufItr + macMsg->FHDR.FCtrl.Bits.FOptsLen;
178
179 if( macMsg->FRMPayloadSize > 0 )
180 {
181 macMsg->Buffer[bufItr++] = macMsg->FPort;
182 }
183
184 memcpy1( &macMsg->Buffer[bufItr], macMsg->FRMPayload, macMsg->FRMPayloadSize );
185 bufItr = bufItr + macMsg->FRMPayloadSize;
186
187 macMsg->Buffer[bufItr++] = macMsg->MIC & 0xFF;
188 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 8 ) & 0xFF;
189 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 16 ) & 0xFF;
190 macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 24 ) & 0xFF;
191
192 macMsg->BufSize = bufItr;
193
194 return LORAMAC_SERIALIZER_SUCCESS;
195 }
196