This commit is contained in:
刘可亮
2025-07-22 11:15:46 +08:00
parent d164b333ed
commit 11c97ef399
2870 changed files with 951307 additions and 26675 deletions

View File

@@ -0,0 +1,120 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID10_SessionControl.h"
#include "service_cfg.h"
#include "SID27_SecurityAccess.h"
// 收到服务请求到做出响应之间的最大时间单位ms
#define P2_SERVER 50
// 回复 NRC 0x78 后到真正的正响应之间的最大时间单位ms接收方接收到这个时间后要 x10
#define P2X_SERVER 400
// 诊断会话状态
static uds_session_t uds_session = UDS_SESSION_STD;
/******************************************************************************
* 函数名称: void set_current_session(uds_session_t session)
* 功能说明: 设置当前诊断会话状态
* 输入参数: uds_session_t session --会话状态
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void set_current_session(uds_session_t session)
{
uds_session = session;
}
/******************************************************************************
* 函数名称: uds_session_t get_current_session(void)
* 功能说明: 获取当前诊断会话状态
* 输入参数: 无
* 输出参数: 无
* 函数返回: 当前会话状态
* 其它说明: 无
******************************************************************************/
uds_session_t get_current_session(void)
{
return uds_session;
}
/******************************************************************************
* 函数名称: bool_t service_10_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 10 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_10_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(2 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_10_SessionControl(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 10 服务 - 诊断会话控制
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_10_SessionControl(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction = 0;
uint8_t rsp_buf[8];
subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_10);
rsp_buf[1] = subfunction;
rsp_buf[2] = (uint8_t)(P2_SERVER >> 8);
rsp_buf[3] = (uint8_t)(P2_SERVER & 0x00ff);
rsp_buf[4] = (uint8_t)(P2X_SERVER >> 8);
rsp_buf[5] = (uint8_t)(P2X_SERVER & 0x00ff);
switch (subfunction)
{
case UDS_SESSION_STD: // 默认会话
set_current_session((uds_session_t)subfunction);
set_current_sa_lv(UDS_SA_NON);
uds_positive_rsp(rsp_buf, 6);
break;
case UDS_SESSION_PROG: // 编程会话
set_current_session((uds_session_t)subfunction);
set_current_sa_lv(UDS_SA_NON);
uds_positive_rsp(rsp_buf, 6);
uds_timer_start(UDS_TIMER_S3server);
break;
case UDS_SESSION_EXT: // 扩展会话
set_current_session((uds_session_t)subfunction);
set_current_sa_lv(UDS_SA_NON);
uds_positive_rsp(rsp_buf, 6);
uds_timer_start(UDS_TIMER_S3server);
break;
default:
uds_negative_rsp(SID_10, NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,60 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID2E_SESSION_CONTROL_H_
#define _SID2E_SESSION_CONTROL_H_
#include <stdint.h>
#include "uds_def.h"
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_10_SessionControl(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 10 服务 - 诊断会话控制
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_10_SessionControl(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_10_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 10 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_10_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: void set_current_session(uds_session_t session)
* 功能说明: 设置当前诊断会话状态
* 输入参数: uds_session_t session --会话状态
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void set_current_session(uds_session_t session);
/******************************************************************************
* 函数名称: uds_session_t get_current_session(void)
* 功能说明: 读取当前诊断会话状态
* 输入参数: 无
* 输出参数: 无
* 函数返回: 当前会话状态
* 其它说明: 无
******************************************************************************/
uds_session_t get_current_session(void);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,82 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID11_EcuReset.h"
#include "service_cfg.h"
#include "uds_def.h"
typedef enum __UDS_RESET_T_
{
UDS_RESET_NONE = 0,
UDS_RESET_HARD,
UDS_RESET_KEYOFFON,
UDS_RESET_SOFT
}uds_reset_t;
/******************************************************************************
* 函数名称: bool_t service_11_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 11 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_11_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(2 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_11_EcuReset(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 11 服务 - ECU 重启
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_11_EcuReset(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[8];
subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
switch (subfunction)
{
case UDS_RESET_HARD:
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_11);
rsp_buf[1] = UDS_RESET_HARD;
uds_positive_rsp(rsp_buf, 2);
// Wait a moment for hard reset
// Add hard reset here
break;
case UDS_RESET_SOFT:
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_11);
rsp_buf[1] = UDS_RESET_SOFT;
uds_positive_rsp(rsp_buf, 2);
// Wait a moment for hard reset
// Add soft reset here
break;
default:
uds_negative_rsp(SID_11, NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,39 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID11_ECU_RESET_H_
#define _SID11_ECU_RESET_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_11_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 11 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_11_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: void service_11_EcuReset(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 11 服务 - ECU 重启
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_11_EcuReset(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,65 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID14_ClearDiagnosticInformation.h"
#include "service_cfg.h"
#include "uds_def.h"
#define UDS_DTC_GROUP_ALL 0xFFFFFF
/******************************************************************************
* 函数名称: bool_t service_14_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 14 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_14_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(4 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_14_ClearDiagnosticInformation(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 14 服务 - 清除诊断信息
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_14_ClearDiagnosticInformation(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t rsp_buf[8];
uint32_t dtc_group = 0;
dtc_group = 0;
dtc_group |= ((uint32_t)msg_buf[1]) << 16;
dtc_group |= ((uint32_t)msg_buf[2]) << 8;
dtc_group |= ((uint32_t)msg_buf[3]) << 0;
if (dtc_group == UDS_DTC_GROUP_ALL)
{
// clear_dtc_by_group (dtc_group);
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_14);
uds_positive_rsp(rsp_buf, 1);
}
else
{
uds_negative_rsp(SID_14, NRC_REQUEST_OUT_OF_RANGE);
}
}
/****************EOF****************/

