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

FILE INFORMATION HEADER

USED IN PROJECT:			S1003065-01 PPS-TX/S1003067-01 PPS-RX software application
ITEM TYPE:					Source Code
ITEM NAME:					xlru_psm.h
ITEM DESCRIPTION:			This file contains the list of class and interface definitions for the PSM / 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_PSM_H_
#define XLRU_PSM_H_

#include <stdint.h>

#include "xlru_message_def.h"

#define DEF_CMDID			0
#define DEF_EMERGENCY		0
#define DEF_FAST_CMD		0

//definition of ASCII for message
#define PSM_COMMAND_MSG				'C'
#define PSM_CMDID_POWER_DOWN		'P'		//power-down request - immediately switch-off
#define PSM_CMDID_POWER_RECYCLE		'R'
#define PSM_CMDID_GO				'G'		//GO, in normal operation
#define PSM_EMERGENCY_E				'E'		//emergency mode enable
#define PSM_EMERGENCY_IGNORE		'B'		//ignore battle short
#define PSM_EMERGENCY_NOT_E			'X'	//<> 'E' emergency mode disable
#define PSM_FAST_CMD_CHARGE			'C'		//charge 3SEC counter
#define PSM_FAST_CMD_DISCHARGE		'_'		//discharge 3SEC counter
//#define PSM_SPARE					'_'


//response
#define PSM_STS_ID					'S'
#define PSM_STS_IDLE				'I'
#define PSM_STS_POWERUP				'P'
#define PSM_STS_NORMAL				'G'
#define PSM_STS_FAIL				'F'
#define PSM_STS_MICROINTERR			'M'
#define PSM_STS_BACKUP				'B'
#define PSM_HAZARD_NORMAL			'N'
#define PSM_HAZARD_WARNING			'W'
#define PSM_HAZARD_1				'h'
#define PSM_HAZARD_2				'H'
#define PSM_3SEC_CHARGED			'C'
#define PSM_3SEC_NOT_CHARGED		'c'
#define PSM_WOW_ACTIVE				'W'
#define PSM_WOW_INACTIVE			'w'
#define PSM_AC_OK					'A'
#define PSM_AC_1					'1'
#define PSM_AC_2					'2'
#define PSM_AC_3					'3'
#define PSM_AC_4					'4'
#define PSM_AC_5					'5'
#define PSM_AC_6					'6'
#define PSM_AC_7					'7'
#define PSM_AC_FAIL					'a'
#define PSM_BS_NORMAL				'N'
#define PSM_BS_ACTIVE				'B'
#define PSM_DC_OK					'N'
#define PSM_DC_FAIL					'U'
#define PSM_PFC_NORMAL				'N'
#define PSM_PFC_UNDER				'U'
#define PSM_PFC_OVER				'O'
#define PSM_OUT_RAIL_NORMAL			'N'
#define PSM_OUT_RAIL_UNDER			'U'
#define PSM_OUT_RAIL_OVER			'O'


// TX MESSAGE PAYLOAD BYTES INDEX
#define PSM_STS_ID_INDEX			4
#define PSM_STS_INDEX				5
#define PSM_HAZARD_INDEX			6
#define PSM_3SEC_INDEX				7
#define PSM_WOW_INDEX				8
#define PSM_AC_INDEX				9
#define PSM_BS_INDEX				10
#define PSM_DC_INDEX				11
#define PSM_PFC_INDEX				12
#define PSM_OUT_RAIL3V3_INDEX		13
#define PSM_OUT_RAIL5V_INDEX		14
#define PSM_OUT_RAIL15V_INDEX		15
#define PSM_OUT_RAILNEG15V_INDEX	16
#define PSM_IN_AMP1_INDEX			17
#define PSM_IN_AMP2_INDEX			18
#define PSM_OUT_VOLT3V3_1_INDEX		19
#define PSM_OUT_VOLT3V3_2_INDEX		20
#define PSM_OUT_VOLT3V3_3_INDEX		21
#define PSM_OUT_VOLT3V3_4_INDEX		22
#define PSM_OUT_VOLT5V_1_INDEX		23
#define PSM_OUT_VOLT5V_2_INDEX		24
#define PSM_OUT_VOLT5V_3_INDEX		25
#define PSM_OUT_VOLT5V_4_INDEX		26
#define PSM_OUT_VOLT15V_1_INDEX		27
#define PSM_OUT_VOLT15V_2_INDEX		28
#define PSM_OUT_VOLT15V_3_INDEX		29
#define PSM_OUT_VOLT15V_4_INDEX		30
#define PSM_OUT_VOLTNEG15V_1_INDEX	31
#define PSM_OUT_VOLTNEG15V_2_INDEX	32
#define PSM_OUT_VOLTNEG15V_3_INDEX	33
#define PSM_OUT_VOLTNEG15V_4_INDEX	34
#define PSM_TEMP1_INDEX				35
#define PSM_TEMP2_INDEX				36
#define PSM_ETI1_INDEX				37
#define PSM_ETI2_INDEX				38
#define PSM_ETI3_INDEX				39
#define PSM_ETI4_INDEX				40
#define PSM_ON_COUNTER1_INDEX		41
#define PSM_ON_COUNTER2_INDEX		42
#define PSM_ON_COUNTER3_INDEX		43
#define PSM_ON_COUNTER4_INDEX		44
#define PSM_SW_MAJORL_INDEX			45
#define PSM_SW_MAJORH_INDEX			46
#define PSM_SW_MINORL_INDEX			47
#define PSM_SW_MINORH_INDEX			48
#define PSM_UNC_COUNTERL_INDEX		49
#define PSM_UNC_COUNTERH_INDEX		50
#define PSM_BITE_WORD1_INDEX		51
#define PSM_BITE_WORD2_INDEX		53
#define PSM_BITE_WORD3_INDEX		55
#define PSM_BITE_WORD4_INDEX		57
#define PSM_SPARE1_TX_INDEX			58
#define PSM_SPARE2_TX_INDEX			59

