1 /*!
2  * \file      RegionKR920.h
3  *
4  * \brief     Region definition for KR920
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  REGIONKR920 Region KR920
34  *            Implementation according to LoRaWAN Specification v1.0.2.
35  * \{
36  */
37 #ifndef __REGION_KR920_H__
38 #define __REGION_KR920_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 KR920_MAX_NB_CHANNELS                       16
51 
52 /*!
53  * Number of default channels
54  */
55 #define KR920_NUMB_DEFAULT_CHANNELS                 3
56 
57 /*!
58  * Number of channels to apply for the CF list
59  */
60 #define KR920_NUMB_CHANNELS_CF_LIST                 5
61 
62 /*!
63  * Minimal datarate that can be used by the node
64  */
65 #define KR920_TX_MIN_DATARATE                       DR_0
66 
67 /*!
68  * Maximal datarate that can be used by the node
69  */
70 #define KR920_TX_MAX_DATARATE                       DR_5
71 
72 /*!
73  * Minimal datarate that can be used by the node
74  */
75 #define KR920_RX_MIN_DATARATE                       DR_0
76 
77 /*!
78  * Maximal datarate that can be used by the node
79  */
80 #define KR920_RX_MAX_DATARATE                       DR_5
81 
82 /*!
83  * Default datarate used by the node
84  */
85 #define KR920_DEFAULT_DATARATE                      DR_0
86 
87 /*!
88  * Minimal Rx1 receive datarate offset
89  */
90 #define KR920_MIN_RX1_DR_OFFSET                     0
91 
92 /*!
93  * Maximal Rx1 receive datarate offset
94  */
95 #define KR920_MAX_RX1_DR_OFFSET                     5
96 
97 /*!
98  * Minimal Tx output power that can be used by the node
99  */
100 #define KR920_MIN_TX_POWER                          TX_POWER_7
101 
102 /*!
103  * Maximal Tx output power that can be used by the node
104  */
105 #define KR920_MAX_TX_POWER                          TX_POWER_0
106 
107 /*!
108  * Default Tx output power used by the node
109  */
110 #define KR920_DEFAULT_TX_POWER                      TX_POWER_0
111 
112 /*!
113  * Default Max EIRP for frequency 920.9 MHz - 921.9 MHz
114  */
115 #define KR920_DEFAULT_MAX_EIRP_LOW                  10.0f
116 
117 /*!
118  * Default Max EIRP for frequency 922.1 MHz - 923.3 MHz
119  */
120 #define KR920_DEFAULT_MAX_EIRP_HIGH                 14.0f
121 
122 /*!
123  * Default antenna gain
124  */
125 #define KR920_DEFAULT_ANTENNA_GAIN                  2.15f
126 
127 /*!
128  * Enabled or disabled the duty cycle
129  */
130 #define KR920_DUTY_CYCLE_ENABLED                    0
131 
132 /*!
133  * Maximum RX window duration
134  */
135 #define KR920_MAX_RX_WINDOW                         4000
136 
137 #if ( KR920_DEFAULT_DATARATE > DR_5 )
138 #error "A default DR higher than DR_5 may lead to connectivity loss."
139 #endif
140 
141 /*!
142  * Second reception window channel frequency definition.
143  */
144 #define KR920_RX_WND_2_FREQ                         921900000
145 
146 /*!
147  * Second reception window channel datarate definition.
148  */
149 #define KR920_RX_WND_2_DR                           DR_0
150 
151 /*!
152  * Default uplink dwell time configuration
153  */
154 #define KR920_DEFAULT_UPLINK_DWELL_TIME             0
155 
156 /*
157  * CLASS B
158  */
159 /*!
160  * Beacon frequency
161  */
162 #define KR920_BEACON_CHANNEL_FREQ                   923100000
163 
164 /*!
165  * Ping slot channel frequency
166  */
167 #define KR920_PING_SLOT_CHANNEL_FREQ                923100000
168 
169 /*!
170  * Payload size of a beacon frame
171  */
172 #define KR920_BEACON_SIZE                           17
173 
174 /*!
175  * Size of RFU 1 field
176  */
177 #define KR920_RFU1_SIZE                             1
178 
179 /*!
180  * Size of RFU 2 field
181  */
182 #define KR920_RFU2_SIZE                             0
183 
184 /*!
185  * Datarate of the beacon channel
186  */
187 #define KR920_BEACON_CHANNEL_DR                     DR_3
188 
189 /*!
190  * Bandwith of the beacon channel
191  */
192 #define KR920_BEACON_CHANNEL_BW                     0
193 
194 /*!
195  * Ping slot channel datarate
196  */
197 #define KR920_PING_SLOT_CHANNEL_DR                  DR_3
198 
199 /*!
200  * Maximum number of bands
201  */
202 #define KR920_MAX_NB_BANDS                          1
203 
204 /*!
205  * Band 0 definition
206  * Band = { DutyCycle, TxMaxPower, LastBandUpdateTime, LastMaxCreditAssignTime, TimeCredits, MaxTimeCredits, ReadyForTransmission }
207  */
208 #define KR920_BAND0                                 { 1 , KR920_MAX_TX_POWER, 0, 0, 0, 0, 0 } //  100.0 %
209 
210 /*!
211  * LoRaMac default channel 1
212  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
213  */
214 #define KR920_LC1                                   { 922100000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
215 
216 /*!
217  * LoRaMac default channel 2
218  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
219  */
220 #define KR920_LC2                                   { 922300000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
221 
222 /*!
223  * LoRaMac default channel 3
224  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
225  */
226 #define KR920_LC3                                   { 922500000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
227 
228 /*!
229  * LoRaMac channels which are allowed for the join procedure
230  */
231 #define KR920_JOIN_CHANNELS                         ( uint16_t )( LC( 1 ) | LC( 2 ) | LC( 3 ) )
232 
233 /*!
234  * Data rates table definition
235  */
236 static const uint8_t DataratesKR920[]  = { 12, 11, 10,  9,  8,  7 };
237 
238 /*!
239  * Bandwidths table definition in Hz
240  */
241 static const uint32_t BandwidthsKR920[] = { 125000, 125000, 125000, 125000, 125000, 125000 };
242 
243 /*!
244  * Maximum payload with respect to the datarate index.
245  */
246 static const uint8_t MaxPayloadOfDatarateKR920[] = { 51, 51, 51, 115, 242, 242 };
247 
248 /*!
249  * \brief The function gets a value of a specific phy attribute.
250  *
251  * \param [IN] getPhy Pointer to the function parameters.
252  *
253  * \retval Returns a structure containing the PHY parameter.
254  */
255 PhyParam_t RegionKR920GetPhyParam( GetPhyParams_t* getPhy );
256 
257 /*!
258  * \brief Updates the last TX done parameters of the current channel.
259  *
260  * \param [IN] txDone Pointer to the function parameters.
261  */
262 void RegionKR920SetBandTxDone( SetBandTxDoneParams_t* txDone );
263 
264 /*!
265  * \brief Initializes the channels masks and the channels.
266  *
267  * \param [IN] type Sets the initialization type.
268  */
269 void RegionKR920InitDefaults( InitDefaultsParams_t* params );
270 
271 /*!
272  * \brief Verifies a parameter.
273  *
274  * \param [IN] verify Pointer to the function parameters.
275  *
276  * \param [IN] type Sets the initialization type.
277  *
278  * \retval Returns true, if the parameter is valid.
279  */
280 bool RegionKR920Verify( VerifyParams_t* verify, PhyAttribute_t phyAttribute );
281 
282 /*!
283  * \brief The function parses the input buffer and sets up the channels of the
284  *        CF list.
285  *
286  * \param [IN] applyCFList Pointer to the function parameters.
287  */
288 void RegionKR920ApplyCFList( ApplyCFListParams_t* applyCFList );
289 
290 /*!
291  * \brief Sets a channels mask.
292  *
293  * \param [IN] chanMaskSet Pointer to the function parameters.
294  *
295  * \retval Returns true, if the channels mask could be set.
296  */
297 bool RegionKR920ChanMaskSet( ChanMaskSetParams_t* chanMaskSet );
298 
299 /*!
300  * Computes the Rx window timeout and offset.
301  *
302  * \param [IN] datarate     Rx window datarate index to be used
303  *
304  * \param [IN] minRxSymbols Minimum required number of symbols to detect an Rx frame.
305  *
306  * \param [IN] rxError      System maximum timing error of the receiver. In milliseconds
307  *                          The receiver will turn on in a [-rxError : +rxError] ms
308  *                          interval around RxOffset
309  *
310  * \param [OUT]rxConfigParams Returns updated WindowTimeout and WindowOffset fields.
311  */
312 void RegionKR920ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams );
313 
314 /*!
315  * \brief Configuration of the RX windows.
316  *
317  * \param [IN] rxConfig Pointer to the function parameters.
318  *
319  * \param [OUT] datarate The datarate index which was set.
320  *
321  * \retval Returns true, if the configuration was applied successfully.
322  */
323 bool RegionKR920RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate );
324 
325 /*!
326  * \brief TX configuration.
327  *
328  * \param [IN] txConfig Pointer to the function parameters.
329  *
330  * \param [OUT] txPower The tx power index which was set.
331  *
332  * \param [OUT] txTimeOnAir The time-on-air of the frame.
333  *
334  * \retval Returns true, if the configuration was applied successfully.
335  */
336 bool RegionKR920TxConfig( TxConfigParams_t* txConfig, int8_t* txPower, TimerTime_t* txTimeOnAir );
337 
338 /*!
339  * \brief The function processes a Link ADR Request.
340  *
341  * \param [IN] linkAdrReq Pointer to the function parameters.
342  *
343  * \retval Returns the status of the operation, according to the LoRaMAC specification.
344  */
345 uint8_t RegionKR920LinkAdrReq( LinkAdrReqParams_t* linkAdrReq, int8_t* drOut, int8_t* txPowOut, uint8_t* nbRepOut, uint8_t* nbBytesParsed );
346 
347 /*!
348  * \brief The function processes a RX Parameter Setup Request.
349  *
350  * \param [IN] rxParamSetupReq Pointer to the function parameters.
351  *
352  * \retval Returns the status of the operation, according to the LoRaMAC specification.
353  */
354 uint8_t RegionKR920RxParamSetupReq( RxParamSetupReqParams_t* rxParamSetupReq );
355 
356 /*!
357  * \brief The function processes a Channel Request.
358  *
359  * \param [IN] newChannelReq Pointer to the function parameters.
360  *
361  * \retval Returns the status of the operation, according to the LoRaMAC specification.
362  */
363 int8_t RegionKR920NewChannelReq( NewChannelReqParams_t* newChannelReq );
364 
365 /*!
366  * \brief The function processes a TX ParamSetup Request.
367  *
368  * \param [IN] txParamSetupReq Pointer to the function parameters.
369  *
370  * \retval Returns the status of the operation, according to the LoRaMAC specification.
371  *         Returns -1, if the functionality is not implemented. In this case, the end node
372  *         shall not process the command.
373  */
374 int8_t RegionKR920TxParamSetupReq( TxParamSetupReqParams_t* txParamSetupReq );
375 
376 /*!
377  * \brief The function processes a DlChannel Request.
378  *
379  * \param [IN] dlChannelReq Pointer to the function parameters.
380  *
381  * \retval Returns the status of the operation, according to the LoRaMAC specification.
382  */
383 int8_t RegionKR920DlChannelReq( DlChannelReqParams_t* dlChannelReq );
384 
385 /*!
386  * \brief Alternates the datarate of the channel for the join request.
387  *
388  * \param [IN] currentDr current datarate.
389  *
390  * \retval Datarate to apply.
391  */
392 int8_t RegionKR920AlternateDr( int8_t currentDr, AlternateDrType_t type );
393 
394 /*!
395  * \brief Searches and set the next random available channel
396  *
397  * \param [OUT] channel Next channel to use for TX.
398  *
399  * \param [OUT] time Time to wait for the next transmission according to the duty
400  *              cycle.
401  *
402  * \param [OUT] aggregatedTimeOff Updates the aggregated time off.
403  *
404  * \retval Function status [1: OK, 0: Unable to find a channel on the current datarate]
405  */
406 LoRaMacStatus_t RegionKR920NextChannel( NextChanParams_t* nextChanParams, uint8_t* channel, TimerTime_t* time, TimerTime_t* aggregatedTimeOff );
407 
408 /*!
409  * \brief Adds a channel.
410  *
411  * \param [IN] channelAdd Pointer to the function parameters.
412  *
413  * \retval Status of the operation.
414  */
415 LoRaMacStatus_t RegionKR920ChannelAdd( ChannelAddParams_t* channelAdd );
416 
417 /*!
418  * \brief Removes a channel.
419  *
420  * \param [IN] channelRemove Pointer to the function parameters.
421  *
422  * \retval Returns true, if the channel was removed successfully.
423  */
424 bool RegionKR920ChannelsRemove( ChannelRemoveParams_t* channelRemove  );
425 
426 /*!
427  * \brief Computes new datarate according to the given offset
428  *
429  * \param [IN] downlinkDwellTime Downlink dwell time configuration. 0: No limit, 1: 400ms
430  *
431  * \param [IN] dr Current datarate
432  *
433  * \param [IN] drOffset Offset to be applied
434  *
435  * \retval newDr Computed datarate.
436  */
437 uint8_t RegionKR920ApplyDrOffset( uint8_t downlinkDwellTime, int8_t dr, int8_t drOffset );
438 
439 /*!
440  * \brief Sets the radio into beacon reception mode
441  *
442  * \param [IN] rxBeaconSetup Pointer to the function parameters
443  */
444  void RegionKR920RxBeaconSetup( RxBeaconSetup_t* rxBeaconSetup, uint8_t* outDr );
445 
446 /*! \} defgroup REGIONKR920 */
447 
448 #ifdef __cplusplus
449 }
450 #endif
451 
452 #endif // __REGION_KR920_H__
453