/**
 * @file sdr_app_main.h
 *
 * @brief This file has the control blocks, enumeration and function prototypes of SDR control layer of S-Band module
 *
 * @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 SDR_APP_MAIN_H
#define SDR_APP_MAIN_H

#include "exo_osal.h"
#include "exo_ral_cmn.h"
#include "exo_types.h"

typedef float f32;
typedef double f64;

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup Beacon message string size
 */
#define BCON_MSG_SZ						18

#define  MAX_SDR_CFG  MAX_RADIO_MOD /** Specific for Janus 2 */

#define UHF_BEACON_REQ 0x01
#define SBAND_BEACON_REQ 0x02

#define OBC_BEACON_RSP   0x01
#define SEN_BEACON_RSP   0x02
#define EPS_BEACON_RSP   0x04
#define ADCS_BEACON_RSP  0x08
#define UHF_BEACON_RSP   0x10
#define SBAND_BEACON_RSP 0x20
#define GPS_BEACON_RSP   0x40

#define SBAND_FW_UPD_ST  0x01
#define UHF_FW_UPD_ST	 0x10

#define SDR_MH_READY (OBC_BEACON_RSP|SEN_BEACON_RSP|EPS_BEACON_RSP|ADCS_BEACON_RSP|UHF_BEACON_RSP|SBAND_BEACON_RSP|GPS_BEACON_RSP)

#define TEMP_SNS_CNT  6
#define VOLT_SNS_CNT  6
#define CUR_SNS_CNT   6

typedef enum
{
    SDR_NOT_PRESENT,  /** This SDR is not physically present in the satellite */
    SDR_PRESENT,  /** This SDR is present and not configured to be activated – powered off */
    SDR_CONFIGURED_INACTIVE, /** This SDR is configured but not doing any active TX/RX */
    SDR_CONFIGURED_ACTIVE_RX, /** This SDR is configured but not doing any active RX */
    SDR_CONFIGURED_ACTIVE_TX, /** This SDR is configured but not doing any active TX */
    SDR_CONFIGURED_ACTIVE /** This SDR is actively doing RX and TX */
}e_sdr_sts;

typedef struct __attribute__((packed)) 
{
    uint8_t sdr_fsm_st:4;
    uint8_t rssi0:2;
    uint8_t rssi1:2;
    uint32_t rx_err_cnt;
    uint32_t tx_frm_cnt;
    uint32_t rx_freq;
    uint16_t symb_freq:8;
    uint16_t mod:2;
    uint16_t roll_off:2;
    uint16_t rand_enb:2;
    uint16_t cc_enb:1;
    uint16_t rs_enb:1;
}s_sdr_sband_mh_info;

typedef struct __attribute__((packed)) 
{
    uint8_t sdr_fsm_st:4;
    uint8_t rssi0:2;
    uint8_t rssi1:2;
    uint32_t rx_err_cnt;
    uint32_t tx_frm_cnt;
    uint32_t rx_freq;
    uint16_t symb_freq:4;
    uint16_t mod:1;
    uint16_t roll_off:1;
    uint16_t crc_enb:2;
    uint16_t dc_filter_enb:2;
    uint16_t cc_enb:2;
    uint16_t rand_enb:2;
    uint16_t manchester_coding_en:2;
}s_sdr_uhf_mh_info;

typedef struct __attribute__((packed)) 
{
    uint8 radio_id;
    uint8 sdr_sts;
    uint8 sdr_dplx_typ;  /** 0-half duplex; 1-full duplex*/
    uint8 sdr_num_rx;  /** Number of RX ports*/
    uint8 sdr_num_tx; /** Number of TX ports*/
    uint8 comb_rx_support;  /**Number of RX supported in parallel or combined */
    uint8 comb_tx_support; /**Number of TX supported in parallel or combined */
    uint32 min_freq; /** Minimum frequency supported */
    uint32 max_freq; /** Maximum frequency supported */
    /** If full duplex, the above value will carry TX frequency values and below fields will carry RX frequencies */
    uint32 min_freq_rx; /** Minimum frequency supported */
    uint32 max_freq_rx; /** Maximum frequency supported */
    uint32 rsvd[10]; /** Reserved for future use */
}s_sdr_cfg;

typedef struct __attribute__((packed)) 
{
    uint32 beacon_enb_tmr;
    uint32 beacon_prd_tmr;
    uint32 beacon_rep_tmr;
    uint32 tx_data_rep_cnt;
    uint32 sdr_tm_tx_tmr;
    uint32 sdr_tx_on_tmr;
    uint32 sdr_rx_on_tmr;
    uint32 sdr_tm_read_tmr;
}s_sdr_tmr_cfg;

