/*!
 * \file      RegionCustomBType.h
 *
 * \brief     Region definition for CustomBType
 *
 * \copyright Revised BSD License, see section \ref LICENSE.
 *
 * \code
 *                ______                              _
 *               / _____)             _              | |
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 *               _____) ) ____| | | || |_| ____( (___| | | |
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 *              (C)2013 Semtech
 *
 *               ___ _____ _   ___ _  _____ ___  ___  ___ ___
 *              / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
 *              \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
 *              |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
 *              embedded.connectivity.solutions===============
 *
 * \endcode
 *
 * \author    Miguel Luis ( Semtech )
 *
 * \author    Gregory Cristian ( Semtech )
 *
 * \author    Daniel Jaeckle ( STACKFORCE )
 *
 * \author    Ray Tsou ( ASE )
 *
 * \defgroup  REGIONCustomBType Region CustomBType
 *            Implementation according to LoRaWAN Specification v1.0.2.
 * \{
 */

#ifndef __REGION_CustomBType_H__
#define __REGION_CustomBType_H__
#include "LoRaMac.h"
#include "Region.h"
#include "RegionCommon.h"
#include "qm_common.h"
/*!
 * LoRaMac maximum number of channels
 */
#define CustomBType_MAX_NB_CHANNELS_def                    72
#define CN470_MAX_NB_CHANNELS                        	   96

/*!
 * Minimal datarate that can be used by the node
 */
#define CustomBType_TX_MIN_DATARATE                       DR_0

/*!
 * Maximal datarate that can be used by the node
 */
#define CustomBType_TX_MAX_DATARATE                       DR_4
#define CN470_TX_MAX_DATARATE                       	  DR_5

/*!
 * Minimal datarate that can be used by the node
 */
#define CustomBType_RX_MIN_DATARATE                       DR_8
#define CN470_RX_MIN_DATARATE                             DR_0

/*!
 * Maximal datarate that can be used by the node
 */
#define CustomBType_RX_MAX_DATARATE                       DR_13
#define CN470_RX_MAX_DATARATE                             DR_5

/*!
 * Default datarate used by the node
 */
#define CustomBType_DEFAULT_DATARATE                      DR_0

/*!
 * Minimal Rx1 receive datarate offset
 */
#define CustomBType_MIN_RX1_DR_OFFSET                     0

/*!
 * Maximal Rx1 receive datarate offset
 */
#define CustomBType_MAX_RX1_DR_OFFSET                     3

/*!
 * Default Rx1 receive datarate offset
 */
#define CustomBType_DEFAULT_RX1_DR_OFFSET                 0

/*!
 * Minimal Tx output power that can be used by the node
 */
#define CustomBType_MIN_TX_POWER_def                          TX_POWER_10
#define CN470_MIN_TX_POWER                                TX_POWER_7

/*!
 * Maximal Tx output power that can be used by the node
 */
#define CustomBType_MAX_TX_POWER_def                          TX_POWER_0

/*!
 * Default Tx output power used by the node
 */
#define CustomBType_DEFAULT_TX_POWER_def                      TX_POWER_5
#define CN470_DEFAULT_TX_POWER                            TX_POWER_2

/*!
 * ADR Ack limit
 */
#define CustomBType_ADR_ACK_LIMIT                         64

/*!
 * ADR Ack delay
 */
#define CustomBType_ADR_ACK_DELAY                         32

/*!
 * Enabled or disabled the duty cycle
 */
#define CustomBType_DUTY_CYCLE_ENABLED                    0

/*!
 * Maximum RX window duration
 */
#define CustomBType_MAX_RX_WINDOW                         3000

/*!
 * Receive delay 1
 */
#define CustomBType_RECEIVE_DELAY1                        1000

/*!
 * Receive delay 2
 */
#define CustomBType_RECEIVE_DELAY2                        2000

/*!
 * Join accept delay 1
 */
#define CustomBType_JOIN_ACCEPT_DELAY1                    5000

/*!
 * Join accept delay 2
 */
#define CustomBType_JOIN_ACCEPT_DELAY2                    6000

