/**
 * @file eps_config.h
 *
 * @brief This file contains function prototypes and data definitions that
 * helps to create and unpack a TC frame
 *
 * @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 DRIVERS_AHW_DRIVERS_EPS_CONFIG_INC_EPS_H_
#define DRIVERS_AHW_DRIVERS_EPS_CONFIG_INC_EPS_H_

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

//#define DRISHTI_EN

#define RSVD_3	 					3		///< Reserved bytes 3
#define RSVD_4	 					4		///< Reserved bytes 4
#define RSVD_5						5		///< Reserved bytes 5
#define RSVD_6						6		///< Reserved bytes 6
#define RSVD_7						7		///< Reserved bytes 7
#define RSVD_8         				8		///< Reserved bytes 8
#define RSVD_11						11		///< Reserved bytes 11
#define RSVD_12						12		///< Reserved bytes 12

#define UPP_LWR_TEMP_THSH_LMT 		2 		///< Upper and Lower Temperature Threshold Limit
#define MAX_MPPT_CONV_CNT			4		///< Number of MPPT Convertors

#ifndef DRISHTI_EN

#define MAX_SLR_PNL_CNT				4		///< Number of solar panels
#define MAX_BTRY_CNT_PER_SET		3		///< Number of Battery Sets
#define MAX_VOLT_RAIL_CNT 			16		///< Number of Channel Voltage Rails
#define SET_OP_CHNL_CUR_LMT         17		///< Length of EPS_SET_OP_CHNL_CUR_LMT_PARAM response
#define MAX_EPS_OUT_CH_CNT			12		///< Number of EPS Output Channels
#define MAX_VOLT_THRSH_CNT          3		///< Number of voltage threshold count

#else
#define MAX_EPS_DEV_CNT      11
#define MAX_SLR_PNL_CNT			    5		///< Number of solar panels
#define MAX_BTRY_CNT_PER_SET		8		///< Number of Battery Sets
#define MAX_VOLT_RAIL_CNT			11		///< Number of Channel Voltage Rails
#define SET_OP_CHNL_CUR_LMT         12		///< Length of EPS_SET_OP_CHNL_CUR_LMT_PARAM response
#define MAX_EPS_OUT_CH_CNT			11		///< Number of EPS Output Channels
#define MAX_VOLT_THRSH_CNT          8		///< Number of voltage threshold count
#endif

#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 EPS_MEM_ADDR_SIZE 			4		///< EPS Address Size
#define EPS_DATA_SIZE 				8		///< EPS Variable Data Size
#define SET_CONV_BRD_RESP_LEN       13      ///< Length of EPS_SET_CONV_BRD response
#define SET_BTRY_BRD_RESP_LEN		19		 ///< Length of EPS_SET_BTRY_BDRY response
#define SET_OP_CHNL_RESP_LEN        3       ///< Length of EPS_SET_OP_CHNL_PARM response
#define SET_EPS_MODE_RESP_LEN       4       ///< Length of EPS_SET_EPS_MODE response
#define GET_CONV_BRD_LEN			2 		 ///< Length of EPS_GET_CONV_BRD command
#define GET_BTRY_BRD_LEN			2 		 ///< Length of EPS_GET_BTRY_BRD command
#define GET_OP_CHNL_PARM_LEN		2 		 ///< Length of EPS_GET_OP_CHNL_PARM command
#define GET_EPS_MODE_LEN			2 		 ///< Length of EPS_GET_EPS_MODE command
#define EPS_ALERT_FLAG_LEN			2 		 ///< Length of EPS_ALERT_FLAG command
#define EPS_FW_LEN			2 		 ///< Length of EPS_ALERT_FLAG command
//#define MAX_VOLT_THRSH_CNT          3		///< Number of maximum voltage threshold
#define MAX_HRM_CNT                 2		///< Number of HRM
#define SET_RTC_RESV_LEN            8
#define GET_RTC_SEN_LEN             2
#define SET_EPS_COM_TIMEOUT_RESP_LEN 2
#define GET_EPS_COM_TIMEOUT_TX_LEN  2

extern uint16_t eps_crc_cnt[2];

/**
 * @brief OBC data packet structure
 */
