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

FILE INFORMATION HEADER

USED IN PROJECT:			S1003065-01 PPS-TX/S1003067-01 PPS-RX software application
ITEM TYPE:					Source Code
ITEM NAME:					xlru_lcu.cpp
ITEM DESCRIPTION:			This file contains the implementation of the class e and the interfaces 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.
==================================================================================================*/

/**************************** INCLUDE ****************************************/
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "xlru_lcu.h"
#include "xlru_message_def.h"
#include "xlru_common_func.h"
#include "xlru_bytes_def.h"

using namespace xlru;

//GENERAL DECLARATION

char rx_buffer[BUFFER_DIM];		//bytes buffer to store input

char tx_buffer[BUFFER_DIM];		//bytes buffer to store output

/*header received */

uint8_t LCU_Mode_Status = LCUPowerUp;
uint8_t LCU_BIT_Progress = LCUIBITnotInProgress;
uint8_t LCU_Bypass_Status = LCUByPassNotActive;
uint8_t LCU_Health_Status = LCUHealthGo;
uint8_t LCU_Hazard_Status = LCUNoHazard;
//uint8_t LCU_Status_byte[5] = {0, 0, 0, 0, 0};

uint32_t previous_time = 0;
uint32_t error_link = 0;

//*** STATIC
xlru_lcu::xlru_lcu_command_t xlru_lcu::xlru_lcu_command;
xlru_lcu::xlru_lcu_status_t xlru_lcu::xlru_lcu_status;
xlru_counter_t xlru_lcu::xlru_counter;

char xlru_lcu::xlru_lcu_fcs_prev = 'p';


/**************************** MACRO DEFINITIONS ******************************/

void xlru_lcu::lcu_header_master(void)
{
	//SOM
	tx_buffer[0] = SOM;

	//DSTID
	tx_buffer[1] = DSTID_LCU;

	// FCS
	char fcs_temp = ((xlru_lcu::xlru_lcu_fcs_prev%16)+'a');
	tx_buffer[FCS_INDEX] = fcs_temp;
	xlru_lcu::xlru_lcu_fcs_prev = fcs_temp;

	tx_buffer[ACK_INDEX] = XGET;
}

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

void xlru_lcu::lcu_set_message_payload_master(void)
{
	tx_buffer[4] = LCU_CONTROL_MSG;				//"L", LCU Operative Control Msg

	if (xlru_lcu::xlru_lcu_command.ibit==1)
		tx_buffer[5] = LCU_TEST;				// LCU_TEST "T"
	else
		tx_buffer[5] = LCU_NO_TEST;				// LCU_NO_TEST "_"

	if (xlru_lcu::xlru_lcu_command.heater==1)
		tx_buffer[6] = LCU_HEATER_ON;			//"H", heater on
	else
		tx_buffer[6] = LCU_NO_HEATER;			//"_", no heater

	if (xlru_lcu::xlru_lcu_command.restart==1)
		tx_buffer[7] = LCU_RESTART;				//"R", no restart
	else
		tx_buffer[7] = LCU_NO_RESTART;			//"_", no restart

    tx_buffer[8] = STATUS_SPARE;
    tx_buffer[9] = STATUS_SPARE;
	tx_buffer[10] = 0;

	char checksum[2] = {0};
	calc_checksum(checksum, tx_buffer);
	tx_buffer[10] = checksum[0];
	tx_buffer[11] = checksum[1];

	tx_buffer[12]= CR_EOM;
	tx_buffer[13]= 0;
}

char* xlru_lcu::lcu_set_message_master(void)
{
	memset(tx_buffer, 0, sizeof(tx_buffer));
	lcu_header_master();
	lcu_set_message_payload_master();
	return tx_buffer;
}

//****************************************************
// MASTER - from EIF TO LCU - GET MESSAGE

void xlru_lcu::lcu_get_message_payload_master(void)
{
	tx_buffer[4] = LCU_GET_MESSAGE;			//"S", LCU General Status Message
	tx_buffer[5] = 0;

	char checksum[2] = {0};
	calc_checksum(checksum, tx_buffer);
	tx_buffer[5] = checksum[0];
	tx_buffer[6] = checksum[1];

	//snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
	tx_buffer[7]= CR_EOM;
	tx_buffer[8]= 0;
}

char* xlru_lcu::lcu_get_message_master(void)
{
	lcu_header_master();
	lcu_get_message_payload_master();
	return tx_buffer;
}

void xlru_lcu::init(void)
{
	//xlru_lcu::xlru_lcu_status.status.commStatus.commDeviceOk = false;
	//xlru_lcu::xlru_lcu_status.status.commStatus.linkUp = false;
	//xlru_lcu::xlru_lcu_status.status.basicStatus.overtempHazard = false;
	//xlru_lcu::xlru_lcu_status.status.basicStatus.overtempWarning = false;
	//xlru_lcu::xlru_lcu_status.status.basicStatus.unitOk = false;

	xlru_lcu::set_lcu_heater(DEF_HEATER);
	xlru_lcu::set_lcu_ibit(DEF_IBIT);
	xlru_lcu::set_lcu_restart(DEF_RESTART);
}

//***********************************************************************************
void xlru_lcu::lcu_reset_buffer_TX_RX(void)
{
	memset(tx_buffer, 0, sizeof(tx_buffer));
	memset(rx_buffer, 0, sizeof(rx_buffer));
}

char * xlru_lcu::lcu_getStringTx(void)
{
	return tx_buffer;
}

char * xlru_lcu::lcu_getStringRx(void)
{
	return rx_buffer;
}

void xlru_lcu::lcu_setStringTx(char *txt)
{
	strcpy(tx_buffer, txt);
}

