/**
 * @file comms_sband_xlink_ss.h
 *
 * @brief This file has the MACROS and control blocks used for X-Band configuration
 *
 */

#ifndef COMMS_SBAND_XLINK_SS_H
#define COMMS_SBAND_XLINK_SS_H


/*######################################################################################*/
/*#############################  S E R V I C E  S E R V ER  ############################*/
/*######################################################################################*/

/*##########################################################################################*/
/*************	Include Header Files									*********************/
/*##########################################################################################*/

#include "comms_sband_xlink_rfe_ctr.h"
#include "exo_types.h"

/*##########################################################################################*/
/*************	Constant Definitions									*********************/
/*##########################################################################################*/
/****************************************************************************************/

/**
 * @defgroup ServiceServerConstantPub ServiceServer public constants
 * public definitions for ServiceServer constants
 * @{
 *****************************************************************************************/
#define SERVICE_SERVER_PORT				51003
#define SERVICE_LIVE_TIME				1000
/* a established TCP connection will be closed after N*10ms without actions */

/* service requests and response flags */
#define SERVICE_XMT_DATA_STOP				0x04000000	/* Stop the XmtData Transfer */
#define SERVICE_XMT_DATA_RUN				0x08000000	/* Run the XmtData Transfer after stop */
#define SERVICE_GET					0x10000000	/* read data (GET) */
#define SERVICE_SET					0x20000000	/* write data (SET) */
#define SERVICE_ACK					0x40000000	/* ACK for a write (SET) */
#define SERVICE_NAK					0x80000000	/* NAK for a write (SET) */
#define SERVICE_BUSY					0xC0000000	/* BUSY for a read (GET) or write (SET) */
#define RX_BUFF_SIZE 1400

/* service events */
/* read only = r; write only = w; read & write = rw */
#define SERVICE_IDLE					0x00000000	/* w  */
#define SERVICE_DEVICE_CONF				0x00000001	/* rw */
#define SERVICE_FEATURE_SELECT				0x00000002	/* r  */
#define SERVICE_VERSION					0x00000003	/* rw */
#define SERVICE_HOUSE_KEEP				0x00000004	/* r  */
#define SERVICE_XPA_INF 				0x00000005	/* r  */
#define SERVICE_IP_ADR					0x00000009	/* rw */
#define SERVICE_IP_ADR_SAVE				0x0000000A	/* w  */
#define SERVICE_FLAGS_CONF				0x0000000C	/* rw */
#define SERVICE_DVBS2_CONF                              0x0000000D	/* w  */
#define SERVICE_START_RF_RCV				0x0000000E	/* w  */
#define SERVICE_RF_XMT_DATA				0x0000000F	/* w  */

#define SERVICE_RESET					0x009AE41B	/* w  */

#define SS_IDLE						0x00				//!< ServiceServer not used
#define SS_TCP						0x01				//!< ServiceServer used over TCP
#define SS_UDP						0x02				//!< ServiceServer used over UDP
#define SS_CSP						0x03				//!< ServiceServer used over CSP

//VERSION_DATE_STRUCT; //!< Structure for Version Info, size: 16 bytes (4 WORDs) *1) meaningless for SERVICE_SET_VERSION
//uint32 Flags 4; //!< Flags for file management and operations
#define SERVICE_FLAG_SOLL_PROG          0x1             //!< File will be loaded at next device start-up
#define SERVICE_FLAG_RUNNING            0x2             //!< File was loaded and is currently running (see Note below!)
#define SERVICE_FLAG_DELETE		0x4		//!< version marked for deleting

// SERVICE_START_RF_RCV_STRUCT; /* size: 28 bytes (7 WORDs) */"
//uint32 send_format : 4; //!< Received data can be sent with additional information
#define RF_RCV_SEND_NORMAL	0x0		//!< sends the complete SERVICE_RF_XMT_DATA_STRUCT
#define RF_RCV_SEND_ERR_INF	0x1		//!< sends only parts of the STRUCT -> starts with PktType
#define RF_RCV_SEND_RAW		0x2		//!< if the BchBlk correct, sends only the raw data (without the TailSequence)
#define RF_RCV_TRF			0x3		//!< sends collected BCH-Blocks from 0...BCH-STOP (TransferFrame)

