
/**
 * @file comms_uhf_csw_fsm_hdlr.c
 *
 * @brief This file has the UHF driver FSM implemenation and the relevant functions
 *
 * @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 "sm_api.h"
#include "comms_uhf_sim.h"
#include "comms_uhf_init.h"
#include "comms_uhf_intf.h"
#include "comms_uhf_ipc.h"
#include "comms_uhf_main.h"
#include "exo_common.h"
#include "exo_io_al_sos_timer.h"
#include "comms_uhf_rx.h"
//#include "otsu_cblk_def.h"
#include "comms_uhf_csp.h"
#include "tc_tm_mngr.h"
#ifdef LINUX_TEMP_PORT
#include "comms_uhf_sim.h"
#endif
#include "comms_uhf_api_map.h"
#include "tc_tm_mngr.h"
#include "sdr_app_main.h"
#include "exo_io_al_rtc_common.h"
#include "exo_io_al_sos_timer.h"
#include "exo_tctm_ipc.h"
#include "otsu_tctm_man.h"
#include "comms_uhf_tmr.h"
#include "logger.h"
#include "comms_ftm.h"
#include "err_handler.h"

#define UHF_CMD_RSP_TIMEOUT_RECOVERY_TMR 2 * 60 * 1000 //HBTODO
#define UHF_CMD_RSP_TIMEOUT_CNT_THRES 20
uint8_t uhf_cmd_rsp_timeout_cnt = 0;
uint8_t uhf_reinit_ongoing = 0;
uint8_t uhf_fw_upd_sts = 0;


sys_time_date_s PLACE_IN_EXT_RAM_MEM  tim_get_u_fs;

s_sdr_gpass_beacon_cfg uhf_gpass_cfg={0};

extern uint32_t uhf_rep_cnt;


/** struct for UHF Trans Timer Interval **/
s_uhf_comms_timer PLACE_IN_EXT_RAM_MEM uhf_trans_timer;

extern uint8_t uhf_cmd_rsp_cnt;

extern os_timer_handle_ptr uhf_cmd_timeout;

extern uint8_t PLACE_IN_EXT_RAM_MEM uhf_tc_cmd_id ;

//** Scheduler based ground contact flag */
uint8_t PLACE_IN_EXT_RAM_MEM uhf_sch_gs_contact =0;

/** Timer used for tm store Periodicity Timer */
extern os_timer_handle_ptr uhf_tm_periodic_read;

/** Timer used for periodic transmission of Beacon signal */
extern os_timer_handle_ptr uhf_beacon_timer;

/** Timer used for enable transmission of Beacon signal */
extern os_timer_handle_ptr uhf_beacon_enb_timer;

/** Timer used for transmit ON duration */
extern os_timer_handle_ptr uhf_tx_tm_on;

/** Timer used for rsp timeout recovery timer */
extern os_timer_handle_ptr uhf_rsp_timeout_recovery_tmr;

/* Timer used for UHF cmd trigger delay */
extern os_timer_handle_ptr uhf_cmd_wait_timer;

/** OTSU Callback status */
uint8_t PLACE_IN_EXT_RAM_MEM comms_uhf_otsu_cb_sts = 0;

/** UHF module first ground contact status */
uint8_t PLACE_IN_EXT_RAM_MEM comms_uhf_first_gs_contact = 0;

/** UHF module ground contact status after OTSU reset */
uint8_t PLACE_IN_EXT_RAM_MEM comms_uhf_otsu_gs_contact = 0;

uint8_t PLACE_IN_EXT_RAM_MEM beacon_op = 2;

extern s_uhf_rf_tx_cfg PLACE_IN_EXT_RAM_MEM uhf_nvm_tx_cfg;
extern s_uhf_rf_rx_cfg PLACE_IN_EXT_RAM_MEM uhf_nvm_rx_cfg;
extern s_uhf_rf_tx_pkt_cfg PLACE_IN_EXT_RAM_MEM uhf_nvm_tx_pkt_cfg;
extern s_uhf_rf_rx_pkt_cfg PLACE_IN_EXT_RAM_MEM uhf_nvm_rx_pkt_cfg;
extern s_uhf_rf_pwr_cfg PLACE_IN_EXT_RAM_MEM uhf_nvm_pwr_cfg;
extern s_sdr_tmr_cfg PLACE_IN_EXT_RAM_MEM uhf_tmr_cfg;


s_comms_uhf_get_tm_prop PLACE_IN_EXT_RAM_MEM comms_uhf_tm_read;

uint8_t uhf_hk_upd_id = 0;
uint8_t is_uhf_hk_upd_fill = 0;
uint8_t uhf_siz_hk_id = 0;

s_sdr_beacon_pld uhf_beacon_data;

/** Declaration of S-Driver FSM function pointer */
typedef uint8_t (*CSW_SM_HDLR)(uint16_t msg_id, void *payload);

/** Definition of S-BAND Driver FSM function pointer */
CSW_SM_HDLR comms_uhf_fsm_hdlr[COMMS_UHF_MAX_NUM_STATE] = {\
    &comms_uhf_init_state_hdlr,\
        &comms_uhf_boot_state_hdlr,\
        &comms_uhf_dfu_state_hdlr,\
        &comms_uhf_cfg_state_hdlr,\
        &comms_uhf_calib_state_hdlr,\
        &comms_uhf_sleep_idle_state_hdlr,\
        &comms_uhf_active_state_hdlr,\
        &comms_uhf_safe_mode_state_hdlr,\
        &comms_uhf_error_state_hdlr,\
        &comms_uhf_bypass_state_hdlr\
};

/** UHF ground contact flag */
volatile uint8_t PLACE_IN_EXT_RAM_MEM uhf_gs_contact_valid = 0;
extern uint8_t comms_gscontact_confirm_status_flag;

/**
 * @brief This function check the uhf ground contact
 */
uint8_t is_uhf_gs_contact_valid(void)
{
    return uhf_gs_contact_valid;
}

/**
 * @brief This function update the uhf ground contact status
 * 
 * This function updates the global variable uhf_gs_contact_valid  
 * to indicate whether there is currently a valid ground contact through
 * the UHF communication system.
 * 
 * @param state Current state of the UHF communication system
 */
void uhf_gs_contact_status_upd(uint16_t state)
{
    if(state == UHF_TC_TM_ST_REQ)
    {
        uhf_gs_contact_valid = 1;
    }
    else if(state == UHF_TC_TM_STOP_REQ)
    {
        uhf_gs_contact_valid = 0;
    }

}

/**
 * @brief This function send the uhf ground contact status
 * 
 * @param state Current UHF ground contact state
 */
void uhf_gs_contact_status_upd_itc(uint16_t state, uint8_t radio_id)
{
    //uint8_t radio_id = COMMS_UHF;
    uint8 prev_val = uhf_gs_contact_valid;

    uhf_gs_contact_status_upd(state);

    if(uhf_gs_contact_valid != prev_val)
    {
        DEBUG_CPRINT(("\n TM Storage IPC %d",state));
        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,TM_STORAGE_IPC__u16_,state);
        uhf_send_ipc_pld(state,TM_STORAGE,sizeof(uint8),&radio_id);
    }
}

#ifndef LINUX_TEMP_PORT

/**
 * @brief This function send ground contact status to OTSU module
 *
 * This function sends the current UHF ground sync status to the 
 * OTSU module via IPC. This allows OTSU to know the sync status
 * for managing telemetry and commands.
 * 
 * @param sync_status Current UHF ground sync status
 */
void send_otsu_uhf_gssync_sts(uint8_t sts)
{
    os_itc_msg_handle_t msg_hdl;
    msg_hdl.src_entity 	= COMMS_UHF_CTLR;
    msg_hdl.pld_info 	= sts;
    msg_hdl.Msg_id 		= OTSU_GS_SYNC_RCV;
    msg_hdl.Msg_len 	= 0;
    msg_hdl.pld.pld_ptr = NULL;
    os_itc_msg_send(&msg_hdl,OBC_OTSU, os_send_itc_wait);
    comms_gscontact_confirm_status_flag = 0xAA;
}
#endif

