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

#ifndef __UDSINTERFACE_G
    #define __UDSINTERFACE_G

    //#include "Std_Types.h"  //19.12.16day, This is a bad move due to unknown error 把uint16的定义copy于此
		typedef unsigned char boolean;

		typedef signed char sint8;
		typedef unsigned char uint8;
		typedef signed short sint16;
		typedef unsigned short uint16;
		typedef signed long sint32;
		typedef unsigned long uint32;
		typedef float float32;

		typedef unsigned long uint8_least;
		typedef unsigned long uint16_least;
		typedef unsigned long uint32_least;

		typedef signed long sint8_least;
		typedef signed long sint16_least;
		typedef signed long sint32_least;
//******************************This is a bad move due to unknown error 把uint16的定义copy于此

    /*UDS错误消息定义*/
    #define _UDS_INDEX_ERROR_GENERALREJECT 0x10
    #define _UDS_INDEX_ERROR_SERVICENOTSUPPORTED 0x11
    #define _UDS_INDEX_ERROR_SUBFUNCTIONNOTSUPPORTED 0x12
    #define _UDS_INDEX_ERROR_INVALIDFORMAT 0x13
    #define _UDS_INDEX_ERROR_BUSYREPEATREQUEST 0x21
    #define _UDS_INDEX_ERROR_CONDITIONNOTCORRECT 0x22
    #define _UDS_INDEX_ERROR_ROUTINENOTCOMPLETEORSERVICEINPROCESS 0x23
	#define _UDS_INDEX_ERROR_REQUESTSEQUENCEERROR 0x24
    #define _UDS_INDEX_ERROR_REQUESTOUTOFRANGE 0x31
    #define _UDS_INDEX_ERROR_SECURITYACCESSDENIED 0x33
    #define _UDS_INDEX_ERROR_INVALIDKEY 0x35
    #define _UDS_INDEX_ERROR_EXCEEDNUMBEROFATTEMPS 0x36
    #define _UDS_INDEX_ERROR_TIMEDELAYNOTEXPIRED 0x37
    #define _UDS_INDEX_ERROR_DOWNLOADNOTACCEPTED 0x40
    #define _UDS_INDEX_ERROR_IMPROPERDOWNLOADTYPE 0x41
    #define _UDS_INDEX_ERROR_CANNOTDOWNLOADTOSPECIFIEDADDRESS 0x42
    #define _UDS_INDEX_ERROR_CANNOTDOWNLOADNUMBEROFBYTESREQUESTED 0x43
    #define _UDS_INDEX_ERROR_UPLOADNOTACCEPTED 0x50
    #define _UDS_INDEX_ERROR_IMPROPERUPLOADTYPE 0x51
    #define _UDS_INDEX_ERROR_CANNOTUPLOADTOSPECIFIEDADDRESS 0x52
    #define _UDS_INDEX_ERROR_CANNOTUPLOADNUMBEROFBYTESREQUESTED 0x53
    #define _UDS_INDEX_ERROR_BLOCKTRANSFERDATACHECKSUMERROR 0x77
	#define _UDS_INDEX_ERROR_SUBFUNCTIONNOTSUPPORTEDINACTIVESESSION 0x7E
	#define _UDS_INDEX_ERROR_SERVICENOTSUPPORTEDINACTIVESESSION 0x7F
    #define _UDS_INDEX_ERROR_SERVICENOTSUPPORTEDINACTIVEDIAGNOSTICMODE 0x80
    #define _UDS_INDEX_ERROR_CHECKSUMERROR 0xFA
    #define _UDS_INDEX_ERROR_ECUERASINGFLASH 0xFB
    #define _UDS_INDEX_ERROR_ECUPROGRAMMINGFLASH 0xFC
    #define _UDS_INDEX_ERROR_ERASINGERROR 0xFD
    #define _UDS_INDEX_ERROR_PROGRAMMINGERROR 0xFE
    
	/*UDS安全等级定义*/
    #define _UDS_NUM_MODE_MAX 3
    #define _UDS_INDEX_SEC_LOCK 0
    #define _UDS_INDEX_SEC_EXTENTED 1
	#define _UDS_INDEX_SEC_ANTITHEFT 2
    #define _UDS_INDEX_SEC_PROGRAMMING 3

	/*UDS RoutineControl Control Type*/
	#define _UDS_ROUTINECONTROL_START			0x01
	#define _UDS_ROUTINECONTROL_STOP			0x02
	#define _UDS_ROUTINECONTROL_REQUESTRESULTS	0x03

    /*UDS 命令定义*/
    #define _UDS_INDEX_COMMAND_NULL 0

    /*Diagnostic Management*/
    #define _UDS_INDEX_COMMAND_STARTDIAGNOSTICSESSION 0x10U
    #define _UDS_INDEX_COMMAND_ECURESET 0x11U
    #define _UDS_INDEX_COMMAND_SECURITYACCESS 0x27U
    #define _UDS_INDEX_COMMAND_TESTERPRESENT 0x3EU
    #define _UDS_INDEX_COMMAND_ACCESSTIMINGPARAMERTERS 0x83U

    /*Data Transmission*/
    #define _UDS_INDEX_COMMAND_READDATABYIDENTIFIER 0x22U
    #define _UDS_INDEX_COMMAND_READMEMORYBYADDRESS 0x23U
    #define _UDS_INDEX_COMMAND_READSCALINGDATABYIDENTIFIER 0x24U
    #define _UDS_INDEX_COMMAND_READDATABYPERIODICIDENTIFIER 0x2AU
    #define _UDS_INDEX_COMMAND_DYNAMICALLYDEFINEDATAIDENTIFIER 0x2CU
    #define _UDS_INDEX_COMMAND_WRITEDATABYIDENTIFIER 0x2EU
    #define _UDS_INDEX_COMMAND_WRITEMEMORYBYADDRESS 0x3DU

	/*Input Output Control*/
    #define _UDS_INDEX_COMMAND_INPUTOUTPUTCONTROL 0x2FU

    /*Fault Memory*/
    #define _UDS_INDEX_COMMAND_READDTCINFORMATION 0x19U
    #define _UDS_INDEX_COMMAND_CLEARDIAGNOSTICINFORMATION 0x14U
    #define _UDS_INDEX_COMMAND_CONTROLDTCSETTING 0x85U

    /*Flash Memory Programming*/
    #define _UDS_INDEX_COMMAND_REQUESTUPLOAD 0x35U
    #define _UDS_INDEX_COMMAND_REQUESTDOWNLOAD 0x34U
    #define _UDS_INDEX_COMMAND_TRANSFERDATA 0x36U
    #define _UDS_INDEX_COMMAND_REQUESTTRANSFEREXIT 0x37U

    /*Remote Activation of Routine*/
    #define _UDS_INDEX_COMMAND_ROUTINECONTROL 0x31U

	/*Communication*/
    #define _UDS_INDEX_COMMAND_COMMUNICATIONCONTROL 0x28U

    #define _UDS_INDEX_POSRESP_COMMAND  0x40U

    /*UDS DID信息上层配置*/
    #define _UDS_DID_MASK_READ			0x01  //Read Enable, 0x22
    #define _UDS_DID_MASK_WRITE			0x02  //Write Enable, 0x2E
    #define _UDS_DID_MASK_CONTROL		0x04  //Control Enable, 0x2F
    #define _UDS_DID_MASK_ENDIANNESS	0x80  //Endianness


	/*UDS Session Mode定义*/
	#define _UDS_INDEX_DIAG_DEFAULT  0x01
	#define _UDS_INDEX_DIAG_PROGRAMMING  0x02
	#define _UDS_INDEX_DIAG_EXTENDED  0x03

	typedef uint8 (*pFunc_UDSIOCtrlCallback)(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP);


    typedef struct
    {
		uint8    DataTypeUB;        ///该DID的数据类型
		uint16   idxDataIDUW;       ///DID
		uint32   addrDataUI;        ///数据地址
		float32  ConvertFactor;     ///该DID的转换因子 B
		float32  ConvertOffset;     ///该DID的转换偏移量 C
		uint16   ConvertnumSizeUW;  ///转换后的字节数
		uint16   numSizeUW;         ///物理值字节数
		boolean  idxByteOrderUB;    ///访问时的大小端方式，0Big/1Little
		uint8    infoMaskReadUW;    ///0x22 readID权限
		uint8    infoMaskWriteUW;   ///0x2E writeID权限
		uint8    infoMaskIOCtrlUW;  ///0x2F IOCtrl权限
		pFunc_UDSIOCtrlCallback pIOCtrlCallback;
    } UDSInterface_infoDIDS_TYPE; /*环境信息*/

    typedef struct
    {
        uint16 numDIDUW;
        const UDSInterface_infoDIDS_TYPE * addrEnvDataSP;
    } UDSInterface_infoDIDConfS_TYPE;

    /*UDS Routine信息上层配置*/
    typedef uint8 (*pFunc_UDSRoutineCallback)(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP);

    typedef struct
    {
        uint16 idxRoutineIDUW;
        uint16 infoMaskUW; //与DID的Mask定义一直，使用其Control部分
        pFunc_UDSRoutineCallback pRoutineCallback;
    } UDSInterface_infoRoutineS_TYPE; /*例程信息*/

    typedef struct
    {
        uint8 numRoutineUB;
        const UDSInterface_infoRoutineS_TYPE * addrRoutineSP;
    } UDSInterface_infoRoutineConfS_TYPE;

    typedef struct
    {
    	uint8 numParamIDUB;
    	const uint16 * addrSnapshotDIDSP;
    } UDSInterface_infoSnapshotConfS_TYPE;



    uint8 UDSInterface_CheckServiceCondition(uint8 idxSIDUB, uint8 stSubFuncUB);
	void UDSInterface_GetSeed(uint8 idxSecLvlUB, uint8 * dataSeedUBP);
	void UDSInterface_GetKey(uint8 idxSecLvlUB, uint8 * dataSeedUBP, uint8 * dataKeyUBP);
	uint8 UDSInterface_RoutineTestCallback(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP);
	uint8 UDSInterface_IOControlCallback(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP);

    extern const UDSInterface_infoDIDConfS_TYPE UDSInterface_infoDIDConfS;
    extern const UDSInterface_infoRoutineConfS_TYPE UDSInterface_infoRoutineConfS;
    extern const UDSInterface_infoSnapshotConfS_TYPE UDSInterface_infoSnapshotConfS;

#endif
