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