#include <stdlib.h>
#include "exo_ah_iob_csn.h"
#include "exo_ahdlsd.h"
#include "logger.h"
#include "exo_common.h"
#include "err_handler.h"
#include "exo_ahw_al_common.h"
#include "exo_ahw_al_data_logger_wrapper.h"
#include "exo_ahw_vdp_data_logger_ad7291.h"
#include "ad7291.h"
#include "exo_ahw_al_data_logger_common.h"

/*
 * @brief This API to init the data logger 
 */
 hal_ret_sts ahw_al_data_logger_init(ahw_al_ad7291_hdl* ht_arg, ahw_inst_id_t ahw_id)
{
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL DATA LOGGER Initialize"));
#endif
    hal_ret_sts sts;
    ht_arg->ahw_gen_info.ahw_inst_id =ahw_id;
    ahal_set_hdle(ahw_id,ht_arg);
    if(HAL_SCS ==ahw_al_dl_common_init(ht_arg))
    {
        sts = HAL_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 DATA LOGGER  Initialization completed successfully"));
#endif
    return sts;
}

/*
 * @brief This API to get data logger voltage 
 */
hal_ret_sts ahw_al_data_logger_getvoltage_convertion(ahw_inst_id_t ahw_id, adc_channel_id chnl,float *data)
{
	hal_ret_sts sts;
	ahw_al_ad7291_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_dl_common_get_voltage_convertion(ahw_al_hts,chnl,data))
		{
			ahiobcsn_updt_ahste(&ahw_al_hts->ahw_gen_info, AH_FREE_STATE);
			sts = HAL_SCS;
		}
		else
		{
			sts = HAL_AH_DRIVER_ERR;
		}
	}
	return sts;
}

/*
 * @brief This API to self test the data logger
 */
hal_ret_sts ahw_al_data_logger_self_test(uint32_t ahw_id)
{
	hal_ret_sts sts;
        ahw_al_ad7291_hdl* hdle = ahal_get_hdle(ahw_id);
	if(HAL_SCS == (hal_ret_sts)ahw_vdp_data_logger_self_test(hdle))
	{
		sts = HAL_SCS;
	}
	else
	{
		//sts = HAL_AH_DRIVER_ERR;
		 sts = 1;
	}
	return sts;
}