// SERVICE_RF_XMT_DATA_STRUCT; /* Service Server Request */
// uint32 PktType : 2; //!< type of Packet:
#define PKT_STREAM				0		//!< for test purposes
#define PKT_PROXIMITY_1			1		//!< not used
#define PKT_TRANSFER_FRM		2
#define PKT_BCH_64_56			3


// SERVICE_RF_XMT_DATA_STRUCT; /* Service Server Request FIFO init */

// uint32 MoreInfo; //!< Magic Number(TCP) or UDP packet counter
#define SERVICE_XMT_MAGIC				0xDEADAFFE

//uint32 ControlBits; //!< TxFiFo init/flush:
    //!< if all 32 control bits == 0xBABEDADE -> TxFiFo init / flush
#define XMT_FIFO_INIT					0xBABEDADE


// SERVICE_RF_XMT_DATA_STRUCT; /* Service Server Response */
    //uint32 PktType : 2; //!< type of Packet:
#define PKT_TRANSFER_FRM 2
    //uint32 ErrInf : 5; //!< error information:
#define RF_XMT_OK 0x00
#define RF_XMT_FIFO_FULL 0x01
#define RF_XMT_FAIL_LEN 0x02
#define RF_XMT_NO_SIG 0x04
#define RF_XMT_PKT_DROP 0x08

// SERVICE_RF_XMT_DATA_STRUCT; /* RF_RCV_Data */
//uint32 PktType : 2; //!< type of Packet
#define PKT_BCH_64_56 3
//uint32 ErrInf : 5; //!< error information
#define RF_RCV_OK				0x00
#define RF_RCV_FAIL_PACKET_COUNTER      	0x01
#define RF_RCV_LEN_TO_SHORT			0x02
#define RF_RCV_FAIL_LEN				0x04
#define RF_RCV_CRC_ERROR			0x08
#define RF_RCV_SLIP_ERROR			0x10
#define RF_RCV_NO_CHECK				0x1F
#define RF_RCV_BCH_NO_ERR			0x00
#define RF_RCV_BCH_EVEN_ERR			0x01
#define RF_RCV_BCH_ODD_ERR			0x02
#define RF_RCV_BCH_CORR_ERR			0x03
#define RF_XMT_OK				0x00
#define RF_XMT_FIFO_FULL			0x01
#define RF_XMT_FAIL_LEN				0x02
#define RF_XMT_NO_SIG				0x04
#define RF_XMT_PKT_DROP				0x08

#define MAX_TC_PRODUCT_KEY_CNT                     4
#define MAX_SER_PRODUCT_KEY_CNT                    4
#define MAX_SER_IDL_PRODUCT_KEY_CNT                4
#define MAX_SER_RESET_PRODUCT_KEY_CNT              4
#define MAX_SER_IP_PRODUCT_KEY_CNT                 4
#define MAX_SER_VER_PRODUCT_KEY_CNT                4
#define MAX_SER_RF_XMT_PRODUCT_KEY_CNT             4
#define MAX_SER_RF_PAYLOAD_CNT                  1400
#define MAX_SER_START_PRODUCT_KEY_CNT              4
#define MAX_SER_FEATURE_PRODUCT_KEY_CNT            4
#define MAX_SER_DEVICE_PRODUCT_KEY_CNT             4
#define MAX_SER_DEVICE_RCVCTRCH_CNT                2
#define MAX_SER_HOS_KEEP_PRODUCT_KEY_CNT           4
#define MAX_SER_DVB_PRODUCT_KEY_CNT                4
#define MAX_SER_FILENAME_CNT                       4

#define MAX_HSEKEEP_VOLT_CNT 6
#define MAX_HSEKEEP_RCV_INTF_CNT 2
#define MAX_HSEKEEP_XMT_INTF_CNT 2

#define MAX_XLINK_SW 15

/*##########################################################################################*/
/*************	Type Definitions										*********************/
/*##########################################################################################*/

/****************************************************************************************/
/**
 * @defgroup ServiceServerTypesPub ServiceServer puplic types
 * public definitions for ServiceServer types
 * @{
 *****************************************************************************************/
typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_TC_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
} SERVICE_COMMON_TC_STRUCT;

