#ifndef __UDS_COMMON_H
#define __UDS_COMMON_H

//#include "CANFD.h"
#include "stdint.h"
//#include "RTE_CAN.h"
//#include "CAN_APP.h"
#include "UDS_DTC.h"
//#include "abstract.h"
#include "EOL_Para.h"
//#include "ODOReset.h"
//#include "MBD_Model.h"
//#include "Work_Flash.h"
#include "NM_Include.h"
//#include "Services_ODO.h"
//#include "s6j3360_HWDG.h"
//#include "Sound_Player.h"
//#include "Common_Interface.h"
//#include "Services_Interval.h"
//#include "Service_DataRecoder.h"
#include "UDS_ISO14229_Server.h"
//#include "UDS_SpeedoTachoMeter.h"
#include "DoCAN_ISO15765_Config.h"
//#include "Services_ODO_Internal.h"
//#include "LCDMBD_Ctrl_Interface.h"
//#include "Communication_Over_CAN.h"
//#include "Telltales_Ctrl_Interface.h"
#include "UDS_ISO14229_Server_Config.h"

/**
  *��������
  */
#define POSITIVE_RSP                                            (uint8_t) 0x00                          /*������*/
#define NEGATIVE_RSP                                            (uint8_t) (!POSITIVE_RSP)               /*������*/

/**
  *DFlash
  */
#define NoNeedWait                                              0x00u
#define NeedWait                                                0x01u
#define JumpToBoot02                                           0x3AA3 //0x58856336u
#define JumpToBoot82                                           0x6AA6 //0x85583663u

enum g_enFlashBlockName
{
    EEPROM_BLOCK_UDS_FLAG = 1U,
    EEPROM_BLOCK_UDS_S27 = 2U,
    EEPROM_BLOCK_UDS_ReadBoot = 3U,
    EEPROM_BLOCK_UDS_S2E = 4U,
};

/**
  *10������ӷ���
  *��ϻỰ
  */
#define DiagnosticSessionControl                                0x10u
#define DefaultSession                                          0x01u
#define ProgrammingSession                                      0x02u
#define ExtendedDiagnosticSession                               0x03u

/**
  *11������ӷ���
  *ECU��λ
  */
#define ECUReset                                                0x11u
#define HardReset                                               0x01u
#define SoftReset                                               0x03u

/**
  *14������ӷ���
  *���������Ϣ
  */
#define ClearDiagnosticInformation                              0x14u

/**
  *19������ӷ���
  *��ȡ������Ϣ
  */
#define ReadDTCInformation                                      0x19u
#define ReportNumberOfDTCByStatusMask                           0x01u
#define ReportDTCByStatusMask                                   0x02u
#define ReportDTCSnapshotRecordByDTCNumber                      0x04u
#define ReportSupportedDTC                                      0x0Au

#define NoSupport                                               0x00u
#define NeedSupport                                             0x01u
#define NormalVottage                                           0x00u
#define LowVottage                                              0x01u
#define HightVottage                                            0x02u
#define AgeingCount                                             100u
#define OfflineLoopMultiple                                     5u
#define MaxDTCPendingCount                                      1u
#define MinimumVoltage                                          18000u
#define RestoreMinimumVoltage                                   19000u
#define MaximumVoltage                                          32000u
#define RestoreMaximumVoltage                                   31000u

#define DTCError                                                0x01u
#define DTCErrorThisCycle                                       0x02u
#define DTCPending                                              0x04u
#define DTCConfirmed                                            0x08u
#define DTCNoTestLastClear                                      0x10u
#define DTCErrorLastClear                                       0x20u
#define DTCNoTestThisCycle                                      0x40u
#define DTCNoError                                              0xFEu

#define DTCMask                                                 0x09u

#define KL15DTCEnableTimer                                      3500u
#define H_LVoltageDTCEnableTimer                                500u
#define BusOffDTCEnableTimer                                    1000u

/**
  *22������ӷ���
  *ͨ��DID��������
  */
#define ReadDataByIdentifier                                    0x22u

/**
  *27������ӷ���
  *��ȫ����
  */