/**
 * @brief This function handle comms initialization state
 *
 * This handler function implements the initialization state logic
 * for the UHF communication system. It performs required setup and
 * transitions to the next system state when initialization is complete.
 * 
 * @return Next state for the UHF state machine
 */
uint8_t comms_uhf_init_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_INIT_STATE;
    int32_t status = 0; //COMMS_UHF_IPC_OK;
    status = status;

    uint8_t *tc_tm_data;

    tc_tm_data = (uint8*)payload;
    //uint16_t rx_size = 0;
    //uint8_t *tc_tm_data;
    //tc_tm_data = (uint8*)payload;

    DEBUG_CPRINT(("received response for COMMS_UHF_INIT : %d \n", msg_id));
    log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,RECEIVED_RESPONSE_FOR_COMMS_UHF_INIT__u16_,msg_id);
    switch (msg_id)
    {
             /*TODO: remove all TC in this state*/

        case OBC_UHF_INIT_REQ:
            {
                comms_uhf_cfg_nvm_read();
                /** UHF TIMER ON for TM Periodic read **/
                uhf_hk_upd_id = 0;
                is_uhf_hk_upd_fill = 0;
                uhf_reinit_ongoing = 0;
                //os_timer_start(uhf_tm_periodic_read, uhf_tmr_cfg.sdr_tm_read_tmr);
                //     state = COMMS_UHF_ACTIVE_STATE;
                state = COMMS_UHF_CFG_STATE;

                status = sdr_uhf_set_tx_cfg_req((s_uhf_rf_tx_cfg *)(&uhf_nvm_tx_cfg));

                uhf_upd_send_uart_cmd(UHF_SET_TX_RADIO_CFG_REQ);
            }
            break;

        case OBC_UHF_DEINIT_REQ:
            {
                /** Nothing to be done */
            }
            break;

        case UHF_UART_CMD_RSP:
            {
                /** Nothing to be done */
            	 uhf_proc_uart_cmd_rsp(payload); //TODO to be removed
            }
            break;

              case SDR_APP_UHF_MH_REQ:
            {
                DEBUG_CPRINT(("\n SDR_APP_UHF_MH_REQ"));
                log_id(LOG_INFO_L2,LOG_COMMS,SDR_APP_UHF_MH_REQ);
               sdr_app_sdr_ctrl_uhf_mh_req(payload);

            }
            break;

            case UHF_TX_PERIODIC_BEACON_TMR_EXP:
            {
                DEBUG_CPRINT(("\n UHF_TX_PERIODIC_BEACON_TMR_EXP"));
                log_id(LOG_INFO_L1,LOG_COMMS,UHF_TX_PERIODIC_BEACON_TMR_EXP);
                uhf_send_ipc(UHF_SDR_APP_BEACON_DATA_REQ, SDR_APP);
            }
            break;
        case COMMS_UHF_REINIT_TMR_EXP:
        {
            uhf_reinit_exp_hdlr();
        }
        break;


        case SDR_APP_SDR_CTRL_SDR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_sdr_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_MODE_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_mode_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_TMR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tmr_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_RSSI_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_rssi_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_TM_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tm_cfg_req(payload);
            }
            break;

        case TC_COMMS_UHF_INITIAL_SYNC:
            {
                uhf_initial_sync_hdlr();
            }
            break;

        case OTSU_UHF_SYNC_REQ:
            {
                /** OTSU Call back - Success*/
                comms_uhf_otsu_cb_sts = 1;
            }
            break;
           
        case SCH_UHF_GS_ST_IND:
        {
            DEBUG_CPRINT(("\n SCH_UHF_GS_ST_IND in init state"));
            uhf_sch_gs_contact = 1;
        }
        break;
        
        case SCH_UHF_GS_STOP_IND:
        {
            DEBUG_CPRINT(("\n SCH_UHF_GS_STOP_IND in init state"));
            uhf_sch_gs_contact = 0;
            uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
        }
        break;

        case TC_COMMS_UHF_GET_TM_INFO:
                    {
                        uhf_upd_send_uart_cmd(UHF_RADIO_MSG_GET_TELEM);
                    }
                    break;

                case TC_COMMS_UHF_SET_CFG: //not used
                    {
                        //uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);

                    }
                    break;

                case TC_COMMS_UHF_SET_TX_CFG:
                    {
                        status = sdr_uhf_set_tx_cfg_req((s_uhf_rf_tx_cfg *)(&tc_tm_data[TC_PLD_IDX]));

                        uhf_upd_send_uart_cmd(UHF_SET_TX_RADIO_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_GET_TX_CFG:
                    {
                        uhf_upd_send_uart_cmd(UHF_GET_TX_RADIO_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_SET_RX_CFG:
                    {
                        status = sdr_uhf_set_rx_cfg_req((s_uhf_rf_rx_cfg *)(&tc_tm_data[TC_PLD_IDX]));

                        uhf_upd_send_uart_cmd(UHF_SET_RX_RADIO_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_GET_RX_CFG:
                    {
                        uhf_upd_send_uart_cmd(UHF_GET_RX_RADIO_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_SET_TX_PKT_CFG:
                    {
                        status = sdr_uhf_set_tx_pkt_cfg_req((s_uhf_rf_tx_pkt_cfg *)(&tc_tm_data[TC_PLD_IDX]));
                        uhf_upd_send_uart_cmd(UHF_SET_TX_PKT_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_GET_TX_PKT_CFG:
                    {
                        uhf_upd_send_uart_cmd(UHF_GET_TX_PKT_CFG_REQ);
                    }
                    break;
                case TC_COMMS_UHF_SET_RX_PKT_CFG:
                    {
                        status = sdr_uhf_set_rx_pkt_cfg_req((s_uhf_rf_rx_pkt_cfg *)(&tc_tm_data[TC_PLD_IDX]));
                        uhf_upd_send_uart_cmd(UHF_SET_RX_PKT_CFG_REQ);
                    }
                    break;
                case TC_COMMS_UHF_GET_RX_PKT_CFG:
                    {
                        uhf_upd_send_uart_cmd(UHF_GET_RX_PKT_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_SET_PWR_CFG:
                    {
                        status = sdr_uhf_set_pwr_cfg_req((s_uhf_rf_pwr_cfg *)(&tc_tm_data[TC_PLD_IDX]));
                        uhf_upd_send_uart_cmd(UHF_SET_PWR_CFG_REQ);
                    }
                    break;
                case TC_COMMS_UHF_GET_PWR_CFG:
                    {
                        uhf_upd_send_uart_cmd(UHF_GET_PWR_CFG_REQ);
                    }
                    break;

                case TC_COMMS_UHF_FW_CFG_ACTIVATE:
                    {

                    }
                    break;

                case TC_COMMS_UHF_BEACON_CFG:
                    {
                        memcpy(&uhf_trans_timer,&tc_tm_data[TC_PLD_IDX],sizeof(s_uhf_comms_timer));
                        uhf_upd_send_uart_cmd(UHF_SET_BCON_TIMER); // beacon timer
                        comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_CFG, 0 ,tc_tm_data);
                    }
                    break;

                case TC_COMMS_UHF_BEACON_TX_ST:
                    {
                        uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STRT);// beacon start cmd 38
                        comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_ST, 0 ,tc_tm_data);
                    }
                    break;

                case TC_COMMS_UHF_BEACON_TX_STOP:
                    {
                        uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);// beacon stop cmd 39
                        comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_STOP, 0 ,tc_tm_data);
                    }
                    break;

                case TC_COMMS_UHF_BEACON_DATA:
                    {
                        uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);// beacon transmission cmd 1C
                        comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_STOP, 0 ,tc_tm_data);
                    }
                    break;
              
               case SDR_APP_SDR_CTRL_GPASS_CFG:
               {
                sdr_app_sdr_ctrl_uhf_sdr_gpass_cfg_req(payload);
               }
                break;
            
                case TC_COMMS_UHF_CSP_TM_INFO:
                    {
                        /**TODO: CSP Debug information to be added */
                    }
                    break;

              /*  case TC_COMMS_UHF_SET_CALLSIGN:
                    {
                        uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CALLSIGN);
                    }
                    break;

                case TC_COMMS_UHF_GET_CALLSIGN:
                    {
                        uhf_upd_send_uart_cmd(UHF_RADIO_MSG_GET_CALLSIGN);
                    }
                    break;*/

                case TC_COMMS_UHF_FW_CFG:
                    SDR_UHF_FIRMWARE_CFG(&tc_tm_data[TC_PLD_IDX]);
                    break;

        case SDR_APP_UHF_BEACON_DATA_IND:
            {
                //comms_uhf_beacon_tx(payload,sizeof(s_sdr_beacon_pld));
            }
            break;

        case TC_COMMS_UHF_GET_FW_STS:
                   {

                       uint32_t size,crc;
                       os_memcpy(&size,&tc_tm_data[TC_PLD_IDX],4);
                       os_memcpy(&crc,&tc_tm_data[TC_PLD_IDX+4],4);
                       /*DEBUG_CPRINT(("comms fw size %ld\n",size));
                       DEBUG_CPRINT(("comms fw crc %lx\n",crc));*/
       #ifndef LINUX_TEMP_PORT
                       comms_get_firmware_availablity_status(COMMS_UHF_FW,size,crc);
       #endif
                       comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_GET_FW_STS, 0 ,tc_tm_data);
            }
            break;

        default:
            state = COMMS_UHF_INIT_STATE;
            break;
    }

    return state;
}

/**
 * @brief This function handle the comms boot state
 * 
 * This handler function implements the initialization state logic
 * for the UHF communication system. It performs required setup and
 * transitions to the next system state when initialization is complete.
 * 
 * @return Next state for the UHF state machine
 */
uint8_t comms_uhf_boot_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_BOOT_STATE;
    int8_t status = COMMS_UHF_IPC_OK;


	DEBUG_CPRINT(("\n\r [UHF] Command response --- CMD_ID : %x \n",uhf_tc_cmd_id));
    DEBUG_CPRINT(("\nreceived response for COMMS_UHF_BOOT : %d \n", msg_id));

    switch (msg_id)
    {
        /* while fw update ongoing, module shall not goes off 
         * since before fw update SDR will check the EPS pwr sts
         */
        /* timeout recovery case handling */
        case OBC_UHF_DEINIT_REQ:
        {
            if(uhf_reinit_ongoing)
            {
                comms_uhf_cfg_nvm_write();

                uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
            }
            uhf_send_ipc(UHF_OBC_DEINIT_RSP,OBC_MAIN);
        }
        break;
        
        /* timeout recovery case handling */
        case OBC_UHF_INIT_REQ:
        {
            if(uhf_reinit_ongoing)
            {
                /* after receovery trigger FW update */
                uhf_send_ipc(UHF_FW_CFG_UPD_IND, COMMS_UHF_CTLR);
            }
        }
        
        case COMMS_UHF_REINIT_TMR_EXP:
        {
            uhf_reinit_exp_hdlr();
        }
        break;

		case UHF_FW_CFG_UPD_IND:
        {
			uhf_upd_send_uart_cmd(UHF_RADIO_MSG_REBOOT);

			os_timer_start(uhf_cmd_timeout, UHF_CMD_REB_TIMER);
			DEBUG_CPRINT(("\n\r TIME START - UPD IND "));
		}
		break;

		case UHF_UART_CMD_RSP_TIMEOUT:
		{
			if(UHF_RADIO_MSG_REBOOT == uhf_tc_cmd_id)
			{
				uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);
			}

			if (UHF_SET_BCON_CFG_STP == uhf_tc_cmd_id)
			{
				uhf_upd_send_uart_cmd(UHF_BOOTLOADER_MSG_ERASE);
			}

			if(UHF_BOOTLOADER_MSG_ERASE == uhf_tc_cmd_id)
			{
				uhf_upd_send_uart_cmd(UHF_RADIO_MSG_REBOOT);
			}

			os_timer_start(uhf_cmd_timeout, UHF_CMD_REB_TIMER);
			DEBUG_CPRINT(("\n\r TIME_OUT - RSP TIM_OUT"));

            uhf_cmd_rsp_timeout_cnt++; 
            comms_uhf_cmd_rsp_timeout_recovery_handling();
		}
		break;

        case UHF_UART_CMD_RSP:
        {
            uint8_t cmd_id = 0;
            uhf_cmd_rsp_timeout_cnt = 0;
            os_timer_stop(uhf_cmd_timeout);

            if(!uhf_fw_upd_sts)
            {
                status = uhf_fw_update_seq(payload);
                DEBUG_CPRINT(("\n\r TIME_STOP - CMD_RSP"));

                if(uhf_fw_upd_sts)
                {
                    os_timer_start(uhf_cmd_wait_timer,5000);
                }
                else
                {
                    //Nothing to do
                }
            }
            /* FW update completed, trigger CFG sequence */
            else
            {
                cmd_id = uhf_proc_uart_cmd_rsp(payload);
                DEBUG_CPRINT(("\n\r BOOT ST AFT FW CMD RSP: %d",cmd_id));

                if(UHF_SET_TX_RADIO_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_rx_cfg_req((s_uhf_rf_rx_cfg *)(&uhf_nvm_rx_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_RX_RADIO_CFG_REQ);
                }
                else if(UHF_SET_RX_RADIO_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_tx_pkt_cfg_req((s_uhf_rf_tx_pkt_cfg *)(&uhf_nvm_tx_pkt_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_TX_PKT_CFG_REQ);
                }
                else if(UHF_SET_TX_PKT_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_rx_pkt_cfg_req((s_uhf_rf_rx_pkt_cfg *)(&uhf_nvm_rx_pkt_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_RX_PKT_CFG_REQ);
                }
                else if(UHF_SET_RX_PKT_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_pwr_cfg_req((s_uhf_rf_pwr_cfg *)(&uhf_nvm_pwr_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_PWR_CFG_REQ);
                }
                else if(UHF_SET_PWR_CFG_ACK == cmd_id)
                {
                    DEBUG_CPRINT(("\n\r [UHF] BOOT ST CFG END"));
                    /* Trigger FW update end indication */
                    uhf_send_ipc(UHF_FW_CFG_END_IND, COMMS_UHF_CTLR);
                }
            }
        }
        break;

        case UHF_CMD_TMR_EXP:
        {
            /* Set config after FW update */

            comms_uhf_cfg_nvm_read();
            /** UHF TIMER ON for TM Periodic read **/
            uhf_hk_upd_id = 0;
            is_uhf_hk_upd_fill = 0;

            sdr_uhf_set_tx_cfg_req((s_uhf_rf_tx_cfg *)(&uhf_nvm_tx_cfg));

            uhf_upd_send_uart_cmd(UHF_SET_TX_RADIO_CFG_REQ);
            DEBUG_CPRINT(("\n\r [UHF] BOOT ST CFG ST"));
        }
        break;

        case UHF_FW_CFG_END_IND:
        {
            uhf_fw_upd_sts = 0;
            if(uhf_reinit_ongoing)
            {
                uhf_reinit_ongoing = 0;
                uhf_send_ipc(UHF_OBC_INIT_RSP,OBC_MAIN);
            }
            state = COMMS_UHF_ACTIVE_STATE;
            os_timer_start(uhf_beacon_enb_timer, uhf_tmr_cfg.beacon_enb_tmr);
            os_timer_start(uhf_tm_periodic_read, uhf_tmr_cfg.sdr_tm_read_tmr);
            //os_timer_start(uhf_tx_tm_on, uhf_tmr_cfg.sdr_tm_tx_tmr);
            os_timer_start(uhf_beacon_timer, uhf_tmr_cfg.beacon_prd_tmr);

            if(uhf_sch_gs_contact)
            {  
                if(!uhf_gpass_cfg.gpass_beacon_tx_enb)
                {
                    uhf_gs_contact_status_upd_itc(UHF_TC_TM_ST_REQ,COMMS_UHF);
                } 
                else
                {
                    uhf_rep_cnt=0;
                    DEBUG_CPRINT(("\n UHF_SDR_APP_BEACON_DATA_REQ sent"));
                    log_id(LOG_INFO_L1,LOG_COMMS,UHF_SDR_APP_BEACON_DATA_REQ_SENT);
                    uhf_send_ipc(UHF_SDR_APP_BEACON_DATA_REQ, SDR_APP);
                }
            }
            else
            {
                uhf_gs_contact_status_upd_itc(UHF_TC_TM_ST_REQ,COMMS_UHF);
                os_timer_start(uhf_tx_tm_on, uhf_tmr_cfg.sdr_tm_tx_tmr);
            }

            status = TC_STS_FW_UP_SUCC;
            comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status, payload);
        }
        break;

           case SCH_UHF_GS_ST_IND:
               {
                   DEBUG_CPRINT(("\n SCH_UHF_GS_ST_IND"));
                   uhf_sch_gs_contact = 1;
               }
               break;

           case SCH_UHF_GS_STOP_IND:
               {
                   DEBUG_CPRINT(("\n SCH_UHF_GS_STOP_IND"));
       //            log_id(LOG_INFO_L1,LOG_COMMS,SCH__UHF_GS_STOP_IND);
                   uhf_sch_gs_contact = 0;
                   uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
               }
            break;

           case UHF_TM_PERIODIC_READ:
           {
          	  		os_timer_start(uhf_tm_periodic_read, uhf_tmr_cfg.sdr_tm_read_tmr);
           }
		break;

          case SDR_APP_SDR_CTRL_GPASS_CFG:
               {
                sdr_app_sdr_ctrl_uhf_sdr_gpass_cfg_req(payload);
               }
                break;

           case UHF_TX_PERIODIC_BEACON_TMR_EXP:
               {
                   DEBUG_CPRINT(("\n UHF_TX_PERIODIC_BEACON_TMR_EXP @ DFU"));
           }
            break;
            
           case SDR_APP_UHF_MH_REQ:
            {
                DEBUG_CPRINT(("\n SDR_APP_UHF_MH_REQ"));
                log_id(LOG_INFO_L2,LOG_COMMS,SDR_APP_UHF_MH_REQ);
                sdr_app_sdr_ctrl_uhf_mh_req(payload);
            }
            break;

           case SDR_APP_SDR_CTRL_SDR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_sdr_cfg_req(payload);
            }
            break;
           
           case SDR_APP_SDR_CTRL_MODE_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_mode_cfg_req(payload);
            }
            break;
           
           case SDR_APP_SDR_CTRL_TMR_CFG_REQ:
            {
                os_memcpy(&uhf_tmr_cfg,(uint8_t *)payload,sizeof(s_sdr_tmr_cfg));
                uhf_send_ipc(SDR_CTRL_SDR_APP_TMR_CFG_RSP,SDR_APP);
            }
            break;
           
           case SDR_APP_SDR_CTRL_RSSI_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_rssi_cfg_req(payload);
            }
            break;
           
           case SDR_APP_SDR_CTRL_TM_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tm_cfg_req(payload);
            }
            break;

            /* To trigger again FW update through other RF 
             * if any failure occur in this state 
             */
           case TC_COMMS_UHF_FW_CFG_ACTIVATE:
            {
                uhf_send_ipc(UHF_SDR_APP_BAT_STS_REQ, SDR_APP);
            }
            break;

           case SDR_APP_UHF_FW_CFG_START_REQ:
            {

                os_timer_stop(uhf_beacon_enb_timer);
                os_timer_stop(uhf_tm_periodic_read);
                os_timer_stop(uhf_tx_tm_on);
                os_timer_stop(uhf_beacon_timer);

                int ret = 0;
                ret = uhf_fw_data_upload();
                if(!ret)
                {
                    state = COMMS_UHF_BOOT_STATE;
                    uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);
                    uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
                    uhf_send_ipc(UHF_FW_CFG_UPD_IND, COMMS_UHF_CTLR);

                }
                else if(ret == 1)
                {
                    DEBUG_CPRINT(("DATA NOT RECV FULL"));
                    status = TC_STS_FW_NOT_RCV;
                    comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,(uint8_t *)payload);
                }
                else if(ret == 2)
                {
                    DEBUG_CPRINT(("EXTRA BYTES ON LINE"));
                    status = TC_STS_FW_WRONG;
                    comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,(uint8_t *)payload);

                }
                else if(ret == 3)
                {
                    DEBUG_CPRINT(("BAD CHECK SUM"));
                    status = TC_STS_FW_BAD_CHKSUM;
                    comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,(uint8_t *)payload);
                }

            }
            break;

        default:
            state = COMMS_UHF_BOOT_STATE ;
            break;
    }
    return state;
}

