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
113 lines
2.8 KiB
C++
113 lines
2.8 KiB
C++
/**
|
||
* @file Platform.h
|
||
* @brief 平台抽象层接口定义
|
||
*/
|
||
|
||
#ifndef USB2CAN_PLATFORM_H
|
||
#define USB2CAN_PLATFORM_H
|
||
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace usb2can {
|
||
namespace platform {
|
||
|
||
/**
|
||
* @brief 平台类型枚举
|
||
*/
|
||
enum class PlatformType {
|
||
WINDOWS, ///< Windows平台
|
||
LINUX ///< Linux平台
|
||
};
|
||
|
||
/**
|
||
* @brief 设备信息结构体
|
||
*/
|
||
struct DeviceInfo {
|
||
std::string hwType; ///< 设备型号
|
||
std::string serialNumber; ///< 设备序列号
|
||
std::string hwVersion; ///< 硬件版本
|
||
std::string fwVersion; ///< 软件版本
|
||
std::string manufactureDate; ///< 生产日期
|
||
};
|
||
|
||
/**
|
||
* @brief 平台抽象接口类
|
||
*/
|
||
class Platform {
|
||
public:
|
||
virtual ~Platform() = default;
|
||
|
||
/**
|
||
* @brief 获取平台类型
|
||
* @return 平台类型
|
||
*/
|
||
virtual PlatformType getType() const = 0;
|
||
|
||
/**
|
||
* @brief 初始化平台
|
||
* @return 初始化成功返回true,否则返回false
|
||
*/
|
||
virtual bool initialize() = 0;
|
||
|
||
/**
|
||
* @brief 扫描USB设备
|
||
* @param vendorId 供应商ID
|
||
* @param productId 产品ID
|
||
* @return 设备路径列表
|
||
*/
|
||
virtual std::vector<std::string> scanUsbDevices(uint16_t vendorId, uint16_t productId) = 0;
|
||
|
||
/**
|
||
* @brief 打开USB设备
|
||
* @param devicePath 设备路径
|
||
* @return 设备句柄,失败返回nullptr
|
||
*/
|
||
virtual void* openUsbDevice(const std::string& devicePath) = 0;
|
||
|
||
/**
|
||
* @brief 关闭USB设备
|
||
* @param deviceHandle 设备句柄
|
||
* @return 操作成功返回true,否则返回false
|
||
*/
|
||
virtual bool closeUsbDevice(void* deviceHandle) = 0;
|
||
|
||
/**
|
||
* @brief 写入USB数据
|
||
* @param deviceHandle 设备句柄
|
||
* @param data 数据缓冲区
|
||
* @param size 数据大小
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 实际写入的字节数,失败返回-1
|
||
*/
|
||
virtual int writeUsbData(void* deviceHandle, const uint8_t* data, size_t size, int timeout) = 0;
|
||
|
||
/**
|
||
* @brief 读取USB数据
|
||
* @param deviceHandle 设备句柄
|
||
* @param data 数据缓冲区
|
||
* @param size 数据大小
|
||
* @param timeout 超时时间(毫秒)
|
||
* @return 实际读取的字节数,失败返回-1
|
||
*/
|
||
virtual int readUsbData(void* deviceHandle, uint8_t* data, size_t size, int timeout) = 0;
|
||
|
||
/**
|
||
* @brief 获取设备信息
|
||
* @param deviceHandle 设备句柄
|
||
* @param[out] info 设备信息
|
||
* @return 操作成功返回true,否则返回false
|
||
*/
|
||
virtual bool getDeviceInfo(void* deviceHandle, DeviceInfo& info) = 0;
|
||
|
||
/**
|
||
* @brief 创建平台实例
|
||
* @return 平台实例
|
||
*/
|
||
static Platform* createPlatform();
|
||
};
|
||
|
||
} // namespace platform
|
||
} // namespace usb2can
|
||
|
||
#endif // USB2CAN_PLATFORM_H
|