/**
 * @file comms_uhf_sim.c
 *
 * @brief This file has the functions of all UHF simulations
 *
 * @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 <stdio.h>
#include"comms_uhf_main.h"
#include"comms_uhf_sim.h"
#include"comms_uhf_rf_cfg.h"
#include"comms_uhf_csp.h"
#include"logger.h"

/** UHF CC1110 register map */
extern s_uhf_cc1110_reg_map PLACE_IN_EXT_RAM_MEM uhf_reg_map;
/** UHF CC1110 register map rollback */  
extern s_uhf_cc1110_reg_map PLACE_IN_EXT_RAM_MEM uhf_reg_rb;
/** UHF PA configuration */
extern s_uhf_pa_cfg PLACE_IN_EXT_RAM_MEM uhf_pa_cfg;
/** UHF packet configuration */
extern s_uhf_pkt_cfg PLACE_IN_EXT_RAM_MEM uhf_pkt_cfg;
/** UHF RF configuration */
extern s_uhf_rf_cfg PLACE_IN_EXT_RAM_MEM uhf_rf_cfg;
/** UHF PA configuration rollback */
extern s_uhf_pa_cfg PLACE_IN_EXT_RAM_MEM uhf_pa_cfg_rb;
/** UHF packet configuration rollback */
extern s_uhf_pkt_cfg PLACE_IN_EXT_RAM_MEM uhf_pkt_cfg_rb;
/** UHF RF configuration rollback */
extern s_uhf_rf_cfg PLACE_IN_EXT_RAM_MEM uhf_rf_cfg_rb;

/**
 * @brief Pack UHF command response packet 
 * 
 * This function packs a UHF command response packet with the 
 * provided command ID.
 * 
 * The response packet format contains:
 * - Command ID
 * - Response payload
 * - CRC
 * 
 * The response payload is populated with dummy data.
 * CRC is calculated and set in the packet.
 * cmd_id
 * The packed response packet is written to the global RX buffer
 * for subsequent reading.
 *
 * @param cmd_id Command ID to set in response
 */
void uhf_sim_pack_cmd_rsp(uint8_t cmd_id)
{
    uint8_t len = 0;
    uint8_t frame[UHF_MAX_PLD + 1] = {0};

    s_comms_uhf_uart_data *ptr = (s_comms_uhf_uart_data *)frame;

    cmd_id +=1;

    os_memcpy(&uhf_reg_rb,&uhf_reg_map,sizeof(s_uhf_cc1110_reg_map));
    os_memcpy(&uhf_pa_cfg_rb,&uhf_pa_cfg,sizeof(s_uhf_pa_cfg));
    os_memcpy(&uhf_pkt_cfg_rb,&uhf_pkt_cfg,sizeof(s_uhf_pkt_cfg));
    os_memcpy(&uhf_rf_cfg_rb,&uhf_rf_cfg,sizeof(s_uhf_rf_cfg));

    ptr->header.command = cmd_id;
    ptr->length = sizeof(cmd_id);

    DEBUG_CPRINT(("\n\r [UHF_SIM] TM cmd: %x",cmd_id));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_SIM_TM_CMD__u8_,cmd_id);

    switch(cmd_id)
    {
        case UHF_SET_ALL_RADIO_CFG_RSP:
            {
            }
            break;

        case UHF_GET_ALL_RADIO_CFG_RSP:
            {
                ptr->length = sizeof(s_uhf_cc1110_reg_map);
                os_memcpy(ptr->data,&uhf_reg_rb,ptr->length);
            }
            break;

        case UHF_GET_TX_RADIO_CFG_RSP :
            {
                ptr->length = sizeof(s_uhf_rf_cfg);
                os_memcpy(ptr->data,&uhf_rf_cfg_rb,ptr->length);
            }
            break;

        case UHF_GET_RX_RADIO_CFG_RSP :
            {
                ptr->length = sizeof(s_uhf_rf_cfg);
                os_memcpy(ptr->data,&uhf_rf_cfg_rb,ptr->length);
            }
            break;

        case UHF_GET_TX_PKT_CFG_RSP   :
            {
                ptr->length = sizeof(s_uhf_pkt_cfg);
                os_memcpy(ptr->data,&uhf_pkt_cfg_rb,ptr->length);
            }
            break;

        case UHF_GET_RX_PKT_CFG_RSP   :
            {
                ptr->length = sizeof(s_uhf_pkt_cfg);
                os_memcpy(ptr->data,&uhf_pkt_cfg_rb,ptr->length);
            }
            break;

        case UHF_GET_PWR_CFG_RSP      :
            {
                ptr->length = sizeof(s_uhf_pa_cfg);
                os_memcpy(ptr->data,&uhf_pa_cfg_rb,ptr->length);
            }
            break;

        case UHF_GET_ADDR_CFG_RSP     :
            {
                ptr->length = sizeof(uint8_t);
                os_memcpy(ptr->data,&uhf_reg_rb.addr,ptr->length);
            }
            break;

        case UHF_SET_TX_RADIO_CFG_ACK :
            {
            }
            break;

        case UHF_SET_RX_RADIO_CFG_ACK :
            {
            }
            break;

        case UHF_SET_TX_PKT_CFG_ACK   :
            {
            }
            break;

        case UHF_SET_RX_PKT_CFG_ACK   :
            {
            }
            break;

        case UHF_SET_PWR_CFG_ACK      :
            {
            }
            break;

     /*   case UHF_SET_ADDR_CFG_ACK     :
            {
            }
            break;*/

        case UHF_RADIO_MSG_CALLSIGN:
            {
            }
            break;

        case UHF_CMN_MSG_ASCII:
            {
            }
            break;

        case UHF_BOOTLOADER_MSG_ACK:
            {
            }
            break;

        case UHF_BOOTLOADER_MSG_NACK:
            {
            }
            break;

        case UHF_CMN_MSG_ACK:
            {
            }
            break;

        case UHF_RADIO_MSG_RANGING_ACK:
            {
            }
            break;

        case UHF_RADIO_MSG_TELEM:
            {
                uint8_t tm_buff[] = {0x0,0x44,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x59,0x5,0xfe,0x6,0xff,0x7,0xf6,0xf,0xff,0x7,0xff,0x7,0xff,0x7,0xf5,0xf,0x6a,0x5,0x3b,0x7,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
                os_memcpy(ptr->data,tm_buff,sizeof(tm_buff));
                ptr->length = sizeof(tm_buff);

            }
            break;

        case UHF_RADIO_MSG_GET_CALLSIGN:
            {
            }
            break;

        case UHF_RADIO_MSG_GET_CFG:
            {
            }
            break;

        case UHF_CMN_MSG_NACK:
            {
            }
            break;

    }

    len = ptr->length + sizeof(s_comms_uhf_uart_header) + sizeof(ptr->length);

    DEBUG_CPRINT(("[UHF_SIM] length: %d",len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_SIM_LENGTH__u8_,len);
    uhf_send_rx_uart_cmd((uint8_t*)ptr,len); //ptr instead of frame
}