typedef struct
{
    uint32_t tm_rsp_id;				/*!< TM response ID */
	uint16_t  rx_data_len;			/*!< RX data frame length */
	uint16_t  tx_data_len;			/*!< TX data frame length */
    uint8_t* rx_data;				/*!< RX data frame  */
	uint8_t* tx_data;				/*!< TX data frame  */
}obc_pck_data_eps;

/**
 * @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;						/*!< This member specifies the number of mppt_enb 				*/
    uint8_t conv_op_enb_cnt;					/*!< This member specifies the number of op_conv_enb 			*/
    uint8_t conv_op_volt_cnt;					/*!< This member specifies the number of op_conv_volt 			*/
    uint8_t  mem_set;							/*!< Mem set 													*/
    uint8_t  mppt_enb[MAX_MPPT_CONV_CNT];		/*!< Mppt enable status 										*/
    uint8_t  op_conv_enb[MAX_MPPT_CONV_CNT];	/*!< output convertor enable 									*/
    float    op_conv_volt[MAX_MPPT_CONV_CNT];	/*!< Output convertor voltage 									*/
}__attribute__((__packed__))eps_set_conv_brd_s;

/**
 * @brief Individual parameters present in set frame of group EPS_SET_RTC_TM
 */
typedef struct _eps_set_rtc_tm_s
{
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    uint8_t date;
    uint8_t month;
    uint8_t year;
    uint8_t weekday;

}__attribute__((__packed__))eps_set_rtc_tm_s;


/**
 * @brief Individual parameters present in response frame of group EPS_SET_CONV_BRD_RESP
 */
typedef struct _eps_set_conv_board_resp_s
{
    uint8_t resp;										/*!< Response variable									*/
    uint8_t num_cfg_fail;								/*!< Number of configuration failure					*/
    uint8_t mppt_conv_enb_sts[MAX_MPPT_CONV_CNT];		/*!< Mppt convertor enable status						*/
    uint8_t op_conv_enb_sts[MAX_MPPT_CONV_CNT];			/*!< Output convertor enable status						*/
    uint8_t op_conv_volt_set_sts[MAX_MPPT_CONV_CNT];	/*!< Oupput convertor voltage set status				*/
}__attribute__((__packed__))eps_set_conv_board_resp_s;

/**
 * @brief Individual parameters present in response frame of group EPS_GET_CONV_BRD_RD
 */
typedef struct _eps_get_conv_brd_rd_s
{
    uint8_t mppt_enb_cnt;							/*!< This member specifies number of mppt_enb 					*/
    uint8_t op_conv_enb_cnt;						/*!< This member specifies number of op_conv_enb_sts 			*/
    uint8_t pv_volt_cnt;							/*!< This member specifies number of pv_volt 				    */
    uint8_t pv_cur_cnt;								/*!< This member specifies number of pv_cur						*/
    uint8_t op_conv_volt_cnt;						/*!< This member specifies number of op_conv_volt				*/
    uint8_t  resp;									/*!< Response variable											*/
    uint8_t  mppt_enb[MAX_MPPT_CONV_CNT];			/*!< mppt enable status											*/
    uint8_t  op_conv_enb_sts[MAX_MPPT_CONV_CNT];	/*!< output convertor enable status								*/
    float    pv_volt[MAX_SLR_PNL_CNT];				/*!< pv voltage													*/
    float    pv_cur[MAX_SLR_PNL_CNT];				/*!< pv current													*/
    float    op_conv_volt[MAX_MPPT_CONV_CNT];		/*!< output convertor voltage									*/
}__attribute__((__packed__))eps_get_conv_brd_rd_s;

