/*
 * @Author       : ChenJie
 * @Date         : 2021-12-20 14:47:39
 * @Version      : V3.0
 * @LastEditors  : ChenJie
 * @LastEditTime : 2022-05-09 17:13:09
 * @Description  : file content
 * @FilePath     : \VehicleControl\VehicleControl\src\System\Vehicle\Communication\CanProtocolFunc.c
 */
/*
 * CanProtocol.c
 * can协议转换
 *  Created on: 2021年12月20日
 *      Author: QiXiang_CHENJIE
 */
#include <CanProtocolFunc.h>
#include "HardwareLib.h"
/******************************************
 *
 *Can0接收发送函数
 **************************************** */
void Can0RxMsgSetInvalidValue(UINT32 ID);

void Can0Encode(UINT32 ID, UINT8 *rawData)
{

}
void Can0Decode(UINT32 ID, UINT8 *rawData)
{
	switch(ID)
	{
	case 0x18FF5A51:
	{
		gashighLevel_gf1 = (UINT8)(((*(UINT8 *)(rawData + 0))) & 0xFF);
		gaslowLevel_gf1 = (UINT8)(((*(UINT8 *)(rawData + 1))) & 0xFF);
		temp_gf1 = (UINT8)(((*(UINT8 *)(rawData + 2))) & 0xFF);
		warningLevel_gf1 = (UINT8)(((*(UINT8 *)(rawData + 4))) & 0xFF);
		workMode_gf1 = (UINT8)(((*(UINT8 *)(rawData + 5))) & 0xFF);
		fumesHigh_gf1 = (UINT8)(((*(UINT8 *)(rawData + 6))) & 0xFF);
		fumesLow_gf1 = (UINT8)(((*(UINT8 *)(rawData + 7))) & 0xFF);
		Can0RxFlag[0] = 0x01;
		break;
	}
	case 0x18FF5A52:
	{
		gashighLevel_gf2 = (UINT8)(((*(UINT8 *)(rawData + 0))) & 0xFF);
		gaslowLevel_gf2 = (UINT8)(((*(UINT8 *)(rawData + 1))) & 0xFF);
		temp_gf2 = (UINT8)(((*(UINT8 *)(rawData + 2))) & 0xFF);
		warningLevel_gf2 = (UINT8)(((*(UINT8 *)(rawData + 4))) & 0xFF);
		workMode_gf2 = (UINT8)(((*(UINT8 *)(rawData + 5))) & 0xFF);
		fumesHigh_gf2 = (UINT8)(((*(UINT8 *)(rawData + 6))) & 0xFF);
		fumesLow_gf2 = (UINT8)(((*(UINT8 *)(rawData + 7))) & 0xFF);
		Can0RxFlag[1] = 0x01;
		break;
	}
	default:
	{
		break;
	}
	}
}
void Can0RxDiagnose(void)
{
	static UINT32 TimeCounter = 0;	// ms
	TimeCounter = TimeCounter + 10; //
	for (UINT8 i = 0; i < sizeof(Can0InTable) / 4; i++)
	{
		if (Can0RxFlag[i] == 1) // received msg, and clear the error conter
		{
			Can0RxErrorCounter[i] = 0;
			Can0RxFlag[i] = 0;
			continue;
		}
		if (Can0RxErrorCounter[i] < 3)
		{
			Can0RxShortError[i] = 0;
			Can0RxLongError[i] = 0;
		}
		else if (Can0RxErrorCounter[i] >= 3 && Can0RxErrorCounter[i] < 50)
		{
			Can0RxShortError[i] = 1;
			Can0RxLongError[i] = 0;
		}
		else if (Can0RxErrorCounter[i] >= 50)
		{
			Can0RxShortError[i] = 0;
			Can0RxLongError[i] = 1;
			Can0RxMsgSetInvalidValue(Can0InTable[i]);
			Can0RxErrorCounter[i] = 0;
		}
		else if (Can0RxErrorCounter[i] == 0xFF)
		{
			Can0RxErrorCounter[i] = 0xFE;
		}
	}
	if (TimeCounter % 100 == 0) // period <=100ms
	{
		Can0RxErrorCounter[0]++;
	}
}
void Can0RxMsgSetInvalidValue(UINT32 ID)
{
	switch (ID)
	{
	case 0x18FF5A51:
	{
		gashighLevel_gf1 = 0xFF;
		gaslowLevel_gf1 = 0xFF;
		temp_gf1 = 0xFF;
		warningLevel_gf1 = 0xFF;
		workMode_gf1 = 0xFF;
		fumesHigh_gf1 = 0xFF;
		fumesLow_gf1 = 0xFF;
		break;
	}
	case 0x18FF5A52:
	{
		gashighLevel_gf2 = 0xFF;
		gaslowLevel_gf2 = 0xFF;
		temp_gf2 = 0xFF;
		warningLevel_gf2 = 0xFF;
		workMode_gf2 = 0xFF;
		fumesHigh_gf2 = 0xFF;
		fumesLow_gf2 = 0xFF;
		break;
	}
	default:
		break;
	}
}

