/********************************************************************************
* 常州易控汽车电子股份有限公司
* (c) Copyright 2009-2015 ECTEK
* 保留所有权利
********************************************************************************
* 工程名称：整车管理
* 文件名： UDSInterface.c
* 功能描述 ： UDS管理
* 引用记录：
* 备注：
* 修订记录：
*       日期            版本          作者        描述
*   2017.07.03          00.01.00      李中     第一版
*******************************************************************************/

#include "UDSInterface.h"
#include "HardwareLib.h"
#include "CANNet.h"
#include "EOLData.h"

uint8 UDSInterface_CheckServiceCondition(uint8 idxSIDUB, uint8 stSubFuncUB)
{
	(void)stSubFuncUB;
	uint8 bResultUB=1;

	switch(idxSIDUB)
	{
		case _UDS_INDEX_COMMAND_SECURITYACCESS:
			/*27*/
			bResultUB=1;
			break;

	    case _UDS_INDEX_COMMAND_ECURESET:
	        /*ECU Reset*/
	        bResultUB=1;
	        break;

		case _UDS_INDEX_COMMAND_WRITEDATABYIDENTIFIER:
			/*Write Data By Identifier*/
			bResultUB=1;
			break;

		case _UDS_INDEX_COMMAND_CONTROLDTCSETTING:
			/*Control DTC Setting*/
			bResultUB=1;
			break;

		case _UDS_INDEX_COMMAND_STARTDIAGNOSTICSESSION:
		    /*Start Diagnostic Session*/
//		    if(stSubFuncUB==_UDS_INDEX_DIAG_PROGRAMMING)
//		    {
//		        //必须先经过检查编程预条件
//		        if(UDSInterface_bPrgPrecondChkedUB==1)
//		        {
//		            bResultUB=1;
//		        }
//		        else
//		        {
//		            bResultUB=0;
//		        }
//		    }
//		    else if(stSubFuncUB==_UDS_INDEX_DIAG_DEFAULT)
//		    {
//		        UDSInterface_bPrgPrecondChkedUB=0;
//		    }
//		    else
//		    {
//		    	bResultUB=1;
//		    }
			bResultUB=1;
		    break;

		default:
			bResultUB=1;
			break;
	}

	return bResultUB;
}

void UDSInterface_GetSeed(uint8 idxSecLvlUB, uint8 * dataSeedUBP)
{
//    uint32 datUI;

	switch(idxSecLvlUB)
	{
		case _UDS_INDEX_SEC_EXTENTED:
        	if(idxSecLvlUB==UDS_GetSecLvl())
        	{
        		/*如果请求的安全等级已解锁，则发送数值为0的种子*/
        		dataSeedUBP[0]=0;
        		dataSeedUBP[1]=0;
        		dataSeedUBP[2]=0;
        		dataSeedUBP[3]=0;
        	}
        	else
        	{
        		dataSeedUBP[0]=11;//11
        		dataSeedUBP[1]=22;//22
        		dataSeedUBP[2]=33;//33
        		dataSeedUBP[3]=44;//44
        	}
			break;
		case _UDS_INDEX_SEC_ANTITHEFT:
        	if(idxSecLvlUB==UDS_GetSecLvl())
        	{
        		/*如果请求的安全等级已解锁，则发送数值为0的种子*/
        		dataSeedUBP[0]=0;
        		dataSeedUBP[1]=0;
        		dataSeedUBP[2]=0;
        		dataSeedUBP[3]=0;
        	}
        	else
        	{
        		dataSeedUBP[0]=0x55;
        		dataSeedUBP[1]=0x66;
        		dataSeedUBP[2]=0x77;
        		dataSeedUBP[3]=0x88;
        	}
			break;
        default:
			dataSeedUBP[0]=0x00;
			dataSeedUBP[1]=0x00;
			dataSeedUBP[2]=0x00;
			dataSeedUBP[3]=0x00;
            break;
	}
}

void UDSInterface_GetKey(uint8 idxSecLvlUB, uint8 * dataSeedUBP, uint8 * dataKeyUBP)
{
	uint8 Count; //循环次数
	uint8 Dir; //循环移位方向，0左移-1右移
	uint8 Pattern; //模式匹配
	uint8 Select; //选择MASK
	uint8 i; //循环计数
	uint32 seed; //中间转换SEED

	uint32 mask[4]={0x5DEC1836,0x5DEC1836,0x5DEC1836,0x5DEC1836}; //mask表
	switch(idxSecLvlUB)
	{
		case _UDS_INDEX_SEC_EXTENTED:
		case _UDS_INDEX_SEC_ANTITHEFT:
			Count=dataSeedUBP[1]&0x0F;
			Select=(dataSeedUBP[3]&0x30)>>4;

			seed=((uint32)dataSeedUBP[0]<<24)+((uint32)dataSeedUBP[1]<<16)+((uint32)dataSeedUBP[2]<<8)+((uint32)dataSeedUBP[3]);

			for(i=0;i<Count;i++)
			{
				Pattern=((uint8)seed)&0x07;
				Dir=(((uint8)seed)&0x08)>>3;

				if(Pattern!=0b101)
				{
					if(Dir==1)
					{
						seed=seed/2+((seed&0x00000001)<<31);//右移
					}
					else
					{
						seed=seed*2+((seed&0x80000000)>>31);//左移
					}
				}
				else
				{
					break;
				}
			}
			seed=seed^mask[Select];
			for(i=0;i<4;i++)
			{
				dataKeyUBP[i]=(uint8)(seed>>((3-i)*8));
			}
			break;
        default:
            break;
	}
}

/*0x31 例程控制回调函数样例*/
uint8 UDSInterface_RoutineTestCallback(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP)
{
    if(numRecvLenUW>3)
    {
        switch(datRecvBuffUBP[0]&0x7F) //suppressPosRspMsgIndicationBit supported
        {
            case _UDS_ROUTINECONTROL_START:
                break;
            case _UDS_ROUTINECONTROL_STOP:
                break;
            case _UDS_ROUTINECONTROL_REQUESTRESULTS:
                break;
            default:
                break;
        }
    }
    else
    {
        return _UDS_INDEX_ERROR_INVALIDFORMAT;
    }

    *numSendLenUWP=4;
    datSendBuffUBP[0]=0x11;
    datSendBuffUBP[1]=0x22;
    datSendBuffUBP[2]=0x33;
    datSendBuffUBP[3]=0x44;

    return 0;
}

/*0x2F 例程控制回调函数样例*/
uint8 UDSInterface_IOControlCallback(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP)
{
    if(numRecvLenUW>3)
    {
        switch(datRecvBuffUBP[0]&0x7F) //suppressPosRspMsgIndicationBit supported
        {
            case _UDS_ROUTINECONTROL_START:
                break;
            case _UDS_ROUTINECONTROL_STOP:
                break;
            case _UDS_ROUTINECONTROL_REQUESTRESULTS:
                break;
            default:
                break;
        }
    }
    else
    {
        return _UDS_INDEX_ERROR_INVALIDFORMAT;
    }

    *numSendLenUWP=4;
    datSendBuffUBP[0]=0x11;
    datSendBuffUBP[1]=0x22;
    datSendBuffUBP[2]=0x33;
    datSendBuffUBP[3]=0x44;

    return 0;
}