/* define a common struct */
typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 data;								//!< max. 1428 byte (357xuint32)
} SERVICE_COMMON_STRUCT;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_IDL_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
} SERVICE_IDLE_STRUCT;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_RESET_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
} SERVICE_RESET_STRUCT;



typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_IP_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 ip_address;							//!< IP-Address
} SERVICE_IP_ADR_STRUCT;

typedef struct
{
    uint32 Seconds			: 6;				//!< 6 bits to represent 60 seconds in a minute
    uint32 Minutes			: 6;				//!< 6 bits to represent 60 minutes in an hour
    uint32 Hours			: 5;				//!< 5 bits to represent 24 hours in a day
    uint32 Year		        	: 6;				//!< 6 bits to represent 0 to 63 (to note year 2000 to 2063)
    uint32 Months			: 4;				//!< 4 bits to represent 12 months in a year
    uint32 Day			        : 5;				//!< 5 bits to represent 31 days in a month
    uint8 FileName[4];						//!< 4 byte for File Name; FPGA / SWAP(CPU0) / CPU1(optional)
    uint32 start_adr;						//!< first address in QSPI (only for information)
    uint32 len			        :28;				//!< len in byte (only for information)
    uint32 Flags			: 4;				//!< Flags for SOLL-1; RUN- 0x2;...
} VERSION_DATE_STRUCT;					//!< Structure for Version Info

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_VER_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    VERSION_DATE_STRUCT	prog_version;
} SERVICE_VERSION_STRUCT;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_VER_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    VERSION_DATE_STRUCT	prog_version[MAX_XLINK_SW];
} SERVICE_VERSION_STRUCT_GET;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_RF_XMT_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 PktType			: 2;				//!< type of Packet
    uint32 ErrInf			: 5;				//!< error information
    uint32 PktLen			:12;				//!< number of bytes per message(payload packet)
    uint32 EthFirst		: 1;				//!< 1- first Ethernet packet of fragmented message(payload packet)
    uint32 EthLast			: 1;				//!< 1- last Ethernet packet of fragmented message(payload packet)
    uint32 EthNumByte		:11;				//!< number of payload bytes in this Ethernet packet
    /*------------------  uint32 word boundary  -------------------*/
    uint32 MoreInfo;							//!< Magic Number, Free Bytes, ....
    uint32 csp_header;
    uint8 Payload[MAX_SER_RF_PAYLOAD_CNT];						//!< Payload Data (1...2048 depending on PktLen)
} SERVICE_RF_XMT_DATA_STRUCT;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_START_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 ip_address;							//!< Destination IP-Address for the RcvData
    uint32 port			:16;				//!< Destination UDP Port
    uint32 send_format		: 4;				//!< the SERVICE_RF_XMT_DATA_STRUCT can be reduced
#define RF_RCV_SEND_NORMAL 0x0 //!< sends the complete SERVICE_RF_XMT_DATA_STRUCT
#define RF_RCV_SEND_ERR_INF 0x1 //!< sends only parts of the STRUCT -> starts with PktType
#define RF_RCV_SEND_RAW 0x2 //!< if the BchBlk is correct decoded, sends only the data
#define RF_RCV_SEND_CLTU 0x3 //!< sends complete CLTUs
    /* NOTE: more then one EthPacket could be necessary */
	uint32 output_interface	: 3; //!< the data output interface
	#define RF_RCV_OUTPUT_UDP 0x0 //!< send uplink data over UDP/ETH
	#define RF_RCV_OUTPUT_CSP 0x1 //!< send uplink data over CSP/UART
	#define RF_RCV_OUTPUT_SPI 0x2 //!< send uplink data over SPI
    uint32 Reserve			: 9;				//!< reserve				//!< reserve
} SERVICE_START_RF_RCV_STRUCT;

