/*
 * @Author       : ChenJie
 * @Date         : 2021-12-24 15:26:57
 * @Version      : V3.0
 * @LastEditors  : ChenJie
 * @LastEditTime : 2022-05-09 16:13:46
 * @Description  : file content
 * @FilePath     : \VehicleControl\VehicleControl\src\System\Vehicle\Communication\VcuCanProtocolFunc.c
 */
#include "VcuCanProtocolFunc.h"
void VcuDecodeFunction(UINT32 ID, UINT8 *rawData)
{
	switch (ID)
	{
	case 0x18F101D0:
		VCU_St_BattRly = (UINT8)(((*(UINT8 *)(rawData + 0))) & 0x3);
		VCU_St_Check = (UINT8)(((*(UINT8 *)(rawData + 0)) >> 2) & 0x3);
		VCU_St_SysIntLock = (UINT8)(((*(UINT8 *)(rawData + 0)) >> 4) & 0x3);
		VCU_St_Veh = (UINT8)(((*(UINT8 *)(rawData + 0)) >> 6) & 0x3);
		VCU_St_Gear = (UINT8)(((*(UINT8 *)(rawData + 3))) & 0xFF);
		VCU_Ft_Sys = (UINT8)(((*(UINT8 *)(rawData + 4))) & 0x1);
		VCU_Ft_Tran = (UINT8)(((*(UINT8 *)(rawData + 4)) >> 1) & 0x1);
		VCU_Ft_BattSys = (UINT8)(((*(UINT8 *)(rawData + 4)) >> 5) & 0x1);
		VCU_State_Veh = (UINT8)(((*(UINT8 *)(rawData + 6))) & 0xF);
		VCU_Counter = (UINT8)(((*(UINT8 *)(rawData + 7))) & 0xFF);
		VcuRxFlag[0] = 0x01;
		break;
	case 0x18F103D0:
		VCU_SoftVersion = (UINT8)(((*(UINT8 *)(rawData + 0))) & 0xFF);
		VCU_St_AccPedal = (UINT8)(((*(UINT8 *)(rawData + 3))) & 0xFF);
		VCU_St_BrakePedal = (UINT8)(((*(UINT8 *)(rawData + 4))) & 0xFF);
		VcuRxFlag[1] = 0x01;
		break;
	case 0x18E1F3D0:
	{
		UINT8 VinNum = 0;
		VinNum = (UINT8)(((*(UINT8 *)(rawData + 0))) & 0xFF);
		if (VinNum == 0 || VinNum > 3)
		{
			break;
		}
		VCU_St_VinRecv = ((VinNum == 1) | ((VinNum == 2) << 1) | ((VinNum == 3) << 2)) | VCU_St_VinRecv;
		for (UINT8 i = 0; i < 7; i++)
		{
			if(((VinNum - 1) * 7 + i) < 17)
			{
				VCU_Num_VIN[(VinNum - 1) * 7 + i] = (UINT8)(((*(UINT8 *)(rawData + i))) & 0xFF);;
			}
		}
		VcuRxFlag[2] = 0x01;
		break;
	}
	case 0x18FEF100:
		VCU_St_ParkBrakeSw = (UINT8)(((*(UINT8 *)(rawData + 0)) >> 2) & 0x3);
		VcuRxFlag[3] = 0x01;
		break;
	default:
		break;
	}
}
void VcuRxDiagnose(void)
{
	static UINT16 TimeCounter = 0;
	TimeCounter = TimeCounter + 10;
	for (UINT8 i = 0; i < sizeof(VcuInTable) / 4; i++)
	{
		if (VcuRxFlag[i] == 1) // received msg, and clear the error conter
		{
			VcuRxErrorCounter[i] = 0;
			VcuRxFlag[i] = 0;
			continue;
		}
		if (VcuRxErrorCounter[i] < 3)
		{
			VcuRxShortError[i] = 0;
			VcuRxLongError[i] = 0;
		}
		else if (VcuRxErrorCounter[i] >= 3 && VcuRxErrorCounter[i] < 13)
		{
			VcuRxShortError[i] = 1;
			VcuRxLongError[i] = 0;
		}
		else if (VcuRxErrorCounter[i] >= 13)
		{
			VcuRxShortError[i] = 0;
			VcuRxLongError[i] = 1;
			VcuRxMsgSetInvalidValue(VcuInTable[i]);
			VcuRxErrorCounter[i] = 0;
		}
		else if (VcuRxErrorCounter[i] == 0xFF)
		{
			VcuRxErrorCounter[i] = 0xFE;
		}
	}
	if (TimeCounter % 100 == 0) // period <=100ms
	{
		VcuRxErrorCounter[0]++; // can msg 0x18F101D0
		// if(canRxErrorCounter[0]>=13)
		VcuRxErrorCounter[1]++; // can msg 0x18F103D0
								// if(canRxErrorCounter[1]>=13)
	}
}
void VcuRxMsgSetInvalidValue(UINT32 ID)
{
	switch (ID)
	{
	case 0x18F101D0:
		VCU_St_BattRly = 0xFF;
		VCU_St_Check = 0xFF;
		VCU_St_SysIntLock = 0xFF;
		VCU_St_Veh = 0xFF;
		VCU_St_Gear = 0xFF;
		VCU_Ft_Sys = 0xFF;
		VCU_Ft_Tran = 0xFF;
		VCU_Ft_BattSys = 0xFF;
		VCU_State_Veh = 0xFF;
		VCU_Counter = 0xFF;
		break;
	case 0x18F103D0:
		VCU_SoftVersion = 0xFF;
		VCU_St_AccPedal = 0xFF;
		VCU_St_BrakePedal = 0xFF;
		break;
	case 0x18FEF100:
		break;
	default:
		break;
	}
}
