/**
 * @file comms_uhf_api.c
 *
 * @brief #TODO
 *
 * @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 <math.h>
#include "comms_uhf_main.h"
#include "comms_uhf_sim.h"
#include "comms_uhf_api.h"
#include "comms_uhf_csp.h"
#include <stdint.h>
#include "comms_uhf_ipc.h"
#include "usart.h"
#include "comms_ftm.h"
#include "logger.h"


#define XO_FREQ 27000000 ///< UHF crystal oscillator frequency
#define BOOT_WR_SUCC 0x00 ///< Bootloader write success status 
#define BOOT_WR_FAIL 0x01 ///< Bootloader write fail status
#define fw_offset_data_len 27632 ///< Firmware offset data length

int8 PLACE_IN_EXT_RAM_MEM outbuff[mem_size]; ///< Output buffer
int PLACE_IN_EXT_RAM_MEM line_count = 0; ///< Line counter
int PLACE_IN_EXT_RAM_MEM total_data = 0; ///< Total data amount 
unsigned int PLACE_IN_EXT_RAM_MEM byte_count = 0; ///< Byte counter
uint8_t PLACE_IN_EXT_RAM_MEM fw_wr_succ = 1; ///< Firmware write success flag
uint8_t PLACE_IN_EXT_RAM_MEM UHF_FW_UP = 0; ///< UHF firmware update flag

#if 0
FILE* file_ptr;

uint8_t* filePath = "openlst_437_radio.txt";
int ser_fd;  //= open("/dev/ttyUSB0", O_RDWR);

struct termios tty;
#endif

s_seri_pack_send_t PLACE_IN_EXT_RAM_MEM snd_pack; ///< Serial packet send structure
int8 signature_key[] = {"ae5c8928f3aa77d8d2f0dd2aba62b51b"};//{0xae, 0x5c, 0x89, 0x28, 0xf3, 0xaa, 0x77, 0xd8, 0xd2, 0xf0, 0xdd, 0x2a, 0xba, 0x62, 0xb5, 0x1b};

s_uhf_cc1110_reg_map PLACE_IN_EXT_RAM_MEM uhf_reg_map; ///< UHF register map structure
s_uhf_cc1110_reg_map PLACE_IN_EXT_RAM_MEM uhf_reg_rb; ///< Readback UHF register map structure 
s_uhf_pa_cfg PLACE_IN_EXT_RAM_MEM uhf_pa_cfg; ///< UHF power amplifier config structure
s_uhf_pkt_cfg PLACE_IN_EXT_RAM_MEM uhf_pkt_cfg; ///< UHF packet config structure
s_uhf_rf_cfg PLACE_IN_EXT_RAM_MEM uhf_rf_cfg; ///< UHF RF config structure
s_comms_uhf_beacon_data	PLACE_IN_EXT_RAM_MEM uhf_tx_beacon_data; ///< UHF beacon data structure

s_uhf_pa_cfg PLACE_IN_EXT_RAM_MEM uhf_pa_cfg_rb; ///< Readback UHF PA config structure
s_uhf_pkt_cfg PLACE_IN_EXT_RAM_MEM uhf_pkt_cfg_rb; ///< Readback UHF packet config structure
s_uhf_rf_cfg PLACE_IN_EXT_RAM_MEM uhf_rf_cfg_rb; ///< Readback UHF RF config structure

extern uint8_t PLACE_IN_EXT_RAM_MEM comms_uhf_csw_rx_buf[512]; ///< UHF CSW RX buffer
extern uint8_t PLACE_IN_EXT_RAM_MEM uhf_uart_rsp[400]; ///< UHF UART response buffer 
extern uint16_t PLACE_IN_EXT_RAM_MEM uhf_uart_rsp_len; ///< UHF UART response length
comms_firmwre_info_save  uhf_fw_buf; ///< UHF firmware buffer


/**
 * @brief Initialize UHF register map.
 * 
 * Initializes the UHF radio register map with default values.
 */
void sdr_uhf_init_reg_map(void)
{
    uhf_reg_map.sync1       =   0xD3 ;
    uhf_reg_map.sync0       =   0x91 ;
    uhf_reg_map.pktlen      =   0xFF ;
    uhf_reg_map.pktctrl1    =   0x10 ;
    uhf_reg_map.pktctrl0    =   0x21 ;
    uhf_reg_map.addr        =   0x00 ;
    uhf_reg_map.channr      =   0x00 ;
    uhf_reg_map.fsctrl1     =   0x06 ;
    uhf_reg_map.fsctrl0     =   0x00 ;
    uhf_reg_map.freq2       =   0x10 ;
    uhf_reg_map.freq1       =   0x2F ;
    uhf_reg_map.freq0       =   0x69 ;
    uhf_reg_map.mdmcfg4     =   0xF8 ;
    uhf_reg_map.mdmcfg3     =   0x20 ;
    uhf_reg_map.mdmcfg2     =   0x03 ;
    uhf_reg_map.mdmcfg1     =   0xA3 ;
    uhf_reg_map.mdmcfg0     =   0xB9 ;
    uhf_reg_map.deviatn     =   0x11 ;
    uhf_reg_map.mcsm2       =   0x07 ;
    uhf_reg_map.mcsm1       =   0x30 ;
    uhf_reg_map.mcsm0       =   0x18 ;
    uhf_reg_map.foccfg      =   0x37 ;
    uhf_reg_map.bscfg       =   0x6C ;
    uhf_reg_map.agcctrl2    =   0x03 ;
    uhf_reg_map.agcctrl1    =   0x40 ;
    uhf_reg_map.agcctrl0    =   0x91 ;
    uhf_reg_map.frend1      =   0x56 ;
    uhf_reg_map.frend0      =   0x10 ;
    uhf_reg_map.fscal3      =   0xE9 ;
    uhf_reg_map.fscal2      =   0x2A ;
    uhf_reg_map.fscal1      =   0x00 ;
    uhf_reg_map.fscal0      =   0x1F ;
    uhf_reg_map.test2       =   0x88 ;
    uhf_reg_map.test1       =   0x31 ;
    uhf_reg_map.test0       =   0x0B ;
    uhf_reg_map.pa_table7   =   0x00 ;
    uhf_reg_map.pa_table6   =   0x00 ;
    uhf_reg_map.pa_table5   =   0x00 ;
    uhf_reg_map.pa_table4   =   0x00 ;
    uhf_reg_map.pa_table3   =   0x00 ;
    uhf_reg_map.pa_table2   =   0x00 ;
    uhf_reg_map.pa_table1   =   0x00 ;
    uhf_reg_map.pa_table0   =   0xC0 ;
    uhf_reg_map.iocfg2      =   0x91 ;
    uhf_reg_map.iocfg1      =   0x28 ;
    uhf_reg_map.iocfg0      =   0x00 ;
}

/**
 * @brief  This function is used for updating the firmware Key cfg of the UHF
 * @brief Configure UHF firmware.
 * 
 * Sends firmware configuration commands to the UHF radio.
 * 
 * @param[in] tc_cmd Pointer to firmware configuration command buffer.
 * @return 0 on success, negative error code on failure.
 */
int32_t sdr_uhf_firmware_cfg(uint8_t *tc_cmd)
{
    int ret =0;

    os_memcpy(signature_key, tc_cmd, sizeof(signature_key));

    return ret;
}

/**
 * @brief Get UHF telemetry properties.
 * 
 * Retrieves telemetry properties from the UHF radio.
 * 
 * @param[out] tm Pointer to telemetry property structure. 
 * @param[in] size Size of telemetry property structure in bytes.
 * @return 0 on success, negative error code on failure.
 */
int32_t sdr_uhf_get_tm_prop(s_comms_uhf_tm_prop *tm, uint16_t size)
{
    int ret =0;
    return ret;
}
/**
 * @brief Update UHF frequency.
 * 
 * Sets the UHF radio frequency.
 * 
 * @param[in] freq Frequency value in Hz.
 */
void sdr_uhf_upd_freq(float freq)
{
    float reg_freq = 0;
    uint32_t reg_val = 0;

    reg_freq = (freq * (1<<16))/XO_FREQ ;
    reg_val = (uint32_t)reg_freq;

    uhf_rf_cfg.freq0 = reg_val & MASK_8_BITS;
    uhf_rf_cfg.freq1 = (reg_val >> 8) & MASK_8_BITS;
    uhf_rf_cfg.freq2 = (reg_val >> 16) & MASK_8_BITS;

    DEBUG_CPRINT(("\nBase Frequency: %f, XO Frequency: %f \n",(f32)freq,(f32)XO_FREQ));
    log_id_with_two_arg(LOG_INFO_L2,LOG_COMMS,BASE_FREQ__u32__XO_FREQ__u32_,(f32)freq,(f32)XO_FREQ);
    DEBUG_CPRINT(("\nRegisters F0: %x, F1: %x, F2: %x\n",uhf_rf_cfg.freq0,uhf_rf_cfg.freq1,uhf_rf_cfg.freq2));
    log_id_with_three_arg(LOG_INFO_L2,LOG_COMMS,REGISTERS_F0__hex__F1__hex__F2__hex_,uhf_rf_cfg.freq0,uhf_rf_cfg.freq1,uhf_rf_cfg.freq2);

}

/**
 * @brief Update UHF IF frequency.
 * 
 * Sets the UHF radio IF frequency.
 * 
 * @param[in] if_freq IF frequency value in Hz. 
 */
void sdr_uhf_upd_if_freq(float if_freq)
{
    float reg_freq = 0;
    uint32_t reg_val = 0;

    reg_freq = (if_freq * (1<<10))/XO_FREQ ;
    reg_val = (uint32_t)(round(reg_freq));

    uhf_rf_cfg.fsctrl1 = reg_val & MASK_5_BITS;

    DEBUG_CPRINT(("\nIF Frequency: %f, XO Frequency: %f \n",(f32)if_freq,(f32)XO_FREQ));
    log_id_with_two_arg(LOG_INFO_L2,LOG_COMMS,IF_FREQ__u32__XO_FREQ__u32_,(f32)if_freq,(f32)XO_FREQ);
    DEBUG_CPRINT(("\nRegisters FSCTRL1: %x\n",uhf_rf_cfg.fsctrl1));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,REGISTERS_FSCTRL1__hex_,uhf_rf_cfg.fsctrl1);

}