void xlru_lcu::lcu_setStringRx(char *txt)
{
	strcpy(rx_buffer, txt);
}

//Decode rx message and fill xlru_lcu_status
//return
//			0		no error
//			1
int xlru_lcu::xlru_lcu_command_message_decoder(char* buffer)
{
	//xlru_lcu::xlru_lcu_status.status.commStatus.commDeviceOk = true;
	//xlru_lcu::xlru_lcu_status.status.commStatus.linkUp = true;

	char tmp = 0;
	int value = 0;
	int error = DECODE_NO_ERROR;
	bool ok = false;

	//check message's len, if it is too short, return error
	int len = strlen(buffer);

	//!3a-t_i_G__001281__		example test status message

	if (len<LCU_STATUS_ETI_INDEX) //LCU_CTRL_BOARD_RELEASE_INDEX)
	{
		error = DECODE_TOO_SHORT;
		return error;	//message is too short
	}

	memset( &xlru_lcu::xlru_lcu_status, 0, sizeof( xlru_lcu::xlru_lcu_status ) );


	//MODE STATUS
	tmp = buffer[LCU_MODE_STATUS_INDEX];
	switch(tmp)
	{
		case LCU_POWER_UP:
			xlru_lcu::xlru_lcu_status.mode_status = LCUPowerUp;
			break;
		case LCU_ON_GO:
			xlru_lcu::xlru_lcu_status.mode_status = LCUOnGo;
			break;
		case LCU_I_BIT:
			xlru_lcu::xlru_lcu_status.mode_status = LCUIBIT;
			break;
		case LCU_RESERVED:
			xlru_lcu::xlru_lcu_status.mode_status = LCUreserved;
			break;
		default:
			error++;	//char not correct
			break;
	}

	//IBIT
	tmp = buffer[LCU_BIT_PROGRESS_INDEX];
	switch(tmp)
	{
		case LCU_I_BIT_NOT_IN_PROGRESS:
			xlru_lcu::xlru_lcu_status.bit_progress = LCUIBITnotInProgress;
			break;
		case LCU_I_BIT_COMPLETED:
			xlru_lcu::xlru_lcu_status.bit_progress = LCUIBITcompelted;
			break;
		case LCU_I_BIT_NEVER_EXECUTED:
			xlru_lcu::xlru_lcu_status.bit_progress = LCUIBITneverExecuted;
			break;
		default:
			error++;	//char not correct
			break;
	}

	//BYPASS
	tmp = buffer[LCU_BYPASS_INDEX];
	switch(tmp)
	{
		case LCU_NOT_ACTIVE:
			xlru_lcu::xlru_lcu_status.byPass_status = LCUByPassNotActive;
			break;
		case LCU_BY_PASS_ACTIVE:
			xlru_lcu::xlru_lcu_status.byPass_status = LCUByPassActive;
			break;
		default:
			error++;	//char not correct
			break;
	}

	//HEATH STATUS
	tmp = buffer[LCU_HEALTH_STATUS_INDEX];
	switch(tmp)
	{
		case LCU_GO:
			xlru_lcu::xlru_lcu_status.health_status = LCUHealthGo;
			break;
		case LCU_WARNING:
			xlru_lcu::xlru_lcu_status.health_status = LCUHealthWarning;
			break;
		case LCU_FAIL:
			xlru_lcu::xlru_lcu_status.health_status = LCUHealthFail;
			break;
		default:
			error++;	//char not correct
			break;
	}

	//HAZARD STATUS
	tmp = buffer[LCU_HAZARD_STATUS_INDEX];
	switch(tmp)
	{
		case LCU_WARNING:
			xlru_lcu::xlru_lcu_status.hazard_status = LCUWarning;
			break;
		case LCU_NO_HAZARD:
			xlru_lcu::xlru_lcu_status.hazard_status = LCUNoHazard;
			break;
		case LCU_HAZARD_1:
			xlru_lcu::xlru_lcu_status.hazard_status = LCUHazard1;
			break;
		case LCU_HAZARD_2:
			xlru_lcu::xlru_lcu_status.hazard_status = LCUHazard2;
			break;
		default:
			error++;	//char not correct
			break;
	}

    //OVERRIDE STATUS
    tmp = buffer[LCU_OVERRIDE_INDEX];
    switch(tmp)
    {
        case LCU_OVERRIDE:
            xlru_lcu::xlru_lcu_status.override_status = LCUOverride;
            break;
        case LCU_NO_OVERRIDE:
            xlru_lcu::xlru_lcu_status.override_status = LCUNoOverride;
            break;
        default:
            error++;	//char not correct
            break;
    }


	if (buffer[4]=='t' || buffer[4]=='T')
	{
        error = 0;
		//LCU_STATUS_ETI_INDEX

		//test status message
		/*char tmp6[7] = { 0 };
		tmp6[0] = buffer[LCU_STATUS_ETI_INDEX];
		tmp6[1] = buffer[LCU_STATUS_ETI_INDEX+1];
		tmp6[2] = buffer[LCU_STATUS_ETI_INDEX+2];
		tmp6[3] = buffer[LCU_STATUS_ETI_INDEX+3];
		tmp6[4] = buffer[LCU_STATUS_ETI_INDEX+4];
		tmp6[5] = buffer[LCU_STATUS_ETI_INDEX+5];

		//valuel = strtol(&tmp6[0], &end, 16);
		*/

		int valuel = 0;
        ok=xlru::hex2u48(&valuel , &buffer[LCU_STATUS_ETI_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

        xlru_lcu::xlru_lcu_status.ETI = valuel;// * LCU_ETI_LSB;
	}
	else
	{
		//LCU_BIT_STATUS

		//BYTES#1 - General and pressure
		ok=xlru::hex2u8(&value , &buffer[LCU_BYTES1_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

		xlru_lcu::xlru_lcu_status.byte1_general_pressure.b1_phase_A_fail = ((value & Bit_PhaseA_failure) == Bit_PhaseA_failure);
		xlru_lcu::xlru_lcu_status.byte1_general_pressure.b2_phase_B_fail = ((value & Bit_PhaseB_failure) == Bit_PhaseB_failure);
		xlru_lcu::xlru_lcu_status.byte1_general_pressure.b3_phase_C_fail = ((value & Bit_PhaseC_failure) == Bit_PhaseC_failure);
		xlru_lcu::xlru_lcu_status.byte1_general_pressure.b4_phase_sequence_fail = ((value & Bit_Phase_Sequence_failure) == Bit_Phase_Sequence_failure);
		xlru_lcu::xlru_lcu_status.byte1_general_pressure.b7_pressure_sensor_fail = ((value & Bit_Pressure_Sensor_fail) == Bit_Pressure_Sensor_fail);

		//BYTES#2 - ctrl_board
		ok=xlru::hex2u8(&value , &buffer[LCU_BYTES2_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

		xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b1_temperature_hazard = ((value & Bit_Temperature_Hazard) == Bit_Temperature_Hazard);
		xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b2_temperature_warning = ((value & Bit_Temperature_Warning) == Bit_Temperature_Warning);
		xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b3_nvm_access_warning = ((value & Bit_NVM_Access_Warning) == Bit_NVM_Access_Warning);
		xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b4_board_error_fail = ((value & Bit_Board_Error_failure) == Bit_Board_Error_failure);
		xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b5_link_fail = ((value & Bit_Link_failure) == Bit_Link_failure);

		//BYTES#3 - motor_pump
		ok=xlru::hex2u8(&value , &buffer[LCU_BYTES3_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

		xlru_lcu::xlru_lcu_status.byte3_motor_pump.b1_motor_speed_sensor_fail = ((value & Bit_motor_speed_sensor_fail) == Bit_motor_speed_sensor_fail);
		xlru_lcu::xlru_lcu_status.byte3_motor_pump.b2_temperature_hazard = ((value & Bit_motor_temperature_hazard2) == Bit_motor_temperature_hazard2);
		xlru_lcu::xlru_lcu_status.byte3_motor_pump.b3_temperature_warning = ((value & Bit_motor_temperature_hazard) == Bit_motor_temperature_hazard);
		xlru_lcu::xlru_lcu_status.byte3_motor_pump.b4_motor_fail = ((value & Bit_motor_failure) == Bit_motor_failure);
		xlru_lcu::xlru_lcu_status.byte3_motor_pump.b8_motor_temperature_sensor_fail = ((value & Bit_motor_temp_sensor_fail) == Bit_motor_temp_sensor_fail);

		//BYTES#4 - sensor_fails
		ok=xlru::hex2u8(&value , &buffer[LCU_BYTES4_INDEX]);
		if (!ok)
			error++;	//problem in decode value;
		xlru_lcu::xlru_lcu_status.byte4_sensor.b1_inlet_temp_sensor_fail_aesa_ts1 = ((value & Bit_inlet_temp_sensor_fail_AESA_TS1) == Bit_inlet_temp_sensor_fail_AESA_TS1);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b2_inlet_temp_sensor_fail_pps_ts2 = ((value & Bit_inlet_temp_sensor_fail_PPS_TS2) == Bit_inlet_temp_sensor_fail_PPS_TS2);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b3_inlet_temp_sensor_fail_combo_ts3 = ((value & Bit_inlet_temp_sensor_fail_Combo_TS3) == Bit_inlet_temp_sensor_fail_Combo_TS3);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b4_outlet_temp_sensor_fail_ts4 = ((value & Bit_outlet_temp_sensor_fail_TS4) == Bit_outlet_temp_sensor_fail_TS4);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b5_inlet_air_temp_sensor_fail_ts5 = ((value & Bit_inlet_air_temp_sensor_fail_TS5) == Bit_inlet_air_temp_sensor_fail_TS5);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b6_outlet_air_temp_sensor_fail_ts6 = ((value & Bit_outlet_air_temp_sensor_fail_TS6) == Bit_outlet_air_temp_sensor_fail_TS6);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b7_inlet_flow_sensor_aesa_branch_fs1 = ((value & Bit_inlet_flow_sensor_AESA_FS1) == Bit_inlet_flow_sensor_AESA_FS1);
		xlru_lcu::xlru_lcu_status.byte4_sensor.b8_inlet_flow_sensor_pps_branch_fs2 = ((value & Bit_inlet_flow_sensor_PPS_FS2) == Bit_inlet_flow_sensor_PPS_FS2);

		//BYTES#5 - general_fails
		ok=xlru::hex2u8(&value , &buffer[LCU_BYTES5_INDEX]);
		if (!ok)
			error++;	//problem in decode value;
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b1_dirty_filter_he_warn = ((value & Bit_Dirty_filter_HE_warning) == Bit_Dirty_filter_HE_warning);
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b2_dirty_aesa_stream_warn1 = ((value & Bit_Dirty_AESA_Stream_warning) == Bit_Dirty_AESA_Stream_warning);
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b3_dirty_aesa_stream_warn2 = ((value & Bit_Dirty_PPS_Stream_warning) == Bit_Dirty_PPS_Stream_warning);
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b5_dirty_filter_he_failure = ((value & Bit_Dirty_filter_HE_failure) == Bit_Dirty_filter_HE_failure);
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b6_dirty_aesa_stream_fail1 = ((value & Bit_Dirty_AESA_stream_failure) == Bit_Dirty_AESA_stream_failure);
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b7_dirty_aesa_stream_fail2 = ((value & Bit_Dirty_PPS_stream_failure) == Bit_Dirty_PPS_stream_failure);
		xlru_lcu::xlru_lcu_status.byte5_general_fails.b8_by_pass_fail = ((value & Bit_By_Pass_failure) == Bit_By_Pass_failure);

		//LCU_CTRL_BOARD_TEMP
		ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.ctrl_board_temp , &buffer[LCU_CTRL_BOARD_TEMP_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

		//LCU_MOTOR_TEMP_INDEX
		ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.motor_temp_mts , &buffer[LCU_MOTOR_TEMP_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

		//LCU_MOTOR_VEL_INDEX
        ok=xlru::hex3s8f(&xlru_lcu::xlru_lcu_status.motor_speed_mfs , &buffer[LCU_MOTOR_VEL_INDEX], LCU_MOTOR_VEL_LSB);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_LIQUID_AESA_TEMP_INDEX
        ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.liquid_inlet_temp_aesa_ts1 , &buffer[LCU_LIQUID_AESA_TEMP_INDEX]);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_LIQUID_PPS_TEMP_INDEX
        ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.liquid_inlet_temp_pps_ts2 , &buffer[LCU_LIQUID_PPS_TEMP_INDEX]);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_LIQUID_PUMP_TEMP_INDEX
        ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.liquid_pump_inlet_temp_ts3 , &buffer[LCU_LIQUID_PUMP_TEMP_INDEX]);
        if (!ok)
           error++;	//problem in decode value;

        //LCU_LIQUID_OUTLET_TEMP_INDEX
        ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.liquid_outlet_temp_ts4 , &buffer[LCU_LIQUID_OUTLET_TEMP_INDEX]);
        if (!ok)
           error++;	//problem in decode value;

        //LCU_AIR_INLET_TEMP_INDEX
        ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.air_inlet_temp_ts5 , &buffer[LCU_AIR_INLET_TEMP_INDEX]);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_AIR_OUTLET_TEMP_INDEX
        ok=xlru::hex2s8(&xlru_lcu::xlru_lcu_status.air_outlet_temp_ts6 , &buffer[LCU_AIR_OUTLET_TEMP_INDEX]);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_LIQUID_AESA_INDEX
        ok=xlru::hex2u8(&xlru_lcu::xlru_lcu_status.liquid_inlet_flow_aesa_fs1 , &buffer[LCU_LIQUID_AESA_INDEX]);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_LIQUID_PPS_INDEX
        ok=xlru::hex2u8(&xlru_lcu::xlru_lcu_status.liquid_inlet_flow_pps_fs2 , &buffer[LCU_LIQUID_PPS_INDEX]);
        if (!ok)
            error++;	//problem in decode value;

        //LCU_LIQUID_OUTLET_PRESSURE_INDEX
        ok=xlru::hex3u8f(&xlru_lcu::xlru_lcu_status.liquid_outlet_pressure_ps , &buffer[LCU_LIQUID_OUTLET_PRESSURE_INDEX], LCU_LIQUID_OUTLET_PRESSURE_LSB);
		if (!ok)
			error++;	//problem in decode value;

		//LCU_CTRL_BOARD_VERSION_INDEX
		ok=xlru::hex2u8(&xlru_lcu::xlru_lcu_status.crtl_board_version, &buffer[LCU_CTRL_BOARD_VERSION_INDEX]);
		if (!ok)
			error++;	//problem in decode value;

		//LCU_CTRL_BOARD_RELEASE_INDEX
		ok=xlru::hex2u8(&xlru_lcu::xlru_lcu_status.crtl_board_release, &buffer[LCU_CTRL_BOARD_RELEASE_INDEX]);
		if (!ok)
			error++;	//problem in decode value;
	}

	xlru_lcu::xlru_lcu_status.status.unitOk=xlru_lcu::xlru_lcu_status.health_status==LCUHealthGo;

	switch(xlru_lcu::xlru_lcu_status.hazard_status)
	{
			case LCUNoHazard:
				xlru_lcu::xlru_lcu_status.status.overtempWarning=false;
				xlru_lcu::xlru_lcu_status.status.overtempHazard=false;
				break;
			case LCUWarning:
				xlru_lcu::xlru_lcu_status.status.overtempWarning=true;
				xlru_lcu::xlru_lcu_status.status.overtempHazard=false;
				break;
			default:
				xlru_lcu::xlru_lcu_status.status.overtempHazard=true;
				xlru_lcu::xlru_lcu_status.status.overtempWarning=true;

			}

	return error;
}

unsigned int xlru_lcu::get_lcu_ibit()
{
	return xlru_lcu::xlru_lcu_command.ibit;
}

unsigned int xlru_lcu::get_lcu_heater()
{
	return xlru_lcu::xlru_lcu_command.heater;
}

unsigned int xlru_lcu::get_lcu_restart()
{
	return xlru_lcu::xlru_lcu_command.restart;
}

void xlru_lcu::set_lcu_ibit(bool enable)
{
	xlru_lcu::xlru_lcu_command.ibit = enable;
}

void xlru_lcu::set_lcu_heater(bool enable)
{
	xlru_lcu::xlru_lcu_command.heater = enable;
}

void xlru_lcu::set_lcu_restart(bool enable)
{
	xlru_lcu::xlru_lcu_command.restart = enable;
}


void xlru_lcu::xlru_display(int (*print_func)(const char*, ...))
{

	print_func("\n     -> Info from LCU <-\n");

	print_func("     %-35s: %d.%d\n","board version.release", xlru_lcu::xlru_lcu_status.crtl_board_version, xlru_lcu_status.crtl_board_release);

	print_func("     %-35s: %c\n","status mode", xlru_lcu::xlru_lcu_status.mode_status);
	print_func("     %-35s: %c\n","bit progress",xlru_lcu::xlru_lcu_status.bit_progress);
	print_func("     %-35s: %c\n","by-pass status",xlru_lcu::xlru_lcu_status.byPass_status);
	print_func("     %-35s: %c\n","health status",xlru_lcu::xlru_lcu_status.health_status);
	print_func("     %-35s: %c\n","hazard status",xlru_lcu::xlru_lcu_status.hazard_status);
    print_func("     %-35s: %c\n","Override status",xlru_lcu::xlru_lcu_status.override_status);

	print_func("     %-35s: %-4d    %-35s: %-4d\n",
			"board temp.",
			xlru_lcu::xlru_lcu_status.ctrl_board_temp,
			"liquid outlet temp TS4",
			xlru_lcu::xlru_lcu_status.liquid_outlet_temp_ts4);

	print_func("     %-35s: %-4d    %-35s: %-4d\n",
			"motor temp. MTS",
			xlru_lcu::xlru_lcu_status.motor_temp_mts,
			"air inlet temp TS5",
			xlru_lcu::xlru_lcu_status.air_inlet_temp_ts5);

	print_func("     %-35s: %-4.1f    %-35s: %-4d\n",
			"motor speed MFS (rpm)",
			xlru_lcu::xlru_lcu_status.motor_speed_mfs,
			"air outlet temp TS6",
			xlru_lcu::xlru_lcu_status.air_outlet_temp_ts6);

	print_func("     %-35s: %-4d    %-35s: %-4d\n",
			"liquid inlet temp aesa TS1",
			xlru_lcu::xlru_lcu_status.liquid_inlet_temp_aesa_ts1,
			"liquid inlet flow aesa FS1 (g/s)",
			xlru_lcu::xlru_lcu_status.liquid_inlet_flow_aesa_fs1);

	print_func("     %-35s: %-4d    %-35s: %-4d\n",
			"liquid inlet temp pps TS2",
			xlru_lcu::xlru_lcu_status.liquid_inlet_temp_pps_ts2,
			"liquid inlet flow pps FS2 (g/s)",
			xlru_lcu::xlru_lcu_status.liquid_inlet_flow_pps_fs2);

	print_func("     %-35s: %-4d    %-35s: %-2.2f\n\n",
			"liquid pump inlet temp TS3",
			xlru_lcu::xlru_lcu_status.liquid_pump_inlet_temp_ts3,
			"liquid outlet pressure PS (psi)",
			xlru_lcu::xlru_lcu_status.liquid_outlet_pressure_ps);

	print_func("\n     -> general/pressure (byte1 status) <-        -> ctrl board (byte2 status) <-\n");

	print_func("bit1 %-35s: %-4d    %-35s: %-4d\n",
			"phase A failure",
			xlru_lcu::xlru_lcu_status.byte1_general_pressure.b1_phase_A_fail,
			"temperature hazard",
			xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b1_temperature_hazard);

	print_func("bit2 %-35s: %-4d    %-35s: %-4d\n",
			"phase B failure",
			xlru_lcu::xlru_lcu_status.byte1_general_pressure.b2_phase_B_fail,
			"temperature warning",
			xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b2_temperature_warning);

	print_func("bit3 %-35s: %-4d    %-35s: %-4d\n",
			"phase C failure",
			xlru_lcu::xlru_lcu_status.byte1_general_pressure.b3_phase_C_fail,
			"nvm access warning",
			xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b3_nvm_access_warning);

	print_func("bit4 %-35s: %-4d    %-35s: %-4d\n",
			"phase sequence failure",
			xlru_lcu::xlru_lcu_status.byte1_general_pressure.b4_phase_sequence_fail,
			"board error failure",
			xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b4_board_error_fail);

	print_func("bit5 %-35s: %-4d    %-35s: %-4d\n",
			"",
			0,
			"link failure",
			xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b5_link_fail);

	print_func("bit6 %-35s: %-4d    \n","",0);
	print_func("bit7 %-35s: %-4d    \n","pressure sensor failure (PS)",xlru_lcu::xlru_lcu_status.byte1_general_pressure.b7_pressure_sensor_fail);
	print_func("bit8 %-35s: %-4d    \n","",0);

	print_func("\n     -> motor pump (byte3 status) <-        	  -> sensor (byte4 status) <-\n");
	print_func("bit1 %-35s: %-4d    %-35s: %-4d\n","motor speed sensor failure",xlru_lcu::xlru_lcu_status.byte3_motor_pump.b1_motor_speed_sensor_fail, "inlet temp sensor fail aesa ts1",xlru_lcu::xlru_lcu_status.byte4_sensor.b1_inlet_temp_sensor_fail_aesa_ts1);
	print_func("bit2 %-35s: %-4d    %-35s: %-4d\n","temperature hazard",xlru_lcu::xlru_lcu_status.byte3_motor_pump.b2_temperature_hazard,"inlet temp sensor fail pps ts2",xlru_lcu::xlru_lcu_status.byte4_sensor.b2_inlet_temp_sensor_fail_pps_ts2);
	print_func("bit3 %-35s: %-4d    %-35s: %-4d\n","temperature warning",xlru_lcu::xlru_lcu_status.byte3_motor_pump.b3_temperature_warning,"inlet temp sensor fail combo ts3",xlru_lcu::xlru_lcu_status.byte4_sensor.b3_inlet_temp_sensor_fail_combo_ts3);
	print_func("bit4 %-35s: %-4d    %-35s: %-4d\n","motor failure",xlru_lcu::xlru_lcu_status.byte3_motor_pump.b4_motor_fail, "outlet temp sensor fail ts4",xlru_lcu::xlru_lcu_status.byte4_sensor.b4_outlet_temp_sensor_fail_ts4);
	print_func("bit5 %-35s: %-4d    %-35s: %-4d\n","", 0, "inlet air temp sensor fail ts5",xlru_lcu::xlru_lcu_status.byte4_sensor.b5_inlet_air_temp_sensor_fail_ts5);
	print_func("bit6 %-35s: %-4d    %-35s: %-4d\n","", 0, "outlet air temp sensor fail ts6",xlru_lcu::xlru_lcu_status.byte4_sensor.b6_outlet_air_temp_sensor_fail_ts6);
	print_func("bit7 %-35s: %-4d    %-35s: %-4d\n","", 0, "inlet flow sensor aesa branch fs1",xlru_lcu::xlru_lcu_status.byte4_sensor.b7_inlet_flow_sensor_aesa_branch_fs1);
	print_func("bit8 %-35s: %-4d    %-35s: %-4d\n","motor temperature sensor failure",xlru_lcu::xlru_lcu_status.byte3_motor_pump.b8_motor_temperature_sensor_fail, "inlet flow sensor pps branch fs2",xlru_lcu::xlru_lcu_status.byte4_sensor.b8_inlet_flow_sensor_pps_branch_fs2);

	print_func("\n     -> general failures (byte5 status) <-\n");
	print_func("bit1 %-35s: %d\n","dirty filter he warn",xlru_lcu::xlru_lcu_status.byte5_general_fails.b1_dirty_filter_he_warn);
	print_func("bit2 %-35s: %d\n","dirty aesa stream warn1",xlru_lcu::xlru_lcu_status.byte5_general_fails.b2_dirty_aesa_stream_warn1);
	print_func("bit3 %-35s: %d\n","dirty aesa stream warn2",xlru_lcu::xlru_lcu_status.byte5_general_fails.b3_dirty_aesa_stream_warn2);
	print_func("bit4 %-35s: %d\n","",0);
	print_func("bit5 %-35s: %d\n","dirty filter he failure",xlru_lcu::xlru_lcu_status.byte5_general_fails.b5_dirty_filter_he_failure);
	print_func("bit6 %-35s: %d\n","dirty aesa stream fail1",xlru_lcu::xlru_lcu_status.byte5_general_fails.b6_dirty_aesa_stream_fail1);
	print_func("bit7 %-35s: %d\n","dirty aesa stream fail2",xlru_lcu::xlru_lcu_status.byte5_general_fails.b7_dirty_aesa_stream_fail2);
	print_func("bit8 %-35s: %d\n","by-pass failure",xlru_lcu::xlru_lcu_status.byte5_general_fails.b8_by_pass_fail);

	print_func("\n");

}

void xlru_lcu::xlru_display_compact(int (*print_func)(const char*, ...))
{

	print_func("\n     -> Info from LCU <-\n");

	print_func("     %-10s: %d.%d     %-10s: %-6.2f  \n",
			"ver.rel", xlru_lcu::xlru_lcu_status.crtl_board_version, xlru_lcu_status.crtl_board_release,
			"ETI", xlru_lcu::xlru_lcu_status.ETI);

	print_func("     %-10s: %c       %-10s: %c\n","status", xlru_lcu::xlru_lcu_status.mode_status, "health", xlru_lcu::xlru_lcu_status.health_status);
	print_func("     %-10s: %c       %-10s: %c\n","bit",xlru_lcu::xlru_lcu_status.bit_progress, "hazard", xlru_lcu::xlru_lcu_status.hazard_status);
    print_func("     %-10s: %c       %-10s: %c\n","by-pass",xlru_lcu::xlru_lcu_status.byPass_status, "override", xlru_lcu::xlru_lcu_status.override_status);

	print_func("     %-10s: %-4d    %-10s: %-4d\n","board temp", xlru_lcu::xlru_lcu_status.ctrl_board_temp, "TS4", xlru_lcu::xlru_lcu_status.liquid_outlet_temp_ts4);
	print_func("     %-10s: %-4d    %-10s: %-4d\n","MTS ",xlru_lcu::xlru_lcu_status.motor_temp_mts,"TS5", xlru_lcu::xlru_lcu_status.air_inlet_temp_ts5);
	print_func("     %-10s: %-4.1f    %-10s: %-4d\n","MFS",xlru_lcu::xlru_lcu_status.motor_speed_mfs,"TS6", xlru_lcu::xlru_lcu_status.air_outlet_temp_ts6);
	print_func("     %-10s: %-4d    %-10s: %-4d\n","TS1",xlru_lcu::xlru_lcu_status.liquid_inlet_temp_aesa_ts1,"FS1", xlru_lcu::xlru_lcu_status.liquid_inlet_flow_aesa_fs1);
	print_func("     %-10s: %-4d    %-10s: %-4d\n","TS2",xlru_lcu::xlru_lcu_status.liquid_inlet_temp_pps_ts2,"FS2", xlru_lcu::xlru_lcu_status.liquid_inlet_flow_pps_fs2);
	print_func("     %-10s: %-4d    %-10s: %-2.2f\n\n","TS3",xlru_lcu::xlru_lcu_status.liquid_pump_inlet_temp_ts3, "PS", xlru_lcu::xlru_lcu_status.liquid_outlet_pressure_ps);


	print_func("\nbit     ->  7 6 5 4 3 2 1 0\n");
	print_func("byte%d   ->  %01d %01d %01d %01d %01d %01d %01d %01d\n",
				1,
				0,
				xlru_lcu::xlru_lcu_status.byte1_general_pressure.b7_pressure_sensor_fail,
				0,
				0,
				xlru_lcu::xlru_lcu_status.byte1_general_pressure.b4_phase_sequence_fail,
				xlru_lcu::xlru_lcu_status.byte1_general_pressure.b3_phase_C_fail,
				xlru_lcu::xlru_lcu_status.byte1_general_pressure.b2_phase_B_fail,
				xlru_lcu::xlru_lcu_status.byte1_general_pressure.b1_phase_A_fail);

	print_func("byte%d   ->  %01d %01d %01d %01d %01d %01d %01d %01d\n",
				2,
				0,
				0,
				0,
				xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b5_link_fail,
				xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b4_board_error_fail,
				xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b3_nvm_access_warning,
				xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b2_temperature_warning,
				xlru_lcu::xlru_lcu_status.byte2_ctrl_board.b1_temperature_hazard
				);

	print_func("byte%d   ->  %01d %01d %01d %01d %01d %01d %01d %01d\n",
				3,
				xlru_lcu::xlru_lcu_status.byte3_motor_pump.b8_motor_temperature_sensor_fail,
				0,
				0,
				0,
				xlru_lcu::xlru_lcu_status.byte3_motor_pump.b4_motor_fail,
				xlru_lcu::xlru_lcu_status.byte3_motor_pump.b3_temperature_warning,
				xlru_lcu::xlru_lcu_status.byte3_motor_pump.b2_temperature_hazard,
				xlru_lcu::xlru_lcu_status.byte3_motor_pump.b1_motor_speed_sensor_fail
				);

	print_func("byte%d   ->  %01d %01d %01d %01d %01d %01d %01d %01d\n",
					4,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b8_inlet_flow_sensor_pps_branch_fs2,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b7_inlet_flow_sensor_aesa_branch_fs1,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b6_outlet_air_temp_sensor_fail_ts6,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b5_inlet_air_temp_sensor_fail_ts5,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b4_outlet_temp_sensor_fail_ts4,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b3_inlet_temp_sensor_fail_combo_ts3,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b2_inlet_temp_sensor_fail_pps_ts2,
					xlru_lcu::xlru_lcu_status.byte4_sensor.b1_inlet_temp_sensor_fail_aesa_ts1
					);

	print_func("byte%d   ->  %01d %01d %01d %01d %01d %01d %01d %01d\n",
					5,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b8_by_pass_fail,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b7_dirty_aesa_stream_fail2,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b6_dirty_aesa_stream_fail1,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b5_dirty_filter_he_failure,
					0,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b3_dirty_aesa_stream_warn2,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b2_dirty_aesa_stream_warn1,
					xlru_lcu::xlru_lcu_status.byte5_general_fails.b1_dirty_filter_he_warn
					);

	print_func("\n");

}

void xlru_lcu::xlru_add_error(int n_error)	//error into last decode response message
{
	xlru_lcu::xlru_counter.last_decode_error += n_error;
}

void xlru_lcu::xlru_add_rx(int n_rx)				//response messages
{
	xlru_lcu::xlru_counter.rx_mess+= n_rx;
}

void xlru_lcu::xlru_add_rx_error(int n_error)		//response messages with error
{
	xlru_lcu::xlru_counter.rx_mess_error += n_error;
}

void xlru_lcu::xlru_add_no_rx(int n_error)	//count messages without response
{
	xlru_lcu::xlru_counter.no_response += n_error;
}

void xlru_lcu::xlru_set_last_error(char *last_error)	//report last error
{
	strcpy(xlru_lcu::xlru_counter.last_error, last_error);
}

void xlru_lcu::xlru_set_last_rx(char *last_rx)		//report last received message
{
	strcpy(xlru_lcu::xlru_counter.last_rx, last_rx);
}
void xlru_lcu::xlru_set_last_tx(char *last_tx)		//report last trasmitted message
{
	strcpy(xlru_lcu::xlru_counter.last_tx, last_tx);
}
char* xlru_lcu::xlru_get_last_rx()
{
	return xlru_lcu::xlru_counter.last_rx;
}
char* xlru_lcu::xlru_get_last_tx()
{
	return xlru_lcu::xlru_counter.last_tx;
}
char* xlru_lcu::xlru_get_last_error()
{
	return xlru_lcu::xlru_counter.last_error;
}

void xlru_lcu::lcu_test_status_payload_master(void)
{
	tx_buffer[4] = LCU_GET_TEST_STATUS;			//"T", LCU test status message
    tx_buffer[5] = STATUS_SPARE;

	char checksum[2] = {0};
	calc_checksum(checksum, tx_buffer);
    tx_buffer[6] = checksum[0];
    tx_buffer[7] = checksum[1];

	//snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[8]= CR_EOM;
    tx_buffer[9]= 0;
}

char* xlru_lcu::lcu_set_test_status_master(void)
{
	memset(tx_buffer, 0, sizeof(tx_buffer));
	lcu_header_master();
	lcu_test_status_payload_master();
	return tx_buffer;
}

void xlru_lcu::lcu_reset_eti_payload_master(void)
{
    tx_buffer[4] = LCU_RESET_ETI;			//"R", reset ETI
    tx_buffer[5] = 0;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[5] = checksum[0];
    tx_buffer[6] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[7]= CR_EOM;
    tx_buffer[8]= 0;
}

char* xlru_lcu::lcu_reset_eti_message_master(void)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));
    lcu_header_master();
    lcu_reset_eti_payload_master();
    return tx_buffer;
}

void xlru_lcu::lcu_active_download_payload_master(void)
{
    tx_buffer[4] = LCU_ACTIVE_DOWNLOAD;			//"D", Active Download
    tx_buffer[5] = 'D';			//"D", Active Download
    tx_buffer[6] = 'W';			//"W, Active Download
    tx_buffer[7] = 'N';			//"L", Active Download
    tx_buffer[8] = 'L';			//"N", Active Download
    tx_buffer[9] = 'L';			//"N", Active Download
    tx_buffer[10] = 'C';			//"N", Active Download
    tx_buffer[11] = 'U';			//"N", Active Download

    tx_buffer[12] = STATUS_SPARE;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[13] = checksum[0];
    tx_buffer[14] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[15]= CR_EOM;
    tx_buffer[16]= 0;
}

char* xlru_lcu::lcu_active_download_message_master(void)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));

    //SOM
    //tx_buffer[0] = SOM;

    //DSTID
    //tx_buffer[1] = DSTID_PPS;

    lcu_header_master();

    lcu_active_download_payload_master();

    return tx_buffer;
}