#define SecurityAccess                                          0x27u
#define requestSeed_LV1                                         0x01u
#define sendKey_LV1                                             0x02u
#define requestSeed_LV11                                        0x11u
#define sendKey_LV11                                            0x12u
#define NormalKeyUnlock                                         0x01u
#define NormalKeyLock                                           0x00u
#define BootKeyUnlock                                           0x02u
#define BootKeyLock                                             0x00u
#define SER27_MAX_REQ_SEED_NUM                                  0x03u
#define SER27_MAX_INVAILD_KEY_NUM                               0x03u
#define SER27_CLEAR_REQ_SEEDED                                  0x00u
#define SER27_REQ_SEEDED                                        0x01u
#define SER27_ERR_DELAY_TIMER_EN                                0x01u
#define SER27_ERR_DELAY_SER_PERIOD                              100u
#define SER27_ERR_DELAY_TIMEOUT                                 10000u

/**
  *28������ӷ���
  *ͨѶ����
  */
#define CommunicationControl                                    0x28u
#define enableRxAndTx                                           0x00u
#define enableRxAndDisableTx                                    0x01u
#define disableRxAndEnableTx                                    0x02u
#define disableRxAndTx                                          0x03u
#define normalMsg                                               0x01u
#define networkManagementMsg                                    0x02u
#define networkManagementMsgAndnormalMsg                        0x03u

/**
  *2E������ӷ���
  *ͨ��DID��д����
  */
#define WriteDataByIdentifier                                   0x2Eu
#define WriteBootData                                           0x00u
#define Write2EData                                             0x01u

/**
  *2F������ӷ���
  *ͨ��DID������ECU
  */
#define InputOutputControlByIdentifier                          0x2Fu
#define WhiteColor                                              0x01u
#define BlackColor                                              0x02u
#define RedColor                                                0x03u
#define GreenColor                                              0x04u
#define BlueColor                                               0x05u
#define GaugeSpeed                                              0x01u
#define GaugeTacho                                              0x02u
#define GaugeFuel                                               0x03u
#define GaugeEngCoolantTemp                                     0x04u
#define ZeroPosition                                            0x00u
#define HalfPosition                                            0x01u
#define FullPosition                                            0x02u
#define Contrl                                                  0x03u
#define Resume                                                  0x00u

/**
  *31������ӷ���
  *���̿���
  */
#define RoutineControl                                          0x31u
#define startRoutine                                            0x01u
#define stopRoutine                                             0x02u
#define requestRoutineResults                                   0x03u
#define MaintenanceReset                                        0X62F1u
#define CheckMemory                                             0x0202u
#define UpdateConditionCheck                                    0x0203u
#define ECU_selftest                                            0xDFF0u
#define EraseMemory                                             0xFF00u
#define CheckProgrammingDependencies                            0xFF01u
#define RoutineCompletedSuccessfully                            0x04u
#define RoutineCompletedUnsuccessfully                          0x05u

/**
  *3E������ӷ���
  *ά�ֵ�ǰ�Ự
  */
#define TesterPresent                                           0x3Eu
#define ZeroSubFunction                                         0x00u

/**
  *85������ӷ���
  *���ϼ�����
  */
#define  ControlDTCSetting                                      0x85u
#define  DTCSettingOn                                           0x01u
#define  DTCSettingOff                                          0x02u

/**
  *���֧�ֵķ���Ӧ����
  */
#define positiveResponse                                        0x00u
#define	serviceNotSupported                                     0x11u
#define	subFunctionNotSupported                                 0x12u
#define	incorrectMessageLength                                  0x13u
#define	responseTooLong                                         0x14u
#define	conditionsNotCorrect                                    0x22u
#define	requestSequenceError                                    0x24u
#define	requestOutOfRange                                       0x31u
#define	securityAccessDenied                                    0x33u
#define	invalidKey                                              0x35u
#define	exceedNumberOfAttempts                                  0x36u
#define	requiredTimeDelayNotExpired                             0x37u
#define	uploadDownloadNotAccepted                               0x70u
#define	transferDataSuspended                                   0x71u
#define	generalProgrammingFailure                               0x72u
#define	wrongBlockSequenceCounter                               0x73u
#define	requestCorrectlyReceivedResponsePending                 0x78u
#define	subfunctionNotSupportinActiveSession                    0x7Eu
#define	serviceNotSupportedInActiveSession                      0x7Fu
#define	engineIsRunning                                         0x83u
#define	engineIsNotRunning                                      0x84u
#define	engineRunTimeTooLow                                     0x85u
#define	voltageTooHigh                                          0x92u
#define	voltageTooLow                                           0x93u