/**
 * @brief Update UHF channel spacing. 
 * 
 * Sets the UHF radio channel spacing.
 * 
 * @param[in] chan_spacing Channel spacing in Hz.
 */
void sdr_uhf_upd_ch_spacing(float chan_spacing)
{
    float reg_cal = 0;
    uint8_t reg_val = 0;

    reg_cal = (chan_spacing * (1<<10) ) / XO_FREQ;
    reg_cal = log2(reg_cal);

    //reg_cal = round(reg_cal);
    reg_cal = floor(reg_cal);
    reg_val = (uint8_t)reg_cal;

    /** Channel spacing Exponent */
    uhf_rf_cfg.chanspc_e = reg_val;

    DEBUG_CPRINT(("\n\ruhf_rf_cfg.chanspc_e: %x\n",uhf_rf_cfg.chanspc_e));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_CHANNEL_SPC_E__hex_,uhf_rf_cfg.chanspc_e);

    reg_cal = (chan_spacing * (1<<18)) / (XO_FREQ * (1 << reg_val)) - 256;

    reg_cal = round(reg_cal);

    reg_val = (uint8_t)reg_cal;

    /** Channel spacing Mantissa */
    uhf_rf_cfg.chanspc_m = reg_val ;

    DEBUG_CPRINT(("\n\r uhf_rf_cfg.chanspc_m: %x\n",uhf_rf_cfg.chanspc_m));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_CHANNEL_SPC_M__hex_,uhf_rf_cfg.chanspc_m);
}

/**
 * @brief Update UHF frequency offset.
 * 
 * Sets the UHF radio frequency offset value.
 * 
 * @param[in] freq_ofst Frequency offset in Hz.
 */ 
void sdr_uhf_upd_freq_offset(float freq_ofst)
{
    float reg_cal = 0;
    uint8_t reg_val = 0;

    reg_cal = (freq_ofst * (1<<14))/ XO_FREQ;

    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.fsctrl0 = reg_val & MASK_8_BITS;

    DEBUG_CPRINT(("\nfreq_offset: %f, fsctrl0: %x\n",(f32)freq_ofst, uhf_rf_cfg.fsctrl0));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,FREQ_OFFSET__u32__FSCTRL0__hex_,(f32)freq_ofst, uhf_rf_cfg.fsctrl0);
}

/**
 * @brief Set UHF transmitter modem configuration.
 * 
 * Configures the UHF transmitter modem settings.
 * 
 * @param[in] rf_cfg Pointer to transmitter configuration structure.
 */
void sdr_uhf_set_tx_modem_cfg(s_uhf_rf_tx_cfg *rf_cfg)
{
    float reg_cal = 0;
    uint8_t reg_val = 0;

    uhf_rf_cfg.mod_format = rf_cfg->mod_type;

    uhf_rf_cfg.sync_mode = rf_cfg->sync_mode;

    uhf_rf_cfg.dem_dcfilt_off = rf_cfg->enb_dc_filter;

    uhf_rf_cfg.fec_en = rf_cfg->fec_enb;

    uhf_rf_cfg.num_preamble = rf_cfg->preamble_num;

    uhf_rf_cfg.manchester_en = rf_cfg->manchester_enable;

    reg_cal = (rf_cfg->data_rate * (1<<20))/XO_FREQ;
    reg_cal = log2(reg_cal);
    reg_cal = round(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.drate_e = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.drate_e: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_DRATE_E__hex_,reg_val);

    reg_cal = ((rf_cfg->data_rate * (1<<(28-reg_val)))/(XO_FREQ ));
    reg_cal = reg_cal-256;
    reg_cal = round(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.drate_m = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.drate_m: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_DRATE_M__hex_,reg_val);

    reg_cal = (XO_FREQ / (32 * rf_cfg->channel_bw ));
    reg_cal = log2(reg_cal);
    reg_cal = floor(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.chanbw_e = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.chanbw_e: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_CHANBW_E__hex_,reg_val);

    reg_cal = ((XO_FREQ / (8*rf_cfg->channel_bw*(1<<reg_val)))-4);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.chanbw_m = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.chanbw_m: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_CHANBW_M__hex_,reg_val);
}

/**
 * @brief Set UHF receiver modem configuration.
 * 
 * Configures the UHF receiver modem settings.
 * 
 * @param[in] rf_cfg Pointer to receiver configuration structure.
 */
void sdr_uhf_set_rx_modem_cfg(s_uhf_rf_rx_cfg *rf_cfg)
{
    double reg_cal = 0;
    uint8_t reg_val = 0;

    uhf_rf_cfg.mod_format = rf_cfg->mod_type;

    uhf_rf_cfg.sync_mode = rf_cfg->sync_mode;

    uhf_rf_cfg.dem_dcfilt_off = rf_cfg->enb_dc_filter;

    uhf_rf_cfg.fec_en = rf_cfg->fec_enb;

    uhf_rf_cfg.num_preamble = rf_cfg->preamble_num;

    uhf_rf_cfg.manchester_en = rf_cfg->manchester_enable;

    reg_cal = (rf_cfg->data_rate * (1<<20))/XO_FREQ;
    reg_cal = log2(reg_cal);
    reg_cal = round(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.drate_e = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.drate_e: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_DRATE_E__hex_,reg_val);

    reg_cal = (((rf_cfg->data_rate * (1<<(28-reg_val)))/(XO_FREQ ))-256);
    reg_cal = round(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.drate_m = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.drate_m: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_DRATE_M__hex_,reg_val);

    reg_cal = (XO_FREQ / (32 * rf_cfg->channel_bw ));
    reg_cal = log2(reg_cal);
    reg_cal = floor(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.chanbw_e = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.chanbw_e: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_CHANBW_E__hex_,reg_val);
    reg_cal = ((XO_FREQ / (8*rf_cfg->channel_bw*(1<<reg_val)))-4);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.chanbw_m = reg_val;
    DEBUG_CPRINT(("\n uhf_rf_cfg.chanbw_m: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_CHANBW_M__hex_,reg_val);

    uhf_rf_cfg.rx_timeout =  rf_cfg->rx_sync_time_out;
    uhf_rf_cfg.rx_attenuation = rf_cfg->rx_attenuation;
    DEBUG_CPRINT(("\n uhf_rf_cfg.rx_timeout: %d",uhf_rf_cfg.rx_timeout));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_RX_TIMEOUT__u8_,uhf_rf_cfg.rx_timeout);
    DEBUG_CPRINT(("\n uhf_rf_cfg.rx_attenuation: %d",uhf_rf_cfg.rx_attenuation));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_RX_ATTENUATION__u8_,uhf_rf_cfg.rx_timeout);
}

/**
 * @brief Set UHF frequency deviation.
 * 
 * Sets the frequency deviation value for the UHF radio.
 * 
 * @param[in] freq_dev Frequency deviation in Hz.
 */
void sdr_uhf_set_freq_dev_cfg(float freq_dev)
{
    float reg_cal = 0;
    uint8_t reg_val = 0;

    reg_cal = (freq_dev * (1<<14))/XO_FREQ;
    reg_cal = log2(reg_cal);
    reg_cal = round(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.deviation_e = reg_val ;

    DEBUG_CPRINT(("\n deviation_e: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEVIATION_E__hex_,reg_val);
    reg_cal = (((freq_dev * (1<<17))/(XO_FREQ*(1<<reg_val)))-8);
    reg_cal = round(reg_cal);
    reg_val = (uint8_t)reg_cal;

    uhf_rf_cfg.deviation_m = reg_val ;
    DEBUG_CPRINT(("\n deviation_m: %x\n",reg_val));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEVIATION_M__hex_,reg_val);
}

/**
 * @brief Set UHF transmitter configuration.
 * 
 * Sends the UHF transmitter configuration command.
 * 
 * @param[in] rf_cfg Pointer to transmitter configuration structure.
 * @return 0 on success, negative error code on failure.
 */
int32_t sdr_uhf_set_tx_cfg_req(s_uhf_rf_tx_cfg *rf_cfg)
{
    int ret =0;

    DEBUG_CPRINT(("base frequency :%f\n",(f32)rf_cfg->base_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,BASE_FREQ__u32_,(f32)rf_cfg->base_freq);
    DEBUG_CPRINT(("if frequency :%f\n",rf_cfg->if_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,IF_FREQ__u32_,rf_cfg->if_freq);
    DEBUG_CPRINT(("channel number :%u\n",rf_cfg->chan_num));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_NUMBER__u8_,rf_cfg->chan_num);
    DEBUG_CPRINT(("channel spacing :%f\n",rf_cfg->chan_spacing));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_SPACING__u32_,rf_cfg->chan_spacing);
    DEBUG_CPRINT(("frequency offset :%f\n",rf_cfg->freq_ofst));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FREQ_OFFSET__u32_,rf_cfg->freq_ofst);
    DEBUG_CPRINT(("mode type :%u\n",rf_cfg->mod_type));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MOD_TYPE__8_,rf_cfg->mod_type);
    DEBUG_CPRINT(("sync mode :%u\n",rf_cfg->sync_mode));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SYNC_MODE__u8_,rf_cfg->sync_mode);
    DEBUG_CPRINT(("data rate:%f\n",rf_cfg->data_rate));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DATA_RATE__u32_,rf_cfg->data_rate);
    DEBUG_CPRINT(("enable dc filter:%u\n",rf_cfg->enb_dc_filter));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ENABLE_DC_FILTER__u8_,rf_cfg->enb_dc_filter);
    DEBUG_CPRINT(("fec enable:%u\n",rf_cfg->fec_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FEC_ENABLE__u8_,rf_cfg->fec_enb);
    DEBUG_CPRINT(("preamble number :%u\n",rf_cfg->preamble_num));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PREAMBLE_NUMBER__u8_,rf_cfg->preamble_num);
    DEBUG_CPRINT(("channel bandwidth :%f\n",rf_cfg->channel_bw));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_BW__u32_,rf_cfg->channel_bw);
    DEBUG_CPRINT(("manchester enable :%u\n",rf_cfg->manchester_enable));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MANCHESTER_ENABLE__u8_,rf_cfg->manchester_enable);
    DEBUG_CPRINT(("frequency deviation :%f\n",rf_cfg->freq_dev));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FREQ_DEVIATION__u32_,rf_cfg->freq_dev);

    sdr_uhf_upd_freq(rf_cfg->base_freq);

    sdr_uhf_upd_if_freq(rf_cfg->if_freq);

    uhf_rf_cfg.channr = rf_cfg->chan_num ;

    sdr_uhf_upd_ch_spacing(rf_cfg->chan_spacing);

    sdr_uhf_upd_freq_offset(rf_cfg->freq_ofst);

    sdr_uhf_set_tx_modem_cfg(rf_cfg);

    sdr_uhf_set_freq_dev_cfg(rf_cfg->freq_dev);

    return ret;
}

