/**
 * @file comms_sband_xlink_api.c
 *
 * @brief This file contains APIs for S-Band parameters configuration
 *        programming and readback
 *
 * @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 "exo_types.h"
#include "exo_common.h"
#include "exo_tctm_ipc.h"
#include "comms_csw.h"
#include "comms_sband_xlink_ss.h"
#include "comms_sband_xlink_api.h"
#include "comms_sband_xlink_rfe_ctr.h"
#include "comms_sband_xlink_sock.h"
#include "comms_ipc.h"
#include <arpa/inet.h>
#include "exo_io_al_sos_timer.h"
#include "comms_sband_intf.h"
#include "logger.h"
#include "err_handler.h"

#define IQS_PWR_ROW 5
#define IQS_FRQ_COL 5
#define SYNC_ON  1

uint16_t PLACE_IN_EXT_RAM_MEM version_info_size =0;

/*##########################################################################################*/
/*************	Variable Declaration									*********************/
/*##########################################################################################*/
// ==> we place the variable declaration outside of the defgroup ServiceServer, otherwise we will see them twice in the DOXYGEN documentation !!
// // for this we create the defgroup ServiceServerVariablePub in the ServiceServer.c file where we define the variables
// // ==> The same applies to functions as soon as the description header differs in ServiceServer.h and ServiceServer.c!!

/** Number of bytes for analog input */
uint32 PLACE_IN_EXT_RAM_MEM sband_NumByteForAi;
/** Number of bytes for analog input high */
uint32 PLACE_IN_EXT_RAM_MEM sband_NumByteForAiHigh;
/** Number of bytes for analog input low */
uint32 PLACE_IN_EXT_RAM_MEM sband_NumByteForAiLow;


/** S-Band firmware configuration size */
uint32_t PLACE_IN_EXT_RAM_MEM sband_fw_cfg_size = 0;
/** S-Band firmware configuration failure count */
uint8_t PLACE_IN_EXT_RAM_MEM sband_fw_cfg_failure_cnt = 0;
/** S-Band periodic telemetry storage */
s_sband_periodic_get_tm PLACE_IN_EXT_RAM_MEM xlink_sband_tm_store;

extern uint8_t sband_iqs_tx_ON;
extern uint8_t bch_dec_param[3];

/** Date-time for device configuration */
sys_time_date_s PLACE_IN_EXT_RAM_MEM tim_get_devconf;

/** IQS time */  
extern sys_time_s PLACE_IN_EXT_RAM_MEM get_t_iqs;

/** Radio ID */
extern PLACE_IN_EXT_RAM_MEM uint8 radio_id;

/** S-Band telemetry/telecommand ID */
extern uint16_t PLACE_IN_EXT_RAM_MEM sband_tc_tm_id;

/** IQS TX configuration */
extern s_comms_xlinks_tx_cfg PLACE_IN_EXT_RAM_MEM iqs_tx_cfg_global;

/** IQS RX configuration */ 
extern s_comms_xlinks_rx_cfg PLACE_IN_EXT_RAM_MEM iqs_rx_cfg_global;

/** S-Band TX general configuration */
extern s_comms_tx_gen_cfg PLACE_IN_EXT_RAM_MEM sband_s_tx_cfg;

/** S-Band RX general configuration */
extern s_comms_rx_gen_cfg PLACE_IN_EXT_RAM_MEM sband_s_rx_cfg;
extern float PLACE_IN_EXT_RAM_MEM avg_rssi[MAX_RX_CH];


/** IQS TX/RX attenuation */
extern s_comms_xlinks_dev_rssi_tx_atten PLACE_IN_EXT_RAM_MEM iqs_tx_rx_atten_glob;

/** Current RX */
extern uint8_t PLACE_IN_EXT_RAM_MEM curr_rx;

/** IQS timeout */
extern s_comms_xlinks_timeout PLACE_IN_EXT_RAM_MEM iqs_tim_out_glob;

/** IQS ground contact duration */
extern s_comms_xlinks_gnd_cnt_duration PLACE_IN_EXT_RAM_MEM iqs_gnd_cont_dur_glob;

/** IQS firmware version */
extern s_comms_xlinks_fw_version PLACE_IN_EXT_RAM_MEM iqs_fw_ver_glob;

/** IQS product key */
extern s_comms_xlinks_dev_prd_key PLACE_IN_EXT_RAM_MEM iqs_prod_key_glob;

/** IQS network properties */  
extern s_comms_xlinks_net_prop_cfg PLACE_IN_EXT_RAM_MEM iqs_net_prop_glob;

extern uint8_t xlinks_rx_ch_en[2];
//uint32 sband_lastProductKey[4] = {0x6C0D1190, 0xC62BB308, 0x0840A0B7, 0x1B083180};		//!< last valid product key to save computing power
uint32 sband_lastProductKey[4] = {0x57712726, 0xfea9f476, 0x4354fe02, 0x308a8b3e};
uint32 sband_oldProductKey[4];

float iqs_attn_tab[IQS_PWR_ROW][IQS_FRQ_COL] = {
    {14.4, 16.3, 16.4, 15.1, 12.6},
    {8.8, 10.8, 11.3, 9.8, 6.7},
    {5, 7, 7.5, 6, 2.6},
    {3.1, 5.5, 6, 4.1, 0.1},
    {0.1, 3.2, 3.8, 0.7, 0}
};

uint8 PLACE_IN_EXT_RAM_MEM sband_need_ss;			//!< ServiceServer use \sa SS_IDLE, SS_TCP, SS_UDP, SS_CSP

uint32 PLACE_IN_EXT_RAM_MEM sband_RcvDataServerTimeout;		//!< Timeout for ARP request
uint32 PLACE_IN_EXT_RAM_MEM sband_RcvDataIPAdr;			//!< Destination IP-Address for RF RcvData
uint32 PLACE_IN_EXT_RAM_MEM sband_RcvDataIPPort;			//!< Destination Port for RF RcvData
uint8 PLACE_IN_EXT_RAM_MEM  sband_RcvDataIPFormat;		//!< Received data can be send in different formats
uint32 PLACE_IN_EXT_RAM_MEM sband_xlink_rf_pwr_global;

//   (RF_RCV_SEND_FORMAT_RAW, RF_RCV_SEND_FORMAT_INFO, RF_RCV_SEND_FORMAT_DEBUG)
uint8 PLACE_IN_EXT_RAM_MEM  sband_RcvDataServerMacAdr[6];	//!< Destination MAC Address for RF RcvData

SERVICE_COMMON_TC_STRUCT PLACE_IN_EXT_RAM_MEM	sband_serv_tc;
SERVICE_COMMON_STRUCT PLACE_IN_EXT_RAM_MEM	sband_serv_common;
SERVICE_IDLE_STRUCT PLACE_IN_EXT_RAM_MEM	sband_serv_idle;
SERVICE_RESET_STRUCT PLACE_IN_EXT_RAM_MEM sband_serv_reset;
SERVICE_IP_ADR_STRUCT PLACE_IN_EXT_RAM_MEM        sband_serv_ip_adr;
SERVICE_VERSION_STRUCT	PLACE_IN_EXT_RAM_MEM        sband_serv_version;
SERVICE_VERSION_STRUCT_GET  PLACE_IN_EXT_RAM_MEM    sband_serv_version_get;
SERVICE_VERSION_STRUCT_GET  PLACE_IN_EXT_RAM_MEM    sband_serv_version_set;
SERVICE_START_RF_RCV_STRUCT PLACE_IN_EXT_RAM_MEM    sband_serv_start_rf_rcv;
SERVICE_RF_XMT_DATA_STRUCT	PLACE_IN_EXT_RAM_MEM	sband_serv_rf_xmt_data;
SERVICE_DEVICE_CONF_STRUCT  PLACE_IN_EXT_RAM_MEM	sband_serv_device_conf;
SERVICE_HOUSE_KEEP_STRUCT   PLACE_IN_EXT_RAM_MEM	sband_serv_house_keep;
SERVICE_FEATURE_SELECT_STRUCT PLACE_IN_EXT_RAM_MEM	sband_serv_feature_select;
SERVICE_DVBS2_CONF_STRUCT  PLACE_IN_EXT_RAM_MEM     sband_serv_dvbs2_cfg;
SERVICE_XPA_INF_STRUCT	PLACE_IN_EXT_RAM_MEM  sband_serv_xpa_inf;

/*	sband_Service_Validate_Key					*************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	checks if service_server is not blocked and received key is valid. For next service
 * 			server processing commands, the received features are stored temporary!
 *
 * @param	[in] RcvData (Pointer to the received service_data)
 *
 * @return	-1 if key is invalid, -2 if processing blocked, 0 if succeed/valid
 * @note		Its recommended to call this function before next service processing
 * 			functions are called! (because of features)
 *********************************************************************************************/



int8 sband_Service_Validate_Key(uint8 *RcvData)
{
    uint8 i = 0,status=0;
    uint32 rcvd_key[4];

    os_memcpy(rcvd_key,RcvData,16);

    for(i=0;i<4;i++)
    {
        if(rcvd_key[i] == sband_lastProductKey[i])
        {
            DEBUG_CPRINT(("\n\r key matched rcvd_key[%d]: %x",i,rcvd_key[i]));
            status =1;
        }
        else
        {
            DEBUG_CPRINT(("\n\r key mis-matched rcvd_key[%d]: %x",i,rcvd_key[i]));
            status = -1;
            break;
        }
    }
    return status;

}

/*	sband_Service_Idle					*********************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_IDLE command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in]  uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		none
 *********************************************************************************************/
uint32 sband_Service_Idle(uint8 *RcvData, uint32 DataRead)
{
    return 0;

}

/*	sband_Service_Reset					*********************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_RESET command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in]  uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		none
 *********************************************************************************************/
uint32 sband_Service_Reset(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}

/*	sband_Service_XmtData					*********************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_RF_XMT_DATA command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData -> service_event)
 * @param	[in]  uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed, 0- No Data processed or -1 if Error
 *
 * @note		count down sband_NumByteForAi for test with reduced throughput -> TCP-Break
 *			==> no AiRcv and AiXmt Processing; no ADC-DMA is running -> for 10ms interval
 *			==> CountDown * 8Bit * 100(10ms Tick) = Mbits/s
 *
 *			-> max. 350000 => 280MBits/s
 *			350000 -> @1.000ms timer_tick -> ~168MBits/s @ 61% CPU-Load
 *			350000 -> @0.500us timer_tick -> ~256MBits/s @ 95% CPU-Load
 *			350000 -> @0.333us timer_tick -> ~280MBits/s @ 99% CPU-Load
 *
 *			-> min. < 5500 could be a problem for the other side because long TCP-Break (>1s)
 *			500    -> @1.000ms timer_tick -> 400kBits/s @ 2.0% CPU-Load (smaller basic load)
 *			500    -> @0.500ms timer_tick -> 400kBits/s @ 3.3% CPU-Load
 *			500    -> @0.333ms timer_tick -> 400kBits/s @ 4.7% CPU-Load (more throughput possible)
 *
 *********************************************************************************************/
int32 sband_Service_XmtData(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}


/*	sband_Service_DevConf				*************************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_DEVICE_CONF command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in] uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		Call sband_Service_Validate_Key function before, to ensure features are filled
 *********************************************************************************************/
uint32 sband_Service_DevConf(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}


/*	sband_Service_GetFeatureSel		*************************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_FEATURE_SELECT command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in] uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		Call sband_Service_Validate_Key function before, to ensure features are filled
 *********************************************************************************************/
uint32 sband_Service_GetFeatureSel(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}

/*	sband_Service_GetHouseKeep		*************************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_HOUSE_KEEP command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in] uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		none
 *********************************************************************************************/
uint32 sband_Service_GetHouseKeep(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}

/*	sband_Service_StartRfRcv			*************************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_START_RF_RCV command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in] uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		none
 *********************************************************************************************/
uint32 sband_Service_StartRfRcv(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}


/*	sband_Service_Version				*************************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_VERSION command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in] uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		none
 *********************************************************************************************/
uint32 sband_Service_Version(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}

/*	sband_Service_IpAdr				*************************************************************/
/**
 * @ingroup	ServiceServerFunctionPub
 * @brief	processes a received SERVICE_IP_ADR or SERVICE_IP_ADR_SAVE command
 *
 * @param	[in] RcvData (Pointer to TcpRcvData after service_event)
 * @param	[in] uint32 DataRead (number of received bytes in TCP Buffer)
 *
 * @return	number of bytes which processed
 * @note		none
 *********************************************************************************************/
uint32 sband_Service_IpAdr(uint8 *RcvData, uint32 DataRead)
{
    return 0;
}

/**
 * @brief This function is used for processing the telecommand and
 *       send request to X-Band module via PS IPCC interface
 * @param serv_event Service event type.
 * @return ret - Execution status.
 *
 * This function handles processing of received S-Band telecommand packet.
 */
