
/**
 * @file comms_sband_init.h
 *
 * @brief This file has the prototypes of functions used for S-Band driver initialisation
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*
*/

#ifndef COMMS_SBAND_INIT_H_
#define COMMS_SBAND_INIT_H_


#include <stdio.h>
#include "comms_csw.h"
#include "comms_sband_api.h"

#define MAX_CHUNK_SIZE 213                    ///< Maximum chunk size
#define MAX_PLD_SIZE 213                      ///< Maximum payload size


#define IQS_MTU_SIZE 1432
#define BUFFERSIZE 1500
#define BER_PRIME_NUM 173
#define RX_BUFF_SIZE 1400
#define CLTU_SIZE	1650

/**
 * @brief Global variable for comms configurations
 */
extern s_comms_tx_cfg PLACE_IN_EXT_RAM_MEM tx_cfg_global;
extern s_comms_rx_cfg PLACE_IN_EXT_RAM_MEM rx_cfg_global;
extern s_comms_sys_cfg PLACE_IN_EXT_RAM_MEM sys_cfg_global;
extern s_comms_cal_cfg PLACE_IN_EXT_RAM_MEM cal_cfg_global;

/*MACROS TO CHECK MIN AND MAX VALUES OF PARAMETER*/

#define MIN_RX_FREQ 2200000000
#define MAX_RX_FREQ 2290000000

#define MIN_RX_TYPE 0
#define MAX_RX_TYPE 2

#define MIN_RX_RATE 100000
#define MAX_RX_RATE 5000000

#define MIN_RX_RAND 0
#define MAX_RX_RAND 1

#define MIN_RX_FRAMES 0
#define MAX_RX_FRAMES 255

#define MIN_RX_DETECTED 0
#define MAX_RX_DETECTED 255

#define MIN_RX_RSSI 0
#define MAX_RX_RSSI 255

#define MIN_RX_FREQERR 0
#define MAX_RX_FREQERR 255

#define MIN_RX_SYMERR 0
#define MAX_RX_SYMERR 255

#define MIN_TX_FREQ 2025000000
#define MAX_TX_FREQ 2110000000

#define MIN_TX_TYPE 0
#define MAX_TX_TYPE 1

#define MIN_TX_RATE 100000
#define MAX_TX_RATE 5000000

#define MIN_TX_POUT 0
#define MAX_TX_POUT 35

#define MIN_TX_CC 0
#define MAX_TX_CC 1

#define MIN_TX_RAND 0
#define MAX_TX_RAND 1

#define MIN_TX_IDLETIME 0
#define MAX_TX_IDLETIME 255

#define MIN_TX_SIZE 1
#define MAX_TX_SIZE 1500

#define MIN_TX_FRAMES 0
#define MAX_TX_FRAMES 1500

#define MIN_IP_STACK_ENB 0
#define MAX_IP_STACK_ENB 1

/*#define MIN_DEV_IP 0
#define MAX_DEV_IP 1

#define MIN_DEV_PORT 0
#define MAX_DEV_PORT 1

#define MIN_ETH_PREFIX 0
#define MAX_ETH_PREFIX 1

#define MIN_DEV_IP_TEMP 0
#define MAX_DEV_IP_TEMP 1

#define MIN_DEV_PORT_UDP 0
#define MAX_DEV_PORT_UDP 1

#define MIN_DEV_IP_UDP 0
#define MAX_DEV_IP_UDP 1

#define MIN_LEOP_PROD_KEY 0
#define MAX_LEOP_PROD_KEY 1

#define MIN_CON_PROD_KEY 0
#define MAX_CON_PROD_KEY 1

*/
#define MIN_RX_GAIN  0
#define MAX_RX_GAIN 255

#define MIN_IDX_GAIN 0
#define MAX_IDX_GAIN 255

#define MIN_PWR_MIN 0
#define MAX_PWR_MIN 255

#define MIN_PWR_MAX 0
#define MAX_PWR_MAX 255

#define MIN_RX_CH_NO 0
#define MAX_RX_CH_NO 8

#define MIN_MTU_SIZE 1
#define MAX_MTU_SIZE 1500

#define MIN_PWR_ATTN_LUT 0
#define MAX_PWR_ATTN_LUT 255

#define MIN_TX_ATTN_GAIN 0
#define MAX_TX_ATTN_GAIN 255
/*
#define MIN_FPGA_VER 0
#define MAX_FPGA_VER 1

#define MIN_SWAP_VER 0
#define MAX_SWAP_VER 1

#define MIN_CPU_VER 0
#define MAX_CPU_VER 1
*/
#define MIN_BEACON_TX_TIMEOUT 0
#define MAX_BEACON_TX_TIMEOUT 255

