Files
hcanview/include/usb/UsbDevice.h
yemai 81bee50cd9 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
2025-09-11 17:56:26 +08:00

139 lines
3.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file UsbDevice.h
* @brief USB设备抽象接口定义
*/
#ifndef USB2CAN_USB_DEVICE_H
#define USB2CAN_USB_DEVICE_H
#include <string>
#include <vector>
#include <cstdint>
#include "../platform/Platform.h"
namespace usb2can {
namespace usb {
/**
* @brief USB设备信息结构体
*/
struct UsbDeviceInfo {
uint16_t vendorId; ///< 供应商ID
uint16_t productId; ///< 产品ID
std::string manufacturer; ///< 制造商
std::string productName; ///< 产品名称
std::string serialNumber; ///< 序列号
std::string devicePath; ///< 设备路径
};
/**
* @brief USB设备抽象接口类
*/
class UsbDevice {
public:
virtual ~UsbDevice() = default;
/**
* @brief 打开USB设备
* @param devicePath 设备路径
* @return 操作成功返回true否则返回false
*/
virtual bool open(const std::string& devicePath) = 0;
/**
* @brief 关闭USB设备
*/
virtual void close() = 0;
/**
* @brief 检查设备是否已打开
* @return 设备已打开返回true否则返回false
*/
virtual bool isOpen() const = 0;
/**
* @brief 写入数据
* @param data 数据缓冲区
* @param size 数据大小
* @param timeout 超时时间(毫秒)
* @return 实际写入的字节数,失败返回-1
*/
virtual int write(const uint8_t* data, size_t size, int timeout = 1000) = 0;
/**
* @brief 读取数据
* @param data 数据缓冲区
* @param size 数据大小
* @param timeout 超时时间(毫秒)
* @return 实际读取的字节数,失败返回-1
*/
virtual int read(uint8_t* data, size_t size, int timeout = 1000) = 0;
/**
* @brief 获取设备信息
* @param[out] info 设备信息
* @return 操作成功返回true否则返回false
*/
virtual bool getDeviceInfo(UsbDeviceInfo& info) = 0;
/**
* @brief 获取设备路径
* @return 设备路径
*/
virtual std::string getDevicePath() const = 0;
/**
* @brief 创建USB设备实例
* @param platform 平台实例
* @return USB设备实例
*/
static UsbDevice* createUsbDevice(platform::Platform* platform);
};
/**
* @brief USB设备管理器类
*/
class UsbDeviceManager {
public:
/**
* @brief 构造函数
* @param platform 平台实例
*/
explicit UsbDeviceManager(platform::Platform* platform);
/**
* @brief 析构函数
*/
~UsbDeviceManager();
/**
* @brief 扫描USB设备
* @param vendorId 供应商ID0表示匹配所有供应商
* @param productId 产品ID0表示匹配所有产品
* @return USB设备信息列表
*/
std::vector<UsbDeviceInfo> scanDevices(uint16_t vendorId = 0, uint16_t productId = 0);
/**
* @brief 打开USB设备
* @param devicePath 设备路径
* @return USB设备实例失败返回nullptr
*/
UsbDevice* openDevice(const std::string& devicePath);
/**
* @brief 关闭USB设备
* @param device USB设备实例
*/
void closeDevice(UsbDevice* device);
private:
platform::Platform* platform_; ///< 平台实例
std::vector<UsbDevice*> devices_; ///< 已打开的设备列表
};
} // namespace usb
} // namespace usb2can
#endif // USB2CAN_USB_DEVICE_H