uint32 sband_proc_tc(uint32 serv_event)
{
    int ret;

#ifdef COMMS_TIME_DEBUG
    get_crnt_time_date(&tim_get_devconf);
    DEBUG_CPRINT(("\nIQS TIME TC PROC SER_EVNT %x ENT %d:%d:%d",serv_event, tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds));
    log_id_with_four_arg(LOG_INFO_L2,LOG_COMMS,IQS_TIME_TC_PROC_SER_EVNT_ENT_u8__u8__u8_SERV_EVENT___u8_,serv_event, tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds );
#endif 
    os_memcpy(sband_serv_tc.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
    DEBUG_CPRINT(("\nRECV_PRD_KEY:%ls\n",sband_lastProductKey));
    sband_serv_tc.service_event = serv_event;

    ret = sband_cmd_write(sizeof(SERVICE_COMMON_TC_STRUCT),(uint8_t*)&sband_serv_tc);

#ifdef COMMS_TIME_DEBUG
    get_crnt_time_date(&tim_get_devconf);
    DEBUG_CPRINT(("\nIQS TIME TC PROC EXT %d:%d:%d",tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds));
    log_id_with_three_arg(LOG_INFO_L2,LOG_COMMS,IQS_TIME_TC_PROC_EXT__u8__u8__u8_,tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds);
#endif
    return ret;
}


/**
 * @brief This function is used for processing the telecommand and
 *       send request to X-Band module via PS IPCC interface.
 * @brief Processes S-Band telecommand payload data.
 *
 * @param serv_event Service event type. 
 * @param pld Pointer to telecommand payload data.
 * @return status - Execution status.
 * 
 * This function processes the payload data of the received S-Band telecommand.
 */
uint32 sband_proc_tc_pld(uint32 serv_event, uint8 *pld)
{
    uint32 ret = 1;

#ifdef COMMS_TIME_DEBUG 
    get_crnt_time_date(&tim_get_devconf);
    DEBUG_CPRINT(("\nIQS TIME TC PROC PLD SERV_EVNT %x ENT %d:%d:%d",serv_event, tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds));
    log_id_with_three_arg(LOG_INFO_L2,LOG_COMMS,IQS_TIME_TC_PROC_PLD_SERV_EVNT__u8__u8__u8_,tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds);
#endif

    switch(serv_event & 0x0F)
    {
        case SERVICE_DEVICE_CONF:
            {
                os_memcpy(((uint8 *)(&sband_serv_device_conf)),pld,(sizeof(SERVICE_DEVICE_CONF_STRUCT)));
                os_memcpy(sband_serv_device_conf.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_device_conf.service_event = serv_event;

                //sband_tm_buff[0] =  MAX_SER_DEVICE_PRODUCT_KEY_CNT;
                //sband_tm_buff[1] =  MAX_SER_DEVICE_RCVCTRCH_CNT; 
                //os_memcpy(&sband_tm_buff[2],&sband_serv_device_conf,sizeof(SERVICE_DEVICE_CONF));

                //comms_sband_xlink_rx_cfg();
                //comms_sband_xlink_tx_cfg();

                DEBUG_CPRINT(("RX-Mixer: %d\n",sband_serv_device_conf.HwPins.RfeRx0On));
                DEBUG_CPRINT(("TX-Mixer: %d\n",sband_serv_device_conf.HwPins.RfeTx0On));
                DEBUG_CPRINT(("RfePa0On: %d\n",sband_serv_device_conf.HwPins.RfePa0On));
                DEBUG_CPRINT(("RfeRxTx0: %d\n",sband_serv_device_conf.HwPins.RfeRxTx0));
                DEBUG_CPRINT(("RfeRx1On: %d\n",sband_serv_device_conf.HwPins.RfeRx1On));
                DEBUG_CPRINT(("ERfeTx1On: %d\n",sband_serv_device_conf.HwPins.RfeTx1On));
                DEBUG_CPRINT(("RfeRxTx1: %d\n",sband_serv_device_conf.HwPins.RfeRxTx1));
                DEBUG_CPRINT(("RfePa1On: %d\n",sband_serv_device_conf.HwPins.RfePa1On));
                DEBUG_CPRINT(("RfeOutEn: %d\n",sband_serv_device_conf.HwPins.RfeOutEn));
                DEBUG_CPRINT(("RfeGpio0: %d\n",sband_serv_device_conf.HwPins.RfeGpio0));
                DEBUG_CPRINT(("RfeLoopDet: %d\n",sband_serv_device_conf.HwPins.RfeLoopDet));
                DEBUG_CPRINT(("RfeLoMux: %d\n",sband_serv_device_conf.HwPins.RfeLoMux));
                DEBUG_CPRINT(("RfePowOn: %d\n",sband_serv_device_conf.HwPins.RfePowOn));
                DEBUG_CPRINT(("RfeDA: %d\n",sband_serv_device_conf.HwPins.RfeDA));
                DEBUG_CPRINT(("RfeCL: %d\n",sband_serv_device_conf.HwPins.RfeCL));
                DEBUG_CPRINT(("RfeEnAD5304: %d\n",sband_serv_device_conf.HwPins.RfeEnAD5304));
                DEBUG_CPRINT(("RfeEnSynth: %d\n",sband_serv_device_conf.HwPins.RfeEnSynth));
                DEBUG_CPRINT(("RfeEn3: %d\n",sband_serv_device_conf.HwPins.RfeEn3));
                DEBUG_CPRINT(("RfeEn4r: %d\n",sband_serv_device_conf.HwPins.RfeEn4));
                DEBUG_CPRINT(("RfeEn5: %d\n",sband_serv_device_conf.HwPins.RfeEn5));
                DEBUG_CPRINT(("7V0 PowerOn enable: %d\n",sband_serv_device_conf.HwPins.Rfe7V0En));
                DEBUG_CPRINT(("Rfe reserve to uint32: %d\n",sband_serv_device_conf.HwPins.RfeRsvuint32));
                DEBUG_CPRINT(("receive DownConversionFrequency channel 0: %d\n",sband_serv_device_conf.RfeFreq.RxMix[0]));
                DEBUG_CPRINT(("receive DownConversionFrequency channle 1: %d\n",sband_serv_device_conf.RfeFreq.RxMix[1]));
                DEBUG_CPRINT(("transmit UpConversionFrequency channel 0: %d\n",sband_serv_device_conf.RfeFreq.TxMix[0]));
                DEBUG_CPRINT(("transmit UpConversionFrequency channel 1: %d\n",sband_serv_device_conf.RfeFreq.TxMix[1]));
                DEBUG_CPRINT(("receive Frequency Channel0: %d\n",sband_serv_device_conf.RfeFreq.RxFreq[0]));
                DEBUG_CPRINT(("receive Frequency Channel1: %d\n",sband_serv_device_conf.RfeFreq.RxFreq[1]));
                DEBUG_CPRINT(("transmit Frequency Channel0: %d\n",sband_serv_device_conf.RfeFreq.TxFreq[0]));
                DEBUG_CPRINT(("transmit Frequency Channel1: %d\n",sband_serv_device_conf.RfeFreq.TxFreq[1]));
                DEBUG_CPRINT(("Rcv lowest  possible frequency Channel0: %d\n",sband_serv_device_conf.LimFreq.RxLow[0]));
                DEBUG_CPRINT(("Rcv lowest  possible frequency Channel1: %d\n",sband_serv_device_conf.LimFreq.RxLow[1]));
                DEBUG_CPRINT(("Rcv highest possible frequency Channel0: %u\n",sband_serv_device_conf.LimFreq.RxHigh[0]));
                DEBUG_CPRINT(("Rcv highest possible frequency Channel1: %u\n",sband_serv_device_conf.LimFreq.RxHigh[1]));
                DEBUG_CPRINT(("Xmt lowest  possible frequency Channel0: %d\n",sband_serv_device_conf.LimFreq.TxLow[0]));
                DEBUG_CPRINT(("Xmt lowest  possible frequency Channel1: %d\n",sband_serv_device_conf.LimFreq.TxLow[1]));
                DEBUG_CPRINT(("Xmt highest possible frequency Channel0: %d\n",sband_serv_device_conf.LimFreq.TxHigh[0]));
                DEBUG_CPRINT(("Xmt highest possible frequency Channel1: %d\n",sband_serv_device_conf.LimFreq.TxHigh[1]));
                DEBUG_CPRINT(("Flag for use 2Rx2Tx mode: %d\n",sband_serv_device_conf.AdCtr._2Rx2Tx));
                DEBUG_CPRINT(("Tx Level for XmtChannel 0 (0...2047): %d\n",sband_serv_device_conf.AdCtr.TxLevel_0));
                DEBUG_CPRINT(("Tx Level for XmtChannel 1 (0...2047): %d\n",sband_serv_device_conf.AdCtr.TxLevel_1));
                DEBUG_CPRINT(("AD9361 reference clock in MHz: %d\n",sband_serv_device_conf.AdCtr.RefClk));
                DEBUG_CPRINT(("Reserve: %d\n",sband_serv_device_conf.AdCtr.Reserve));

                DEBUG_CPRINT(("Xmt Modulation Channel_0 : %d\n",sband_serv_device_conf.XmtCtr0.Modulat_0));
                DEBUG_CPRINT(("Xmt Mode Channel_0: %d\n",sband_serv_device_conf.XmtCtr0.Mode_0));
                DEBUG_CPRINT(("Xmt FEC Channel_0: %d\n",sband_serv_device_conf.XmtCtr0.Fec_0));
                DEBUG_CPRINT(("Xmt Enable Channel 0: %d\n",sband_serv_device_conf.XmtCtr0.ChEn_0));
                DEBUG_CPRINT(("Xmt PseudoRanomizer 0: %d\n",sband_serv_device_conf.XmtCtr0.PRnd_0));
                DEBUG_CPRINT(("Xmt ASM at FrmStart 0: %d\n",sband_serv_device_conf.XmtCtr0.ASMStart_0));
                DEBUG_CPRINT(("Xmt Offset Modulation 0: %d\n",sband_serv_device_conf.XmtCtr0.OffsetMod_0));
                DEBUG_CPRINT(("set FPGA output XmtOut_Data: %d\n",sband_serv_device_conf.XmtCtr0.Ch1_diff_Ch2));
                DEBUG_CPRINT(("Xmt On/Off Channel_0: %d\n",sband_serv_device_conf.XmtCtr0.ChSwitchOn_0));
                DEBUG_CPRINT(("Xmt Modulation Channel_1: %d\n",sband_serv_device_conf.XmtCtr0.Modulat_1));
                DEBUG_CPRINT(("Xmt Mode Channel_1: %d\n",sband_serv_device_conf.XmtCtr0.Mode_1));
                DEBUG_CPRINT(("Xmt FEC Channel_1: %d\n",sband_serv_device_conf.XmtCtr0.Fec_1));
                DEBUG_CPRINT(("Xmt Enable Channel 0: %d\n",sband_serv_device_conf.XmtCtr0.ChEn_1));
                DEBUG_CPRINT(("Xmt PseudoRanomizer 1: %d\n",sband_serv_device_conf.XmtCtr0.PRnd_1));
                DEBUG_CPRINT(("Xmt ASM at FrmStart 1: %d\n",sband_serv_device_conf.XmtCtr0.ASMStart_1));
                DEBUG_CPRINT(("Xmt Offset Modulation 1: %d\n",sband_serv_device_conf.XmtCtr0.OffsetMod_1));
                DEBUG_CPRINT(("Xmt On/Off Channel 1: %d\n",sband_serv_device_conf.XmtCtr0.ChSwitchOn_1));
                DEBUG_CPRINT(("Xmt 1-Offset Modulation normaly;: %d\n",sband_serv_device_conf.XmtCtr0.OffsetModNormaly));
                DEBUG_CPRINT(("AttTxGain_0: %d\n",sband_serv_device_conf.XmtCtr3.AttTxGain_0));
                DEBUG_CPRINT(("AttTxGain_1: %d\n",sband_serv_device_conf.XmtCtr3.AttTxGain_1));
                DEBUG_CPRINT(("TrFrmSz: %d\n",sband_serv_device_conf.XmtCtr3.TrFrmSz));

                DEBUG_CPRINT(("PhiOffset16: %d\n",sband_serv_device_conf.XmtCtr4.PhiOffset16));
                DEBUG_CPRINT(("y16: %d\n",sband_serv_device_conf.XmtCtr4.Y16));
                DEBUG_CPRINT(("TxSampFact: %d\n",sband_serv_device_conf.XmtCtr4.TxSampFact));
                DEBUG_CPRINT(("RsMode: %d\n",sband_serv_device_conf.XmtCtr4.RsMode));

                DEBUG_CPRINT(("RxGainMax: %d\n",sband_serv_device_conf.RcvCtrCh[0].RxGainMax));
                DEBUG_CPRINT(("ChEn: %d\n",sband_serv_device_conf.RcvCtrCh[0].ChEn));
                DEBUG_CPRINT(("Modulat: %d\n",sband_serv_device_conf.RcvCtrCh[0].Modulat));
                DEBUG_CPRINT(("MsbFirst: %d\n",sband_serv_device_conf.RcvCtrCh[0].MsbFirst));
                DEBUG_CPRINT(("Fec: %d\n",sband_serv_device_conf.RcvCtrCh[0].Fec));
                DEBUG_CPRINT(("PRnd: %d\n",sband_serv_device_conf.RcvCtrCh[0].PRnd));
                DEBUG_CPRINT(("BchDec: %d\n",sband_serv_device_conf.RcvCtrCh[0].BchDec));
                DEBUG_CPRINT(("AccBchErr: %d\n",sband_serv_device_conf.RcvCtrCh[0].AccBchErr));
                DEBUG_CPRINT(("RBchCountMode: %d\n",sband_serv_device_conf.RcvCtrCh[0].BchCountMode));
                DEBUG_CPRINT(("PowerOff: %d\n",sband_serv_device_conf.RcvCtrCh[0].PwrOff));
                DEBUG_CPRINT(("RcvCalib: %d\n",sband_serv_device_conf.RcvCtrCh[0].RcvCalib));

                DEBUG_CPRINT(("RxGainMax: %d\n",sband_serv_device_conf.RcvCtrCh[1].RxGainMax));
                DEBUG_CPRINT(("ChEn: %d\n",sband_serv_device_conf.RcvCtrCh[1].ChEn));
                DEBUG_CPRINT(("Modulat: %d\n",sband_serv_device_conf.RcvCtrCh[1].Modulat));
                DEBUG_CPRINT(("MsbFirst: %d\n",sband_serv_device_conf.RcvCtrCh[1].MsbFirst));
                DEBUG_CPRINT(("Fec: %d\n",sband_serv_device_conf.RcvCtrCh[1].Fec));
                DEBUG_CPRINT(("PRnd: %d\n",sband_serv_device_conf.RcvCtrCh[1].PRnd));
                DEBUG_CPRINT(("BchDec: %d\n",sband_serv_device_conf.RcvCtrCh[1].BchDec));
                DEBUG_CPRINT(("AccBchErr: %d\n",sband_serv_device_conf.RcvCtrCh[1].AccBchErr));
                DEBUG_CPRINT(("RBchCountMode: %d\n",sband_serv_device_conf.RcvCtrCh[1].BchCountMode));
                DEBUG_CPRINT(("PowerOff: %d\n",sband_serv_device_conf.RcvCtrCh[1].PwrOff));
                DEBUG_CPRINT(("RcvCalib: %d\n",sband_serv_device_conf.RcvCtrCh[1].RcvCalib));

                DEBUG_CPRINT(("XmtSymbFreq: %d\n",sband_serv_device_conf.SymbFreq.XmtSymbFreq));
                DEBUG_CPRINT(("RcvSymbFreq: %d\n",sband_serv_device_conf.SymbFreq.RcvSymbFreq));

                DEBUG_CPRINT(("RcvMode: %d\n",sband_serv_device_conf.RcvCtr_0.RcvMode));
                DEBUG_CPRINT(("RcvChannel: %d\n",sband_serv_device_conf.RcvCtr_0.RcvChannel));
                DEBUG_CPRINT(("ChSwitchOn_0: %d\n",sband_serv_device_conf.RcvCtr_0.ChSwitchOn_0));
                DEBUG_CPRINT(("ChSwitchOn_1: %d\n",sband_serv_device_conf.RcvCtr_0.ChSwitchOn_1));
                DEBUG_CPRINT(("DropSample: %d\n",sband_serv_device_conf.RcvCtr_0.DropSample));
                DEBUG_CPRINT(("dT_minus: %d\n",sband_serv_device_conf.RcvCtr_0.dT_minus));
                DEBUG_CPRINT(("dT_plus: %d\n",sband_serv_device_conf.RcvCtr_0.dT_plus));
                DEBUG_CPRINT(("Reserve: %d\n",sband_serv_device_conf.RcvCtr_0.RxState));
                DEBUG_CPRINT(("Reserve: %d\n",sband_serv_device_conf.RcvCtr_0.ModType));
                DEBUG_CPRINT(("Reserve: %d\n",sband_serv_device_conf.RcvCtr_0.Reserve));

                ret = sband_cmd_write(sizeof(SERVICE_DEVICE_CONF_STRUCT),(uint8_t*)&sband_serv_device_conf);
            }
            break;

        case SERVICE_VERSION:
            {
                os_memcpy(((uint8_t *)(&sband_serv_version)),pld,((sizeof(SERVICE_VERSION_STRUCT))));
                os_memcpy(sband_serv_version.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_version.service_event = serv_event;

                DEBUG_CPRINT(("Seconds: %d\n",sband_serv_version.prog_version.Seconds));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SECONDS__u32_,sband_serv_version.prog_version.Seconds);
                DEBUG_CPRINT(("Minutes: %d\n",sband_serv_version.prog_version.Minutes));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MINUTES__u32_,sband_serv_version.prog_version.Minutes);
                DEBUG_CPRINT(("Hours: %d\n",sband_serv_version.prog_version.Hours));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,HOURS__u32_,sband_serv_version.prog_version.Hours);
                DEBUG_CPRINT(("Year: %d\n",sband_serv_version.prog_version.Year));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,YEAR__u32_,sband_serv_version.prog_version.Year);
                DEBUG_CPRINT(("Months: %d\n",sband_serv_version.prog_version.Months));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MONTHS__u32_,sband_serv_version.prog_version.Months);
                DEBUG_CPRINT(("Day: %d\n",sband_serv_version.prog_version.Day));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DAY__u32_,sband_serv_version.prog_version.Day);
                DEBUG_CPRINT(("FileName : %hhn\n",sband_serv_version.prog_version.FileName));
                log_string(LOG_INFO_L1,LOG_COMMS,"FILE_NAME__u8_");
                log_string(LOG_INFO_L1,LOG_COMMS,(char *)sband_serv_version.prog_version.FileName);
                DEBUG_CPRINT(("Start_address: %d\n",sband_serv_version.prog_version.start_adr));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,START_ADDRESS__u32_,sband_serv_version.prog_version.start_adr);
                DEBUG_CPRINT(("Len: %d\n",sband_serv_version.prog_version.len));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,LENGTH__u32_,sband_serv_version.prog_version.len);
                DEBUG_CPRINT(("Flags: %d\n",sband_serv_version.prog_version.Flags));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FLAGS__u32_,sband_serv_version.prog_version.Flags);
                //sband_tm_buff[0] =  MAX_SER_VER_PRODUCT_KEY_CNT ;
                //os_memcpy(&sband_tm_buff[1],&sband_serv_version,sizeof(SERVICE_VERSION));

                ret = sband_cmd_write(sizeof(SERVICE_VERSION_STRUCT),(uint8_t*)&sband_serv_version); // TODO size to be updated based on number of firmwares
            }
            break;

        case SERVICE_IP_ADR:
            {
                os_memcpy((uint8_t *)(&sband_serv_ip_adr),pld,(sizeof(SERVICE_IP_ADR_STRUCT)));
                os_memcpy(sband_serv_ip_adr.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_ip_adr.service_event = serv_event;
                sband_serv_ip_adr.ip_address = inet_addr ("100.1.168.192");

                DEBUG_CPRINT(("ip_address: %x\n",sband_serv_ip_adr.ip_address));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SERVICE_IP_ADDRESS__hex_,sband_serv_ip_adr.ip_address);
                ret = sband_cmd_write(sizeof(SERVICE_IP_ADR_STRUCT),(uint8_t*)&sband_serv_ip_adr);
            }
            break;

        case SERVICE_IP_ADR_SAVE:
            {
                os_memcpy((uint8_t *)(&sband_serv_ip_adr),pld,(sizeof(SERVICE_IP_ADR_STRUCT)));
                os_memcpy(sband_serv_ip_adr.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_ip_adr.service_event = serv_event;
                sband_serv_ip_adr.ip_address = inet_addr ("1.1.168.192");

                DEBUG_CPRINT(("ip_address: %x\n",sband_serv_ip_adr.ip_address));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SERVICE_IP_ADDRESS__hex_,sband_serv_ip_adr.ip_address);
                //sband_tm_buff[0] =  MAX_SER_IP_PRODUCT_KEY_CNT;
                //os_memcpy(&sband_tm_buff[1],&sband_serv_ip_adr,sizeof(SERVICE_IP_ADR_STRUCT));

                ret = sband_cmd_write(sizeof(SERVICE_IP_ADR_STRUCT),(uint8_t*)&sband_serv_ip_adr);
            }
            break;

