Commit 53981bdb authored by 李延凯's avatar 李延凯

feat: 增加诊断27服务

parent 5f9e98d4
...@@ -68,6 +68,7 @@ static void Power_KL30_Init(void) ...@@ -68,6 +68,7 @@ static void Power_KL30_Init(void)
g_stRTCInformation.u8RTCYear = 20; g_stRTCInformation.u8RTCYear = 20;
RTE_RTC_Init(g_stRTCInformation); RTE_RTC_Init(g_stRTCInformation);
Menu_User_Init(); Menu_User_Init();
UDS_Startup_Init();
Can_Init(); Can_Init();
Protocol_KL30_Wakeup_Init(); Protocol_KL30_Wakeup_Init();
TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 500); TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 500);
......
...@@ -203,11 +203,13 @@ void UDS_S_Data_Indication(uint16_t S_TAtype, uint8_t *S_Data, uint16_t S_Length ...@@ -203,11 +203,13 @@ void UDS_S_Data_Indication(uint16_t S_TAtype, uint8_t *S_Data, uint16_t S_Length
返回值:无 SYSC0_RUNPLL2CNTR 返回值:无 SYSC0_RUNPLL2CNTR
******************************************************************************/ ******************************************************************************/
extern uint8_t S3_Server_refresh; extern uint8_t S3_Server_refresh;
extern uint8_t S3_ServerEN;
extern uint8_t SessionType;
void UDS_Process_Service_Request(void) void UDS_Process_Service_Request(void)
{ {
if (UDS_APP_RX.A_PDU.New) if (UDS_APP_RX.A_PDU.New)
{ {
S3_Server_refresh = 1; S3_ServerEN = 0; // 收到消息时,暂停S3Timer
switch (UDS_APP_RX.A_PDU.A_PCI.SI) switch (UDS_APP_RX.A_PDU.A_PCI.SI)
{ {
case 0x10: UDS_Service_10_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data); case 0x10: UDS_Service_10_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
...@@ -218,10 +220,10 @@ void UDS_Process_Service_Request(void) ...@@ -218,10 +220,10 @@ void UDS_Process_Service_Request(void)
UDS_Service_22_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data); UDS_Service_22_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
} }
break; break;
/* case 0x2E: UDS_Service_2E_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x27: UDS_Service_27_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data); case 0x27: UDS_Service_27_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break; break;
/* case 0x2E: UDS_Service_2E_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x28: UDS_Service_28_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data); case 0x28: UDS_Service_28_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break; break;
case 0x85: UDS_Service_85_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data); case 0x85: UDS_Service_85_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
...@@ -239,6 +241,12 @@ void UDS_Process_Service_Request(void) ...@@ -239,6 +241,12 @@ void UDS_Process_Service_Request(void)
break; break;
} }
UDS_APP_RX.A_PDU.New = 0; UDS_APP_RX.A_PDU.New = 0;
if(SessionType > DefaultSession) // 非默认会话时执行
{
S3_Server_refresh = 1; // 诊断请求处理完成, 刷新S3Timer
S3_ServerEN = 1; // 消息处理完成,恢复S3Timer计时
}
} }
} }
......
...@@ -34,12 +34,20 @@ DiagSendDataNeg NegRes; ...@@ -34,12 +34,20 @@ DiagSendDataNeg NegRes;
uint8_t UDS_ISO14229_Transfer[250]; uint8_t UDS_ISO14229_Transfer[250];
DiagFlag DiagDataForFlag; DiagFlag DiagDataForFlag;
uint8_t Service27_DelayTimer_Enable;
uint8_t Service27_DelayTimer;
uint8_t Services27_Seed_Requested;
uint8_t DiagLockFlag;
uint32_t Ser27_SeedRandomNumber;
uint32_t GenerateKey;
uint8_t Attemptcnt;
/****************S3Timer********************/ /****************S3Timer********************/
uint8_t S3_ServerEN = 0; uint8_t S3_ServerEN = 0;
uint8_t S3_ServerCnt = 0; uint8_t S3_ServerCnt = 0;
uint8_t S3_Server_refresh = 0; uint8_t S3_Server_refresh = 0;
/****************DiagnosticSession*****************/ /****************DiagnosticSession*****************/
uint8_t SessionType = 0x01; uint8_t SessionType;
/****************************************************************************** /******************************************************************************
The service access point of the diagnostics application layer provides a number The service access point of the diagnostics application layer provides a number
of services that all have the same general structure. For each service, three of services that all have the same general structure. For each service, three
...@@ -110,13 +118,35 @@ void S3_ServerCNTT(void) ...@@ -110,13 +118,35 @@ void S3_ServerCNTT(void)
if ( S3_ServerCnt >= S3_SERVER ) if ( S3_ServerCnt >= S3_SERVER )
{ {
DIAG_InitParameter( ); UDS_Normal_Init( );
} }
else else
{ {
++S3_ServerCnt; ++S3_ServerCnt;
} }
} }
if(Service27_DelayTimer_Enable == 1U)
{
if(Service27_DelayTimer >= 100U)
{
Service27_DelayTimer_Enable = 0U;
Attemptcnt -= AttemptMaxCnt - 1U;
Service27_DelayTimer = 0U;
}
else
{
++Service27_DelayTimer;
}
}
}
void Ser27_CreateRandomSeed(void)
{
do
{
Ser27_SeedRandomNumber = Ser27_SeedRandomNumber * 1103515245ul + 12345u;
} while ((Ser27_SeedRandomNumber == 0U) || (Ser27_SeedRandomNumber == 0xFFFFFFFFU));
} }
uint8_t SerXXNRC12Filter(uint8_t insub, const uint8_t *supportsub, uint8_t size, uint8_t *index) uint8_t SerXXNRC12Filter(uint8_t insub, const uint8_t *supportsub, uint8_t size, uint8_t *index)
...@@ -339,12 +369,8 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -339,12 +369,8 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
UDS_ISO14229_Transfer[4] = 200; UDS_ISO14229_Transfer[4] = 200;
if (DefaultSession == SubFunction) if (DefaultSession == SubFunction)
{ {
S3_ServerEN = 0; UDS_Normal_Init();
SessionType = DefaultSession; SessionType = DefaultSession;
DIAG_InitParameter();
CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);//切会话解除
if (AbleResponseType == Ser10BanResponse) if (AbleResponseType == Ser10BanResponse)
{ {
...@@ -360,10 +386,6 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -360,10 +386,6 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
} }
else if (ProgrammingSession == SubFunction) else if (ProgrammingSession == SubFunction)
{ {
S3_ServerEN = 1;
S3_Server_refresh = 1;
SessionType = ProgrammingSession;
if (AbleResponseType == Ser10BanResponse) if (AbleResponseType == Ser10BanResponse)
{ {
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7)) if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
...@@ -372,22 +394,17 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -372,22 +394,17 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code)); UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
Write_App_InValid(Jump_To_Boot_Need_Answer); Write_App_InValid(Jump_To_Boot_Need_Answer);
for (i = 0; i < 250; ++i)
{
__NOP();
}
__NVIC_SystemReset();
} }
else else
{ {
Write_App_InValid(Jump_To_Boot_No_Answer); Write_App_InValid(Jump_To_Boot_No_Answer);
for (i = 0; i < 250; ++i)
{
__NOP();
}
__NVIC_SystemReset();
} }
for (i = 0; i < 250; ++i)
{
__NOP();
}
__NVIC_SystemReset();
} }
else else
{ {
...@@ -403,8 +420,7 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -403,8 +420,7 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
} }
else if (ExtendedDiagnosticSession == SubFunction) else if (ExtendedDiagnosticSession == SubFunction)
{ {
S3_ServerEN = 1; UDS_Normal_Init();
S3_Server_refresh = 1;
SessionType = ExtendedDiagnosticSession; SessionType = ExtendedDiagnosticSession;
if (AbleResponseType == Ser10BanResponse) if (AbleResponseType == Ser10BanResponse)
...@@ -428,14 +444,12 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -428,14 +444,12 @@ void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
{ {
NegRes.code = NrcCode; NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code)); UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
} }
} }
else else
{ {
NegRes.code = NrcCode; NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code)); UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
} }
} }
} }
...@@ -700,6 +714,175 @@ void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -700,6 +714,175 @@ void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
} }
} }
void UDS_Service_27_Indication(uint32_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint16_t i;
uint8_t NRC;
uint32_t KeyReceive;
uint8_t Seed_Array[4];
uint8_t SID = SecurityAccess;
uint8_t SubFunction = 0U;
uint16_t Data_Length = A_Length;
uint32_t Rx_ID = A_TA_type;
/* do not support functional address */
if (Rx_ID == DIAG_ID_Rx_FUN)
{
NRC = serviceNotSupported;
}
else
{
NRC = POSITIVE_RSP;
}
/* NRC7F */
if (0U == NRC)
{
if (SessionType != ProgrammingSession)
{
NRC = serviceNotSupportedInActiveSession;
}
}
/* NRC13 */
if (0U == NRC)
{
if (Data_Length < 1U)
{
NRC = incorrectMessageLength;
}
}
/* NRC12 */
if (0U == NRC)
{
for (i = 0; i < Data_Length; i++)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
SubFunction = UDS_ISO14229_Transfer[0];
if ((SubFunction != requestSeed_LV1) && (SubFunction != sendKey_LV1))
{
NRC = subFunctionNotSupported;
}
}
/* NRC13 */
if (0U == NRC)
{
if (SubFunction == requestSeed_LV1)
{
if (Data_Length != 1U)
{
NRC = incorrectMessageLength;
}
}
else
{
if (Data_Length != 5U)
{
NRC = incorrectMessageLength;
}
}
}
/* NRC24 */
if (0U == NRC)
{
if (SubFunction == sendKey_LV1)
{
if (Services27_Seed_Requested != 1U)
{
NRC = requestSequenceError;
}
}
}
/* NRC37 */
if (0U == NRC)
{
if (SubFunction == requestSeed_LV1)
{
if (Service27_DelayTimer_Enable == 1)
{
NRC = requiredTimeDelayNotExpired;
}
}
}
if (0U == NRC)
{
switch (SubFunction)
{
case requestSeed_LV1:
if (DiagLockFlag == BootKeyUnlock)
{
UDS_ISO14229_Transfer[0] = requestSeed_LV1;
UDS_ISO14229_Transfer[1] = 0;
UDS_ISO14229_Transfer[2] = 0;
UDS_ISO14229_Transfer[3] = 0;
UDS_ISO14229_Transfer[4] = 0;
}
else
{
Ser27_CreateRandomSeed();
UDS_ISO14229_Transfer[1] = (uint8_t)(Ser27_SeedRandomNumber >> 24U);
UDS_ISO14229_Transfer[2] = (uint8_t)(Ser27_SeedRandomNumber >> 16U);
UDS_ISO14229_Transfer[3] = (uint8_t)(Ser27_SeedRandomNumber >> 8U);
UDS_ISO14229_Transfer[4] = (uint8_t)Ser27_SeedRandomNumber;
Services27_Seed_Requested = 1;
}
UDS_Service_Response(SID, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
Seed_Array[0] = UDS_ISO14229_Transfer[1];
Seed_Array[1] = UDS_ISO14229_Transfer[2];
Seed_Array[2] = UDS_ISO14229_Transfer[3];
Seed_Array[3] = UDS_ISO14229_Transfer[4];
GenerateKey = CalculateKey(Seed_Array);
break;
case sendKey_LV1:
Services27_Seed_Requested = 0U;
KeyReceive = Combine_U8_Into_U32(&UDS_ISO14229_Transfer[1]);
if (KeyReceive == GenerateKey)
{
DiagLockFlag = BootKeyUnlock;
UDS_Service_Response(SID, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
else
{
Attemptcnt++;
if (Attemptcnt >= 3)
{
Service27_DelayTimer_Enable = 1U;
NRC = exceedNumberOfAttempts;
UDS_Service_Response(SID, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NRC));
}
else
{
NRC = invalidKey;
UDS_Service_Response(SID, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NRC));
}
}
break;
default:
NRC = subFunctionNotSupported;
UDS_Service_Response(SID, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NRC));
break;
}
}
else
{
if(Rx_ID == DIAG_ID_Rx_PHY)
{
UDS_Service_Response(SID, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NRC));
}
}
}
/****************************************************************************** /******************************************************************************
3E# - TesterPresent 3E# - TesterPresent
******************************************************************************/ ******************************************************************************/
...@@ -800,12 +983,28 @@ void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A ...@@ -800,12 +983,28 @@ void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A
} }
} }
void UDS_Normal_Init(void)
{
Services27_Seed_Requested = 0U;
DiagLockFlag = BootKeyLock;
GenerateKey = 0U;
}
/*************IGN on,S3 Timeout****************/ /*************IGN on,S3 Timeout****************/
void DIAG_InitParameter(void) void UDS_Startup_Init(void)
{ {
S3_ServerCnt = 0; S3_ServerCnt = 0;
S3_ServerEN = 0; S3_ServerEN = 0;
S3_Server_refresh = 0;
SessionType = DefaultSession; SessionType = DefaultSession;
Ser27_SeedRandomNumber = 20240728U;
Services27_Seed_Requested = 0U;
DiagLockFlag = BootKeyLock;
GenerateKey = 0U;
Attemptcnt = 0U;
Service27_DelayTimer = 0U;
Service27_DelayTimer_Enable = 0U;
} }
void Get_Into_630H_Update_MCU_Mode(void) void Get_Into_630H_Update_MCU_Mode(void)
...@@ -818,3 +1017,39 @@ void Get_Into_630H_Update_MCU_Mode(void) ...@@ -818,3 +1017,39 @@ void Get_Into_630H_Update_MCU_Mode(void)
} }
__NVIC_SystemReset(); __NVIC_SystemReset();
} }
uint32_t CalculateKey(uint8_t seed_array[])
{
uint8_t key_array[4];
uint32_t key_value;
key_array[0] = seed_array[0] | seed_array[3];
key_array[1] = (seed_array[1] >> 1) | (seed_array[2] << 1);
key_array[2] = (seed_array[2] >> 2) | (seed_array[1] << 2);
key_array[3] = (seed_array[3] >> 3) | (seed_array[0] << 3);
key_value = ((uint32_t)key_array[0] << 24U);
key_value = key_value | ((uint32_t)key_array[1] << 16U);
key_value = key_value | ((uint32_t)key_array[2] << 8U);
key_value = key_value | (uint32_t)key_array[3];
return (key_value);
}
/**
* @brief 按照传入的8bit数据的指针,向后查找4个8bit数据,按照顺序组合成一个32bit数据
*/
uint32_t Combine_U8_Into_U32(uint8_t *U8)
{
uint32_t ret = 0U;
uint32_t d_temp;
d_temp = (uint32_t)U8[0];
ret = (d_temp << 24) & 0xFF000000UL;
d_temp = (uint32_t)U8[1];
ret = ret | ((d_temp << 16) & 0x00FF0000UL);
d_temp = (uint32_t)U8[2];
ret = ret | ((d_temp << 8) & 0x0000FF00UL);
d_temp = (uint32_t)U8[3];
ret = ret | (d_temp & 0x000000FFUL);
return (ret);
}
...@@ -341,9 +341,12 @@ typedef struct ...@@ -341,9 +341,12 @@ typedef struct
void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data); void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_11_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data); void UDS_Service_11_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data); void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_27_Indication(uint32_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data); void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void S3_ServerCNTT(void); void S3_ServerCNTT(void);
extern void DIAG_InitParameter(void);
extern void UDS_Normal_Init(void);
extern void UDS_Startup_Init(void);
typedef void (*InitFunction)(void); typedef void (*InitFunction)(void);
...@@ -360,4 +363,8 @@ void Write_App_InValid(uint32_t m32); ...@@ -360,4 +363,8 @@ void Write_App_InValid(uint32_t m32);
extern void Get_Into_630H_Update_MCU_Mode(void); extern void Get_Into_630H_Update_MCU_Mode(void);
extern void Check_MCU_Info_in_630H(void); extern void Check_MCU_Info_in_630H(void);
extern void Ser27_CreateRandomSeed(void);
extern uint32_t CalculateKey(uint8_t seed_array[]);
extern uint32_t Combine_U8_Into_U32(uint8_t *U8);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment