/*
 * exo_ahw_al_spi_to_uart_common.c
 *
 *  Created on: May 15, 2023
 *      Author: Invcuser_63
 */

#include "exo_ahw_al_spi_to_uart_common.h"
#include <stdio.h>
#include "exo_ahw_al_uart_conv.h"
#include "exo_ah_iob_csn.h"
#include "exo_ahdlsd.h"
#include <stdlib.h>
#include "logger.h"
#include "exo_ahw_vdp_uart_conv_max14830.h"

extern void* ahw_inst_hdle_ptr[MAX_AH_INST_ID];

/*
 * @brief This API initialize the control block memory and do basic configurations
 */
hal_ret_sts ahw_al_uart_conv_init(s_ahw_max14830_hdle *hdle,ahw_inst_id_t ahw_id)
{
#ifdef LINUX_TEMP_PORT
	DEBUG_CPRINT(("\n EXO AH-AL UART convertor Initialise"));
#endif
	hal_ret_sts sts;
    ahal_set_hdle(ahw_id, hdle);
	hdle->ahw_gen_info.ahw_inst_id = ahw_id;
		if(HAL_SCS == ahw_al_uart_conv_common_init(hdle))
		{
			ahw_inst_hdle_ptr[ahw_id] = hdle;
			sts = HAL_SCS;
	    	log_id(LOG_INFO_L1,LOG_AHW,AHW_AL_UART_CONV_INIT_SCS);
			if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ACTIVATED))
			{
				sts = HAL_AHDLSD_FW_ERR;
			}
			if(HAL_SCS != ahiobcsn_updt_ahste(&hdle->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_UART_CONV_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 UART convertor Initialization completed successfully"));
	#endif

	 return sts;

}

/**
 * @brief This API to initialize the port configurations
 */
hal_ret_sts ahw_al_uart_conv_port_init(uint32_t ahw_id,s_ahw_max14830_init_cfg *init_cfg)
{
	hal_ret_sts sts;
	s_ahw_max14830_hdle *hdle = ahal_get_hdle(ahw_id);
	sts = ahiobcsn_check_ste(&hdle->ahw_gen_info);
	if(HAL_SCS == sts)
	{
		ahiobcsn_updt_ahste(&hdle->ahw_gen_info, AH_BUSY_STATE);
		if(HAL_SCS == (hal_ret_sts)ahw_al_uart_conv_common_init_intf(hdle,init_cfg))
		{
			ahiobcsn_updt_ahste(&hdle->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_UART_CONV_PORT_INIT_ERR_INST_ID__eq__u8_,UART_EXPANDER_MAX14830_OBC);
			ahiobcsn_updt_ahste(&hdle->ahw_gen_info, AH_FREE_STATE);
		}
	}
	else
	{
	}
	return sts;
}

/**
 * @brief This API to transmit the data
 */
hal_ret_sts ahw_al_uart_conv_transmit(uint32_t ahw_id,uint8_t port,uint8_t* tx_buff,uint16_t tx_len )
{
	hal_ret_sts sts;
	s_ahw_max14830_hdle *hdle = ahal_get_hdle(ahw_id);
	sts = ahiobcsn_check_ste(&hdle->ahw_gen_info);
	if(HAL_SCS == sts)
	{
		ahiobcsn_updt_ahste(&hdle->ahw_gen_info, AH_BUSY_STATE);
		if(HAL_SCS == (hal_ret_sts)ahw_al_uart_conv_common_transmit(hdle,port,tx_buff,tx_len))
		{
			ahiobcsn_updt_ahste(&hdle->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_UART_CONV_TRANSMIT_ERR_INST_ID__eq__u8_,UART_EXPANDER_MAX14830_OBC);
			ahiobcsn_updt_ahste(&hdle->ahw_gen_info, AH_FREE_STATE);
		}
	}
	else
	{
	}
	return sts;
}

#if 1
/**
 * @brief This API to receive the data
 */
hal_ret_sts ahw_al_uart_conv_receive(uint32_t ahw_id,uint8_t port,uint16_t rx_len, rx_cb_fptr_t callback_func,uint8_t* recieve_ptr)
{
	hal_ret_sts sts;
	s_ahw_max14830_hdle *hdle = ahal_get_hdle(ahw_id);
	switch(port)
	{
	case 0x00:
			hdle->rcv0_ptr = recieve_ptr;
			break;
	case 0x20:
			hdle->rcv1_ptr = recieve_ptr;
			break;
	case 0x40:
			hdle->rcv2_ptr = recieve_ptr;
			break;
	case 0x60:
			hdle->rcv3_ptr = recieve_ptr;
			break;
	}

	sts = ahiobcsn_check_ste(&hdle->ahw_gen_info);
	if(HAL_SCS == sts)
	{
		ahiobcsn_updt_ahste(&hdle->ahw_gen_info, AH_BUSY_STATE);
		if(HAL_SCS == (hal_ret_sts)ahw_al_uart_conv_common_receive(hdle,port,rx_len,callback_func))
		{
			ahiobcsn_updt_ahste(&hdle->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_UART_CONV_RECEIVE_ERR_INST_ID__eq__u8_,UART_EXPANDER_MAX14830_OBC);
			ahiobcsn_updt_ahste(&hdle->ahw_gen_info, AH_FREE_STATE);
		}
	}
	else
	{
	}
	return sts;
}

hal_ret_sts ahw_al_uart_conv_self_test(uint32_t ahw_id)
{
	hal_ret_sts sts;
	s_ahw_max14830_hdle *hdle = ahal_get_hdle(ahw_id);
	if(HAL_SCS == (hal_ret_sts)ahw_vdp_uart_conv_self_test(hdle))
	{
		sts = HAL_SCS;
	}
	else
	{
		sts = HAL_AH_DRIVER_ERR;
	}
	return sts;
}
#endif