/**
 * @brief Individual parameters present in response frame of group EPS_GET_RTC_TM
 */
typedef struct _eps_get_rtc_tm_s
{
	uint8_t resp;
	uint8_t hour;
	uint8_t minute;
	uint8_t second;
	uint8_t date;
	uint8_t month;
	uint8_t year ;
	uint8_t weekday;

}__attribute__((__packed__)) eps_get_rtc_tm_s;


/**
 * @brief Individual parameters present in response frame of group EPS_GET_CONV_BRD_STNG
 */
typedef struct _eps_get_conv_brd_stng_s
{
    uint8_t op_conv_volt_cnt;						/*!< This member specifies number of op_conv_volt		*/
    uint8_t  resp;									/*!< Response variable									*/
    float    op_conv_volt[MAX_MPPT_CONV_CNT];		/*!< Output convertor voltage							*/
}__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;											/*!< This member specifies number of tmp_lmt		*/
    uint8_t mem_set;													/*!< mem set										*/
    float   cur_lmt_chrg;												/*!< Cureent limit charge							*/
    float   cur_lmt_dischrg;											/*!< Cureent limit discharge						*/
    uint8_t temp_lmt[MAX_BTRY_TMP_SNSR_CNT][UPP_LWR_TEMP_THSH_LMT];		/*!< temperature limit								*/
    uint8_t heat_mode;													/*!< Heat mode										*/
    uint8_t heat_mode_flgs;												/*!< Heat mode flags								*/
}__attribute__((__packed__))eps_set_btry_brd_s;

/**
 * @brief Individual parameters present in set frame of group EPS_SET_BTRY_BRD_RESP
 * doubt
 */
typedef struct _eps_set_btry_brd_resp_s
{
    uint8_t resp;														/*!< Response variable										*/
    uint8_t num_cfg_fail;												/*!< Number of configuration failure						*/
    uint8_t btry_chrg_lmt_sts;											/*!< battery charge limit status							*/
    uint8_t btry_disch_lmt_sts;											/*!< battery discharge limit status							*/
    uint8_t temp_lmt[MAX_BTRY_TMP_SNSR_CNT][UPP_LWR_TEMP_THSH_LMT];   	/*!< Temperature limit										*/
}__attribute__((__packed__))eps_set_btry_brd_resp_s;

/**
 * @brief Individual parameters present in response frame of group EPS_SET_BTRY_BRD_RD
 */
typedef struct _eps_get_btry_brd_rd_s
{
    uint8_t btry_volt_cnt;							/*!< This member specifies number of btry_volt				*/
    uint8_t btry_temp_cnt;								/*!< This member specifies number of btry_temp				*/
    uint8_t resp;										/*!< Response variable										*/
    float tot_btry_volt;								/*!< Total battery voltage									*/
    float btry_volt[MAX_VOLT_THRSH_CNT];				/*!< Battery voltage										*/
    float btry_cur;										/*!< battery current										*/
    uint8_t btry_temp[MAX_BTRY_TMP_SNSR_CNT];			/*!< Battery temperature									*/
    uint8_t btry_flg;									/*!< Battery flag											*/
}__attribute__((__packed__))eps_get_btry_brd_rd_s;

/**
 * @brief Individual parameters present in response frame of group EPS_SET_BTRY_BRD_STNG
 */
typedef struct _eps_get_btry_brd_stng_s
{
    uint8_t btry_temp_sns_cnt;											/*!< This member specifies number of temp_lmt		*/
    uint8_t resp;														/*!< Response variable								*/
    float   cur_lmt_chrg;												/*!< Current limit charge							*/
    float   cur_lmt_dischrg;											/*!< current limit discharge						*/
    uint8_t temp_lmt[MAX_BTRY_TMP_SNSR_CNT][UPP_LWR_TEMP_THSH_LMT];		/*!< Temperature limit								*/
    uint8_t heat_mode;													/*!< Heat mode										*/
    uint8_t heat_mode_flgs;												/*!< Heat mode flags								*/
}__attribute__((__packed__))eps_get_btry_brd_stng_s;