/**
 * @brief This function handles comms DFU state
 * 
 * This handler implements the logic for the DFU state in the 
 * UHF communication state machine. It performs DFU actions like
 * firmware updates and transitions to the next state when 
 * DFU is complete.
 *
 * @return Next UHF state machine state after DFU
 */
uint8_t comms_uhf_dfu_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_DFU_STATE;
    int8_t status = COMMS_UHF_IPC_OK;
    CPRINT_VARIABLE_UNUSED(msg_id);
    CPRINT_VARIABLE_UNUSED(status);
    CPRINT_VARIABLE_UNUSED(state);
    DEBUG_CPRINT(("received response for COMMS_UHF_DFU : %d \n", msg_id));
    log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,RECEIVED_RESPONSE_FOR_COMMS_UHF_DFU__u16_,msg_id);
    switch (msg_id)
    {
        case COMMS_UHF_DFU_DONE:
            DEBUG_CPRINT(("In  DFU_DONE \n "));
            log_id(LOG_INFO_L2,LOG_COMMS,IN_DFU_DONE);
            state = COMMS_UHF_CFG_STATE;
            //comms_uhf_csw_trans_msg.Msg_id = COMMS_UHF_INTRNL_LOG_CFG_REQ;
            DEBUG_CPRINT(("sending internal log cfg request \n"));
            log_id(LOG_INFO_L2,LOG_COMMS,SENDING_INTERNAL_LOG_CFG_REGUEST);
            DEBUG_CPRINT(("In DFU_DONE state, status of internal log_cfg request is %d \n ",status));
            log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,IN_DFU_DONE_STATE_STATUS_OF_INTERNAL_LOG_CFG_REQUEST___u16_,status);
            break;

        case COMMS_UHF_DFU_FAIL:
            DEBUG_CPRINT(("DFU_FAIL event \n"));
            log_id(LOG_ERROR,LOG_COMMS,DFU_FAIL_EVENT);
            state = COMMS_UHF_ERROR_STATE ;
            break;

        default:
            state = COMMS_UHF_SAFE_MODE_STATE ;
            break;
    }
    return state;
}

