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

FILE INFORMATION HEADER

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

#include <stdint.h>
#include "xlru_message_def.h"

#define DEF_HEATER	true
#define DEF_IBIT	false
#define DEF_RESTART	false

//definition of ASCII for message
#define LCU_CONTROL_MSG				'L'
#define LCU_NO_TEST					'_'
#define LCU_TEST					'T'
#define LCU_NO_HEATER				'_'
#define LCU_HEATER_ON				'H'
#define LCU_NO_RESTART				'_'
#define LCU_RESTART					'R'
#define LCU_GET_STATUS				'S'
#define LCU_GET_TEST_STATUS 		'T'
#define LCU_STATUS_MESSAGE			's'
#define LCU_GET_MESSAGE				'S'
#define LCU_RESERVED				'_'
#define LCU_RESET_ETI               'R'
#define LCU_ACTIVE_DOWNLOAD         'D'
#define LCU_SW_STATUS               'A'

//response
#define LCU_POWER_UP				'P'
#define LCU_ON_GO					'G'
#define LCU_I_BIT					'I'
#define LCU_I_BIT_NOT_IN_PROGRESS	'i'
#define LCU_I_BIT_COMPLETED			'I'
#define LCU_I_BIT_NEVER_EXECUTED 	'_'
#define LCU_NOT_ACTIVE				'_'
#define LCU_BY_PASS_ACTIVE			'B'
#define LCU_GO						'G'
#define LCU_WARNING					'W'
#define LCU_FAIL					'F'
#define LCU_NO_HAZARD				'_'
#define LCU_HAZARD_1				'h'
#define LCU_HAZARD_2				'H'
#define LCU_TEST_STATUS				't'
//#define LCU_STATUS_SPARE			'_'
#define LCU_OVERRIDE                'w'
#define LCU_NO_OVERRIDE             '_'
#define LCU_CMD_OVERRIDE            'X'
#define LCU_CMD_OVERRIDE_SET_VALUE  'S'
#define LCU_CMD_OVERRIDE_RST_FACT   'R'
#define LCU_CMD_OVERRIDE_GET_VALUE  'G'
#define LCU_CMD_OVERRIDE_GET_FACT   'g'

//index for response message
#define LCU_MODE_STATUS_INDEX				5
#define LCU_BIT_PROGRESS_INDEX				6
#define LCU_BYPASS_INDEX					7
#define LCU_HEALTH_STATUS_INDEX				8
#define LCU_HAZARD_STATUS_INDEX				9
#define LCU_BYTES1_INDEX					14
#define LCU_BYTES2_INDEX					16
#define LCU_BYTES3_INDEX					18
#define LCU_BYTES4_INDEX					20
#define LCU_BYTES5_INDEX					22
#define LCU_CTRL_BOARD_TEMP_INDEX			24
#define LCU_MOTOR_TEMP_INDEX				26
#define LCU_MOTOR_VEL_INDEX					28
#define LCU_LIQUID_AESA_TEMP_INDEX			30
#define LCU_LIQUID_PPS_TEMP_INDEX			32
#define LCU_LIQUID_PUMP_TEMP_INDEX			34
#define LCU_LIQUID_OUTLET_TEMP_INDEX		36
#define LCU_AIR_INLET_TEMP_INDEX			38
#define LCU_AIR_OUTLET_TEMP_INDEX			40
#define LCU_LIQUID_AESA_INDEX				44
#define LCU_LIQUID_PPS_INDEX				46
#define LCU_LIQUID_OUTLET_PRESSURE_INDEX 	48
#define LCU_CTRL_BOARD_VERSION_INDEX		52
#define LCU_CTRL_BOARD_RELEASE_INDEX		54
#define LCU_OVERRIDE_INDEX          		56

#define LCU_STATUS_ETI_INDEX				11

#define LCU_MOTOR_VEL_LSB					100.0
#define LCU_LIQUID_OUTLET_PRESSURE_LSB		0.1
#define LCU_ETI_LSB							6.0

enum lcu_mode_status_t
{
	LCUreserved = '_',
	LCUPowerUp = 'P',
	LCUOnGo = 'G',
	LCUIBIT = 'I'
};

enum lcu_bit_progress_status_t
{
	LCUIBITnotInProgress = 'i',
	LCUIBITcompelted = 'I',
	LCUIBITneverExecuted = '_'
};