//	TX FOOTER BYTES INDEX
#define PSM_CHK_TXH_INDEX		60
#define PSM_CHK_TXL_INDEX		61
#define PSM_EOM_TX_INDEX		62

#define PSM_IN_AMP_LSB				(double)0.1		//DC_BUS_CURRENT measure
#define PSM_OUT_VOLT_LSB			(double)0.1		//5V, 3V3_int, 15V, -15V measure
#define PSM_TEMP_LSB				(double)1		//BOARD_TEMP measure

enum psm_mode_status_t
{
	PSMIdle = 'I',				//idle
	PSMPowerUp = 'P',			//power-up in progress
	PSMNormal = 'G',			//normal operation
	PSMFail = 'F',				//fail
	PSMMicroInterrupt = 'M',	//micro-interruption in progress
	PSMBackuUp = 'B'			//backup tha is in case of timeout on valid command reception
};

enum psm_hazard_status_t
{
	PSMHazardNormal = 'N',			//normal
	PSMWarning = 'w',			//warning
	PSMHazard1 = 'h',			//hazard 1
	PSMHazard2 = 'H'			//hazard 2 not override by emergency command
};

enum psm_3sec_status_t
{
	PSM3SecCharged = 'C',		//3sec charged
	PSM3SecNotCharged = 'c'		//3sec not charged
};

enum psm_wow_status_t
{
	PSMWowActive = 'W',			//wow active
	PSMWowInactive = 'w'		//wow inactive
};

enum psm_ac_status_t
{
	PSMACOk = 'A',				//AC ok, AC_LOSS (high) ok
	PSMAC1PhaseA	= '1',
	PSMAC2PhaseB = '2',
	PSMAC3 = '3',
	PSMAC4PhaseC = '4',
	PSMAC5 = '5',
	PSMAC6	= '6',
	PSMAC7	= '7',
	PSMACFail = 'a'				//AC Fail, AC_LOSS (low) fail
};

enum psm_bs_status_t
{
	PSMBattleShortCmdOff = 'N',	//battle short cmd off
	PSMBattleShortCmdOn = 'B'	//battle short cmd on
};

enum psm_dc_status_t
{
	PSMUV270OK = 'N',			//uv_270v (high) off
	PSMUV270Fail = 'U'			//uv_270v (low) fail
};

enum psm_pfc_status_t
{
	PSMUV350veOV350vnormal = 'N', //normal, uv_350v(high) & ov_350v(high)
	PSMUV350v = 'U',			//under, uv_350v (low)
	PSMOV350v = 'O'				//over, ov_350v (low)
};

enum psm_out_rail_status_t
{
	PSMOutRailNormal = 'N', 	//‘N’: normal, 5V, 3V3_int, 15V, -15V between min, max
	PSMOutRailUnder = 'U',		//'U’: under, 5V, 3V3_int, 15V, -15V under min
	PSMOutRailOver = 'O'		//‘O’: over,  5V, 3V3_int, 15V, -15V over max
};

struct psm_status_byte4_t
{
	unsigned int b0_rdr_on_sts : 1;
	unsigned int b1_lcu_rdy_uc : 1;
	unsigned int b2_uc_interlock_o : 1;
	unsigned int b3_ac_loss : 1;
	unsigned int b4_ps_interlock_n : 1;
	unsigned int b5_ov_temp_haz : 1;
	unsigned int b6_uv_270v : 1;
	unsigned int b7_battle_short_cmd : 1;

} __attribute__((packed));