/**
 * @brief This function handle comms configuration states
 *
 * Implements logic for UHF configuration state. Handles configuring 
 * parameters, registers, etc. and transitions to next state when done.
 *
 * @return Next UHF state after configuration
 */
uint8_t comms_uhf_cfg_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_CFG_STATE;
    int32_t status = 0;
    status = status;

    DEBUG_CPRINT(("received response for COMMS_UHF_CFG : %d \n", msg_id));
    log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,RECEIVED_RESPONSE_FOR_COMMS_UHF_CFG___u16_,msg_id);
    switch (msg_id)
    {
        case OBC_UHF_DEINIT_REQ:
            {
                comms_uhf_cfg_nvm_write();

                state = COMMS_UHF_INIT_STATE;
                uhf_send_ipc(UHF_OBC_DEINIT_RSP,OBC_MAIN);
                uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
                os_timer_stop(uhf_rsp_timeout_recovery_tmr);
            }
            break;
		
		
		case UHF_UART_CMD_RSP:
            {
                uint8_t cmd_id = 0;
                uhf_cmd_rsp_timeout_cnt = 0;

                cmd_id = uhf_proc_uart_cmd_rsp(payload);

                if(UHF_SET_TX_RADIO_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_rx_cfg_req((s_uhf_rf_rx_cfg *)(&uhf_nvm_rx_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_RX_RADIO_CFG_REQ);
                }
                else if(UHF_SET_RX_RADIO_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_tx_pkt_cfg_req((s_uhf_rf_tx_pkt_cfg *)(&uhf_nvm_tx_pkt_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_TX_PKT_CFG_REQ);
                }
                else if(UHF_SET_TX_PKT_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_rx_pkt_cfg_req((s_uhf_rf_rx_pkt_cfg *)(&uhf_nvm_rx_pkt_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_RX_PKT_CFG_REQ);
                }
                else if(UHF_SET_RX_PKT_CFG_ACK == cmd_id)
                {
                    status = sdr_uhf_set_pwr_cfg_req((s_uhf_rf_pwr_cfg *)(&uhf_nvm_pwr_cfg));

                    uhf_upd_send_uart_cmd(UHF_SET_PWR_CFG_REQ);
                }
                else if(UHF_SET_PWR_CFG_ACK == cmd_id)
                {
                    // TODO: Beacon configuration
                    state = COMMS_UHF_ACTIVE_STATE;
#ifdef COMMS_TIME_DEBUG 
                    get_crnt_time_date(&tim_get_u_fs);
                   	DEBUG_CPRINT(("\nUHF TIME TM_RD TMR ENT %d:%d:%d",tim_get_u_fs.hour, tim_get_u_fs.minute, tim_get_u_fs.seconds));
                   	log_id_with_three_arg(LOG_INFO_L2,LOG_COMMS,UHF_TIME_TM_RD_TMR_ENT__u8__u8__u8_,tim_get_u_fs.hour, tim_get_u_fs.minute, tim_get_u_fs.seconds);
#endif 
                    uhf_hk_upd_id = 0;
                    is_uhf_hk_upd_fill = 0;

                    DEBUG_CPRINT(("OS TIMER started at OBC UHF CFG %d", uhf_tmr_cfg.sdr_tm_read_tmr ));
                    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,OS_TIMER_STARTED_AT_OBC_UHF_INIT__u32_,uhf_tmr_cfg.sdr_tm_read_tmr);
                    os_timer_start(uhf_tm_periodic_read, uhf_tmr_cfg.sdr_tm_read_tmr);

                    /** Nothing to be done */
                    uhf_send_ipc(UHF_OBC_INIT_RSP,OBC_MAIN);
                    if(uhf_sch_gs_contact)
                    {
                        if(!uhf_gpass_cfg.gpass_beacon_tx_enb)
                        {
                            uhf_gs_contact_status_upd_itc(UHF_TC_TM_ST_REQ,COMMS_UHF);
                        }
                        else
                        {
                            DEBUG_CPRINT(("\n UHF_SDR_APP_BEACON_DATA_REQ sent"));
                            log_id(LOG_INFO_L1,LOG_COMMS,UHF_SDR_APP_BEACON_DATA_REQ_SENT);
                            uhf_send_ipc(UHF_SDR_APP_BEACON_DATA_REQ, SDR_APP);
                        }
                    }

                }
            }
            break;
		
        case UHF_UART_CMD_RSP_TIMEOUT:
		{
            uhf_cmd_rsp_timeout_cnt++;
			uhf_upd_send_uart_cmd(uhf_tc_cmd_id);

			DEBUG_CPRINT(("\n\r TIME_OUT - RSP TIM_OUT CFG_ST"));
            
            comms_uhf_cmd_rsp_timeout_recovery_handling();
		}
		break;
        
        case SDR_APP_UHF_MH_REQ:
        {
            DEBUG_CPRINT(("\n SDR_APP_UHF_MH_REQ"));
            log_id(LOG_INFO_L2,LOG_COMMS,SDR_APP_UHF_MH_REQ);
            sdr_app_sdr_ctrl_uhf_mh_req(payload);
        }
        break;

        case SDR_APP_SDR_CTRL_SDR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_sdr_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_MODE_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_mode_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_TMR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tmr_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_RSSI_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_rssi_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_TM_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tm_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_GPASS_CFG:
               {
                sdr_app_sdr_ctrl_uhf_sdr_gpass_cfg_req(payload);
               }
                break;
        
        case TC_COMMS_UHF_GET_TM_STORE:
            {
                if(is_uhf_hk_upd_fill == 1)//PERI_UHF_HK_SYNC_ID)
                {
                	comms_uhf_tm_read.cnt_hk_str = PERI_UHF_HK_SYNC_ID;
                }
                else
                {
                    comms_uhf_tm_read.cnt_hk_str = uhf_hk_upd_id;
                }

                uint16_t rx_size = 0;
				rx_size = ((sizeof(s_comms_uhf_tm_prop))*comms_uhf_tm_read.cnt_hk_str) +1;
                comms_uhf_tm_stor_get_rsp_hdlr(TM_COMMS_UHF_GET_TM_STORE, (uint8_t*)&comms_uhf_tm_read, rx_size);

                uhf_hk_upd_id = 0;
                is_uhf_hk_upd_fill = 0;
            }
            break;


        default:
            state = COMMS_UHF_CFG_STATE;
            break;
    }
    return state;
}

/**
 * @brief This function handle comms calibration state
 *
 * Implements calibration logic for UHF in this state handler. Performs 
 * required calibration procedures and moves to next state when complete.
 * 
 * @return Next UHF state after calibration
 */
uint8_t comms_uhf_calib_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_CALIB_STATE;
    int8_t status = COMMS_UHF_IPC_OK;
    CPRINT_VARIABLE_UNUSED(msg_id);
    CPRINT_VARIABLE_UNUSED(state);
    CPRINT_VARIABLE_UNUSED(status);
    DEBUG_CPRINT(("received response for COMMS_UHF_CLAIB : %d \n", msg_id));
    log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,RECEIVED_RESPONSE_FOR_COMMS_UHF_CLAIB__u16_,msg_id);
    switch (msg_id)
    {
        case COMMS_UHF_CALIB_DONE:
            DEBUG_CPRINT(("In CALIB_DONE state \n "));
            log_id(LOG_INFO_L2,LOG_COMMS,IN_CALIB_DONE_STATE);
            state = COMMS_UHF_SLEEP_IDLE_STATE;
            //comms_uhf_csw_trans_msg.Msg_id =  COMMS_UHF_INTRNL_SLEEP_REQ;

            DEBUG_CPRINT(("sending internal sleep request \n"));
            log_id(LOG_INFO_L1,LOG_COMMS,SENDING_INTERNAL_SLEEP_REQUEST);
            DEBUG_CPRINT(("In CALIB_DONE state, status of internal sleep request is %d \n ",status));
            log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,IN_CALIB_DONE_STATE_STATUS_OF_INTERNAL_SLEEP_REQUEST_u16_,status);
            break;

        case COMMS_UHF_CALIB_FAIL:
            DEBUG_CPRINT(("CALIB_FAIL event \n"));
            log_id(LOG_ERROR,LOG_COMMS,CALIB_FAIL_EVENT);
            state = COMMS_UHF_ERROR_STATE ;
            break;

        default:
            state = COMMS_UHF_SAFE_MODE_STATE ;
            break;
    }
    return state;
}