#if 0
            /** SET option is not available in current documentation */
        case SERVICE_FLAGS_CONF:
            {
                os_memcpy(sband_serv_.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_tc.service_event = serv_event;
                os_memcpy(&sband_serv_ip_adr.ip_address,pld,(sizeof(uint32)));

                ret = sband_cmd_write(sizeof(SERVICE_VERSION_STRUCT),&sband_serv_ip_adr);
            }
            break;
#endif
        case SERVICE_DVBS2_CONF:
            {
                os_memcpy(((uint8 *)(&sband_serv_dvbs2_cfg)) + sizeof(uint32),pld,(sizeof(SERVICE_DVBS2_CONF_STRUCT)));
                os_memcpy(sband_serv_dvbs2_cfg.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_dvbs2_cfg.service_event = serv_event;


                DEBUG_CPRINT(("ModCod_0: %d\n",sband_serv_dvbs2_cfg.ModCod_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MODCOD_0__u32_,sband_serv_dvbs2_cfg.ModCod_0);
                DEBUG_CPRINT(("FrmType_0: %d\n",sband_serv_dvbs2_cfg.FrmType_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FRMTYPE_0__u32_,sband_serv_dvbs2_cfg.FrmType_0);
                DEBUG_CPRINT(("InsertPilots_0: %d\n",sband_serv_dvbs2_cfg.InsertPilots_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,INSERT_PILOTS_0__u32_,sband_serv_dvbs2_cfg.InsertPilots_0);
                DEBUG_CPRINT(("Acm_Ccm_0: %d\n",sband_serv_dvbs2_cfg.Acm_Ccm_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ACM_CCM_0__u32_,sband_serv_dvbs2_cfg.Acm_Ccm_0);
                DEBUG_CPRINT(("RollOffVal_0: %d\n",sband_serv_dvbs2_cfg.RollOffVal_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ROLLOFF_VAL_0__u32_,sband_serv_dvbs2_cfg.RollOffVal_0);
                DEBUG_CPRINT(("PlSpreadNumber_0: %d\n",sband_serv_dvbs2_cfg.PlSpreadNumber_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PL_SPREAD_NUMBER_0__u32_,sband_serv_dvbs2_cfg.PlSpreadNumber_0);
                DEBUG_CPRINT(("Reserve_0: %d\n",sband_serv_dvbs2_cfg.Reserve_0));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RESERVE_0__u32_,sband_serv_dvbs2_cfg.Reserve_0);
                DEBUG_CPRINT(("Reserve_1: %d\n",sband_serv_dvbs2_cfg.Reserve_1));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RESERVE_1__u32_,sband_serv_dvbs2_cfg.Reserve_1);
                //os_memcpy(((uint8 *)(&sband_serv_dvbs2_cfg.service_event)) + sizeof(uint32),pld,((sizeof(SERVICE_DVBS2_CONF_STRUCT))-(sizeof(uint32 )* 5)));

                //sband_tm_buff[0] =  MAX_SER_DVB_PRODUCT_KEY_CNT;
                //os_memcpy(&sband_tm_buff[1],&sband_serv_dvbs2_cfg,sizeof(SERVICE_DVBS2_CONF));

                ret = sband_cmd_write(sizeof(SERVICE_DVBS2_CONF_STRUCT),(uint8_t*)&sband_serv_dvbs2_cfg);
            }
            break;

        case SERVICE_START_RF_RCV:
            {
                os_memcpy(((uint8 *)&sband_serv_start_rf_rcv),pld,(sizeof(SERVICE_START_RF_RCV_STRUCT)));
                os_memcpy(sband_serv_start_rf_rcv.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_start_rf_rcv.service_event = serv_event;

                DEBUG_CPRINT(("Rcv_address: %d\n",sband_serv_start_rf_rcv.ip_address));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RECEIVE_ADDRESS__u32_,sband_serv_start_rf_rcv.ip_address);
                DEBUG_CPRINT(("Port: %d\n",sband_serv_start_rf_rcv.port));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PORT__u32_,sband_serv_start_rf_rcv.port);
                DEBUG_CPRINT(("send_format: %d\n",sband_serv_start_rf_rcv.send_format));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SEND_FORMAT__u32_,sband_serv_start_rf_rcv.send_format);
                DEBUG_CPRINT(("send_format: %d\n",sband_serv_start_rf_rcv.output_interface));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,OUTPUT_INTERFACE__u32_,sband_serv_start_rf_rcv.output_interface);
                DEBUG_CPRINT(("Reserve: %d\n",sband_serv_start_rf_rcv.Reserve));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RESERVE__u32_,sband_serv_start_rf_rcv.Reserve);
                //os_memcpy(((uint8 *)&sband_serv_start_rf_rcv.ip_address),pld,((sizeof(SERVICE_START_RF_RCV_STRUCT))-(sizeof(uint32 )* 5)));
                //sband_tm_buff[0] =  MAX_SER_START_PRODUCT_KEY_CNT;
                //os_memcpy(&sband_tm_buff[1],&sband_serv_start_rf_rcv,sizeof(SERVICE_START_RF_RCV));

                ret = sband_cmd_write(sizeof(SERVICE_START_RF_RCV_STRUCT),(uint8_t*)&sband_serv_start_rf_rcv);
            }
            break;

        case SERVICE_RF_XMT_DATA:
            {
                os_memcpy((uint8 *)(&sband_serv_rf_xmt_data) ,pld,(sizeof(SERVICE_RF_XMT_DATA_STRUCT)));
                os_memcpy(sband_serv_rf_xmt_data.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
                sband_serv_rf_xmt_data.service_event = serv_event;


                DEBUG_CPRINT(("PktType: %d\n",sband_serv_rf_xmt_data.PktType));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PKT_TYPE__u32_,sband_serv_rf_xmt_data.PktType);
                DEBUG_CPRINT(("ErrInf: %d\n",sband_serv_rf_xmt_data.ErrInf));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ERR_INF__u32_,sband_serv_rf_xmt_data.ErrInf);
                DEBUG_CPRINT(("PktLen: %d\n",sband_serv_rf_xmt_data.PktLen));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PKT_LEN__u32_,sband_serv_rf_xmt_data.PktLen);
                DEBUG_CPRINT(("EthFirst: %d\n",sband_serv_rf_xmt_data.EthFirst));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ETH_FIRST__u32_,sband_serv_rf_xmt_data.EthFirst);
                DEBUG_CPRINT(("EthLast: %d\n",sband_serv_rf_xmt_data.EthLast));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ETH_LAST__u32_,sband_serv_rf_xmt_data.EthLast);
                DEBUG_CPRINT(("EthNumByte: %d\n",sband_serv_rf_xmt_data.EthNumByte));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ETH_NUM_BYTE__u32_,sband_serv_rf_xmt_data.EthNumByte);
                DEBUG_CPRINT(("Moreinfo: %d\n",sband_serv_rf_xmt_data.MoreInfo));
                log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MORE_INFO__u32_,sband_serv_rf_xmt_data.MoreInfo);
                DEBUG_CPRINT(("Payload"));
                for(int i=0;i<MAX_SER_RF_PAYLOAD_CNT;i++)
                {
                    DEBUG_CPRINT(("%d ",sband_serv_rf_xmt_data.Payload[i]));
                    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PAYLOAD__u8_,sband_serv_rf_xmt_data.Payload[i]);
                }
                DEBUG_CPRINT(("\n"));
                //os_memcpy((uint8 *)(&sband_serv_rf_xmt_data.service_event) + (sizeof(uint32)) ,pld,((sizeof(SERVICE_RF_XMT_DATA_STRUCT))-(sizeof(uint32 )* 5)));
                // sband_tm_buff[0] =  MAX_SER_RF_XMT_PRODUCT_KEY_CNT;
                //sband_tm_buff[1] =  (uint8_t)MAX_SER_RF_PAYLOAD_CNT & 0xff;
                //sband_tm_buff[2] =  (uint8_t)MAX_SER_RF_PAYLOAD_CNT >> 8;

                //os_memcpy(&sband_tm_buff[3],&sband_serv_rf_xmt_data,sizeof(SERVICE_RF_XMT_DATA));

                ret = sband_cmd_write(sizeof(SERVICE_RF_XMT_DATA_STRUCT),(uint8_t*)&sband_serv_rf_xmt_data);
            }
            break;
    }
