#include "common_include.h"

uint8_t Flag_SaveDate = 0;

void UDS_Backdoor()
{
    if ((Timer10msCnts > SendCanMessage10msTimer) && ((Timer10msCnts - SendCanMessage10msTimer) >= 10)) {        
        //10ms 
        SendCanMessage10msTimer = Timer10msCnts; 
        Speed120();
        InitBuf();
#if (CAN_2ND)        
        CanSendCan0Msg(0x386,  WHL_SPD11, 8,1);
#endif        
#if (CAN_3RD)        
        CanSendCan0Msg(0x0A0,   WHL_01_10ms, 24, 1);
#endif
    }
}

void UDS_Unlock(void)//д��
{
		switch (Key27Step) 
		{
		case 0:
			RxFinish = UNFINISH;
			EOL_UDS_10();
			Delay(50);//50ms
#if ((PRO_SQFL) || (PRO_CN7C))
            EOL_UDS_27(0x01);
#else
            EOL_UDS_27(0x03); 
		
#endif
			Key27Step = 1;
			break;
       case 1:      
            if (RxFinish == FINISH)
            {
				//�����㷨 
#if (PRO_SQFL)
                Delay(50);//50ms 
                EOL_UDS_Key27Step_1_2(0x02);
#elif(PRO_CN7C)
                EOL_UDS_Key27Step_1_1(0x02);
#else
				EOL_UDS_Key27Step_1_1(0x04);
#endif
				Key27Step = 2;
			}
			break;
		case 2:
			if (Receive_7CE_Data.data[0] == 0x30) {
				Delay(5);//5ms
				EOL_UDS_Key27Step_2_1();
				Key27Step = 3;
			}
			break;
		}
}