void xlru_lcu::lcu_sw_status_payload_master(void)
{
    tx_buffer[4] = LCU_SW_STATUS;			//"A", software status
    tx_buffer[5] = 0;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[5] = checksum[0];
    tx_buffer[6] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[7]= CR_EOM;
    tx_buffer[8]= 0;
}

char* xlru_lcu::lcu_sw_status_message_master(void)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));
    lcu_header_master();
    lcu_sw_status_payload_master();
    return tx_buffer;
}


void xlru_lcu::lcu_override_set_value_payload(lcu_override_param_id_t _param_id,int _value)
{
    int idx = 4;
    tx_buffer[idx++] = LCU_CMD_OVERRIDE;			//"X", override

    tx_buffer[idx++] = LCU_CMD_OVERRIDE_SET_VALUE;  //S, set value

    char tmp[3] = { 0 };

    snprintf(tmp, 3, "%02X", _param_id);

    tx_buffer[idx++] = tmp[0];
    tx_buffer[idx++] = tmp[1];

    char tmp1[5] = { 0 };

    snprintf(tmp1, 5, "%04X", _value);

    tx_buffer[idx++] = tmp1[0];
    tx_buffer[idx++] = tmp1[1];
    tx_buffer[idx++] = tmp1[2];
    tx_buffer[idx++] = tmp1[3];

/*#define LCU_CMD_OVERRIDE            'X'
#define LCU_CMD_OVERRIDE_SET_VALUE  'S'
#define LCU_CMD_OVERRIDE_RST_FACT   'R'
#define LCU_CMD_OVERRIDE_SET_VALUE  'S'
#define LCU_CMD_OVERRIDE_GET_FACT   'g'*/


    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[idx++] = checksum[0];
    tx_buffer[idx++] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[idx++]= CR_EOM;
    tx_buffer[idx++]= 0;
}