/**
 * @brief Set UHF receiver configuration. 
 * 
 * Sends the UHF receiver configuration command.
 * 
 * @param[in] rf_cfg Pointer to receiver configuration structure.
 * @return 0 on success, negative error code on failure. 
 */
int32_t sdr_uhf_set_rx_cfg_req(s_uhf_rf_rx_cfg *rf_cfg)
{
    int ret =0;
    DEBUG_CPRINT(("base frequency :%f\n",(f32)rf_cfg->base_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,BASE_FREQ__u32_,(f32)rf_cfg->base_freq);
    DEBUG_CPRINT(("if frequency :%f\n",rf_cfg->if_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,IF_FREQ__u32_,rf_cfg->if_freq);
    DEBUG_CPRINT(("channel number :%u\n",rf_cfg->chan_num));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_NUMBER__u8_,rf_cfg->chan_num);
    DEBUG_CPRINT(("channel spacing :%f\n",rf_cfg->chan_spacing));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_SPACING__u32_,rf_cfg->chan_spacing);
    DEBUG_CPRINT(("frequency offset :%f\n",rf_cfg->freq_ofst));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FREQ_OFFSET__u32_,rf_cfg->freq_ofst);
    DEBUG_CPRINT(("mode type :%u\n",rf_cfg->mod_type));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MOD_TYPE__8_,rf_cfg->mod_type);
    DEBUG_CPRINT(("sync mode :%u\n",rf_cfg->sync_mode));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SYNC_MODE__u8_,rf_cfg->sync_mode);
    DEBUG_CPRINT(("data rate:%f\n",rf_cfg->data_rate));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DATA_RATE__u32_,rf_cfg->data_rate);
    DEBUG_CPRINT(("enable dc filter:%u\n",rf_cfg->enb_dc_filter));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ENABLE_DC_FILTER__u8_,rf_cfg->enb_dc_filter);
    DEBUG_CPRINT(("fec enable:%u\n",rf_cfg->fec_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FEC_ENABLE__u8_,rf_cfg->fec_enb);
    DEBUG_CPRINT(("preamble number :%u\n",rf_cfg->preamble_num));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PREAMBLE_NUMBER__u8_,rf_cfg->preamble_num);
    DEBUG_CPRINT(("channel bandwidth :%f\n",rf_cfg->channel_bw));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_BW__u32_,rf_cfg->channel_bw);
    DEBUG_CPRINT(("manchester enable :%u\n",rf_cfg->manchester_enable));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MANCHESTER_ENABLE__u8_,rf_cfg->manchester_enable);
    DEBUG_CPRINT(("frequency deviation :%f\n",rf_cfg->freq_dev));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FREQ_DEVIATION__u32_,rf_cfg->freq_dev);
    DEBUG_CPRINT(("sync timeout :%f\n",rf_cfg->rx_sync_time_out));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SYNC_TIMEOUT__u32_,rf_cfg->rx_sync_time_out);
    DEBUG_CPRINT(("rx attenuation :%u\n",rf_cfg->rx_attenuation));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_ATTENUATION__u32_,rf_cfg->rx_attenuation);


    sdr_uhf_upd_freq(rf_cfg->base_freq);

    sdr_uhf_upd_if_freq(rf_cfg->if_freq);

    uhf_rf_cfg.channr = rf_cfg->chan_num ;

    sdr_uhf_upd_ch_spacing(rf_cfg->chan_spacing);

    sdr_uhf_upd_freq_offset(rf_cfg->freq_ofst);

    sdr_uhf_set_rx_modem_cfg(rf_cfg);

    sdr_uhf_set_freq_dev_cfg(rf_cfg->freq_dev);

    return ret;
}

/**
 * @brief Set UHF transmitter packet configuration.
 * 
 * Sends the UHF transmitter packet configuration command.
 * 
 * @param[in] tx_pkt_cfg Pointer to TX packet configuration structure.
 * @return 0 on success, negative error code on failure.
 */
int32_t sdr_uhf_set_tx_pkt_cfg_req(s_uhf_rf_tx_pkt_cfg *tx_pkt_cfg)
{
    int ret = 0;

    uhf_pkt_cfg.sync0 = 0;
    uhf_pkt_cfg.sync0 |= (tx_pkt_cfg->demod_sync_word & MASK_8_BITS) ;

    uhf_pkt_cfg.sync1 =0;
    uhf_pkt_cfg.sync1 |= ((tx_pkt_cfg->demod_sync_word >> 8)&(MASK_8_BITS));

    uhf_pkt_cfg.pktlen = tx_pkt_cfg->max_pkt_len;

    DEBUG_CPRINT(("\n sync0: %x,sync1: %x\n",uhf_pkt_cfg.sync0,uhf_pkt_cfg.sync1));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,SYNC0__hex__SYNC1__hex_,uhf_pkt_cfg.sync0,uhf_pkt_cfg.sync1);
    DEBUG_CPRINT(("\n packet length: %x",uhf_pkt_cfg.pktlen));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PKT_LEN__hex_,uhf_pkt_cfg.pktlen);
    uhf_pkt_cfg.white_data = tx_pkt_cfg->whitening_enb ;

    uhf_pkt_cfg.pkt_format = tx_pkt_cfg->pkt_fmt ;

    uhf_pkt_cfg.crc_en = tx_pkt_cfg->crc_enb ;

    uhf_pkt_cfg.length_config = tx_pkt_cfg->pkt_len_cfg ;

    uhf_reg_map.addr = tx_pkt_cfg->device_addr;
    DEBUG_CPRINT(("\n demod sync word: %x",tx_pkt_cfg->demod_sync_word));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEMOD_SYNC_WORD__hex_,tx_pkt_cfg->demod_sync_word);
    DEBUG_CPRINT(("\n packet length: %x",tx_pkt_cfg->max_pkt_len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH__hex_,tx_pkt_cfg->max_pkt_len);
    DEBUG_CPRINT(("\n whitening enable: %x",tx_pkt_cfg->whitening_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,WHITENING_ENABLE__hex_,tx_pkt_cfg->whitening_enb);
    DEBUG_CPRINT(("\n packet format: %x",tx_pkt_cfg->pkt_fmt));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_FORMAT__hex_,tx_pkt_cfg->pkt_fmt);
    DEBUG_CPRINT(("\n crc enable: %x",tx_pkt_cfg->crc_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CRC_ENABLE__hex_,tx_pkt_cfg->crc_enb);
    DEBUG_CPRINT(("\n packet length cfg: %x",tx_pkt_cfg->pkt_len_cfg));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH_CFG__hex_,tx_pkt_cfg->pkt_len_cfg);
    DEBUG_CPRINT(("\n device address: %x",tx_pkt_cfg->device_addr));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEVICE_ADDRESS__hex_,tx_pkt_cfg->device_addr);

    return ret;
}


/**
 * @brief Set UHF receiver packet configuration.
 * 
 * Sends the UHF receiver packet configuration command.
 * 
 * @param[in] rx_pkt_cfg Pointer to RX packet configuration structure. 
 * @return 0 on success, negative error code on failure.
 */
int32_t sdr_uhf_set_rx_pkt_cfg_req(s_uhf_rf_rx_pkt_cfg *rx_pkt_cfg)
{
    int ret = 0;

    uhf_pkt_cfg.sync0 = 0;
    uhf_pkt_cfg.sync0 |= ((rx_pkt_cfg->demod_sync_word & MASK_8_BITS) << 0);

    uhf_pkt_cfg.sync1 &= ~(MASK_8_BITS << 8) ;
    uhf_pkt_cfg.sync1 |= ((rx_pkt_cfg->demod_sync_word & (MASK_8_BITS << 8)) >> 8);

    uhf_pkt_cfg.pktlen = rx_pkt_cfg->max_pkt_len;

    DEBUG_CPRINT(("\n sync0: %x,sync1: %x\n",uhf_pkt_cfg.sync0,uhf_pkt_cfg.sync1));
    DEBUG_CPRINT(("\n packet length: %x",uhf_pkt_cfg.pktlen));

    uhf_pkt_cfg.white_data = rx_pkt_cfg->whitening_enb ;

    uhf_pkt_cfg.pkt_format = rx_pkt_cfg->pkt_fmt ;

    uhf_pkt_cfg.crc_en = rx_pkt_cfg->crc_enb ;

    uhf_pkt_cfg.length_config = rx_pkt_cfg->pkt_len_cfg ;

    uhf_reg_map.addr = rx_pkt_cfg->device_addr;

    DEBUG_CPRINT(("\n demod sync word: %x",rx_pkt_cfg->demod_sync_word));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEMOD_SYNC_WORD__hex_,rx_pkt_cfg->demod_sync_word);
    DEBUG_CPRINT(("\n packet length: %x",rx_pkt_cfg->max_pkt_len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH__hex_,rx_pkt_cfg->max_pkt_len);
    DEBUG_CPRINT(("\n whitening enable: %x",rx_pkt_cfg->whitening_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,WHITENING_ENABLE__hex_,rx_pkt_cfg->whitening_enb);
    DEBUG_CPRINT(("\n packet format: %x",rx_pkt_cfg->pkt_fmt));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_FORMAT__hex_,rx_pkt_cfg->pkt_fmt);
    DEBUG_CPRINT(("\n crc enable: %x",rx_pkt_cfg->crc_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CRC_ENABLE__hex_,rx_pkt_cfg->crc_enb);
    DEBUG_CPRINT(("\n packet length cfg: %x",rx_pkt_cfg->pkt_len_cfg));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH_CFG__hex_,rx_pkt_cfg->pkt_len_cfg);
    DEBUG_CPRINT(("\n device address: %x",rx_pkt_cfg->device_addr));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEVICE_ADDRESS__hex_,rx_pkt_cfg->device_addr);


    return ret;
}

