#ifndef __ESP_CONFIG_H__
#define __ESP_CONFIG__H__


#include "stdio.h"
#include <string.h>
#include "stdint.h"
#include "stdlib.h"



#define BUS_6U      6
#define BUS_16U     16


#ifdef DRISHTI_EN
	#define BUS_TYPE    BUS_ESPA
#else	
	#define BUS_TYPE    BUS_6U
#endif



#if BUS_TYPE == BUS_6U

#define MAX_SLR_PNL_CNT                         4               //Number of solar panels
#define MAX_MPPT_CONV_CNT                       4               //Number of MPPT Convertors
#define MAX_BTRY_CNT_PER_SET            3               //Number of Battery Sets
#define UPP_LWR_TEMP_THSH_LMT           2               //Upper and Lower Temperature Threshold Limit
#define MAX_BTRY_TMP_SNSR_CNT           8               //Number of Battery Temperature Sensors
                                                        //In Janus1, there are 9 batteries in total.
                                                        //However temperature sensors are available only for 8 of those batteries.
#define MAX_VOLT_RAIL_CNT                       16              //Number of Channel Voltage Rails
#define MAX_EPS_OUT_CH_CNT                      12              //Number of EPS Output Channels
#define EPS_MEM_ADDR_SIZE                       4               //EPS Address Size
#define EPS_DATA_SIZE                           8               //EPS Variable Data Size
#define MAX_VOLT_CNT                        5
#define MAX_DEV_TMR_ST_VAL                  5
#define MAX_CHNL_MAP_CNT                    3
#define MAX_AUTO_ON_CNT                     3
#define RSVD_3                                          4               //Reserved bytes 4
#define RSVD_4                                          4               //Reserved bytes 4
#define RSVD_7                                          7
#define RSVD_5                                          5
#define RSVD_11                                         11
#define RSVD_6                                          6
#define RSVD_8                                          8
#define EXPOSURE_LUT_SIZE   10


#define SET_CONV_BRD_RESP_LEN   13
#define SET_BTRY_BRD_RESP_LEN   19
#define SET_OP_CHNL_RESP_LEN    3
#define OP_CHNL_CUR_RESP_LEN    17
#define SET_EPS_MODE_RESP_LEN   4


#define MAX_VOLT_THRSH_CNT          3
#define EPS_BTRY_OFF_VAL       9.0      //volt
#define EPS_BTRY_CRTCAL_VAL  10.5
#define EPS_BTRY_SAFE_VAL    10.8
#define EPS_BTRY_NORMAL_VAL  11.5
#define EPS_BTRY_FULL_VAL    12.6

#elif BUS_TYPE == BUS_ESPA

#define EPS_BTRY_OFF_VAL     28      //volt
#define EPS_BTRY_CRTCAL_VAL  28.8
#define EPS_BTRY_SAFE_VAL    29.8
#define EPS_BTRY_NORMAL_VAL  31
#define EPS_BTRY_FULL_VAL    33.6

#define MAX_VOLT_THRSH_CNT          8
#define MAX_SLR_PNL_CNT                         5               //Number of solar panels
#define MAX_MPPT_CONV_CNT                       4               //Number of MPPT Convertors
#define MAX_BTRY_CNT_PER_SET            8               //Number of Battery Sets
#define UPP_LWR_TEMP_THSH_LMT           2               //Upper and Lower Temperature Threshold Limit
#define MAX_BTRY_TMP_SNSR_CNT           8               //Number of Battery Temperature Sensors
#define EXPOSURE_LUT_SIZE   10
                                                        //In Janus1, there are 9 batteries in total.
                                                        //However temperature sensors are available only for 8 of those batteries.
#define MAX_VOLT_RAIL_CNT                       11              //Number of Channel Voltage Rails
#define MAX_EPS_OUT_CH_CNT                      11              //Number of EPS Output Channels
#define EPS_MEM_ADDR_SIZE                       4               //EPS Address Size
#define EPS_DATA_SIZE                           8               //EPS Variable Data Size
#define MAX_VOLT_CNT                        5
#define MAX_DEV_TMR_ST_VAL                  5
#define MAX_CHNL_MAP_CNT                    3
#define MAX_AUTO_ON_CNT                     3
#define RSVD_3                                          4               //Reserved bytes 4
#define RSVD_4                                          4               //Reserved bytes 4
#define RSVD_7                                          7
#define RSVD_5                                          5
#define RSVD_11                                         11
#define RSVD_6                                          6
#define RSVD_8                                          8