#ifdef COMMS_TIME_DEBUG
    get_crnt_time_date(&tim_get_devconf);
    DEBUG_CPRINT(("\nIQS TIME TC PROC PLD SERV_EVNT %x EXT %d:%d:%d",serv_event, tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds));
    log_id_with_four_arg(LOG_INFO_L2,LOG_COMMS,IQS_TIME_TC_PROC_PLD_SERV_ENT_u8__u8__u8__SERV_EVENT__hex_,serv_event, tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds);
#endif
    return ret;

}


/**
 * @brief This function is used for processing the control information received
 *        from X-Band module via IPCC interface
 *
 * @param RcvData Pointer to received data buffer. 
 * @param length Length of received data in bytes.
 * @return Execution status.
 *
 * This function handles processing of received S-Band data.
 */
uint32 sband_proc_rcv_data(uint8 *RcvData, int length)
{
    //int8 icd_rsp_status = (int8)(*RcvData);
    uint8 status = 0;
    uint16 tm_size = 0;

    DEBUG_CPRINT(("\nReceived S-Band IQS response \n"));
    log_id(LOG_INFO_L1,LOG_COMMS,RECEIVED_SBAND_IQS_RESPONSE);

    SERVICE_COMMON_STRUCT *rcv_data = NULL;

    rcv_data = (SERVICE_COMMON_STRUCT*)(RcvData);
    //rcv_data = (SERVICE_COMMON_STRUCT*)(RcvData + sizeof(int8));

    status = sband_Service_Validate_Key((uint8*)rcv_data);

    if(status)
    {
        DEBUG_CPRINT(("\n\r Received Service event: %x",rcv_data->service_event));
        log_id_with_one_arg(LOG_INFO_L3,LOG_COMMS,RECEIVED_SERVICE_EVENT__hex_,rcv_data->service_event);
        if(SERVICE_ACK == (0xC0000000 & rcv_data->service_event))
        {
            status = SBAND_TM_SUCCESS;

            switch((rcv_data->service_event & 0x3FFFFFFF))
            {
                case (SERVICE_SET | SERVICE_IDLE):
                    {
                        sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_IDLE_CFG,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_DEVICE_CONF):
                    {
                        //sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_DEVICE_CONF,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_VERSION):
                    {
                        //sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_VERSION,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_IP_ADR):
                    {
                        //sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_IP_ADR,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_IP_ADR_SAVE):
                    {
                        //sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_IP_ADR_SAVE,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_FLAGS_CONF):
                    {
                        sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_FLAGS_CONF,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_DVBS2_CONF):
                    {
                        sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_DVBS2_CONF,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_START_RF_RCV):
                    {
                        //sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_START_RF_RCV,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_RF_XMT_DATA):
                    {
                        sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_RF_XMT_DATA,status);
                    }
                    break;
                case (SERVICE_SET | SERVICE_RESET):
                    {
                        //sband_tc_tm_set_rsp_hdlr(TM_SBAND_SET_RESET,status);
                    }
                    break;
                case (SERVICE_GET | SERVICE_HOUSE_KEEP):
                    {
                        tm_size = sizeof(SERVICE_HOUSE_KEEP_STRUCT);
                        os_memcpy((uint8_t *)&sband_serv_house_keep,(uint8_t *)rcv_data,tm_size);

                        DEBUG_CPRINT(("AD9361: %d\n",sband_serv_house_keep.HouseKeep.Temp.AD9361));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,AD9361__u32_,sband_serv_house_keep.HouseKeep.Temp.AD9361);
                        DEBUG_CPRINT(("EthPhy: %d\n",sband_serv_house_keep.HouseKeep.Temp.EthPhy));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ETHPHY__u32_,sband_serv_house_keep.HouseKeep.Temp.EthPhy);
                        DEBUG_CPRINT(("Zynq: %d\n",sband_serv_house_keep.HouseKeep.Temp.Zynq));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ZYNQ__u32_,sband_serv_house_keep.HouseKeep.Temp.Zynq);
                        DEBUG_CPRINT(("Rest: %d\n",sband_serv_house_keep.HouseKeep.Temp.Rest));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,REST__u32_,sband_serv_house_keep.HouseKeep.Temp.Rest);
                        DEBUG_CPRINT(("On: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].On));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ON__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].On);
                        DEBUG_CPRINT(("Sync: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].Sync));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SYNC__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].Sync);
                        DEBUG_CPRINT(("idxGain: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].idxGain));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,IDXGAIN__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].idxGain);
                        DEBUG_CPRINT(("SirMin: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].SirMin));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SIRMIN__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].SirMin);
                        DEBUG_CPRINT(("SirMax: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].SirMax));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SIRMAX__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].SirMax);
                        DEBUG_CPRINT(("DopplerFreq: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].DopplerFreq));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DOPPLERFREQ__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].DopplerFreq);
                        DEBUG_CPRINT(("Rest0: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].Rest0));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,REST_0__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].Rest0);
                        DEBUG_CPRINT(("PwrMin: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].PwrMin));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PWR_MIN__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].PwrMin);
                        DEBUG_CPRINT(("PwrMax: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].PwrMax));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,PWRMAX__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].PwrMax);
                        DEBUG_CPRINT(("RxState: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].RxState));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RXSTATE__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].RxState);
                        DEBUG_CPRINT(("Rest1: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].Rest1));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,REST1__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].Rest1);
                        DEBUG_CPRINT(("BchBlockCount: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].BchBlockCount));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,BCHBLOCK_COUNT__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].BchBlockCount);
                        DEBUG_CPRINT(("BchErrCount: %d\n",sband_serv_house_keep.HouseKeep.RcvInfCh[0].BchErrCount));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,BCH_ERR_COUNT__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].BchErrCount);
                        DEBUG_CPRINT(("On: %d\n",sband_serv_house_keep.HouseKeep.XmtInfCh[0].On));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ON__u32_,sband_serv_house_keep.HouseKeep.RcvInfCh[0].On);
                        DEBUG_CPRINT(("Modulation: %d\n",sband_serv_house_keep.HouseKeep.XmtInfCh[0].Modulation));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MODULATION__u32_,sband_serv_house_keep.HouseKeep.XmtInfCh[0].Modulation);
                        DEBUG_CPRINT(("Fec: %d\n",sband_serv_house_keep.HouseKeep.XmtInfCh[0].Fec));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FEC__u32_,sband_serv_house_keep.HouseKeep.XmtInfCh[0].Fec);
                        DEBUG_CPRINT(("Att: %d\n",sband_serv_house_keep.HouseKeep.XmtInfCh[0].Att));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,ATT__u32_,sband_serv_house_keep.HouseKeep.XmtInfCh[0].Att);
                        DEBUG_CPRINT(("Rest: %d\n",sband_serv_house_keep.HouseKeep.XmtInfCh[0].Rest));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,REST__u32_,sband_serv_house_keep.HouseKeep.XmtInfCh[0].Rest);
                        DEBUG_CPRINT(("card_run_counter: %d\n",sband_serv_house_keep.HouseKeep.card_run_counter));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CARD_RUN_COUNTER__u32_,sband_serv_house_keep.HouseKeep.card_run_counter);

                    }
                    break;

                case (SERVICE_GET | SERVICE_XPA_INF):
                    {
                        tm_size = sizeof(SERVICE_XPA_INF);
                        os_memcpy(&sband_serv_xpa_inf,rcv_data,tm_size);
                    }
                    break;

                case (SERVICE_GET | SERVICE_FEATURE_SELECT):
                    {
                        tm_size = sizeof(SERVICE_FEATURE_SELECT_STRUCT);
                        os_memcpy(&sband_serv_feature_select,rcv_data,tm_size);

                        tm_size = tm_size - 5*sizeof(uint32);

                        DEBUG_CPRINT(("Crc16: %d\n",sband_serv_feature_select.feature_select.Crc16));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,CRC16__u32_,sband_serv_feature_select.feature_select.Crc16);
                        DEBUG_CPRINT(("Reserve_0: %d\n",sband_serv_feature_select.feature_select.Reserve_0));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RESERVE_0__u32_,sband_serv_feature_select.feature_select.Reserve_0);
                        DEBUG_CPRINT(("MoreXmtMod: %d\n",sband_serv_feature_select.feature_select.MoreXmtMod));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MORE_XMT_MOD__u32_,sband_serv_feature_select.feature_select.MoreXmtMod);
                        DEBUG_CPRINT(("XmtOffsetMod: %d\n",sband_serv_feature_select.feature_select.XmtOffsetMod));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,XMTOFFSETMOD__u32_,sband_serv_feature_select.feature_select.XmtOffsetMod);
                        DEBUG_CPRINT(("RsMode: %d\n",sband_serv_feature_select.feature_select.RsMode));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RSMODE__u32_,sband_serv_feature_select.feature_select.RsMode);
                        DEBUG_CPRINT(("XmtCh0_En: %d\n",sband_serv_feature_select.feature_select.XmtCh0_En));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,XMT_CH_0_EN__u32_,sband_serv_feature_select.feature_select.XmtCh0_En);
                        DEBUG_CPRINT(("XmtCh1_En: %d\n",sband_serv_feature_select.feature_select.XmtCh1_En));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,XMT_CH_1_EN__u32_,sband_serv_feature_select.feature_select.XmtCh1_En);
                        DEBUG_CPRINT(("RcvCh0_En: %d\n",sband_serv_feature_select.feature_select.RcvCh0_En));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RCV_CH_0__u32_,sband_serv_feature_select.feature_select.RcvCh0_En);
                        DEBUG_CPRINT(("RcvCh1_Ene: %d\n",sband_serv_feature_select.feature_select.RcvCh1_En));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RCV_CH_1__u32_,sband_serv_feature_select.feature_select.RcvCh1_En);
                        DEBUG_CPRINT(("RcvCh0_GainMax: %d\n",sband_serv_feature_select.feature_select.RcvCh0_GainMax));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RCVCH_0_GAIN_MAX__u32_,sband_serv_feature_select.feature_select.RcvCh0_GainMax);
                        DEBUG_CPRINT(("RcvCh1_GainMax: %d\n",sband_serv_feature_select.feature_select.RcvCh1_GainMax));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RCVCH_1_GAIN_MAX__u32_,sband_serv_feature_select.feature_select.RcvCh1_GainMax);
                        DEBUG_CPRINT(("DisableXmtSymbFreqChange: %d\n",sband_serv_feature_select.feature_select.DisableXmtSymbFreqChange));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DISABLE_XMT_SYMB_FREQ_CHANGE__u32_,sband_serv_feature_select.feature_select.DisableXmtSymbFreqChange);
                        DEBUG_CPRINT(("DisableRcvSymbFreqChange: %d\n",sband_serv_feature_select.feature_select.DisableRcvSymbFreqChange));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DISABLE_RCV_SYMB_FREQ_CHANGE_u32_,sband_serv_feature_select.feature_select.DisableRcvSymbFreqChange);
                        DEBUG_CPRINT(("DisableXmtUpSampling: %d\n",sband_serv_feature_select.feature_select.DisableXmtUpSampling));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DISABLE_XMT_UP_SAMPLING__u32_,sband_serv_feature_select.feature_select.DisableXmtUpSampling);
                        DEBUG_CPRINT(("Reserve_1: %d\n",sband_serv_feature_select.feature_select.Reserve_1));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RESERVE_1__u32_,sband_serv_feature_select.feature_select.Reserve_1);
                        DEBUG_CPRINT(("XmtSymbFreq: %d\n",sband_serv_feature_select.feature_select.XmtSymbFreq));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,XMT_SYMB_FREQ__u32_,sband_serv_feature_select.feature_select.XmtSymbFreq);
                        DEBUG_CPRINT(("RcvSymbFreq: %d\n",sband_serv_feature_select.feature_select.RcvSymbFreq));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RCVSYMB_FREQ__u32_,sband_serv_feature_select.feature_select.RcvSymbFreq);
                        DEBUG_CPRINT(("Reserve_3: %d\n",sband_serv_feature_select.feature_select.Reserve_3));
                        log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,RESERVE_3__u32_,sband_serv_feature_select.feature_select.Reserve_3);
                    }
                    break;

                case (SERVICE_GET | SERVICE_IP_ADR):
                    {
                        tm_size = sizeof(SERVICE_IP_ADR_STRUCT);
                        os_memcpy(&sband_serv_ip_adr,rcv_data,tm_size);
                        tm_size = 4;
                        DEBUG_CPRINT(("ip_address: %x\n",sband_serv_ip_adr.ip_address));
                        log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,IP_ADDRESS__u32_,sband_serv_ip_adr.ip_address);
                    }
                    break;

                case (SERVICE_GET | SERVICE_DEVICE_CONF):
                    {
                        tm_size = sizeof(SERVICE_DEVICE_CONF_STRUCT);
                        os_memcpy(&sband_serv_device_conf,rcv_data,tm_size);

                        tm_size = tm_size - 5*sizeof(uint32);

                        DEBUG_CPRINT(("RX-Mixer: %d\n",sband_serv_device_conf.HwPins.RfeRx0On));
                        DEBUG_CPRINT(("TX-Mixer: %d\n",sband_serv_device_conf.HwPins.RfeTx0On));
                        DEBUG_CPRINT(("RfePa0On: %d\n",sband_serv_device_conf.HwPins.RfePa0On));
                        DEBUG_CPRINT(("RfeRxTx0: %d\n",sband_serv_device_conf.HwPins.RfeRxTx0));
                        DEBUG_CPRINT(("RfeRx1On: %d\n",sband_serv_device_conf.HwPins.RfeRx1On));
                        DEBUG_CPRINT(("ERfeTx1On: %d\n",sband_serv_device_conf.HwPins.RfeTx1On));
                        DEBUG_CPRINT(("RfeRxTx1: %d\n",sband_serv_device_conf.HwPins.RfeRxTx1));
                        DEBUG_CPRINT(("RfePa1On: %d\n",sband_serv_device_conf.HwPins.RfePa1On));
                        DEBUG_CPRINT(("RfeOutEn: %d\n",sband_serv_device_conf.HwPins.RfeOutEn));
                        DEBUG_CPRINT(("RfeGpio0: %d\n",sband_serv_device_conf.HwPins.RfeGpio0));
                        DEBUG_CPRINT(("RfeLoopDet: %d\n",sband_serv_device_conf.HwPins.RfeLoopDet));
                        DEBUG_CPRINT(("RfeLoMux: %d\n",sband_serv_device_conf.HwPins.RfeLoMux));
                        DEBUG_CPRINT(("RfePowOn: %d\n",sband_serv_device_conf.HwPins.RfePowOn));
                        DEBUG_CPRINT(("RfeDA: %d\n",sband_serv_device_conf.HwPins.RfeDA));
                        DEBUG_CPRINT(("RfeCL: %d\n",sband_serv_device_conf.HwPins.RfeCL));
                        DEBUG_CPRINT(("RfeEnAD5304: %d\n",sband_serv_device_conf.HwPins.RfeEnAD5304));
                        DEBUG_CPRINT(("RfeEnSynth: %d\n",sband_serv_device_conf.HwPins.RfeEnSynth));
                        DEBUG_CPRINT(("RfeEn3: %d\n",sband_serv_device_conf.HwPins.RfeEn3));
                        DEBUG_CPRINT(("RfeEn4r: %d\n",sband_serv_device_conf.HwPins.RfeEn4));
                        DEBUG_CPRINT(("RfeEn5: %d\n",sband_serv_device_conf.HwPins.RfeEn5));
                        DEBUG_CPRINT(("7V0 PowerOn enable: %d\n",sband_serv_device_conf.HwPins.Rfe7V0En));
                        DEBUG_CPRINT(("Rfe reserve to uint32: %d\n",sband_serv_device_conf.HwPins.RfeRsvuint32));
                        DEBUG_CPRINT(("receive DownConversionFrequency channel 0: %d\n",sband_serv_device_conf.RfeFreq.RxMix[0]));
                        DEBUG_CPRINT(("receive DownConversionFrequency channle 1: %d\n",sband_serv_device_conf.RfeFreq.RxMix[1]));
                        DEBUG_CPRINT(("transmit UpConversionFrequency channel 0: %d\n",sband_serv_device_conf.RfeFreq.TxMix[0]));
                        DEBUG_CPRINT(("transmit UpConversionFrequency channel 1: %d\n",sband_serv_device_conf.RfeFreq.TxMix[1]));
                        DEBUG_CPRINT(("receive Frequency Channel0: %d\n",sband_serv_device_conf.RfeFreq.RxFreq[0]));
                        DEBUG_CPRINT(("receive Frequency Channel1: %d\n",sband_serv_device_conf.RfeFreq.RxFreq[1]));
                        DEBUG_CPRINT(("transmit Frequency Channel0: %d\n",sband_serv_device_conf.RfeFreq.TxFreq[0]));
                        DEBUG_CPRINT(("transmit Frequency Channel1: %d\n",sband_serv_device_conf.RfeFreq.TxFreq[1]));
                        DEBUG_CPRINT(("Rcv lowest  possible frequency Channel0: %d\n",sband_serv_device_conf.LimFreq.RxLow[0]));
                        DEBUG_CPRINT(("Rcv lowest  possible frequency Channel1: %d\n",sband_serv_device_conf.LimFreq.RxLow[1]));
                        DEBUG_CPRINT(("Rcv highest possible frequency Channel0: %u\n",sband_serv_device_conf.LimFreq.RxHigh[0]));
                        DEBUG_CPRINT(("Rcv highest possible frequency Channel1: %u\n",sband_serv_device_conf.LimFreq.RxHigh[1]));
                        DEBUG_CPRINT(("Xmt lowest  possible frequency Channel0: %d\n",sband_serv_device_conf.LimFreq.TxLow[0]));
                        DEBUG_CPRINT(("Xmt lowest  possible frequency Channel1: %d\n",sband_serv_device_conf.LimFreq.TxLow[1]));
                        DEBUG_CPRINT(("Xmt highest possible frequency Channel0: %d\n",sband_serv_device_conf.LimFreq.TxHigh[0]));
                        DEBUG_CPRINT(("Xmt highest possible frequency Channel1: %d\n",sband_serv_device_conf.LimFreq.TxHigh[1]));
                        DEBUG_CPRINT(("Flag for use 2Rx2Tx mode: %d\n",sband_serv_device_conf.AdCtr._2Rx2Tx));
                        DEBUG_CPRINT(("Tx Level for XmtChannel 0 (0...2047): %d\n",sband_serv_device_conf.AdCtr.TxLevel_0));
                        DEBUG_CPRINT(("Tx Level for XmtChannel 1 (0...2047): %d\n",sband_serv_device_conf.AdCtr.TxLevel_1));
                        DEBUG_CPRINT(("AD9361 reference clock in MHz: %d\n",sband_serv_device_conf.AdCtr.RefClk));
                        DEBUG_CPRINT(("Reserve: %d\n",sband_serv_device_conf.AdCtr.Reserve));

                        DEBUG_CPRINT(("Xmt Modulation Channel_0 : %d\n",sband_serv_device_conf.XmtCtr0.Modulat_0));
                        DEBUG_CPRINT(("Xmt Mode Channel_0: %d\n",sband_serv_device_conf.XmtCtr0.Mode_0));
                        DEBUG_CPRINT(("Xmt FEC Channel_0: %d\n",sband_serv_device_conf.XmtCtr0.Fec_0));
                        DEBUG_CPRINT(("Xmt Enable Channel 0: %d\n",sband_serv_device_conf.XmtCtr0.ChEn_0));
                        DEBUG_CPRINT(("Xmt PseudoRanomizer 0: %d\n",sband_serv_device_conf.XmtCtr0.PRnd_0 ));
                        DEBUG_CPRINT(("Xmt ASM at FrmStart 0: %d\n",sband_serv_device_conf.XmtCtr0.ASMStart_0));
                        DEBUG_CPRINT(("Xmt Offset Modulation 0: %d\n",sband_serv_device_conf.XmtCtr0.OffsetMod_0));
                        DEBUG_CPRINT(("set FPGA output XmtOut_Data: %d\n",sband_serv_device_conf.XmtCtr0.Ch1_diff_Ch2));
                        DEBUG_CPRINT(("Xmt On/Off Channel_0: %d\n",sband_serv_device_conf.XmtCtr0.ChSwitchOn_0));
                        DEBUG_CPRINT(("Xmt Modulation Channel_1: %d\n",sband_serv_device_conf.XmtCtr0.Modulat_1));
                        DEBUG_CPRINT(("Xmt Mode Channel_1: %d\n",sband_serv_device_conf.XmtCtr0.Mode_1));
                        DEBUG_CPRINT(("Xmt FEC Channel_1: %d\n",sband_serv_device_conf.XmtCtr0.Fec_1));
                        DEBUG_CPRINT(("Xmt Enable Channel 1: %d\n",sband_serv_device_conf.XmtCtr0.ChEn_1));
                        DEBUG_CPRINT(("Xmt PseudoRanomizer 1: %d\n",sband_serv_device_conf.XmtCtr0.PRnd_1));
                        DEBUG_CPRINT(("Xmt ASM at FrmStart 1: %d\n",sband_serv_device_conf.XmtCtr0.ASMStart_1));
                        DEBUG_CPRINT(("Xmt Offset Modulation 1: %d\n",sband_serv_device_conf.XmtCtr0.OffsetMod_1));
                        DEBUG_CPRINT(("Xmt On/Off Channel 1: %d\n",sband_serv_device_conf.XmtCtr0.ChSwitchOn_1));
                        DEBUG_CPRINT(("Xmt 1-Offset Modulation normaly;: %d\n",sband_serv_device_conf.XmtCtr0.OffsetModNormaly));
                        DEBUG_CPRINT(("AttTxGain_0: %d\n",sband_serv_device_conf.XmtCtr3.AttTxGain_0));
                        DEBUG_CPRINT(("AttTxGain_1: %d\n",sband_serv_device_conf.XmtCtr3.AttTxGain_1));
                        DEBUG_CPRINT(("TrFrmSz: %d\n",sband_serv_device_conf.XmtCtr3.TrFrmSz));

                        DEBUG_CPRINT(("PhiOffset16: %d\n",sband_serv_device_conf.XmtCtr4.PhiOffset16));
                        DEBUG_CPRINT(("y16: %d\n",sband_serv_device_conf.XmtCtr4.Y16));
                        DEBUG_CPRINT(("TxSampFact: %d\n",sband_serv_device_conf.XmtCtr4.TxSampFact));
                        DEBUG_CPRINT(("RsMode: %d\n",sband_serv_device_conf.XmtCtr4.RsMode));

                        DEBUG_CPRINT(("RxGainMax: %d\n",sband_serv_device_conf.RcvCtrCh[0].RxGainMax));
                        DEBUG_CPRINT(("ChEn: %d\n",sband_serv_device_conf.RcvCtrCh[0].ChEn));
                        DEBUG_CPRINT(("Modulat: %d\n",sband_serv_device_conf.RcvCtrCh[0].Modulat));
                        DEBUG_CPRINT(("MsbFirst: %d\n",sband_serv_device_conf.RcvCtrCh[0].MsbFirst));
                        DEBUG_CPRINT(("Fec: %d\n",sband_serv_device_conf.RcvCtrCh[0].Fec));
                        DEBUG_CPRINT(("PRnd: %d\n",sband_serv_device_conf.RcvCtrCh[0].PRnd));
                        DEBUG_CPRINT(("BchDec: %d\n",sband_serv_device_conf.RcvCtrCh[0].BchDec));
                        DEBUG_CPRINT(("AccBchErr: %d\n",sband_serv_device_conf.RcvCtrCh[0].AccBchErr));
                        DEBUG_CPRINT(("RBchCountMode: %d\n",sband_serv_device_conf.RcvCtrCh[0].BchCountMode));
                        DEBUG_CPRINT(("PowerOff: %d\n",sband_serv_device_conf.RcvCtrCh[0].PwrOff));
                        DEBUG_CPRINT(("RcvCalib: %d\n",sband_serv_device_conf.RcvCtrCh[0].RcvCalib));

                        DEBUG_CPRINT(("RxGainMax: %d\n",sband_serv_device_conf.RcvCtrCh[1].RxGainMax));
                        DEBUG_CPRINT(("ChEn: %d\n",sband_serv_device_conf.RcvCtrCh[1].ChEn));
                        DEBUG_CPRINT(("Modulat: %d\n",sband_serv_device_conf.RcvCtrCh[1].Modulat));
                        DEBUG_CPRINT(("MsbFirst: %d\n",sband_serv_device_conf.RcvCtrCh[1].MsbFirst));
                        DEBUG_CPRINT(("Fec: %d\n",sband_serv_device_conf.RcvCtrCh[1].Fec));
                        DEBUG_CPRINT(("PRnd: %d\n",sband_serv_device_conf.RcvCtrCh[1].PRnd));
                        DEBUG_CPRINT(("BchDec: %d\n",sband_serv_device_conf.RcvCtrCh[1].BchDec));
                        DEBUG_CPRINT(("AccBchErr: %d\n",sband_serv_device_conf.RcvCtrCh[1].AccBchErr));
                        DEBUG_CPRINT(("RBchCountMode: %d\n",sband_serv_device_conf.RcvCtrCh[1].BchCountMode));
                        DEBUG_CPRINT(("PowerOff: %d\n",sband_serv_device_conf.RcvCtrCh[1].PwrOff));
                        DEBUG_CPRINT(("RcvCalib: %d\n",sband_serv_device_conf.RcvCtrCh[1].RcvCalib));

                        DEBUG_CPRINT(("XmtSymbFreq: %d\n",sband_serv_device_conf.SymbFreq.XmtSymbFreq));
                        DEBUG_CPRINT(("RcvSymbFreq: %d\n",sband_serv_device_conf.SymbFreq.RcvSymbFreq));

                        DEBUG_CPRINT(("RcvMode: %d\n",sband_serv_device_conf.RcvCtr_0.RcvMode));
                        DEBUG_CPRINT(("RcvChannel: %d\n",sband_serv_device_conf.RcvCtr_0.RcvChannel));
                        DEBUG_CPRINT(("ChSwitchOn_0: %d\n",sband_serv_device_conf.RcvCtr_0.ChSwitchOn_0));
                        DEBUG_CPRINT(("ChSwitchOn_1: %d\n",sband_serv_device_conf.RcvCtr_0.ChSwitchOn_1));
                        DEBUG_CPRINT(("DropSample: %d\n",sband_serv_device_conf.RcvCtr_0.DropSample));
                        DEBUG_CPRINT(("dT_minus: %d\n",sband_serv_device_conf.RcvCtr_0.dT_minus));
                        DEBUG_CPRINT(("dT_plus: %d\n",sband_serv_device_conf.RcvCtr_0.dT_plus));
                        DEBUG_CPRINT(("Reserve: %d\n",sband_serv_device_conf.RcvCtr_0.Reserve));

                    }
                    break;
                case (SERVICE_GET | SERVICE_VERSION):
                    {
                        tm_size = length;

                        os_memcpy(&sband_serv_version_get,rcv_data,tm_size);

                        tm_size = tm_size - 5*sizeof(uint32);

                        version_info_size = tm_size;
                        DEBUG_CPRINT(("LEN = %d, %d\n", length, version_info_size ));
                        uint8_t arr_cnt = version_info_size / sizeof(VERSION_DATE_STRUCT);
                        for (int i = 0; i < arr_cnt; i++)
                        {
                            DEBUG_CPRINT(("seconds: %d\n",sband_serv_version_get.prog_version[i].Seconds));//todo
                            // log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,SECONDS__u32_,sband_serv_version_get.prog_version[i].Seconds);
                            DEBUG_CPRINT(("Minute: %d\n",sband_serv_version_get.prog_version[i].Minutes));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MINUTES__u32_,sband_serv_version_get.prog_version[i].Minutes);
                            DEBUG_CPRINT(("hours: %d\n",sband_serv_version_get.prog_version[i].Hours));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,HOURS__u32_,sband_serv_version_get.prog_version[i].Hours);
                            DEBUG_CPRINT(("year: %d\n",sband_serv_version_get.prog_version[i].Year));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,YEAR__u32_,sband_serv_version_get.prog_version[i].Year);
                            DEBUG_CPRINT(("months: %d\n",sband_serv_version_get.prog_version[i].Months));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,MONTHS__u32_,sband_serv_version_get.prog_version[i].Months);
                            DEBUG_CPRINT(("day: %d\n",sband_serv_version_get.prog_version[i].Day));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,DAY__u32_,sband_serv_version_get.prog_version[i].Day);
                            DEBUG_CPRINT(("day: %s\n",sband_serv_version_get.prog_version[i].FileName));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FILE_NAME__u8_,sband_serv_version_get.prog_version[i].FileName);
                            DEBUG_CPRINT(("start address: %d\n",sband_serv_version_get.prog_version[i].start_adr));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,START_ADDRESS__u32_,sband_serv_version_get.prog_version[i].start_adr);
                            DEBUG_CPRINT(("length: %d\n",sband_serv_version_get.prog_version[i].len));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,LEN__u32_,sband_serv_version_get.prog_version[i].len);
                            DEBUG_CPRINT(("flags(SOLL-1; RUN- 0x2;...): %d\n",sband_serv_version_get.prog_version[i].Flags));
                            //log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,FLAGS__u32_,sband_serv_version_get.prog_version[i].Flags);
                        }
                        // sband_tc_tm_get_rsp_hdlr(TM_SBAND_GET_VERSION,status,tm_size+1,(uint8_t*)&sband_tm_buff[0]);
                    }
                    break;
                case (SERVICE_GET | SERVICE_FLAGS_CONF):
                    {
                        //tm_size = sizeof(SERVICE_FLAGS_CONF);
                        //os_memcpy(sband_serv_flags_conf,rcv_data,tm_size);

                        // sband_tc_tm_get_rsp_hdlr(TM_SBAND_GET_FLAGS_CONF,status);
                    }
                    break;
            }
        }
        else if(SERVICE_NAK == (0xC0000000 & rcv_data->service_event))
        {
            status = SBAND_TM_FAILURE;
        }
        else if(SERVICE_BUSY == (0xC0000000 & rcv_data->service_event))
        {
            status = SBAND_TM_BUSY;
        }

        DEBUG_CPRINT(("\r\n S-Band response status: %d",status));
        log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,SBAND_RESPONSE_STATUS__u32_,status);
    }

