#ifndef	_Protocol_iSTP_H_
#define	_Protocol_iSTP_H_

// ****************************************************************************
// CONDITIONAL COMPILATION DIRECTIVES: Conditional compilation control
// ****************************************************************************


// ****************************************************************************
// INCLUDE FILES: Header files of modules referenced by this module
// ****************************************************************************


// ****************************************************************************
// MODULE DOCUMENTATION: Detailed description of the module
// ****************************************************************************


// ****************************************************************************
// PUBLIC TYPES: Public types shared with other modules
// ****************************************************************************
typedef struct								/**! \brief Packet structure contains all mandatory members. Sum size is 18 bytes*/
{
	uint32_t	  MsgNumber;				/**! \brief Message number. The number starts from 0 and increasing by 1*/
	uint32_t	  Type;	//uint32_t	Type;	/**! \brief Type of message*/
	uint64_t      Time;
	uint32_t	  ID;						/**! \brief Specify type of message*/
	uint32_t	  CRC;						/**! \brief Control sum from all packet members including data vector's members*/
	uint32_t	  Length;					/**! \brief Equal to length of data vector*/
}PacketStruct_t;

// ****************************************************************************
// PUBLIC MACROS: Public #define directives shared with other modules
// ****************************************************************************

#define	BUFFER_LENGTH	512		/**! \brief Maximum members in data vector*/
#define HEADER			0x23	/**! \brief Header hexadecimal value (equal to #)*/
#define SUCCESS			1		/**! \brief Global value for success executions from function*/
#define ERR				-1		/**! \brief Global value for non-specific error*/
#define ERR_NOT_ALOC	-2		/**! \brief Global value for allocate memory error*/
#define ERR_CANT_READ	-3		/**! \brief Global value for can't read error*/
#define ERR_CANT_SET	-4		/**! \brief Global value for can't set error*/

//******************************************************************************
// Type of message
//******************************************************************************
#define	FORCESTOP	1			/**! \brief Force stop message*/
#define VERSION_MSG	(0x05)							    /**! \brief Version message*/
#define	ERROR_MSG	(1 << 1)	/**! \brief Error message*/
#define	CONFFILE	(1 << 2)	/**! \brief Configure message*/
#define	INIT_MSG	(1 << 3)	/**! \brief Initialize message*/
#define DATA_MSG	(1 << 4)	/**! \brief Data message*/
#define CONTROL_MSG	(1 << 5)	/**! \brief Control message*/
#define COMENT		(1 << 6)	/**! \brief Comment message*/
#define HEALTH_DATA (0x11)      /**! \brief Health Data message*/
#define	ACK			0xFF		/**! \brief ACK message*/

//******************************************************************************
// Type of ID - Force stop
//******************************************************************************
#define  FORCESTOP_ID_STOP 1	/**! \brief Force stop message. Process must be terminated*/

//******************************************************************************
// Defines type of ID - Initialization
//******************************************************************************
#define INIT_MSG_ID_3V3		1			/**! \brief Value for 3V power line*/
#define INIT_MSG_ID_5VD		(1 << 1)	/**! \brief Value for 5V DC power line*/
#define INIT_MSG_ID_5VA		(1 << 2)	/**! \brief Value for 5V AC power line*/
#define INIT_MSG_ID_EXT		(1 << 3)	/**! \brief Value for EXT power line*/
//******************************************************************************
// Defines type of ID - Control message
//******************************************************************************
#define CONTROL_ID_START		1			/**! \brief Control message with start command*/
#define CONTROL_ID_STOP			(1 << 1)	/**! \brief Control message with stop command*/
#define CONTROL_ID_STOP_CONF	(1 << 2)	/**! \brief Control message with stop configure process command*/
//******************************************************************************
// Defines type of ID - Configure message
//******************************************************************************
#define CONFFILE_ID_SEND	     1		 /**! \brief Configure message with send command*/
#define CONFFILE_ID_BOARD	    (1 << 1) /**! \brief Configure message with board re-send*/
#define	CONFFILE_CHANGE		    (1 << 2) /**! \brief Configure message with start String*/
#define CONFFILE_SAMPLE_RATE	(1 << 3) /**! \brief Configure message with new sensor sampling frequency*/
#define CONFFILE_SEN_NUM	    (1 << 4) /**! \brief Configure message with number of virtual sensors */
#define CONFFILE_FIFO_MODE	    (1 << 5) /**! \brief Configure message with FIFO enable/disable*/
#define CONFFILE_FIFO_READ_RATE	(1 << 6) /**! \brief Configure message with rate of sensor FIFO reading*/
#define CONFFILE_ID_HEALTH_DATA (0x07)   /**! \brief Configure message with enable/disable health data request*/
//******************************************************************************
// Defines type of ID - Error message
//******************************************************************************
#define ERROR_ID_LOST_PERIODS    1		 /**! \brief Error message - Lost Periods */
#define ERROR_ID_RATE_FAILURE	 2		 /**! \brief Sample rate cannot be achieved */
#define	ERROR_ID_CMD_FAILURE	 3    	 /**! \brief Received command is unknown or bad data */
#define ERROR_ID_BAD_CRC       	 4       /**! \brief Received CRC is not the same as calculated */

//******************************************************************************
// Defines bit mask for ID
//******************************************************************************
#define MASK_SENSOR_NUM		0x0000FFFF		/**! \brief Mask for separation sensor number*/
#define MACK_BOARD_NUM		0x00FF0000		/**! \brief Mask for separation board number*/
#define MASK_AXIS_NUM		0xFF000000		/**! \brief Mask for separation orientation number*/
//******************************************************************************
// Defines
//******************************************************************************
#define DOLAR_CHECKER(c)	(c == 0x24) ? (1) : (0)		/**! \brief Method for checked if input value is equal to hexadecimal number 0x24 (equal to $)*/
#define DOLAR_GET			0x25						/**! \brief %*/
#define CHAR_MSG			0x23						/**! \brief #*/


// ****************************************************************************
// PUBLIC VARIABLES: Public variables shared with other modules
// ****************************************************************************
//extern uint32_t MsgNumber;

// ****************************************************************************
// PUBLIC FUNCTIONS: Public functions shared with other modules
// ****************************************************************************
void ISTPPrxMessageProcessed(void);
void ReadIncomeMSG(PacketStruct_t *p_Packet);
int8_t SendSwVersionMsg( void );
int8_t SendErrMsg(PacketStruct_t *p_Packet, uint32_t aValue);
int8_t SendHealthDataMsg( uint32_t * healthData, uint8_t healthDataLen );
int8_t NullPacketData(PacketStruct_t *p_Packet);
int8_t SendInitMsg(PacketStruct_t *p_Packet, uint32_t aValue);
void decodePacket(void);
void readSensorDataAndSendItToPc(double *timestamp);
int8_t GetUSBState(void);
uint32_t ISTPPgetLostTxMessagesCnt(void);
void HealthDataProvider(double timestamp);

#endif   //_Protocol_iSTP_H_