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