/**
 * @brief Set UHF power configuration.
 * 
 * Sends the UHF power configuration command.
 * 
 * @param[in] pwr_cfg Pointer to power configuration structure.
 * @return 0 on success, negative error code on failure.
 */
int32_t sdr_uhf_set_pwr_cfg_req(s_uhf_rf_pwr_cfg *pwr_cfg)
{
    int ret = 0;

    uhf_pa_cfg.max_dvga_gain   = pwr_cfg->max_dvga_gain;
    uhf_pa_cfg.max_lna_gain    = pwr_cfg->max_lna_gain;
    uhf_pa_cfg.agc_lna_priority= pwr_cfg->lna_sel;
    uhf_pa_cfg.filter_length   = pwr_cfg->filter_len;
    uhf_pa_cfg.frend0          = pwr_cfg->power_table_sel;
    uhf_pa_cfg.pa_table0       = pwr_cfg->rf_output_power;

    DEBUG_CPRINT(("\n max dvga gain: %x",pwr_cfg->max_dvga_gain));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MA_DVGA_GAIN__hex_,pwr_cfg->max_dvga_gain);
    DEBUG_CPRINT(("\n max lna gain: %x",pwr_cfg->max_lna_gain));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MAX_LNA_GAIN__hex_,pwr_cfg->max_lna_gain);
    DEBUG_CPRINT(("\n lna sel: %x",pwr_cfg->lna_sel));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,LNA_SEL__hex_,pwr_cfg->lna_sel);
    DEBUG_CPRINT(("\n filterlen: %x",pwr_cfg->filter_len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FILTERLEN__hex_,pwr_cfg->filter_len);
    DEBUG_CPRINT(("\n power table sel: %x",pwr_cfg->power_table_sel));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,POWER_TABLE__hex_,pwr_cfg->power_table_sel);
    DEBUG_CPRINT(("\n output power: %x",pwr_cfg->rf_output_power));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,OUTPUT_PPOWER__hex_,pwr_cfg->rf_output_power);

    return ret;
}


/**
 * @brief Get UHF transmitter packet configuration.
 * 
 * Retrieves the current UHF transmitter packet configuration.
 */
void sdr_uhf_get_tx_pkt_cfg(void)
{
    s_uhf_rf_tx_pkt_cfg tx_pkt_cfg;

    tx_pkt_cfg.demod_sync_word = (uhf_pkt_cfg_rb.sync0) | (uhf_pkt_cfg_rb.sync1 << 8);
    tx_pkt_cfg.max_pkt_len = uhf_pkt_cfg_rb.pktlen;
    tx_pkt_cfg.whitening_enb = uhf_pkt_cfg_rb.white_data ;
    tx_pkt_cfg.pkt_fmt = uhf_pkt_cfg_rb.pkt_format;
    tx_pkt_cfg.crc_enb = uhf_pkt_cfg_rb.crc_en;
    tx_pkt_cfg.pkt_len_cfg = uhf_pkt_cfg_rb.length_config;
    tx_pkt_cfg.device_addr = uhf_pkt_cfg_rb.addr;
    DEBUG_CPRINT(("\n demod sync word: %x",tx_pkt_cfg.demod_sync_word));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEMOD_SYNC_WORD__hex_,tx_pkt_cfg.demod_sync_word);
    DEBUG_CPRINT(("\n packet length: %x",tx_pkt_cfg.max_pkt_len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH__hex_,tx_pkt_cfg.max_pkt_len);
    DEBUG_CPRINT(("\n whitening enable: %x",tx_pkt_cfg.whitening_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,WHITENING_ENABLE__hex_,tx_pkt_cfg.whitening_enb);
    DEBUG_CPRINT(("\n packet format: %x",tx_pkt_cfg.pkt_fmt));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_FORMAT__hex_,tx_pkt_cfg.pkt_fmt);
    DEBUG_CPRINT(("\n crc enable: %x",tx_pkt_cfg.crc_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CRC_ENABLE__hex_,tx_pkt_cfg.crc_enb);
    DEBUG_CPRINT(("\n packet length cfg: %x",tx_pkt_cfg.pkt_len_cfg));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH_CFG__hex_,tx_pkt_cfg.pkt_len_cfg);
    DEBUG_CPRINT(("\n device address: %x",tx_pkt_cfg.device_addr));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEVICE_ADDRESS__hex_,tx_pkt_cfg.device_addr);


    os_memcpy(&comms_uhf_csw_rx_buf[TM_PLD_IDX],&tx_pkt_cfg,sizeof(s_uhf_rf_tx_pkt_cfg));
}

/**
 * @brief Get UHF receiver packet configuration.
 * 
 * Retrieves the current UHF receiver packet configuration.
 */
void sdr_uhf_get_rx_pkt_cfg(void)
{
    s_uhf_rf_rx_pkt_cfg rx_pkt_cfg;

    rx_pkt_cfg.demod_sync_word = (uhf_pkt_cfg_rb.sync0) | (uhf_pkt_cfg_rb.sync1 << 8);
    rx_pkt_cfg.max_pkt_len = uhf_pkt_cfg_rb.pktlen;
    rx_pkt_cfg.whitening_enb = uhf_pkt_cfg_rb.white_data ;
    rx_pkt_cfg.pkt_fmt = uhf_pkt_cfg_rb.pkt_format;
    rx_pkt_cfg.crc_enb = uhf_pkt_cfg_rb.crc_en;
    rx_pkt_cfg.pkt_len_cfg = uhf_pkt_cfg_rb.length_config;
    rx_pkt_cfg.device_addr = uhf_pkt_cfg_rb.addr;

    DEBUG_CPRINT(("\n demod sync word: %x",rx_pkt_cfg.demod_sync_word));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEMOD_SYNC_WORD__hex_,rx_pkt_cfg.demod_sync_word);
    DEBUG_CPRINT(("\n packet length: %x",rx_pkt_cfg.max_pkt_len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH__hex_,rx_pkt_cfg.max_pkt_len);
    DEBUG_CPRINT(("\n whitening enable: %x",rx_pkt_cfg.whitening_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,WHITENING_ENABLE__hex_,rx_pkt_cfg.whitening_enb);
    DEBUG_CPRINT(("\n packet format: %x",rx_pkt_cfg.pkt_fmt));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_FORMAT__hex_,rx_pkt_cfg.pkt_fmt);
    DEBUG_CPRINT(("\n crc enable: %x",rx_pkt_cfg.crc_enb));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CRC_ENABLE__hex_,rx_pkt_cfg.crc_enb);
    DEBUG_CPRINT(("\n packet length cfg: %x",rx_pkt_cfg.pkt_len_cfg));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PACKET_LENGTH_CFG__hex_,rx_pkt_cfg.pkt_len_cfg);
    DEBUG_CPRINT(("\n device address: %x",rx_pkt_cfg.device_addr));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DEVICE_ADDRESS__hex_,rx_pkt_cfg.device_addr);

    os_memcpy(&comms_uhf_csw_rx_buf[TM_PLD_IDX],&rx_pkt_cfg,sizeof(s_uhf_rf_rx_pkt_cfg));
}


/**
 * @brief Get UHF power configuration.
 * 
 * Retrieves the current UHF power configuration.
 */
void sdr_uhf_get_pwr_cfg(void)
{
    s_uhf_rf_pwr_cfg pwr_cfg;

    pwr_cfg.max_dvga_gain  =uhf_pa_cfg_rb.max_dvga_gain    ;
    pwr_cfg.max_lna_gain   =uhf_pa_cfg_rb.max_lna_gain     ;
    pwr_cfg.lna_sel        =uhf_pa_cfg_rb.agc_lna_priority;
    pwr_cfg.filter_len     =uhf_pa_cfg_rb.filter_length    ;
    pwr_cfg.power_table_sel=uhf_pa_cfg_rb.frend0           ;
    pwr_cfg.rf_output_power=uhf_pa_cfg_rb.pa_table0        ;

    DEBUG_CPRINT(("\n max dvga gain: %x",pwr_cfg.max_dvga_gain));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MA_DVGA_GAIN__hex_,pwr_cfg.max_dvga_gain);
    DEBUG_CPRINT(("\n max lna gain: %x",pwr_cfg.max_lna_gain));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MAX_LNA_GAIN__hex_,pwr_cfg.max_lna_gain);
    DEBUG_CPRINT(("\n lna sel: %x",pwr_cfg.lna_sel));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,LNA_SEL__hex_,pwr_cfg.lna_sel);
    DEBUG_CPRINT(("\n filterlen: %x",pwr_cfg.filter_len));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FILTERLEN__hex_,pwr_cfg.filter_len);
    DEBUG_CPRINT(("\n power table sel: %x",pwr_cfg.power_table_sel));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,POWER_TABLE__hex_,pwr_cfg.power_table_sel);
    DEBUG_CPRINT(("\n output power: %x",pwr_cfg.rf_output_power));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,OUTPUT_PPOWER__hex_,pwr_cfg.rf_output_power);

    os_memcpy(&comms_uhf_csw_rx_buf[TM_PLD_IDX],&pwr_cfg,sizeof(s_uhf_rf_pwr_cfg));
}