#define getDID(DID_H, DID_L)                                    ((uint16_t)(((uint16_t)(DID_H)) << 8u) | (uint16_t)(DID_L))
#define toUint16_t(data0, data1)                                ((uint16_t)(((uint16_t)(data0)) << 8u) | (uint16_t)(data1))
#define toUint32_t(data0, data1, data2, data3)                  ((uint32_t)(((uint32_t)(data0)) << 24u) | (uint32_t)(((uint32_t)(data1)) << 16u) | (uint32_t)(((uint32_t)(data2)) << 8u) | (uint32_t)(data3))
#define isBCD(data)                                             ((((uint8_t)((data) & 0x0Fu)) < 0x0Au) && (((uint8_t)(((uint8_t)((data) & 0xF0u)) >> 4u )) < 0x0Au))
#define DIM( X )                                                (sizeof(X) / sizeof((X)[0]))


/**
  * TYPEDEFS
  */
typedef union 
{
    uint8_t Data[150];
    struct
    {
        uint8_t SubID                                           :7;
        uint8_t PositiveSuppression                             :1;
        uint8_t Data[149];
    } SidDetail;
    struct
    {
        uint8_t DID_H;
        uint8_t DID_L;
        uint8_t Data[148];
    } DidDetail;
    struct
    {
        uint32_t Parameter                                      :24;
        uint8_t Data[147];
    } S14Detail;
    struct
    {
        uint8_t SubID;
        uint8_t DID_H;
        uint8_t DID_L;
        uint8_t Data[147];
    } S31Detail;
    struct
    {
      uint8_t SubID                                            :7;
      uint8_t PositiveSuppression                              :1; 
      uint8_t ContrlType;
      uint8_t Data[148];  
    }S28Detail;
    
} msgDataUnion;

typedef struct
{
    uint32_t msgID;
    uint16_t msgDLC;
    msgDataUnion msgData;
} DiagMSGUnion;

typedef struct
{
    uint8_t LV1Ctrl;
    uint8_t LV1DelayCtrl;
    uint16_t LV1DelayTimer;
    uint8_t LV1SafetyStatus;
    uint8_t LV1Seed[4];
    uint8_t LV1Key[4];
} Ser27_FlowCtrlUnion;

typedef struct
{
    //uint32_t Flag;
    //uint32_t JumpToBootFlag;
    //uint32_t AppValidFlag;
    //uint32_t S11Flag;
  uint32_t Flag;
  uint16_t JumpToBootFlag;
  uint16_t AppValidFlag;
  uint32_t S11Flag;
  uint16_t uwBootWinTimer;
  uint16_t  ubBootTrigWinDelay;
} DiagFlagUnion;

typedef struct
{
    uint32_t Flag;
    uint8_t  LV1ReqSeedCnt;
    uint8_t  LV1InvaidKyeCnt;
    uint8_t  LV11_ReqSeedCnt;
    uint8_t  LV11_InvaidKyeCnt;
    uint32_t RandNum;
} Ser27_FlowCtrlCntUnion;

typedef struct
{
    uint32_t Flag;
    uint8_t  DID_0200;
    uint8_t  DID_0201;
    uint8_t  DID_F180[16];
    uint8_t  DID_F187[21];
    uint8_t  DID_F189[10];
    uint8_t  DID_F193[16];
    uint8_t  DID_F198[16];
    uint8_t  DID_F199[4];
    uint8_t  DID_DFEC[64];
    uint8_t  DID_DFED[64];
    uint8_t  Filling;
} BootToDFlashInfoUnion;

typedef struct
{
    uint32_t Flag;
    uint8_t  DID_F190[17];
    uint8_t  DID_F1A2[4];
    uint8_t  DID_F112[8];
    uint8_t  DID_F197[8];  
    uint8_t  DID_DFEE[64];
    uint8_t  DID_DFEF[64];
    uint8_t  DID_F101[8];
    uint8_t  DID_F102[24];
    uint8_t  DID_F110[8];
    uint8_t  DID_F100[16];
    uint8_t  DID_7101[3];
    uint8_t  DID_7102[3];
    uint8_t  DID_7103;
    uint8_t  DID_7104;
    uint8_t  Filling;
} Ser2EToDFlashInfoUnion;
/*28 ����*/
typedef struct
{
    uint8_t  APP_RX_Enable;     
    uint8_t  APP_TX_Enable;
}_CAN_Control;