/*!
 * Maximum frame counter gap
 */
#define CustomBType_MAX_FCNT_GAP                          16384

/*!
 * Ack timeout
 */
#define CustomBType_ACKTIMEOUT                            2000

/*!
 * Random ack timeout limits
 */
#define CustomBType_ACK_TIMEOUT_RND                       1000

/*!
 * Second reception window channel frequency definition.
 */
#define CustomBType_RX_WND_2_FREQ                         923300000

/*!
 * Second reception window channel datarate definition.
 */
#define CustomBType_RX_WND_2_DR                           DR_8

/*!
 * LoRaMac maximum number of bands
 */
#define CustomBType_MAX_NB_BANDS                           1

/*!
 * Band 0 definition
 * { DutyCycle, TxMaxPower, LastTxDoneTime, TimeOff }
 */
#define CustomBType_BAND0                                 { 1, TX_POWER_5, 0,  0 } //  100.0 %

/*!
 * Defines the first channel for RX window 1 for US band
 */
#define CustomBType_FIRST_RX1_CHANNEL_def                     ( (uint32_t) 923.3e6 )
#define CN470_FIRST_RX1_CHANNEL                     		  ( (uint32_t) 500.3e6 )

/*!
 * Defines the last channel for RX window 1 for US band
 */
#define CustomBType_LAST_RX1_CHANNEL_def                      ( (uint32_t) 927.5e6 )
#define CN470_LAST_RX1_CHANNEL                      		  ( (uint32_t) 509.7e6 )

/*!
 * Defines the step width of the channels for RX window 1
 */
#define CustomBType_STEPWIDTH_RX1_CHANNEL_def                 ( (uint32_t) 600e3 )
#define CN470_STEPWIDTH_RX1_CHANNEL                       ( (uint32_t) 200e3 )

/*!
 * Data rates table definition
 */
static const uint8_t DataratesCustomBType[]  = { 10, 9, 8,  7,  8,  0,  0, 0, 12, 11, 10, 9, 8, 7, 0, 0 };
 static const uint8_t DataratesCN470[]  = { 12, 11, 10,  9,  8,  7 };

/*!
 * Up/Down link data rates offset definition
 */
static const int8_t DatarateOffsetsCustomBType[5][4] =
{
    { DR_10, DR_9 , DR_8 , DR_8  }, // DR_0
    { DR_11, DR_10, DR_9 , DR_8  }, // DR_1
    { DR_12, DR_11, DR_10, DR_9  }, // DR_2
    { DR_13, DR_12, DR_11, DR_10 }, // DR_3
    { DR_13, DR_13, DR_12, DR_11 }, // DR_4
};

static const int8_t DatarateOffsetsAU915[7][6] =
{
    { DR_8 , DR_8 , DR_8 , DR_8, DR_8, DR_8  }, // DR_0
    { DR_9 , DR_8 , DR_8 , DR_8, DR_8, DR_8  }, // DR_1
    { DR_10, DR_9 , DR_8 , DR_8, DR_8, DR_8  }, // DR_2
    { DR_11, DR_10 , DR_9 , DR_8, DR_8, DR_8 }, // DR_3
    { DR_12, DR_11 , DR_10 , DR_9, DR_8, DR_8 }, // DR_4
	{ DR_13, DR_12 , DR_11 , DR_10, DR_9, DR_8 }, // DR_5
	{ DR_13, DR_13 , DR_12 , DR_11, DR_10, DR_9 }, // DR_6
};

/*!
 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
 */
static const uint8_t MaxPayloadOfDatarateCustomBType[] = { 11, 53, 125, 242, 242, 0, 0, 0, 53, 129, 242, 242, 242, 242, 0, 0 };
static const uint8_t MaxPayloadOfDatarateCN470[] = { 51, 51, 51, 115, 222, 222 };

/*!
 * Maximum payload with respect to the datarate index. Can operate with repeater.
 */