/******************************************
 *
 *Can1接收发送函数
 **************************************** */
/**
 * @brief : BMC发送
 * @param {uint8} CmdIdx
 * @return {*}
 */
void Can1RxMsgSetInvalidValue(UINT32 ID);
void Can1Encode(UINT32 ID, UINT8 *rawData)
{
	switch(ID)
	{
	case 0x18FF1100:
	{
		*(rawData + 0) = connectorHvlock1;
		*(rawData + 1) = connectorHvlock2;
		*(rawData + 2) = chrgModuelPwrCtlFB;
		*(rawData + 3) = paraChrgCtlFB;
		*(rawData + 4) = outToChrgCtlFB_A;
		*(rawData + 5) = outToBaseCtlFB_A;
		*(rawData + 6) = outToChrgCtlFB_B;
		*(rawData + 7) = outToBaseCtlFB_B;
		break;
	}
	case 0x18FF1101:
	{
		*(rawData + 0) = fuseActionFB_A;
		*(rawData + 1) = fuseActionFB_B;
		*(rawData + 2) = sigSwitchReadyFB_A;
		*(rawData + 3) = sigSwitchReadyFB_B;
		*(rawData + 4) = ctrlFXFB1;
		*(rawData + 5) = ctrlFXFB2;
		*(rawData + 6) = 0xFF;
		*(rawData + 7) = 0xFF;
		break;
	}
	case 0x18FF1102:
	{
		*(rawData + 0) = conn1Temp1;
		*(rawData + 1) = conn1Temp2;
		*(rawData + 2) = conn2Temp1;
		*(rawData + 3) = conn2Temp2;
		*(rawData + 4) = chrgGunTemp1_A;
		*(rawData + 5) = chrgGunTemp2_A;
		*(rawData + 6) = chrgGunTemp1_B;
		*(rawData + 7) = chrgGunTemp2_B;
		break;
	}
	case 0x18FF1103:
	{
		*(rawData + 0) = chrgBoxTemp1;
		*(rawData + 1) = chrgBoxTemp2;
		*(rawData + 2) = chrgBoxGasOver1;
		*(rawData + 3) = chrgBoxGasOver2;
		*(rawData + 4) = chrgBoxFumesOver1;
		*(rawData + 5) = chrgBoxFumesOver2;
		*(rawData + 6) = chrgBox1Warning;
		*(rawData + 7) = chrgBox2Warning;
		break;
	}
	default:
		break;
	}
}
/**
 * @brief : BMC解析
 * @param {*}
 * @return {*}
 */
void Can1Decode(UINT32 ID, UINT8 *rawData)
{
	switch(ID)
	{
	case 0x18FF1200:
	{
		chrgModulePwrCtrl = (UINT8)(((*(UINT8 *)(rawData + 0))) & 0xFF);
		baseSwitchCtrl = (UINT8)(((*(UINT8 *)(rawData + 1))) & 0xFF);
		paraChrgCtrlState = (UINT8)(((*(UINT8 *)(rawData + 2))) & 0xFF);
		chrgStartState = (UINT8)(((*(UINT8 *)(rawData + 3))) & 0xFF);
		chrgStopState = (UINT8)(((*(UINT8 *)(rawData + 4))) & 0xFF);
		Can1RxFlag[0] = 0x01;
		break;
	}
	default:
	{
		break;
	}
	}
}
void Can1RxDiagnose(void)
{
	static UINT32 TimeCounter = 0;	// ms
	TimeCounter = TimeCounter + 10; //
	for (UINT8 i = 0; i < sizeof(Can1InTable) / 4; i++)
	{
		if (Can1RxFlag[i] == 1) // received msg, and clear the error conter
		{
			Can1RxErrorCounter[i] = 0;
			Can1RxFlag[i] = 0;
			continue;
		}
		if (Can1RxErrorCounter[i] < 3)
		{
			Can1RxShortError[i] = 0;
			Can1RxLongError[i] = 0;
		}
		else if (Can1RxErrorCounter[i] >= 3 && Can1RxErrorCounter[i] < 50)
		{
			Can1RxShortError[i] = 1;
			Can1RxLongError[i] = 0;
		}
		else if (Can1RxErrorCounter[i] >= 50)
		{
			Can1RxShortError[i] = 0;
			Can1RxLongError[i] = 1;
			Can1RxMsgSetInvalidValue(Can1InTable[i]);
			Can1RxErrorCounter[i] = 0;
		}
		else if (Can1RxErrorCounter[i] == 0xFF)
		{
			Can1RxErrorCounter[i] = 0xFE;
		}
	}
	if (TimeCounter % 100 == 0) // period <=100ms
	{
		Can1RxErrorCounter[0]++;
	}
}
void Can1RxMsgSetInvalidValue(UINT32 ID)
{
	switch (ID)
	{
	case 0x18FF1200:
		chrgModulePwrCtrl = 0xFF;
		baseSwitchCtrl = 0xFF;
		paraChrgCtrlState = 0xFF;
		chrgStartState = 0xFF;
		chrgStopState = 0xFF;
		break;
	default:
		break;
	}
}
