/**
*@file eps_cntrl.h
*@brief Header file for EPS control module
*/

#ifndef EPS_INC_EPS_CNTRL_H_
#define EPS_INC_EPS_CNTRL_H_

#include "eps.h"

/**
*@def EPS_DBG_EN
*@brief Enable EPS debug
*/

#define EPS_DBG_EN
#define EPS_BATTERY_ERR									   04
#define EPS_BTRY_TEMP_RD_TMR  1000
#define EPS_MODE_CNT 3
#define RD_TMR_SECS 		 4*1000				//60*1000   /*!< Periodic read timer in seconds*/
#define EPS_RD_BFR_MAX_SIZE  18		   /*!< Maximum length of EPS read buffer*/
#define BTRY_MODE_CHNG_CNTR  2
#define OVR_CUR_ERR_TM 	10*1000
#define EPS_CHNL_PWR_DWN_CHECK_TMR   20*1000
#define EPS_MAX_TM_MIN 		5
#define EPS_ONE_MIN_TMR     14
#define EPS_BTRY_TRMP_CB_TMR  4

typedef sat_hw_ch_s eps_ch_s;

extern sat_btry_info_s  sat_btry_info;
extern sat_dev_sts_s 	sat_dev_sts[MAX_SAT_DEV_CNT];

typedef struct _eps_beakcon_info_s
{
	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;

}eps_beakcon_info_s;

typedef struct _epc_chnl_ovr_cur_err_s
{
	uint8_t chnl_enb_dis;
	uint8_t tmr_cntr;
	uint8_t rst_cntr;
	uint8_t eps_chnl_ovr_cur_cntr;
}epc_chnl_ovr_cur_err_s;




/**
 * @brief enum count for triggering periodical data read from EPS module
 *
 */
typedef enum
{
    EPS_GS_GET_CONV_BRD_BUFF = 0,			///< get convertor board buffer
    EPS_GS_GET_BTRY_BRD_BUFF,				///< get battery board buffer
    EPS_GS_GET_CHNL_PARM_BUFF,				///< get channel parameter buffer
    EPS_GS_GET_ALRT_BRD_BUFF				///< get alert board buffer
}eps_buff_e;


/**
 * @brief enum representing power state of the output channel
 *
 */
typedef enum
{
	EPS_PWR_OFF=0,						///< Power OFF
	EPS_PWR_ON=1,						///< Power ON
	EPS_PWR_RSTRT=2						///< Power RESTART
}eps_ch_state;

/**
 * @brief enum representing states present in eps control thread (eps_proc)
 *
 */
typedef enum eps_sem
{
	 EPS_ONE_TMR_CB=100,				///< EPS RD HW information
	 EPS_ALRT_SEM=101,					///< EPS alert
	 EPS_HW_PWR_SEQ=102,				///< HW power sequence
	 EPS_BTRY_MODE_CHNL_STATE=103,		///< Battery mode channel state
//	 EPS_SET_CHNL_STATE = 104,			///< set channel state
	 PGOOD_STATE = 105,
	 EPS_BTRY_TEM_CB=106,
	 EPS_CHNL_OVR_CUR_ERR_CB=107,
	EPS_RESET_TRIG=108

}eps_sem_s;


/**
 * @brief EPS controller success failure status
 *
 */
typedef enum
{
	EPS_CTRL_SUCCESS 	= 0,				///< EPS control success
	EPS_CTRL_COMM_ERR 	= 1,				///< EPS control communication error
	EPS_CTRL_PARAM_ERR	= 2,				///< EPS control parameter error
	EPS_CTRL_HW_ERR		= 3,				///< EPS control HW error
	EPS_CTRL_ERR		= 4,				///< EPS control error
	EPS_CTRL_BTRY_ERR 	= 5,				///< EPS control battery error
	EPS_CTRL_IN_PROG	= 6,					///< EPS control in progress
	EPS_CTRL_FW_MISMATCH_ERR	= 7,					///< EPS control firmware version mismatch error
	EPS_CTRL_CHNL_OVR_CUR_ERR = 8
}eps_ctrlr_err_sts_s;

/**
 * @brief EPS information queue id
 */
typedef enum
{
    EPS_ALRT_FLAG_QUEUE_ID,
    EPS_BTRY_BRD_QUEUE_ID,
    EPS_CONV_BRD_QUEUE_ID,
    EPS_CHNL_PAR_QUEUE_ID,
    EPS_QUEUE_ID_MAX
}eps_hlth_queue_id;

#if 0
/**
 * @brief Device number representation for respective output channel connected with EPS module
 *
 */
