/**
 * @file exo_ahw_al_temp_sensor_common.c
 *
 * @brief This file contains abstracted driver functions for temperature sensor
 *
 * @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.
 */

#include "exo_ahw_al_temp_sensor_common.h"
#include "exo_ahw_al_temp_sensor_wrapper.h"
#include "exo_ah_iob_csn.h"
#include "exo_ahdlsd.h"
#include <stdlib.h>
#include <stdio.h>
#include "logger.h"
#include "err_handler.h"

/**
 * @brief  API to initialize the temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_init(ahw_al_temp_sensor_hdl* ht_arg, ahw_inst_id_t ahw_id)
{
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL TEMPERATURE SENSOR Initialize"));
#endif
    hal_ret_sts sts;
    ht_arg->ahw_gen_info.ahw_inst_id =ahw_id;
    if(HAL_SCS ==ahw_al_ts_common_init(ht_arg))
    {
        ahal_set_hdle(ahw_id,ht_arg);
        sts = HAL_SCS;
    	log_id(LOG_INFO_L1,LOG_AHW,AHW_AL_TS_INIT_SCS);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ACTIVATED))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
        if(HAL_SCS != ahiobcsn_updt_ahste(&ht_arg->ahw_gen_info, AH_FREE_STATE))
        {
            sts = HAL_AHIOBCSN_FW_ERR;
        }
    }
    else
    {
        sts = HAL_AH_INIT_ERR;
    	log_id(LOG_ERROR,LOG_AHW,AHW_AL_TS_INIT_ERR);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ERROR))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
    }
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL TEMPERATURE SENSOR  Initialization completed successfully"));
#endif
    return sts;
}

/**
 * @brief  API to de-Initialize the temperature sensor
 */
hal_ret_sts  ahw_al_temp_sensor_deinit(ahw_al_temp_sensor_hdl* ht_arg, ahw_inst_id_t ahw_id)
{
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL TEMPERATURE SENSOR de-initialization"));
#endif
    hal_ret_sts sts;
    ahal_get_hdle(ahw_id);
    if(HAL_SCS == ahw_al_ts_common_deinit(ht_arg))
    {
        sts = HAL_SCS;
    	log_id(LOG_INFO_L1,LOG_AHW,AHW_AL_TS_DEINIT_SCS);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_DEACTIVATED))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
    }
    else
    {
        sts = HAL_AH_INIT_ERR;
    	log_id(LOG_ERROR,LOG_AHW,AHW_AL_TS_DEINIT_ERR);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ERROR))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
    }
#ifdef LINUX_TEMP_PORT
DEBUG_CPRINT(("\n EXO AH-AL TEMPERATURE SENSOR de-initialization completed successfully"));
#endif
return sts;
}

/**
 * @brief  API to get the event status of the temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_get_evnt_sts(uint32_t ahw_id, uint8_t* evnt_sts)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_get_evnt_sts(ahw_al_hts,evnt_sts))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_GET_EVNT_STS_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief API to configure temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_config(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_config(ahw_al_hts))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_CONFIG_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief API to set interrupt clear bit in shutdown mode
 */
hal_ret_sts ahw_al_temp_sensor_intrupt_clear(uint32_t ahw_id, uint8_t interrupt_clear)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_intrupt_clear(ahw_al_hts,interrupt_clear))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_INTRUPT_CLEAR_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief API to set event output mode of temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_set_event_output_mode(uint32_t ahw_id,temp_sensor_event_output_mode_t event_mode)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS ==(hal_ret_sts) ahw_al_ts_common_set_event_output_mode(ahw_al_hts,event_mode))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_SET_EVENT_OUTPUT_MODE_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief  API to set event polarity of temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_set_event_polarity(uint32_t ahw_id,temp_sensor_event_polarity_t polarity)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS ==(hal_ret_sts) ahw_al_ts_common_set_event_polarity(ahw_al_hts,polarity))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_SET_EVENT_POLARITY_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}
/*
 * @brief  API to set temperature high threshold
 */
hal_ret_sts ahw_al_temp_sensor_set_temp_high_threshold(uint32_t ahw_id, float high_temp_threshold)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_set_temp_high_threshold(ahw_al_hts,high_temp_threshold))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_SET_TEMP_HIGH_THRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief  API to set temperature low threshold
 */
hal_ret_sts ahw_al_temp_sensor_set_temp_low_threshold(uint32_t ahw_id, float low_temp_threshold)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_set_temp_low_threshold(ahw_al_hts,low_temp_threshold))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_SET_TEMP_LOW_THRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief API to set temperature critical threshold
 */
hal_ret_sts ahw_al_temp_sensor_set_temp_critical_threshold(uint32_t ahw_id, float critical_temp_threshold)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS ==(hal_ret_sts) ahw_al_ts_common_set_temp_critical_threshold(ahw_al_hts,critical_temp_threshold))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_SET_TEMP_CRITICAL_THRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}
/*
 * @brief API to set the resolution of temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_set_resolution(uint32_t ahw_id, temp_sensor_res_t resolution)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS ==(hal_ret_sts) ahw_al_ts_common_set_resolution(ahw_al_hts,resolution))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_SET_RESOLUTION_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief API to get resolution of temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_get_resolution(uint32_t ahw_id,temp_sensor_res_t *res)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_get_resolution(ahw_al_hts,res))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_GET_RESOLUTION_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief  API to reads the current temperature using Temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_gettemperature(uint32_t ahw_id,float *temperature)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_FAULT_ERROR_HANDLER;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == (hal_ret_sts)ahw_al_ts_common_gettemperature(ahw_al_hts,temperature))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_GETTEMPERATURE_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/*
 * @brief API to get temperature in fahrenheit using temperature sensor
 */
hal_ret_sts ahw_al_temp_sensor_get_fahrenheit(uint32_t ahw_id, float* temperature)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_ts_common_get_fahrenheit(ahw_al_hts,temperature))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);

            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_GET_FAHRENHEIT_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}
/*
 * @brief API to read Sensor Test ID and return 16-bit hex value
 * here it returns the hal_ret_sts for communicating wrapper common layer.
 */
hal_ret_sts ahw_al_temp_sensor_self_test (uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_ts_common_self_test(ahw_al_hts))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
                   }
        else
        {
           // sts = HAL_AH_DRIVER_ERR;
        	 sts = 1;
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    return sts;
}
/*
 * @brief API to read Manufacture ID and return 16-bit hex value representing the temperature data from the MCP9843 register,
 * here it returns the hal_ret_sts for communicating wrapper common layer.
 */
/*
 * @brief API to read Manufacture ID and return 16-bit hex value representing the temperature data from the MCP9843 register,
 * here it returns the hal_ret_sts for communicating wrapper common layer.
 */
hal_ret_sts ahw_al_temp_sensor_read_manufacture_id(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_temp_sensor_hdl *ahw_al_hts = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&ahw_al_hts->ahw_gen_info);
    if(HAL_SCS == sts)
    {
        ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_ts_common_read_manufacture_id(ahw_al_hts))
        {
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_TS_READ_MANUFACTURE_ID_ERR_INST_ID__eq__u32_,ahw_id);
            ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
        }
    }
    else
    {
    }
    return sts;
}