typedef struct
{

    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_FEATURE_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    FEATURE_SELECT_STRUCT feature_select;	//!< 16Byte (128Bit) structure for Feature Selection
}SERVICE_FEATURE_SELECT_STRUCT;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_DEVICE_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    uint32 service_event;						//!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    RFE_HW_PIN_STRUCT   	HwPins;				//!< 1xuint32 Structure for RFE HW-Pins
    RFE_FREQ_STRUCT		RfeFreq;			//!< 8xuint32 Structure for RFE Frequencies
    LIM_FREQ_STRUCT		LimFreq;			//!< 8xuint32 Structure for RFE HW-Frequencies Limits
    AD9361_CTR			AdCtr;				//!< 1xuint32 Structure for AD9361 Parameters
    XMT_CTR0			XmtCtr0;			//!< 1xuint32 Structure for FPGA control (Xmt signal generation)
    XMT_CTR3			XmtCtr3;			//!< 1xuint32 Structure for Xmt Power and Transfer Frame Size control
    XMT_CTR4			XmtCtr4;			//!< 1xuint32 Structure for more Xmt Parameters
    RCV_CTR			RcvCtrCh[MAX_SER_DEVICE_RCVCTRCH_CNT];    		//!< 2xuint32 Structure for different Rcv Parameters
    AD9361_SYMB_FREQ    	SymbFreq;			//!< 2xU16 Xmt/Rcv Symbol Frequency
    RCV_CTR0			RcvCtr_0;			//!< 1xuint32 Structure for Rcv ChSwitchOn_0/1
} SERVICE_DEVICE_CONF_STRUCT;

typedef struct
{
    /*----------------------- U64 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_HOS_KEEP_PRODUCT_KEY_CNT];						//!< XLINK device specific key
    /*----------------------- U64 boundary [2] -------------------------*/
    uint32 service_event;						//!< type of the service request
    uint32 Rest;							//!< data structure alignment (8 byte)
    /*----------------------- U64 boundary [3] -------------------------*/
    HOUSE_KEEP_STRUCT HouseKeep;			//!< XLINK House Keeping Data
}SERVICE_HOUSE_KEEP_STRUCT;

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[MAX_SER_DVB_PRODUCT_KEY_CNT]; //!< XLINK device specific key
    uint32 service_event; //!< type of the service request
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 ModCod_0 : 5; //!< Configuration: MODCOD see table below
    uint32 FrmType_0 : 2; //!< Frame type 0-NORMAL; 1-SHORT; 2-ALTERNATIV;
    uint32 InsertPilots_0 : 1; //!< Pilots insertion 0-Off, 1-On
    uint32 Acm_Ccm_0 : 1; //!< 0-ACM(Adaptive-CM), 1-CCM(Constant-CM)
    uint32 RollOffVal_0 : 3; //!< RollOff Factor 2-0.20
    uint32 PlSpreadNumber_0 :18; //!< PL spreading sequence number, 0...262141, mission constant
    uint32 Reserve_0 : 2; //!< reserve bits
    /*----------------------- uint32 boundary [6] -------------------------*/
    uint32 Reserve_1; //!< dummy for future channel specific configs or other
} SERVICE_DVBS2_CONF_STRUCT; /* size: 28 bytes (7 WORDs) */

#if 0
typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[4]; //!< XLINK device specific key
    uint32 service_event; //!< type of the service request Here: 0x2000000F
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 ControlBits; //!< TxFiFo init/flush:
    /*----------------------- uint32 boundary [6] -------------------------*/
    uint32 MoreInfo; //!< Magic Number:
    /*----------------------- uint32 boundary [7] -------------------------*/
} SERVICE_RF_XMT_DATA_STRUCT; /* Service Server Request FIFO init */

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[4]; //!< XLINK device specific key
    uint32 service_event; //!< type of the service response
    /*----------------------- uint32 boundary [5] -------------------------*/
    uint32 PktType : 2; //!< type of Packet:
    uint32 ErrInf : 5; //!< error information:
    uint32 PktLen :12; //!< number of processed input data bytes (incl. Service Server structures)
    uint32 EthFirst : 1; //!< not used
    uint32 EthLast : 1; //!< not used
    uint32 EthNumByte:11; //!< not used
    /*----------------------- uint32 boundary [6] -------------------------*/
    uint32 MoreInfo; //!< If RF_XMT_PKT_DROP, the missing UDP packet counter, else the currently occupied internal TransferFrame (FIFO-)memory
    /*----------------------- uint32 boundary [7] -------------------------*/
} SERVICE_RF_XMT_DATA_STRUCT; /* Service Server Response */