#ifdef COMMS_TIME_DEBUG 
    get_crnt_time_date(&tim_get_devconf);
    DEBUG_CPRINT(("\nIQS TIME TC PROC PLD EXT %d:%d:%d",tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds));
    log_id_with_three_arg(LOG_INFO_L2,LOG_COMMS,IQS_TIME_TC_PROC_PLD_EXT__u8__u8__u8_,tim_get_devconf.hour, tim_get_devconf.minute, tim_get_devconf.seconds);
#endif

    return status;

}


/**
 * @brief Initializes IQS configuration.
 *
 * This function performs initialization of the IQS configuration parameters.
 */
void comms_init_iqs_cfg(void)
{
    uint8 status = 0;
    iqs_rx_cfg_global.rx_freq=2042500000;
    iqs_rx_cfg_global.rx_type=0;
    iqs_rx_cfg_global.rx_rate=512;
    iqs_rx_cfg_global.rx_rolloff=1;
    iqs_rx_cfg_global.rx_gain=70;
    iqs_rx_cfg_global.rx_rs=0;
    iqs_rx_cfg_global.rx_cc=0;
    iqs_rx_cfg_global.rx_rand=1;
    iqs_rx_cfg_global.rx_crc=0;
    iqs_rx_cfg_global.rx_size=1432;
    iqs_rx_cfg_global.rx_id=0;
    iqs_rx_cfg_global.rx_rsvd1=0;
    iqs_rx_cfg_global.rx_frames=0;
    iqs_rx_cfg_global.rx_detected=0;
    iqs_rx_cfg_global.rx_rssi=0;
    iqs_rx_cfg_global.rx_freqerr=0;
    iqs_rx_cfg_global.rx_symerr=0;
    iqs_rx_cfg_global.rx_rsvd2=0;
    iqs_rx_cfg_global.rx_rsvd3=0;
    iqs_rx_cfg_global.rx_rsvd4=0;
    iqs_tx_cfg_global.tx_freq= 2242500000;
    iqs_tx_cfg_global.tx_type=0;
    iqs_tx_cfg_global.tx_rate=2048;
    iqs_tx_cfg_global.tx_rolloff=1;
    iqs_tx_cfg_global.tx_pout=33;
    iqs_tx_cfg_global.tx_alc=0;
    iqs_tx_cfg_global.tx_rs=0;
    iqs_tx_cfg_global.tx_cc=1;
    iqs_tx_cfg_global.tx_rand=1;
    iqs_tx_cfg_global.tx_crc=0;
    iqs_tx_cfg_global.tx_idletime=30;
    iqs_tx_cfg_global.tx_size=1432;
    iqs_tx_cfg_global.tx_id=0;
    iqs_tx_cfg_global.tx_tmp_limit=0;
    iqs_tx_cfg_global.tx_frames=0;
    iqs_net_prop_glob.ip_stack_enb=1;

    //iqs_net_prop_glob.dev_ip[11]={"192.168.1.1"};
    sprintf((char*)iqs_net_prop_glob.dev_ip,"%s", "192.168.1.1");
    iqs_net_prop_glob.dev_port=51003;
    iqs_net_prop_glob.eth_prefix=24;
    sprintf((char*)iqs_net_prop_glob.dev_ip_temp,"%s", "192.168.1.100");
    //iqs_net_prop_glob.dev_ip_temp="192.168.1.100";
    sprintf((char*)iqs_net_prop_glob.dev_ip_udp,"%s", "192.168.1.111");
    //iqs_net_prop_glob.dev_ip_udp="192.168.1.111";
    iqs_net_prop_glob.dev_port_udp=51004;
    iqs_net_prop_glob.eth_prefix=24;
    sprintf((char*)iqs_prod_key_glob.leop_prod_key,"%s","57712726,fea9f476,4354fe02,308a8b3e");
    sprintf((char*)iqs_prod_key_glob.con_prod_key,"%s","57712726,fea9f476,4354fe02,308a8b3e");
    iqs_tx_rx_atten_glob.rx_gain=0;
    iqs_tx_rx_atten_glob.idx_gain=0;
    iqs_tx_rx_atten_glob.pwr_min=0;
    iqs_tx_rx_atten_glob.pwr_max=0;
    iqs_tx_rx_atten_glob.rx_ch_no=0;
    iqs_tx_rx_atten_glob.mtu_size=1432;

    sprintf((char*)iqs_fw_ver_glob.fpga_ver,"%s","1.7.1");
    sprintf((char*)iqs_fw_ver_glob.swap_ver,"%s", "1.7.1");
    sprintf((char*)iqs_fw_ver_glob.cpu_ver,"%s","1.7.1");
    //iqs_gnd_cont_dur_glob.gs_first_contact);
    //iqs_gnd_cont_dur_glob.otsu_first_contact);
    //iqs_tim_out_glob.beacon_tx_timeout);
    //iqs_tim_out_glob.tx_win_timeout);

#if 1
    sband_serv_ip_adr.ip_address = inet_addr ("1.1.168.192");
    sband_serv_start_rf_rcv.ip_address = inet_addr ("111.1.168.192");
    sband_serv_start_rf_rcv.port = 51004;
    sband_serv_start_rf_rcv.send_format = 3;
    sband_serv_start_rf_rcv.output_interface = 0;
    sband_serv_rf_xmt_data.PktType = 2;
    sband_serv_rf_xmt_data.ErrInf = 1;
    sband_serv_rf_xmt_data.PktLen = 1432;
    sband_serv_rf_xmt_data.EthFirst = 1;
    sband_serv_rf_xmt_data.EthLast = 1;
    sband_serv_rf_xmt_data.EthNumByte = 10;
    sband_serv_rf_xmt_data.MoreInfo = 0xDEADAFFE;
    sband_serv_dvbs2_cfg.ModCod_0 = 1;
    sband_serv_dvbs2_cfg.FrmType_0 = 2;
    sband_serv_dvbs2_cfg.InsertPilots_0 = 0;
    sband_serv_dvbs2_cfg.Acm_Ccm_0 = 0;
    sband_serv_dvbs2_cfg.RollOffVal_0 = 4;
    sband_serv_dvbs2_cfg.PlSpreadNumber_0 = 5;
    sband_serv_dvbs2_cfg.Reserve_0 = 2;
    sband_serv_dvbs2_cfg.Reserve_1 = 4;
    sband_serv_device_conf.HwPins.RfeRx0On = 0;
    sband_serv_device_conf.HwPins.RfeTx0On = 0;
    sband_serv_device_conf.HwPins.RfePa0On = 0;
    sband_serv_device_conf.HwPins.RfeRxTx0 = 1;
    sband_serv_device_conf.HwPins.RfeRx1On = 0;
    sband_serv_device_conf.HwPins.RfeTx1On = 0;
    sband_serv_device_conf.HwPins.RfePa1On = 0;
    sband_serv_device_conf.HwPins.RfeRxTx1 = 0;
    sband_serv_device_conf.HwPins.RfeOutEn = 1;
    sband_serv_device_conf.HwPins.RfeGpio0 = 0;
    sband_serv_device_conf.HwPins.RfeLoopDet = 0;
    sband_serv_device_conf.HwPins.RfeLoMux = 0;
    sband_serv_device_conf.HwPins.RfePowOn = 0;
    sband_serv_device_conf.HwPins.RfeDA = 0;
    sband_serv_device_conf.HwPins.RfeCL = 0;
    sband_serv_device_conf.HwPins.RfeEnAD5304 = 0;
    sband_serv_device_conf.HwPins.RfeEnSynth = 0;
    sband_serv_device_conf.HwPins.RfeEn3 = 0;
    sband_serv_device_conf.HwPins.RfeEn4 = 0;
    sband_serv_device_conf.HwPins.RfeEn5 = 0;
    sband_serv_device_conf.HwPins.Rfe7V0En = 1;
    sband_serv_device_conf.HwPins.RfeRsvuint32 = 0;
    sband_serv_device_conf.RfeFreq.RxMix[0] = 0;
    sband_serv_device_conf.RfeFreq.RxMix[1] = 0;
    sband_serv_device_conf.RfeFreq.TxMix[0] = 2;
    sband_serv_device_conf.RfeFreq.TxMix[1] = 2;
    sband_serv_device_conf.RfeFreq.RxFreq[0] = 2042500;
    sband_serv_device_conf.RfeFreq.RxFreq[1] = 2044000;
    sband_serv_device_conf.RfeFreq.TxFreq[0] = 2242500;
    sband_serv_device_conf.RfeFreq.TxFreq[1] = 2244500;
    sband_serv_device_conf.LimFreq.RxLow[0] = 0;
    sband_serv_device_conf.LimFreq.RxLow[1] = 0;
    sband_serv_device_conf.LimFreq.RxHigh[0] = 12000000;
    sband_serv_device_conf.LimFreq.RxHigh[1] = 12000000;
    sband_serv_device_conf.LimFreq.TxLow[0] = 0;
    sband_serv_device_conf.LimFreq.TxLow[1] = 0;
    sband_serv_device_conf.LimFreq.TxHigh[0] = 12000000;
    sband_serv_device_conf.LimFreq.TxHigh[1] = 12000000;
    sband_serv_device_conf.AdCtr._2Rx2Tx = 1;
    sband_serv_device_conf.AdCtr.TxLevel_0 = 880;
    sband_serv_device_conf.AdCtr.TxLevel_1 = 880;
    sband_serv_device_conf.AdCtr.RefClk = 59;
    sband_serv_device_conf.AdCtr.Reserve = 0;
    sband_serv_device_conf.XmtCtr0.Modulat_0 = 2;
    sband_serv_device_conf.XmtCtr0.Mode_0 = 8;
    sband_serv_device_conf.XmtCtr0.Fec_0 = 1;
    sband_serv_device_conf.XmtCtr0.ChEn_0 = 1;
    sband_serv_device_conf.XmtCtr0.PRnd_0 = 1;
    sband_serv_device_conf.XmtCtr0.ASMStart_0 = 1;
    sband_serv_device_conf.XmtCtr0.OffsetMod_0 = 0;
    sband_serv_device_conf.XmtCtr0.Ch1_diff_Ch2 = 0;
    sband_serv_device_conf.XmtCtr0.ChSwitchOn_0 = 1;
    sband_serv_device_conf.XmtCtr0.Modulat_1 = 2;
    sband_serv_device_conf.XmtCtr0.Mode_1 = 8;
    sband_serv_device_conf.XmtCtr0.Fec_1 = 1;
    sband_serv_device_conf.XmtCtr0.ChEn_1 = 1;
    sband_serv_device_conf.XmtCtr0.PRnd_1 = 1;
    sband_serv_device_conf.XmtCtr0.ASMStart_1 = 1;
    sband_serv_device_conf.XmtCtr0.OffsetMod_1 = 0;
    sband_serv_device_conf.XmtCtr0.ChSwitchOn_1 = 1;
    sband_serv_device_conf.XmtCtr0.OffsetModNormaly = 1;
    sband_serv_device_conf.XmtCtr3.AttTxGain_0 = 32;
    sband_serv_device_conf.XmtCtr3.AttTxGain_1 = 32;
    sband_serv_device_conf.XmtCtr3.TrFrmSz = 1432;
    sband_serv_device_conf.XmtCtr4.PhiOffset16 = 0;
    sband_serv_device_conf.XmtCtr4.Y16 = 0;
    sband_serv_device_conf.XmtCtr4.TxSampFact = 2;
    sband_serv_device_conf.XmtCtr4.RsMode = 0;
    sband_serv_device_conf.RcvCtrCh[0].RxGainMax = 70;
    sband_serv_device_conf.RcvCtrCh[0].ChEn = 1;
    sband_serv_device_conf.RcvCtrCh[0].Modulat = 1;
    sband_serv_device_conf.RcvCtrCh[0].MsbFirst = 1;
    sband_serv_device_conf.RcvCtrCh[0].Fec = 0;
    sband_serv_device_conf.RcvCtrCh[0].PRnd = 1;
    sband_serv_device_conf.RcvCtrCh[0].BchDec = 1;
    sband_serv_device_conf.RcvCtrCh[0].AccBchErr = 0;
    sband_serv_device_conf.RcvCtrCh[0].BchCountMode = 0;
    sband_serv_device_conf.RcvCtrCh[0].PwrOff = 37;
    sband_serv_device_conf.RcvCtrCh[0].RcvCalib = 0;
    sband_serv_device_conf.RcvCtrCh[1].RxGainMax = 70;
    sband_serv_device_conf.RcvCtrCh[1].ChEn = 1;
    sband_serv_device_conf.RcvCtrCh[1].Modulat = 1;
    sband_serv_device_conf.RcvCtrCh[1].MsbFirst = 1;
    sband_serv_device_conf.RcvCtrCh[1].Fec = 0;
    sband_serv_device_conf.RcvCtrCh[1].PRnd = 1;
    sband_serv_device_conf.RcvCtrCh[1].BchDec = 1;
    sband_serv_device_conf.RcvCtrCh[1].AccBchErr = 0;
    sband_serv_device_conf.RcvCtrCh[1].BchCountMode = 0;
    sband_serv_device_conf.RcvCtrCh[1].PwrOff = 37;
    sband_serv_device_conf.RcvCtrCh[1].RcvCalib = 0;
    // sband_serv_device_conf.SymbFreq.XmtSymbFreq = 2048;
    // sband_serv_device_conf.SymbFreq.RcvSymbFreq = 512;
    sband_serv_device_conf.RcvCtr_0.RcvMode = 0;
    sband_serv_device_conf.RcvCtr_0.RcvChannel = 0;
    sband_serv_device_conf.RcvCtr_0.ChSwitchOn_0 = 0;
    sband_serv_device_conf.RcvCtr_0.ChSwitchOn_1 = 0;
    sband_serv_device_conf.RcvCtr_0.DropSample = 0;
    sband_serv_device_conf.RcvCtr_0.dT_minus = 0;
    sband_serv_device_conf.RcvCtr_0.dT_plus = 0;
    sband_serv_device_conf.RcvCtr_0.Reserve = 0;
#endif
//    sband_proc_tc(SERVICE_GET | SERVICE_DEVICE_CONF);

  //  status = comms_sband_xlink_tx_cfg();

    DEBUG_CPRINT(("[SBAND] temp NVM Read TX configuration status: %d",status));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,NVM_READ_TX_CONFIURATION_STATUS__s8_,status);

    //status = comms_sband_xlink_rx_cfg();

    //status = sband_proc_tc_pld((SERVICE_SET|SERVICE_DEVICE_CONF), (uint8_t*)&sband_serv_device_conf);

    DEBUG_CPRINT(("[SBAND] temp NVM Read RX configuration status: %d",status));
    log_id_with_one_arg(LOG_INFO_L1,LOG_COMMS,NVM_READ_RX_CONFIURATION_STATUS__s8_,status);

}