static const uint8_t MaxPayloadOfDatarateRepeaterCustomBType[] = { 11, 53, 125, 242, 242, 0, 0, 0, 33, 109, 222, 222, 222, 222, 0, 0 };
static const uint8_t MaxPayloadOfDatarateRepeaterCN470[] = { 51, 51, 51, 115, 222, 222 };

/*!
 * Tx output powers table definition
 */
static const int8_t TxPowersCustomBType[] = { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10 };
static const int8_t TxPowersCN470[] = { 17, 16, 14, 12, 10, 7, 5, 2 };



/*!
 * \brief The function gets a value of a specific phy attribute.
 *
 * \param [IN] getPhy Pointer to the function parameters.
 */
void RegionCustomBTypeGetPhyParam( GetPhyParams_t* getPhy );

/*!
 * \brief Updates the last TX done parameters of the current channel.
 *
 * \param [IN] txDone Pointer to the function parameters.
 */
void RegionCustomBTypeSetBandTxDone( SetBandTxDoneParams_t* txDone );

/*!
 * \brief Initializes the channels masks and the channels.
 *
 * \param [IN] type Sets the initialization type.
 */
void RegionCustomBTypeInitDefaults( InitType_t type );

/*!
 * \brief Verifies a parameter.
 *
 * \param [IN] verify Pointer to the function parameters.
 *
 * \param [IN] type Sets the initialization type.
 *
 * \retval Returns true, if the parameter is valid.
 */
bool RegionCustomBTypeVerify( VerifyParams_t* verify, PhyAttribute_t phyAttribute );

/*!
 * \brief The function parses the input buffer and sets up the channels of the
 *        CF list.
 *
 * \param [IN] applyCFList Pointer to the function parameters.
 */
void RegionCustomBTypeApplyCFList( ApplyCFListParams_t* applyCFList );

/*!
 * \brief Sets a channels mask.
 *
 * \param [IN] chanMaskSet Pointer to the function parameters.
 *
 * \retval Returns true, if the channels mask could be set.
 */
bool RegionCustomBTypeChanMaskSet( ChanMaskSetParams_t* chanMaskSet );

/*!
 * \brief Calculates the next datarate to set, when ADR is on or off.
 *
 * \param [IN] adrNext Pointer to the function parameters.
 *
 * \param [OUT] drOut The calculated datarate for the next TX.
 *
 * \param [OUT] txPowOut The TX power for the next TX.
 *
 * \param [OUT] adrAckCounter The calculated ADR acknowledgement counter.
 *
 * \retval Returns true, if an ADR request should be performed.
 */
bool RegionCustomBTypeAdrNext( AdrNextParams_t* adrNext, int8_t* drOut, int8_t* txPowOut, uint32_t* adrAckCounter );

/*!
 * \brief Configuration of the RX windows.
 *
 * \param [IN] rxConfig Pointer to the function parameters.
 *
 * \param [OUT] datarate The datarate index which was set.
 *
 * \retval Returns true, if the configuration was applied successfully.
 */
bool RegionCustomBTypeRxConfig( RxConfigParams_t* rxConfig, int8_t* datarate );

/*!
 * \brief TX configuration.
 *
 * \param [IN] txConfig Pointer to the function parameters.
 *
 * \param [OUT] txPower The tx power index which was set.
 *
 * \param [OUT] txTimeOnAir The time-on-air of the frame.
 *
 * \retval Returns true, if the configuration was applied successfully.
 */
bool RegionCustomBTypeTxConfig( TxConfigParams_t* txConfig, int8_t* txPower, uint32_t* txTimeOnAir );

/*!
 * \brief The function processes a Link ADR Request.
 *
 * \param [IN] linkAdrReq Pointer to the function parameters.
 *
 * \retval Returns the status of the operation, according to the LoRaMAC specification.
 */
uint8_t RegionCustomBTypeLinkAdrReq( LinkAdrReqParams_t* linkAdrReq, int8_t* drOut, int8_t* txPowOut, uint8_t* nbRepOut, uint8_t* nbBytesParsed );

/*!
 * \brief The function processes a RX Parameter Setup Request.
 *
 * \param [IN] rxParamSetupReq Pointer to the function parameters.
 *
 * \retval Returns the status of the operation, according to the LoRaMAC specification.
 */
