1 /*!
2  * \file      RegionCN470.h
3  *
4  * \brief     Region definition for CN470
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 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  * \defgroup  REGIONCN470 Region CN470
34  *            Implementation according to LoRaWAN Specification v1.0.2.
35  * \{
36  */
37 #ifndef __REGION_CN470_H__
38 #define __REGION_CN470_H__
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 #include "region/Region.h"
46 
47 /*!
48  * LoRaMac maximum number of channels
49  */
50 #define CN470_MAX_NB_CHANNELS                        96
51 
52 /*!
53  * Minimal datarate that can be used by the node
54  */
55 #define CN470_TX_MIN_DATARATE                       DR_1
56 
57 /*!
58  * Maximal datarate that can be used by the node
59  */
60 #define CN470_TX_MAX_DATARATE                       DR_5
61 
62 /*!
63  * Minimal datarate that can be used by the node
64  */
65 #define CN470_RX_MIN_DATARATE                       DR_1
66 
67 /*!
68  * Maximal datarate that can be used by the node
69  */
70 #define CN470_RX_MAX_DATARATE                       DR_5
71 
72 /*!
73  * Default datarate used by the node
74  */
75 #define CN470_DEFAULT_DATARATE                      DR_1
76 
77 /*!
78  * Minimal Rx1 receive datarate offset
79  */
80 #define CN470_MIN_RX1_DR_OFFSET                     0
81 
82 /*!
83  * Maximal Rx1 receive datarate offset
84  */
85 #define CN470_MAX_RX1_DR_OFFSET                     5
86 
87 /*!
88  * Minimal Tx output power that can be used by the node
89  */
90 #define CN470_MIN_TX_POWER                          TX_POWER_7
91 
92 /*!
93  * Maximal Tx output power that can be used by the node
94  */
95 #define CN470_MAX_TX_POWER                          TX_POWER_0
96 
97 /*!
98  * Default Tx output power used by the node
99  */
100 #define CN470_DEFAULT_TX_POWER                      TX_POWER_0
101 
102 /*!
103  * Default Max EIRP
104  */
105 #define CN470_DEFAULT_MAX_EIRP                      19.15f
106 
107 /*!
108  * Default antenna gain
109  */
110 #define CN470_DEFAULT_ANTENNA_GAIN                  2.15f
111 
112 /*!
113  * Enabled or disabled the duty cycle
114  */
115 #define CN470_DUTY_CYCLE_ENABLED                    0
116 
117 /*!
118  * Maximum RX window duration
119  */
120 #define CN470_MAX_RX_WINDOW                         3000
121 
122 /*!
123  * Second reception window channel datarate definition.
124  */
125 #define CN470_RX_WND_2_DR                           DR_1
126 
127 /*!
128  * Default uplink dwell time configuration
129  */
130 #define CN470_DEFAULT_UPLINK_DWELL_TIME             0
131 
132 /*
133  * CLASS B
134  */
135 
136 /*!
137  * Payload size of a beacon frame
138  */
139 #define CN470_BEACON_SIZE                           19
140 
141 /*!
142  * Size of RFU 1 field
143  */
144 #define CN470_RFU1_SIZE                             2
145 
146 /*!
147  * Size of RFU 2 field
148  */
149 #define CN470_RFU2_SIZE                             1
150 
151 /*!
152  * Datarate of the beacon channel
153  */
154 #define CN470_BEACON_CHANNEL_DR                     DR_2
155 
156 /*!
157  * Bandwith of the beacon channel
158  */
159 #define CN470_BEACON_CHANNEL_BW                     0
160 
161 /*!
162  * Ping slot channel datarate
163  */
164 #define CN470_PING_SLOT_CHANNEL_DR                  DR_2
165 
166 /*!
167  * LoRaMac maximum number of bands
168  */
169 #define CN470_MAX_NB_BANDS                          1
170 
171 /*!
172  * Band 0 definition
173  * Band = { DutyCycle, TxMaxPower, LastBandUpdateTime, LastMaxCreditAssignTime, TimeCredits, MaxTimeCredits, ReadyForTransmission }
174  */
175 #define CN470_BAND0                                 { 1, CN470_MAX_TX_POWER, 0, 0, 0, 0, 0 } //  100.0 %
176 
177 /*!
178  * Defines the first channel for RX window 1 for CN470 band
179  */
180 #define CN470_FIRST_RX1_CHANNEL                     ( (uint32_t) 500300000 )
181 
182 /*!
183  * Defines the last channel for RX window 1 for CN470 band
184  */
185 #define CN470_LAST_RX1_CHANNEL                      ( (uint32_t) 509700000 )
186 
187 /*!
188  * Defines the step width of the channels for RX window 1
189  */
190 #define CN470_STEPWIDTH_RX1_CHANNEL                 ( (uint32_t) 200000 )
191 
192 #define CN470_DEFAULT_DR_RANGE                      { .Value = ( CN470_TX_MAX_DATARATE << 4 ) | CN470_TX_MIN_DATARATE }
193 
194 #define CN470_COMMON_JOIN_CHANNELS \
195 { \
196     { .Frequency = 470900000, .Rx1Frequency = 484500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
197     { .Frequency = 472500000, .Rx1Frequency = 486100000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
198     { .Frequency = 474100000, .Rx1Frequency = 487700000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
199     { .Frequency = 475700000, .Rx1Frequency = 489300000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
200     { .Frequency = 504100000, .Rx1Frequency = 490900000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
201     { .Frequency = 505700000, .Rx1Frequency = 492500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
202     { .Frequency = 507300000, .Rx1Frequency = 494100000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
203     { .Frequency = 508900000, .Rx1Frequency = 495700000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
204 \
205     { .Frequency = 479900000, .Rx1Frequency = 479900000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
206     { .Frequency = 499900000, .Rx1Frequency = 499900000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
207 \
208     { .Frequency = 470300000, .Rx1Frequency = 492500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
209     { .Frequency = 472300000, .Rx1Frequency = 492500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
210     { .Frequency = 474300000, .Rx1Frequency = 492500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
211     { .Frequency = 476300000, .Rx1Frequency = 492500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
212     { .Frequency = 478300000, .Rx1Frequency = 492500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
213 \
214     { .Frequency = 480300000, .Rx1Frequency = 502500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
215     { .Frequency = 482300000, .Rx1Frequency = 502500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
216     { .Frequency = 484300000, .Rx1Frequency = 502500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
217     { .Frequency = 486300000, .Rx1Frequency = 502500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
218     { .Frequency = 488300000, .Rx1Frequency = 502500000, CN470_DEFAULT_DR_RANGE, .Band = 0 }, \
219 }
220 
221 #define CN470_COMMON_JOIN_CHANNELS_SIZE             20
222 
223 #define CN470_JOIN_CHANNELS                         { 0xFFFF, 0x000F }
224 
225 /*!
226  * Data rates table definition
227  */
228 static const uint8_t DataratesCN470[]  = { 12, 11, 10,  9,  8,  7, 7, 50 };
229 
230 /*!
231  * Bandwidths table definition in Hz
232  */
233 static const uint32_t BandwidthsCN470[] = { 125000, 125000, 125000, 125000, 125000, 125000, 500000, 0 };
234 
235 /*!
236  * Up/Down link data rates offset definition
237  */
238 static const int8_t DatarateOffsetsCN470[8][6] =
239 {
240     { DR_0, DR_0, DR_0, DR_0, DR_0, DR_0 }, // DR_0
241     { DR_1, DR_1, DR_1, DR_1, DR_1, DR_1 }, // DR_1
242     { DR_2, DR_1, DR_1, DR_1, DR_1, DR_1 }, // DR_2
243     { DR_3, DR_2, DR_1, DR_1, DR_1, DR_1 }, // DR_3
244     { DR_4, DR_3, DR_2, DR_1, DR_1, DR_1 }, // DR_4
245     { DR_5, DR_4, DR_3, DR_2, DR_1, DR_1 }, // DR_5
246     { DR_6, DR_5, DR_4, DR_3, DR_2, DR_1 }, // DR_6
247     { DR_7, DR_6, DR_5, DR_4, DR_3, DR_2 }, // DR_7
248 };
249 
250 /*!
251  * Maximum payload with respect to the datarate index. Cannot operate with repeater.
252  */
253 static const uint8_t MaxPayloadOfDatarateCN470[] = { 0, 23, 86, 184, 242, 242, 242, 242 };
254 
255 /*!
256  * \brief The function gets a value of a specific phy attribute.
257  *
258  * \param [IN] getPhy Pointer to the function parameters.
259  *
260  * \retval Returns a structure containing the PHY parameter.
261  */
262 PhyParam_t RegionCN470GetPhyParam( GetPhyParams_t* getPhy );
263 
264 /*!
265  * \brief Updates the last TX done parameters of the current channel.
266  *
267  * \param [IN] txDone Pointer to the function parameters.
268  */
269 void RegionCN470SetBandTxDone( SetBandTxDoneParams_t* txDone );
270 
271 /*!
272  * \brief Initializes the channels masks and the channels.
273  *
274  * \param [IN] type Sets the initialization type.
275  */
276 void RegionCN470InitDefaults( InitDefaultsParams_t* params );
277 
278 /*!
279  * \brief Verifies a parameter.
280  *
281  * \param [IN] verify Pointer to the function parameters.
282  *
283  * \param [IN] type Sets the initialization type.
284  *
285  * \retval Returns true, if the parameter is valid.
286  */
287 bool RegionCN470Verify( VerifyParams_t* verify, PhyAttribute_t phyAttribute );
288 
289 /*!
290  * \brief The function parses the input buffer and sets up the channels of the
291  *        CF list.
292  *
293  * \param [IN] applyCFList Pointer to the function parameters.
294  */
295 void RegionCN470ApplyCFList( ApplyCFListParams_t* applyCFList );
296 
297 /*!
298  * \brief Sets a channels mask.
299  *
300  * \param [IN] chanMaskSet Pointer to the function parameters.
301  *
302  * \retval Returns true, if the channels mask could be set.
303  */
304 bool RegionCN470ChanMaskSet( ChanMaskSetParams_t* chanMaskSet );
305 
306 /*!
307  * Computes the Rx window timeout and offset.
308  *
309  * \param [IN] datarate     Rx window datarate index to be used
310  *
311  * \param [IN] minRxSymbols Minimum required number of symbols to detect an Rx frame.
312  *
313  * \param [IN] rxError      System maximum timing error of the receiver. In milliseconds
314  *                          The receiver will turn on in a [-rxError : +rxError] ms
315  *                          interval around RxOffset
316  *
317  * \param [OUT]rxConfigParams Returns updated WindowTimeout and WindowOffset fields.
318  */
319 void RegionCN470ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams );
320 
321 /*!
322  * \brief Configuration of the RX windows.
323  *
324  * \param [IN] rxConfig Pointer to the function parameters.
325  *
326  * \param [OUT] datarate The datarate index which was set.
327  *
328  * \retval Returns true, if the configuration was applied successfully.
329  */
330 bool RegionCN470RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate );
331 
332 /*!
333  * \brief TX configuration.
334  *
335  * \param [IN] txConfig Pointer to the function parameters.
336  *
337  * \param [OUT] txPower The tx power index which was set.
338  *
339  * \param [OUT] txTimeOnAir The time-on-air of the frame.
340  *
341  * \retval Returns true, if the configuration was applied successfully.
342  */
343 bool RegionCN470TxConfig( TxConfigParams_t* txConfig, int8_t* txPower, TimerTime_t* txTimeOnAir );
344 
345 /*!
346  * \brief The function processes a Link ADR Request.
347  *
348  * \param [IN] linkAdrReq Pointer to the function parameters.
349  *
350  * \retval Returns the status of the operation, according to the LoRaMAC specification.
351  */
352 uint8_t RegionCN470LinkAdrReq( LinkAdrReqParams_t* linkAdrReq, int8_t* drOut, int8_t* txPowOut, uint8_t* nbRepOut, uint8_t* nbBytesParsed );
353 
354 /*!
355  * \brief The function processes a RX Parameter Setup Request.
356  *
357  * \param [IN] rxParamSetupReq Pointer to the function parameters.
358  *
359  * \retval Returns the status of the operation, according to the LoRaMAC specification.
360  */
361 uint8_t RegionCN470RxParamSetupReq( RxParamSetupReqParams_t* rxParamSetupReq );
362 
363 /*!
364  * \brief The function processes a Channel Request.
365  *
366  * \param [IN] newChannelReq Pointer to the function parameters.
367  *
368  * \retval Returns the status of the operation, according to the LoRaMAC specification.
369  */
370 int8_t RegionCN470NewChannelReq( NewChannelReqParams_t* newChannelReq );
371 
372 /*!
373  * \brief The function processes a TX ParamSetup Request.
374  *
375  * \param [IN] txParamSetupReq Pointer to the function parameters.
376  *
377  * \retval Returns the status of the operation, according to the LoRaMAC specification.
378  *         Returns -1, if the functionality is not implemented. In this case, the end node
379  *         shall not process the command.
380  */
381 int8_t RegionCN470TxParamSetupReq( TxParamSetupReqParams_t* txParamSetupReq );
382 
383 /*!
384  * \brief The function processes a DlChannel Request.
385  *
386  * \param [IN] dlChannelReq Pointer to the function parameters.
387  *
388  * \retval Returns the status of the operation, according to the LoRaMAC specification.
389  */
390 int8_t RegionCN470DlChannelReq( DlChannelReqParams_t* dlChannelReq );
391 
392 /*!
393  * \brief Alternates the datarate of the channel for the join request.
394  *
395  * \param [IN] currentDr Current datarate.
396  *
397  * \retval Datarate to apply.
398  */
399 int8_t RegionCN470AlternateDr( int8_t currentDr, AlternateDrType_t type );
400 
401 /*!
402  * \brief Searches and set the next random available channel
403  *
404  * \param [OUT] channel Next channel to use for TX.
405  *
406  * \param [OUT] time Time to wait for the next transmission according to the duty
407  *              cycle.
408  *
409  * \param [OUT] aggregatedTimeOff Updates the aggregated time off.
410  *
411  * \retval Function status [1: OK, 0: Unable to find a channel on the current datarate]
412  */
413 LoRaMacStatus_t RegionCN470NextChannel( NextChanParams_t* nextChanParams, uint8_t* channel, TimerTime_t* time, TimerTime_t* aggregatedTimeOff );
414 
415 /*!
416  * \brief Adds a channel.
417  *
418  * \param [IN] channelAdd Pointer to the function parameters.
419  *
420  * \retval Status of the operation.
421  */
422 LoRaMacStatus_t RegionCN470ChannelAdd( ChannelAddParams_t* channelAdd );
423 
424 /*!
425  * \brief Removes a channel.
426  *
427  * \param [IN] channelRemove Pointer to the function parameters.
428  *
429  * \retval Returns true, if the channel was removed successfully.
430  */
431 bool RegionCN470ChannelsRemove( ChannelRemoveParams_t* channelRemove  );
432 
433 /*!
434  * \brief Computes new datarate according to the given offset
435  *
436  * \param [IN] downlinkDwellTime Downlink dwell time configuration. 0: No limit, 1: 400ms
437  *
438  * \param [IN] dr Current datarate
439  *
440  * \param [IN] drOffset Offset to be applied
441  *
442  * \retval newDr Computed datarate.
443  */
444 uint8_t RegionCN470ApplyDrOffset( uint8_t downlinkDwellTime, int8_t dr, int8_t drOffset );
445 
446 /*!
447  * \brief Sets the radio into beacon reception mode
448  *
449  * \param [IN] rxBeaconSetup Pointer to the function parameters
450  */
451  void RegionCN470RxBeaconSetup( RxBeaconSetup_t* rxBeaconSetup, uint8_t* outDr );
452 
453 /*! \} defgroup REGIONCN470 */
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 #endif // __REGION_CN470_H__
460