View File

@@ -0,0 +1,40 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID14_CLEAR_DIAGNOSTIC_INFORMATION_H_
#define _SID14_CLEAR_DIAGNOSTIC_INFORMATION_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_14_ClearDiagnosticInformation(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 14 服务 - 清除诊断信息
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_14_ClearDiagnosticInformation(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_14_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 14 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_14_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,75 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID19_ReadDTCInformation.h"
#include "service_cfg.h"
#include "uds_def.h"
#define REPORT_DTC_NUMBER_BY_STATUS_MASK (0x01)
#define REPORT_DTC_BY_STATUS_MASK (0x02)
#define REPORT_DTC_SNOPSHOT_BY_DTC_NUMBER (0x04)
#define REPORT_DTC_EXTENDED_DATA_RECORD_BY_DTC_NUMBER (0x06)
#define REPORT_SUPPORTED_DTC (0x0a)
/******************************************************************************
* 函数名称: bool_t service_19_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 19 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_19_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
return ret;
}
/******************************************************************************
* 函数名称: void service_19_ReadDTCInformation(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 19 服务 - 读取故障码信息
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_19_ReadDTCInformation(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[64];
subfunction = UDS_GET_SUB_FUNCTION (msg_buf[1]);
switch (subfunction)
{
case REPORT_DTC_NUMBER_BY_STATUS_MASK:
{
uint16_t dtc_count;
dtc_count = 5; // example
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_19);
rsp_buf[1] = subfunction;
rsp_buf[2] = 0x7F;
rsp_buf[3] = 1; // 14229
rsp_buf[4] = (uint8_t)dtc_count;
rsp_buf[5] = (dtc_count >> 8) & 0xFF;
uds_positive_rsp (rsp_buf, 6);
break;
}
default:
uds_negative_rsp(SID_19, NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,41 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID19_READ_DTC_INFORMATION_H_
#define _SID19_READ_DTC_INFORMATION_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_19_ReadDTCInformation(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 19 服务 - 读取故障码信息
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_19_ReadDTCInformation(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_19_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 19 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_19_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,69 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID22_ReadDataByIdentifier.h"
#include "service_cfg.h"
#include "uds_def.h"
#include "SID10_SessionControl.h"
/******************************************************************************
* 函数名称: bool_t service_22_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 22 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_22_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(3 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_22_ReadDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 22 服务 - 根据标识符读取数据
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_22_ReadDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t rsp_buf[128];
uint16_t did;
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_22);
rsp_buf[1] = msg_buf[1];
rsp_buf[2] = msg_buf[2];
did = ((uint16_t)msg_buf[1]) << 8;
did |= msg_buf[2];
switch (did)
{
case 0xF186:
rsp_buf[3] = get_current_session();
uds_positive_rsp(rsp_buf, 4);
break;
default:
uds_negative_rsp(SID_22, NRC_REQUEST_OUT_OF_RANGE);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,39 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID22_READ_DATA_BY_IDENTIFIER_H_
#define _SID22_READ_DATA_BY_IDENTIFIER_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_22_ReadDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 22 服务 - 根据标识符读取数据
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_22_ReadDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_22_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 22 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_22_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,229 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID27_SecurityAccess.h"
#include "service_cfg.h"
#include "uds_def.h"
#include <stdlib.h>
#include <time.h>
#define UNLOCKKEY 0x00000000
#define UNLOCKSEED 0x00000000
#define UNDEFINESEED 0xFFFFFFFF
#define SEEDMASK 0x80000000
#define SHIFTBIT 1
#define ALGORITHMASK 0x42303131
#define UDS_SEED_LENGTH (0x04)
#define UDS_REQUEST_SEED (0x01)
#define UDS_SEND_KEY (0x02)
#define UDS_FAS_MAX_TIMES (0x02) /* failed security access */
static uint8_t req_seed = 0; // 接收到请求种子标志
static uint8_t org_seed_buf[UDS_SEED_LENGTH];
// 当前安全访问等级
static uds_sa_lv curr_sa = UDS_SA_NON;
// 安全访问种子匹配错误次数
uint8_t uds_fsa_cnt = 0;
/******************************************************************************
* 函数名称: void set_current_sa_lv(uds_sa_lv level)
* 功能说明: 设置当前安全访问等级
* 输入参数: uds_sa_lv level --安全访问等级
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void set_current_sa_lv(uds_sa_lv level)
{
curr_sa = level;
}
/******************************************************************************
* 函数名称: uds_session_t get_current_sa_lv(void)
* 功能说明: 获取当前安全访问等级
* 输入参数: 无
* 输出参数: 无
* 函数返回: 当前安全访问等级
* 其它说明: 无
******************************************************************************/
uds_sa_lv get_current_sa_lv(void)
{
return curr_sa;
}
/******************************************************************************
* 函数名称: static uint8_t rand_u8 (void)
* 功能说明: 获取随机数
* 输入参数: 无
* 输出参数: 无
* 函数返回: 8 位随机数
* 其它说明: 无
******************************************************************************/
static uint8_t rand_u8(void)
{
static uint8_t initialized = 0;
if (!initialized) {
srand(time(NULL));
initialized = 1;
}
return rand() % 0xFF;
}
/******************************************************************************
* 函数名称: static uint32_t seedTOKey(uint32_t seed)
* 功能说明: 安全访问算法
* 输入参数: uint32_t seed --种子
* 输出参数: 无
* 函数返回: key 值
* 其它说明: 该算法需根据实际需求而定
******************************************************************************/
static uint32_t seedTOKey(uint32_t seed)
{
return (~seed);
}
/******************************************************************************
* 函数名称: int uds_security_access(uint8_t* key_buf, uint8_t* seed_buf)
* 功能说明: 比较自己根据种子 seed 计算的 key 值与接收到的 key 值是否一致
* 输入参数: uint8_t* key_buf --接收到的 key
    uint8_t* seed_buf --种子
* 输出参数: 无
* 函数返回: 0: 一致; -1: 不一致
* 其它说明: 无
******************************************************************************/
int uds_security_access(uint8_t* key_buf, uint8_t* seed_buf)
{
uint32_t key = 0;
uint32_t seed = 0;
key = (key_buf[0] << 24) | (key_buf[1] << 16) | (key_buf[2] << 8) | key_buf[3];
seed = (seed_buf[0] << 24) | (seed_buf[1] << 16) | (seed_buf[2] << 8) | seed_buf[3];
if (key == seedTOKey(seed))
return 0;
else
return -1;
}
/******************************************************************************
* 函数名称: bool_t service_27_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 27 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_27_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
uint8_t subfunction;
subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
if ((UDS_REQUEST_SEED == subfunction && 2 == msg_dlc)
|| (UDS_SEND_KEY == subfunction && 6 == msg_dlc))
{
ret = TRUE;
}
return ret;
}
/******************************************************************************
* 函数名称: void service_27_SecurityAccess(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 27 服务 - 安全访问
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_27_SecurityAccess(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[8];
uint16_t i;
subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
switch (subfunction)
{
case UDS_REQUEST_SEED: // 请求种子
{
// 锁定时间要求不能因模块断电被清零,这里暂未实现掉电保存的功能
if (uds_timer_chk(UDS_TIMER_FSA) > 0)
{
uds_negative_rsp(SID_27, NRC_REQUIRED_TIME_DELAY_NOT_EXPIRED);
break;
}
req_seed = 1;
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_27);
rsp_buf[1] = subfunction;
for (i = 0; i < UDS_SEED_LENGTH; i++)
{
// ECU 在已经解锁的情况下,如果再次收到请求种子,则返回种子 0x00000000
if (curr_sa == UDS_SA_LV1)
org_seed_buf[i] = 0;
else
org_seed_buf[i] = rand_u8();
rsp_buf[2+i] = org_seed_buf[i];
}
uds_positive_rsp (rsp_buf, UDS_SEED_LENGTH+2);
break;
}
case UDS_SEND_KEY: // 发送密钥
{
// 在发送秘钥前必须先请求种子
if (req_seed == 0)
{
uds_negative_rsp(SID_27, NRC_REQUEST_SEQUENCE_ERROR);
break;
}
req_seed = 0;
// 判断发送过来的密钥和自己计算的密钥是否一致
if (!uds_security_access((uint8_t *)&msg_buf[2], org_seed_buf))
{
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_27);
rsp_buf[1] = subfunction;
uds_positive_rsp (rsp_buf,2);
set_current_sa_lv(UDS_SA_LV1);
}
else
{
uds_fsa_cnt++;
if (uds_fsa_cnt >= UDS_FAS_MAX_TIMES)
{
// 密钥尝试次数超过限值
uds_timer_start (UDS_TIMER_FSA); // 锁定时间要求不能因模块断电被清零,这里暂未实现掉电保存的功能
uds_negative_rsp (SID_27, NRC_EXCEEDED_NUMBER_OF_ATTEMPTS);
} else
{
// 密钥无效
uds_negative_rsp (SID_27, NRC_INVALID_KEY);
}
}
break;
}
default:
uds_negative_rsp (SID_27, NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,64 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID27_SECURITY_ACCESS_H_
#define _SID27_SECURITY_ACCESS_H_
#include <stdint.h>
#include "uds_def.h"
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_27_SecurityAccess(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 27 服务 - 安全访问
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_27_SecurityAccess(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_27_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 27 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_27_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: void set_current_sa_lv(uds_sa_lv level)
* 功能说明: 设置当前安全访问等级
* 输入参数: uds_sa_lv level --安全访问等级
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void set_current_sa_lv(uds_sa_lv level);
/******************************************************************************
* 函数名称: uds_session_t get_current_sa_lv(void)
* 功能说明: 获取当前安全访问等级
* 输入参数: 无
* 输出参数: 无
* 函数返回: 当前安全访问等级
* 其它说明: 无
******************************************************************************/
uds_sa_lv get_current_sa_lv(void);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,103 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID28_CommunicationControl.h"
#include "service_cfg.h"
#include "uds_def.h"
/* uds Communication control type */
typedef enum __UDS_CC_TYPE__
{
UDS_CC_TYPE_NONE = 0,
UDS_CC_TYPE_NORMAL,
UDS_CC_TYPE_NM,
UDS_CC_TYPE_NM_NOR
}uds_cc_type;
bool_t dis_normal_xmit; // 禁止发送标志
bool_t dis_normal_recv; // 禁止接收标志
/******************************************************************************
* 函数名称: bool_t service_28_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 28 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_28_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(3 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_28_CommunicationControl(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 28 服务 - 通讯控制
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_28_CommunicationControl(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[8];
uint8_t cc_type;
subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
cc_type = msg_buf[2];
switch (subfunction)
{
case UDS_CC_MODE_RX_TX: // 使能接收和发送
if (cc_type == UDS_CC_TYPE_NORMAL || cc_type == UDS_CC_TYPE_NM || cc_type == UDS_CC_TYPE_NM_NOR)
{
dis_normal_xmit = FALSE;
dis_normal_recv = FALSE;
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_28);
rsp_buf[1] = subfunction;
uds_positive_rsp(rsp_buf, 2);
}
else
{
uds_negative_rsp(SID_28, NRC_REQUEST_OUT_OF_RANGE);
}
break;
case UDS_CC_MODE_NO_NO: // 禁止接收、禁止发送
if (cc_type == UDS_CC_TYPE_NORMAL || cc_type == UDS_CC_TYPE_NM || cc_type == UDS_CC_TYPE_NM_NOR)
{
dis_normal_xmit = TRUE;
dis_normal_recv = TRUE;
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_28);
rsp_buf[1] = subfunction;
uds_positive_rsp(rsp_buf, 2);
}
else
{
uds_negative_rsp(SID_28, NRC_REQUEST_OUT_OF_RANGE);
}
break;
default:
uds_negative_rsp(SID_28, NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,49 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID28_COMMUNICATION_CONTROL_H_
#define _SID28_COMMUNICATION_CONTROL_H_
#include <stdint.h>
#include "uds_def.h"
typedef enum __UDS_CC_MODE__
{
UDS_CC_MODE_RX_TX = 0, // 使能接收和发送
UDS_CC_MODE_RX_NO, // 使能接收、禁止发送
UDS_CC_MODE_NO_TX, // 禁止接收、使能发送
UDS_CC_MODE_NO_NO // 禁止接收、禁止发送
}uds_cc_mode;
/******************************************************************************
* 函数名称: void service_28_CommunicationControl(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 28 服务 - 通讯控制
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_28_CommunicationControl(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_28_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 28 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_28_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,69 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID2E_WriteDataByIdentifier.h"
#include "service_cfg.h"
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_2E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 2E 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_2E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(msg_dlc > 4)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_2E_WriteDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 2E 服务 - 根据标识符写入数据
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_2E_WriteDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t rsp_buf[8];
uint16_t did;
bool_t write_ret = 0;
did = ((uint16_t)msg_buf[1]) << 8;
did |= msg_buf[2];
switch(did)
{
case 0x1234:
// write_ret = eeprom_write(xxx, xxx);
if(write_ret)
{
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_2E);
rsp_buf[1] = msg_buf[1];
rsp_buf[2] = msg_buf[2];
uds_positive_rsp(rsp_buf, 3);
}
else
{
uds_negative_rsp(SID_2E, NRC_GENERAL_PROGRAMMING_FAILURE);
}
break;
default:
uds_negative_rsp(SID_2E, NRC_REQUEST_OUT_OF_RANGE);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,37 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID2E_WRITE_DATA_BY_IDENTIFIER_H_
#define _SID2E_WRITE_DATA_BY_IDENTIFIER_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_2E_WriteDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 2E 服务 - 根据标识符写入数据
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_2E_WriteDataByIdentifier(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_2E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 2E 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_2E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,151 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID31_RoutineControl.h"
#include "service_cfg.h"
#include "uds_def.h"
/*
* 31 01 ff 00 擦除内存
* 31 01 02 02 检查传输数据完整性
* 31 01 02 03 更新条件查询
* 31 01 ff 01 软硬件一致性检查
* 31 01 df e0 从节点模式选择
* 31 01 02 04 数字签名
*/
typedef enum __UDS_ROUTINE_CTRL_TYPE__
{
UDS_ROUTINE_CTRL_NONE = 0,
UDS_ROUTINE_CTRL_START = 0x01,
UDS_ROUTINE_CTRL_STOP = 0x02,
UDS_ROUTINE_CTRL_REQUEST_RESULT = 0x03
}uds_routine_ctrl_type;
/******************************************************************************
* 函数名称: bool_t service_31_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 31 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_31_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(msg_dlc > 4)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_31_RoutineControl(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 31 服务 - 例程控制
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_31_RoutineControl(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[8];
uint16_t rid;
// uint16_t rid_n;
bool_t find_rid;
subfunction = UDS_GET_SUB_FUNCTION (msg_buf[1]);
rid = ((uint16_t)msg_buf[2]) << 8;
rid |= msg_buf[3];
find_rid = FALSE;
// for (rid_n = 0; rid_n < RTCTRL_NUM; rid_n++)
// {
// if (rtctrl_list[rid_n].rid == rid)
// {
// find_rid = TRUE;
// break;
// }
// }
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_31);
rsp_buf[1] = msg_buf[1];
rsp_buf[2] = msg_buf[2];
rsp_buf[3] = msg_buf[3];
switch (subfunction)
{
case UDS_ROUTINE_CTRL_START:
if (find_rid == TRUE)
{
// if (rtctrl_list[rid_n].rtst == UDS_RT_ST_RUNNING)
if(1)
{
uds_negative_rsp (SID_31,NRC_REQUEST_SEQUENCE_ERROR);
}
else
{
// rtctrl_list[rid_n].init_routine ();
uds_positive_rsp (rsp_buf,4);
}
}
else
{
uds_negative_rsp (SID_31,NRC_REQUEST_OUT_OF_RANGE);
}
break;
case UDS_ROUTINE_CTRL_STOP:
if (find_rid == TRUE)
{
// if (rtctrl_list[rid_n].rtst == UDS_RT_ST_IDLE)
if(1)
{
uds_negative_rsp (SID_31,NRC_REQUEST_SEQUENCE_ERROR);
}
else
{
// rtctrl_list[rid_n].stop_routine ();
uds_positive_rsp (rsp_buf,4);
}
}
else
{
uds_negative_rsp (SID_31,NRC_REQUEST_OUT_OF_RANGE);
}
break;
case UDS_ROUTINE_CTRL_REQUEST_RESULT:
if (find_rid == TRUE)
{
// if (rtctrl_list[rid_n].rtst == UDS_RT_ST_IDLE)
if(1)
{
uds_negative_rsp (SID_31,NRC_REQUEST_SEQUENCE_ERROR);
}
else
{
// rsp_buf[4] = (uint8_t)rtctrl_list[rid_n].rtst;
}
}
else
{
uds_negative_rsp (SID_31,NRC_REQUEST_OUT_OF_RANGE);
}
break;
default:
uds_negative_rsp (SID_31, NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,39 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID31_ROUTINE_CONTROL_H_
#define _SID31_ROUTINE_CONTROL_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_31_RoutineControl(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 31 服务 - 例程控制
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_31_RoutineControl(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_31_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 31 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_31_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,57 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID34_RequestDownload.h"
#include "service_cfg.h"
#include "uds_def.h"
// 36 服务数据传输报文总大小
#define TOTAL_LEN_36 (512 + 2)
/******************************************************************************
* 函数名称: bool_t service_34_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 34 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_34_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_34_RequestDownload(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 34 服务 - 请求下载
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_34_RequestDownload(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t rsp_buf[8];
// 这里需要解析 34 服务报文
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_34);
rsp_buf[1] = 0x20;
rsp_buf[2] = (uint8_t)(TOTAL_LEN_36 >> 8);
rsp_buf[3] = (uint8_t)(TOTAL_LEN_36 >> 0);
uds_positive_rsp(rsp_buf, 4);
}
/****************EOF****************/

View File

@@ -0,0 +1,39 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID34_REQUEST_DOWNLOAD_H_
#define _SID34_REQUEST_DOWNLOAD_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_34_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 34 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_34_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: void service_34_RequestDownload(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 34 服务 - 请求下载
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_34_RequestDownload(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,54 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID36_TransferData.h"
#include "service_cfg.h"
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_36_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 36 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_36_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_36_TransferData(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 36 服务 - 数据传输
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_36_TransferData(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t rsp_buf[8];
uint8_t bn = 0;
// 这里需要解析 36 服务报文
bn = msg_buf[1];
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_36);
rsp_buf[1] = bn;
uds_positive_rsp(rsp_buf, 2);
}
/****************EOF****************/

View File

@@ -0,0 +1,39 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID36_TRANSFER_DATA_H_
#define _SID36_TRANSFER_DATA_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_36_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 36 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_36_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: void service_36_TransferData(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 36 服务 - 数据传输
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_36_TransferData(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,49 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID37_RequestTransferExit.h"
#include "service_cfg.h"
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_37_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 37 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_37_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_37_RequestTransferExit(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 37 服务 - 请求退出传输
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_37_RequestTransferExit(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t rsp_buf[8];
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_37);
uds_positive_rsp(rsp_buf, 1);
}
/****************EOF****************/

View File

@@ -0,0 +1,39 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID37_REQUEST_TRANSFER_EXIT_H_
#define _SID37_REQUEST_TRANSFER_EXIT_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: bool_t service_37_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 37 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_37_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: void service_37_RequestTransferExit(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 37 服务 - 请求退出传输
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_37_RequestTransferExit(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,62 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID3E_TesterPresent.h"
#include "service_cfg.h"
#include "uds_def.h"
#define ZERO_SUBFUNCTION (0x00)
/******************************************************************************
* 函数名称: bool_t service_3E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 3E 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_3E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(2 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_3E_TesterPresent(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 3E 服务 - 待机握手
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_3E_TesterPresent(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[8];
subfunction = UDS_GET_SUB_FUNCTION (msg_buf[1]);
if (subfunction == ZERO_SUBFUNCTION)
{
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_3E);
rsp_buf[1] = subfunction;
uds_positive_rsp (rsp_buf,2);
}
else
{
uds_negative_rsp(SID_3E, NRC_SUBFUNCTION_NOT_SUPPORTED);
}
}
/****************EOF****************/

View File

@@ -0,0 +1,37 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID2E_TEST_PRESENT_H_
#define _SID2E_TEST_PRESENT_H_
#include <stdint.h>
#include "uds_def.h"
/******************************************************************************
* 函数名称: void service_3E_TesterPresent(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 3E 服务 - 待机握手
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_3E_TesterPresent(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_3E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 3E 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_3E_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,73 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "SID85_ControlDTCSetting.h"
#include "service_cfg.h"
#include "uds_def.h"
bool_t dtc_setting = UDS_DTC_SETTING_ON;
/******************************************************************************
* 函数名称: bool_t service_85_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 85 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_85_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
{
bool_t ret = FALSE;
(void)msg_buf;
if(2 == msg_dlc)
ret = TRUE;
return ret;
}
/******************************************************************************
* 函数名称: void service_85_ControlDTCSetting(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 85 服务 - 控制 DTC 设置
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_85_ControlDTCSetting(const uint8_t* msg_buf, uint16_t msg_dlc)
{
uint8_t subfunction;
uint8_t rsp_buf[8];
subfunction = UDS_GET_SUB_FUNCTION (msg_buf[1]);
switch (subfunction)
{
case UDS_DTC_SETTING_ON:
dtc_setting = UDS_DTC_SETTING_ON;
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_85);
rsp_buf[1] = subfunction;
uds_positive_rsp (rsp_buf,2);
break;
case UDS_DTC_SETTING_OFF:
dtc_setting = UDS_DTC_SETTING_OFF;
rsp_buf[0] = USD_GET_POSITIVE_RSP(SID_85);
rsp_buf[1] = subfunction;
uds_positive_rsp (rsp_buf,2);
break;
default:
uds_negative_rsp (SID_85,NRC_SUBFUNCTION_NOT_SUPPORTED);
break;
}
}
/****************EOF****************/

View File

@@ -0,0 +1,48 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SID85_CONTROL_DTC_SETTING_H_
#define _SID85_CONTROL_DTC_SETTING_H_
#include <stdint.h>
#include "uds_def.h"
/* uds DTC setting */
typedef enum __UDS_DTC_SETTING__
{
UDS_DTC_SETTING_NONE = 0,
UDS_DTC_SETTING_ON, // 使能故障码设置
UDS_DTC_SETTING_OFF // 禁止故障码设置
}uds_dtc_setting;
/******************************************************************************
* 函数名称: void service_85_ControlDTCSetting(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 85 服务 - 控制 DTC 设置
* 输入参数: uint8_t* msg_buf --数据首地址
    uint8_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: 无
* 其它说明: 无
******************************************************************************/
void service_85_ControlDTCSetting(const uint8_t* msg_buf, uint16_t msg_dlc);
/******************************************************************************
* 函数名称: bool_t service_85_check_len(const uint8_t* msg_buf, uint16_t msg_dlc)
* 功能说明: 检查 85 服务数据长度是否合法
* 输入参数: uint16_t msg_dlc --数据长度
* 输出参数: 无
* 函数返回: TRUE: 合法; FALSE: 非法
* 其它说明: 无
******************************************************************************/
bool_t service_85_check_len(const uint8_t* msg_buf, uint16_t msg_dlc);
#endif
/****************EOF****************/

View File

@@ -0,0 +1,51 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "service_cfg.h"
#include "uds_def.h"
#include "SID10_SessionControl.h"
#include "SID11_EcuReset.h"
#include "SID27_SecurityAccess.h"
#include "SID28_CommunicationControl.h"
#include "SID3E_TesterPresent.h"
#include "SID85_ControlDTCSetting.h"
#include "SID22_ReadDataByIdentifier.h"
#include "SID2E_WriteDataByIdentifier.h"
#include "SID14_ClearDiagnosticInformation.h"
#include "SID19_ReadDTCInformation.h"
#include "SID31_RoutineControl.h"
#include "SID34_RequestDownload.h"
#include "SID36_TransferData.h"
#include "SID37_RequestTransferExit.h"
// 服务配置表
const uds_service_t uds_service_list[SID_NUM] =
{
/* SID 服务处理函数 长度是否合法 是否支持默认会话
是否支持编程会话
是否支持扩展会话
是否支持功能寻址
是否支持肯定响应抑制
安全访问等级 */
{SID_10, service_10_SessionControl, service_10_check_len, TRUE, TRUE, TRUE, TRUE, TRUE, UDS_SA_NON},
{SID_11, service_11_EcuReset, service_11_check_len, TRUE, TRUE, TRUE, TRUE, TRUE, UDS_SA_NON},
{SID_27, service_27_SecurityAccess, service_27_check_len, TRUE, TRUE, TRUE, FALSE, TRUE, UDS_SA_NON},
{SID_28, service_28_CommunicationControl, service_28_check_len, TRUE, TRUE, TRUE, TRUE, TRUE, UDS_SA_NON},
{SID_3E, service_3E_TesterPresent, service_3E_check_len, TRUE, TRUE, TRUE, TRUE, TRUE, UDS_SA_NON},
{SID_85, service_85_ControlDTCSetting, service_85_check_len, TRUE, TRUE, TRUE, TRUE, TRUE, UDS_SA_NON},
{SID_22, service_22_ReadDataByIdentifier, service_22_check_len, TRUE, TRUE, TRUE, TRUE, FALSE, UDS_SA_NON},
{SID_2E, service_2E_WriteDataByIdentifier, service_2E_check_len, FALSE, TRUE, TRUE, TRUE, FALSE, UDS_SA_LV1},
{SID_14, service_14_ClearDiagnosticInformation, service_14_check_len, TRUE, TRUE, TRUE, TRUE, FALSE, UDS_SA_NON},
{SID_19, service_19_ReadDTCInformation, service_19_check_len, TRUE, TRUE, TRUE, TRUE, FALSE, UDS_SA_NON},
{SID_31, service_31_RoutineControl, service_31_check_len, TRUE, TRUE, TRUE, FALSE, TRUE, UDS_SA_LV1},
{SID_34, service_34_RequestDownload, service_34_check_len, FALSE, TRUE, FALSE, FALSE, FALSE, UDS_SA_LV1},
{SID_36, service_36_TransferData, service_36_check_len, FALSE, TRUE, FALSE, FALSE, FALSE, UDS_SA_LV1},
{SID_37, service_37_RequestTransferExit, service_37_check_len, FALSE, TRUE, FALSE, FALSE, FALSE, UDS_SA_LV1},
};
/****************EOF****************/

View File

@@ -0,0 +1,31 @@
/*
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _SERVICE_CFG_H_
#define _SERVICE_CFG_H_
#define SID_NUM 16 // 当前共支持 16 个服务
#define SID_10 (0x10) /* SessionControl */
#define SID_11 (0x11) /* ECUReset */
#define SID_14 (0x14) /* ClearDTC */
#define SID_18 (0x18) /* KWPReadDTC */
#define SID_19 (0x19) /* ReadDTC */
#define SID_22 (0x22) /* ReadID */
#define SID_27 (0x27) /* SecurityAccess */
#define SID_2E (0x2E) /* WriteID */
#define SID_2F (0x2F) /* InputOutputControlID */
#define SID_28 (0x28) /* CommunicationControl */
#define SID_31 (0x31) /* RoutineControl */
#define SID_3E (0x3E) /* TesterPresent */
#define SID_85 (0x85) /* ControlDTCSetting */
#define SID_34 (0x34) /* RequestDownload */
#define SID_36 (0x36) /* TransferData */
#define SID_37 (0x37) /* RequestTransferExit */
#endif
/****************EOF****************/