/**
 * @file api_req_resp_lib_common.c
 *
 * @brief This file contain functionalities that are common to tele command
 *        tele data and telemetry.
 *
 * @copyright Copyright 2023 Antaris, Inc.
 */
/*********************************************************************/
#include "api_req_resp_lib_common.h"
#include "exo_io_al_sos_timer.h"
#include "api_req_common.h"
#include "api_resp_common.h"
#include <stdio.h>
#include "exo_common.h"

/**
 * @brief api that returns epoch utc time
 */
time_t adcs_get_utc_epoch(void)
{
    time_t seconds;

#ifdef LINUX_TEMP_PORT
    seconds = time(NULL);

#else
    seconds = get_crnt_time_date_in_epoch();

#endif
    return seconds;
}

/**
 * @brief This API encode the request data into basilisk api request frame.
 */
int32_t api_req_encode(uint8_t tc_id,uint8_t* tx_buffer,uint16_t tx_size,
        uint8_t *buffer)
{
    s_api_req_frame adcs_api_req_orig = {0};
    int32_t frame_size = 0;
    api_req_orig_init(&adcs_api_req_orig);
    api_req_orig_cfg_sof(&adcs_api_req_orig,API_REQ_RESP_LIB_SOF1,
            API_REQ_RESP_LIB_SOF2);
    api_req_orig_cfg_api_req_ctrl(&adcs_api_req_orig,API_REQ_RESP_CRTL_CMD);
    api_req_orig_cfg_api_req_id(&adcs_api_req_orig,tc_id);
    api_req_orig_cfg_api_req_len(&adcs_api_req_orig,tx_size);
    api_req_orig_cfg_api_req_data(&adcs_api_req_orig,tx_buffer);
    api_req_orig_cfg_eof(&adcs_api_req_orig,API_REQ_RESP_LIB_EOF);
    frame_size = api_req_pack(buffer,&adcs_api_req_orig);
    if(0 > frame_size)
    {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
        DEBUG_CPRINT(("API REQ Pack Failed Error:- %ld\n",frame_size));
#pragma GCC diagnostic pop
    }
    return frame_size;
}

/**
 * @brief This API encode the response data into Basilisk response
 * frame format.
 */
int32_t api_resp_encode(uint8_t tlm_id,uint8_t* tlm_buffer,uint16_t tlm_size,
        uint8_t *buffer)
{
    s_api_resp_frame adcs_api_resp_orig = {0};
    int32_t frame_size = 0;
    api_resp_orig_init(&adcs_api_resp_orig);
    api_resp_orig_cfg_sof(&adcs_api_resp_orig,API_REQ_RESP_LIB_SOF1,
                                    API_REQ_RESP_LIB_SOF2);
    api_resp_orig_cfg_api_resp_ctrl(&adcs_api_resp_orig,API_REQ_RESP_CRTL_RESP);
    api_resp_orig_cfg_api_resp_id(&adcs_api_resp_orig,tlm_id);
    api_resp_orig_cfg_api_resp_len(&adcs_api_resp_orig,tlm_size);
    api_resp_orig_cfg_api_resp_data(&adcs_api_resp_orig,tlm_buffer);
    api_resp_orig_cfg_eof(&adcs_api_resp_orig,API_REQ_RESP_LIB_EOF);
    frame_size = api_resp_pack(buffer,&adcs_api_resp_orig);
    if(0 > frame_size)
    {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
        DEBUG_CPRINT(("API RESP Pack Failed Error:- %ld\n",frame_size));
#pragma GCC diagnostic pop
    }
    return frame_size;
}

/**
 * @brief This API decode the request data into payload and request id.
 */
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)
{
    s_api_req_frame adcs_api_req_term = {0};
    int32_t frame_size = 0;
    api_req_orig_init(&adcs_api_req_term);
    api_req_orig_cfg_api_req_data(&adcs_api_req_term,tx_buffer);
    frame_size = api_req_unpack(rx_data,rx_size,&adcs_api_req_term);
    if(frame_size < 0)
    {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
        DEBUG_CPRINT(("API REQ Unpack Failed Error %ld\n",frame_size));
#pragma GCC diagnostic pop
    }
    else
    {
        api_req_term_extract_api_req_id(&adcs_api_req_term,req_id);
        api_req_term_extract_api_req_len(&adcs_api_req_term,req_len);
    }
    return frame_size;
}

/**
 * @brief This API decode the response data.
 */
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)
{
    s_api_resp_frame adcs_api_resp_term = {0};
    int32_t frame_size = 0;
    api_resp_orig_init(&adcs_api_resp_term);
    api_resp_orig_cfg_api_resp_data(&adcs_api_resp_term,tlm_buffer);
    frame_size = api_resp_unpack(rx_data,rx_size,&adcs_api_resp_term);
    if(frame_size < 0)
    {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
        DEBUG_CPRINT(("API RESP Unpack Failed Error %ld\n",frame_size));
#pragma GCC diagnostic pop
    }
    else
    {
        api_resp_term_extract_api_resp_id(&adcs_api_resp_term,resp_id);
        api_resp_term_extract_api_resp_len(&adcs_api_resp_term,resp_len);
    }
    return frame_size;
}


