/*
 * @file sensor_driver_intf.h
 *
 * @brief This file contains structures,enumerations,function prototypes to
 * poll the data from the sensors and handle the interrupts
 *
 * @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_SENSOR_INTERFACE_H_
#define DRIVERS_SENSOR_INTERFACE_H_


#include <stdint.h>
#include "exo_hal_io_al_common.h"
#include "exo_osal.h"
#include "exo_common.h"
#include "exo_ahw_al_dt_common.h"
#include "exo_ahw_al_psm_common.h"
#include "exo_ahw_al_hsc_common.h"
#include "exo_ahw_al_temp_sensor_common.h"
#include "obc_tc_tm.h"

typedef void (*sensor_data_process_cb_fptr)(void *args);
typedef void (*sensor_event_cb_fptr)(void *args);

#define MIN_HEAT_COIL_PRDCTY 50      // in milli seconds
#define SENSOR_MAX_BUFF_SIZE 5

#ifdef LINUX_TEMP_PORT
#define SENSOR_POLLING_TM 5*1000
#else
#define SENSOR_POLLING_TM 1*60*1000
#endif
#define SENSOR_TIMER_MAX 0xFFFFFFFF
#define BRD_RESTART_TMR_MS 10*60*1000

/**
 * @brief AHW driver configuration structure
 */
typedef struct
{
	uint8_t ahw_id;										/*!< AHW id															*/
	uint32_t timer_index;								/*!< timer periodicity													*/
	sensor_data_process_cb_fptr sns_data_process_cb;	/*!< sensor data processing callback function pointer				*/
	sensor_event_cb_fptr sns_event_cb;					/*!< sensor data event callback function pointer				*/

}ahw_driver_cfg_s;

/**
 * @brief Sensor event type enumeration
 */
typedef enum
{
	SNS_TMR_EXPRY_EVNT,					///< sensor timer expiry event
	SNS_CFG_EVNT,						///< sensor configuration event
	MODULE_PWR_ON_EVNT,					///< Module power ON event
	MODULE_PWR_OFF_EVNT,				///< Module power OFF event
	HEAT_COIL_ON_EVNT,					///< Heat coil ON event
	HEAT_COIL_OFF_EVNT,					///< Heat coil OFF event
	SNS_INTERRUPT,						///< Sensor interrupt
	SNS_BRD_RESTART_TMR_EXPRY,			///< Sensor board restart timer expiry
	SNS_EVNT_MAX
}sns_event_type;

/**
 * @brief Sensor threshold status enumeration
 */
typedef enum
{
	OBC_TEMP_SNS_HIGH_THRSLD,			///< on-board controller temperature sensor high threshold
	PS_TEMP_SNS_HIGH_THRSLD,			///< payload server temperature sensor high threshold
	EDGE_TEMP_SNS_HIGH_THRSLD,			///< edge board temperature sensor high threshold
	GPS_TEMP_SNS_HIGH_THRSLD,			///< gps temperature sensor high threshold

	/***** GEN2 *****/
	EDGE1_GEN2_TEMP_SNS_HIGH_THRSLD,
	EDGE2_GEN2_TEMP_SNS_HIGH_THRSLD,
	PS_GEN2_TEMP_SNS_HIGH_THRSLD,
	NIC1_TEMP_SNS_HIGH_THRSLD,
	SSD1_TEMP_SNS_HIGH_THRSLD,
	SSD2_TEMP_SNS_HIGH_THRSLD,
	SSD3_TEMP_SNS_HIGH_THRSLD,
	SSD4_TEMP_SNS_HIGH_THRSLD,

	OBC_TEMP_SNS_LOW_THRSLD,			///< on-board controller temperature sensor low threshold
	PS_TEMP_SNS_LOW_THRSLD,				///< payload server temperature sensor low threshold
	EDGE_TEMP_SNS_LOW_THRSLD,			///< edge board temperature sensor low threshold
	GPS_TEMP_SNS_LOW_THRSLD,			///< gps temperature sensor high threshold

	EDGE1_GEN2_TEMP_SNS_LOW_THRSLD,
	EDGE2_GEN2_TEMP_SNS_LOW_THRSLD,
	PS_GEN2_TEMP_SNS_LOW_THRSLD,
	NIC1_TEMP_SNS_LOW_THRSLD,
	SSD1_TEMP_SNS_LOW_THRSLD,
	SSD2_TEMP_SNS_LOW_THRSLD,
	SSD3_TEMP_SNS_LOW_THRSLD,
	SSD4_TEMP_SNS_LOW_THRSLD,

	OBC_PWR_SNS_HIGH_THRSLD,			///< on-board controller power sense monitor high threshold
	PS_PWR_SNS_HIGH_THRSLD,				///< payload server power sense monitor high threshold

	OBC_PWR_SNS_LOW_THRSLD,				///< on-board controller power sense monitor low threshold
	PS_PWR_SNS_LOW_THRSLD,				///< payload server power sense monitor low threshold

	SNS_THRSLD_TYPE_MAX

}sns_thrsld_sts;