typedef struct
{
    /*----------------------- uint32 boundary [0] -------------------------*/
    uint32 product_key[4]; //!< Character string: ""\r\nRF-RCVData----""
    uint32 service_event; //!< Here: 0x0000000F
    /*----------------------- uint32 boundary -------------------------*/
    uint32 PktType : 2; //!< type of Packet
    uint32 ErrInf : 5; //!< error information
    uint32 PktLen :12; //!< not used
    uint32 EthFirst : 1; //!< 1- first Ethernet packet of fragmented message(payload packet)
    uint32 EthLast : 1; //!< 1- last Ethernet packet of fragmented message(payload packet)
    uint32 EthNumByte:11; //!< number of payload bytes in this Ethernet packet (always 7)
    /*----------------------- uint32 boundary -------------------------*/
    uint32 MoreInfo; //!< Index of BCH block data within received CLTU or 0x0001FFFF for CLTU tail sequence
    /*----------------------- uint32 boundary -------------------------*/
    uint8 Payload[7]; //!< 7-Byte BCH decoded data or CLTU tail sequence
} SERVICE_RF_XMT_DATA_STRUCT; /* RF_RCV_Data */
/* 35 bytes, if send_format = RF_RCV_SEND_NORMAL */
/* 15 bytes, if send_format = RF_RCV_SEND_ERR_INF */
#endif


/***
 * @brief This is newly added structure as per vendors latest service manual v1.7.1
 *
 */

typedef struct
{
/*----------------------- U32 boundary [0] -------------------------*/
uint32_t product_key[4]; //!< XLINK device specific key
uint32_t service_event; //!< type of the service request
/*----------------------- U32 boundary [5] -------------------------*/
uint32_t PGOOD_0 :1; //!< XPA 0 flag if internal power supply is good
uint32_t Rest_0 :31; //!< reserve
/*----------------------- U32 boundary [6] -------------------------*/
int32_t Current_0 :16; //!< XPA 0 internal amplifier current
int32_t Temp_int_0 :16; //!< XPA 0 internal Temperature
/*----------------------- U32 boundary [7] -------------------------*/
uint32_t PGOOD_1 :1; //!< XPA 0 flag if internal power supply is good
uint32_t Rest_1 :31; //!< reserve
/*----------------------- U32 boundary [8] -------------------------*/
int32_t Current_1 :16; //!< XPA 0 internal amplifier current
int32_t Temp_int_1 :16; //!< XPA 1 internal Temperature
} SERVICE_XPA_INF_STRUCT; /* size: 36 bytes (9 WORDs) */



/* !!! NOTE !!!
 * All members of the SERVICE_UNION have to be uint32 alignment a size! (with the exception of SERVICE_HOUSE_KEEP_STRUCT -> U64)
 * Also all transferred Packets have to be modulo 4(n x uint32) size!
 */
typedef union COMMAND_STRUCT
{
    SERVICE_COMMON_STRUCT		service_common;
    SERVICE_IDLE_STRUCT			service_idle;
    SERVICE_RESET_STRUCT		service_reset;
    SERVICE_IP_ADR_STRUCT               service_ip_adr;
    SERVICE_VERSION_STRUCT	        service_version;
    SERVICE_START_RF_RCV_STRUCT         service_start_rf_rcv;
    SERVICE_RF_XMT_DATA_STRUCT	        service_rf_xmt_data;
    SERVICE_DEVICE_CONF_STRUCT		service_device_conf;
    SERVICE_HOUSE_KEEP_STRUCT		service_house_keep;
    SERVICE_FEATURE_SELECT_STRUCT	service_feature_select;
}COMMAND_STRUCT;



/*** RX_UDP_DATA_STRUCT ***/
typedef struct
{

uint16_t	start_seq;
uint8_t		data[RX_BUFF_SIZE];
uint32_t	tail_seq[2];
} RX_UDP_DATA_STRUCT;


/** @} *//*defgroup ServiceServerTypesPub ***********************************************/


/** @} *//*defgroup ServiceServerConstantPub ********************************************/


/*##########################################################################################*/
/*************	Macros (InLine Functions) Definitions					*********************/
/*##########################################################################################*/
/****************************************************************************************/
/**
 * @defgroup ServiceServerMacroPub ServiceServer public macros
 * public definitions for ServiceServer macros
 * @{
 *****************************************************************************************/
/** @} *//*defgroup ServiceServerMacroPub ***********************************************/


#endif /*COMMS_SBAND_XLINK_SS_H */
