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 
77     macMsg->Buffer[bufItr++] = macMsg->ReJoinType;
78 
79     memcpyr( &macMsg->Buffer[bufItr], macMsg->JoinEUI, LORAMAC_JOIN_EUI_FIELD_SIZE );
80     bufItr += LORAMAC_JOIN_EUI_FIELD_SIZE;
81 
82     memcpyr( &macMsg->Buffer[bufItr], macMsg->DevEUI, LORAMAC_DEV_EUI_FIELD_SIZE );
83     bufItr += LORAMAC_DEV_EUI_FIELD_SIZE;
84 
85     macMsg->Buffer[bufItr++] = macMsg->RJcount1 & 0xFF;
86     macMsg->Buffer[bufItr++] = ( macMsg->RJcount1 >> 8 ) & 0xFF;
87 
88     return LORAMAC_SERIALIZER_SUCCESS;
89 }
90 
LoRaMacSerializerReJoinType0or2(LoRaMacMessageReJoinType0or2_t * macMsg)91 LoRaMacSerializerStatus_t LoRaMacSerializerReJoinType0or2( LoRaMacMessageReJoinType0or2_t* macMsg )
92 {
93     if( ( macMsg == 0 ) || ( macMsg->Buffer == 0 ) )
94     {
95         return LORAMAC_SERIALIZER_ERROR_NPE;
96     }
97 
98     uint16_t bufItr = 0;
99 
100     // Check macMsg->BufSize
101     if( macMsg->BufSize < LORAMAC_RE_JOIN_0_2_MSG_SIZE )
102     {
103         return LORAMAC_SERIALIZER_ERROR_BUF_SIZE;
104     }
105 
106     macMsg->Buffer[bufItr++] = macMsg->MHDR.Value;
107 
108     macMsg->Buffer[bufItr++] = macMsg->ReJoinType;
109 
110     memcpy1( &macMsg->Buffer[bufItr], macMsg->NetID, LORAMAC_NET_ID_FIELD_SIZE );
111     bufItr += LORAMAC_NET_ID_FIELD_SIZE;
112 
113     memcpyr( &macMsg->Buffer[bufItr], macMsg->DevEUI, LORAMAC_DEV_EUI_FIELD_SIZE );
114     bufItr += LORAMAC_DEV_EUI_FIELD_SIZE;
115 
116     macMsg->Buffer[bufItr++] = macMsg->RJcount0 & 0xFF;
117     macMsg->Buffer[bufItr++] = ( macMsg->RJcount0 >> 8 ) & 0xFF;
118 
119     return LORAMAC_SERIALIZER_SUCCESS;
120 }
121 
LoRaMacSerializerData(LoRaMacMessageData_t * macMsg)122 LoRaMacSerializerStatus_t LoRaMacSerializerData( LoRaMacMessageData_t* macMsg )
123 {
124     if( ( macMsg == 0 ) || ( macMsg->Buffer == 0 ) )
125     {
126         return LORAMAC_SERIALIZER_ERROR_NPE;
127     }
128 
129     uint16_t bufItr = 0;
130 
131     // Check macMsg->BufSize
132     uint16_t computedBufSize =   LORAMAC_MHDR_FIELD_SIZE
133                                + LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE
134                                + LORAMAC_FHDR_F_CTRL_FIELD_SIZE
135                                + LORAMAC_FHDR_F_CNT_FIELD_SIZE;
136 
137     computedBufSize += macMsg->FHDR.FCtrl.Bits.FOptsLen;
138 
139     if( macMsg->FRMPayloadSize > 0 )
140     {
141         computedBufSize += LORAMAC_F_PORT_FIELD_SIZE;
142     }
143 
144     computedBufSize += macMsg->FRMPayloadSize;
145     computedBufSize += LORAMAC_MIC_FIELD_SIZE;
146 
147     if( macMsg->BufSize < computedBufSize )
148     {
149         return LORAMAC_SERIALIZER_ERROR_BUF_SIZE;
150     }
151 
152     macMsg->Buffer[bufItr++] = macMsg->MHDR.Value;
153 
154     macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr ) & 0xFF;
155     macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr >> 8 ) & 0xFF;
156     macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr >> 16 ) & 0xFF;
157     macMsg->Buffer[bufItr++] = ( macMsg->FHDR.DevAddr >> 24 ) & 0xFF;
158 
159     macMsg->Buffer[bufItr++] = macMsg->FHDR.FCtrl.Value;
160 
161     macMsg->Buffer[bufItr++] = macMsg->FHDR.FCnt & 0xFF;
162     macMsg->Buffer[bufItr++] = ( macMsg->FHDR.FCnt >> 8 ) & 0xFF;
163 
164     memcpy1( &macMsg->Buffer[bufItr], macMsg->FHDR.FOpts, macMsg->FHDR.FCtrl.Bits.FOptsLen );
165     bufItr = bufItr + macMsg->FHDR.FCtrl.Bits.FOptsLen;
166 
167     if( macMsg->FRMPayloadSize > 0 )
168     {
169         macMsg->Buffer[bufItr++] = macMsg->FPort;
170     }
171 
172     memcpy1( &macMsg->Buffer[bufItr], macMsg->FRMPayload, macMsg->FRMPayloadSize );
173     bufItr = bufItr + macMsg->FRMPayloadSize;
174 
175     macMsg->Buffer[bufItr++] = macMsg->MIC & 0xFF;
176     macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 8 ) & 0xFF;
177     macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 16 ) & 0xFF;
178     macMsg->Buffer[bufItr++] = ( macMsg->MIC >> 24 ) & 0xFF;
179 
180     macMsg->BufSize = bufItr;
181 
182     return LORAMAC_SERIALIZER_SUCCESS;
183 }
184