/**
 * @brief This API handle the comms idle state
 *
 * Implements sleep/idle logic for UHF. Manages low power mode and 
 * transitions to active state when needed.
 *
 * @return Next UHF state from sleep/idle
 */
uint8_t comms_uhf_sleep_idle_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_SLEEP_IDLE_STATE;
    int8_t status = COMMS_UHF_IPC_OK;
    CPRINT_VARIABLE_UNUSED(msg_id);
    CPRINT_VARIABLE_UNUSED(status);
    CPRINT_VARIABLE_UNUSED(state);
    DEBUG_CPRINT(("received response for COMMS_UHF_SLEEP : %d \n", msg_id));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RECEIVED_RESPONSE_FOR_COMMS_UHF_SLEEP__u16_,msg_id);
    switch (msg_id)
    {
        case COMMS_UHF_SLEEP_DONE:
            DEBUG_CPRINT(("In SLEEP_IDLE_DONE state \n "));
            log_id(LOG_INFO_L2,LOG_COMMS,IN_SLEEP_IDLE_DONE_STATE);
            state = COMMS_UHF_ACTIVE_STATE;
            //comms_uhf_csw_trans_msg.Msg_id =  COMMS_UHF_INTRNL_TX_REQ;
            DEBUG_CPRINT(("sending internal tx request \n"));
            log_id(LOG_INFO_L2,LOG_COMMS,SENDING_INTERNAL_TX_REQUEST);
            DEBUG_CPRINT(("In SLEEP_DONE state, status of internal tx request is %d \n ",status));
            log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,IN_SLEEP_DONE_STATE_STATUS_OF_INTERNAL_TX_REQUEST__u16_,status);
            break;

        case COMMS_UHF_SLEEP_FAIL:
            DEBUG_CPRINT(("SLEEP_IDLE_FAIL event \n"));
            log_id(LOG_ERROR,LOG_COMMS,SLEEP_IDLE_FAIL_EVENT);
            state = COMMS_UHF_ERROR_STATE ;
            break;

        default:
            state = COMMS_UHF_SAFE_MODE_STATE ;
            break;
    }
    return state;
}