typedef struct __attribute__((packed)) 
{
    uint8 valid_rssi_cnt;
    uint8 inv_rssi_cnt;
    float rssi_min;
    float rssi_typ;
    uint32 rssi_prd_tmr;
    uint32 short_rx_on_tmr;
    uint32 long_rx_on_tmr;
}s_sdr_rssi_cfg;

typedef struct __attribute__((packed)) 
{
        uint8 tm_auto_read_sts; /**0-disable, 1-enable*/
        uint32 tm_read_depth; /**Depth of in which TM information need to be retrieved and stored*/
        uint32 tm_short_store_dur; /**Duration for which the information can be stored in RAM */
        uint32 tm_long_store_dur; /** Duration for which the information can be stored in external memory */

    }s_sdr_tm_cfg;

    typedef struct __attribute__((packed)) 
    {
        uint16_t temp_sens_typ_thrs;
        uint16_t temp_sens_min_thrs;
        uint16_t temp_sens_max_thrs;

    }s_comms_temp_sns;

    typedef struct __attribute__((packed)) 
    {
        uint16_t volt_sens_typ_thrs;
        uint16_t volt_sens_min_thrs;
        uint16_t volt_sens_max_thrs;

    }s_comms_volt_sns;

    typedef struct __attribute__((packed)) 
    {
        uint16_t cur_sens_typ_thrs;
        uint16_t cur_sens_min_thrs;
        uint16_t cur_sens_max_thrs;

    }s_comms_cur_sens;

    typedef struct __attribute__((packed)) 
    {
        s_comms_temp_sns  temp_sensor[TEMP_SNS_CNT];
        s_comms_volt_sns  volt_sensor[VOLT_SNS_CNT];
        s_comms_cur_sens   current_sensor[CUR_SNS_CNT];

    }s_sdr_sens_cfg;

typedef struct __attribute__((packed)) 
{
        uint8 radio_id;
        uint8 sdr_sts;
        uint8 sdr_dplx_typ;
        uint8 sdr_num_rx;
        uint8 sdr_num_tx;
        uint8 comb_rx_support;
        uint8 comb_tx_support;
        uint32 min_freq;
        uint32 max_freq;
        uint32 min_freq_rx;
        uint32 max_freq_rx;
        uint32 rsvd;
        uint32 beacon_normal_enb_tmr;
        uint32 beacon_normal_prd_tmr;
        uint32 beacon_normal_rep_tmr;
        uint32 beacon_safe_enb_tmr;
        uint32 beacon_safe_prd_tmr;
        uint32 beacon_safe_rep_tmr;
        uint32 tx_data_rep_cnt;
        uint32 sdr_normal_tm_tx_tmr;
        uint32 sdr_normal_tx_on_tmr;
        uint32 sdr_normal_rx_on_tmr;
        uint32 sdr_safe_tm_tx_tmr;
        uint32 sdr_safe_tx_on_tmr;
        uint32 sdr_safe_rx_on_tmr;
        uint32 sdr_tm_read_tmr;
    uint8 valid_rssi_cnt;
    uint8 inv_rssi_cnt;
    float rssi_min;
    float rssi_typ;
    uint32 rssi_prd_tmr;
    uint32 short_rx_on_tmr;
    uint32 long_rx_on_tmr;
        uint8 tm_auto_read_sts;
        uint32 tm_read_depth;
        uint32 tm_short_store_dur;
        uint32 tm_long_store_dur;
        uint16 volt_sens_typ_thrs[6];
        uint16 volt_sens_min_thrs[6];
        uint16 volt_sens_max_thrs[6];
        uint16 temp_sens_typ_thrs[6];
        uint16 temp_sens_min_thrs[6];
        uint16 temp_sens_max_thrs[6];
        uint16 cur_sens_typ_thrs[6];
        uint16 cur_sens_min_thrs[6];
        uint16 cur_sens_max_thrs[6];

    }s_sdr_nvm_cfg;

typedef struct __attribute__((packed)) 
{
    uint8_t fsm_state_mode;          /*!< ADCS Current state and ADCS Current mode */
    uint16_t sns_actr_pwr_sts; /*!< Sensor, actuators Power status*/
    uint8_t actrs_run_sts;      /*!< ADCS Actuator running status*/

}s_sdr_adcs_mh_pld;

typedef struct __attribute__((packed)) 
{
	uint8_t totl_btry_volt;
	uint8_t totl_panel_pwr;
	uint16_t chnl_sts;
	uint8_t overcrnt_err_flg_byte1;
	uint16_t overcrnt_err_flg_byte2;

}s_sdr_eps_mh_pld;

typedef struct __attribute__((packed)) 
{
    /* FSM current state */
    uint8_t fsm_state;

    /* No of OBC resets */
    uint8_t num_resets;

    /* IO errors */
    uint16_t io_err;

    /* System errors */
    uint8_t sys_err;

}s_sdr_obc_mh_pld;