struct psm_status_byte3_t
{
	unsigned int b0_ov_13v_b : 1;
	unsigned int b1_uv_13v_b : 1;
	unsigned int b2_wow_out : 1;
	unsigned int b3_3sec_in : 1;
	unsigned int b4_uv_350v : 1;
	unsigned int b5_ov_350v : 1;
	unsigned int b6_blko_tst : 1;
	unsigned int b7_pwr_err : 1;

} __attribute__((packed));

struct psm_status_byte2_t
{
	unsigned int b0_ov_m15v : 1;
	unsigned int b1_uv_m15v : 1;
	unsigned int b2_ov_15v : 1;
	unsigned int b3_uv_15v : 1;
	unsigned int b4_ov_5v : 1;
	unsigned int b5_uv_5v : 1;
	unsigned int b6_ov_3v3 : 1;
	unsigned int b7_uv_3v3 : 1;

} __attribute__((packed));

struct psm_status_byte1_t
{
	unsigned int b0_spare : 1;
	unsigned int b1_PSM_EIF_comm : 1;
	unsigned int b2_rs422_comm : 1;
	unsigned int b3_rs485_comm : 1;
	unsigned int b4_rs232_comm : 1;
	unsigned int b5_ov_temp_haz_2 : 1;
	unsigned int b6_ov_board_temp : 1;
	unsigned int b7_ov_dc_bus_current : 1;

} __attribute__((packed));


class xlru_psm
{
private:
	struct xlru_psm_command_t
	{
		unsigned int cmd_id;		// 0 -> 'P', 1 -> 'R', 2 -> 'G'
		unsigned int emergency;		// 0 -> 'E', 1 -> 'B' ,3 -> <>'E'
		unsigned int fast_cmd;		// 0 -> 'C', 1 -> '_'
		int mode;
	};

	static xlru_psm_command_t xlru_psm_command;

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

		psm_mode_status_t mode_status;
		psm_hazard_status_t hazard_status;
		psm_3sec_status_t _3sec_status;
		psm_wow_status_t wow_status;
		psm_ac_status_t ac_status;
		psm_bs_status_t bs_status;
		psm_dc_status_t dc_status;
		psm_pfc_status_t pfc_status;
		psm_out_rail_status_t out_rail_VOLT3V3_status;
		psm_out_rail_status_t out_rail_VOLT5V_status;
		psm_out_rail_status_t out_rail_VOLT15V_status;
		psm_out_rail_status_t out_rail_VOLTNEG15V_status;

		float in_amp;		//LSB=0.1 DC_BUS_CURRENT measure
		float out_volt_VOLT3V3;		//LSB=0.1  3V3_int
		float out_volt_VOLT5V;		//LSB=0.1  5V
		float out_volt_VOLT15V;		//LSB=0.1  15V
		float out_volt_VOLTNEG15V;	//LSB=0.1  -15V
		int temperature;			//LSB=1  BOARD_TEMP measure

		unsigned int eti;			//Counter_day*1440+Counter_hr*60+Counter_min
		unsigned int on_counter;	//psm_on_counter
		unsigned int sw_major;		//software version
		unsigned int sw_minor;		//software revision, release
		unsigned int unc_counter;	//Micro-interruptions counter - microint_count

		psm_status_byte1_t byte1;
		psm_status_byte2_t byte2;
		psm_status_byte3_t byte3;
		psm_status_byte4_t byte4;


		char extra_info[BUFFER_DIM];//contiene tutti i byte presenti nel payload in più rispetto al protocollo

	};

public:

	static xlru_psm_status_t xlru_psm_status;

	static xlru_counter_t xlru_counter;

	static char xlru_psm_fcs_prev;			//fcs previous

	//****************************************************
	// MASTER - from EIF TO PSM-
	//				SET MESSAGE - PSM Operative Control Msg
	//				GET MESSAGE
	static void psm_header_master(void);
	static void psm_get_message_payload_master(void);
	static void psm_set_message_payload_master(void);
	static char* psm_get_message_master(void);
	static char* psm_set_message_master(void);
	static char *psm_getStringTx(void);
	static char *psm_getStringRx(void);

	static void psm_setStringTx(char *txt);
	static void psm_setStringRx(char *txt);

	static void psm_reset_buffer_TX_RX(void);
	static void init(void);

	static unsigned int get_psm_cmd_id();
	static unsigned int get_psm_fast_cmd();
	static unsigned int get_psm_emergency();

	static void set_psm_cmd_id(int value);		// 0 -> 'P', 1 -> 'R', 2 -> 'G'
	static void set_psm_fast_cmd(int value);	// 0 -> 'E', 1 -> 'B' ,3 -> <>'E'
	static void set_psm_emergency(int value);	// 0 -> 'C', 1 -> '_'

	static int xlru_psm_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();

};



#endif /* XLRU_PSM_H_ */