/**
 * @brief This API handle the comms active states
 * 
 *  Handles UHF active state logic like data transmission. Returns to 
 * sleep when no activity.
 * 
 * @return Next UHF state from active
 */
uint8_t comms_uhf_active_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_ACTIVE_STATE;
    int8_t status = COMMS_UHF_IPC_OK;
    status = status;

    uint16_t rx_size = 0;
    rx_size = rx_size;
    uint8_t *tc_tm_data;

    tc_tm_data = (uint8*)payload;

    DEBUG_CPRINT(("received response for COMMS_UHF_ACTIVE_STATE : %d \n", msg_id));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RECEIVED_RESPONSE_FOR_COMMS_UHF_ACTIVE_STATE__u16_,msg_id);
    switch (msg_id)
    {
        case OBC_UHF_INIT_REQ:
            {
                comms_uhf_cfg_nvm_read();
                /** UHF TIMER ON for TM Periodic read **/
                uhf_hk_upd_id = 0;
                is_uhf_hk_upd_fill = 0;
                uhf_reinit_ongoing = 0;
                //os_timer_start(uhf_tm_periodic_read, uhf_tmr_cfg.sdr_tm_read_tmr);
                //     state = COMMS_UHF_ACTIVE_STATE;
                state = COMMS_UHF_CFG_STATE;

                status = sdr_uhf_set_tx_cfg_req((s_uhf_rf_tx_cfg *)(&uhf_nvm_tx_cfg));

                uhf_upd_send_uart_cmd(UHF_SET_TX_RADIO_CFG_REQ);
            }
            break;
        case OBC_UHF_DEINIT_REQ:
            {
                comms_uhf_cfg_nvm_write();

                state = COMMS_UHF_INIT_STATE;
                uhf_send_ipc(UHF_OBC_DEINIT_RSP,OBC_MAIN);
                uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
                
                os_timer_stop(uhf_cmd_timeout);
                os_timer_stop(uhf_rsp_timeout_recovery_tmr);
                os_timer_stop(uhf_tm_periodic_read);
                os_timer_stop(uhf_tx_tm_on);
                os_timer_stop(uhf_beacon_timer);
            }
            break;
		case UHF_UART_CMD_RSP_TIMEOUT:
		{
            uhf_cmd_rsp_timeout_cnt++;
			uhf_upd_send_uart_cmd(uhf_tc_cmd_id);

			DEBUG_CPRINT(("\n\r TIME_OUT - RSP TIM_OUT"));
            
            comms_uhf_cmd_rsp_timeout_recovery_handling();
		}
		break;
        case TC_COMMS_UHF_INITIAL_SYNC:
            {
                uhf_initial_sync_hdlr();
            }
            break;

        case OBC_UHF_HEALTH_CHECK_REQ:
            {
                //uhf_health_report_tm();
            }
            break;


        case UHF_TM_PERIODIC_READ:
            {
                comms_uhf_health_report_tm();
            }
            break;

        case TC_COMMS_UHF_GET_TM_STORE:
            {
                if(is_uhf_hk_upd_fill == 1)//PERI_UHF_HK_SYNC_ID)
                {
                	comms_uhf_tm_read.cnt_hk_str = PERI_UHF_HK_SYNC_ID;
                }
                else
                {
                    comms_uhf_tm_read.cnt_hk_str = uhf_hk_upd_id;
                }

				rx_size = ((sizeof(s_comms_uhf_tm_prop))*comms_uhf_tm_read.cnt_hk_str) +1;
                comms_uhf_tm_stor_get_rsp_hdlr(TM_COMMS_UHF_GET_TM_STORE, (uint8_t*)&comms_uhf_tm_read, rx_size);

                uhf_hk_upd_id = 0;
                is_uhf_hk_upd_fill = 0;
            }
            break;

        case UHF_UART_CMD_RSP:
            {
                uhf_cmd_rsp_timeout_cnt = 0;
            	os_timer_stop(uhf_cmd_timeout);
            	uhf_cmd_rsp_cnt = 0;
                uhf_proc_uart_cmd_rsp(payload);
            }
            break;

        case TC_COMMS_UHF_GET_TM_INFO:
            {
                uhf_upd_send_uart_cmd(UHF_RADIO_MSG_GET_TELEM);
            }
            break;

        case TC_COMMS_UHF_SET_CFG: //not used
            {
               // uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);

            }
            break;

        case TC_COMMS_UHF_SET_TX_CFG:
            {
                status = sdr_uhf_set_tx_cfg_req((s_uhf_rf_tx_cfg *)(&tc_tm_data[TC_PLD_IDX]));

                uhf_upd_send_uart_cmd(UHF_SET_TX_RADIO_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_GET_TX_CFG:
            {
                uhf_upd_send_uart_cmd(UHF_GET_TX_RADIO_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_SET_RX_CFG:
            {
                status = sdr_uhf_set_rx_cfg_req((s_uhf_rf_rx_cfg *)(&tc_tm_data[TC_PLD_IDX]));

                uhf_upd_send_uart_cmd(UHF_SET_RX_RADIO_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_GET_RX_CFG:
            {
                uhf_upd_send_uart_cmd(UHF_GET_RX_RADIO_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_SET_TX_PKT_CFG:
            {
                status = sdr_uhf_set_tx_pkt_cfg_req((s_uhf_rf_tx_pkt_cfg *)(&tc_tm_data[TC_PLD_IDX]));
                uhf_upd_send_uart_cmd(UHF_SET_TX_PKT_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_GET_TX_PKT_CFG:
            {
                uhf_upd_send_uart_cmd(UHF_GET_TX_PKT_CFG_REQ);
            }
            break;
        case TC_COMMS_UHF_SET_RX_PKT_CFG:
            {
                status = sdr_uhf_set_rx_pkt_cfg_req((s_uhf_rf_rx_pkt_cfg *)(&tc_tm_data[TC_PLD_IDX]));
                uhf_upd_send_uart_cmd(UHF_SET_RX_PKT_CFG_REQ);
            }
            break;
        case TC_COMMS_UHF_GET_RX_PKT_CFG:
            {
                uhf_upd_send_uart_cmd(UHF_GET_RX_PKT_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_SET_PWR_CFG:
            {
                status = sdr_uhf_set_pwr_cfg_req((s_uhf_rf_pwr_cfg *)(&tc_tm_data[TC_PLD_IDX]));
                uhf_upd_send_uart_cmd(UHF_SET_PWR_CFG_REQ);
            }
            break;
        case TC_COMMS_UHF_GET_PWR_CFG:
            {
                uhf_upd_send_uart_cmd(UHF_GET_PWR_CFG_REQ);
            }
            break;

        case TC_COMMS_UHF_FW_CFG_ACTIVATE:
            {
        	uhf_send_ipc(UHF_SDR_APP_BAT_STS_REQ, SDR_APP);
        }
        break;

        case SDR_APP_UHF_FW_CFG_START_REQ:
        {

        	os_timer_stop(uhf_beacon_enb_timer);
        	os_timer_stop(uhf_tm_periodic_read);
        	os_timer_stop(uhf_tx_tm_on);
        	os_timer_stop(uhf_beacon_timer);

        	int ret = 0;
        	ret = uhf_fw_data_upload();
        	if(!ret)
            {
                state = COMMS_UHF_BOOT_STATE;
				uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);
        	uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
        		uhf_send_ipc(UHF_FW_CFG_UPD_IND, COMMS_UHF_CTLR);

            }
        	else if(ret == 1)
        	{
        		DEBUG_CPRINT(("DATA NOT RECV FULL"));
				status = TC_STS_FW_NOT_RCV;
				comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,tc_tm_data);
        	}
        	else if(ret == 2)
        	{
        		DEBUG_CPRINT(("EXTRA BYTES ON LINE"));
				status = TC_STS_FW_WRONG;
				comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,tc_tm_data);

        	}
        	else if(ret == 3)
        	{
        		DEBUG_CPRINT(("BAD CHECK SUM"));
				status = TC_STS_FW_BAD_CHKSUM;
				comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,tc_tm_data);
        	}

            }
            break;

        case SDR_APP_UHF_FW_CFG_STOP_REQ:
        {
        	status = TC_STS_FW_UP_DELAY;
        	comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG_ACTIVATE, status ,tc_tm_data);
        }

		case TC_COMMS_UHF_FW_CFG:
		{
			status = SDR_UHF_FIRMWARE_CFG(&tc_tm_data[TC_PLD_IDX]);
			comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_FW_CFG, status ,tc_tm_data);
		}
		break;

        case TC_COMMS_UHF_BEACON_CFG:
            {
                memcpy(&uhf_trans_timer,&tc_tm_data[TC_PLD_IDX],sizeof(s_uhf_comms_timer));
                uhf_upd_send_uart_cmd(UHF_SET_BCON_TIMER); // beacon timer
                comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_CFG, 0 ,tc_tm_data);
            }
            break;

        case TC_COMMS_UHF_BEACON_TX_ST:
            {
               // uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STRT);// beacon start cmd 38
               // comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_ST, 0 ,tc_tm_data);

                memcpy(&beacon_op,&tc_tm_data[TC_PLD_IDX],sizeof(uint8_t));

                if(beacon_op == 1)
                {
                    uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STRT);// beacon start cmd 38
                }
                else if(beacon_op == 2)
                {
                    os_timer_start(uhf_beacon_timer, uhf_tmr_cfg.beacon_prd_tmr);
                }
                else if(beacon_op == 3)
                {
                    uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STRT);
                    os_timer_start(uhf_beacon_timer, uhf_tmr_cfg.beacon_prd_tmr);
                }

                comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_ST, 0 ,tc_tm_data);
            }
            break;

        case TC_COMMS_UHF_BEACON_TX_STOP:
            {
                //uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);// beacon stop cmd 39
                //comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_STOP, 0 ,tc_tm_data);

            	memcpy(&beacon_op,&tc_tm_data[TC_PLD_IDX],sizeof(uint8_t));

            	if(beacon_op == 1)
            	{
            		uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);// beacon stop cmd 39
            	}
                else if(beacon_op == 2)
            	{
            		os_timer_stop(uhf_beacon_timer);
            		os_timer_start(uhf_beacon_enb_timer, uhf_tmr_cfg.beacon_enb_tmr);
            	}
                else if(beacon_op == 3)
                {
                    uhf_upd_send_uart_cmd(UHF_SET_BCON_CFG_STP);// beacon stop cmd 39
				
                    os_timer_stop(uhf_beacon_timer);
                    os_timer_start(uhf_beacon_enb_timer, uhf_tmr_cfg.beacon_enb_tmr);
                }
				
                comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_TX_STOP, 0 ,tc_tm_data);
            }
            break;

        case TC_COMMS_UHF_BEACON_DATA: //for testing
            {
                uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);// beacon transmission cmd 1C
                comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_BEACON_DATA, 0 ,tc_tm_data);
            }
            break;

        case TC_COMMS_UHF_CSP_TM_INFO:
            {
                /**TODO: CSP Debug information to be added */
            }
            break;

      /*  case TC_COMMS_UHF_SET_CALLSIGN:
            {
                uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CALLSIGN);
            }
            break;

        case TC_COMMS_UHF_GET_CALLSIGN:
            {
                uhf_upd_send_uart_cmd(UHF_RADIO_MSG_GET_CALLSIGN);
            }
            break;*/

        case OTSU_UHF_SYNC_REQ:
            {
                /** OTSU Call back - Success*/
                if(!uhf_gs_contact_valid)
                {
                    comms_uhf_otsu_cb_sts = 1;
                }
                else
                {
#ifndef LINUX_TEMP_PORT
                    //otsu_send_uhf_sync_status_cblk(1);
#endif
                }
            }
            break;

        case SCH_UHF_GS_ST_IND:
            {
                DEBUG_CPRINT(("\n SCH_UHF_GS_ST_IND"));
                log_id(LOG_INFO_L1,LOG_COMMS,SCH_UHF_GS_ST_IND);
                uhf_sch_gs_contact = 1;
                
                if(!uhf_gpass_cfg.gpass_beacon_tx_enb)
                {
                    uhf_gs_contact_status_upd_itc(UHF_TC_TM_ST_REQ,COMMS_UHF);
                }
                else
                {
                    DEBUG_CPRINT(("\n UHF_SDR_APP_BEACON_DATA_REQ sent"));
                    log_id(LOG_INFO_L1,LOG_COMMS,UHF_SDR_APP_BEACON_DATA_REQ_SENT);
                    uhf_send_ipc(UHF_SDR_APP_BEACON_DATA_REQ, SDR_APP);
                }

                //comms_uhf_tx_link_alert_tm();
            }
            break;

        case SCH_UHF_GS_STOP_IND:
            {
                DEBUG_CPRINT(("\n SCH_UHF_GS_STOP_IND"));
                log_id(LOG_INFO_L1,LOG_COMMS,SCH_UHF_GS_STOP_IND);
                uhf_sch_gs_contact = 0;
                uhf_gs_contact_status_upd_itc(UHF_TC_TM_STOP_REQ,COMMS_UHF);
            }
            break;

        case UHF_TX_PERIODIC_BEACON_TMR_EXP:
            {
                DEBUG_CPRINT(("\n UHF_TX_PERIODIC_BEACON_TMR_EXP"));
                log_id(LOG_INFO_L1,LOG_COMMS,UHF_TX_PERIODIC_BEACON_TMR_EXP);
                uhf_send_ipc(UHF_SDR_APP_BEACON_DATA_REQ, SDR_APP);
            }
            break;

         case SDR_APP_SDR_CTRL_GPASS_CFG:
               {
                sdr_app_sdr_ctrl_uhf_sdr_gpass_cfg_req(payload);
               }
                break;


        case SDR_APP_SDR_CTRL_SDR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_sdr_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_MODE_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_mode_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_TMR_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tmr_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_RSSI_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_rssi_cfg_req(payload);
            }
            break;
        case SDR_APP_SDR_CTRL_TM_CFG_REQ:
            {
                sdr_app_sdr_ctrl_uhf_tm_cfg_req(payload);
            }
            break;

        case SDR_APP_UHF_MH_REQ:
            {
                DEBUG_CPRINT(("\n SDR_APP_UHF_MH_REQ"));
                log_id(LOG_INFO_L1,LOG_COMMS,SDR_APP_UHF_MH_REG);
               sdr_app_sdr_ctrl_uhf_mh_req(payload);

            }
            break;

        case SDR_APP_UHF_BEACON_DATA_IND:
            {
                os_memcpy(&uhf_beacon_data,payload,sizeof(s_sdr_beacon_pld));

            	if(beacon_op == 1)
            	{
                    uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);// beacon transmission cmd 1C
            	}
                else if(beacon_op == 2)
                {
                    comms_uhf_beacon_tx((uint8_t *)&uhf_beacon_data,sizeof(s_sdr_beacon_pld));
                }
                else if(beacon_op ==3)
                {
                    comms_uhf_beacon_tx((uint8_t *)&uhf_beacon_data,sizeof(s_sdr_beacon_pld));
                    uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);// beacon transmission cmd 1C
                }
            }
            break;
        case UHF_REP_TMR_EXP:
            {
            	if(beacon_op == 1)
            	{
                    uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);// beacon transmission cmd 1C
            	}
                else if(beacon_op == 2)
                {
                    comms_uhf_beacon_tx((uint8_t *)&uhf_beacon_data,sizeof(s_sdr_beacon_pld));
                }
                else if(beacon_op ==3)
                {
                    comms_uhf_beacon_tx((uint8_t *)&uhf_beacon_data,sizeof(s_sdr_beacon_pld));
                    uhf_upd_send_uart_cmd(UHF_RADIO_MSG_SET_CFG);// beacon transmission cmd 1C
                }
            }
            break;

        case TC_COMMS_UHF_GET_FW_STS:
            {

                uint32_t size,crc;
                os_memcpy(&size,&tc_tm_data[TC_PLD_IDX],4);
                os_memcpy(&crc,&tc_tm_data[TC_PLD_IDX+4],4);
                /*DEBUG_CPRINT(("comms fw size %ld\n",size));
                  DEBUG_CPRINT(("comms fw crc %lx\n",crc));*/
#ifndef LINUX_TEMP_PORT
                comms_get_firmware_availablity_status(COMMS_UHF_FW,size,crc);
#endif
                comms_uhf_tc_tm_set_rsp_hdlr(TM_COMMS_UHF_GET_FW_STS, 0 ,tc_tm_data);
            }
            break;

        default:
            state = COMMS_UHF_ACTIVE_STATE;
            break;
    }
    return state;
}