#define SET_CONV_BRD_RESP_LEN   13
#define SET_BTRY_BRD_RESP_LEN   19
#define SET_OP_CHNL_RESP_LEN    3
#define OP_CHNL_CUR_RESP_LEN    12
#define SET_EPS_MODE_RESP_LEN   4

#else 
	
    #error BUS Type configuration is invalid
#endif


#define GET_CONV_BRD_LEN		9 		 ///< Length of EPS_GET_CONV_BRD command
#define GET_BTRY_BRD_LEN		9 		 ///< Length of EPS_GET_BTRY_BRD command
#define GET_OP_CHNL_PARM_LEN		9 		 ///< Length of EPS_GET_OP_CHNL_PARM command
#define GET_EPS_MODE_LEN		9 		 ///< Length of EPS_GET_EPS_MODE command
#define EPS_ALERT_FLAG_LEN		9 		 ///< Length of EPS_ALERT_FLAG command

#define RSVD_12						12

#define MAX_EPS_OUT_CH_CNT_DIV		2		//Number of EPS Output Channels
#define MAX_HRM_CNT                 2
#define EPS_BTRTY_TMPR_CNT			8


extern uint16_t eps_crc_cnt[2];

typedef struct
{
    uint32_t tm_rsp_id;
    uint8_t* data;
}obc_pack_data_s;

/**
 * @brief EPS module success failure status
 */
typedef enum
{
        EPS_SUCCESS=0x00U,                              ///< EPS success
        EPS_CRC_FAIL=-1,                                ///< EPS CRC failure
        EPS_ERR=-2,                                             ///< EPS error
        EPS_COM_FAIL=-3,                                ///< EPS communication failure
        EPS_INVLD_ARG=-4,                               ///< EPS invalid argument
        EPS_NULL_PTR_ERR=-5,                    ///< EPS NULL pointer error
        EPS_SET_PARAM_ERR=-6,                   ///< EPS set parameter error
        EPS_RESP_ERR=-7
}eps_err_sts_e;


/**
 * @brief Individual parameters present in set frame of group EPS_SET_CONV_BRD
 *
 */
typedef struct _eps_set_conv_brd_s
{
    uint8_t mppt_enb_cnt;
    uint8_t conv_op_enb_cnt;
    uint8_t conv_op_volt_cnt;
    uint8_t  mem_set;
    uint8_t  mppt_enb[MAX_MPPT_CONV_CNT];
    uint8_t  op_conv_enb[MAX_MPPT_CONV_CNT];
    float    op_conv_volt[MAX_MPPT_CONV_CNT];
}__attribute__((__packed__))eps_set_conv_brd_s;

/**
 * @brief Individual parameters present in response frame of group EPS_SET_CONV_BRD.
 * As this structure is not in usecase taglength was not implemented
 *
 */
typedef struct _eps_set_conv_board_resp_s
{
    uint8_t resp;
    uint8_t num_cfg_fail;
    uint8_t mppt_conv_enb_sts[MAX_MPPT_CONV_CNT];
    uint8_t op_conv_enb_sts[MAX_MPPT_CONV_CNT];
    uint8_t op_conv_volt_set_sts[MAX_MPPT_CONV_CNT];
}__attribute__((__packed__))eps_set_conv_board_resp_s;


/**
 * @brief Individual parameters present in response frame of group EPS_GET_CONV_BRD
 *
 */

typedef struct _eps_get_conv_brd_rd_s
{
    uint8_t mppt_enb_cnt;
    uint8_t op_conv_enb_cnt;
    uint8_t pv_volt_cnt;
    uint8_t pv_cur_cnt;
    uint8_t op_conv_volt_cnt;
    uint8_t  resp;
    uint8_t  mppt_enb[MAX_MPPT_CONV_CNT];
    uint8_t  op_conv_enb_sts[MAX_MPPT_CONV_CNT];
    float    pv_volt[MAX_SLR_PNL_CNT];
    float    pv_cur[MAX_SLR_PNL_CNT];
    float    op_conv_volt[MAX_MPPT_CONV_CNT];
}__attribute__((__packed__))eps_get_conv_brd_rd_s;


