Refactor DLT_Utilities to use STL containers, removing manual memory management && Add a debug output for command-line argument input

This commit is contained in:
2026-04-30 20:49:57 +08:00
parent df949ce460
commit 3ec4327d45
4 changed files with 348 additions and 500 deletions
+46 -95
View File
@@ -1,6 +1,18 @@
/**
* 相关参考资料
* DLTv1 传输协议: https://www.autosar.org/fileadmin/standards/R20-11/FO/AUTOSAR_PRS_LogAndTraceProtocol.pdf
* DLTv2 传输协议: https://www.autosar.org/fileadmin/standards/R22-11/FO/AUTOSAR_PRS_LogAndTraceProtocol.pdf
* COVESA 开源项目组: https://github.com/COVESA
* dlt-viewer开源项目: https://github.com/COVESA/dlt-viewer
* DLT文件结构设计定义: https://github.com/COVESA/dlt-daemon/blob/master/doc/dlt_design_specification.md
*/
#ifndef __DLT_UTILITIES_HPP__
#define __DLT_UTILITIES_HPP__
#include "vector"
#include "string"
extern "C"
{
#include "stdint.h"
@@ -8,13 +20,14 @@ extern "C"
}
// DLT 类型定义
namespace DLT_Type
class DLT_Log
{
#pragma pack(1) // 紧凑字节对齐
protected:
// 紧凑字节对齐
#pragma pack(1)
/**
* The structure of the DLT file storage header. This header is used before each stored DLT message.
*/
// The structure of the DLT file storage header. This header is used before each stored DLT message.
// 存储头不是AUTOSAR规范定义的一部分,来自COVESA开源项目实现
typedef struct
{
char pattern[4]; /**< This pattern should be DLT0x01 */
@@ -23,6 +36,7 @@ namespace DLT_Type
char ecu[4]; /**< The ECU id is added, if it is not already in the DLT message itself */
} DltStorageHeader;
// The structure of the DLT standard header. This header is used in each DLT message.
typedef struct
{
struct
@@ -39,9 +53,7 @@ namespace DLT_Type
uint16_t len; /**< Length of the complete message, without storage header */
} DltStandardHeader;
/**
* The structure of the DLT extra header parameters. Each parameter is sent only if enabled in htyp.
*/
// The structure of the DLT extra header parameters. Each parameter is sent only if enabled in htyp.
typedef struct
{
char ecu[4]; /**< ECU id */
@@ -49,39 +61,21 @@ namespace DLT_Type
uint32_t tmsp; /**< Timestamp since system start in 0.1 milliseconds */
} DltStandardHeaderExtra;
/**
* The structure of the DLT extended header. This header is only sent if enabled in htyp parameter.
*/
// The structure of the DLT extended header. This header is only sent if enabled in htyp parameter.
typedef struct
{
struct
{
uint8_t VERB : 1; // (Verbose) 冗余模式 1 启用 0 不启用
uint8_t MSTP : 3; // (Message Type)
uint8_t MTIN : 4; // (Message Type Info)
uint8_t VERB : 1; // (Verbose) 冗余模式 1 启用 0 不启用
uint8_t MSTP : 3; // (Message Type) 消息类型
uint8_t MTIN : 4; // (Message Type Info) 选定类型下的消息条目
} msin; /**< messsage info */
uint8_t noar; /**< number of arguments */
char apid[4]; /**< application id */
char ctid[4]; /**< context id */
} DltExtendedHeader;
#pragma pack(0) // 恢复字节对齐
// DLT 错误信息枚举
typedef enum
{
DLT_ERROR_NONE = 0, // 无错误
DLT_ERROR_NULL_POINTER, // 空指针
DLT_ERROR_FILE_OPEN_FAILED, // 文件打开失败
DLT_ERROR_FILE_READ_FAILED, // 文件读取失败
DLT_ERROR_FILE_CREATE_FAILED, // 文件创建失败
DLT_ERROR_END_OF_FILE, // 文件结尾
DLT_ERROR_INVALID_HEADER, // 非法头
DLT_ERROR_INCOMPLETE_DATA, // 数据不完整
DLT_ERROR_MEM_ALLOCATE_FAILED, // 内存分配失败
DLT_ERROR_MSG_LIST_EMPTY, // 消息列表为空
} DLT_Err;
// DLT消息条目结构定义
typedef struct
{
@@ -89,82 +83,39 @@ namespace DLT_Type
DltStandardHeader standard_header; // 标准头
DltStandardHeaderExtra standard_header_extra; // 标准头额外内容
DltExtendedHeader extendedheader; // 扩展头
size_t payload_length; // 消息长度
uint8_t *payload_buffer; // 载荷
} DLT_Msg; // 文件中的消息条目
std::vector<uint8_t> payload; // 载荷
} DLT_Msg;
// DLT消息链表节点
typedef struct DLT_Msg_Node_Struct
{
DLT_Msg Msg;
DLT_Msg_Node_Struct *p_next;
} DLT_Msg_Node;
}
// DLT 日志类
class DLT_Log
{
private:
size_t loaded_msg_count; // 消息条目计数
size_t max_payload_len; // 最长载荷长度
size_t mem_use; // 存储占用
DLT_Type::DLT_Msg_Node *Msg_List_Head; // 消息列表头
DLT_Type::DLT_Msg_Node *Msg_List_End; // 消息列表尾
/**
* @brief 从文件中读取dlt头
* @param p_file 要读取的文件
* @param Msg 读取后存储的目标结构体
* @return DLT_Type::DLT_Err 错误类型枚举
*/
DLT_Type::DLT_Err dlt_file_read_header(FILE *p_file, DLT_Type::DLT_Msg *Msg);
/**
* @brief 从文件中读取dlt载荷
* @param p_file 要读取的文件
* @param Msg 读取后存储的目标结构体
* @return DLT_Type::DLT_Err 错误类型枚举
*/
DLT_Type::DLT_Err dlt_file_read_payload(FILE *p_file, DLT_Type::DLT_Msg *Msg);
protected:
// 最长载荷长度
size_t private_max_payload_len = 0;
// 消息列表
std::vector<DLT_Msg> private_msg_list;
public:
DLT_Log(); // 构造函数
~DLT_Log(); // 析构函数
/**
* @brief 从文件加载DLT消息,多个文件会顺序加入
* @param file_name_str 目标文件名
* @return DLT_Type::DLT_Err 错误类型枚举
*/
DLT_Type::DLT_Err load_from_file(const char *file_name_str);
// 只读引用
const std::vector<DLT_Msg> &msg_list = this->private_msg_list;
const size_t &max_payload_len = this->private_max_payload_len;
/**
* @brief 获取解析完成的消息列表
* @param p_msg_list 消息列表存入的指针
* @return 消息列表计数
* @brief 从文件加载
* @param path_to_file 文件路径串
* @return std::string 错误信息
*/
size_t get_msg_list(DLT_Type::DLT_Msg_Node *(&p_msg_list));
std::string Load_From_File(const char *path_to_file);
/**
* @brief 将消息列表按时间戳排序
* @param null
* @return DLT_Type::DLT_Err 错误类型枚举
* @brief 以CSV格式导出到文件
* @param path_to_file 文件路径串
* @return std::string 错误信息
*/
DLT_Type::DLT_Err sort_msg_list(void);
std::string Export_CSV(const char *path_to_file);
/**
* @brief 导出为CSV
* @param file_name_str 目标文件名
* @return DLT_Type::DLT_Err 错误类型枚举
*/
DLT_Type::DLT_Err export_to_csv(const char *file_name_str);
// 对消息列表进行排序
void Sort(void);
/**
* @brief 清理请求的存储并恢复初始状态
* @param null
* @return null
*/
void clear(void);
// 清除缓存的信息
void Clear(void);
};
#endif