1 /*!
2  * \file      RegionRU864.c
3  *
4  * \brief     Region implementation for RU864
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 #include "radio.h"
32 #include "RegionCommon.h"
33 #include "RegionRU864.h"
34 
35 // Definitions
36 #define CHANNELS_MASK_SIZE              1
37 
38 /*
39  * Non-volatile module context.
40  */
41 static RegionNvmDataGroup1_t* RegionNvmGroup1;
42 static RegionNvmDataGroup2_t* RegionNvmGroup2;
43 static Band_t* RegionBands;
44 
45 // Static functions
VerifyRfFreq(uint32_t freq)46 static bool VerifyRfFreq( uint32_t freq )
47 {
48     // Check radio driver support
49     if( Radio.CheckRfFrequency( freq ) == false )
50     {
51         return false;
52     }
53 
54     // Check frequency bands
55     if( ( freq < 864000000 ) ||  ( freq > 870000000 ) )
56     {
57         return false;
58     }
59     return true;
60 }
61 
GetTimeOnAir(int8_t datarate,uint16_t pktLen)62 static TimerTime_t GetTimeOnAir( int8_t datarate, uint16_t pktLen )
63 {
64     int8_t phyDr = DataratesRU864[datarate];
65     uint32_t bandwidth = RegionCommonGetBandwidth( datarate, BandwidthsRU864 );
66     TimerTime_t timeOnAir = 0;
67 
68     if( datarate == DR_7 )
69     { // High Speed FSK channel
70         timeOnAir = Radio.TimeOnAir( MODEM_FSK, bandwidth, phyDr * 1000, 0, 5, false, pktLen, true );
71     }
72     else
73     {
74         timeOnAir = Radio.TimeOnAir( MODEM_LORA, bandwidth, phyDr, 1, 8, false, pktLen, true );
75     }
76     return timeOnAir;
77 }
78 
RegionRU864GetPhyParam(GetPhyParams_t * getPhy)79 PhyParam_t RegionRU864GetPhyParam( GetPhyParams_t* getPhy )
80 {
81     PhyParam_t phyParam = { 0 };
82 
83     switch( getPhy->Attribute )
84     {
85         case PHY_MIN_RX_DR:
86         {
87             phyParam.Value = RU864_RX_MIN_DATARATE;
88             break;
89         }
90         case PHY_MIN_TX_DR:
91         {
92             phyParam.Value = RU864_TX_MIN_DATARATE;
93             break;
94         }
95         case PHY_DEF_TX_DR:
96         {
97             phyParam.Value = RU864_DEFAULT_DATARATE;
98             break;
99         }
100         case PHY_NEXT_LOWER_TX_DR:
101         {
102             RegionCommonGetNextLowerTxDrParams_t nextLowerTxDrParams =
103             {
104                 .CurrentDr = getPhy->Datarate,
105                 .MaxDr = ( int8_t )RU864_TX_MAX_DATARATE,
106                 .MinDr = ( int8_t )RU864_TX_MIN_DATARATE,
107                 .NbChannels = RU864_MAX_NB_CHANNELS,
108                 .ChannelsMask = RegionNvmGroup2->ChannelsMask,
109                 .Channels = RegionNvmGroup2->Channels,
110             };
111             phyParam.Value = RegionCommonGetNextLowerTxDr( &nextLowerTxDrParams );
112             break;
113         }
114         case PHY_MAX_TX_POWER:
115         {
116             phyParam.Value = RU864_MAX_TX_POWER;
117             break;
118         }
119         case PHY_DEF_TX_POWER:
120         {
121             phyParam.Value = RU864_DEFAULT_TX_POWER;
122             break;
123         }
124         case PHY_DEF_ADR_ACK_LIMIT:
125         {
126             phyParam.Value = REGION_COMMON_DEFAULT_ADR_ACK_LIMIT;
127             break;
128         }
129         case PHY_DEF_ADR_ACK_DELAY:
130         {
131             phyParam.Value = REGION_COMMON_DEFAULT_ADR_ACK_DELAY;
132             break;
133         }
134         case PHY_MAX_PAYLOAD:
135         {
136             phyParam.Value = MaxPayloadOfDatarateRU864[getPhy->Datarate];
137             break;
138         }
139         case PHY_DUTY_CYCLE:
140         {
141             phyParam.Value = RU864_DUTY_CYCLE_ENABLED;
142             break;
143         }
144         case PHY_MAX_RX_WINDOW:
145         {
146             phyParam.Value = RU864_MAX_RX_WINDOW;
147             break;
148         }
149         case PHY_RECEIVE_DELAY1:
150         {
151             phyParam.Value = REGION_COMMON_DEFAULT_RECEIVE_DELAY1;
152             break;
153         }
154         case PHY_RECEIVE_DELAY2:
155         {
156             phyParam.Value = REGION_COMMON_DEFAULT_RECEIVE_DELAY2;
157             break;
158         }
159         case PHY_JOIN_ACCEPT_DELAY1:
160         {
161             phyParam.Value = REGION_COMMON_DEFAULT_JOIN_ACCEPT_DELAY1;
162             break;
163         }
164         case PHY_JOIN_ACCEPT_DELAY2:
165         {
166             phyParam.Value = REGION_COMMON_DEFAULT_JOIN_ACCEPT_DELAY2;
167             break;
168         }
169         case PHY_RETRANSMIT_TIMEOUT:
170         {
171             phyParam.Value = ( REGION_COMMON_DEFAULT_RETRANSMIT_TIMEOUT + randr( -REGION_COMMON_DEFAULT_RETRANSMIT_TIMEOUT_RND, REGION_COMMON_DEFAULT_RETRANSMIT_TIMEOUT_RND ) );
172             break;
173         }
174         case PHY_DEF_DR1_OFFSET:
175         {
176             phyParam.Value = REGION_COMMON_DEFAULT_RX1_DR_OFFSET;
177             break;
178         }
179         case PHY_DEF_RX2_FREQUENCY:
180         {
181             phyParam.Value = RU864_RX_WND_2_FREQ;
182             break;
183         }
184         case PHY_DEF_RX2_DR:
185         {
186             phyParam.Value = RU864_RX_WND_2_DR;
187             break;
188         }
189         case PHY_CHANNELS_MASK:
190         {
191             phyParam.ChannelsMask = RegionNvmGroup2->ChannelsMask;
192             break;
193         }
194         case PHY_CHANNELS_DEFAULT_MASK:
195         {
196             phyParam.ChannelsMask = RegionNvmGroup2->ChannelsDefaultMask;
197             break;
198         }
199         case PHY_MAX_NB_CHANNELS:
200         {
201             phyParam.Value = RU864_MAX_NB_CHANNELS;
202             break;
203         }
204         case PHY_CHANNELS:
205         {
206             phyParam.Channels = RegionNvmGroup2->Channels;
207             break;
208         }
209         case PHY_DEF_UPLINK_DWELL_TIME:
210         {
211             phyParam.Value = RU864_DEFAULT_UPLINK_DWELL_TIME;
212             break;
213         }
214         case PHY_DEF_DOWNLINK_DWELL_TIME:
215         {
216             phyParam.Value = REGION_COMMON_DEFAULT_DOWNLINK_DWELL_TIME;
217             break;
218         }
219         case PHY_DEF_MAX_EIRP:
220         {
221             phyParam.fValue = RU864_DEFAULT_MAX_EIRP;
222             break;
223         }
224         case PHY_DEF_ANTENNA_GAIN:
225         {
226             phyParam.fValue = RU864_DEFAULT_ANTENNA_GAIN;
227             break;
228         }
229         case PHY_BEACON_CHANNEL_FREQ:
230         {
231             phyParam.Value = RU864_BEACON_CHANNEL_FREQ;
232             break;
233         }
234         case PHY_BEACON_FORMAT:
235         {
236             phyParam.BeaconFormat.BeaconSize = RU864_BEACON_SIZE;
237             phyParam.BeaconFormat.Rfu1Size = RU864_RFU1_SIZE;
238             phyParam.BeaconFormat.Rfu2Size = RU864_RFU2_SIZE;
239             break;
240         }
241         case PHY_BEACON_CHANNEL_DR:
242         {
243             phyParam.Value = RU864_BEACON_CHANNEL_DR;
244             break;
245         }
246         case PHY_PING_SLOT_CHANNEL_FREQ:
247         {
248             phyParam.Value = RU864_PING_SLOT_CHANNEL_FREQ;
249             break;
250         }
251         case PHY_PING_SLOT_CHANNEL_DR:
252         {
253             phyParam.Value = RU864_PING_SLOT_CHANNEL_DR;
254             break;
255         }
256         case PHY_SF_FROM_DR:
257         {
258             phyParam.Value = DataratesRU864[getPhy->Datarate];
259             break;
260         }
261         case PHY_BW_FROM_DR:
262         {
263             phyParam.Value = RegionCommonGetBandwidth( getPhy->Datarate, BandwidthsRU864 );
264             break;
265         }
266         default:
267         {
268             break;
269         }
270     }
271 
272     return phyParam;
273 }
274 
RegionRU864SetBandTxDone(SetBandTxDoneParams_t * txDone)275 void RegionRU864SetBandTxDone( SetBandTxDoneParams_t* txDone )
276 {
277     RegionCommonSetBandTxDone( &RegionBands[RegionNvmGroup2->Channels[txDone->Channel].Band],
278                                txDone->LastTxAirTime, txDone->Joined, txDone->ElapsedTimeSinceStartUp );
279 }
280 
RegionRU864InitDefaults(InitDefaultsParams_t * params)281 void RegionRU864InitDefaults( InitDefaultsParams_t* params )
282 {
283     Band_t bands[RU864_MAX_NB_BANDS] =
284     {
285         RU864_BAND0
286     };
287 
288     switch( params->Type )
289     {
290         case INIT_TYPE_DEFAULTS:
291         {
292             if( ( params->NvmGroup1 == NULL ) || ( params->NvmGroup2 == NULL ) )
293             {
294                 return;
295             }
296 
297             RegionNvmGroup1 = (RegionNvmDataGroup1_t*) params->NvmGroup1;
298             RegionNvmGroup2 = (RegionNvmDataGroup2_t*) params->NvmGroup2;
299             RegionBands = (Band_t*) params->Bands;
300 
301             // Default bands
302             memcpy1( ( uint8_t* )RegionBands, ( uint8_t* )bands, sizeof( Band_t ) * RU864_MAX_NB_BANDS );
303 
304             // Default channels
305             RegionNvmGroup2->Channels[0] = ( ChannelParams_t ) RU864_LC1;
306             RegionNvmGroup2->Channels[1] = ( ChannelParams_t ) RU864_LC2;
307 
308             // Default ChannelsMask
309             RegionNvmGroup2->ChannelsDefaultMask[0] = LC( 1 ) + LC( 2 );
310 
311             // Update the channels mask
312             RegionCommonChanMaskCopy( RegionNvmGroup2->ChannelsMask, RegionNvmGroup2->ChannelsDefaultMask, CHANNELS_MASK_SIZE );
313             break;
314         }
315         case INIT_TYPE_RESET_TO_DEFAULT_CHANNELS:
316         {
317             // Reset Channels Rx1Frequency to default 0
318             RegionNvmGroup2->Channels[0].Rx1Frequency = 0;
319             RegionNvmGroup2->Channels[1].Rx1Frequency = 0;
320             // Update the channels mask
321             RegionCommonChanMaskCopy( RegionNvmGroup2->ChannelsMask, RegionNvmGroup2->ChannelsDefaultMask, CHANNELS_MASK_SIZE );
322             break;
323         }
324         case INIT_TYPE_ACTIVATE_DEFAULT_CHANNELS:
325         {
326             // Restore channels default mask
327             RegionNvmGroup2->ChannelsMask[0] |= RegionNvmGroup2->ChannelsDefaultMask[0];
328             break;
329         }
330         default:
331         {
332             break;
333         }
334     }
335 }
336 
RegionRU864Verify(VerifyParams_t * verify,PhyAttribute_t phyAttribute)337 bool RegionRU864Verify( VerifyParams_t* verify, PhyAttribute_t phyAttribute )
338 {
339     switch( phyAttribute )
340     {
341         case PHY_FREQUENCY:
342         {
343             return VerifyRfFreq( verify->Frequency );
344         }
345         case PHY_TX_DR:
346         {
347             return RegionCommonValueInRange( verify->DatarateParams.Datarate, RU864_TX_MIN_DATARATE, RU864_TX_MAX_DATARATE );
348         }
349         case PHY_DEF_TX_DR:
350         {
351             return RegionCommonValueInRange( verify->DatarateParams.Datarate, DR_0, DR_5 );
352         }
353         case PHY_RX_DR:
354         {
355             return RegionCommonValueInRange( verify->DatarateParams.Datarate, RU864_RX_MIN_DATARATE, RU864_RX_MAX_DATARATE );
356         }
357         case PHY_DEF_TX_POWER:
358         case PHY_TX_POWER:
359         {
360             // Remark: switched min and max!
361             return RegionCommonValueInRange( verify->TxPower, RU864_MAX_TX_POWER, RU864_MIN_TX_POWER );
362         }
363         case PHY_DUTY_CYCLE:
364         {
365             return RU864_DUTY_CYCLE_ENABLED;
366         }
367         default:
368             return false;
369     }
370 }
371 
RegionRU864ApplyCFList(ApplyCFListParams_t * applyCFList)372 void RegionRU864ApplyCFList( ApplyCFListParams_t* applyCFList )
373 {
374     ChannelParams_t newChannel;
375     ChannelAddParams_t channelAdd;
376     ChannelRemoveParams_t channelRemove;
377 
378     // Setup default datarate range
379     newChannel.DrRange.Value = ( DR_5 << 4 ) | DR_0;
380 
381     // Size of the optional CF list
382     if( applyCFList->Size != 16 )
383     {
384         return;
385     }
386 
387     // Last byte CFListType must be 0 to indicate the CFList contains a list of frequencies
388     if( applyCFList->Payload[15] != 0 )
389     {
390         return;
391     }
392 
393     // Last byte is RFU, don't take it into account
394     for( uint8_t i = 0, chanIdx = RU864_NUMB_DEFAULT_CHANNELS; chanIdx < RU864_MAX_NB_CHANNELS; i+=3, chanIdx++ )
395     {
396         if( chanIdx < ( RU864_NUMB_CHANNELS_CF_LIST + RU864_NUMB_DEFAULT_CHANNELS ) )
397         {
398             // Channel frequency
399             newChannel.Frequency = (uint32_t) applyCFList->Payload[i];
400             newChannel.Frequency |= ( (uint32_t) applyCFList->Payload[i + 1] << 8 );
401             newChannel.Frequency |= ( (uint32_t) applyCFList->Payload[i + 2] << 16 );
402             newChannel.Frequency *= 100;
403 
404             // Initialize alternative frequency to 0
405             newChannel.Rx1Frequency = 0;
406         }
407         else
408         {
409             newChannel.Frequency = 0;
410             newChannel.DrRange.Value = 0;
411             newChannel.Rx1Frequency = 0;
412         }
413 
414         if( newChannel.Frequency != 0 )
415         {
416             channelAdd.NewChannel = &newChannel;
417             channelAdd.ChannelId = chanIdx;
418 
419             // Try to add all channels
420             RegionRU864ChannelAdd( &channelAdd );
421         }
422         else
423         {
424             channelRemove.ChannelId = chanIdx;
425 
426             RegionRU864ChannelsRemove( &channelRemove );
427         }
428     }
429 }
430 
RegionRU864ChanMaskSet(ChanMaskSetParams_t * chanMaskSet)431 bool RegionRU864ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
432 {
433     switch( chanMaskSet->ChannelsMaskType )
434     {
435         case CHANNELS_MASK:
436         {
437             RegionCommonChanMaskCopy( RegionNvmGroup2->ChannelsMask, chanMaskSet->ChannelsMaskIn, 1 );
438             break;
439         }
440         case CHANNELS_DEFAULT_MASK:
441         {
442             RegionCommonChanMaskCopy( RegionNvmGroup2->ChannelsDefaultMask, chanMaskSet->ChannelsMaskIn, 1 );
443             break;
444         }
445         default:
446             return false;
447     }
448     return true;
449 }
450 
RegionRU864ComputeRxWindowParameters(int8_t datarate,uint8_t minRxSymbols,uint32_t rxError,RxConfigParams_t * rxConfigParams)451 void RegionRU864ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
452 {
453     uint32_t tSymbolInUs = 0;
454 
455     // Get the datarate, perform a boundary check
456     rxConfigParams->Datarate = MIN( datarate, RU864_RX_MAX_DATARATE );
457     rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsRU864 );
458 
459     if( rxConfigParams->Datarate == DR_7 )
460     { // FSK
461         tSymbolInUs = RegionCommonComputeSymbolTimeFsk( DataratesRU864[rxConfigParams->Datarate] );
462     }
463     else
464     { // LoRa
465         tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesRU864[rxConfigParams->Datarate], BandwidthsRU864[rxConfigParams->Datarate] );
466     }
467 
468     RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
469 }
470 
RegionRU864RxConfig(RxConfigParams_t * rxConfig,int8_t * datarate)471 bool RegionRU864RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
472 {
473     RadioModems_t modem;
474     int8_t dr = rxConfig->Datarate;
475     int8_t phyDr = 0;
476     uint32_t frequency = rxConfig->Frequency;
477 
478     if( Radio.GetStatus( ) != RF_IDLE )
479     {
480         return false;
481     }
482 
483     if( rxConfig->RxSlot == RX_SLOT_WIN_1 )
484     {
485         // Apply window 1 frequency
486         frequency = RegionNvmGroup2->Channels[rxConfig->Channel].Frequency;
487         // Apply the alternative RX 1 window frequency, if it is available
488         if( RegionNvmGroup2->Channels[rxConfig->Channel].Rx1Frequency != 0 )
489         {
490             frequency = RegionNvmGroup2->Channels[rxConfig->Channel].Rx1Frequency;
491         }
492     }
493 
494     // Read the physical datarate from the datarates table
495     phyDr = DataratesRU864[dr];
496 
497     Radio.SetChannel( frequency );
498 
499     // Radio configuration
500     if( dr == DR_7 )
501     {
502         modem = MODEM_FSK;
503         Radio.SetRxConfig( modem, 50000, phyDr * 1000, 0, 83333, 5, rxConfig->WindowTimeout, false, 0, true, 0, 0, false, rxConfig->RxContinuous );
504     }
505     else
506     {
507         modem = MODEM_LORA;
508         Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
509     }
510 
511     Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateRU864[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );
512 
513     *datarate = (uint8_t) dr;
514     return true;
515 }
516 
RegionRU864TxConfig(TxConfigParams_t * txConfig,int8_t * txPower,TimerTime_t * txTimeOnAir)517 bool RegionRU864TxConfig( TxConfigParams_t* txConfig, int8_t* txPower, TimerTime_t* txTimeOnAir )
518 {
519     RadioModems_t modem;
520     int8_t phyDr = DataratesRU864[txConfig->Datarate];
521     int8_t txPowerLimited = RegionCommonLimitTxPower( txConfig->TxPower, RegionBands[RegionNvmGroup2->Channels[txConfig->Channel].Band].TxMaxPower );
522     uint32_t bandwidth = RegionCommonGetBandwidth( txConfig->Datarate, BandwidthsRU864 );
523     int8_t phyTxPower = 0;
524 
525     // Calculate physical TX power
526     phyTxPower = RegionCommonComputeTxPower( txPowerLimited, txConfig->MaxEirp, txConfig->AntennaGain );
527 
528     // Setup the radio frequency
529     Radio.SetChannel( RegionNvmGroup2->Channels[txConfig->Channel].Frequency );
530 
531     if( txConfig->Datarate == DR_7 )
532     { // High Speed FSK channel
533         modem = MODEM_FSK;
534         Radio.SetTxConfig( modem, phyTxPower, 25000, bandwidth, phyDr * 1000, 0, 5, false, true, 0, 0, false, 4000 );
535     }
536     else
537     {
538         modem = MODEM_LORA;
539         Radio.SetTxConfig( modem, phyTxPower, 0, bandwidth, phyDr, 1, 8, false, true, 0, 0, false, 4000 );
540     }
541 
542     // Update time-on-air
543     *txTimeOnAir = GetTimeOnAir( txConfig->Datarate, txConfig->PktLen );
544 
545     // Setup maximum payload lenght of the radio driver
546     Radio.SetMaxPayloadLength( modem, txConfig->PktLen );
547 
548     *txPower = txPowerLimited;
549     return true;
550 }
551 
RegionRU864LinkAdrReq(LinkAdrReqParams_t * linkAdrReq,int8_t * drOut,int8_t * txPowOut,uint8_t * nbRepOut,uint8_t * nbBytesParsed)552 uint8_t RegionRU864LinkAdrReq( LinkAdrReqParams_t* linkAdrReq, int8_t* drOut, int8_t* txPowOut, uint8_t* nbRepOut, uint8_t* nbBytesParsed )
553 {
554     uint8_t status = 0x07;
555     RegionCommonLinkAdrParams_t linkAdrParams = { 0 };
556     uint8_t nextIndex = 0;
557     uint8_t bytesProcessed = 0;
558     uint16_t chMask = 0;
559     GetPhyParams_t getPhy;
560     PhyParam_t phyParam;
561     RegionCommonLinkAdrReqVerifyParams_t linkAdrVerifyParams;
562 
563     while( bytesProcessed < linkAdrReq->PayloadSize )
564     {
565         // Get ADR request parameters
566         nextIndex = RegionCommonParseLinkAdrReq( &( linkAdrReq->Payload[bytesProcessed] ), &linkAdrParams );
567 
568         if( nextIndex == 0 )
569             break; // break loop, since no more request has been found
570 
571         // Update bytes processed
572         bytesProcessed += nextIndex;
573 
574         // Revert status, as we only check the last ADR request for the channel mask KO
575         status = 0x07;
576 
577         // Setup temporary channels mask
578         chMask = linkAdrParams.ChMask;
579 
580         // Verify channels mask
581         if( ( linkAdrParams.ChMaskCtrl == 0 ) && ( chMask == 0 ) )
582         {
583             status &= 0xFE; // Channel mask KO
584         }
585         else if( ( ( linkAdrParams.ChMaskCtrl >= 1 ) && ( linkAdrParams.ChMaskCtrl <= 5 )) ||
586                 ( linkAdrParams.ChMaskCtrl >= 7 ) )
587         {
588             // RFU
589             status &= 0xFE; // Channel mask KO
590         }
591         else
592         {
593             for( uint8_t i = 0; i < RU864_MAX_NB_CHANNELS; i++ )
594             {
595                 if( linkAdrParams.ChMaskCtrl == 6 )
596                 {
597                     if( RegionNvmGroup2->Channels[i].Frequency != 0 )
598                     {
599                         chMask |= 1 << i;
600                     }
601                 }
602                 else
603                 {
604                     if( ( ( chMask & ( 1 << i ) ) != 0 ) &&
605                         ( RegionNvmGroup2->Channels[i].Frequency == 0 ) )
606                     {// Trying to enable an undefined channel
607                         status &= 0xFE; // Channel mask KO
608                     }
609                 }
610             }
611         }
612     }
613 
614     // Get the minimum possible datarate
615     getPhy.Attribute = PHY_MIN_TX_DR;
616     getPhy.UplinkDwellTime = linkAdrReq->UplinkDwellTime;
617     phyParam = RegionRU864GetPhyParam( &getPhy );
618 
619     linkAdrVerifyParams.Status = status;
620     linkAdrVerifyParams.AdrEnabled = linkAdrReq->AdrEnabled;
621     linkAdrVerifyParams.Datarate = linkAdrParams.Datarate;
622     linkAdrVerifyParams.TxPower = linkAdrParams.TxPower;
623     linkAdrVerifyParams.NbRep = linkAdrParams.NbRep;
624     linkAdrVerifyParams.CurrentDatarate = linkAdrReq->CurrentDatarate;
625     linkAdrVerifyParams.CurrentTxPower = linkAdrReq->CurrentTxPower;
626     linkAdrVerifyParams.CurrentNbRep = linkAdrReq->CurrentNbRep;
627     linkAdrVerifyParams.NbChannels = RU864_MAX_NB_CHANNELS;
628     linkAdrVerifyParams.ChannelsMask = &chMask;
629     linkAdrVerifyParams.MinDatarate = ( int8_t )phyParam.Value;
630     linkAdrVerifyParams.MaxDatarate = RU864_TX_MAX_DATARATE;
631     linkAdrVerifyParams.Channels = RegionNvmGroup2->Channels;
632     linkAdrVerifyParams.MinTxPower = RU864_MIN_TX_POWER;
633     linkAdrVerifyParams.MaxTxPower = RU864_MAX_TX_POWER;
634     linkAdrVerifyParams.Version = linkAdrReq->Version;
635 
636     // Verify the parameters and update, if necessary
637     status = RegionCommonLinkAdrReqVerifyParams( &linkAdrVerifyParams, &linkAdrParams.Datarate, &linkAdrParams.TxPower, &linkAdrParams.NbRep );
638 
639     // Update channelsMask if everything is correct
640     if( status == 0x07 )
641     {
642         // Set the channels mask to a default value
643         memset1( ( uint8_t* ) RegionNvmGroup2->ChannelsMask, 0, sizeof( RegionNvmGroup2->ChannelsMask ) );
644         // Update the channels mask
645         RegionNvmGroup2->ChannelsMask[0] = chMask;
646     }
647 
648     // Update status variables
649     *drOut = linkAdrParams.Datarate;
650     *txPowOut = linkAdrParams.TxPower;
651     *nbRepOut = linkAdrParams.NbRep;
652     *nbBytesParsed = bytesProcessed;
653 
654     return status;
655 }
656 
RegionRU864RxParamSetupReq(RxParamSetupReqParams_t * rxParamSetupReq)657 uint8_t RegionRU864RxParamSetupReq( RxParamSetupReqParams_t* rxParamSetupReq )
658 {
659     uint8_t status = 0x07;
660 
661     // Verify radio frequency
662     if( VerifyRfFreq( rxParamSetupReq->Frequency ) == false )
663     {
664         status &= 0xFE; // Channel frequency KO
665     }
666 
667     // Verify datarate
668     if( RegionCommonValueInRange( rxParamSetupReq->Datarate, RU864_RX_MIN_DATARATE, RU864_RX_MAX_DATARATE ) == false )
669     {
670         status &= 0xFD; // Datarate KO
671     }
672 
673     // Verify datarate offset
674     if( RegionCommonValueInRange( rxParamSetupReq->DrOffset, RU864_MIN_RX1_DR_OFFSET, RU864_MAX_RX1_DR_OFFSET ) == false )
675     {
676         status &= 0xFB; // Rx1DrOffset range KO
677     }
678 
679     return status;
680 }
681 
RegionRU864NewChannelReq(NewChannelReqParams_t * newChannelReq)682 int8_t RegionRU864NewChannelReq( NewChannelReqParams_t* newChannelReq )
683 {
684     uint8_t status = 0x03;
685     ChannelAddParams_t channelAdd;
686     ChannelRemoveParams_t channelRemove;
687 
688     if( newChannelReq->NewChannel->Frequency == 0 )
689     {
690         channelRemove.ChannelId = newChannelReq->ChannelId;
691 
692         // Remove
693         if( RegionRU864ChannelsRemove( &channelRemove ) == false )
694         {
695             status &= 0xFC;
696         }
697     }
698     else
699     {
700         channelAdd.NewChannel = newChannelReq->NewChannel;
701         channelAdd.ChannelId = newChannelReq->ChannelId;
702 
703         switch( RegionRU864ChannelAdd( &channelAdd ) )
704         {
705             case LORAMAC_STATUS_OK:
706             {
707                 break;
708             }
709             case LORAMAC_STATUS_FREQUENCY_INVALID:
710             {
711                 status &= 0xFE;
712                 break;
713             }
714             case LORAMAC_STATUS_DATARATE_INVALID:
715             {
716                 status &= 0xFD;
717                 break;
718             }
719             case LORAMAC_STATUS_FREQ_AND_DR_INVALID:
720             {
721                 status &= 0xFC;
722                 break;
723             }
724             default:
725             {
726                 status &= 0xFC;
727                 break;
728             }
729         }
730     }
731 
732     return status;
733 }
734 
RegionRU864TxParamSetupReq(TxParamSetupReqParams_t * txParamSetupReq)735 int8_t RegionRU864TxParamSetupReq( TxParamSetupReqParams_t* txParamSetupReq )
736 {
737     // Do not accept the request
738     return -1;
739 }
740 
RegionRU864DlChannelReq(DlChannelReqParams_t * dlChannelReq)741 int8_t RegionRU864DlChannelReq( DlChannelReqParams_t* dlChannelReq )
742 {
743     uint8_t status = 0x03;
744 
745     if( dlChannelReq->ChannelId >= ( CHANNELS_MASK_SIZE * 16 ) )
746     {
747         return 0;
748     }
749 
750     // Verify if the frequency is supported
751     if( VerifyRfFreq( dlChannelReq->Rx1Frequency ) == false )
752     {
753         status &= 0xFE;
754     }
755 
756     // Verify if an uplink frequency exists
757     if( RegionNvmGroup2->Channels[dlChannelReq->ChannelId].Frequency == 0 )
758     {
759         status &= 0xFD;
760     }
761 
762     // Apply Rx1 frequency, if the status is OK
763     if( status == 0x03 )
764     {
765         RegionNvmGroup2->Channels[dlChannelReq->ChannelId].Rx1Frequency = dlChannelReq->Rx1Frequency;
766     }
767 
768     return status;
769 }
770 
RegionRU864AlternateDr(int8_t currentDr,AlternateDrType_t type)771 int8_t RegionRU864AlternateDr( int8_t currentDr, AlternateDrType_t type )
772 {
773     return currentDr;
774 }
775 
RegionRU864NextChannel(NextChanParams_t * nextChanParams,uint8_t * channel,TimerTime_t * time,TimerTime_t * aggregatedTimeOff)776 LoRaMacStatus_t RegionRU864NextChannel( NextChanParams_t* nextChanParams, uint8_t* channel, TimerTime_t* time, TimerTime_t* aggregatedTimeOff )
777 {
778     uint8_t nbEnabledChannels = 0;
779     uint8_t nbRestrictedChannels = 0;
780     uint8_t enabledChannels[RU864_MAX_NB_CHANNELS] = { 0 };
781     RegionCommonIdentifyChannelsParam_t identifyChannelsParam;
782     RegionCommonCountNbOfEnabledChannelsParams_t countChannelsParams;
783     LoRaMacStatus_t status = LORAMAC_STATUS_NO_CHANNEL_FOUND;
784     uint16_t joinChannels = RU864_JOIN_CHANNELS;
785 
786     if( RegionCommonCountChannels( RegionNvmGroup2->ChannelsMask, 0, 1 ) == 0 )
787     { // Reactivate default channels
788         RegionNvmGroup2->ChannelsMask[0] |= LC( 1 ) + LC( 2 );
789     }
790 
791     // Search how many channels are enabled
792     countChannelsParams.Joined = nextChanParams->Joined;
793     countChannelsParams.Datarate = nextChanParams->Datarate;
794     countChannelsParams.ChannelsMask = RegionNvmGroup2->ChannelsMask;
795     countChannelsParams.Channels = RegionNvmGroup2->Channels;
796     countChannelsParams.Bands = RegionBands;
797     countChannelsParams.MaxNbChannels = RU864_MAX_NB_CHANNELS;
798     countChannelsParams.JoinChannels = &joinChannels;
799 
800     identifyChannelsParam.AggrTimeOff = nextChanParams->AggrTimeOff;
801     identifyChannelsParam.LastAggrTx = nextChanParams->LastAggrTx;
802     identifyChannelsParam.DutyCycleEnabled = nextChanParams->DutyCycleEnabled;
803     identifyChannelsParam.MaxBands = RU864_MAX_NB_BANDS;
804 
805     identifyChannelsParam.ElapsedTimeSinceStartUp = nextChanParams->ElapsedTimeSinceStartUp;
806     identifyChannelsParam.LastTxIsJoinRequest = nextChanParams->LastTxIsJoinRequest;
807     identifyChannelsParam.ExpectedTimeOnAir = GetTimeOnAir( nextChanParams->Datarate, nextChanParams->PktLen );
808 
809     identifyChannelsParam.CountNbOfEnabledChannelsParam = &countChannelsParams;
810 
811     status = RegionCommonIdentifyChannels( &identifyChannelsParam, aggregatedTimeOff, enabledChannels,
812                                            &nbEnabledChannels, &nbRestrictedChannels, time );
813 
814     if( status == LORAMAC_STATUS_OK )
815     {
816         // We found a valid channel
817         *channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )];
818     }
819     else if( status == LORAMAC_STATUS_NO_CHANNEL_FOUND )
820     {
821         // Datarate not supported by any channel, restore defaults
822         RegionNvmGroup2->ChannelsMask[0] |= LC( 1 ) + LC( 2 );
823     }
824     return status;
825 }
826 
RegionRU864ChannelAdd(ChannelAddParams_t * channelAdd)827 LoRaMacStatus_t RegionRU864ChannelAdd( ChannelAddParams_t* channelAdd )
828 {
829     bool drInvalid = false;
830     bool freqInvalid = false;
831     uint8_t id = channelAdd->ChannelId;
832 
833     if( id < RU864_NUMB_DEFAULT_CHANNELS )
834     {
835         return LORAMAC_STATUS_FREQ_AND_DR_INVALID;
836     }
837 
838     if( id >= RU864_MAX_NB_CHANNELS )
839     {
840         return LORAMAC_STATUS_PARAMETER_INVALID;
841     }
842 
843     // Validate the datarate range
844     if( RegionCommonValueInRange( channelAdd->NewChannel->DrRange.Fields.Min, RU864_TX_MIN_DATARATE, RU864_TX_MAX_DATARATE ) == false )
845     {
846         drInvalid = true;
847     }
848     if( RegionCommonValueInRange( channelAdd->NewChannel->DrRange.Fields.Max, RU864_TX_MIN_DATARATE, RU864_TX_MAX_DATARATE ) == false )
849     {
850         drInvalid = true;
851     }
852     if( channelAdd->NewChannel->DrRange.Fields.Min > channelAdd->NewChannel->DrRange.Fields.Max )
853     {
854         drInvalid = true;
855     }
856 
857     // Check frequency
858     if( freqInvalid == false )
859     {
860         if( VerifyRfFreq( channelAdd->NewChannel->Frequency ) == false )
861         {
862             freqInvalid = true;
863         }
864     }
865 
866     // Check status
867     if( ( drInvalid == true ) && ( freqInvalid == true ) )
868     {
869         return LORAMAC_STATUS_FREQ_AND_DR_INVALID;
870     }
871     if( drInvalid == true )
872     {
873         return LORAMAC_STATUS_DATARATE_INVALID;
874     }
875     if( freqInvalid == true )
876     {
877         return LORAMAC_STATUS_FREQUENCY_INVALID;
878     }
879 
880     memcpy1( ( uint8_t* ) &(RegionNvmGroup2->Channels[id]), ( uint8_t* ) channelAdd->NewChannel, sizeof( RegionNvmGroup2->Channels[id] ) );
881     RegionNvmGroup2->Channels[id].Band = 0;
882     RegionNvmGroup2->ChannelsMask[0] |= ( 1 << id );
883     return LORAMAC_STATUS_OK;
884 }
885 
RegionRU864ChannelsRemove(ChannelRemoveParams_t * channelRemove)886 bool RegionRU864ChannelsRemove( ChannelRemoveParams_t* channelRemove  )
887 {
888     uint8_t id = channelRemove->ChannelId;
889 
890     if( id < RU864_NUMB_DEFAULT_CHANNELS )
891     {
892         return false;
893     }
894 
895     // Remove the channel from the list of channels
896     RegionNvmGroup2->Channels[id] = ( ChannelParams_t ){ 0, 0, { 0 }, 0 };
897 
898     return RegionCommonChanDisable( RegionNvmGroup2->ChannelsMask, id, RU864_MAX_NB_CHANNELS );
899 }
900 
RegionRU864ApplyDrOffset(uint8_t downlinkDwellTime,int8_t dr,int8_t drOffset)901 uint8_t RegionRU864ApplyDrOffset( uint8_t downlinkDwellTime, int8_t dr, int8_t drOffset )
902 {
903     int8_t datarate = dr - drOffset;
904 
905     if( datarate < 0 )
906     {
907         datarate = DR_0;
908     }
909     return datarate;
910 }
911 
RegionRU864RxBeaconSetup(RxBeaconSetup_t * rxBeaconSetup,uint8_t * outDr)912 void RegionRU864RxBeaconSetup( RxBeaconSetup_t* rxBeaconSetup, uint8_t* outDr )
913 {
914     RegionCommonRxBeaconSetupParams_t regionCommonRxBeaconSetup;
915 
916     regionCommonRxBeaconSetup.Datarates = DataratesRU864;
917     regionCommonRxBeaconSetup.Frequency = rxBeaconSetup->Frequency;
918     regionCommonRxBeaconSetup.BeaconSize = RU864_BEACON_SIZE;
919     regionCommonRxBeaconSetup.BeaconDatarate = RU864_BEACON_CHANNEL_DR;
920     regionCommonRxBeaconSetup.BeaconChannelBW = RU864_BEACON_CHANNEL_BW;
921     regionCommonRxBeaconSetup.RxTime = rxBeaconSetup->RxTime;
922     regionCommonRxBeaconSetup.SymbolTimeout = rxBeaconSetup->SymbolTimeout;
923 
924     RegionCommonRxBeaconSetup( &regionCommonRxBeaconSetup );
925 
926     // Store downlink datarate
927     *outDr = RU864_BEACON_CHANNEL_DR;
928 }
929