/**
 * @brief Sensor information queue id
 */
typedef enum
{
	PSM_PS_DATA_QUEUE_ID,
	PSM_OBC_DATA_QUEUE_ID,
	TEMP_OBC_DATA_QUEUE_ID,
	TEMP_PS_DATA_QUEUE_ID,
	TEMP_ES_DATA_QUEUE_ID,
	TEMP_GPS_DATA_QUEUE_ID,
	/***** GEN2 TEMP*****/
	TEMP_EDGE1_GEN2_DATA_QUEUE_ID,
	TEMP_EDGE2_GEN2_DATA_QUEUE_ID,
	TEMP_PS_GEN2_DATA_QUEUE_ID,
	TEMP_NIC1_DATA_QUEUE_ID,
	TEMP_SSD1_DATA_QUEUE_ID,
	TEMP_SSD2_DATA_QUEUE_ID,
	TEMP_SSD3_DATA_QUEUE_ID,
	TEMP_SSD4_DATA_QUEUE_ID,
	/***** GEN2 HSC *****/
	HSC_PS_BRD_SUPPLY_DATA_QUEUE_ID,
	HSC_PS1_SUPPLY_DATA_QUEUE_ID,
	HSC_PS2_SUPPLY_DATA_QUEUE_ID,
	HSC_EDGE1_SUPPLY_DATA_QUEUE_ID,
	HSC_EDGE2_SUPPLY_DATA_QUEUE_ID,
	HSC_EDGE1_HEAT_COIL_DATA_QUEUE_ID,
	HSC_EDGE2_HEAT_COIL_DATA_QUEUE_ID,
	HSC_IO_SUPPLY_DATA_QUEUE_ID,
	HSC_IO_BRD_TOP1_DATA_QUEUE_ID,
	HSC_IO_BRD_TOP2_DATA_QUEUE_ID,
	HSC_IO_BRD_BOT1_DATA_QUEUE_ID,
	HSC_IO_BRD_BOT2_DATA_QUEUE_ID,
	/***** GEN2 PSM *****/
	PSM_PS_GEN2_DATA_QUEUE_ID,
	PSM_CORE_BRD_A_DATA_QUEUE_ID,
	PSM_CORE_BRD_B_DATA_QUEUE_ID,
	PSM_PS1_DATA_QUEUE_ID,
	PSM_PS2_DATA_QUEUE_ID,
	PSM_EDGE1_DATA_QUEUE_ID,
	PSM_EDGE1_SUPPLY_DATA_QUEUE_ID,
	PSM_IO_SUPPLY_DATA_QUEUE_ID,

	DT_SPE01_DATA_QUEUE_ID,
	DT_SPE02_DATA_QUEUE_ID,
	DT_SPB01_DATA_QUEUE_ID,
	DT_SPW01_DATA_QUEUE_ID,
	DT_SPW02_DATA_QUEUE_ID,
	HSC_EDGE_DATA_QUEUE_ID,
	HSC_PS1_DATA_QUEUE_ID,
	HSC_PS2_DATA_QUEUE_ID,
	DT_PS_DATA_QUEUE_ID,
	DT_EDGE_DATA_QUEUE_ID,
	SNS_QUEUE_ID_MAX
}sns_queue_id;

/**
 * @brief sensor Beacon health structure
 */
typedef struct
{

    /* OBC power sensor value in watts */
    uint8_t obc_power;

    /* PS power sensor value in watts */
    uint8_t ps_power;

    /* OBC temperature */
    int8_t temp_obc;

    /* PS temperature */
    int8_t temp_ps;

    /* ES temperature */
    int8_t temp_es;

    /* GPS temperature */
    int8_t temp_gps;

    /***** GEN 2 *****/

    /* GEN2 EDGE BRD1 temperature */
    int8_t temp_edge1;

    /* GEN2 EDGE BRD2 temperature */
    int8_t temp_edge2;

    /* GEN2 PS temperature */
    int8_t temp_ps_gen2;

    /* GEN2 NIC temperature */
    int8_t temp_nic1;

    /* GEN2 SSD1 temperature */
    int8_t temp_ssd1;

    /* GEN2 SSD2 temperature */
    int8_t temp_ssd2;

    /* GEN2 SSD3 temperature */
    int8_t temp_ssd3;

    /* GEN2 SSD4 temperature */
    int8_t temp_ssd4;

}s_sensor_beacon_info;

/**
 * @brief API to get the RTC time
 * @param[in]  time - pointer of RTC time structure
 * @retval no return
 */