typedef enum eps_ch
{
	EPS_PL_TRANSCELESTIAL = 0,				///< EPS payload transelestial
	EPS_X_BAND,								///< EPS x band
	EPS_S_BAND,								///< EPS s band
	EPS_ADCS0,								///< EPS ADCS 0
	EPS_ADCS,								///< EPS ADCS
	EPS_PROP_UNIT_THRUSTER,					///< EPS propellant unit thruster
	EPS_PL_ZES,								///< EPS payload zes
	EPS_PL_SAYARLABS,						///< EPS payload sayarlabs
	EPS_PL_NETRA,							///< EPS payload netra
	EPS_EDGE_BRD,							///< EPS edge board
	EPS_OBC_PS_GPS_STD_PL_BRD,				///< EPS OBC payload server GPS STD payload board
	EPS_HRM,								///< EPS HRM
	EPS_PL_SERVER,							///< EPS payload server
	EPS_GPS,								///< EPS GPS
	MAX_EPS_DEV_CNT
}eps_ch_s;

#ifdef DRISHTI_EN

#define EPS_PL_HW1  		EPS_PL_TRANSCELESTIAL
#define EPS_PL_HW2 			EPS_PL_ZES
#define EPS_EDGE_BRD1		EPS_PL_SAYARLABS
#define EPS_UHF_RADIO_		EPS_ADCS0
#define EPS_MSI_PL_HEATR	EPS_PL_NETRA
#define EPS_SSD_HEATR		EPS_HRM
#define EPS_ETH_SW			MAX_EPS_DEV_CNT

#endif
#define EPS_VALID_ON  3
#define EPS_AUTO_ON  3
#endif

/**
 * @brief structure containing information on device auto on and auto off based on current battery mode
 *
 */
typedef struct __attribute__((packed))
{
    uint8_t  mode_cnt;						/*!< This member specifies number of valid on 				*/
    uint16_t valid_on[EPS_MODE_CNT];			/*!< valid on												*/
    uint16_t auto_on[EPS_MODE_CNT];				/*!< auto on 												*/
}eps_btry_ch_map_s;
#if 0
/**
 * @brief structure containing information on device auto on and auto off based on current battery mode without EPS OFF mode
 *
 */

typedef struct __attribute__((packed))
{
	 uint16_t valid_on[3];						/*!< valid on			*/
	 uint16_t auto_on[3];						/*!< auto on			*/
}eps_btry_ch_map_old_s;
#endif
/**
 * @brief structure containing current system time information
 *
 */
typedef struct __attribute__((packed))
{
	uint8_t min;						/*!< minute					*/
	uint8_t sec;						/*!< seconds				*/
	uint16_t msec;						/*!< millisecond			*/
}eps_time_s;


/**
 * @brief structure containing maximum state count and timer count value for individual device
 *
 */
typedef struct __attribute__((packed))
{
  uint8_t max_st_cnt;					/*!< Maximum state count			*/
  eps_time_s st_val[5];					/*!< state value					*/
}eps_dev_tmr_s;

/**
 * @brief structure containing maximum state count and timer count value for individual device power on and power off
 *
 */
typedef struct __attribute__((packed))
{
	eps_dev_tmr_s on_tmr;				/*!< on timer			*/
	eps_dev_tmr_s off_tmr;				/*!< off timer			*/
}eps_dev_tmr_t;

/**
 * @brief structure containing information on device channel number and its state
 *
 */
typedef struct __attribute__((packed))
{
  uint8_t ch_no;					/*!< channel number			*/
  uint8_t ch_st;					/*!< channel state			*/
}eps_dev_sts_s;


/**
 * @brief structure containing information on current state count of individual device when on/off power sequence triggered
 *
 */
typedef struct __attribute__((packed))
{
    uint8_t ch_no;						/*!< channel number				*/
    eps_dev_tmr_t ch_tmr_val;			/*!< channel timer value		*/
}eps_dev_tmr_c;

/**
 * @brief structure containing information voltage range of all battery modes
 *
 */
typedef struct
{
    float volt[3];					/*!< voltage			*/
}eps_btry_thrshld_s;


/**
* @brief API for initialize
*
*/
void eps_init();

/**
* @brief API for deinitialize timer and thread
*
*/
void eps_deinit();

/**
* @brief This API triggers on off sequence of output channel provided current battery voltage
*
* @param[in]  ch_no  : output channel selection
* @param[in]  state  : on off request for channel number
*
* @return status of eps_ctrlr_err_sts_s
* @retval equal to zero -> Success / greater than zero -> failure.
*
*/
eps_ctrlr_err_sts_s eps_ctrl_en_chnl(eps_ch_s ch_no,eps_ch_state state);