/**
 * @brief Individual parameters present in response frame of group EPS_GET_CONV_BRD
 *
 */

typedef struct _eps_get_conv_brd_stng_s
{
    uint8_t op_conv_volt_cnt;
    uint8_t  resp;
    float    op_conv_volt[MAX_MPPT_CONV_CNT];
}__attribute__((__packed__))eps_get_conv_brd_stng_s;

/**
 * @brief Individual parameters present in set frame of group EPS_SET_BTRY_BRD
 *
 */
typedef struct _eps_set_btry_brd_s
{
    uint8_t temp_lmt_sns_cnt;
    uint8_t mem_set;
    float   cur_lmt_chrg;
    float   cur_lmt_dischrg;
    uint8_t temp_lmt[MAX_BTRY_TMP_SNSR_CNT][UPP_LWR_TEMP_THSH_LMT];
    uint8_t heat_mode;
    uint8_t heat_mode_flgs;
}__attribute__((__packed__))eps_set_btry_brd_s;


/**
 * @brief Individual parameters present in response frame of group EPS_SET_BTRY_BRD
 * doubt.
 *As this structure is not in usecase taglength was not implemented
 */
typedef struct _eps_set_btry_brd_resp_s
{
    uint8_t resp;
    uint8_t num_cfg_fail;
    uint8_t btry_chrg_lmt_sts;
    uint8_t btry_disch_lmt_sts;
    uint8_t temp_lmt[MAX_BTRY_TMP_SNSR_CNT][UPP_LWR_TEMP_THSH_LMT];
}__attribute__((__packed__))eps_set_btry_brd_resp_s;

/**
 * @brief Individual parameters present in response frame of group EPS_SET_BTRY_BRD
 *
 */
typedef struct _eps_get_btry_brd_rd_s
{
    uint8_t btry_volt_cnt;
    uint8_t btry_temp_cnt;
    uint8_t resp;
    float tot_btry_volt;
    float btry_volt[MAX_VOLT_THRSH_CNT];
    float btry_cur;
    uint8_t btry_temp[MAX_BTRY_TMP_SNSR_CNT];
    uint8_t btry_flg;
}__attribute__((__packed__))eps_get_btry_brd_rd_s;

/**
 * @brief Individual parameters present in response frame of group EPS_SET_BTRY_BRD
 *
 */
typedef struct _eps_get_btry_brd_stng_s
{
    uint8_t btry_temp_sns_cnt ;
    uint8_t resp;
    float   cur_lmt_chrg;
    float   cur_lmt_dischrg;
    uint8_t temp_lmt[MAX_BTRY_TMP_SNSR_CNT][UPP_LWR_TEMP_THSH_LMT];
    uint8_t heat_mode;
    uint8_t heat_mode_flgs;
}__attribute__((__packed__))eps_get_btry_brd_stng_s;

/**
 * @brief EPS_SET_OP_CHNL_CUR
 *
 */
typedef struct _eps_set_out_chnl_enb_S
{
    uint8_t mem_set;
    uint8_t chnl1_8;
    uint8_t chnl9_12;
}__attribute__((__packed__))eps_set_out_chnl_enb_s;

/**
 * @brief EPS_SET_OP_CHNL_CUR
 *  As this structure is not in usecase taglength was not implemented
 */
typedef struct _eps_set_out_chnl_enb_resp_s
{
    uint8_t resp;
    uint8_t num_cfg_rsp;
    uint8_t chnl1_8;
    uint8_t chnl9_12;
}__attribute__((__packed__))eps_set_out_chnl_enb_resp_s;


/**
 * @brief EPS_SET_OP_CHNL_CUR
 *
 */
typedef struct _eps_get_out_chnl_sts_s
{
    uint8_t resp;
     uint8_t chnl1_8;
    uint8_t chnl9_12;
    uint8_t hrm_rls_left;
    uint8_t hrm_rls_right;
}__attribute__((__packed__))eps_get_out_chnl_sts_s;

/**
 * @brief EPS_SET_OP_CHNL_CUR
 *
 */
