Files
hcanview/include/usb/UsbDevice.h

139 lines
3.3 KiB
C
Raw Normal View History

/**
* @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 truefalse
*/
virtual bool open(const std::string& devicePath) = 0;
/**
* @brief USB设备
*/
virtual void close() = 0;
/**
* @brief
* @return truefalse
*/
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 truefalse
*/
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