char* xlru_lcu::lcu_override_set_value_message(lcu_override_param_id_t _param_id,int _value)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));
    lcu_header_master();
    lcu_override_set_value_payload(_param_id, _value);
    return tx_buffer;
}


void xlru_lcu::lcu_override_get_value_payload(lcu_override_param_id_t _param_id)
{
    int idx = 4;
    tx_buffer[idx++] = LCU_CMD_OVERRIDE;			//"X", override

    tx_buffer[idx++] = LCU_CMD_OVERRIDE_GET_VALUE;  //S, set value

    char tmp[3] = { 0 };

    snprintf(tmp, 3, "%02X", _param_id);

    tx_buffer[idx++] = tmp[0];
    tx_buffer[idx++] = tmp[1];

/*#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'*/


    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[idx++] = checksum[0];
    tx_buffer[idx++] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[idx++]= CR_EOM;
    tx_buffer[idx++]= 0;
}

char* xlru_lcu::lcu_override_get_value_message(lcu_override_param_id_t _param_id)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));
    lcu_header_master();
    lcu_override_get_value_payload(_param_id);
    return tx_buffer;
}

void xlru_lcu::lcu_override_get_factory_payload(lcu_override_param_id_t _param_id)
{
    int idx = 4;
    tx_buffer[idx++] = LCU_CMD_OVERRIDE;			//"X", override

    tx_buffer[idx++] = LCU_CMD_OVERRIDE_GET_FACT;  //f, get factory

    char tmp[3] = { 0 };

    snprintf(tmp, 3, "%02X", _param_id);

    tx_buffer[idx++] = tmp[0];
    tx_buffer[idx++] = tmp[1];


    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[idx++] = checksum[0];
    tx_buffer[idx++] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[idx++]= CR_EOM;
    tx_buffer[idx++]= 0;
}