void get_rtc_tm(uint64_t *time);

/**
 * @brief API to initialize the sensor task
 * @retval no return
 */
void sensors_init();

/**
 * @brief API to initialize and start the polling timer of the
 * sensor with given application hardware id
 * @param[in]  timer_index - timer periodicity
 * @param[in]  ahw_id - application hardware ID
 * @retval no return
 */
void sensor_driver_start(uint32_t timer_index, uint32_t ahw_id);

/**
 * @brief API to stop the sensor with given application hardware id
 * @param[in]  ahw_id - application hardware ID
 * @retval no return
 */
void sensor_driver_stop(uint8_t ahw_id);

/**
 * @brief API to initialize the temperature sensor with the given configuration
 * @param[in]  tmp_hdl - pointer to temperature sensor handle
 * @param[in]  t_sns_prm - pointer to temperature sensor configuration structure
 * @retval Error status
 */
uint8_t temp_sns_init(ahw_al_temp_sensor_hdl* tmp_hdl, temp_sens_cfg* t_sns_prm);

/**
 * @brief API to initialize the hotswap controller with the given configuration
 * @param[in]  hsc_hdl - pointer to hotswap controller handle
 * @param[in]  hswp_sns_prm - pointer to hotswap controller configuration structure
 * @retval Error status
 */
uint8_t hsc_sns_init(ahw_al_hsc_hdl* hsc_hdl,hotswap_cntlr_cfg* hswp_sns_prm);

/**
 * @brief API to initialize the power sense monitor with the given configuration
 * @param[in]  psm_hdl - pointer to power sense monitor handle
 * @param[in]  psm_sns_prm - pointer to power sense monitor configuration structure
 * @retval Error status
 */
uint8_t psm_sns_init(ahw_al_psm_hdle* psm_hdl, power_sense_mntr_cfg* psm_sns_prm);

/**
 * @brief API to initialize the digital thermostat with the given configuration
 * @param[in]  dt_hdl - pointer to digital thermostat handle
 * @param[in]  dt_sns_prm - pointer to digital thermostat configuration structure
 * @retval Error status
 */
uint8_t dt_sns_init(ahw_al_dt_hdle* dt_hdl,digital_thermostat_cfg* dt_sns_prm);

/**
 * @brief API to deinitialize the temperature sensor
 * @param[in]  ahw_id - application hardware ID
 * @retval Error status
 */
uint8_t temp_sns_deinit(uint8_t ahw_id);

/**
 * @brief API to deinitialize the hotswap controller
 * @param[in]  ahw_id - application hardware ID
 * @retval Error status
 */
uint8_t hsc_sns_deinit(uint8_t ahw_id);

/**
 * @brief API to deinitialize the power sense monitor
 * @param[in]  ahw_id - application hardware ID
 * @retval Error status
 */
uint8_t psm_sns_deinit(uint8_t ahw_id);

/**
 * @brief API to deinitialize the power sense monitor
 * @param[in]  ahw_id - application hardware ID
 * @retval Error status
 */
uint8_t dt_sns_deinit(uint8_t ahw_id);

/**
 * @brief API to initialize the sensor parameters from the NVM
 * @retval Error status
 */
uint8_t sns_nvm_init();

/**
 * @brief API to write the sensor default values in NVM
 * @retval Error status
 */
uint8_t sns_nvm_write_dft();

/**
 * @brief API to read the sensor configuration data from the NVM
 * @retval Error status
 */
uint8_t sns_nvm_read_cfg();

uint8_t get_ssd_heat_coil_sts(uint8_t board_id);
uint32_t get_ssd_heat_coil_periodicity(uint8_t board_id);
uint8_t set_ssd_heat_coil_periodicity(uint8_t board_id, uint32_t ht_cl_prdcty);
uint8_t set_ssd_heat_coil_enable(uint8_t board_id, uint8_t enable_sts);

void temp_sns_reconfig(uint32_t ahw_id);
void psm_sns_reconfig(uint32 ahw_id);
void dt_sns_reconfig(uint32 ahw_id);
void hsc_sns_reconfig(uint32_t ahw_id);

uint8_t get_temperature_sensor_cfg(uint16_t ahw_id, temp_sens_cfg* config);
uint8_t get_power_monitor_sensor_cfg(uint16_t ahw_id, power_sense_mntr_cfg* config);
uint8_t get_hotswap_cntlr_sensor_cfg(uint16_t ahw_id, hotswap_cntlr_cfg* config);
uint8_t get_dgtl_thermst_sensor_cfg(uint16_t ahw_id, digital_thermostat_cfg* config);
void tc_buff_fw_req_queue(uint32 instance_size, uint32 num_of_instance, uint8 queue_id);
void tc_buff_fw_enqueue_req(uint8* psm_data, uint8 queue_id);

#endif