/**
 * @brief Calculates RF power attenuation value.
 *
 * @param pwr   RF power in dBm. 
 * @param frq   RF frequency in MHz.
 * @return Attenuation value in dB.
 *
 * This function calculates the RF attenuation value based on 
 * configured power and frequency.
 */
float xlink_pwr_attn_val(int pwr, int frq)
{
    int pwr_index = (pwr >= 23 && pwr < 28) ? 0 : \
                    (pwr >= 28 && pwr < 31) ? 1 : \
                    (pwr >= 31 && pwr < 32) ? 2 : \
                    (pwr >= 32 && pwr < 33) ? 3 : \
                    (pwr == 33) ? 4 : -1;

    int frq_index = (frq >= 2210 && frq < 2230) ? 0 : \
                    (frq >= 2230 && frq < 2245) ? 1 : \
                    (frq >= 2245 && frq < 2260) ? 2 : \
                    (frq >= 2260 && frq < 2280) ? 3 : \
                    (frq == 2280) ? 4 : -1;

    DEBUG_CPRINT(("\nPWR_idx: %d, Freq_idx: %d, TX ATT: %f\n",pwr_index,frq_index, iqs_attn_tab[pwr_index][frq_index]));
    return iqs_attn_tab[pwr_index][frq_index];
}

