/*==================================================================================================

FILE INFORMATION HEADER

USED IN PROJECT:			S1003065-01 PPS-TX/S1003067-01 PPS-RX software application
ITEM TYPE:					Source Code
ITEM NAME:					xlru_pps.h
ITEM DESCRIPTION:			This file contains the list of class and interface definitions for the PPS / REP connection
TARGET:						ATSAM4LC8B / ATSAM4LS8B
DOC REFERENCES:				SWDS1003065-01/SWDS1003065-01
PROGRAMMER NAME(S):			A.Chessa, L.Vallongo
COPYRIGHT:					LEONARDO-FINMECCANICA S.p.A
REVISION HISTORY:
- 01.00: first release

====================================================================================================
© Copyright LEONARDO S.p.A.. All rights reserved
Any right of industrial and intellectual property on this document,
and of technical Know-how herein contained, belongs to
LEONARDO S.p.A. and/or third parties.
According to the law, it is forbidden to disclose, reproduce or however
use this document and any data herein contained for any use without
previous written authorization by LEONARDO S.p.A.
==================================================================================================*/

#ifndef XLRU_PPS_H_
#define XLRU_PPS_H_

#include <stdint.h>

#include "xlru_message_def.h"

#define DEF_OPERATIVE	0
#define DEF_HAZARD		0
#define DEF_RADOME		0

//ASCII for message
#define PPS_CONTROL_MSG				'O'
#define PPS_GENERAL_STATUS			'S'
#define PPS_TEST_REPORTS			't'
#define PPS_OPERATIVE_CMD_IDLE		'I'
#define PPS_OPERATIVE_CMD_GO		'G'
#define PPS_OPERATIVE_CMD_T			'T'
#define PPS_HAZARD_OVERRIDE_NO		'N'
#define PPS_HAZARD_OVERRIDE			'O'
#define PPS_RADOME_BOUNCE_TEST_NO	'N'
#define PPS_RADOME_BOUNCE_TEST		'R'
#define PPS_SPARE					'_'
#define PPS_GET_MESSAGE				'T'

//Response
#define PPS_MODE_IDLE				'i'
#define PPS_MODE_POWERUP			'P'
#define PPS_MODE_STANDBY			'S'
#define PPS_MODE_IBIT				'I'
#define PPS_MODE_GO					'G'
#define PPS_HEALTH_GO				'G'
#define PPS_HEALTH_WARNING			'W'
#define PPS_HEALTH_FAIL				'F'
#define PPS_WOW_NOT_ACTIVE			'w'
#define PPS_WOW_ACTIVE				'W'
#define PPS_WOW_FAIL				'F'
#define PPS_HAZARD_NO				'_'
#define PPS_HAZARD_1				'h'
#define PPS_HAZARD_2				'H'
#define PPS_LIMIT_NO				'_'
#define PPS_LIMIT_CURRENT			'C'

//INDEX position
#define PPS_OPERATIVE_CMD_IDX		1
#define PPS_HAZARD_OVERRIDE_IDX		2
#define PPS_RADOME_BOUNCE_TEST_IDX	3

//index for response message
#define PPS_MODE_STATUS_INDEX		5
#define PPS_HEALTH_STATUS_INDEX		6
#define PPS_WOW_STATUS_INDEX		7
#define PPS_HAZARD_STATUS_INDEX		8
#define PPS_CURR_LIMIT_STATUS_INDEX	9
#define PPS_BYTES1_INDEX			10
#define PPS_BYTES2_INDEX			12
#define PPS_BYTES3_INDEX			14
#define PPS_BYTES4_INDEX			16
#define PPS_BYTES5_INDEX			18
#define PPS_BYTES6_INDEX			20
#define PPS_RX_VERSION_INDEX		22
#define PPS_RX_RELEASE_INDEX		24
#define PPS_TX1_VERSION_INDEX		26
#define PPS_TX1_RELEASE_INDEX		28
#define PPS_TX2_VERSION_INDEX		30
#define PPS_TX2_RELEASE_INDEX		32
#define PPS_TX3_VERSION_INDEX		34
#define PPS_TX3_RELEASE_INDEX		36
#define PPS_RX_TEMP_INDEX			38
#define PPS_TX1_TEMP_INDEX			40
#define PPS_TX2_TEMP_INDEX			42
#define PPS_TX3_TEMP_INDEX			44
#define PPS_LIQUID_INLET_TEMP_IDX	46
#define PPS_LIQUID_OUTLET_TEMP_IDX	48
#define PPS_CURR_IDX				50
#define PPS_TX1_CURR_IDX            52
#define PPS_TX2_CURR_IDX			54
#define PPS_TX3_CURR_IDX			56
#define PPS_TX1_CURR_MAX_IDX        58          //aggiunte nuove info
#define PPS_TX2_CURR_MAX_IDX        60          //correnti medie e correnti massime per ogni tx
#define PPS_TX3_CURR_MAX_IDX        62
#define PPS_TX1_CURR_AVG_IDX        64
#define PPS_TX2_CURR_AVG_IDX        66
#define PPS_TX3_CURR_AVG_IDX        68
#define PPS_PHASER_MIN_IDX          70          //aggiunte nuove info
#define PPS_PHASES_MIN_IDX          72          //phase R,S, min e max
#define PPS_PHASET_MIN_IDX          74
#define PPS_PHASER_MAX_IDX          76
#define PPS_PHASES_MAX_IDX          78
#define PPS_PHASET_MAX_IDX          80