/**
 * @brief Individual parameters present in set frame of group EPS_SET_OP_CHNL_ENB
 */
typedef struct _eps_set_out_chnl_enb_S
{
    uint8_t mem_set;								/*!< mem set								*/
    uint8_t chnl1_8;								/*!< channel for 1 to 8						*/
    uint8_t chnl9_12;								/*!< channel for 9 to 12					*/
}__attribute__((__packed__))eps_set_out_chnl_enb_s;

/**
 * @brief Individual parameters present in set frame of group EPS_SET_OP_CHNL_ENB_RESP
 */
typedef struct _eps_set_out_chnl_enb_resp_s
{
    uint8_t resp;									/*!< Response variable				*/
    uint8_t num_cfg_rsp;							/*!< number of configuration		*/
    uint8_t chnl1_8;								/*!< channel for 1 to 8						*/
    uint8_t chnl9_12;								/*!< channel for 9 to 12					*/
}__attribute__((__packed__))eps_set_out_chnl_enb_resp_s;

/**
 * @brief Individual parameters present in response frame of EPS_GET_OP_CHNL_STS
 */
typedef struct _eps_get_out_chnl_sts_s
{
    uint8_t resp;									/*!< Response variable								*/
    uint8_t chnl1_8;                                /*!< channel for 1 to 8                                         */
    uint8_t chnl9_12;								/*!< channel for 9 to 12                                         */
    uint8_t hrm_rls_left;							/*!< hrm release indication for left side			*/
    uint8_t hrm_rls_right;							/*!< hrm release indication for right side			*/
}__attribute__((__packed__))eps_get_out_chnl_sts_s;

/**
 * @brief Individual parameters present in set frame of EPS_SET_OP_CHNL_CUR_LMT
 */
typedef struct _eps_set_out_chnl_crnt_lmt_s
{
    uint8_t chnl_cnt;								/*!< This member specifies number of chnl			*/
    uint8_t mem_set;								/*!< mem set										*/
    float   chnl[MAX_VOLT_RAIL_CNT];				/*!< channel 										*/
}__attribute__((__packed__))eps_set_out_chnl_crnt_lmt_s;

/**
 * @brief Individual parameters present in set response frame of EPS_SET_OP_CHNL_CUR_LMT_RESP
 */
typedef struct _eps_set_out_chnl_cur_lmt_resp_s
{
    uint8_t resp;									/*!< Response variable						*/
    uint8_t num_cfg_fail;							/*!< Number of configuration failure		*/
    uint8_t chnl[MAX_VOLT_RAIL_CNT];				/*!< Channel								*/
}__attribute__((__packed__))eps_set_out_chnl_cur_lmt_resp_s;

/**
 * @brief Individual parameters present in response frame of EPS_GET_OP_CHNL_CUR_RD
 */
typedef struct _eps_get_out_chnl_cur_rd_s
{
    uint8_t volt_rail_cnt;							/*!< This member specifies number of volt_rail			*/
    uint8_t resp;									/*!< response variable									*/
    float  volt_rail[MAX_VOLT_RAIL_CNT];			/*!< voltage rail										*/
}__attribute__((__packed__))eps_get_chnl_cur_rd_s;

/**
 * @brief Individual parameters present in response frame of EPS_GET_OP_CHNL_CUR_STNG
 */
typedef struct _eps_get_out_chnl_cur_stng_s
{
    uint8_t volt_rail_cnt;					/*!< This member specifies number of volt_rail			*/
    uint8_t resp;							/*!< response variable									*/
    float  volt_rail[MAX_VOLT_RAIL_CNT];	/*!< voltage rail										*/
}__attribute__((__packed__))eps_get_chnl_cur_stng_s;