typedef struct _eps_set_out_chnl_crnt_lmt_s
{
    uint8_t chnl_cnt;
    uint8_t mem_set;
    float   chnl[MAX_VOLT_RAIL_CNT];
}__attribute__((__packed__))eps_set_out_chnl_crnt_lmt_s;

/**
 * @brief EPS_SET_OP_CHNL_CUR
 *As this structure is not in usecase taglength was not implemented
 */
typedef struct _eps_set_out_chnl_cur_lmt_resp_s
{
    uint8_t resp;
    uint8_t num_cfg_fail;
    uint8_t chnl[MAX_VOLT_RAIL_CNT];
}__attribute__((__packed__))eps_set_out_chnl_cur_lmt_resp_s;

/**
 * @brief extracted data from eps_get_btry_brd_s added with latest system time when data received
 *
 */
typedef struct _eps_get_out_chnl_cur_rd_s
{
    uint8_t volt_rail_cnt;
    uint8_t resp;
    float  volt_rail[MAX_VOLT_RAIL_CNT];
}__attribute__((__packed__))eps_get_chnl_cur_rd_s;

/**
 * @brief extracted data from eps_get_btry_brd_s added with latest system time when data received
 *
 */
typedef struct _eps_get_out_chnl_cur_stng_s
{
    uint8_t volt_rail_cnt;
    uint8_t resp;
    float  volt_rail[MAX_VOLT_RAIL_CNT];
}__attribute__((__packed__))eps_get_chnl_cur_stng_s;

/**
 * @brief extracted data from eps_get_btry_brd_s added with latest system time when data received
 *
 */
typedef struct set_eps_mode_s
{
    uint8_t mem_set;
    float   nrml_mode_thrld_volt;
    float   safe_mode_thrld_volt;
    float   crtl_mode_thrld_volt;
}__attribute__((__packed__))eps_set_mode_s;


/**
 * @brief Individual parameters present in frame of group EPS_SET_MODE_PARM
 * As this structure is not in usecase taglength was not implemented
 */
typedef struct _eps_set_mode_resp_s
{
    uint8_t resp;
    uint8_t num_cfg_fail;
    uint8_t nrml_mode_thrld_volt;
    uint8_t safe_mode_thrld_volt;
    uint8_t crtt_mode_thrld_volt;
}__attribute__((__packed__))eps_set_mode_resp_s;


/**
 * @brief Individual parameters present in frame of group EPS_GET_MODE_PARM
 *
 */
typedef struct
{
    uint8_t resp;
    float   nrml_mode_thrld_volt;
    float   safe_mode_thrld_volt;
    float   crtl_mode_thrld_volt;
}__attribute__((__packed__))eps_get_stng_mode_resp_s;

/**
 * @brief Individual parameters present in frame of group EPS_GET_MODE_PARM
 *
 */
typedef struct
{
    uint8_t resp;
    uint8_t mode;
    uint8_t chnl1_5_ovr_cur_flg;
    uint8_t chnl6_11_ovr_cur_flg;
    uint8_t btry_chrg_ovrcur_flg;
    uint8_t btry_dischrg_ovrcur_flg;
    uint8_t btry_volt_blw_10_7v;
    uint8_t chnl_num;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
}__attribute__((__packed__))eps_alert_flag_resp_s;

typedef struct
{
    uint8_t resp;
    uint8_t fw_mjr_vers;
    uint8_t fw_mnr_vers;
    uint8_t fw_patch_vers;					
    uint8_t com_prtcl_mjr_vers;
    uint8_t com_prtcl_mnr_vers;
//    uint8_t crc;
}__attribute__((__packed__))eps_get_fw_s;

typedef struct
{
    uint16_t obc_com_timeout_sec;
    uint8_t num_of_obc_rst_for_eps_rst;
}__attribute__((__packed__))eps_set_obc_com_timeout_s;

typedef struct
{
    uint8_t resp;
    uint8_t obc_com_timeout_sec;
    uint8_t num_of_obc_rst_for_eps_rst;
}__attribute__((__packed__))eps_set_obc_com_timeout_resp_s;

typedef struct
{
    uint8_t resp;
    uint8_t obc_com_timeout_sec_byte1;
    uint8_t obc_com_timeout_sec_byte2;
    uint8_t num_of_obc_rst_for_eps_rst;
}__attribute__((__packed__))eps_get_obc_com_timeout_s;


#endif