#define PPS_CHECKSUM_MIN_INDEX			58		//per compatibilitą, lunghezza minima

#define PPS_CHECKSUM_INDEX			82			//da verificare, passato da 58 a 82 per aggiunta nuove correnti.


#define PPS_CURR_LSB			(double)0.1

enum pps_mode_status_t
{
	PPSIdle = 'i',
	PPSPowerUp = 'P',
	PPSStandBy = 'S',
	PPSOnGo = 'G',
	PPSIBIT = 'I'
};

enum pps_health_status_t
{
	PPSHealthGo = 'G',
	PPSHealthWarning = 'W',
	PPSHealthFail = 'F'

};

enum pps_wow_tellback_status_t
{
	PPSWowNotActive = 'w',
	PPSWowActive = 'W',
	PPSWowFail = 'F'
};

enum pps_hazard_status_t
{
	PPSNoHazard = '_',
	PPSHazard1 = 'h',
	PPSHazard2 = 'H'
};

enum pps_current_limitation_status_t
{
	PPSNoCurrentLimitation = '_',
	PPSCurrentLimitation = 'C'
};

struct pps_status_byte1_holdUp_boards_t
{
    unsigned int  b1_LP_under_voltage_fail : 1;
    unsigned int  b2_HP_under_voltage_fail : 1;
    unsigned int  b3_spare : 1;
    unsigned int  b4_spare : 1;
    unsigned int  b5_spare : 1;
	unsigned int  b6_AC_power_umbalance_fail_ph1 : 1;
	unsigned int  b7_AC_power_umbalance_fail_ph2 : 1;
	unsigned int  b8_AC_power_umbalance_fail_ph3 : 1;

}__attribute__((packed));

struct pps_status_byte2_rx_board_t
{
	unsigned int  b1_rx_link_fail : 1;
	unsigned int  b2_rx_input_dc_overcurrent_fail : 1;
    unsigned int  b3_rx_rectified_dc_under_voltage_fail_ac_power_loss : 1;
    unsigned int  b4_spare : 1;
	unsigned int  b5_output_rx_over_voltage_fail : 1;
	unsigned int  b6_output_rx_under_voltage_fail : 1;
	unsigned int  b7_rx_hazard : 1;
	unsigned int  b8_rx_hazard : 1;

}__attribute__((packed));

struct pps_status_byte3_tx1_board_t
{
	unsigned int  b1_tx1_link_with_rx : 1;
	unsigned int  b2_tx1_input_dc_over_current_fail : 1;
    unsigned int  b3_tx1_rectified_dc_under_voltage_fail_270_uv : 1;
    unsigned int  b4_spare : 1;
    //unsigned int  b4_tx1_rectified_dc_over_voltage_fail : 1;
	unsigned int  b5_output_tx1_over_voltage_fail : 1;
	unsigned int  b6_output_tx1_under_voltage_fail : 1;
	unsigned int  b7_tx1_hazard : 1;
	unsigned int  b8_tx1_hazard : 1;

}__attribute__((packed));

struct pps_status_byte4_tx2_board_t
{
	unsigned int  b1_tx2_link_with_rx : 1;
	unsigned int  b2_tx2_input_dc_over_current_fail : 1;
    unsigned int  b3_tx2_rectified_dc_under_voltage_fail_270_uv : 1;
    unsigned int  b4_spare : 1;
    //unsigned int  b4_tx2_rectified_dc_over_voltage_fail : 1;
	unsigned int  b5_output_tx2_over_voltage_fail : 1;
	unsigned int  b6_output_tx2_under_voltage_fail : 1;
	unsigned int  b7_tx2_hazard : 1;
	unsigned int  b8_tx2_hazard : 1;

}__attribute__((packed));

struct pps_status_byte5_tx3_board_t
{
	unsigned int  b1_tx3_link_with_rx : 1;
	unsigned int  b2_tx3_input_dc_over_current_fail : 1;
    unsigned int  b3_tx3_rectified_dc_under_voltage_fail_270_uv : 1;
    unsigned int  b4_spare : 1;
    //unsigned int  b4_tx3_rectified_dc_over_voltage_fail : 1;
	unsigned int  b5_output_tx3_over_voltage_fail : 1;
	unsigned int  b6_output_tx3_under_voltage_fail : 1;
	unsigned int  b7_tx3_hazard : 1;
	unsigned int  b8_tx3_hazard : 1;

}__attribute__((packed));