/**
* @brief This API sets power timer for individual channel which triggered from TC TM
*
* @param[in]  eps_dev_tmr_c - structure holds the eps device timer
*
* @return status of eps_ctrlr_err_sts_s
* @retval equal to zero -> Success / greater than zero -> failure.
*
*/
eps_ctrlr_err_sts_s eps_ctrl_set_pwr_tmr(eps_dev_tmr_c *eps_dev_tmr);

/**
* @brief This API is for copying existing data to structure eps_btry_ch_map
*
* @param[in]  _eps_btry_ch_map : destination id of thread
*
* @return status of eps_ctrlr_err_sts_s
* @retval equal to zero -> Success / greater than zero -> failure.
*/

eps_ctrlr_err_sts_s eps_ctrl_get_btry_ch_map(eps_btry_ch_map_s *_eps_btry_ch_map);

/**
* @brief This API is for copying incoming data to structure eps_btry_ch_map
*
* @param[in]  _eps_btry_ch_map : channel vs battery mode map
*
* @return status of eps_ctrlr_err_sts_s
* @retval equal to zero -> Success / greater than zero -> failure.
*/


eps_ctrlr_err_sts_s eps_ctrl_set_btry_ch_map(eps_btry_ch_map_s *_eps_btry_ch_map);

#if 0
/**
* @brief This API sets battery threshold limit for all 4 modes that can be changed dynamically from GS
*
* @param[in]  _eps_btry_thrshld - battery threshold values of 4 battery mode
*
*/

void eps_set_btry_thrshld(eps_btry_thrshld_s *_volt);
#endif
/**
* @brief This API gets data from eps driver and extract re-pack the information and send to ground station
*
* @param[in]  data  : output channel number
* @param[out] param : on-off state of channel
*
*/
void eps_gs_read_buffer(uint8_t *data,uint8_t param);

/**
* @brief This API gets data from eps driver and extract re-pack the information
*
* @param[out]  data : data pointer to copy eps health info
*
*/
void eps_gs_hlth_chk(uint8_t *data);

/**
* @brief This API get battery board read values
*
*/
void eps_get_btry_brd_rd_val();

/**
* @brief This API trigger the battery temperature read callback function
*
* @param[in]  eps_get_btry_brd : structure holds the get battery board parameter
*
*/
void eps_ctrlr_btry_tempr_rd_cb_trig(eps_get_btry_brd_tmr_s *eps_get_btry_brd);

/*
 * @brief This API checks the EPS FW version
 *
 * @retval equal to zero -> Success / greater than zero -> failure.
 */
eps_err_sts_e eps_fw_vers_check();

/*
 * @brief This API send the TC to EPS control function and
 * sends back the TM response
 *
 * @param[in]  eps_tc_len : TC data length
 * @param[in]  eps_tc_buff : TC data
 * @param[in]  eps_tc_id : TC id
 */
void eps_ctrl_tc_tm_pack(uint16_t eps_tc_len,void *eps_tc_buff,uint16_t eps_tc_id);

/*
 * @brief This API controls the TC and TM for EPS controller
 *
 * @param[in]  data : obc pack data info pointer
 */
void eps_ctrl_tctm_code(obc_pck_data_eps *data);

/**
* @brief This API used to send Buffer request to TC buffering FW
*
* @param[in] instance_size: sizeo of each instance
* @param[in] num_of_instance: number of instances
* @param[in] queue_id: queue id
*
*/
void tc_eps_buff_fw_req_queue(uint32_t instance_size, uint32_t num_of_instance, uint8_t queue_id);

/**
* @brief This API used to send Buffer request to TC buffering FW
*
* @param[in] data : data to be enqueued
* @param[in] queue_id: queue id
*
*/
void tc_eps_buff_fw_enqueue_req(uint8_t* data, uint8_t queue_id);

/**
* @brief This API used to send Buffer request to TC buffering FW
*
*/
void tc_eps_req_hlt_buff(void);
void eps_chnl_ovr_cur_err_flag_indication(uint16_t eps_chnl_ovr_cur_flag);
eps_ctrlr_err_sts_s eps_fw_update_ctrl();
void eps_nvm_write_fw_vers();
void eps_nvm_read_fw_vers(eps_get_fw_s *eps_get_fw);
void eps_deinit_other_process_before_fw_upd();
#endif /* DRIVERS_AHW_DRIVERS_EPS_INC_EPS_H_ */