/**
 * @brief This functions handle the comms safe mode state
 * 
 * Implements safe mode logic for UHF when in safe state. Performs 
 * safe mode specific actions and transitions when safe mode ends.
 *
 * @return Next UHF state from safe mode 
 */
uint8_t comms_uhf_safe_mode_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_SAFE_MODE_STATE;
    return state;
}

/**
 * @brief This function handle the comms error state
 * 
 * Handles UHF error state logic. Performs error logging, 
 * recovery procedures, etc. Transitions once error is cleared. 
 * 
 * @return Next UHF state from error
 */
uint8_t comms_uhf_error_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_ERROR_STATE;
    return state;
}

/**
 * @brief This function handling Bypass the comms states
 * 
 * Implements bypass logic for UHF state machine. Allows 
 * transitions between states without action. 
 *
 * @return Next bypassed UHF state
 */
uint8_t comms_uhf_bypass_state_hdlr(uint16_t msg_id, void *payload)
{
    uint8_t state = COMMS_UHF_BYPASS_STATE;
    int8_t status = COMMS_UHF_IPC_OK;
    status = status;
    uint16_t rx_size = 1;
    rx_size = rx_size;
    //  uint8_t *tc_tm_data;

    //tc_tm_data = (uint8*)payload;

    DEBUG_CPRINT(("\nCOMMS_UHF_BYPASS_State\n"));
    log_id(LOG_INFO_L1,LOG_COMMS,COMMS_UHF_BYPASS_STATE);
    switch (msg_id)
    {
        default:
            state = COMMS_UHF_BYPASS_STATE ;
            break;
    }
    return state;
}