uint8_t RegionCustomBTypeRxParamSetupReq( RxParamSetupReqParams_t* rxParamSetupReq );

/*!
 * \brief The function processes a Channel Request.
 *
 * \param [IN] newChannelReq Pointer to the function parameters.
 *
 * \retval Returns the status of the operation, according to the LoRaMAC specification.
 */
uint8_t RegionCustomBTypeNewChannelReq( NewChannelReqParams_t* newChannelReq );

/*!
 * \brief The function processes a TX ParamSetup Request.
 *
 * \param [IN] txParamSetupReq Pointer to the function parameters.
 *
 * \retval Returns the status of the operation, according to the LoRaMAC specification.
 *         Returns -1, if the functionality is not implemented. In this case, the end node
 *         shall not process the command.
 */
int8_t RegionCustomBTypeTxParamSetupReq( TxParamSetupReqParams_t* txParamSetupReq );

/*!
 * \brief The function processes a DlChannel Request.
 *
 * \param [IN] dlChannelReq Pointer to the function parameters.
 *
 * \retval Returns the status of the operation, according to the LoRaMAC specification.
 */
uint8_t RegionCustomBTypeDlChannelReq( DlChannelReqParams_t* dlChannelReq );

/*
 * \brief Alternates the datarate of the channel for the join request.
 *
 * \param [IN] alternateDr Pointer to the function parameters.
 *
 * \retval Datarate to apply.
 */
int8_t RegionCustomBTypeAlternateDr( AlternateDrParams_t* alternateDr );

/*!
 * \brief Calculates the back-off time.
 *
 * \param [IN] calcBackOff Pointer to the function parameters.
 */
void RegionCustomBTypeCalcBackOff( CalcBackOffParams_t* calcBackOff );

/*!
 * \brief Searches and set the next random available channel
 *
 * \param [OUT] channel Next channel to use for TX.
 *
 * \param [OUT] time Time to wait for the next transmission according to the duty
 *              cycle.
 *
 * \retval Function status [1: OK, 0: Unable to find a channel on the current datarate]
 */
bool RegionCustomBTypeNextChannel( NextChanParams_t* nextChanParams, uint8_t* channel, uint32_t* time );

/*!
 * \brief Adds a channel.
 *
 * \param [IN] channelAdd Pointer to the function parameters.
 *
 * \retval Status of the operation.
 */
LoRaMacStatus_t RegionCustomBTypeChannelAdd( ChannelAddParams_t* channelAdd );

/*!
 * \brief Removes a channel.
 *
 * \param [IN] channelRemove Pointer to the function parameters.
 *
 * \retval Returns true, if the channel was removed successfully.
 */
bool RegionCustomBTypeChannelsRemove( ChannelRemoveParams_t* channelRemove  );

/*!
 * \brief Sets the radio into continuous wave mode.
 *
 * \param [IN] continuousWave Pointer to the function parameters.
 */
void RegionCustomBTypeSetContinuousWave( ContinuousWaveParams_t* continuousWave );


void RegionCustomBTypeSetPreambleLength( uint8_t CustomPreambleLength );

void RegionCustomBTypeSetCustomMaxChannel( uint8_t CustomMaxChannel );

void RegionCustomBTypeSetNBOfChannels( uint8_t NumbChannels);

void RegionCustomBTypeSetRX2WindowFreq( uint32_t Frequency );

void RegionCustomBSetRegion( LoRaMacRegion_t region);

void RegionCustomBSetFirstRX1Channel( uint32_t firstRx1Channel);

void RegionCustomBSetLastRX1Channel( uint32_t lastRx1Channel);

void RegionCustomBSetStepWidthRX1Channel( uint32_t stepwidthRx1Channel);

void RegionCustomBTypeTXPowerParams(uint8_t MinTxPower, uint8_t MaxTxPower, uint8_t DefaultTxPower );

/*! \} defgroup REGIONCustomBType */

#endif // __REGION_CustomBType_H__
