1 /*!
2  * \file      RegionAS923.h
3  *
4  * \brief     Region definition for AS923
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  REGIONAS923 Region AS923
34  *            Implementation according to LoRaWAN Specification v1.0.2.
35  * \{
36  */
37 #ifndef __REGION_AS923_H__
38 #define __REGION_AS923_H__
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 #include "region/Region.h"
46 
47 
48 /*!
49  * Channel plan group AS923-1
50  * AS923_FREQ_OFFSET = 0
51  */
52 #define CHANNEL_PLAN_GROUP_AS923_1                  1
53 
54 /*!
55  * Channel plan group AS923-2
56  * AS923_FREQ_OFFSET = -1.8MHz
57  */
58 #define CHANNEL_PLAN_GROUP_AS923_2                  2
59 
60 /*!
61  * Channel plan group AS923-3
62  * AS923_FREQ_OFFSET = -6.6MHz
63  */
64 #define CHANNEL_PLAN_GROUP_AS923_3                  3
65 
66 /*!
67  * Channel plan group AS923-4
68  * AS923_FREQ_OFFSET = -5.90MHz
69  */
70 #define CHANNEL_PLAN_GROUP_AS923_4                  4
71 
72 /*!
73  * Channel plan group AS923-1 for Japan - channels 24 to 38 Listen Before Talk
74  * AS923_FREQ_OFFSET = 0
75  */
76 #define CHANNEL_PLAN_GROUP_AS923_1_JP_CH24_CH38_LBT 5
77 
78 /*!
79  * Channel plan group AS923-1 for Japan - channels 24 to 38 Duty Cycle
80  * AS923_FREQ_OFFSET = 0
81  */
82 #define CHANNEL_PLAN_GROUP_AS923_1_JP_CH24_CH38_DC  6
83 
84 /*!
85  * Channel plan group AS923-1 for Japan - channels 37 to 61 Listen Before Talk + Duty Cycle
86  * AS923_FREQ_OFFSET = 0
87  */
88 #define CHANNEL_PLAN_GROUP_AS923_1_JP_CH37_CH61_LBT_DC 7
89 
90 /*!
91  * LoRaMac maximum number of channels
92  */
93 #define AS923_MAX_NB_CHANNELS                       16
94 
95 /*!
96  * Number of default channels
97  */
98 #define AS923_NUMB_DEFAULT_CHANNELS                 2
99 
100 /*!
101  * Number of channels to apply for the CF list
102  */
103 #define AS923_NUMB_CHANNELS_CF_LIST                 5
104 
105 /*!
106  * Minimal datarate that can be used by the node
107  */
108 #define AS923_TX_MIN_DATARATE                       DR_0
109 
110 /*!
111  * Maximal datarate that can be used by the node
112  */
113 #define AS923_TX_MAX_DATARATE                       DR_7
114 
115 /*!
116  * Minimal datarate that can be used by the node
117  */
118 #define AS923_RX_MIN_DATARATE                       DR_0
119 
120 /*!
121  * Maximal datarate that can be used by the node
122  */
123 #define AS923_RX_MAX_DATARATE                       DR_7
124 
125 /*!
126  * Default datarate used by the node
127  */
128 #define AS923_DEFAULT_DATARATE                      DR_2
129 
130 /*!
131  * The minimum datarate which is used when the
132  * dwell time is limited.
133  */
134 #define AS923_DWELL_LIMIT_DATARATE                  DR_2
135 
136 /*!
137  * Minimal Rx1 receive datarate offset
138  */
139 #define AS923_MIN_RX1_DR_OFFSET                     0
140 
141 /*!
142  * Maximal Rx1 receive datarate offset
143  */
144 #define AS923_MAX_RX1_DR_OFFSET                     7
145 
146 /*!
147  * Minimal Tx output power that can be used by the node
148  */
149 #define AS923_MIN_TX_POWER                          TX_POWER_7
150 
151 /*!
152  * Maximal Tx output power that can be used by the node
153  */
154 #define AS923_MAX_TX_POWER                          TX_POWER_0
155 
156 /*!
157  * Default Tx output power used by the node
158  */
159 #define AS923_DEFAULT_TX_POWER                      TX_POWER_0
160 
161 /*!
162  * Default uplink dwell time configuration
163  */
164 #define AS923_DEFAULT_UPLINK_DWELL_TIME             1
165 
166 /*!
167  * Default Max EIRP
168  */
169 #define AS923_DEFAULT_MAX_EIRP                      16.0f
170 
171 /*!
172  * Default antenna gain
173  */
174 #define AS923_DEFAULT_ANTENNA_GAIN                  2.15f
175 
176 /*!
177  * Enabled or disabled the duty cycle
178  */
179 #define AS923_DUTY_CYCLE_ENABLED                    0
180 
181 /*!
182  * Maximum RX window duration
183  */
184 #define AS923_MAX_RX_WINDOW                         3000
185 
186 #if ( AS923_DEFAULT_DATARATE > DR_5 )
187 #error "A default DR higher than DR_5 may lead to connectivity loss."
188 #endif
189 
190 /*!
191  * Second reception window channel frequency definition.
192  */
193 #define AS923_RX_WND_2_FREQ                         923200000
194 
195 /*!
196  * Second reception window channel datarate definition.
197  */
198 #define AS923_RX_WND_2_DR                           DR_2
199 
200 /*
201  * CLASS B
202  */
203 /*!
204  * Beacon frequency
205  */
206 #define AS923_BEACON_CHANNEL_FREQ                   923400000
207 
208 /*!
209  * Ping slot channel frequency
210  */
211 #define AS923_PING_SLOT_CHANNEL_FREQ                923400000
212 
213 /*!
214  * Payload size of a beacon frame
215  */
216 #define AS923_BEACON_SIZE                           17
217 
218 /*!
219  * Size of RFU 1 field
220  */
221 #define AS923_RFU1_SIZE                             1
222 
223 /*!
224  * Size of RFU 2 field
225  */
226 #define AS923_RFU2_SIZE                             0
227 
228 /*!
229  * Datarate of the beacon channel
230  */
231 #define AS923_BEACON_CHANNEL_DR                     DR_3
232 
233 /*!
234  * Bandwith of the beacon channel
235  */
236 #define AS923_BEACON_CHANNEL_BW                     0
237 
238 /*!
239  * Ping slot channel datarate
240  */
241 #define AS923_PING_SLOT_CHANNEL_DR                  DR_3
242 
243 /*!
244  * Maximum number of bands
245  */
246 #define AS923_MAX_NB_BANDS                          1
247 
248 /*!
249  * Band 0 definition
250  * Band = { DutyCycle, TxMaxPower, LastBandUpdateTime, LastMaxCreditAssignTime, TimeCredits, MaxTimeCredits, ReadyForTransmission }
251  */
252 #define AS923_BAND0                                 { 100, AS923_MAX_TX_POWER, 0, 0, 0, 0, 0 } //  1.0 %
253 
254 /*!
255  * LoRaMac default channel 1
256  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
257  */
258 #define AS923_LC1                                   { 923200000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
259 
260 /*!
261  * LoRaMac default channel 2
262  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
263  */
264 #define AS923_LC2                                   { 923400000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
265 
266 /*!
267  * LoRaMac channels which are allowed for the join procedure
268  */
269 #define AS923_JOIN_CHANNELS                         ( uint16_t )( LC( 1 ) | LC( 2 ) )
270 
271 /*!
272  * Data rates table definition
273  */
274 static const uint8_t DataratesAS923[]  = { 12, 11, 10,  9,  8,  7, 7, 50 };
275 
276 /*!
277  * Bandwidths table definition in Hz
278  */
279 static const uint32_t BandwidthsAS923[] = { 125000, 125000, 125000, 125000, 125000, 125000, 250000, 0 };
280 
281 /*!
282  * Maximum payload with respect to the datarate index.
283  * The table is valid for the dwell time configuration of 0 for uplinks and downlinks.
284  */
285 static const uint8_t MaxPayloadOfDatarateDwell0AS923[] = { 51, 51, 115, 115, 242, 242, 242, 242 };
286 
287 /*!
288  * Maximum payload with respect to the datarate index.
289  * The table is only valid for uplinks.
290  */
291 static const uint8_t MaxPayloadOfDatarateDwell1AS923[] = { 0, 0, 11, 53, 125, 242, 242, 242 };
292 
293 /*!
294  * Effective datarate offsets for receive window 1 when downlink dwell time is zero.
295  */
296 static const int8_t EffectiveRx1DrOffsetDownlinkDwell0AS923[8][8] =
297     {
298         { DR_0 , DR_0 , DR_0 , DR_0 , DR_0 , DR_0 , DR_1 , DR_2  }, // DR_0
299         { DR_1 , DR_0 , DR_0 , DR_0 , DR_0 , DR_0 , DR_2 , DR_3  }, // DR_1
300         { DR_2 , DR_1 , DR_0 , DR_0 , DR_0 , DR_0 , DR_3 , DR_4  }, // DR_2
301         { DR_3 , DR_2 , DR_1 , DR_0 , DR_0 , DR_0 , DR_4 , DR_5  }, // DR_3
302         { DR_4 , DR_3 , DR_2 , DR_1 , DR_0 , DR_0 , DR_5 , DR_6  }, // DR_4
303         { DR_5 , DR_4 , DR_3 , DR_2 , DR_1 , DR_0 , DR_6 , DR_7  }, // DR_5
304         { DR_6 , DR_5 , DR_4 , DR_3 , DR_2 , DR_1 , DR_7 , DR_7  }, // DR_6
305         { DR_7 , DR_6 , DR_5 , DR_4 , DR_3 , DR_2 , DR_7 , DR_7  }, // DR_7
306     };
307 
308 /*!
309  * Effective datarate offsets for receive window 1 when downlink dwell time is one.
310  */
311 static const int8_t EffectiveRx1DrOffsetDownlinkDwell1AS923[8][8] =
312     {
313         { DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2  }, // DR_0
314         { DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_3  }, // DR_1
315         { DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_3 , DR_4  }, // DR_2
316         { DR_3 , DR_2 , DR_2 , DR_2 , DR_2 , DR_2 , DR_4 , DR_5  }, // DR_3
317         { DR_4 , DR_3 , DR_2 , DR_2 , DR_2 , DR_2 , DR_5 , DR_6  }, // DR_4
318         { DR_5 , DR_4 , DR_3 , DR_2 , DR_2 , DR_2 , DR_6 , DR_7  }, // DR_5
319         { DR_6 , DR_5 , DR_4 , DR_3 , DR_2 , DR_2 , DR_7 , DR_7  }, // DR_6
320         { DR_7 , DR_6 , DR_5 , DR_4 , DR_3 , DR_2 , DR_7 , DR_7  }, // DR_7
321     };
322 
323 /*!
324  * \brief The function gets a value of a specific phy attribute.
325  *
326  * \param [IN] getPhy Pointer to the function parameters.
327  *
328  * \retval Returns a structure containing the PHY parameter.
329  */
330 PhyParam_t RegionAS923GetPhyParam( GetPhyParams_t* getPhy );
331 
332 /*!
333  * \brief Updates the last TX done parameters of the current channel.
334  *
335  * \param [IN] txDone Pointer to the function parameters.
336  */
337 void RegionAS923SetBandTxDone( SetBandTxDoneParams_t* txDone );
338 
339 /*!
340  * \brief Initializes the channels masks and the channels.
341  *
342  * \param [IN] type Sets the initialization type.
343  */
344 void RegionAS923InitDefaults( InitDefaultsParams_t* params );
345 
346 /*!
347  * \brief Verifies a parameter.
348  *
349  * \param [IN] verify Pointer to the function parameters.
350  *
351  * \param [IN] type Sets the initialization type.
352  *
353  * \retval Returns true, if the parameter is valid.
354  */
355 bool RegionAS923Verify( VerifyParams_t* verify, PhyAttribute_t phyAttribute );
356 
357 /*!
358  * \brief The function parses the input buffer and sets up the channels of the
359  *        CF list.
360  *
361  * \param [IN] applyCFList Pointer to the function parameters.
362  */
363 void RegionAS923ApplyCFList( ApplyCFListParams_t* applyCFList );
364 
365 /*!
366  * \brief Sets a channels mask.
367  *
368  * \param [IN] chanMaskSet Pointer to the function parameters.
369  *
370  * \retval Returns true, if the channels mask could be set.
371  */
372 bool RegionAS923ChanMaskSet( ChanMaskSetParams_t* chanMaskSet );
373 
374 /*!
375  * Computes the Rx window timeout and offset.
376  *
377  * \param [IN] datarate     Rx window datarate index to be used
378  *
379  * \param [IN] minRxSymbols Minimum required number of symbols to detect an Rx frame.
380  *
381  * \param [IN] rxError      System maximum timing error of the receiver. In milliseconds
382  *                          The receiver will turn on in a [-rxError : +rxError] ms
383  *                          interval around RxOffset
384  *
385  * \param [OUT]rxConfigParams Returns updated WindowTimeout and WindowOffset fields.
386  */
387 void RegionAS923ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams );
388 
389 /*!
390  * \brief Configuration of the RX windows.
391  *
392  * \param [IN] rxConfig Pointer to the function parameters.
393  *
394  * \param [OUT] datarate The datarate index which was set.
395  *
396  * \retval Returns true, if the configuration was applied successfully.
397  */
398 bool RegionAS923RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate );
399 
400 /*!
401  * \brief TX configuration.
402  *
403  * \param [IN] txConfig Pointer to the function parameters.
404  *
405  * \param [OUT] txPower The tx power index which was set.
406  *
407  * \param [OUT] txTimeOnAir The time-on-air of the frame.
408  *
409  * \retval Returns true, if the configuration was applied successfully.
410  */
411 bool RegionAS923TxConfig( TxConfigParams_t* txConfig, int8_t* txPower, TimerTime_t* txTimeOnAir );
412 
413 /*!
414  * \brief The function processes a Link ADR Request.
415  *
416  * \param [IN] linkAdrReq Pointer to the function parameters.
417  *
418  * \retval Returns the status of the operation, according to the LoRaMAC specification.
419  */
420 uint8_t RegionAS923LinkAdrReq( LinkAdrReqParams_t* linkAdrReq, int8_t* drOut, int8_t* txPowOut, uint8_t* nbRepOut, uint8_t* nbBytesParsed );
421 
422 /*!
423  * \brief The function processes a RX Parameter Setup Request.
424  *
425  * \param [IN] rxParamSetupReq Pointer to the function parameters.
426  *
427  * \retval Returns the status of the operation, according to the LoRaMAC specification.
428  */
429 uint8_t RegionAS923RxParamSetupReq( RxParamSetupReqParams_t* rxParamSetupReq );
430 
431 /*!
432  * \brief The function processes a Channel Request.
433  *
434  * \param [IN] newChannelReq Pointer to the function parameters.
435  *
436  * \retval Returns the status of the operation, according to the LoRaMAC specification.
437  */
438 int8_t RegionAS923NewChannelReq( NewChannelReqParams_t* newChannelReq );
439 
440 /*!
441  * \brief The function processes a TX ParamSetup Request.
442  *
443  * \param [IN] txParamSetupReq Pointer to the function parameters.
444  *
445  * \retval Returns the status of the operation, according to the LoRaMAC specification.
446  *         Returns -1, if the functionality is not implemented. In this case, the end node
447  *         shall not process the command.
448  */
449 int8_t RegionAS923TxParamSetupReq( TxParamSetupReqParams_t* txParamSetupReq );
450 
451 /*!
452  * \brief The function processes a DlChannel Request.
453  *
454  * \param [IN] dlChannelReq Pointer to the function parameters.
455  *
456  * \retval Returns the status of the operation, according to the LoRaMAC specification.
457  */
458 int8_t RegionAS923DlChannelReq( DlChannelReqParams_t* dlChannelReq );
459 
460 /*!
461  * \brief Alternates the datarate of the channel for the join request.
462  *
463  * \param [IN] currentDr Current datarate.
464  *
465  * \retval Datarate to apply.
466  */
467 int8_t RegionAS923AlternateDr( int8_t currentDr, AlternateDrType_t type );
468 
469 /*!
470  * \brief Searches and set the next random available channel
471  *
472  * \param [OUT] channel Next channel to use for TX.
473  *
474  * \param [OUT] time Time to wait for the next transmission according to the duty
475  *              cycle.
476  *
477  * \param [OUT] aggregatedTimeOff Updates the aggregated time off.
478  *
479  * \retval Function status [1: OK, 0: Unable to find a channel on the current datarate]
480  */
481 LoRaMacStatus_t RegionAS923NextChannel( NextChanParams_t* nextChanParams, uint8_t* channel, TimerTime_t* time, TimerTime_t* aggregatedTimeOff );
482 
483 /*!
484  * \brief Adds a channel.
485  *
486  * \param [IN] channelAdd Pointer to the function parameters.
487  *
488  * \retval Status of the operation.
489  */
490 LoRaMacStatus_t RegionAS923ChannelAdd( ChannelAddParams_t* channelAdd );
491 
492 /*!
493  * \brief Removes a channel.
494  *
495  * \param [IN] channelRemove Pointer to the function parameters.
496  *
497  * \retval Returns true, if the channel was removed successfully.
498  */
499 bool RegionAS923ChannelsRemove( ChannelRemoveParams_t* channelRemove  );
500 
501 /*!
502  * \brief Computes new datarate according to the given offset
503  *
504  * \param [IN] downlinkDwellTime Downlink dwell time configuration. 0: No limit, 1: 400ms
505  *
506  * \param [IN] dr Current datarate
507  *
508  * \param [IN] drOffset Offset to be applied
509  *
510  * \retval newDr Computed datarate.
511  */
512 uint8_t RegionAS923ApplyDrOffset( uint8_t downlinkDwellTime, int8_t dr, int8_t drOffset );
513 
514 /*!
515  * \brief Sets the radio into beacon reception mode
516  *
517  * \param [IN] rxBeaconSetup Pointer to the function parameters
518  */
519  void RegionAS923RxBeaconSetup( RxBeaconSetup_t* rxBeaconSetup, uint8_t* outDr );
520 
521 /*! \} defgroup REGIONAS923 */
522 
523 #ifdef __cplusplus
524 }
525 #endif
526 
527 #endif // __REGION_AS923_H__
528