/**
 * @file adcs_bsk_sim.h
 *
 * @brief This file contains abstraction functions, structures, enum for
 *          ADCS simulation.
 *
 * @copyright Copyright 2023 Antaris, Inc.
 */

#ifndef ADCS_BSK_SIM_H_
#define ADCS_BSK_SIM_H_

#include "exo_types.h"
#include "stdint.h"
#include "adcs_va.h"

#ifdef __cplusplus
extern "C" {
#endif

/// TC TM ID
typedef enum
{
    API_REQ_RESET = 1,          ///< Reset command
    API_REQ_UTC,                ///< UTC Set command
    API_REQ_CLEAR_ERROR,        ///< Clear Error
    API_REQ_SAVE_ORBIT,         ///< Save orbit parameters
    API_REQ_TLE,                ///< TLE Set command
    API_REQ_MOI,                ///< Set moment of intertia
    API_REQ_PWR,                ///< Set Power configuration
    API_REQ_MAGMTR_MODE_OPER,   ///< Set magnetometer operation mode
    API_REQ_GYRO_CFG,           ///< Set gyro configuration
    API_REQ_CSS_CFG,            ///< Set coarse sun sensor configuration
    API_REQ_STAR_TRAC_CFG,      ///< Set star tracker configuration
    API_REQ_MAGMTR_CFG,         ///< Set Magnetometer configuration
    API_REQ_REDUN_MAGMTR_CFG,   ///< Set Redudent magnetometer
                                ///< configuration
    API_REQ_MAG_BOOM,           ///< Set Magnetometer boom
    API_REQ_MGNTRQR_OUT,        ///< Set Magnetorquer output
    API_REQ_RW_SPEED,           ///< Set Reaction wheel speed
    API_REQ_MGNTRQR_CFG,        ///< Set Magnetorquer configuration
    API_REQ_RW_CFG,             ///< Set Reaction wheel configuration
    API_REQ_CTRL_MODE,          ///< Set Control mode configuration
    API_REQ_DETUMBLE_CTRL,      ///< Set Detumble control parameters
    API_REQ_Y_RW_CTRL,          ///< Set Y axis Reaction wheel control
                                ///< parameter
    API_REQ_Z_RW_CTRL,          ///< Set Z axis Reaction wheel control
                                ///< parameter
    API_REQ_RW_CTRL,            ///< Set Reaction wheel control
    API_REQ_TRCK_CTRL_GAIN,     ///< Set tracking control gain
    API_REQ_ATTITUDE_ANGL,      ///< Set attitude angle parameters
    API_REQ_TRCK_TARGET,        ///< Set target tracking parameters
    API_REQ_INRTL_REF,          ///< Set inertial reference
    API_REQ_EST_MODE,           ///< Set estimation mode
    API_REQ_EST_PARAM,          ///< Set estimation parameters
    API_REQ_SENSORS_CFG,        ///< Set Sensor configuration
    API_REQ_ACTUATORS_CFG,      ///< Set Acruator configuration
    API_REQ_FSS_CFG,            ///< Set Fine sun sensor configuration
    API_REQ_NADIR_CFG,          ///< Set Nadir configuration
    API_REQ_THRUSTER_CMD,       ///< Set thruster command configuration
    API_RESP_ID = 128,          ///< Get Identification
    API_RESP_STATE,             ///< Get ADCS state
    API_RESP_TLE,               ///< Get TLE
    API_RESP_MOI,               ///< Get moment of inetria
    API_RESP_PWR,               ///< Get power configuration
    API_RESP_MAG_FIELD_VEC,     ///< Get magnetic field vector
    API_RESP_CSS_VEC,           ///< Get CSS vector
    API_RESP_FSS_VEC,           ///< Get FSS vector
    API_RESP_NADIR_VEC,         ///< Get nadir vector
    API_RESP_SENSOR_RATE,       ///< Get sensor rate
    API_RESP_STAR_TRKR_BVEC,    ///< Get star tracker body vector
    API_RESP_STAR_TRKR_OVEC,    ///< Get star trackere orbit vector
    API_RESP_RAW_NADIR_SEN,     ///< Get raw nadir sensor meaurement
    API_RESP_RAW_FSS_SEN,       ///< Get raw fine sun sensor measurement
    API_RESP_RAW_MAGMTR,        ///< Get raw magnetometer measurement
    API_RESP_RAW_GPS_STS,       ///< Get raw GPS staus
    API_RESP_RAW_GPS_TIME,      ///< Get raw GPS time
    API_RESP_RAW_GPS_X,         ///< Get raw GPS X axis
    API_RESP_RAW_GPS_Y,         ///< Get raw GPS Y axis
    API_RESP_RAW_GPS_Z,         ///< Get raw GPS Z axis
    API_RESP_STAR_MAG,          ///< Get star magnitude
    API_RESP_STAR_PERFORM,      ///< Get star performance
    API_RESP_STAR_TIMING,       ///< Get star timing
    API_RESP_RAW_SENS,          ///< Get raw sensor measurement
    API_RESP_RAW_GPS,           ///< Get raw GPS measurement
    API_RESP_RAW_STAR_TRAC,     ///< Get raw star tracker measurement
    API_RESP_STAR_RDATA,        ///< Get raw star tracker data
    API_RESP_RAW_SEC_MAGMTR,    ///< Get raw secondary magnetometer
                                ///< measurement
    API_RESP_RAW_SENS_RATE,     ///< Get raw sensor rates
    API_RESP_STAR_EST_RATE,     ///< Get star tracker estimation rate
    API_RESP_SENS_CUR,          ///< Get sensor current
    API_RESP_CON_CUR,           ///< Get adcs consumed current measurement
    API_RESP_RW_CUR,            ///< Get reaction wheel current
    API_RESP_TEMP,              ///< Get adcs temperature
    API_RESP_SENS_RATE_TEMP,    ///< Get rate sensor temperature
    API_RESP_POW_TEMP,          ///< Get power and temperature measurement
    API_RESP_GYRO_CFG,          ///< Get gyro configuration
    API_RESP_STAR_TRAC_CFG,     ///< Get star tracker configuration
    API_RESP_MAGMTR_CFG,        ///< Get magnetometer configuration
    API_RESP_CSS_CFG,           ///< Get CSS configuration
    API_RESP_FSS_CFG,           ///< Get FSS configuration
    API_RESP_NADIR_CFG,         ///< Get Nadir configuration
    API_RESP_RED_MAGMTR_CFG,    ///< Get Redudant magnetometer
                                ///< configuration
    API_RESP_MGNTRQR_CFG,       ///< Get magnetorquer configuration
    API_RESP_RW_CFG,            ///< Get reaction wheel configuration
    API_RESP_DETUMBLE_CTRL,     ///< Get detumble parameters
    API_RESP_Y_RW_CTRL,         ///< Get Y reaction wheel control
    API_RESP_RW_CTRL,           ///< Get reaction wheel control
    API_RESP_TRCK_CTRL_GAIN,    ///< Get tracking control gain
    API_RESP_ATTITUDE_ANGL,     ///< Get attitude angle
    API_RESP_TRCK_TARGET,       ///< Get target tracking parameters
    API_RESP_INRTL_REF,         ///< Get inertial reference
    API_RESP_EST_PARAM,         ///< Get estimation paremeters
    API_RESP_POS_ECI,           ///< Get sattelite position ECI
    API_RESP_VEL,               ///< Get sattelite velocity
    API_RESP_POS_LLH,           ///< Get sattelte position LLH
    API_RESP_EST_RATE,          ///< Get estimation rates
    API_RESP_EST_QUTRN,         ///< Get estimated quaternion
    API_RESP_ECEF_POS,          ///< Get ECEF sattelite position
    API_RESP_MODL_MAG,          ///< Get moduled magnetic field vector
    API_RESP_MODL_SUN,          ///< Get moduled sun vector
    API_RESP_EST_GYRO,          ///< Get estimated gyro measurement
    API_RESP_EST_INNOVN,        ///< Get estimation innovation
    API_RESP_QUTR_ERROR,        ///< Get quaternion error vector
    API_RESP_QUTR_COVR,         ///< Get quaternion covariance
    API_RESP_ANGL_COVR,         ///< Get angular rate covariance
    API_RESP_CTRL_MODE,         ///< Get Control mode
    API_RESP_RW_SPEED,          ///< Get Reaction wheel commanded speed
    API_RESP_RW_SPEED_MEASURE   ///< Get Reaction wheel measurement
}e_api_req_id;

/// API Response Id
typedef enum
{
    API_RESP_OK=1,                    ///< Responce ID OK
    API_RESP_ERROR                    ///< Responce Error
}e_api_resp_id;


/// API Error
typedef enum
{
    API_ERR_INVALID_REQ=1,            ///< Invalid Request Id received
    API_ERR_FRAME_ERR,                ///< Frame Error (SOF/EOF)
    API_ERR_CRC_ERR,                  ///< Checksum Error
    API_ERR_DATA_INVALID              ///< Invalid Data Error
}e_api_error;

/// BSK operation status
typedef enum
{
    API_SUCCESS = 0,      ///< BSK operation success
    API_FAIL = -1,        ///< BSK operation failure
    API_ID_ERROR = -2     ///< BSK ID error
}e_bsk_status;

/***************************API Reference**********************************/

/**
 * @brief Function to send telecommand to BSK through socket interface
 *
 * @param[in] tc_id : Telecommand ID
 * @param[in] tc_buffer : pointer to telecommand buffer
 * @param[in] tx_size : size of transmit
 *
 * @return status of socket send
 */
int8 adcs_sendtc_bsk(uint8 tc_id, uint8 *tx_buffer, uint16 tx_size);

/**
 * @brief Function to Request telemetry to BSK through socket interface
 *
 * @param[in] tlmId : Telecommand ID
 * @param[out] msgBuffer : pointer to telecommand buffer
 * @param[in] len : size of transmit
 *
 * @return status of socket receive
 */
int8 adcs_requesttlm_bsk(uint8 tlmId, uint8* msgBuffer, uint16 len);

/**
 * @brief Function to handle basilisk bypass command
 *
 * @param[in] tx_data    : pointer to the data to be transmitted to adcs.
 * @param[out] rx_data   : pointer to the data received from adcs.
 * @param[in] tx_size    : pointer to size of transmitted data.
 * @param[out] rx_size   : pointer to size of received data.
 *
 * @retval API_SUCCESS -> Success
 *         Other values -> Error
 */
e_adcs_status adcs_basilisk_bypass_mode(uint8 *tx_data, uint8 *rx_data,
        uint16 tx_size, uint16 *rx_size);

/**
 * @brief This function handle the socket timeout
 *
 * @param[in] adcs_sock_fd : Handle client or server
 *
 * @return void
 */
void socket_flush(int adcs_sock_fd);

#ifdef __cplusplus
}
#endif
#endif /* ADCS_BSK_SIM_H_ */