/**
 * @brief Gets S-Band average RSSI value.
 *
 * @param rcv_ch_no S-Band receive channel number.
 * @return Average RSSI value in dBm.
 *
 * This function returns the calculated average RSSI for the 
 * specified S-Band receive channel.
 */ 
float comms_xlink_s_get_avg_rssi(uint8_t rcv_ch_no)
{
    float avg_rssi_ = avg_rssi[rcv_ch_no];

    return avg_rssi_;
}

/**
 * @brief Gets S-Band RSSI value.
 *
 * @param rcv_ch_no S-Band receive channel number. 
 * @return RSSI value in dBm.
 *
 * This function returns the current RSSI value for the
 * specified S-Band receive channel. 
 */
float comms_xlink_s_get_rssi(uint8_t rcv_ch_no)
{
    float rssi;
    int rx_gain_max;
    int idx_gain;
    int pwr_min;
    int pwr_max;

    //-108dBm + (RxGainMax - idxGain) - (43 - ((PwrMin + PwrMax)/2))

    //    DEBUG_CPRINT(("GET xlink RSSI \n"));
    //   log_id(LOG_INFO_L2,LOG_COMMS,GET_XLINK_RSSI);
    rx_gain_max = sband_serv_device_conf.RcvCtrCh[rcv_ch_no].RxGainMax;
    iqs_tx_rx_atten_glob.rx_gain = rx_gain_max;
    idx_gain = sband_serv_house_keep.HouseKeep.RcvInfCh[rcv_ch_no].idxGain;
    iqs_tx_rx_atten_glob.idx_gain = idx_gain;
    pwr_min = sband_serv_house_keep.HouseKeep.RcvInfCh[rcv_ch_no].PwrMin;
    iqs_tx_rx_atten_glob.pwr_min = pwr_min;
    pwr_max = sband_serv_house_keep.HouseKeep.RcvInfCh[rcv_ch_no].PwrMax;
    iqs_tx_rx_atten_glob.pwr_max = pwr_max;

    rssi = -108 + (rx_gain_max - idx_gain) - (43 - ((pwr_min + pwr_max)/2));

    iqs_tx_rx_atten_glob.pwr_attn_lut = rssi;

    return rssi;
}

/**
 * @brief Enables/disables S-Band Xlink TX.
 *
 * @param sband_iqs_tx_on 1 to enable, 0 to disable TX.
 * @return Execution status.
 *
 * This function turns ON/OFF the S-Band Xlink TX based on input flag.
*/
uint8_t comms_sband_xlink_tx_on(uint8_t sband_iqs_tx_on)
{
    uint8 ret = 0;
    get_rtc_time(&get_t_iqs);
    DEBUG_CPRINT(("\nIQS RTC Time-%d:%d:%d\n",get_t_iqs.hour, get_t_iqs.min, get_t_iqs.sec));
    log_id_with_three_arg(LOG_INFO_L2,LOG_COMMS,IQS_RTC_TIME__u8__u8__u8_,get_t_iqs.hour, get_t_iqs.min, get_t_iqs.sec);

    if (!sband_iqs_tx_on)
    {
        DEBUG_CPRINT(("\n\rIQS DVC_PAR_OFF : "));
        log_id(LOG_INFO_L1,LOG_COMMS,IQS_DVC_PAR_OFF);
        sband_iqs_tx_on = 0;
    }
    else
    {
        DEBUG_CPRINT(("\n\rIQS DVC_PAR_ON : "));
        log_id(LOG_INFO_L1,LOG_COMMS,IQS_DVC_PAR_ON);
        sband_iqs_tx_on = 1;
    }
    sband_serv_device_conf.XmtCtr0.ChSwitchOn_0 = sband_iqs_tx_on;
    sband_serv_device_conf.XmtCtr0.ChSwitchOn_1 = sband_iqs_tx_on;
    ret = sband_proc_tc_pld((SERVICE_SET|SERVICE_DEVICE_CONF), (uint8_t*)&sband_serv_device_conf);

    DEBUG_CPRINT(("\nIQS RF Turn ON Status:%hu\n", sband_iqs_tx_on));
    log_id_with_one_arg(LOG_INFO_L2,LOG_COMMS,RF_TURN_ON_STATUS__u8_,sband_iqs_tx_on);
    return ret;
}