#define MIN_TX_WIN_TIMEOUT 0
#define MAX_TX_WIN_TIMEOUT 255

/*DEFAULT VALUES OF PARAMETERS*/
#define DEFAULT_RX_FREQ 2042500000


#define DEFAULT_RX_TYPE 0


#define DEFAULT_RX_RATE 64


#define DEFAULT_RX_RAND 1


#define DEFAULT_RX_FRAMES 0


#define DEFAULT_RX_RSSI 151


 #define DEFAULT_RX_FREQERR 0


#define DEFAULT_RX_SYMERR 0 


#define DEFAULT_TX_FREQ 2242500000


#define DEFAULT_TX_TYPE 2048


#define DEFAULT_TX_POUT 33


#define DEFAULT_TX_CC 1


#define DEFAULT_TX_RAND 1


#define DEFAULT_TX_IDLETIME 30


#define DEFAULT_TX_SIZE 1432


#define DEFAULT_TX_FRAMES 0


#define DEFAULT_TX_RATE 64


#define DEFAULT_IP_STACK_ENB 0
/*

#define DEFAULT_DEV_IP 0


#define DEFAULT_DEV_PORT 0


#define DEFAULT_ETH_PREFIX 0


#define DEFAULT_DEV_IP_TEMP 0


#define DEFAULT_DEV_PORT_UDP 0


#define DEFAULT_DEV_IP_UDP 0


#define DEFAULT_LEOP_PROD_KEY 0


#define DEFAULT_CON_PROD_KEY 0

*/
#define DEFAULT_RX_GAIN 76 


#define DEFAULT_IDX_GAIN 64


#define DEFAULT_PWR_MIN 0


#define DEFAULT_PWR_MAX 33


#define DEFAULT_RX_CH_NO 0


#define DEFAULT_MTU_SIZE 1432


//#define DEFAULT_PWR_ATTN_LUT 0


#define DEFAULT_TX_ATTN_GAIN 3


//#define DEFAULT_FPGA_VER 0


//#define DEFAULT_SWAP_VER 0


//#define DEFAULT_CPU_VER 0


#define DEFAULT_BEACON_TX_TIMEOUT 0


#define DEFAULT_BEACON_TX_WIN_TIMEOUT 0

#define DEFAULT_RX_DETECTED 151

#define DEFAULT_TX_WIN_TIMEOUT 0

/**
 * @brief This thread receives the tc data through the socket interface
 * and handles the received data through handle_tc_data()
 *
 * @return Doesn't return
 * @retval Doesn't return
 */
void comms_sband_rx_thread(void*);

/**
* @brief  This function is used for write the S-band configuration to NVM memory
*/
void comms_sband_cfg_nvm_write(void);

/**
* @brief  This function is used for write the S-band configuration to NVM memory
*/
void comms_sband_xlinks_cfg_nvm_write(void);


/**
* @brief  This function is used for reading the S-Band configuration parameters from NVM memory
*/
void comms_sband_xlinks_cfg_nvm_read(void);


/**
* @brief  This function is used for reading the S-Band configuration parameters from NVM memory
*/
void comms_sband_cfg_nvm_read(void);

/**
* @brief  This function is used for updating the ground contact status post OTSU reset
*/
void sband_nvm_upd_otsu_ground_contact(void);

/**
* @brief  This function is used for updating the very first ground contact status
*/
void sband_nvm_upd_first_ground_contact(void);

/**
* @brief  This function is used for fetching the satellite flight time stored in the NVM location
*
* @return epochtime of the satellite flight start
*/
uint64_t sband_permanant_timer_read(void);

/**
* @brief  This function is used for S-Band parameters initialisation
*/
void comms_init_sband_cfg(void);

/**
* @brief This function is used for S-Band System parameters initialisation
*
* @param sys_cfg : pointer to system configuration
*/
void comms_init_sys_cfg(s_comms_sys_cfg *sys_cfg);

/**
* @brief This function is used for S-Band calibration parameters initialisation
*
* @param cal_cfg : pointer to calibration config.
*/
void comms_init_cal_cfg(s_comms_cal_cfg *cal_cfg);

/**
* @brief This function is used for S-Band TX parameters initialisation
*
* @param tx_cfg : pointer to transmit config
*/
void comms_init_tx_cfg(s_comms_tx_cfg *tx_cfg);

/**
* @brief This function is used for S-Band RX parameters initialisation
*
* @param rx_cfg : pointer to receive config
*/
void comms_init_rx_cfg(s_comms_rx_cfg *rx_cfg);

void comms_init_sband_cfg(void);

void comms_sband_nvm_init(void);

void comms_sband_timer_init_default(void);



#endif /*COMMS_SBAND_INIT_H_*/