/**
 * @brief Get UHF RF transmitter configuration.
 * 
 * Retrieves the current UHF RF transmitter configuration.
 */
void sdr_uhf_get_rf_tx_cfg(void)
{
    float reg_cal = 0;
    float reg_cal1 = 0;
    uint32_t freq = 0;
    s_uhf_rf_tx_cfg rb_tx_cfg;

    freq = uhf_rf_cfg_rb.freq0 |(uhf_rf_cfg_rb.freq1<<8)|(uhf_rf_cfg_rb.freq2<<16);

    rb_tx_cfg.chan_num = uhf_rf_cfg_rb.channr;
    rb_tx_cfg.mod_type = uhf_rf_cfg_rb.mod_format;
    rb_tx_cfg.sync_mode = uhf_rf_cfg_rb.sync_mode;
    rb_tx_cfg.enb_dc_filter = uhf_rf_cfg_rb.dem_dcfilt_off;
    rb_tx_cfg.manchester_enable = uhf_rf_cfg_rb.manchester_en;
    rb_tx_cfg.fec_enb = uhf_rf_cfg_rb.fec_en;
    rb_tx_cfg.preamble_num = uhf_rf_cfg_rb.num_preamble;

    reg_cal = ((float)XO_FREQ/(float)(1<<16));
    reg_cal1 = (uhf_rf_cfg_rb.channr * (256 + uhf_rf_cfg_rb.chanspc_m)* (1<<uhf_rf_cfg_rb.chanspc_e));

    rb_tx_cfg.base_freq = reg_cal * ((float)freq + reg_cal1);

    //rb_tx_cfg.base_freq = (XO_FREQ/(1<<16))*(freq + (uhf_rf_cfg_rb.channr * (256 + uhf_rf_cfg_rb.chanspc_m)* (1<<uhf_rf_cfg_rb.chanspc_e)));

    rb_tx_cfg.if_freq = (float)XO_FREQ*((float)((float)(uhf_rf_cfg_rb.fsctrl1 & MASK_5_BITS)/(1<<10)));
    DEBUG_CPRINT(("\n uhf_rf_cfg_rb.fsctrl1: %x",uhf_rf_cfg_rb.fsctrl1));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_RB_FSCTRL1__hex_,uhf_rf_cfg_rb.fsctrl1);
    rb_tx_cfg.chan_spacing = ((float)XO_FREQ/(1<<18))*((256 + uhf_rf_cfg_rb.chanspc_m)* (1<<uhf_rf_cfg_rb.chanspc_e));
    rb_tx_cfg.data_rate = (float)((float)((float)(256 + uhf_rf_cfg_rb.drate_m)* (1<<uhf_rf_cfg_rb.drate_e))/(1<<28))* (float)XO_FREQ;
    rb_tx_cfg.channel_bw =  (float)XO_FREQ/(8*(4+uhf_rf_cfg_rb.chanbw_m)* (1<<uhf_rf_cfg_rb.chanbw_e));
    rb_tx_cfg.freq_dev =  ((float)XO_FREQ/(1<<17))*((8+uhf_rf_cfg_rb.deviation_m)* (1<< uhf_rf_cfg_rb.deviation_e));
    rb_tx_cfg.freq_ofst = ((float)uhf_rf_cfg_rb.fsctrl0 * (float)XO_FREQ)/(1<<14);

    DEBUG_CPRINT(("\nuhf_rf_cfg_rb.channr %d, rb_tx_cfg.chan_num %d", uhf_rf_cfg_rb.channr, rb_tx_cfg.chan_num));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_RB_CHANNR__u8__RB_TX_CFG_RB_CHAN_NUM__u8_,uhf_rf_cfg_rb.channr, rb_tx_cfg.chan_num);
    DEBUG_CPRINT(("\nrb_tx_cfg.mod_type %d",rb_tx_cfg.mod_type));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MOD_TYPE__u8_,rb_tx_cfg.mod_type);
    DEBUG_CPRINT(("\nrb_tx_cfg.sync_mode %d",rb_tx_cfg.sync_mode));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SYNC_MOD__u8_,rb_tx_cfg.sync_mode);
    DEBUG_CPRINT(("\nrb_tx_cfg.enb_dc_filter %d",rb_tx_cfg.enb_dc_filter));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ENB_DC_FILTER__u8_,rb_tx_cfg.enb_dc_filter);
    DEBUG_CPRINT(("\nrb_tx_cfg.manchester_enable %d",rb_tx_cfg.manchester_enable));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MANCHESTER_ENABLE__u8_,rb_tx_cfg.manchester_enable);
    DEBUG_CPRINT(("\nrb_tx_cfg.base_freq %f",rb_tx_cfg.base_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,BASE_FREQ__u32_,rb_tx_cfg.base_freq);
    DEBUG_CPRINT(("\nrb_tx_cfg.if_freq %f",rb_tx_cfg.if_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,IF_FREQ__u32_,rb_tx_cfg.if_freq);
    DEBUG_CPRINT(("\nrb_tx_cfg.chan_spacing %f",rb_tx_cfg.chan_spacing));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHAN_SPACING__u32_,rb_tx_cfg.chan_spacing);
    DEBUG_CPRINT(("\nrb_tx_cfg.data_rate %f",rb_tx_cfg.data_rate));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DATA_RATE__u32_,rb_tx_cfg.data_rate);
    DEBUG_CPRINT(("\nrb_tx_cfg.channel_bw %f",rb_tx_cfg.channel_bw));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CHANNEL_BW__u32_,rb_tx_cfg.channel_bw);
    DEBUG_CPRINT(("\nrb_tx_cfg.freq_dev %f",rb_tx_cfg.freq_dev));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FREQ_DEV__u32_,rb_tx_cfg.freq_dev);
    DEBUG_CPRINT(("\nrb_tx_cfg.freq_ofst %f",rb_tx_cfg.freq_ofst));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FREQ_OFFSET__u32_,rb_tx_cfg.freq_ofst);

    DEBUG_CPRINT(("\n tx channel spacing_e: %x,channel spacing_m: %x",uhf_rf_cfg_rb.chanspc_e,uhf_rf_cfg_rb.chanspc_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,TX_CHANNEL_SPACING_E__hex__CHANNEL_SPACING_M__hex_,uhf_rf_cfg_rb.chanspc_e,uhf_rf_cfg_rb.chanspc_m);
    DEBUG_CPRINT(("\n tx data rate_e: %x,data rate_m: %x",uhf_rf_cfg_rb.drate_e,uhf_rf_cfg_rb.chanbw_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,TX_DATA_RATE_E__hex__DATA_RATE_M__hex_,uhf_rf_cfg_rb.drate_e,uhf_rf_cfg_rb.drate_m);
    DEBUG_CPRINT(("\n tx channel bandwidth_e: %x,channel bandwidth_m: %x",uhf_rf_cfg_rb.chanbw_e,uhf_rf_cfg_rb.chanbw_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,TX_CHANNEL_BW_E__hex__CHANNEL_BW_M__hex_,uhf_rf_cfg_rb.chanbw_e,uhf_rf_cfg_rb.chanbw_m);
    DEBUG_CPRINT(("\n tx freq deviation_e: %x,freq deviation_m: %x",uhf_rf_cfg_rb.deviation_e,uhf_rf_cfg_rb.deviation_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,TX_FREQ_DEVIATION_E__hex__TX_FREQ_DEVIATION_M__hex_,uhf_rf_cfg_rb.deviation_e,uhf_rf_cfg_rb.deviation_m);

    os_memcpy(&comms_uhf_csw_rx_buf[TM_PLD_IDX],&rb_tx_cfg,sizeof(s_uhf_rf_tx_cfg));
}

/**
 * @brief Get UHF RF receiver configuration.
 * 
 * Retrieves the current UHF RF receiver configuration. 
 */
void sdr_uhf_get_rf_rx_cfg(void)
{
    float reg_cal = 0;
    float reg_cal1 = 0;
    uint32_t freq = 0;
    s_uhf_rf_rx_cfg rb_rx_cfg;
    freq = uhf_rf_cfg_rb.freq0 |(uhf_rf_cfg_rb.freq1<<8)|(uhf_rf_cfg_rb.freq2<<16);

    rb_rx_cfg.rx_sync_time_out = uhf_rf_cfg_rb.rx_timeout;
    rb_rx_cfg.rx_attenuation = uhf_rf_cfg_rb.rx_attenuation;
    rb_rx_cfg.chan_num = uhf_rf_cfg_rb.channr;
    rb_rx_cfg.mod_type = uhf_rf_cfg_rb.mod_format;
    rb_rx_cfg.sync_mode = uhf_rf_cfg_rb.sync_mode;
    rb_rx_cfg.enb_dc_filter = uhf_rf_cfg_rb.dem_dcfilt_off;
    rb_rx_cfg.manchester_enable = uhf_rf_cfg_rb.manchester_en;
    rb_rx_cfg.fec_enb = uhf_rf_cfg_rb.fec_en;
    rb_rx_cfg.preamble_num = uhf_rf_cfg_rb.num_preamble;
    rb_rx_cfg.freq_ofst = (float)((float)uhf_rf_cfg_rb.fsctrl0 * (float)XO_FREQ)/(1<<14);

    reg_cal = ((float)XO_FREQ/(float)(1<<16));
    reg_cal1 = (uhf_rf_cfg_rb.channr * (256 + uhf_rf_cfg_rb.chanspc_m)* (1<<uhf_rf_cfg_rb.chanspc_e));

    rb_rx_cfg.base_freq = reg_cal * ((float)freq + reg_cal1);

    //rb_tx_cfg.base_freq = (XO_FREQ/(1<<16))*(freq + (uhf_rf_cfg_rb.channr * (256 + uhf_rf_cfg_rb.chanspc_m)* (1<<uhf_rf_cfg_rb.chanspc_e)));

    rb_rx_cfg.if_freq = (float)XO_FREQ*((float)((float)(uhf_rf_cfg_rb.fsctrl1 & MASK_5_BITS)/(1<<10)));
    DEBUG_CPRINT(("\n uhf_rf_cfg_rb.fsctrl1: %x",uhf_rf_cfg_rb.fsctrl1));
    rb_rx_cfg.chan_spacing = ((float)XO_FREQ/(1<<18))*((256 + uhf_rf_cfg_rb.chanspc_m)* (1<<uhf_rf_cfg_rb.chanspc_e));
    rb_rx_cfg.data_rate = (float)((float)((float)(256 + uhf_rf_cfg_rb.drate_m)* (1<<uhf_rf_cfg_rb.drate_e))/(1<<28))* (float)XO_FREQ;
    rb_rx_cfg.channel_bw =  (float)XO_FREQ/(8*(4+uhf_rf_cfg_rb.chanbw_m)* (1<<uhf_rf_cfg_rb.chanbw_e));
    rb_rx_cfg.freq_dev =  ((float)XO_FREQ/(1<<17))*((8+uhf_rf_cfg_rb.deviation_m)* (1<< uhf_rf_cfg_rb.deviation_e));

    DEBUG_CPRINT(("\nuhf_rf_cfg_rb.channr %d, rb_rx_cfg.chan_num %d", uhf_rf_cfg_rb.channr, rb_rx_cfg.chan_num));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,UHF_RF_CFG_RB_CHANNR__u8__RB_RX_CFG_RB_CHAN_NUM__u8_,uhf_rf_cfg_rb.channr, rb_rx_cfg.chan_num);
    DEBUG_CPRINT(("\nrb_rx_cfg.mod_type %d",rb_rx_cfg.mod_type));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_MOD_TYPE__u8_,rb_rx_cfg.mod_type);
    DEBUG_CPRINT(("\nrb_rx_cfg.sync_mode %d",rb_rx_cfg.sync_mode));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_RXSYNC_MOE__u8_,rb_rx_cfg.sync_mode);
    DEBUG_CPRINT(("\nrb_rx_cfg.enb_dc_filter %d",rb_rx_cfg.enb_dc_filter));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_ENB_DC_FILTER__u8_,rb_rx_cfg.enb_dc_filter);
    DEBUG_CPRINT(("\nrb_rx_cfg.manchester_enable %d",rb_rx_cfg.manchester_enable));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_MANCHESTER_ENABLE__u8_,rb_rx_cfg.manchester_enable);
    DEBUG_CPRINT(("\nrb_rx_cfg.base_freq %f",rb_rx_cfg.base_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_BASE_FREQ__u32_,rb_rx_cfg.base_freq);
    DEBUG_CPRINT(("\nrb_rx_cfg.if_freq %f",rb_rx_cfg.if_freq));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_IF_FREQ__u32_,rb_rx_cfg.if_freq);
    DEBUG_CPRINT(("\nrb_rx_cfg.chan_spacing %f",rb_rx_cfg.chan_spacing));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_CHAN_SPACING__u32_,rb_rx_cfg.chan_spacing);
    DEBUG_CPRINT(("\nrb_rx_cfg.data_rate %f",rb_rx_cfg.data_rate));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_DATA_RATE__u32_,rb_rx_cfg.data_rate);
    DEBUG_CPRINT(("\nrb_rx_cfg.channel_bw %f",rb_rx_cfg.channel_bw));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_CHANNEL_BW__u32_,rb_rx_cfg.channel_bw);
    DEBUG_CPRINT(("\nrb_rx_cfg.freq_dev %f",rb_rx_cfg.freq_dev));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_FREQ_DEV__u32_,rb_rx_cfg.freq_dev);
    DEBUG_CPRINT(("\nrb_tx_cfg.freq_ofst %f",rb_rx_cfg.freq_ofst));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_FREQ_OFFSET__u32_,rb_rx_cfg.freq_ofst);
    DEBUG_CPRINT(("\nrb_rx_cfg.rx_sync_time_out %f",rb_rx_cfg.rx_sync_time_out));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_SYNC_TIMEOUT__u32_,rb_rx_cfg.rx_sync_time_out);
    DEBUG_CPRINT(("\nrb_rx_cfg.rx_attenuation %d",rb_rx_cfg.rx_attenuation));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RX_ATTENUATION__u32_,rb_rx_cfg.rx_attenuation);

    DEBUG_CPRINT(("\n rx channel spacing_e: %x,channel spacing_m: %x",uhf_rf_cfg_rb.chanspc_e,uhf_rf_cfg_rb.chanspc_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,RX_CHANNEL_SPACING_E__hex__CHANNEL_SPACING_M__hex_,uhf_rf_cfg_rb.chanspc_e,uhf_rf_cfg_rb.chanspc_m);

    DEBUG_CPRINT(("\n rx data rate_e: %x,data rate_m: %x",uhf_rf_cfg_rb.drate_e,uhf_rf_cfg_rb.chanbw_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,RX_DATA_RATE_E__hex__DATA_RATE_M__hex_,uhf_rf_cfg_rb.drate_e,uhf_rf_cfg_rb.drate_m);

    DEBUG_CPRINT(("\n rx channel bandwidth_e: %x,channel bandwidth_m: %x",uhf_rf_cfg_rb.chanbw_e,uhf_rf_cfg_rb.chanbw_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,RX_CHANNEL_BW_E__hex__CHANNEL_BW_M__hex_,uhf_rf_cfg_rb.chanbw_e,uhf_rf_cfg_rb.chanbw_m);

    DEBUG_CPRINT(("\n rx freq deviation_e: %x,freq deviation_m: %x",uhf_rf_cfg_rb.deviation_e,uhf_rf_cfg_rb.deviation_m));
    log_id_with_two_arg(LOG_INFO_L1,LOG_COMMS,RX_FREQ_DEVIATION_E__hex__TX_FREQ_DEVIATION_M__hex_,uhf_rf_cfg_rb.deviation_e,uhf_rf_cfg_rb.deviation_m);


    os_memcpy(&comms_uhf_csw_rx_buf[TM_PLD_IDX],&rb_rx_cfg,sizeof(s_uhf_rf_rx_cfg));
}

/**
 * @brief  This function is used to get CSP - TM property of CSP library
 */
//void sdr_get_csp_tm_prop(s_comms_uhf_csp_tm  *csp_tm, uint16_t *size)
//{
//csp_upd_tm();

// os_memcpy(csp_tm, &csp_tm_global, sizeof(s_comms_uhf_csp_tm));

/*    DEBUG_CPRINT(("\ncsp_tm->csp_int[0].mtu %d",csp_tm->csp_int[0].mtu));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].tx %d",csp_tm->csp_int[0].tx));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].rx %d",csp_tm->csp_int[0].rx));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].tx_error %d",csp_tm->csp_int[0].tx_error));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].rx_error %d",csp_tm->csp_int[0].rx_error));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].drop %d",csp_tm->csp_int[0].drop));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].autherr %d",csp_tm->csp_int[0].autherr));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].frame %d", csp_tm->csp_int[0].frame));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].txbytes %d", csp_tm->csp_int[0].txbytes));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[0].rxbytes %d",csp_tm->csp_int[0].rxbytes));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].mtu %d",csp_tm->csp_int[1].mtu));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].tx %d",csp_tm->csp_int[1].tx));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].rx %d",csp_tm->csp_int[1].rx));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].tx_error %d",csp_tm->csp_int[1].tx_error));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].rx_error %d",csp_tm->csp_int[1].rx_error));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].drop %d",csp_tm->csp_int[1].drop));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].autherr %d",csp_tm->csp_int[1].autherr));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].frame %d", csp_tm->csp_int[1].frame));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].txbytes %d", csp_tm->csp_int[1].txbytes));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[1].rxbytes %d",csp_tm->csp_int[1].rxbytes));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].mtu %d",csp_tm->csp_int[2].mtu));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].tx %d",csp_tm->csp_int[2].tx));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].rx %d",csp_tm->csp_int[2].rx));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].tx_error %d",csp_tm->csp_int[2].tx_error));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].rx_error %d",csp_tm->csp_int[2].rx_error));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].drop %d",csp_tm->csp_int[2].drop));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].autherr %d",csp_tm->csp_int[2].autherr));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].frame %d", csp_tm->csp_int[2].frame));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].txbytes %d", csp_tm->csp_int[2].txbytes));
      DEBUG_CPRINT(("\ncsp_tm->csp_int[2].rxbytes %d",csp_tm->csp_int[2].rxbytes));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_nomem %d", csp_tm->csp_err_nomem));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_inval %d", csp_tm->csp_err_inval));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_timedout %d", csp_tm->csp_err_timedout));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_used %d", csp_tm->csp_err_used));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_notsup %d", csp_tm->csp_err_notsup));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_busy %d", csp_tm->csp_err_busy));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_already %d", csp_tm->csp_err_already));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_reset %d", csp_tm->csp_err_reset));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_nobufs %d", csp_tm->csp_err_nobufs));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_tx %d", csp_tm->csp_err_tx));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_driver %d", csp_tm->csp_err_driver));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_again %d", csp_tm->csp_err_again));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_hmac %d", csp_tm->csp_err_hmac));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_xtea %d", csp_tm->csp_err_xtea));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_crc32 %d", csp_tm->csp_err_crc32));
      DEBUG_CPRINT(("\ncsp_tm->csp_err_sfp %d", csp_tm->csp_err_sfp));*/
//}

/**
 * @brief Get UHF beacon data.
 * 
 * Retrieves the current UHF beacon data.
 * 
 * @return UHF beacon data array.
 */
uint8 comms_uhf_beacon_data()
{
	uhf_tx_beacon_data.len = sizeof(uhf_tx_beacon_data);
	uhf_tx_beacon_data.utc_tim = 0x5d4c49be;
	uhf_tx_beacon_data.utc_gps_tim = 0x01;
	uhf_tx_beacon_data.lat_noso = 'N';
	uhf_tx_beacon_data.lon_etwt = 'E';
	uhf_tx_beacon_data.lat = 53.116;
	uhf_tx_beacon_data.lon = 40.574;
	uhf_tx_beacon_data.alt = 23;
	return 0;
}

int8 dataarr[50];

/**
 * @brief Upload UHF firmware data.
 * 
 * Handles uploading new firmware data to the UHF radio.
 * 
 * @return Next sequence step for firmware upload.
 */ 
uint8_t uhf_fw_data_upload() {

  //  int8_t ret_sts = 0;
	//int8 hex_data[mem_size] = {0};
	memset(outbuff, 0xFF, mem_size);
 
	//ret_sts = comms_read_uhf_fw();
	//ret_sts =
#ifndef LINUX_TEMP_PORT
	comms_read_fw_info(COMMS_UHF_FW, &uhf_fw_buf);
 
	char *hex_data = (char*)uhf_fw_buf.src_addrs;

	for (int i = 0; i < 16; i++) {
		outbuff[27632+i] = asctohex(signature_key, i);
		DEBUG_CPRINT(("%x\t",outbuff[27632+i]));
	}

#if 0
	serial_port_init();

	file_ptr = fopen(filePath, "r");
    if (file_ptr == NULL)
	{
        perror("Error to open the file\n");
        exit(1);
    }


    fread(hex_data, sizeof(*hex_data), mem_size, file_ptr);
#endif

    int8* line;
    int addrb1 = 0;
    int addrb2 = 0;
    int checksum = 0;
    int adrbyt = 0;
    unsigned int byt_data = 0;
    unsigned int record_type = 0;
    line = strtok(hex_data, "\n");
    while (line != NULL) {
        line_count++;
        if (line[0] != ':') {
            DEBUG_CPRINT(("Can't read payload file - line %d doesn't start with ':'\n", line_count));
	    return 1;
        }
        int exp_checksum = 0;
        //byte_count = asctohex(&line[1],0);
        sscanf(&line[1], "%2x", &byte_count);
        if(!byte_count) {

        	sscanf(&line[7], "%2x", &record_type);

        	if (record_type)
        	{
        		return 0;
        	}
        	else return 1;
        }
      //  DEBUG_CPRINT(("the line count is %d, byte_count is %d\n", line_count, byte_count));
        //strtol(&line[1], NULL, 16);
        exp_checksum += byte_count;
        //int addrb1 = asctohex(&line[1],1); //strtol(&line[3], NULL, 16);
        sscanf(&line[3], "%2x", &adrbyt);
        //DEBUG_CPRINT(("the addr 1 is %x \n", addrb1));
        //int addrb2 = asctohex(&line[1],2); //strtol(&line[5], NULL, 16);
        sscanf(&line[5], "%2x", &addrb2);
        //DEBUG_CPRINT(("the addr 2 is %x \n", addrb2));
        addrb1 |= adrbyt;
        adrbyt = adrbyt << 8;
        adrbyt |= addrb2;
        //address = (addrb1 << 8) | addrb2;
		//DEBUG_CPRINT(("the address is %x \n", address));
        exp_checksum += addrb1 + addrb2;
        //record_type = asctohex(&line[1],3); //strtol(&line[7], NULL, 16);
        sscanf(&line[7], "%2x", &record_type);
        exp_checksum += record_type;
        if (record_type == 0) {


            for (int i = 0, j= 9; i < byte_count; i++) {
                //data[i] = asctohex(&line[1], i+4);
                sscanf(&line[j], "%2x", &byt_data);
                dataarr[i] = (int8)byt_data;
                j= j+2;
            }
            //checksum = asctohex(&line[1], byte_count+4);
            sscanf(&line[1+((byte_count+4)*2)], "%2x", &checksum);

            //DEBUG_CPRINT(("linelength is %hhu, %d \n",strlen(line), checksum);//strtol(&line[9 + 2 * byte_count], NULL, 16));
            if ((strlen(line)-1)/2 > byte_count + 5) {
                DEBUG_CPRINT(("extra bytes on line %d\n", line_count));
                return 2;
            }
     /*     for (int i = 0; i < byte_count; i++) {
                exp_checksum += dataarr[i];
            }
            DEBUG_CPRINT(("the chk_sum %x\n", exp_checksum));
            DEBUG_CPRINT(("the chk_sum %x\n", checksum));
            if ((exp_checksum & 0xff) != ((~checksum + 1) & 0xff)) {
                DEBUG_CPRINT(("Bad checksum on line %d\n", line_count));
                return 3;
            }
*/
        	   os_memcpy(&outbuff[adrbyt], dataarr, byte_count);

            total_data += byte_count;
        }
		else if (record_type == 1)  //End of File
            {return 0;}
        line = strtok(NULL, "\n");
    }

 /*   DEBUG_CPRINT(("\n\n"));
    for (uint32_t i = 27362; i < 16; i++) {
                DEBUG_CPRINT(("%x\t",outbuff[i]));
    }
	*/
#endif
#if 0

	format_serial_data();
	close(ser_fd);

#endif

    return 0;
}

/**
 * @brief Convert ASCII hex to integer.
 * 
 * Converts an ASCII hex character to its integer value.
 * 
 * @param[in] hex Pointer to ASCII hex char.
 * @param[in] ret_val Integer value to return.
 * @return Integer value of hex char.
 */
uint8_t asctohex(char* hex, uint8_t ret_val)
{
    int8_t len = strlen(hex);
    unsigned int bytes[len];
	memset(bytes,0, sizeof(bytes));

    for (int i = 0; i < len; i++) {
        sscanf(&hex[2*i], "%2x", &bytes[i]);
    }

   	return bytes[ret_val];
}

#if 0
uint8_t serial_port_rd_wr(uint8_t* data, uint8_t* reply, uint16_t data_len)
{

    // Write to serial port
  uint8_t msg[mem_size];// = { 'H', 'e', 'l', 'l', 'o', '\r' };
  memcpy(&msg[0],data,data_len );
  for (int i = 0; i < data_len; i++) {
          		DEBUG_CPRINT(("%x ", msg[i] ));
          		log_id_with_one_arg(LOG_INFO_L3,LOG_COMMS,MSG__u8_,msg[i]);

  }
  DEBUG_CPRINT(("\n\n"));

  int wr_len = write(ser_fd, msg, data_len);
  if (wr_len < 0) {
  	  perror("write:" );
        DEBUG_CPRINT(("Error writing: %s \n", strerror(errno)));
        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ERROR_WRITING__s8_,strerror(errno));

    }

  sleep(1);
  // Allocate memory for read buffer, set size according to your needs
  uint8_t read_buf [256];

  // Normally you wouldn't do this memset() call, but since we will just receive
  // ASCII data for this example, we'll set everything to 0 so we can
  // call printf() easily.
  memset(&read_buf, '\0', sizeof(read_buf));

  // Read bytes. The behaviour of read() (e.g. does it block?,
  // how long does it block for?) depends on the configuration
  // settings above, specifically VMIN and VTIME
  int num_bytes = read(ser_fd, &read_buf, 10);
  sleep(1);
  // num_bytes is the number of bytes read. num_bytes may be 0 if no bytes were received, and can also be -1 to signal an error.
  if (num_bytes < 0) {
	  perror("read:" );
	  DEBUG_CPRINT(("Error reading: %s \n", strerror(errno)));
	  log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ERROR_READING__s8_,strerror(errno));
	  return 1;
  }
  else if(num_bytes > 0){
	  // Here we assume we received ASCII data, but you might be sending raw bytes (in that case, don't try and
	  // print it to the screen like this!)
	  DEBUG_CPRINT(("Read %i bytes. Received message: %s \n", num_bytes, read_buf));
	  for(int i = 0; i < num_bytes; i++ )
	  {
		  DEBUG_CPRINT(("%x ",read_buf[i]));
	  }

	  tcflush(ser_fd, TCIFLUSH);
	  DEBUG_CPRINT(("\n\n"));
	  //if(&reply[0] != 0)
	  //{
		  DEBUG_CPRINT(("%x \n", read_buf[8]));
		  //int n =  memcmp(read_buf, reply, sizeof(reply));
		  if(read_buf[8] == 0x01)
		  {
			  return 0;
		  }
		  else if(read_buf[8] == 0x0F)
		  {
			  return 0;
		  }
		  else
		  {
			  return 1;
		  }
	  //}
  }

  //close(ser_fd);
  //return 0; // success
}
#endif
#if 0
uint8_t format_serial_data()
{
	uint8_t in_bootloader = 0;
	bool is_empty = false;
	uint8_t page_data[128];

	//int8 cmd_ret;
	int8 l_buff[138]= {0};
	uint8_t page_number;
	uint8_t ret_succ;
	//uint8_t data_len = 138;

	memset(&snd_pack, '\0', sizeof(s_seri_pack_send_t));
	snd_pack.sync_word = 0x6922;
	snd_pack.lngth = 87;
	snd_pack.hwid = 0x0001;
	snd_pack.seq = 0x0000;
	snd_pack.dst = 0x01;

	while (!in_bootloader)
	{
		//cmd_char1 = "lst reboot";
		DEBUG_CPRINT(("lst reboot \n"));
		snd_pack.lngth = 6;
		snd_pack.seq++;
		snd_pack.cmd = UHF_RADIO_MSG_REBOOT;
		memcpy(l_buff,&snd_pack, (sizeof(snd_pack) - sizeof(snd_pack.hx_data))); //len 9
#if 0
		send_cmd_once(l_buff, (sizeof(snd_pack) - sizeof(snd_pack.hx_data))); //len 9
		ret_sts = uhf_uart_boot_wr_rsp();
		if (ret_sts != BOOT_WR_FAIL)
		{
			UHF_FW_UP = 1; //succ
		}
		else UHF_FW_UP = 0;
#endif
		uhf_fw_update_fsm(UHF_RADIO_MSG_REBOOT,l_buff,(sizeof(snd_pack) - sizeof(snd_pack.hx_data))); //len 9

#if 0
		usleep(200000);
#endif
		os_delay(200);
		DEBUG_CPRINT(("lst bootloader_erase \n"));
		snd_pack.lngth = 6;
		snd_pack.seq++;
		snd_pack.dst = 0x01;
		snd_pack.cmd = UHF_BOOTLOADER_MSG_ERASE;
		memcpy(l_buff,&snd_pack, (sizeof(snd_pack) - sizeof(snd_pack.hx_data))); //len 9
		//cmd_ret =  UHF_BOOTLOADER_MSG_ACK;
#if 0
		in_bootloader = send_cmd_resp(l_buff, cmd_ret, (sizeof(snd_pack) - sizeof(snd_pack.hx_data)), 1); //len 9//
		ret_sts = uhf_uart_boot_wr_rsp();
		if (ret_sts != BOOT_WR_FAIL)
		{
			UHF_FW_UP = 1; //succ
		}
		else UHF_FW_UP = 0;


#endif
		uhf_fw_update_fsm(UHF_BOOTLOADER_MSG_ERASE,l_buff,(sizeof(snd_pack) - sizeof(snd_pack.hx_data))); //len 9
		in_bootloader = UHF_FW_UP;
		os_delay(200);
#if 0
		usleep(200000);
#endif
	}

	for (int page = FLASH_APP_START; page <= FLASH_APP_END; page += FLASH_PAGE_SIZE)
	{
		page_number = page / FLASH_PAGE_SIZE;

		memcpy(&page_data[0], &outbuff[page], FLASH_PAGE_SIZE);

		snd_pack.lngth = 0x87;
		snd_pack.seq++;
		snd_pack.dst = 0x01;
		snd_pack.cmd = UHF_BOOTLOADER_MSG_WRITE_PAGE;
		snd_pack.hx_data[0] = page_number;
		memcpy(&snd_pack.hx_data[1], &page_data[0], FLASH_PAGE_SIZE);

		memcpy(l_buff,&snd_pack,sizeof(snd_pack)); //len 138
		int null_cnt = 0;
		if (page_data[0] == 0xff) {
			for (int i = 0; i < FLASH_PAGE_SIZE; i++) {
				if (page_data[i] == 0xff){
					null_cnt = i;
				}
				else {
					is_empty = false;
					break;
				}
			}
			if(null_cnt == FLASH_PAGE_SIZE -1 )
			{
				is_empty = true;
				DEBUG_CPRINT(("Page %d is writing \n", page_number));
				log_id_with_one_arg(LOG_INFO_L3,LOG_COMMS,PAGE__u8_,page_number);
			}
		}

		DEBUG_CPRINT(("\n\n"));

		if (is_empty) {
			DEBUG_CPRINT(("Page %d is empty, skipping \n", page_number));
			log_id_with_one_arg(LOG_INFO_L3,LOG_COMMS,PAGE__u8_,page_number);
			continue;
		}
		//char* hex_data = hexlify(page_data, FLASH_PAGE_SIZE);
		//cmd_char1 = "lst bootloader_write_page";
		//cmd_char = "lst bootloader_ack";
		//cmd_ret =  UHF_BOOTLOADER_MSG_ACK;

		DO_AGAIN:
#if 0
		ret_succ = send_cmd_resp(l_buff, cmd_ret, sizeof(snd_pack), 1); //len 138
#else

		uhf_fw_update_fsm(UHF_BOOTLOADER_MSG_WRITE_PAGE,l_buff, sizeof(snd_pack)); //len 138
		ret_succ = UHF_FW_UP;
#endif
		if (ret_succ != 1)
		{
			goto DO_AGAIN;
		}
	}

	snd_pack.lngth = 0x07;
	snd_pack.seq++;
	snd_pack.dst = 0x01;
	snd_pack.cmd = UHF_BOOTLOADER_MSG_WRITE_PAGE;
	snd_pack.hx_data[0] = 0xFF;
	memcpy(l_buff,&snd_pack,10);
	//cmd_ret = 0x0F;
#if 0
	send_cmd_resp(l_buff, cmd_ret, (sizeof(snd_pack) - sizeof(snd_pack.hx_data)), 1); //len 9
#else

	uhf_fw_update_fsm(UHF_BOOTLOADER_MSG_ERASE,l_buff, (sizeof(snd_pack) - sizeof(snd_pack.hx_data)) );
	ret_succ = UHF_FW_UP;
#endif

	DEBUG_CPRINT(("lst bootloader_write_page 255 \n"));
	log_id(LOG_INFO_L3,LOG_COMMS,LST_BOOTLOADER_WRITE_PAGE_255);

	return 1;
}