/**
 * @brief This API handle the comms finite states machine
 * 
 * Top-level handling function that runs the UHF state machine.
 * Calls the current state's handler function.
 */
uint8_t comms_uhf_fsm_hdlr_fn(uint8_t state,uint16_t msg_id, void *payload)
{
#ifdef COMMS_TIME_DEBUG
	get_crnt_time_date(&tim_get_u_fs);
	DEBUG_CPRINT(("\nUHF TIME FSM %d:%d:%d , State - %d, MSG id - %d",tim_get_u_fs.hour, tim_get_u_fs.minute, tim_get_u_fs.seconds, state, msg_id));
	log_id_with_four_arg(LOG_INFO_L2,LOG_COMMS,UHF_TIME_FSM_u8__u8__u8__status__u8_,tim_get_u_fs.hour, tim_get_u_fs.minute, tim_get_u_fs.seconds, state);
#endif
	log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,UHF_TIME_FSM_MSG_ID__u16_,msg_id);
    return(comms_uhf_fsm_hdlr[state](msg_id,payload));
}

/**
 * @brief This function monitor and report the uhf health metrics
 * 
 * Transmits a health report telemetry packet containing 
 * UHF status information.
 */
void comms_uhf_health_report_tm(void)
{
    uhf_upd_send_uart_cmd(UHF_RADIO_MSG_GET_TELEM);
    uhf_hk_upd_id++;
    if(uhf_hk_upd_id == PERI_UHF_HK_SYNC_ID)
    {
        uhf_hk_upd_id = 0;
        is_uhf_hk_upd_fill = 1;
    }
    os_timer_start(uhf_tm_periodic_read, uhf_tmr_cfg.sdr_tm_read_tmr);
}

void comms_uhf_cmd_rsp_timeout_recovery_handling(void)
{
    uint32_t eps_req[2];
    eps_req[0] = HW_UHF;

    if((UHF_CMD_RSP_TIMEOUT_CNT_THRES == uhf_cmd_rsp_timeout_cnt) && (!uhf_reinit_ongoing))
    {
        os_timer_stop(uhf_cmd_timeout);

        eps_req[1] = PWR_OFF;
        uhf_cmd_rsp_timeout_cnt = 0;
        uhf_reinit_ongoing = 1;

        DEBUG_CPRINT(("\nUHF CMD RSP TIMEOUT CNT %d\n", uhf_cmd_rsp_timeout_cnt));
        (void)eps_req;
        uhf_send_ipc_pld(EPS_SET_CHNL_STATE,EPS_TASK,sizeof(eps_req),(uint8_t *)eps_req);
        err_detected(ERR_GROUP_COMMS, ERR_COMMS_UHF_RSP_TIMEOUT);
        os_timer_start(uhf_rsp_timeout_recovery_tmr, UHF_CMD_RSP_TIMEOUT_RECOVERY_TMR);
    }
}

/* @brief Handle uhf re-initialization.
 *
 * Performs re-init steps on unrecoverable error:
 * - Stops uhf threads and timers.
 * - Resets global data structures.
 * - Restarts driver threads and state machine.
 */
void uhf_reinit_exp_hdlr(void)
{
    uint32_t eps_req[2];
    eps_req[0] = HW_UHF;
    eps_req[1] = PWR_ON;

    uhf_send_ipc_pld(EPS_SET_CHNL_STATE,EPS_TASK,sizeof(eps_req),(uint8_t *)eps_req);
    DEBUG_CPRINT(("\nUHF REINIT ON %d\n", uhf_cmd_rsp_timeout_cnt));
    uhf_cmd_rsp_timeout_cnt = 0; 
}