typedef enum {
  //Enable = 0u,
  APP_Enable = 1u,
  NM_Enable = 2u,
  Enable = 3u,
  APP_DisEnable = 0xFEu,
  NM_DisEnable = 0xFDu,
  //APP_DisEnable = 0,
}TxAndRxStatus;/**/

typedef void (*InitFunction)(void);

extern uint8_t S31DID62F3RequestStatus;
extern uint8_t HVoltageStatus;
extern uint8_t LVoltageStatus;
extern DiagMSGUnion DiagMSG;
extern DiagFlagUnion DiagFlag;
extern Ser27_FlowCtrlUnion Ser27_FlowCtrl;
extern Ser27_FlowCtrlCntUnion Ser27_FlowCtrlCnt;
extern BootToDFlashInfoUnion BootToDFlashInfo;
extern Ser2EToDFlashInfoUnion Ser2EToDFlashInfo;
extern DTCToDFlashInfoUnion DTCToDFlashInfo;
extern uint32_t DTCTimerList[27u];
extern uint32_t msgList[22u][4u];
extern uint8_t BUS_OFF_FLAG;
extern uint8_t BusOffEvent;
extern _CAN_Control CAN_Control; 

void setSessionType(uint8_t status);
uint8_t getSessionType(void);
void setDiagMSG(uint32_t Identifier, uint16_t dlc, uint8_t Data[]);
DiagMSGUnion getDiagMSG(void);
void clearDiagMSG(void);
uint16_t getSpeed(void);
uint16_t getESpeed(void);                                                      
uint16_t getKL30Voltage(void);
uint8_t getKL15Status(void);
uint8_t getSecurityLevel(void);
void Ser27_ChangeSessionHandle(void);
void UDS_RESET(void);
void UDS_Delay(uint16_t count);
void UDS_S11_Sand(uint8_t subid);
void UDS_S11_Confirm(void);
void UDS_NRC78_Sand(uint8_t sid);
void UDS_SendNRCRsp(uint8_t sid,uint8_t status);
void UDS_14_SendRsp(uint8_t sid);
void UDS_50us_Service(void);
void UDS_10ms_Service(void);
void UDS_KL15_init(void);
void MsgLostTimer(uint16_t timer);
void ReceivedMsg(uint32_t MsgID, uint8_t DLC);
void S3_Service(uint16_t timer);
void S3_ServiceClear(void);
void S3_ServiceWait(void);
void S3_ServiceTimeOut(void);
void Ser27_ErrDelayTimerService(uint16_t timer);
void ControlMessageCommunication(uint8_t Type, uint8_t Status);
void enableDTCTimer(uint16_t Timer);
uint8_t isDTCEnable(void);
void setDTCEnableTimer(uint16_t Timer);
void OpenDTCTest(void);
void CloseDTCTest(void);
uint8_t DTCTestIsOpen(void);
void DTC_Ageing(void);
void ReadAllDFlashData(void);
uint8_t ReadDFlashData(uint8_t u8BlockNum, uint32_t u32Data[], uint16_t u16Len, InitFunction initFunction);
uint8_t WriteDFlashData(uint8_t u8BlockNum, uint32_t u32Data[], uint16_t u16Len, uint8_t isWait);
void AllFlagInit(void);
void Ser27ToDFlashInfoInit(void);
void BootToDFlashInfoInit(void);
void Ser2EToDFlashInfoInit(void);
void DTCToDFlashInfoInit(void);
void DTCConfigurationInit(void);
uint8_t getBUS_OFF_Status(void);
uint8_t getBUS_OFF_FLAG(void);
uint16_t Get_SigABS_VehicleSpeed(void);
uint16_t Get_SigEMS_EngSpeed(void);
void Com_RxStart(void);
void Com_TxStart(void);
void Com_RxStop(void);
void Com_TxStop(void);
#endif