void UDS_F18F_Response(void)
{
   if (RxFinish == UNFINISH) {
       MultiplePack.Data [0] = 0x11;
       MultiplePack.Data [1] = 0x22;
       MultiplePack.Data [2] = 0x33;
       MultiplePack.Data [3] = 0x44;
       MultiplePack.Data [4] = 0x55;
       MultiplePack.Data [5] = 0x66;
       MultiplePack.Data [6] = 0x77;
       MultiplePack.Data [7] = 0x88;
       RxFinish = FINISH;
   }
}
void UDS_2E_F18C_F187(void)
{
	uint8_t F18CSendBuf[22] = {0};
	uint8_t F187SendBuf[16] = {0};
	uint8_t tmpF187SendBuf[16] = {0};
	uint16_t Crc16 = 0;
    uint32_t tmp = 0;
    uint32_t tmpID = 0;
	
	F18CSendBuf[0] = 0x32; 
	F18CSendBuf[1] = 0x30;
	F18CSendBuf[2] = (tmpBarcodeDate / 100000) + 0x30;
	F18CSendBuf[3] = (tmpBarcodeDate / 10000) % 10 + 0x30;
	F18CSendBuf[4] = (tmpBarcodeDate / 1000) % 10 + 0x30;
	F18CSendBuf[5] = (tmpBarcodeDate / 100) % 10 + 0x30;
	F18CSendBuf[6] = (tmpBarcodeDate / 10) % 10 + 0x30;
	F18CSendBuf[7] = tmpBarcodeDate % 10 + 0x30;  
	F18CSendBuf[8] = tmpClusterID / 1000 % 10 + 0x30;
	F18CSendBuf[9] = tmpClusterID / 100 % 10 + 0x30;
	F18CSendBuf[10] = tmpClusterID / 10 % 10 + 0x30;
	F18CSendBuf[11] = tmpClusterID % 10 + 0x30;
	F18CSendBuf[12] = 0x00;
	F18CSendBuf[13] = 0x00;
	F18CSendBuf[14] = 0x00;  
	F18CSendBuf[15] = 0x00; 
	F18CSendBuf[16] = 0x00;
	F18CSendBuf[17] = 0x00;
	F18CSendBuf[18] = 0x00;  
	F18CSendBuf[19] = 0x00;
	*(uint16_t *)&F18CSendBuf[20] = CRC16_cal(F18CSendBuf, 20);
	
	memcpy(tmpF187SendBuf, tmpPartNum_AssemblyWorkshop, 10);
	*(uint16_t *)&tmpF187SendBuf[14] = CRC16_cal(tmpF187SendBuf, 14);
	DataEncrypt(tmpF187SendBuf, 16u, F187SendBuf);
	UDS_Backdoor();
	if(Flag_SaveDate == 1)
	{
		CANSendEnable = OFF; //�ر��������ķ���
		//APP_EOL();   		
		UDS_Unlock();
#if ((PRO_SQFL) || (PRO_CN7C))
        if((Receive_7CE_Data.data[1] == 0x67) && (Receive_7CE_Data.data[2]== 0x02))
#else
        if((Receive_7CE_Data.data[1] == 0x67) && (Receive_7CE_Data.data[2]== 0x12))
#endif
		{
			Flag_SaveDate = 2;  
			Write2EStep = 0;
			Can_Clear((uint8_t*)&Receive_7CE_Data.data,sizeof(Receive_7CE_Data));
			memset(ReceiveData, 0, sizeof(ReceiveData));
			memset(UdsKeyData, 0, sizeof(UdsKeyData));
			memset(Key27Buffer, 0, sizeof(Key27Buffer));
		}
	}
	else if(Flag_SaveDate == 2)
	{
		switch(Write2EStep)
		{
		case 0:
			Send_7C6_Data.msg.bits.Byte0Data = 0x10;
			Send_7C6_Data.msg.bits.Byte1Data = 0x19;  
			Send_7C6_Data.msg.bits.Byte2Data = 0x2E;
			Send_7C6_Data.msg.bits.Byte3Data = 0xF1;
			Send_7C6_Data.msg.bits.Byte4Data = 0x8C;  
			Send_7C6_Data.msg.bits.Byte5Data = F18CSendBuf[0]; 
			Send_7C6_Data.msg.bits.Byte6Data = F18CSendBuf[1];
			Send_7C6_Data.msg.bits.Byte7Data = F18CSendBuf[2];
			CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_21, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 8);
			Delay(20);//20ms  
			Write2EStep = 1;
			break;
		case 1:
			if (Receive_7CE_Data.data[0] == 0x30) {
				Delay(5);//5ms
				Send_7C6_Data.msg.bits.Byte0Data = 0x21;
				Send_7C6_Data.msg.bits.Byte1Data = F18CSendBuf[3];
				Send_7C6_Data.msg.bits.Byte2Data = F18CSendBuf[4];
				Send_7C6_Data.msg.bits.Byte3Data = F18CSendBuf[5];
				Send_7C6_Data.msg.bits.Byte4Data = F18CSendBuf[6];
				Send_7C6_Data.msg.bits.Byte5Data = F18CSendBuf[7];
				Send_7C6_Data.msg.bits.Byte6Data = F18CSendBuf[8];
				Send_7C6_Data.msg.bits.Byte7Data = F18CSendBuf[9];
				CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_8, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 8); 
				Delay(1);//1ms
				Send_7C6_Data.msg.bits.Byte0Data = 0x22;
				Send_7C6_Data.msg.bits.Byte1Data = F18CSendBuf[10];
				Send_7C6_Data.msg.bits.Byte2Data = F18CSendBuf[11];  
				Send_7C6_Data.msg.bits.Byte3Data = F18CSendBuf[12];
				Send_7C6_Data.msg.bits.Byte4Data = F18CSendBuf[13];
				Send_7C6_Data.msg.bits.Byte5Data = F18CSendBuf[14];
				Send_7C6_Data.msg.bits.Byte6Data = F18CSendBuf[15];
				Send_7C6_Data.msg.bits.Byte7Data = F18CSendBuf[16];
				CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_8, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 8);
				Delay(1);//1ms
				Send_7C6_Data.msg.bits.Byte0Data = 0x23;
				Send_7C6_Data.msg.bits.Byte1Data = F18CSendBuf[17];
				Send_7C6_Data.msg.bits.Byte2Data = F18CSendBuf[18];  
				Send_7C6_Data.msg.bits.Byte3Data = F18CSendBuf[19];
				Send_7C6_Data.msg.bits.Byte4Data = F18CSendBuf[20];
				Send_7C6_Data.msg.bits.Byte5Data = F18CSendBuf[21]; 
				CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_8, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 6);
				Write2EStep = 2;
			}
			break;
		case 2:
			Write2EStep = 0;
			Flag_SaveDate = 3; 
			break;
		}
	}
	else if(Flag_SaveDate == 3)
	{
		if((Receive_7CE_Data.data[1] == 0x6E) && (Receive_7CE_Data.data[2] == 0xF1) && (Receive_7CE_Data.data[3] == 0x8C))//�жϷ���
		{
			Flag_SaveDate = 4;
			Key27Step = 0;
			Can_Clear((uint8_t*)&Receive_7CE_Data.data,sizeof(Receive_7CE_Data));
		}
		else if((Receive_7CE_Data.data[1] == 0x7F) && (Receive_7CE_Data.data[2] == 0x2E))//�жϷ���
		{
			Flag_SaveDate = 0;
			Key27Step = 0;
			SetClusterState = 4;
			Can_Clear((uint8_t*)&Receive_7CE_Data.data,sizeof(Receive_7CE_Data));
		}
	}
	else if(Flag_SaveDate == 4)
	{
		Flag_SaveDate = 5;
		memset(&MultiplePack, 0, sizeof (MultiplePack));
		MyRecvCount = 0;
		
		Send_7CE_Data.Byte0Data = 0x03;
		Send_7CE_Data.Byte1Data = 0x22;
		Send_7CE_Data.Byte2Data = 0xF1;
		Send_7CE_Data.Byte3Data = 0x8C;
		CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_27, TX_ISR_EN, (uint8_t*)&Send_7CE_Data.mdata, 4);
		RxFinish = UNFINISH;
	}
	else if(Flag_SaveDate == 5)
	{
		if(RxFinish == FINISH)
		{
			tmp = (MultiplePack.Data[2] - 0x30) * 100000;
			tmp += (MultiplePack.Data[3] - 0x30) * 10000;
			tmp += (MultiplePack.Data[4] - 0x30) * 1000;
			tmp += (MultiplePack.Data[5] - 0x30) * 100;
			tmp += (MultiplePack.Data[6] - 0x30) * 10;
			tmp += (MultiplePack.Data[7] - 0x30) * 1;
                        
                        tmpID = (MultiplePack.Data[8] - 0x30) * 1000;
			tmpID += (MultiplePack.Data[9] - 0x30) * 100;
			tmpID += (MultiplePack.Data[10] - 0x30) * 10;
			tmpID += (MultiplePack.Data[11] - 0x30) * 1;
			
			if((tmp == tmpBarcodeDate) && (tmpID == (tmpClusterID % 10000)))
			{
				Flag_SaveDate = 6;
				Write2EStep = 0;
				Key27Step = 0;
			}
			else
			{
				Flag_SaveDate = 0;
				SetClusterState = 4;
				Key27Step = 0;
			}
		}
	}
	else if(Flag_SaveDate == 6)
	{
		CANSendEnable = OFF; //�ر��������ķ���
		//APP_EOL();
        UDS_Unlock();
#if ((PRO_SQFL) || (PRO_CN7C))   		
		if((Receive_7CE_Data.data[1] == 0x67) && (Receive_7CE_Data.data[2]== 0x02))
#else
        if((Receive_7CE_Data.data[1] == 0x67) && (Receive_7CE_Data.data[2]== 0x12))
#endif
		{
			Flag_SaveDate = 7;  
			Write2EStep = 0;
			Can_Clear((uint8_t*)&Receive_7CE_Data.data,sizeof(Receive_7CE_Data));
			memset(ReceiveData, 0, sizeof(ReceiveData));
			memset(UdsKeyData, 0, sizeof(UdsKeyData));
			memset(Key27Buffer, 0, sizeof(Key27Buffer));
		}
	}
	else if(Flag_SaveDate == 7)
	{
		Delay(50);
		switch(Write2EStep)
		{
		case 0:
			Send_7C6_Data.msg.bits.Byte0Data = 0x10;
			Send_7C6_Data.msg.bits.Byte1Data = 0x13;  
			Send_7C6_Data.msg.bits.Byte2Data = 0x2E;
			Send_7C6_Data.msg.bits.Byte3Data = 0xF1;
			Send_7C6_Data.msg.bits.Byte4Data = 0x87;  
			Send_7C6_Data.msg.bits.Byte5Data = F187SendBuf[0]; 
			Send_7C6_Data.msg.bits.Byte6Data = F187SendBuf[1];
			Send_7C6_Data.msg.bits.Byte7Data = F187SendBuf[2];
			CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_21, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 8);
			Delay(20);//20ms  
			Write2EStep = 1;
			break;
		case 1:
			if (Receive_7CE_Data.data[0] == 0x30) {
				Delay(5);//5ms
				Send_7C6_Data.msg.bits.Byte0Data = 0x21;
				Send_7C6_Data.msg.bits.Byte1Data = F187SendBuf[3];
				Send_7C6_Data.msg.bits.Byte2Data = F187SendBuf[4];
				Send_7C6_Data.msg.bits.Byte3Data = F187SendBuf[5];
				Send_7C6_Data.msg.bits.Byte4Data = F187SendBuf[6];
				Send_7C6_Data.msg.bits.Byte5Data = F187SendBuf[7];
				Send_7C6_Data.msg.bits.Byte6Data = F187SendBuf[8];
				Send_7C6_Data.msg.bits.Byte7Data = F187SendBuf[9];
				CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_8, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 8); 
				Delay(1);//1ms
				Send_7C6_Data.msg.bits.Byte0Data = 0x22;
				Send_7C6_Data.msg.bits.Byte1Data = F187SendBuf[10];
				Send_7C6_Data.msg.bits.Byte2Data = F187SendBuf[11];  
				Send_7C6_Data.msg.bits.Byte3Data = F187SendBuf[12];
				Send_7C6_Data.msg.bits.Byte4Data = F187SendBuf[13];
				Send_7C6_Data.msg.bits.Byte5Data = F187SendBuf[14];
				Send_7C6_Data.msg.bits.Byte6Data = F187SendBuf[15];
				CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_8, TX_ISR_EN, (uint8_t*)&Send_7C6_Data.msg.data, 7);
				Write2EStep = 2;
			}
			break;
		case 2:
			Write2EStep = 0;
			Flag_SaveDate = 8; 
			break;
		}
	}
	else if(Flag_SaveDate == 8)
	{
		if((Receive_7CE_Data.data[1] == 0x6E) && (Receive_7CE_Data.data[2] == 0xF1) && (Receive_7CE_Data.data[3] == 0x87))//�жϷ���
		{
			Flag_SaveDate = 9;
			Key27Step = 0;
			Can_Clear((uint8_t*)&Receive_7CE_Data.data,sizeof(Receive_7CE_Data));
		}
		else if((Receive_7CE_Data.data[1] == 0x7F) && (Receive_7CE_Data.data[2] == 0x2E))//�жϷ���
		{
			Flag_SaveDate = 0;
			SetClusterState = 4;
			Key27Step = 0;
			Can_Clear((uint8_t*)&Receive_7CE_Data.data,sizeof(Receive_7CE_Data));
			memset(ReceiveData, 0, sizeof(ReceiveData));
			memset(UdsKeyData, 0, sizeof(UdsKeyData));
			memset(Key27Buffer, 0, sizeof(Key27Buffer));
		}
	}
	else if(Flag_SaveDate == 9)
	{
		Flag_SaveDate = 10;
		memset(&MultiplePack, 0, sizeof (MultiplePack));
		MyRecvCount = 0;
		
		Send_7CE_Data.Byte0Data = 0x03;
		Send_7CE_Data.Byte1Data = 0x22;
		Send_7CE_Data.Byte2Data = 0xF1;
		Send_7CE_Data.Byte3Data = 0x87;
		CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_27, TX_ISR_EN, (uint8_t*)&Send_7CE_Data.mdata, 4);
		RxFinish = UNFINISH;
	}
	else if(Flag_SaveDate == 10)
	{
		if(RxFinish == FINISH)
		{
			if(memcmp(&MultiplePack.Data[0], PartNum_AssemblyWorkshop, 10) == 0)
			{
				Flag_SaveDate = 11;
				Write2EStep = 0;
			}
			else
			{
				Flag_SaveDate = 0;
				Key27Step = 0;
				SetClusterState = 4;
			}
		}
	}
	else if(Flag_SaveDate == 11)
	{
		Flag_SaveDate = 12;
		memset(&MultiplePack, 0, sizeof (MultiplePack));
		MyRecvCount = 0;
		
		Send_7CE_Data.Byte0Data = 0x03;
		Send_7CE_Data.Byte1Data = 0x22;
		Send_7CE_Data.Byte2Data = 0xF1;
		Send_7CE_Data.Byte3Data = 0x8F;
		CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_27, TX_ISR_EN, (uint8_t*)&Send_7CE_Data.mdata, 4);
		RxFinish = UNFINISH;
        //UDS_Backdoor();
	}
	else if(Flag_SaveDate == 12)
	{
#if (PRO_CN7C)
   UDS_F18F_Response();
#endif       
        if(RxFinish == FINISH){
			if((MultiplePack.Data[0] == 0x11) && (MultiplePack.Data[1] == 0x22) && (MultiplePack.Data[2] == 0x33) && (MultiplePack.Data[3] == 0x44)
			   && (MultiplePack.Data[4] == 0x55) && (MultiplePack.Data[5] == 0x66) && (MultiplePack.Data[6] == 0x77) && (MultiplePack.Data[7] == 0x88))
			{
				Flag_SaveDate = 0;
				Write2EStep = 0;
				SetClusterState = 1;
			}
			else
			{
			
				Flag_SaveDate = 0;
				Key27Step = 0;
				SetClusterState = 4;
			}
#if (PRO_JAPE2)
            Speed_Output_Update(PWMCH_SPEED_OUT, 0, 0);
#endif
	    }
	}
}