/**
 * @brief Configures S-Band Xlink TX.
 *
 * @return Execution status. 
 * 
 * This function handles configuration of the S-Band Xlink TX parameters.
*/
uint8_t comms_sband_xlink_tx_cfg(void)
{
    DEBUG_CPRINT(("GET xlink tx config \n"));
    log_id(LOG_INFO_L1,LOG_COMMS,GET_XLINK_TX_CONFIG);
    sband_serv_device_conf.RfeFreq.TxFreq[0] = iqs_tx_cfg_global.tx_freq/1000;
    sband_serv_device_conf.RfeFreq.TxFreq[1] = iqs_tx_cfg_global.tx_freq/1000;
    sband_serv_device_conf.XmtCtr0.Modulat_0 = (iqs_tx_cfg_global.tx_type + 1)  & 0x07;
    sband_serv_device_conf.XmtCtr0.Modulat_1 = (iqs_tx_cfg_global.tx_type + 1) & 0x07;
    //sband_serv_device_conf.SymbFreq.XmtSymbFreq = iqs_tx_cfg_global.tx_rate & 0xffff;
    //sband_serv_dvbs2_cfg.RollOffVal_0 = iqs_tx_cfg_global.tx_rolloff & 0x07;
    sband_serv_device_conf.XmtCtr4.RsMode = iqs_tx_cfg_global.tx_rs & 0xf;
    sband_serv_device_conf.XmtCtr0.Fec_0 = iqs_tx_cfg_global.tx_cc & 0x07;
    sband_serv_device_conf.XmtCtr0.Fec_1 = iqs_tx_cfg_global.tx_cc & 0x07;
    sband_serv_device_conf.XmtCtr0.PRnd_0 = iqs_tx_cfg_global.tx_rand & 0x01;
    sband_serv_device_conf.XmtCtr0.PRnd_1 = iqs_tx_cfg_global.tx_rand & 0x01;
    sband_serv_device_conf.XmtCtr3.TrFrmSz = iqs_tx_cfg_global.tx_size & 0xFFF;
    sband_xlink_rf_pwr_global = (uint32_t)iqs_tx_cfg_global.tx_pout;
    sband_serv_device_conf.XmtCtr3.AttTxGain_0 = ((float) xlink_pwr_attn_val(sband_xlink_rf_pwr_global, (sband_serv_device_conf.RfeFreq.TxFreq[0]/1000)))*10;
    sband_serv_device_conf.XmtCtr3.AttTxGain_1 = ((float) xlink_pwr_attn_val(sband_xlink_rf_pwr_global, (sband_serv_device_conf.RfeFreq.TxFreq[1]/1000)))*10;

    //sband_serv_device_conf.XmtCtr0.ChSwitchOn_0 = sband_iqs_tx_ON ;
    //sband_serv_device_conf.XmtCtr0.ChSwitchOn_1 = sband_iqs_tx_ON ;

    return 0;
}

/**
 * @brief Configures S-Band Xlink RX. 
 *
 * @return Execution status.
 *
 * This function handles configuration of the S-Band Xlink RX parameters.
*/
uint8_t comms_sband_xlink_rx_cfg(void)
{

    DEBUG_CPRINT(("GET xlink rx config \n"));
    log_id(LOG_INFO_L2,LOG_COMMS,GET_XLINK_RX_CONFIG);
    sband_serv_device_conf.RfeFreq.RxFreq[0] = iqs_rx_cfg_global.rx_freq/1000;
    sband_serv_device_conf.RfeFreq.RxFreq[1] = iqs_rx_cfg_global.rx_freq/1000;
    sband_serv_device_conf.RcvCtrCh[0].Modulat = (iqs_rx_cfg_global.rx_type + 1) & 0x07;
    sband_serv_device_conf.RcvCtrCh[1].Modulat = (iqs_rx_cfg_global.rx_type + 1) & 0x07;
    //sband_serv_device_conf.SymbFreq.RcvSymbFreq = iqs_rx_cfg_global.rx_rate;
    //sband_serv_device_conf.RcvCtrCh[0].Fec = iqs_rx_cfg_global.rx_cc & 0x07;
    //sband_serv_device_conf.RcvCtrCh[1].Fec = iqs_rx_cfg_global.rx_cc & 0x07;
    sband_serv_device_conf.RcvCtrCh[0].PRnd = iqs_rx_cfg_global.rx_rand & 0x01;
    sband_serv_device_conf.RcvCtrCh[1].PRnd = iqs_rx_cfg_global.rx_rand & 0x01;
    //sband_serv_device_conf.RcvCtrCh[0].RxGainMax = iqs_rx_cfg_global.rx_gain;
    //sband_serv_device_conf.RcvCtrCh[1].RxGainMax = iqs_rx_cfg_global.rx_gain;
    
    //sband_serv_device_conf.RcvCtr_0.ChSwitchOn_0  = xlinks_rx_ch_en[0] ; 
    //sband_serv_device_conf.RcvCtr_0.ChSwitchOn_1  = xlinks_rx_ch_en[1] ; 

    sband_serv_device_conf.RcvCtrCh[0].BchDec = bch_dec_param[0];
    sband_serv_device_conf.RcvCtrCh[0].AccBchErr = bch_dec_param[1];
    sband_serv_device_conf.RcvCtrCh[0].BchCountMode = bch_dec_param[2];
    sband_serv_device_conf.RcvCtrCh[1].BchDec = bch_dec_param[0];
    sband_serv_device_conf.RcvCtrCh[1].AccBchErr = bch_dec_param[1];
    sband_serv_device_conf.RcvCtrCh[1].BchCountMode = bch_dec_param[2];

    return 0;
}

/**
 * @brief Gets S-Band RX synchronization state.
 *
 * @param rcv_ch_no S-Band receive channel number.
 * @return Synchronization status flag.
 * 
 * This function returns sync state of specified S-Band RX channel.
*/
uint8_t comms_sband_rx_get_sync_state(uint8_t rcv_ch_no)
{
    return  sband_serv_house_keep.HouseKeep.RcvInfCh[rcv_ch_no].Sync;
}


/**
 * @brief Processes S-Band firmware configuration update.
 *
 * @param pld Pointer to configuration data. 
 * @return Execution status.
 *
 * This function handles applying new firmware configuration received. 
*/
uint8_t comms_sband_proc_fw_cfg_upd(uint8_t *pld)
{
    uint8_t num_fw_files=0,file_cnt=0, ret=0;

    os_memcpy(&num_fw_files,pld,sizeof(uint8_t));
    pld = pld + sizeof(uint8_t);

    os_memcpy(&file_cnt,pld,sizeof(uint8_t));
    pld = pld + sizeof(uint8_t);

    os_memcpy(sband_serv_version_set.prog_version,pld,(sizeof(VERSION_DATE_STRUCT)*num_fw_files));
    os_memcpy(sband_serv_version_set.product_key,sband_lastProductKey,sizeof(sband_lastProductKey));
    sband_serv_version_set.service_event = (SERVICE_SET|SERVICE_VERSION);
    sband_fw_cfg_size = (sizeof(VERSION_DATE_STRUCT)*num_fw_files) + (sizeof(uint32_t)*5);
    return ret;
}

/**
 * @brief Applies S-Band firmware configuration.
 * 
 * @return Execution status.
 * 
 * This function applies the firmware configuration to the S-Band.
*/
uint8_t comms_sband_proc_fw_cfg(void)
{
    int ret =0;
repeat_fw_tx:

    ret = sband_cmd_write(sband_fw_cfg_size,(uint8_t*)&sband_serv_version_set);

    DEBUG_CPRINT(("\n[S-Band] FW update status %d\n",ret));
    if(!ret)
    {
        ret = sband_proc_tc(SERVICE_SET | SERVICE_RESET);
        os_delay(30000);
    }
    else
    {
        sband_fw_cfg_failure_cnt ++;
        err_detected(ERR_GROUP_COMMS, ERR_COMMS_SBAND_FW_UPD_FAIL);
        if(sband_fw_cfg_failure_cnt<3)
        {
            goto repeat_fw_tx;
        }
        else
        {
            ret = 1;
        }
    }

    DEBUG_CPRINT(("\n[S-Band] FW update reset status %d\n",ret));

    if(!ret)
    {
        //Nothing to do
    }

    return ret;
}

/**
 * @brief Gets S-Band firmware version.
 *
 * @return Execution status. 
 * 
 * This function retrieves the firmware version of the S-Band.
*/
uint8_t comms_sband_get_vers(void)
{
    uint8_t ret = 0;
    uint8_t sband_tm_buff[300];
    uint8_t file_count =0;
    int j = 3;

    ret = sband_proc_tc(SERVICE_GET | SERVICE_VERSION);

    file_count = version_info_size / sizeof(VERSION_DATE_STRUCT);

    sband_tm_buff[0] =  ret;
    sband_tm_buff[1] =  file_count;
    sband_tm_buff[2] =  MAX_SER_FILENAME_CNT;
    DEBUG_CPRINT(("\n RET = %d, %d\n", ret, file_count));

    for(int i= 0; i < file_count; i++)
    {
        os_memcpy(&sband_tm_buff[j],&sband_serv_version_get.prog_version[i],sizeof(VERSION_DATE_STRUCT));
        j = j + sizeof(VERSION_DATE_STRUCT);
        DEBUG_CPRINT(("SEND TC_TM"));
    }

    sband_tc_tm_get_rsp_hdlr(TM_COMMS_FW_CFG,ret,version_info_size + 3,sband_tm_buff);

    return 0;

}

/**
 * @brief Copies S-Band Xlink TX data.
 *
 * @return Execution status.
 *
 * This function handles copying TX data for S-Band Xlink interface.
*/
uint8_t comms_sband_xlink_tx_cpy(void)
{
    iqs_tx_cfg_global.tx_freq	= sband_s_tx_cfg.tx_cfg_freq;
    iqs_tx_cfg_global.tx_type	= sband_s_tx_cfg.tx_cfg_type;
    iqs_tx_cfg_global.tx_rate	= sband_s_tx_cfg.tx_cfg_rate;
    iqs_tx_cfg_global.tx_rolloff = sband_s_tx_cfg.tx_cfg_rolloff;
    iqs_tx_cfg_global.tx_pout	= sband_s_tx_cfg.tx_cfg_pout;
    iqs_tx_cfg_global.tx_rs		= sband_s_tx_cfg.tx_cfg_rs;
    iqs_tx_cfg_global.tx_cc		= sband_s_tx_cfg.tx_cfg_cc;
    iqs_tx_cfg_global.tx_rand	= sband_s_tx_cfg.tx_cfg_rand;
    iqs_tx_cfg_global.tx_size	= sband_s_tx_cfg.tx_cfg_size;

    iqs_tx_cfg_global.tx_idletime = sband_s_tx_cfg.tx_cfg_idletime;       /*!< Active transmitter time after each frame                      */
    return 0;
}

/**
 * @brief Copies S-Band Xlink RX data. 
 * 
 * @return Execution status.
 * 
 * This function handles copying RX data for S-Band Xlink interface. 
*/
uint8_t comms_sband_xlink_rx_cpy(void)
{
    iqs_rx_cfg_global.rx_freq	= sband_s_rx_cfg.rx_cfg_freq;
    iqs_rx_cfg_global.rx_type	= sband_s_rx_cfg.rx_cfg_type;
    iqs_rx_cfg_global.rx_rate	= sband_s_rx_cfg.rx_cfg_rate;
    iqs_rx_cfg_global.rx_rolloff = sband_s_rx_cfg.rx_cfg_rolloff;
    iqs_rx_cfg_global.rx_rs		= sband_s_rx_cfg.rx_cfg_rs;
    iqs_rx_cfg_global.rx_cc		= sband_s_rx_cfg.rx_cfg_cc;
    iqs_rx_cfg_global.rx_rand	= sband_s_rx_cfg.rx_cfg_rand;
    iqs_rx_cfg_global.rx_size	= sband_s_rx_cfg.rx_cfg_size;

    return 0;
}
/**
 * @brief Updates S-Band Xlink product key.
 *
 * @return Execution status.
 *
 * This function handles updating the product key sequence for 
 * the S-Band Xlink interface.
*/

void comms_sband_rx_rcv_cfg(void)
{
    sband_serv_start_rf_rcv.ip_address = xlink_get_udp_ip_addr();
    sband_serv_start_rf_rcv.port                   = 51004;			
    sband_serv_start_rf_rcv.send_format            = RF_RCV_SEND_CLTU;		
    sband_serv_start_rf_rcv.output_interface       = RF_RCV_OUTPUT_UDP;
    sband_serv_start_rf_rcv.Reserve                = 0;

    DEBUG_CPRINT(("\nTemp IP Address: %d\n",sband_serv_start_rf_rcv.ip_address));

    sband_proc_tc_pld((SERVICE_SET|SERVICE_START_RF_RCV), (uint8_t*)&sband_serv_start_rf_rcv);

    //DEBUG_CPRINT(("\nIQS RX RCV status: %ld",status));

}
void comms_sband_rx_tx_turn_on(void)
{
    int32_t ret = 0;
    sband_serv_device_conf.RcvCtr_0.ChSwitchOn_0 = xlinks_rx_ch_en[0];
    sband_serv_device_conf.RcvCtr_0.ChSwitchOn_1 = xlinks_rx_ch_en[1];

    if(sband_iqs_tx_ON) 
    {
        ret = comms_sband_xlink_tx_on(sband_iqs_tx_ON);

        if(0 == ret)
        {
            sband_iqs_tx_ON = 1;
        }
        else
        {
            sband_iqs_tx_ON = 0;
        }
    }
    else 
    {
        ret = sband_proc_tc_pld((SERVICE_SET|SERVICE_DEVICE_CONF), (uint8_t*)&sband_serv_device_conf);
    }
}