enum lcu_by_pass_status_t
{
	LCUByPassNotActive = '_',
	LCUByPassActive = 'B'
};

enum lcu_health_status_t
{
	LCUHealthGo = 'G',
	LCUHealthWarning = 'W',
	LCUHealthFail = 'F'

};

enum lcu_hazard_status_t
{
	LCUNoHazard = '_',
	LCUWarning = 'W',
	LCUHazard1 = 'h',
	LCUHazard2 = 'H'
};

enum lcu_override_status_t
{
    LCUNoOverride = '_',
    LCUOverride = 'w'
};

enum lcu_override_param_id_t
{
    all_threshold = 0,
    temp_board_warning = 1,
    temp_board_hazard = 2,
    motor_pump_temp_warning = 3,
    motor_pump_temp_hazard = 4,
    pressure_sencsor_fail_ps = 5,
    motor_speed_sensor_fail_warning_mfs = 6,
    motor_temp_sensor_fail_mts = 7,
    inlet_temp_sensor_fail_aesa_ts1 = 8,
    inlet_temp_sensor_fail_pps_ts2 = 9,
    inlet_temp_sensor_fail_combo_ts3 = 10,
    outlet_temp_sensor_fail_ts4 = 11,
    inlet_air_temp_sensor_fail_ts5 = 12,
    outlet_air_temp_sensor_fail_ts6 = 13,
    inlet_flow_sensor_aesa_branch_fs1 = 14,
    inlet_flow_sensor_pps_branch_fs2 = 15
};

struct lcu_status_byte1_general_pressure_t
{
	unsigned int b1_phase_A_fail : 1;
	unsigned int b2_phase_B_fail : 1;
	unsigned int b3_phase_C_fail : 1;
	unsigned int b4_phase_sequence_fail : 1;
    unsigned int b5_spare : 1;
    unsigned int b6_spare : 1;
	unsigned int b7_pressure_sensor_fail : 1;
    unsigned int b8_spare : 1;

} __attribute__((packed));

struct lcu_status_byte2_ctrl_board_t
{
	unsigned int b1_temperature_hazard : 1;
	unsigned int b2_temperature_warning : 1;
	unsigned int b3_nvm_access_warning : 1;
	unsigned int b4_board_error_fail : 1;
	unsigned int b5_link_fail : 1;
    unsigned int b6_spare : 1;
    unsigned int b7_spare : 1;
    unsigned int b8_spare : 1;

}__attribute__((packed));

struct lcu_status_byte3_motor_pump_t
{
	unsigned int b1_motor_speed_sensor_fail : 1;
	unsigned int b2_temperature_hazard : 1;
	unsigned int b3_temperature_warning : 1;
	unsigned int b4_motor_fail : 1;
    unsigned int b5_spare : 1;
    unsigned int b6_spare : 1;
    unsigned int b7_spare : 1;
	unsigned int b8_motor_temperature_sensor_fail : 1;

}__attribute__((packed));

struct lcu_status_byte4_sensor_t
{
	unsigned int b1_inlet_temp_sensor_fail_aesa_ts1 : 1;
	unsigned int b2_inlet_temp_sensor_fail_pps_ts2 : 1;
	unsigned int b3_inlet_temp_sensor_fail_combo_ts3 : 1;
	unsigned int b4_outlet_temp_sensor_fail_ts4 : 1;
	unsigned int b5_inlet_air_temp_sensor_fail_ts5 : 1;
	unsigned int b6_outlet_air_temp_sensor_fail_ts6 : 1;
	unsigned int b7_inlet_flow_sensor_aesa_branch_fs1 : 1;
	unsigned int b8_inlet_flow_sensor_pps_branch_fs2 : 1;

}__attribute__((packed));

struct lcu_status_byte5_general_fails_t
{
	unsigned int  b1_dirty_filter_he_warn : 1;
	unsigned int  b2_dirty_aesa_stream_warn1 : 1;
	unsigned int  b3_dirty_aesa_stream_warn2 : 1;
    unsigned int  b4_spare : 1;
	unsigned int  b5_dirty_filter_he_failure : 1;
	unsigned int  b6_dirty_aesa_stream_fail1 : 1;
	unsigned int  b7_dirty_aesa_stream_fail2 : 1;
	unsigned int  b8_by_pass_fail : 1;

}__attribute__((packed));