typedef struct __attribute__((packed)) 
{
    /* OBC Voltage sensor value */
    uint8_t obc_power_sensor;

    /* PS sensor value */
    uint8_t ps_power_sensor;

    /* OBC temperature */
    uint8_t temp_obc;

    /* PS temperature */
    uint8_t temp_ps;

    /* ES temperature */
    uint8_t temp_es;

    /* GPS temperature */
    uint8_t temp_gps;

}s_sdr_sen_mh_pld;

/*** TX_Beacon_DATA_STRUCT ***/
typedef struct __attribute__((packed)) 
{
	uint8 bcon_msg[BCON_MSG_SZ];
	uint32 utc_tim;
	uint8 utc_gps_tim_sts; //UTC Time from GPS/RTC	Uint8	1
	float latitude;// Latitude// 	Float	4
	float longitude; //Longitude//	Float	4
	float altitude; ////Antenna altitude above/below mean sealevel in meters
	double x_pos;
	double y_pos;
	double z_pos;
	double x_vel;
	double y_vel;
	double z_vel;
}s_sdr_bcon_utc_gps;

typedef struct __attribute__((packed)) 
{
	s_sdr_bcon_utc_gps bcon_utc_gps;
	s_sdr_obc_mh_pld obc_mh;
	s_sdr_sen_mh_pld sen_mh;
	s_sdr_eps_mh_pld eps_mh;
	s_sdr_adcs_mh_pld adcs_mh;
	s_sdr_sband_mh_info sdr_sband_mh;
	s_sdr_uhf_mh_info sdr_uhf_mh;
}s_sdr_beacon_pld;

/**
 *@brief This structure is used for overrule provision
 */

typedef struct
{
    uint8_t overrule_enb;
    uint8_t norm_radio_id;
    uint8_t safe_radio_id;
}__attribute__((__packed__)) s_tctm_rm_overrule;



typedef struct {
    uint8_t radio_id;
    uint8_t gpass_beacon_tx_enb;
    uint8_t beacon_tx_count;
    uint32_t beacon_tx_interval;
}__attribute__((__packed__)) s_sdr_gpass_beacon_cfg;


/**
  * @brief  sdr_app tc id enum definition
  */

/*
typedef enum
{
    TC_SET_SDR_CFG=850,
    TC_GET_SDR_CFG,
    TC_SET_SDR_STS,
    TC_GET_SDR_STS,
    TC_SET_SDR_TMR_CFG,
    TC_GET_SDR_TMR_CFG,
    TC_SET_SDR_RX_RSSI_CFG,
    TC_GET_SDR_RX_RSSI_CFG,
    TC_SET_SDR_TM_CFG,
    TC_GET_SDR_TM_CFG,

}e_sdr_app_tc_id;
*/

/**
  * @brief  sdr_app tm id enum definition
  */
/*
typedef enum
{
    TM_SET_SDR_CFG=850,
    TM_GET_SDR_CFG,
    TM_SET_SDR_STS,
    TM_GET_SDR_STS,
    TM_SET_SDR_TMR_CFG,
    TM_GET_SDR_TMR_CFG,
    TM_SET_SDR_RX_RSSI_CFG,
    TM_GET_SDR_RX_RSSI_CFG,
    TM_SET_SDR_TM_CFG,
    TM_GET_SDR_TM_CFG,

}e_sdr_app_tm_id;
*/

typedef struct __attribute__((packed)) 
{
	uint8_t safe;
	uint8_t norm;
	uint8_t crit;

}e_sdr_app_bat_sts;

typedef enum
{
	TC_STS_SET_SUCC,
	TC_STS_GET_SUCC,
	TC_STS_SET_FAIL,
	TC_STS_GET_FAIL,
	TC_STS_FW_UP_SUCC,
	TC_STS_FW_UP_FAIL,
	TC_STS_FW_UP_DELAY,
	TC_STS_FW_NOT_RCV,
	TC_STS_FW_WRONG,
	TC_STS_FW_BAD_CHKSUM,
	TC_STS_UNKNOWN_TC_TM
}e_tc_tm_sts;



int32_t sdr_app_init(void);
void sdr_app_main(void *param);
int8_t sdr_app_msg_hdlr(uint16_t msg_id, uint16_t msg_len, void *pld_ptr);
uint8_t comms_cust_pattern_rf_tx_gen(uint8_t* data);
void comms_force_tx_ipc_sdr(uint8_t radio_id, uint16_t pld_len, uint8_t* pld_ptr);
void comms_upd_tmr_normal(void);
void comms_upd_tmr_safe(void);
void sdr_gs_contact_status_upd_itc(void);


#ifdef __cplusplus
}
#endif
#endif /* SDR_APP_MAIN_H */