// Function to send command once
void send_cmd_once(int8* cmd, int8_t len) {

#if 0
	serial_port_rd_wr(cmd, 0, len);
#else
	//uhf_fw_write(cmd, 0, len);
#endif
}

// Function to send command with response
uint8_t send_cmd_resp(int8* cmd, uint8_t reply, int8_t len, int8_t ret) {
    // Implementation specific
#if 0
	if (serial_port_rd_wr(cmd, reply, len) == 1)
	{
		return 0; //nack
	}

#endif

#ifndef LINUX_TEMP_PORT
	if(uhf_fw_write(cmd, reply, len) == 1)
	{
		return 0; //nack
	}

#endif

return 1; //ack

}

// Function to send command
void send_cmd(uint8_t* cmd) {
    // Implementation specific
#if 0
	serial_port_rd_wr(cmd, 0, 30);
#endif

}

void uhf_fw_update_fsm(uint8_t fw_state_id, int8* pld, uint8 data_len)
{
#ifndef  LINUX_TEMP_PORT
	int ret_sts;
#endif
	switch(fw_state_id) {
	case UHF_RADIO_MSG_REBOOT:
#ifndef LINUX_TEMP_PORT
		uhf_upd_send_uart_cmd(UHF_RADIO_MSG_REBOOT);
		ret_sts = uhf_uart_boot_wr_rsp();
		if (ret_sts != BOOT_WR_FAIL)
		{
			UHF_FW_UP = 1; //succ
		}
		else UHF_FW_UP = 0;
#endif
	break;

	case UHF_BOOTLOADER_MSG_ERASE:
#ifndef LINUX_TEMP_PORT
		uhf_upd_send_uart_cmd(UHF_BOOTLOADER_MSG_ERASE);
		ret_sts = uhf_uart_boot_wr_rsp();
		if (ret_sts != BOOT_WR_FAIL)
		{
			UHF_FW_UP = 1; //succ
		}
		else UHF_FW_UP = 0;
#endif
	break;

	case UHF_BOOTLOADER_MSG_WRITE_PAGE:
		//uhf_upd_send_uart_cmd(UHF_BOOTLOADER_MSG_WRITE_PAGE);
		//ret_sts = uhf_uart_boot_wr_rsp();
#ifndef LINUX_TEMP_PORT
		uhf_fw_write(pld, 0, data_len);
		ret_sts = uhf_uart_boot_wr_rsp();
		if (ret_sts != BOOT_WR_FAIL)
		{
			UHF_FW_UP = 1; //succ
		}
		else UHF_FW_UP = 0;
#endif
	break;

	}

}


