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
268 lines
7.5 KiB
C++
268 lines
7.5 KiB
C++
/**
|
||
* @file Usb2CanDevice.h
|
||
* @brief USB2CAN设备类定义
|
||
*/
|
||
|
||
#ifndef USB2CAN_USB2CAN_DEVICE_H
|
||
#define USB2CAN_USB2CAN_DEVICE_H
|
||
|
||
#include "../usb/UsbDevice.h"
|
||
#include "../can/CanController.h"
|
||
#include "../can/CanFdController.h"
|
||
#include "../can/CanMessage.h"
|
||
#include "../platform/Platform.h"
|
||
|
||
#include <string>
|
||
#include <memory>
|
||
|
||
namespace usb2can {
|
||
namespace device {
|
||
|
||
/**
|
||
* @brief USB2CAN设备类型枚举
|
||
*/
|
||
enum class DeviceType {
|
||
CAN, ///< 常规CAN设备
|
||
CANFD ///< CANFD设备
|
||
};
|
||
|
||
/**
|
||
* @brief USB2CAN设备信息结构体
|
||
*/
|
||
struct Usb2CanDeviceInfo {
|
||
DeviceType type; ///< 设备类型
|
||
uint16_t vendorId; ///< 供应商ID
|
||
uint16_t productId; ///< 产品ID
|
||
std::string manufacturer; ///< 制造商
|
||
std::string productName; ///< 产品名称
|
||
std::string serialNumber; ///< 序列号
|
||
std::string hwVersion; ///< 硬件版本
|
||
std::string fwVersion; ///< 软件版本
|
||
std::string manufactureDate; ///< 生产日期
|
||
std::string devicePath; ///< 设备路径
|
||
uint32_t clockFrequency; ///< 主频
|
||
int deviceIndex; ///< 设备索引
|
||
};
|
||
|
||
/**
|
||
* @brief USB2CAN设备类
|
||
*/
|
||
class Usb2CanDevice {
|
||
public:
|
||
/**
|
||
* @brief 构造函数
|
||
* @param platform 平台实例
|
||
* @param deviceIndex 设备索引
|
||
*/
|
||
Usb2CanDevice(platform::Platform* platform, int deviceIndex);
|
||
|
||
/**
|
||
* @brief 析构函数
|
||
*/
|
||
~Usb2CanDevice();
|
||
|
||
/**
|
||
* @brief 打开设备
|
||
* @return 打开成功返回true,否则返回false
|
||
*/
|
||
bool open();
|
||
|
||
/**
|
||
* @brief 关闭设备
|
||
*/
|
||
void close();
|
||
|
||
/**
|
||
* @brief 检查设备是否已打开
|
||
* @return 设备已打开返回true,否则返回false
|
||
*/
|
||
bool isOpen() const;
|
||
|
||
/**
|
||
* @brief 获取设备信息
|
||
* @param[out] info 设备信息
|
||
* @return 获取成功返回true,否则返回false
|
||
*/
|
||
bool getDeviceInfo(Usb2CanDeviceInfo& info);
|
||
|
||
/**
|
||
* @brief 获取设备类型
|
||
* @return 设备类型
|
||
*/
|
||
DeviceType getDeviceType() const;
|
||
|
||
/**
|
||
* @brief 获取设备索引
|
||
* @return 设备索引
|
||
*/
|
||
int getDeviceIndex() const;
|
||
|
||
/**
|
||
* @brief 获取设备路径
|
||
* @return 设备路径
|
||
*/
|
||
std::string getDevicePath() const;
|
||
|
||
/**
|
||
* @brief 初始化CAN控制器
|
||
* @param config CAN配置
|
||
* @return 初始化成功返回true,否则返回false
|
||
*/
|
||
bool initCan(const can::CanConfig& config);
|
||
|
||
/**
|
||
* @brief 初始化CANFD控制器
|
||
* @param config CANFD配置
|
||
* @return 初始化成功返回true,否则返回false
|
||
*/
|
||
bool initCanFd(const can::CanFdConfig& config);
|
||
|
||
/**
|
||
* @brief 发送CAN消息
|
||
* @param message CAN消息
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 发送成功返回true,否则返回false
|
||
*/
|
||
bool sendCanMessage(const can::CanMessage& message, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 接收CAN消息
|
||
* @param[out] message CAN消息
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 接收成功返回true,否则返回false
|
||
*/
|
||
bool receiveCanMessage(can::CanMessage& message, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 批量发送CAN消息
|
||
* @param messages CAN消息列表
|
||
* @param count 消息数量
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 实际发送的消息数量,失败返回-1
|
||
*/
|
||
int sendCanMessages(const can::CanMessage* messages, int count, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 批量接收CAN消息
|
||
* @param[out] messages CAN消息列表
|
||
* @param count 最大接收消息数量
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 实际接收的消息数量,失败返回-1
|
||
*/
|
||
int receiveCanMessages(can::CanMessage* messages, int count, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 发送CANFD消息
|
||
* @param message CANFD消息
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 发送成功返回true,否则返回false
|
||
*/
|
||
bool sendCanFdMessage(const can::CanFdMessage& message, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 接收CANFD消息
|
||
* @param[out] message CANFD消息
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 接收成功返回true,否则返回false
|
||
*/
|
||
bool receiveCanFdMessage(can::CanFdMessage& message, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 批量发送CANFD消息
|
||
* @param messages CANFD消息列表
|
||
* @param count 消息数量
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 实际发送的消息数量,失败返回-1
|
||
*/
|
||
int sendCanFdMessages(const can::CanFdMessage* messages, int count, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 批量接收CANFD消息
|
||
* @param[out] messages CANFD消息列表
|
||
* @param count 最大接收消息数量
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 实际接收的消息数量,失败返回-1
|
||
*/
|
||
int receiveCanFdMessages(can::CanFdMessage* messages, int count, int timeout = 1000);
|
||
|
||
/**
|
||
* @brief 获取CAN接收缓冲区中的消息数量
|
||
* @return 接收缓冲区中的消息数量,失败返回-1
|
||
*/
|
||
int getCanReceiveQueueCount();
|
||
|
||
/**
|
||
* @brief 获取CANFD接收缓冲区中的消息数量
|
||
* @return 接收缓冲区中的消息数量,失败返回-1
|
||
*/
|
||
int getCanFdReceiveQueueCount();
|
||
|
||
/**
|
||
* @brief 设置CAN过滤器
|
||
* @param filterId 过滤器ID
|
||
* @param type 过滤器类型
|
||
* @param id 过滤ID
|
||
* @param mask 过滤掩码
|
||
* @param enable 是否启用过滤器
|
||
* @return 设置成功返回true,否则返回false
|
||
*/
|
||
bool setCanFilter(uint8_t filterId, uint8_t type, uint32_t id, uint32_t mask, bool enable);
|
||
|
||
/**
|
||
* @brief 设置CANFD过滤器
|
||
* @param filterId 过滤器ID
|
||
* @param type 过滤器类型
|
||
* @param id 过滤ID
|
||
* @param mask 过滤掩码
|
||
* @param enable 是否启用过滤器
|
||
* @return 设置成功返回true,否则返回false
|
||
*/
|
||
bool setCanFdFilter(uint8_t filterId, uint8_t type, uint32_t id, uint32_t mask, bool enable);
|
||
|
||
/**
|
||
* @brief 复位CAN控制器
|
||
* @return 复位成功返回true,否则返回false
|
||
*/
|
||
bool resetCan();
|
||
|
||
/**
|
||
* @brief 复位CANFD控制器
|
||
* @return 复位成功返回true,否则返回false
|
||
*/
|
||
bool resetCanFd();
|
||
|
||
/**
|
||
* @brief 获取CAN控制器状态
|
||
* @param[out] status CAN状态
|
||
* @return 获取成功返回true,否则返回false
|
||
*/
|
||
bool getCanStatus(can::CanStatus& status);
|
||
|
||
/**
|
||
* @brief 获取CANFD控制器状态
|
||
* @param[out] status CAN状态
|
||
* @return 获取成功返回true,否则返回false
|
||
*/
|
||
bool getCanFdStatus(can::CanStatus& status);
|
||
|
||
private:
|
||
platform::Platform* platform_; ///< 平台实例
|
||
int deviceIndex_; ///< 设备索引
|
||
std::unique_ptr<usb::UsbDevice> usbDevice_; ///< USB设备
|
||
std::unique_ptr<can::CanController> canController_; ///< CAN控制器
|
||
std::unique_ptr<can::CanFdController> canFdController_; ///< CANFD控制器
|
||
Usb2CanDeviceInfo deviceInfo_; ///< 设备信息
|
||
bool isOpen_; ///< 设备是否已打开
|
||
DeviceType deviceType_; ///< 设备类型
|
||
|
||
/**
|
||
* @brief 初始化设备信息
|
||
* @return 初始化成功返回true,否则返回false
|
||
*/
|
||
bool initDeviceInfo();
|
||
};
|
||
|
||
} // namespace device
|
||
} // namespace usb2can
|
||
|
||
#endif // USB2CAN_USB2CAN_DEVICE_H
|