Initial commit: Complete USB2CAN cross-platform framework
Features: - Cross-platform support (Windows/Linux/macOS) - CAN and CANFD protocol support - USB communication (WinUSB/libusb) - Device management and configuration - Message transmission and reception - Filter configuration - CMake build system - Comprehensive examples and tests
This commit is contained in:
265
include/api/Usb2Can.h
Normal file
265
include/api/Usb2Can.h
Normal file
@@ -0,0 +1,265 @@
|
||||
/**
|
||||
* @file Usb2Can.h
|
||||
* @brief USB2CAN应用接口定义
|
||||
*/
|
||||
|
||||
#ifndef USB2CAN_API_H
|
||||
#define USB2CAN_API_H
|
||||
|
||||
#include "../device/DeviceManager.h"
|
||||
#include "../device/Usb2CanDevice.h"
|
||||
#include "../can/CanMessage.h"
|
||||
#include "../platform/Platform.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace usb2can {
|
||||
|
||||
/**
|
||||
* @brief USB2CAN主类,提供简化的API接口
|
||||
*/
|
||||
class Usb2Can {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
*/
|
||||
Usb2Can();
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~Usb2Can();
|
||||
|
||||
/**
|
||||
* @brief 初始化USB2CAN框架
|
||||
* @return 初始化成功返回true,否则返回false
|
||||
*/
|
||||
bool initialize();
|
||||
|
||||
/**
|
||||
* @brief 扫描USB2CAN设备
|
||||
* @return 设备数量
|
||||
*/
|
||||
int scanDevices();
|
||||
|
||||
/**
|
||||
* @brief 获取设备数量
|
||||
* @return 设备数量
|
||||
*/
|
||||
int getDeviceCount() const;
|
||||
|
||||
/**
|
||||
* @brief 获取设备类型
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 设备类型:0表示常规CAN,1表示CANFD,失败返回-1
|
||||
*/
|
||||
int getDeviceType(int deviceIndex) const;
|
||||
|
||||
/**
|
||||
* @brief 获取设备主频
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 设备主频,失败返回0
|
||||
*/
|
||||
uint32_t getDeviceClockFrequency(int deviceIndex) const;
|
||||
|
||||
/**
|
||||
* @brief 打开设备
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 打开成功返回true,否则返回false
|
||||
*/
|
||||
bool openDevice(int deviceIndex);
|
||||
|
||||
/**
|
||||
* @brief 关闭设备
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 关闭成功返回true,否则返回false
|
||||
*/
|
||||
bool closeDevice(int deviceIndex);
|
||||
|
||||
/**
|
||||
* @brief 读取设备信息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param[out] info 设备信息
|
||||
* @return 读取成功返回true,否则返回false
|
||||
*/
|
||||
bool readDeviceInfo(int deviceIndex, device::Usb2CanDeviceInfo& info);
|
||||
|
||||
/**
|
||||
* @brief 设置CAN过滤器
|
||||
* @param deviceIndex 设备索引
|
||||
* @param filterId 过滤器ID
|
||||
* @param type 过滤器类型
|
||||
* @param id 过滤ID
|
||||
* @param mask 过滤掩码
|
||||
* @param enable 是否启用过滤器
|
||||
* @return 设置成功返回true,否则返回false
|
||||
*/
|
||||
bool setFilter(int deviceIndex, uint8_t filterId, uint8_t type, uint32_t id, uint32_t mask, bool enable);
|
||||
|
||||
/**
|
||||
* @brief 复位设备
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 复位成功返回true,否则返回false
|
||||
*/
|
||||
bool reset(int deviceIndex);
|
||||
|
||||
/**
|
||||
* @brief 获取设备状态
|
||||
* @param deviceIndex 设备索引
|
||||
* @param[out] status 设备状态
|
||||
* @return 获取成功返回true,否则返回false
|
||||
*/
|
||||
bool getStatus(int deviceIndex, can::CanStatus& status);
|
||||
|
||||
/**
|
||||
* @brief 初始化CAN控制器
|
||||
* @param deviceIndex 设备索引
|
||||
* @param config CAN配置
|
||||
* @return 初始化成功返回true,否则返回false
|
||||
*/
|
||||
bool initCan(int deviceIndex, const can::CanConfig& config);
|
||||
|
||||
/**
|
||||
* @brief 发送CAN消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param message CAN消息
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 发送成功返回true,否则返回false
|
||||
*/
|
||||
bool transmit(int deviceIndex, const can::CanMessage& message, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 批量发送CAN消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param messages CAN消息列表
|
||||
* @param count 消息数量
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 实际发送的消息数量,失败返回-1
|
||||
*/
|
||||
int transmit(int deviceIndex, const can::CanMessage* messages, int count, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 定时发送CAN消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param messages CAN消息列表
|
||||
* @param count 消息数量
|
||||
* @param[out] txItems 实际发送的消息数量
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 发送成功返回true,否则返回false
|
||||
*/
|
||||
bool transmitRt(int deviceIndex, const can::CanMessage* messages, int count, unsigned int* txItems, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 获取CAN接收缓冲区中的消息数量
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 接收缓冲区中的消息数量,失败返回-1
|
||||
*/
|
||||
int getReceiveNum(int deviceIndex);
|
||||
|
||||
/**
|
||||
* @brief 接收CAN消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param[out] message CAN消息
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 接收成功返回true,否则返回false
|
||||
*/
|
||||
bool receive(int deviceIndex, can::CanMessage& message, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 批量接收CAN消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param[out] messages CAN消息列表
|
||||
* @param count 最大接收消息数量
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 实际接收的消息数量,失败返回-1
|
||||
*/
|
||||
int receive(int deviceIndex, can::CanMessage* messages, int count, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 初始化CANFD控制器
|
||||
* @param deviceIndex 设备索引
|
||||
* @param config CANFD配置
|
||||
* @return 初始化成功返回true,否则返回false
|
||||
*/
|
||||
bool initCanFd(int deviceIndex, const can::CanFdConfig& config);
|
||||
|
||||
/**
|
||||
* @brief 发送CANFD消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param message CANFD消息
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 发送成功返回true,否则返回false
|
||||
*/
|
||||
bool transmitFd(int deviceIndex, const can::CanFdMessage& message, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 批量发送CANFD消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param messages CANFD消息列表
|
||||
* @param count 消息数量
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 实际发送的消息数量,失败返回-1
|
||||
*/
|
||||
int transmitFd(int deviceIndex, const can::CanFdMessage* messages, int count, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 定时发送CANFD消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param messages CANFD消息列表
|
||||
* @param count 消息数量
|
||||
* @param[out] txItems 实际发送的消息数量
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 发送成功返回true,否则返回false
|
||||
*/
|
||||
bool transmitFdRt(int deviceIndex, const can::CanFdMessage* messages, int count, unsigned int* txItems, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 获取CANFD接收缓冲区中的消息数量
|
||||
* @param deviceIndex 设备索引
|
||||
* @return 接收缓冲区中的消息数量,失败返回-1
|
||||
*/
|
||||
int getFdReceiveNum(int deviceIndex);
|
||||
|
||||
/**
|
||||
* @brief 接收CANFD消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param[out] message CANFD消息
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 接收成功返回true,否则返回false
|
||||
*/
|
||||
bool receiveFd(int deviceIndex, can::CanFdMessage& message, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 批量接收CANFD消息
|
||||
* @param deviceIndex 设备索引
|
||||
* @param[out] messages CANFD消息列表
|
||||
* @param count 最大接收消息数量
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return 实际接收的消息数量,失败返回-1
|
||||
*/
|
||||
int receiveFd(int deviceIndex, can::CanFdMessage* messages, int count, int timeout = 1000);
|
||||
|
||||
/**
|
||||
* @brief 注册热插拔回调函数
|
||||
* @param callback 回调函数
|
||||
* @return 注册成功返回true,否则返回false
|
||||
*/
|
||||
bool registerHotplugCallback(void (*callback)(void));
|
||||
|
||||
/**
|
||||
* @brief 注销热插拔回调函数
|
||||
* @return 注销成功返回true,否则返回false
|
||||
*/
|
||||
bool unregisterHotplugCallback();
|
||||
|
||||
private:
|
||||
std::unique_ptr<platform::Platform> platform_; ///< 平台实例
|
||||
std::unique_ptr<device::DeviceManager> deviceManager_; ///< 设备管理器
|
||||
std::vector<std::unique_ptr<device::Usb2CanDevice>> openedDevices_; ///< 已打开的设备列表
|
||||
bool isInitialized_; ///< 框架是否已初始化
|
||||
};
|
||||
|
||||
} // namespace usb2can
|
||||
|
||||
#endif // USB2CAN_API_H
|
||||
Reference in New Issue
Block a user