/**
 * @brief Individual parameters present in set frame of EPS_SET_MODE
 */
typedef struct set_eps_mode_s
{
    uint8_t mem_set;						/*!< mem set							*/
    float   nrml_mode_thrld_volt;			/*!< normal mode threshold voltage		*/
    float   safe_mode_thrld_volt;			/*!< safe mode threshold voltage		*/
    float   crtl_mode_thrld_volt;			/*!< critical mode threshold voltage	*/
}__attribute__((__packed__))eps_set_mode_s;

/**
 * @brief Individual parameters present in set response frame of group EPS_SET_MODE_PARM_RESP
 */
typedef struct _eps_set_mode_resp_s
{
    uint8_t resp;							/*!< Response variable						*/
    uint8_t num_cfg_fail;					/*!< number of configuration failure		*/
    uint8_t nrml_mode_thrld_volt;			/*!< normal mode threshold voltage			*/
    uint8_t safe_mode_thrld_volt;			/*!< safe mode threshold voltage			*/
    uint8_t crtt_mode_thrld_volt;			/*!< critical mode threshold voltage		*/
}__attribute__((__packed__))eps_set_mode_resp_s;

/**
 * @brief Individual parameters present in response frame of group EPS_GET_MODE_PARM
 */
typedef struct
{
    uint8_t resp;							/*!< Response variable					*/
    float   nrml_mode_thrld_volt;			/*!< normal mode threshold voltage		*/
    float   safe_mode_thrld_volt;			/*!< Safe mode threshold voltage		*/
    float   crtl_mode_thrld_volt;			/*!< critical mode threshold voltage	*/
}__attribute__((__packed__))eps_get_stng_mode_resp_s;

/**
 * @brief Individual parameters present in response frame of group EPS_GET_ALERT_FLAG
 */
typedef struct
{
    uint8_t resp;									/*!< Response variable											*/
    uint8_t mode;									/*!< mode														*/
    uint8_t chnl1_5_ovr_cur_flg;					/*!< overcurrent error flag for channel 1 to 5 */
    uint8_t chnl6_11_ovr_cur_flg;					/*!< overcurrent error flag for channel 6 to 11 */
    uint8_t btry_chrg_ovrcur_flg;					/*!< battery charge overcurrent error flag  */
    uint8_t btry_dischrg_ovrcur_flg;				/*!< battery discharge overcurrent error flag  */
    uint8_t btry_volt_blw_10_7v;					/*!< Battery volt flag */
    uint8_t chnl_num;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
}__attribute__((__packed__))eps_alert_flag_resp_s;

/**
 * @brief Individual parameters present in response frame of group EPS_GET_FW
 */
typedef struct 
{
    uint8_t resp;								/*!< Response flag */
    uint8_t fw_mjr_vers;						/*!< Firmware major version */
    uint8_t fw_mnr_vers;						/*!< Firmware minor version */
    uint8_t fw_patch_vers;						/*!< Firmware minor version */
    uint8_t com_prtcl_mjr_vers;					/*!< Communication protocol major version */
    uint8_t com_prtcl_mnr_vers;					/*!< Communication protocol minor version */
}__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_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;



#if 0
/**
 * @brief Individual parameters present in send frame of group EPS_GET_ADDR_VAR_OP_BRD
 *
 */
typedef struct
{
    uint8_t addr[EPS_MEM_ADDR_SIZE];
}__attribute__((__packed__))eps_set_var_addr_s;

/**
 * @brief Individual parameters present in recieve frame of group EPS_GET_ADDR_VAR_OP_BRD
 *
 */
typedef struct
{
    uint8_t data[EPS_DATA_SIZE];
    uint8_t reserved[RSVD_11];
}__attribute__((__packed__))eps_get_var_data_s;
#endif

#endif /* DRIVERS_AHW_DRIVERS_EPS_CONFIG_INC_EPS_H_ */
