540 lines
14 KiB
C
540 lines
14 KiB
C
/*!
|
||
* @file uds_interface.c
|
||
* @brief customized service sequence definition. Here is the demo sequence. Modify the file according to the requirements.
|
||
*
|
||
*/
|
||
|
||
#include "uds_config.h"
|
||
/*!
|
||
* Demo Macro. Services number in the sequence.
|
||
*/
|
||
#define SERVICE_SEQUENCE_NUM (UDS_SERVICE_NUM)
|
||
/*!
|
||
* Demo Type. Service Control type.
|
||
*/
|
||
_UdsUpData UdsUpData;
|
||
__UDSService UdsService;
|
||
|
||
const uint8_t UDSsid[] = {
|
||
UDS_SERVICE_FOREACH(GENERATE_UDS_SERVICE_ID)
|
||
};
|
||
|
||
|
||
|
||
/*!
|
||
* Demo Type. Service Function type.
|
||
*/
|
||
typedef void (*SERVICE_FUNC_T)(void);
|
||
//static uint8_t UDS_Routine_ID[2]; /*!< Demo Variable. Routine ID buffer for Service 31 */
|
||
uint8_t UDS_SeedKeyBuf[8]; /*!< Demo Variable. Seed key buffer for Service 27 */
|
||
//static uint8_t UDS_AddrBuf[4]; /*!< Demo Variable. Address buffer for Service 34 */
|
||
//static uint8_t UDS_LengthBuf[4]; /*!< Demo Variable. Length buffer for Service 34 */
|
||
//static uint8_t UDS_DataBuf[100]; /*!< Demo Variable. Data buffer for Service 36 */
|
||
UDS_Demo_Service_Ctrl_T Service_Ctrl; /*!< Demo Variable. Service Control flag */
|
||
|
||
static void UDS_10_Service_Session_Control(void);
|
||
static void UDS_11_Service_Ecu_Reset(void);
|
||
static void UDS_22_Service_Read_Data(void);
|
||
static void UDS_2E_Service_Write_Data(void);
|
||
static void UDS_3E_Service_Tester_Present(void);
|
||
static void UDS_27_Service_Security_Access(void);
|
||
static void UDS_31_Service_Routine_Ctrl(void);
|
||
static void UDS_28_Service_Communication_Control(void);
|
||
static void UDS_85_Service_Control_DTC(void);
|
||
#if (BOOTLOADER_CODE_FLAG == 1)
|
||
static void UDS_34_Service_Request_Download(void);
|
||
static void UDS_36_Service_Transfer_Data(void);
|
||
static void UDS_37_Service_Transfer_Exit(void);
|
||
#else
|
||
static void UDS_14_Service_Clear_DTC(void);
|
||
static void UDS_19_Service_Read_DTC(void);
|
||
static void UDS_2F_IO_OutputControl(void);
|
||
#endif
|
||
|
||
static void UDS_Service_NRC(void);
|
||
|
||
/*!
|
||
* Demo Variable. Service Sequence table.
|
||
*/
|
||
static SERVICE_FUNC_T UDS_Demo_Service_Table[SERVICE_SEQUENCE_NUM]= {
|
||
UDS_SERVICE_FOREACH(GENERATE_UDS_SERVICE_TABLE)
|
||
};
|
||
|
||
#ifdef PRINT_LOG_INFO
|
||
void print_rx_data(uint8_t code)
|
||
{
|
||
// uint8_t i;
|
||
// Sprintf("\r\n=======>Rx uds 0x%02x code data:",code);
|
||
// for(i = 0; i< UdsService.RxDlc; i++) {
|
||
// Sprintf("0x%02x,",UdsService.ReqCmdBuf[i]);
|
||
//
|
||
// }
|
||
// Sprintf("\r\n");
|
||
}
|
||
#endif
|
||
|
||
bool CanRxConStatus(void)
|
||
{
|
||
bool xbRst=false;
|
||
//if((gSystemStatus.EngineStatus)&&(ign1.state=='L'))
|
||
// if(( true == UdsUpData.UpModleFlg )||((!HsCanError.StartTime)&&(('L' == ign1.state)||('L' == ign3.state))))
|
||
{
|
||
xbRst = true;
|
||
}
|
||
return xbRst;
|
||
}
|
||
|
||
static void UDS_10_Service_Session_Control(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x10 == UdsService.Id)) {
|
||
UDS_Service_10_Response(UdsService.RxSessionDiagModel);
|
||
UdsService.RequestFlg=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
static void UDS_11_Service_Ecu_Reset(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x11 == UdsService.Id)) {
|
||
UDS_Service_11(UdsService.ReqCmdBuf);
|
||
UdsService.RequestFlg=0;
|
||
}
|
||
}
|
||
#if (BOOTLOADER_CODE_FLAG == 0)
|
||
static void UDS_14_Service_Clear_DTC(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x14 == UdsService.Id)) {
|
||
UDS_Service_14(UdsService.ReqCmdBuf,3);
|
||
UdsService.RequestFlg=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
return;
|
||
}
|
||
|
||
static void UDS_19_Service_Read_DTC(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x19 == UdsService.Id)) {
|
||
UDS_Service_0x19_Response();
|
||
//memset(TP_Buffer,0x0,255);
|
||
UdsService.RequestFlg=0;
|
||
}
|
||
}
|
||
|
||
static void UDS_2F_IO_OutputControl(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x2F == UdsService.Id)) {
|
||
UDS_Service_0x2F_Response();
|
||
UdsService.RequestFlg=0;
|
||
}
|
||
}
|
||
|
||
#endif
|
||
static void UDS_22_Service_Read_Data(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x22 == UdsService.Id)) {
|
||
UDS_Service_0x22_Response();
|
||
//memset(TP_Buffer,0x0,255);
|
||
UdsService.RequestFlg=0;
|
||
}
|
||
}
|
||
static void UDS_2E_Service_Write_Data(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x2E == UdsService.Id)) {
|
||
UDS_Service_0x2E_Response(&TP_Buffer[1],TP_Rx_Index-1);
|
||
UdsService.MultFrameComp=0;
|
||
UdsService.RequestFlg=0;
|
||
//memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
|
||
static void UDS_3E_Service_Tester_Present(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x3E == UdsService.Id)) {
|
||
UDS_Service_0x3E_Response(&UdsService.ReqCmdBuf[0]);
|
||
UdsService.RequestFlg=0;
|
||
//memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
|
||
static void UDS_27_Service_Security_Access(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x27 == UdsService.Id)) {
|
||
if (UDS_SERVICE_27_SEND_KEY != UdsService.ReqCmdBuf[0]) {
|
||
UDS_Service_27(UDS_SERVICE_27_REQUEST_SEED,UdsService.ReqCmdBuf, UDS_SeedKeyBuf);
|
||
UdsService.RequestFlg=0;
|
||
//memset(TP_Buffer,0x0,255);
|
||
} else if (UDS_SERVICE_27_SEND_KEY == UdsService.ReqCmdBuf[0]) {
|
||
UDS_Service_27(UDS_SERVICE_27_SEND_KEY,UdsService.ReqCmdBuf,UDS_SeedKeyBuf);
|
||
UdsService.RequestFlg=0;
|
||
UdsService.MultFrameComp=0;
|
||
memset(UDS_SeedKeyBuf,0x0,8);
|
||
//memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
}
|
||
|
||
//static void UDS_Demo_Service_Request_Seed(void)
|
||
//{
|
||
// if((UdsService.RequestFlg)&&(0x27 == UdsService.Id)) {
|
||
// UDS_Service_27(UDS_SERVICE_27_REQUEST_SEED,UdsService.ReqCmdBuf, UDS_SeedKeyBuf);
|
||
// UdsService.RequestFlg=0;
|
||
// //memset(TP_Buffer,0x0,255);
|
||
// }
|
||
//}
|
||
//
|
||
//static void UDS_Demo_Service_Send_Key(void)
|
||
//{
|
||
// if((UdsService.RequestFlg)&&(0x27 == UdsService.Id)) {
|
||
// UDS_Service_27(UDS_SERVICE_27_SEND_KEY,UdsService.ReqCmdBuf,UDS_SeedKeyBuf);
|
||
// UdsService.RequestFlg=0;
|
||
// UdsService.MultFrameComp=0;
|
||
// memset(UDS_SeedKeyBuf,0x0,8);
|
||
// //memset(TP_Buffer,0x0,255);
|
||
// }
|
||
//}
|
||
|
||
static void UDS_31_Service_Routine_Ctrl(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x31 == UdsService.Id)) {
|
||
UDS_Service_31(UdsService.ReqCmdBuf,3);
|
||
UdsService.RequestFlg=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
static void UDS_28_Service_Communication_Control(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x28 == UdsService.Id)) {
|
||
#ifdef PRINT_LOG_INFO
|
||
print_rx_data(0x28);
|
||
#endif
|
||
UDS_Service_28(UdsService.ReqCmdBuf,2);
|
||
UdsService.RequestFlg=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
//static void UDS_0x29_Service_Diag_Session(void)
|
||
//{
|
||
// if((UdsService.RequestFlg)&&(0x29 == UdsService.Id)) {
|
||
// UDS_Service_29(UdsService.ReqCmdBuf,1);
|
||
// UdsService.RequestFlg=0;
|
||
// memset(TP_Buffer,0x0,255);
|
||
// }
|
||
//}
|
||
//static void UDS_20_Service_Diag_Session(void)
|
||
//{
|
||
// if((UdsService.RequestFlg)&&(0x20 == UdsService.Id)) {
|
||
// UDS_Service_20(UdsService.ReqCmdBuf,1);
|
||
// UdsService.RequestFlg=0;
|
||
// memset(TP_Buffer,0x0,255);
|
||
// }
|
||
//}
|
||
|
||
static void UDS_85_Service_Control_DTC(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x85 == UdsService.Id)) {
|
||
#ifdef PRINT_LOG_INFO
|
||
print_rx_data(0x85);
|
||
#endif
|
||
UDS_Service_85(UdsService.ReqCmdBuf,1);
|
||
UdsService.RequestFlg=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
|
||
void GotoDefaultSession(void)
|
||
{
|
||
//INT8U Tmp[10];
|
||
static INT16U Timer1sCnt=0;
|
||
static INT16U Timer10msCnt=0;
|
||
|
||
//if(++Timer1sCnt >= 1000)
|
||
if(++Timer10msCnt >= 10) {
|
||
//Timer1sCnt=0;
|
||
Timer10msCnt = 0;
|
||
if (UdsService.SessionTimer < (UDS_S3Sever/10)) {
|
||
UdsService.SessionTimer++;
|
||
if (UdsService.SessionTimer == (UDS_S3Sever/10)) {
|
||
UdsService.IsUdsFlg = false;
|
||
if ((_UDS_EXT_SESSION == UdsService.SessionDiagModel)||(_UDS_PROGRAM_SESSION == UdsService.SessionDiagModel)) {
|
||
UdsService.RequestFlg=0;
|
||
UdsService.SessionDiagModel=_UDS_DEFAULT_SESSION;
|
||
UdsService.SeedKeyIsOK=0;
|
||
UdsService.AlreadSendSeed = false;
|
||
UdsService.SessionTimer=0;
|
||
UdsService.NcmDisRxAndTx = 0;
|
||
UdsService.NmcmDisRxAndTx = 0;
|
||
UdsService.DisDTCRecord=0;
|
||
UdsService.ChkProgPreconditionFlg = false;
|
||
if(true == UdsUpData.UpModleFlg ) {
|
||
UdsUpData.CalcCrc=0;
|
||
UdsUpData.UpModleFlg = false;
|
||
//EEPROM_write(0,"APP2",4);
|
||
//Tmp[0]=0x00;
|
||
//Tmp[1]=0x00;
|
||
//EEPROM_write(UDS_UPDATE_ADD,Tmp,2);
|
||
}
|
||
#if BOOTLOADER_CODE_FLAG == 0
|
||
IO_InputOutputRelease();
|
||
#else
|
||
UdsService.ResetMcuFlg = true; //<2F><><EFBFBD><EFBFBD>չ<EFBFBD>Ựģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD>̻Ựģʽ<C4A3>£<EFBFBD>S3 <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD>ᵼ<EFBFBD><E1B5BC> ECU <20><><EFBFBD><EFBFBD>
|
||
#endif
|
||
}
|
||
}
|
||
}
|
||
|
||
if (++Timer1sCnt < 100) return;
|
||
Timer1sCnt = 0;
|
||
if (UdsService.KeyErrLockTimer) {
|
||
UdsService.KeyErrLockTimer--;
|
||
if (UdsService.KeyErrLockTimer == 0) {
|
||
if(true == UdsService.KeyLockFlg ) {
|
||
UdsService.KeyLockFlg=false;
|
||
if (UdsService.KeyErrCnt > 0) {
|
||
UdsService.KeyErrCnt -= 1;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (UdsService.NcmDisRxAndTx) {
|
||
udsInfo2Vehicle.NcmRxEnable = false;
|
||
udsInfo2Vehicle.NcmTxEnable = false;
|
||
} else {
|
||
udsInfo2Vehicle.NcmRxEnable = true;
|
||
udsInfo2Vehicle.NcmTxEnable = true;
|
||
}
|
||
|
||
if (UdsService.NmcmDisRxAndTx) {
|
||
udsInfo2Vehicle.NmcmRxEnable = false;
|
||
udsInfo2Vehicle.NmcmTxEnable = false;
|
||
} else {
|
||
udsInfo2Vehicle.NmcmRxEnable = true;
|
||
udsInfo2Vehicle.NmcmTxEnable = true;
|
||
}
|
||
|
||
if (UdsService.IsUdsFlg) {
|
||
udsInfo2Vehicle.UdsOffLine = false;
|
||
} else {
|
||
udsInfo2Vehicle.UdsOffLine = true;
|
||
#if BOOTLOADER_CODE_FLAG == 0
|
||
IO_InputOutputRelease();
|
||
#endif
|
||
}
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
#if (BOOTLOADER_CODE_FLAG == 1)
|
||
static void UDS_34_Service_Request_Download(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x34 == UdsService.Id)) {
|
||
//UDS_Service_34(0,0x44,UDS_AddrBuf,UDS_LengthBuf);
|
||
UDS_Service_34_Ack(&TP_Buffer[1],TP_Rx_Index-1);
|
||
UdsService.RequestFlg=0;
|
||
UdsService.Id=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
/*!
|
||
* @brief Demo Service Function to transfer data
|
||
* This function is used to transfer data
|
||
*/
|
||
static void UDS_36_Service_Transfer_Data(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x36 == UdsService.Id)) {
|
||
UDS_Service_36_Ack(UdsService.ReqCmdBuf,TP_Rx_Index-1);
|
||
UdsService.RequestFlg=0;
|
||
UdsService.Id=0;
|
||
memset(TP_Buffer,0x0,TP_BUFFER_SIZE);
|
||
}
|
||
}
|
||
/*!
|
||
* @brief Demo Service Function to exit transfer
|
||
* This function is used to exit transfer
|
||
*/
|
||
static void UDS_37_Service_Transfer_Exit(void)
|
||
{
|
||
if((UdsService.RequestFlg)&&(0x37 == UdsService.Id)) {
|
||
UDS_Service_37(&TP_Buffer[1],TP_Rx_Index-1);
|
||
UdsService.RequestFlg=0;
|
||
UdsService.Id=0;
|
||
}
|
||
}
|
||
#endif
|
||
|
||
static void UDS_Service_NRC(void)
|
||
{
|
||
if(UdsService.RequestFlg) {
|
||
UDS_Service_NonSupport_Response(UdsService.Id);
|
||
UdsService.RequestFlg=0;
|
||
UdsService.Id=0;
|
||
memset(TP_Buffer,0x0,255);
|
||
}
|
||
}
|
||
|
||
/*!
|
||
* @brief Demo Service Function to start service sequencce
|
||
* This function is used to start service sequencce
|
||
*/
|
||
void UDS_Demo_Services_Start(void)
|
||
{
|
||
//Service_Ctrl.index = 0;
|
||
Service_Ctrl.start = true;
|
||
UDS_Service_Init();
|
||
}
|
||
/*!
|
||
* @brief Demo Service Function to call the services in sequencce
|
||
* This function is used to call the services in sequencce
|
||
*/
|
||
void UDS_Demo_Services_Sequence(void)
|
||
{
|
||
//the functioni will be called in 10ms task
|
||
if(Service_Ctrl.start == true) {
|
||
if(Service_Ctrl.index < SERVICE_SEQUENCE_NUM) {
|
||
SERVICE_FUNC_T service = UDS_Demo_Service_Table[Service_Ctrl.index];
|
||
service();
|
||
} else {
|
||
Service_Ctrl.start = false;
|
||
}
|
||
}
|
||
}
|
||
/*!
|
||
* @brief Demo service function to calculate key according to seed.
|
||
* The function is used to calculate key according to seed.
|
||
*
|
||
* @param rsp_buf Response buf pointer
|
||
*/
|
||
|
||
void UDS_Demo_Calculate_Key(uint8_t* rsp_buf)
|
||
{
|
||
//request seed, store seed
|
||
UDS_SeedKeyBuf[0] = rsp_buf[2];
|
||
UDS_SeedKeyBuf[1] = rsp_buf[3];
|
||
|
||
//calculate key
|
||
UDS_SeedKeyBuf[0] = UDS_SeedKeyBuf[0]+1;
|
||
UDS_SeedKeyBuf[1] = UDS_SeedKeyBuf[1]+2;
|
||
|
||
}
|
||
/*!
|
||
* @brief Demo Service Function for positive response callback
|
||
* This function is used for positive response callback
|
||
*/
|
||
void UDS_Pos_Response_Callback(void)
|
||
{
|
||
Service_Ctrl.index++;
|
||
}
|
||
/*!
|
||
* @brief Demo Service Function for negtive response callback
|
||
* This function is used for negtive response callback
|
||
*/
|
||
void UDS_Neg_Response_Callback(void)
|
||
{
|
||
//Service_Ctrl.index = 0;
|
||
Service_Ctrl.start = false;
|
||
UDS_Service_Init();
|
||
}
|
||
|
||
|
||
|
||
|
||
bool CheckIsBeyonSession(INT8U CurSession,INT8U SelfSession,INT8U Type)
|
||
{
|
||
bool bRst=false;
|
||
if(Type) {
|
||
if(CurSession!=SelfSession)
|
||
bRst = true;
|
||
else
|
||
bRst = false;
|
||
} else {
|
||
if(CurSession == SelfSession)
|
||
bRst = true;
|
||
else
|
||
bRst = false;
|
||
}
|
||
return bRst;
|
||
}
|
||
bool CheckUdsDlcIsOk(INT16U RxDlc,INT16U rDlc)
|
||
{
|
||
bool bRst=false;
|
||
|
||
if( RxDlc == rDlc)
|
||
bRst = true;
|
||
|
||
return bRst;
|
||
}
|
||
bool CheckSubSidNeedPosResp(INT8U SubSID)
|
||
{
|
||
bool bRst=false;
|
||
|
||
if(!(SubSID&0x80))
|
||
bRst = true;
|
||
|
||
return bRst;
|
||
}
|
||
bool CheckIsKeyOk(void)
|
||
{
|
||
bool bRst=false;
|
||
|
||
if(1==UdsService.SeedKeyIsOK)
|
||
bRst = true;
|
||
return bRst;
|
||
}
|
||
|
||
bool CheckIsSupportBootloaderRead_Sid22(uint16_t did)
|
||
{
|
||
bool cbRst=false;
|
||
|
||
if (did == DIDId[TesterSerialNumber] \
|
||
|| did == DIDId[ProgrammingDate] \
|
||
|| did == DIDId[BootSoftware]) {
|
||
cbRst = true;
|
||
}
|
||
return cbRst;
|
||
}
|
||
|
||
bool CheckIsSupportBootloaderWrite_Sid2E(uint16_t did)
|
||
{
|
||
bool cbRst=false;
|
||
|
||
if (did == DIDId[TesterSerialNumber] \
|
||
|| did == DIDId[ProgrammingDate]) {
|
||
cbRst = true;
|
||
}
|
||
return cbRst;
|
||
}
|
||
|
||
bool CheckIsSupportAppWrite_Sid2E(uint16_t did)
|
||
{
|
||
bool cbRst=false;
|
||
|
||
if (did == DIDId[TesterSerialNumber] \
|
||
|| did == DIDId[ProgrammingDate] \
|
||
|| did == DIDId[VehicleIdentificationNumber]) {
|
||
cbRst = true;
|
||
}
|
||
return cbRst;
|
||
}
|
||
|
||
|
||
|
||
bool CheckIsSupportSubFunc_Sid19(uint8_t sub)
|
||
{
|
||
bool cbRst=false;
|
||
|
||
if (sub == 0x01 || sub == 0x02 || sub == 0x04 || sub == 0x06 || sub == 0x0A) {
|
||
cbRst = true;
|
||
}
|
||
return cbRst;
|
||
}
|
||
|
||
bool CheckIsSupportSubFunc_Sid11(uint8_t sub)
|
||
{
|
||
bool cbRst=false;
|
||
|
||
if (sub == 0x01 || sub == 0x03) {
|
||
cbRst = true;
|
||
}
|
||
return cbRst;
|
||
}
|