struct pps_status_byte6_general_t
{
	unsigned int  b1_wow_tellback_1 : 1;
	unsigned int  b2_wow_tellback_2 : 1;
	unsigned int  b3_wow_tellback_3 : 1;
    unsigned int  b4_spare : 1;
    unsigned int  b5_spare : 1;
    unsigned int  b6_spare : 1;
    unsigned int  b7_spare : 1;
    unsigned int  b8_spare : 1;

}__attribute__((packed));


class xlru_pps
{
private:

	struct xlru_pps_command_t
	{
		unsigned int operativeCmd;			// 0 -> 'I', 1 -> 'G'. 2 -> 'T'
		unsigned int hazardOverride;		// 0 -> 'N', 1 -> 'O'
		unsigned int radomeBounce_Test;		// 0 -> 'N', 1 -> 'R'
		int mode;
	};

	static xlru_pps_command_t xlru_pps_command;

	struct xlru_pps_status_t
	{
		xlru_unit_status_t status;
		bool warning;
		bool wow; //Weight-on-Wheel

		pps_mode_status_t mode_status;
		pps_health_status_t health_status;
		pps_wow_tellback_status_t wow_tellback_status;
		pps_hazard_status_t hazard_status;
		pps_current_limitation_status_t current_limitation_status;

		pps_status_byte1_holdUp_boards_t byte1_holdUp_boards;
		pps_status_byte2_rx_board_t byte2_rx_board;
		pps_status_byte3_tx1_board_t byte3_tx1_board;
		pps_status_byte4_tx2_board_t byte4_tx2_board;
		pps_status_byte5_tx3_board_t byte5_tx3_board;
		pps_status_byte6_general_t byte6_general;

		unsigned int rx_board_version;
		unsigned int rx_board_release;
		unsigned int tx1_board_version;
		unsigned int tx1_board_release;
		unsigned int tx2_board_version;
		unsigned int tx2_board_release;
		unsigned int tx3_board_version;
		unsigned int tx3_board_release;

		int rx_board_temp;
		int tx1_board_temp;
		int tx2_board_temp;
		int tx3_board_temp;
		int liquid_inlet_temp;
		int liquid_outlet_temp;

		float curr_42v;
		float tx_curr[3];
		
		bool tx_curr_max_valid;
		float tx_curr_max[3];
		bool tx_curr_avg_valid;
		float tx_curr_avg[3];

        float rx_phase_min[3];
        float rx_phase_max[3];
	};

public:

	static xlru_pps_status_t xlru_pps_status;

	static xlru_counter_t xlru_counter;

	static char xlru_pps_fcs_prev;			//fcs previous

	//****************************************************
	// MASTER - from EIF TO PPS -
	//				SET MESSAGE - PPS Operative Control Msg
	//				GET MESSAGE

	static void pps_header_master(void);
	static void pps_get_message_payload_master(void);
	static void pps_set_message_payload_master(void);
	static char* pps_get_message_master(void);
	static char* pps_set_message_master(void);
	static void pps_test_status_payload_master(void);

	static char *pps_getStringTx(void);
	static char *pps_getStringRx(void);

	static void pps_setStringTx(char *txt);
	static void pps_setStringRx(char *txt);

	static void pps_reset_buffer_TX_RX(void);
	static void init(void);

	static unsigned int get_pps_operativeCmd();			// 0 -> 'I', 1 -> 'G'. 2 -> 'T'
	static unsigned int get_pps_hazardOverride();		// 0 -> 'N', 1 -> 'O'
	static unsigned int get_pps_radomeBounce();			// 0 -> 'N', 1 -> 'R'

	static void set_pps_operative_cmd(int value);
	static void set_pps_hazard_override(int value);
	static void set_pps_radome_bounce(int value);

	static int xlru_pps_command_message_decoder(char* buffer);

	static void xlru_display(int (*print_func)(const char*, ...));
	static void xlru_display_compact(int (*print_func)(const char*, ...));

	static void xlru_add_error(int n_error);		//error into last decode response message
	static void xlru_add_rx(int n_rx);				//response messages
	static void xlru_add_rx_error(int n_error);		//response messages with error
	static void xlru_add_no_rx(int n_error);		//count messages without response
	static void xlru_set_last_error(char *last_error);	//report last error
	static void xlru_set_last_rx(char *last_rx);		//report last received message
	static void xlru_set_last_tx(char *last_tx);		//report last trasmitted message

	static char* xlru_get_last_error();
	static char* xlru_get_last_rx();
	static char* xlru_get_last_tx();

	static char* pps_set_test_status_master(void);
};

#endif /* XLRU_PPS_H_ */
