Initial commit from DCSP - 2025/12/30 17:02:59
This commit is contained in:
130
CanHS.h
Normal file
130
CanHS.h
Normal file
@@ -0,0 +1,130 @@
|
||||
#ifndef CANHS_H
|
||||
#define CANHS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <pthread.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 简化后的服务类型定义 */
|
||||
#define O2_NORMAL_TELEMETRY 0x2 /* 常规遥测 */
|
||||
#define O2_TELEMETRY_CONTROL 0x4 /* 遥控 */
|
||||
|
||||
/* 帧序列定义 */
|
||||
#define O2_MID_FRAME 0x0
|
||||
#define O2_START_FRAME 0x1
|
||||
#define O2_END_FRAME 0x2
|
||||
#define O2_SINGLE_FRAME 0x3
|
||||
|
||||
/* CAN帧结构体 */
|
||||
typedef struct {
|
||||
uint32_t CAN_uiId;
|
||||
uint8_t CAN_bRtr;
|
||||
uint8_t CAN_bExtId;
|
||||
uint8_t CAN_ucLen;
|
||||
uint8_t CAN_ucData[8];
|
||||
} CAN_FRAME;
|
||||
|
||||
/* 多帧重组状态 */
|
||||
typedef struct {
|
||||
uint8_t buffer[1024];
|
||||
uint16_t length;
|
||||
uint8_t serve_type;
|
||||
uint8_t expected_frame_index;
|
||||
uint8_t active;
|
||||
struct timeval start_time;
|
||||
} MultiFrameState;
|
||||
|
||||
/* 统计计数器 */
|
||||
typedef struct {
|
||||
uint8_t rec_normal_telemetry; /* 接收遥测请求次数 */
|
||||
uint8_t rec_telemetry_control; /* 接收遥控指令次数 */
|
||||
uint8_t send_single_frame; /* 发送单帧次数 */
|
||||
uint8_t send_multi_frame; /* 发送多帧次数 */
|
||||
uint8_t can_reset; /* CAN重置次数 */
|
||||
} CanStats;
|
||||
|
||||
/* CAN服务上下文 */
|
||||
typedef struct {
|
||||
int fd_can;
|
||||
bool running;
|
||||
uint8_t send_id_on_o2; /* CAN发送ID */
|
||||
|
||||
CanStats stats;
|
||||
|
||||
/* 遥测数据缓冲区 */
|
||||
uint8_t telemetry_buffer[1024];
|
||||
size_t telemetry_len;
|
||||
pthread_mutex_t telemetry_mutex;
|
||||
bool telemetry_request_pending;
|
||||
|
||||
/* 多帧重组状态 */
|
||||
MultiFrameState multi_frame_state;
|
||||
pthread_mutex_t multi_frame_mutex;
|
||||
|
||||
/* 线程相关 */
|
||||
pthread_t listener_thread;
|
||||
} CanServiceContext;
|
||||
|
||||
/* 函数声明 */
|
||||
|
||||
/* 初始化与销毁 */
|
||||
bool can_service_init(const char* can_device);
|
||||
void can_service_destroy(void);
|
||||
|
||||
/* 运行控制 */
|
||||
void can_service_start(void);
|
||||
void can_service_stop(void);
|
||||
|
||||
/* 统计数据获取 */
|
||||
const CanStats* can_service_get_stats(void);
|
||||
|
||||
/* CAN发送函数 */
|
||||
int can_send_single(uint8_t serve_type, uint8_t *data, uint8_t data_len);
|
||||
int can_send_multi(uint8_t serve_type, uint8_t *data, uint16_t data_len);
|
||||
|
||||
/* 遥测数据处理 */
|
||||
void can_set_telemetry_data(const uint8_t *data, size_t data_len);
|
||||
bool can_get_telemetry_data(uint8_t *buffer, size_t buffer_size, size_t *data_len);
|
||||
void send_telemetry_response(const uint8_t *data, size_t data_len);
|
||||
|
||||
/* 快速查询接口 */
|
||||
static inline uint8_t can_get_normal_telemetry_count(void) {
|
||||
const CanStats* stats = can_service_get_stats();
|
||||
return stats ? stats->rec_normal_telemetry : 0;
|
||||
}
|
||||
|
||||
static inline uint8_t can_get_telemetry_control_count(void) {
|
||||
const CanStats* stats = can_service_get_stats();
|
||||
return stats ? stats->rec_telemetry_control : 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/* C++接口声明(与ComS保持一致) */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* FastDDS相关函数 */
|
||||
void CanHSWriteLog(const std::string &msg);
|
||||
void CanHS_init(uint8_t domainid, std::string appname);
|
||||
void CanHS_command_Pub(uint8_t* data, std::string dest, uint16_t len);
|
||||
void CanHS_telemetry_Pub(uint8_t* data, std::string dest, uint16_t len);
|
||||
|
||||
void telemetry_callback(std::string src, std::string dest, std::string type,
|
||||
std::string reserve1, std::string reserve2,
|
||||
std::vector<uint8_t>& data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CANHS_H */
|
||||
Reference in New Issue
Block a user