/**
 * @file adcs_comm_interface.h
 *
 * @brief This file contains communication interface abstracted functions
 *          prototypes.
 *
 * @copyright Copyright 2023 Antaris, Inc.
 */

#ifndef ADCS_COMM_INT_H_
#define ADCS_COMM_INT_H_

#include "exo_types.h"
#include "exo_osal_common.h"

#ifdef __cplusplus
extern "C" {
#endif

#if defined(LINUX_TEMP_PORT) || defined(ADCS_HW_SIM)
/**
 * @brief Function to send telecommand to ADCS through socket interface.
 *
 * @param[in] tc_id : Telecommand ID
 * @param[in] tc_Buffer : pointer to Telecommand buffer
 * @param[in] tx_size : Tansmit buffer size.
 *
 * @return status of telecommand
 * @retval 0-> Telecommand success, 1-> Telecommand fail
 */
int8 adcs_sendtc_sock(uint8 tc_id, uint8 *tx_buffer, uint16 tx_size);

/**
 * @brief ADCS socket init interface api.
 *
 * @return status of socket connection
 * @retval 1-> success, -1-> connection failed or
 *         connection error.
 */
int adcs_sock_init(void);
#endif

/**
 * @brief Function to send telecommand to ADCS
 *
 * @param[in] tcId : Telecommand id
 * @param[in] msgBuffer : Pointer to data that needs to be tranmsitted
 * @param[in] len : Length of data that needs to be tranmsitted
 *
 * @return Return value of the interface send api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_send_cmd(uint8 tcId, uint8* msgBuffer, uint16 len);

/**
 * @brief Function to request telemetry from ADCS
 *
 * @param[in] tlmId : Telemetry id
 * @param[out] msgBuffer : Pointer to data that is received from ADCS
 * @param[in] len : Length of data that needs to be received
 *
 * @return Return value of the interface send/receive api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_requesttlm(uint8 tlmId, uint8* msgBuffer, uint16 len);

/**
 * @brief Function to request download burst from ADCS
 *
 * @param[in] msgBuffer : pointer to TX Message buffer
 * @param[out] rx_buffer : pointer to RX buffer
 * @param[in] tx_size : Transmit buffer size
 * @param[out] rx_size : Receive buffer size
 *
 * @return status of the download burst
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_downldburst(uint8 *msgBuffer, uint8 *rx_buffer, uint16 tx_size,
                          uint16 *rx_size);

/**
 * @brief Function to request telemetry from ADCS through CAN interface
 *
 * @param[in] tlmId : Telemetry id
 * @param[out] msgBuffer : Pointer to data that is received from ADCS
 * @param[in] len : Length of data that needs to be received
 *
 * @return Return value of the interface send/receive api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_requesttlm_can(uint8 tlmId, uint8* msgBuffer, uint16 len);

/**
 * @brief Function to request telemetry from ADCS through UART interface
 *
 * @param[in] tlmId : Telemetry id
 * @param[out] msgBuffer : Pointer to data that is received from ADCS
 * @param[in] len : Length of data that needs to be received
 *
 * @return Return value of the interface send/receive api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_requesttlm_uart(uint8 tlmId, uint8* msgBuffer, uint16 len);

/**
 * @brief Function to send telecommand to ADCS through CAN interface
 *
 * @param[in] tcId : Telecommand id
 * @param[in] msgBuffer : Pointer to data that needs to be tranmsitted
 * @param[in] len : Length of data that needs to be tranmsitted
 *
 * @return Return value of the interface send api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_sendtc_can(uint8 tcId, uint8* msgBuffer, uint16 len);

/**
 * @brief Function to send telecommand to ADCS through UART interface
 *
 * @param[in] tcId : Telecommand id
 * @param[in] msgBuffer : Pointer to data that needs to be tranmsitted
 * @param[in] len : Length of data that needs to be tranmsitted
 *
 * @return Return value of the interface send api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_sendtc_uart(uint8 tcId, uint8* msgBuffer, uint16 len);
/**
 * @brief ADCS CAN interface queue init api
 *
 * @return status of os_queue_create
 * @retval 1 -> Success
 *         Other values -> Error
 */
int8 adcs_can_init(void);

/**
 * @brief ADCS UART interface init api
 *
 * @return status of os_sem_create
 * @retval 1 -> Success
 *         Other values -> Error
 */
int8 adcs_uart_init(void);

/**
 * @brief API to get the adcs queue handler
 *
 * @param[in] hdlr : Pointer to os_queue_handle_ptr
 *
 * @return Always zero
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_get_can_queue_hdlr(os_queue_handle_ptr *hdlr);
/**
 * @brief This API to set the filedownload parameters
 *
 * @param[in] file_size : Total size of the file
 * @param[in] tlm_blk_size : size of telemetry block
 *
 * @return status of the set operation
 * @retval 0-> success
 *
 */
int8 adcs_set_fdwnld_param(uint16 file_size, uint16 tlm_blk_size);
/**
 * @brief This API downloads data in burst through CAN interface
 *
 * @param[in] msgBuffer : pointer to TX Message buffer
 * @param[out] rx_buffer : pointer to RX buffer
 * @param[in] tx_size : Transmit buffer size
 * @param[out] rx_size : Receive buffer size
 *
 * @return status of the download burst
 * @retval status -1-> unexpected response/status 0-> success
 *
 */
int8 adcs_downldburst_can(uint8* msgBuffer, uint8* rx_buffer, uint16 tx_size,
        uint16* rx_size);

/**
 * @brief This API encode the uart telemtry data.
 *
 * @param[in] tlmId : Identity of Telemtry
 * @param[in] buff  : pointer to telemtry data buffer
 */
void adcs_encode_uart_tlm(uint8_t tlmId, uint8_t* buff);

/**
 * @brief This API encode the uart telecommand data.
 *
 * @param[out] buff  : pointer to telecommand data buffer
 * @param[in] tcId   : Identity of  telecommand
 * @param[in] msgBuffer : pointer to telecommand message buffer
 * @param[in] len : length of message buffer
 */
void adcs_encode_uart_tc(uint8_t* buff, uint8 tcId, uint8_t* msgBuffer,\
        uint16_t len);

/*
 * @brief This API verify the download block status and update the block len
 *
 * @param[out] rx_size  : download block size
 * @return status of the download block ready status
 * @retval status -1-> unexpected response/status 0-> success
 */
int8 adcs_get_download_block_status(uint16_t *rx_size);

/**
 * @brief This API downloads data in burst through CAN interface
 *
 * @param[in] msgBuffer : pointer to TX Message buffer
 * @param[out] rx_buffer : pointer to RX buffer
 * @param[in] tx_size : Transmit buffer size
 * @param[out] rx_size : Receive buffer size
 *
 * @return status of the download burst
 * @retval status -1-> unexpected response/status 0-> success
 *
 */
int8 adcs_downldburst_uart(uint8* msgBuffer, uint8* rx_buffer, uint16 tx_size,
        uint16* rx_size);

/**
 * @brief Function to send telecommand to ADCS through UART interface
 * without acknowledgment
 *
 * @param[in] tcId      : Telecommand id
 * @param[in] msgBuffer : Pointer to data that needs to be transmitted
 * @param[in] len       : Length of data that needs to be transmitted
 *
 * @return Return value of the interface send api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_sendtc_uart_nack(uint8 tcId, uint8 *msgBuffer, uint16 len);

/**
 * @brief Function to configure the primary interface of ADCS
 *
 * @param[in] adcs_interface    : ADCS Primary interface
 *
 * @return Return value of the interface send api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_interface_cfg(uint8 adcs_interface);

/**
 * @brief Function to return the primary interface of ADCS
 *
 * @return Return ADCS primary interface
 * @retval 0 -> ADCS_UART
 *         2 -> ADCS_CAN
 */
int8 get_adcs_interface(void);

/**
 * @brief Function to decode escape character in TM
 *
 * @param[in] data      : TM paylaod data
 * @param[in] len       : TM payload data length
 */
void adcs_esc_char_dec(uint8_t* data, uint16_t len);

/**
 * @brief ADCS interface init api
 *
 * @return status of os_sem_create
 * @retval 1 -> Success
 *         Other values -> Error
 */
int8 adcs_intf_init(void);

/**
 * @brief Function to request Telemetry with payload data from ADCS
 *
 * @param[in] tc_data : Telecommand data with Telemetry id
 * @param[in] tc_len  : Telecommand length
 * @param[out] msgBuffer : Pointer to data that is received from ADCS
 * @param[in] len : Length of data that needs to be received
 *
 * @return Return value of the interface send/receive api
 * @retval 0 -> Success
 *         Other values -> Error
 */
int8 adcs_requesttlm_wt_data(uint8* tc_data, uint16 tc_len,\
       uint8* msgBuffer, uint16 len);

#ifdef __cplusplus
}
#endif
#endif /* ADCS_COMM_INT_H_ */
