/**
 * @file api_req_resp_lib_common.h
 *
 * @brief This file contains function prototypes and types that is common for
 *        all tc tm and td modules
 *
 * @copyright Copyright 2023 Antaris, Inc.
 */

#ifndef API_REQ_RESP_LIB_COMMON_H_
#define API_REQ_RESP_LIB_COMMON_H_

#include <stdint.h>
#include <time.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

#define API_REQ_RESP_LIB_OK               (0)  ///< Success return value
#define API_REQ_RESP_LIB_NULL_PTR         (-1) ///< Null pointer return value
#define API_REQ_RESP_LIB_CRC_ERROR        (-2) ///< CRC value mismatch
#define API_REQ_RESP_LIB_SOF_EOF_ERROR    (-6) ///< Frame identifier error
#define API_REQ_RESP_LIB_EOF_ERROR        (-7) ///< EOF identifier error
#define API_REQ_RESP_MIN_LEN_ERROR        (-8) ///< TC/TM len less than min len

/// Start of Frame and End of Frame definitions
#define API_REQ_RESP_LIB_SOF1           (0x1f) ///< Start of Frame Identifier 1
#define API_REQ_RESP_LIB_SOF2           (0x2f) ///< Start of Frame Identifier 2
#define API_REQ_RESP_LIB_EOF            (0x7f) ///< End of Frame Identifier

#define API_REQ_RESP_CRTL_RESP (0x00)  ///< Api request response control
#define API_REQ_RESP_CRTL_CMD  (0x01)  ///< Api request response command
#define API_REQ_RESP_SYNC_BIT  (0x02)  ///< Api request response sync bit

/**
 * @brief This API returns utc epoch time.
 *
 * @param[in] : void
 *
 * @return epoch time
 * @retval epoch time
 */
time_t adcs_get_utc_epoch(void);

/**
 * @brief This API encode the request data.
 *
 * @param[in] tc_id : Telecommand ID
 * @param[in] tx_buffer : pointer to transmit buffer
 * @param[in] tx_size : size of transmit
 * @param[out] buffer : pointer to encoded buffer
 *
 * @return size of data stream
 * @retval Error value or packed frame size.
 */
int32_t api_req_encode(uint8_t tc_id,uint8_t *tx_buffer,uint16_t tx_size,
        uint8_t *buffer);

/**
 * @brief This API encode the response data.
 *
 * @param[in] tlm_id : Telemetry ID
 * @param[in] tlm_buffer : pointer to telemetry buffer
 * @param[in] tlm_size : size of telemetry buffer
 * @param[out] buffer : pointer to encoded buffer
 *
 * @return size of data stream
 * @retval Error value or packed frame size.
 */
int32_t api_resp_encode(uint8_t tlm_id,uint8_t *tlm_buffer,uint16_t tlm_size,
        uint8_t *buffer);

/**
 * @brief This API decode the request data.
 *
 * @param[in] rx_data : pointer to receive data.
 * @param[in] rx_size : size of receive data.
 * @param[out] tx_buffer : pointer to transmit buffer
 * @param[out] req_id : Pointer to Api_Req ID
 * @param[out] req_len : Pointer to Api_Req len
 *
 * @return size of data stream
 * @retval Error value or packed frame size.
 */
int32_t api_req_decode(uint8_t *rx_data,uint16_t rx_size,uint8_t *tx_buffer,
        uint8_t *req_id,uint16_t *req_len);

/**
 * @brief This API decode the response data.
 *
 * @param[in] rx_data : pointer to receive data.
 * @param[in] rx_size : size of receive data.
 * @param[out] tlm_buffer : pointer to telemetry buffer
 * @param[out] resp_id : Pointer to Api_Resp ID
 * @param[out] resp_len : Pointer to Api_Resp len
 *
 * @return size of data stream
 * @retval Error value or packed frame size.
 */
int32_t api_resp_decode(uint8_t *rx_data,uint16_t rx_size,uint8_t *tlm_buffer,
        uint8_t *resp_id,uint16_t *resp_len);

#ifdef __cplusplus
}
#endif
#endif /* API_REQ_RESP_LIB_COMMON_H_ */