uint8_t uhf_uart_boot_wr_rsp()
{

#ifndef LINUX_TEMP_PORT
	if (uhf_uart_rsp_len)
	{
		DEBUG_CPRINT(("Read %i bytes. Received message: %s \n", uhf_uart_rsp_len, uhf_uart_rsp));
		for(int i = 0; i < uhf_uart_rsp_len; i++ )
		{
			DEBUG_CPRINT(("%x ",uhf_uart_rsp[i]));
		}

		DEBUG_CPRINT(("\n\n"));
		DEBUG_CPRINT(("%x \n", uhf_uart_rsp[8]));

		if(uhf_uart_rsp[8] == UHF_BOOTLOADER_MSG_ACK)//0x01)
		{
			fw_wr_succ = 0; //succ
		}
		else if(uhf_uart_rsp[8] == UHF_BOOTLOADER_MSG_NACK)//0x0F)
		{
			fw_wr_succ = 0; //succ
		}
		else if(uhf_uart_rsp[8] == UHF_CMN_MSG_ACK)//0xFF)
		{
			fw_wr_succ = 0; //succ
		}
		else
		{
			fw_wr_succ = 1; //fail
		}

		uhf_uart_rsp_len =0;
	}

	return fw_wr_succ;
#else
	return 0;
#endif
}



