/**
 * @file xband_ipc.c
 *
 * @brief This file has the functions related to IPC message encode/decode
 *        for IPCs to communicate with TC-TM manager, OBC and other SW layers
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/*********************************************************************/
#include "xband_ipc.h"
#include "exo_common.h"
#include "logger.h"

extern uint8_t PLACE_IN_EXT_RAM_MEM xband_radio_id;
extern uint16_t PLACE_IN_EXT_RAM_MEM xband_tc_seq_no;

/**
 * @brief This API for xband layer that sends message to
 * destination thread using os IPC functionalities
 */
os_status_t xband_send_ipc(uint16_t msg_id, uint8_t dst_id)
{
    os_status_t status = 0 ;

    os_itc_msg_handle_t xband_ipc;

    xband_ipc.src_entity= COMMS_XBAND_CTLR;

    xband_ipc.pld.pld_ptr = NULL;

    xband_ipc.Msg_id = msg_id;

    status = os_itc_msg_send(&xband_ipc, dst_id ,os_send_itc_wait);

    return status;
}

/**
 * @brief This API for xband layer that sends message payload
 *  to destination thread using os IPC functionalities
 */
os_status_t xband_send_ipc_pld(uint16_t msg_id, uint8_t dst_id, uint32_t msg_len, uint8_t *pld_ptr)
{
    os_status_t status = 0 ;

    os_itc_msg_handle_t xband_ipc;

    xband_ipc.src_entity= COMMS_XBAND_CTLR;

    xband_ipc.Msg_id = msg_id;

    xband_ipc.Msg_len = msg_len;

    xband_ipc.pld.pld_ptr = (uint8_t *)os_malloc(msg_len);

    os_memcpy(xband_ipc.pld.pld_ptr, pld_ptr, msg_len);

    status = os_itc_msg_send(&xband_ipc, dst_id , os_send_itc_wait);
/*TBD_FREE -- Free neeed to be done in receiver side and done only if payload available*/
  //  os_free(xband_ipc.pld.pld_ptr);
    //xband_ipc.pld.pld_ptr = NULL;

    return status;
}

/**
 * @brief This function handles the xband telecommand, telemetry set response
 */
void xband_tc_tm_set_rsp_hdlr(uint16_t msg_id, int32_t status)
{
    uint8_t *pld = NULL;

    os_itc_msg_handle_t xband_ipc;             ///< COMMS state transmit message

    //Init IPC
    xband_ipc.src_entity= COMMS_XBAND_CTLR;

    xband_ipc.pld.pld_ptr = (uint8_t *)os_malloc(5);

    pld = xband_ipc.pld.pld_ptr;

    //Extract the sequence number
    os_memcpy(pld, &xband_tc_seq_no, sizeof(uint16_t));
    os_memcpy(&pld[TM_RADIO_MOD_IDX], &xband_radio_id, sizeof(uint8_t));

    /*
     * Append the TM_CTRL data. 0x70 represents enable
     * encryption, authentication and message type as
     * response. Refer TC/TM manager document for more
     * information
     */
    pld[TM_CTRL_DATA_IDX] = TM_DEFAULT_CTRL_DATA;
    pld[TM_PLD_IDX] = status;

    xband_ipc.Msg_id = msg_id;
    xband_ipc.Msg_len = 1;

    DEBUG_CPRINT(("[XBAND] TM response sent"));
    log_id(LOG_INFO_L1,LOG_COMMS,__XBAND__TM_RESPONSE_SENT);
    status = os_itc_msg_send(&xband_ipc,TM_STORAGE,os_send_itc_wait);
/*TBD_FREE -- Free neeed to be done in receiver side and done only if payload available*/
  //  os_free(xband_ipc.pld.pld_ptr);
    //xband_ipc.pld.pld_ptr = NULL;
}

/**
 * @brief This function handles the xband telecommand, telemetry get response
 */
void xband_tc_tm_get_rsp_hdlr(uint16_t msg_id,int32_t status, uint16_t size, uint8_t *tm_pld)
{
    uint8_t *pld = NULL;

    os_itc_msg_handle_t xband_ipc;             ///< COMMS state transmit message

    //Init IPC
    xband_ipc.src_entity= COMMS_XBAND_CTLR;

    xband_ipc.pld.pld_ptr = (uint8_t *)os_malloc(size + 5);

    pld = xband_ipc.pld.pld_ptr;

    //Extract the sequence number
    os_memcpy(pld, &xband_tc_seq_no, sizeof(uint16_t));
    os_memcpy(&pld[TM_RADIO_MOD_IDX], &xband_radio_id, sizeof(uint8_t));

    /*
     * Append the TM_CTRL data. 0x70 represents enable
     * encryption, authentication and message type as
     * response. Refer TC/TM manager document for more
     * information
     */
    pld[TM_CTRL_DATA_IDX] = TM_DEFAULT_CTRL_DATA;
    xband_ipc.Msg_id = msg_id;
    xband_ipc.Msg_len = size;

    os_memcpy(&pld[TM_PLD_IDX],tm_pld, size+3);

    DEBUG_CPRINT(("[XBAND] TM pld response sent"));
    log_id(LOG_INFO_L1,LOG_COMMS,__XBAND__TM_PLD_RESPONSE_SENT);
    status = os_itc_msg_send(&xband_ipc, TM_STORAGE, os_send_itc_wait);
}