char* xlru_lcu::lcu_override_get_factory_message(lcu_override_param_id_t _param_id)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));
    lcu_header_master();
    lcu_override_get_factory_payload(_param_id);
    return tx_buffer;
}

void xlru_lcu::lcu_sw_set_to_factory_payload(lcu_override_param_id_t _param_id)
{
    int idx = 4;
    tx_buffer[idx++] = LCU_CMD_OVERRIDE;			//"X", override

    tx_buffer[idx++] = LCU_CMD_OVERRIDE_RST_FACT;  //R, reset to factory

    char tmp[3] = { 0 };

    snprintf(tmp, 3, "%02X", _param_id);

    tx_buffer[idx++] = tmp[0];
    tx_buffer[idx++] = tmp[1];

    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;
    tx_buffer[idx++] = STATUS_SPARE;

    char checksum[2] = {0};
    calc_checksum(checksum, tx_buffer);
    tx_buffer[idx++] = checksum[0];
    tx_buffer[idx++] = checksum[1];

    //snprintf(&tx_buffer[5], 3, "%02x", get_checksum(tx_buffer));
    tx_buffer[idx++]= CR_EOM;
    tx_buffer[idx++]= 0;
}

char* xlru_lcu::lcu_override_set_to_factory_message(lcu_override_param_id_t _param_id)
{
    memset(tx_buffer, 0, sizeof(tx_buffer));
    lcu_header_master();
    lcu_sw_set_to_factory_payload(_param_id);
    return tx_buffer;
}