uint8_t serial_port_init()
{
	ser_fd  = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

	  // Read in existing settings, and handle any error
	  if(tcgetattr(ser_fd, &tty) != 0) {
	      perror("open:" );
	    DEBUG_CPRINT(("Error %i from tcgetattr: %s\n", errno, strerror(errno)));
	      return 1;
	  }

	  tty.c_cflag &= ~PARENB; // Clear parity bit
	  tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
	  tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
	  tty.c_cflag |= CS8; // 8 bits per byte
	  tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
	  tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)

	  tty.c_lflag &= ~ICANON;
	  tty.c_lflag &= ~ECHO; // Disable echo
	  tty.c_lflag &= ~ECHOE; // Disable erasure
	  tty.c_lflag &= ~ECHONL; // Disable new-line echo
	  tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
	  tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
	  tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes

	  tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
	  tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed


	  tty.c_cc[VTIME] = 10; //10;    // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
	  tty.c_cc[VMIN] = 0;

	  // Set in/out baud rate to be 115200
	  cfsetispeed(&tty, B115200);
	  cfsetospeed(&tty, B115200);

	  // Save tty settings, also checking for error
	  if (tcsetattr(ser_fd, TCSANOW, &tty) != 0) {
		  perror("set_ser_config;");
	      DEBUG_CPRINT(("Error %i from tcsetattr: %s\n", errno, strerror(errno)));
	      return 1;
	  }

	  return 0;
}
#endif