class xlru_lcu
{
public:
	struct xlru_lcu_command_t
	{
		unsigned int ibit;			// 0 -> '_', 1 -> 'T'
		unsigned int heater;		// 0 -> '_', 1 -> 'H'
		unsigned int restart;		// 0 -> '_', 1 -> 'R'
		int mode;
	};

	static xlru_lcu_command_t xlru_lcu_command;

	struct xlru_lcu_status_t
	{
		xlru_unit_status_t status;

		bool wow; //Weight-on-Wheel
		bool inputPowerFail;

		lcu_mode_status_t mode_status;
		lcu_bit_progress_status_t bit_progress;
		lcu_by_pass_status_t byPass_status;
		lcu_health_status_t health_status;
		lcu_hazard_status_t hazard_status;
        lcu_override_status_t override_status;
		lcu_status_byte1_general_pressure_t byte1_general_pressure;
		lcu_status_byte2_ctrl_board_t byte2_ctrl_board;
		lcu_status_byte3_motor_pump_t byte3_motor_pump;
		lcu_status_byte4_sensor_t byte4_sensor;
		lcu_status_byte5_general_fails_t byte5_general_fails;

		int ctrl_board_temp;
		int motor_temp_mts;
		float motor_speed_mfs;
		int liquid_inlet_temp_aesa_ts1;
		int liquid_inlet_temp_pps_ts2;
		int liquid_pump_inlet_temp_ts3;
		int liquid_outlet_temp_ts4;
		int air_inlet_temp_ts5;
		int air_outlet_temp_ts6;
		unsigned int liquid_inlet_flow_aesa_fs1;
		unsigned int liquid_inlet_flow_pps_fs2;
		float liquid_outlet_pressure_ps;
		unsigned int crtl_board_version;
		unsigned int crtl_board_release;

		float ETI;		//elapse time indicator
	};

	static xlru_lcu_status_t xlru_lcu_status;

	static xlru_counter_t xlru_counter;

	static char xlru_lcu_fcs_prev;			//fcs previous

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

	static void lcu_header_master(void);
	static void lcu_get_message_payload_master(void);
	static void lcu_set_message_payload_master(void);
	static char* lcu_get_message_master(void);
	static char* lcu_set_message_master(void);
	static void lcu_test_status_payload_master(void);
    static void lcu_reset_eti_payload_master(void);
    static char* lcu_reset_eti_message_master(void);
    static void lcu_active_download_payload_master(void);
    static char* lcu_active_download_message_master(void);
    static void lcu_sw_status_payload_master(void);
    static char* lcu_sw_status_message_master(void);

    static void lcu_override_set_value_payload(lcu_override_param_id_t _param_id,int _value);
    static char* lcu_override_set_value_message(lcu_override_param_id_t _param_id,int _value);

    static void lcu_override_get_value_payload(lcu_override_param_id_t _param_id);
    static char* lcu_override_get_value_message(lcu_override_param_id_t _param_id);

    static void lcu_override_get_factory_payload(lcu_override_param_id_t _param_id);
    static char* lcu_override_get_factory_message(lcu_override_param_id_t _param_id);

    static void lcu_sw_set_to_factory_payload(lcu_override_param_id_t _param_id);
    static char* lcu_override_set_to_factory_message(lcu_override_param_id_t _param_id);

	static char *lcu_getStringTx(void);
	static char * lcu_getStringRx(void);

	static void lcu_setStringTx(char *txt);
	static void lcu_setStringRx(char *txt);

	static void lcu_reset_buffer_TX_RX(void);
	static void init(void);

	static unsigned int get_lcu_ibit();
	static unsigned int get_lcu_heater();
	static unsigned int get_lcu_restart();

	static void set_lcu_ibit(bool enable);		// 0 -> '_', 1 -> 'T'
	static void set_lcu_heater(bool enable);	// 0 -> '_', 1 -> 'H'
	static void set_lcu_restart(bool enable);	// 0 -> '_', 1 -> 'R'

	static int xlru_lcu_command_message_decoder(char* buffer);

	//static void xlru_display();
	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* lcu_set_test_status_master(void);
};

#endif /* XLRU_LCU_H_ */
