86 lines
2.4 KiB
C
86 lines
2.4 KiB
C
|
|
/**
|
|||
|
|
* @file LinuxUsbDevice.h
|
|||
|
|
* @brief Linux平台USB设备实现
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#ifndef USB2CAN_LINUX_USB_DEVICE_H
|
|||
|
|
#define USB2CAN_LINUX_USB_DEVICE_H
|
|||
|
|
|
|||
|
|
#include "UsbDevice.h"
|
|||
|
|
|
|||
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|||
|
|
#include <libusb.h>
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
namespace usb2can {
|
|||
|
|
namespace usb {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Linux平台USB设备实现类
|
|||
|
|
*/
|
|||
|
|
class LinuxUsbDevice : public UsbDevice {
|
|||
|
|
public:
|
|||
|
|
LinuxUsbDevice(platform::Platform* platform);
|
|||
|
|
~LinuxUsbDevice() override;
|
|||
|
|
|
|||
|
|
bool open(const std::string& devicePath) override;
|
|||
|
|
void close() override;
|
|||
|
|
bool isOpen() const override;
|
|||
|
|
int write(const uint8_t* data, size_t size, int timeout = 1000) override;
|
|||
|
|
int read(uint8_t* data, size_t size, int timeout = 1000) override;
|
|||
|
|
bool getDeviceInfo(UsbDeviceInfo& info) override;
|
|||
|
|
std::string getDevicePath() const override;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
platform::Platform* platform_; ///< 平台实例
|
|||
|
|
void* deviceHandle_; ///< 设备句柄
|
|||
|
|
std::string devicePath_; ///< 设备路径
|
|||
|
|
bool isOpen_; ///< 设备是否已打开
|
|||
|
|
UsbDeviceInfo deviceInfo_; ///< 设备信息
|
|||
|
|
uint8_t inEndpoint_; ///< 输入端点
|
|||
|
|
uint8_t outEndpoint_; ///< 输出端点
|
|||
|
|
|
|||
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|||
|
|
libusb_context* ctx_; ///< libusb上下文
|
|||
|
|
std::vector<libusb_endpoint_descriptor> endpoints_; ///< 端点描述符列表
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 初始化libusb
|
|||
|
|
* @return 操作成功返回true,否则返回false
|
|||
|
|
*/
|
|||
|
|
bool initializeLibUsb();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取libusb设备句柄
|
|||
|
|
* @return libusb设备句柄
|
|||
|
|
*/
|
|||
|
|
libusb_device_handle* getLibusbHandle() const;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取USB设备描述符
|
|||
|
|
* @param descriptor 设备描述符
|
|||
|
|
* @return 操作成功返回true,否则返回false
|
|||
|
|
*/
|
|||
|
|
bool getDeviceDescriptor(libusb_device_descriptor& descriptor);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取USB字符串描述符
|
|||
|
|
* @param descIndex 描述符索引
|
|||
|
|
* @return 字符串描述符内容
|
|||
|
|
*/
|
|||
|
|
std::string getStringDescriptor(uint8_t descIndex);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 查找USB设备的端点
|
|||
|
|
* @param inEndpoint 输入端点
|
|||
|
|
* @param outEndpoint 输出端点
|
|||
|
|
* @return 操作成功返回true,否则返回false
|
|||
|
|
*/
|
|||
|
|
bool findEndpoints(uint8_t& inEndpoint, uint8_t& outEndpoint);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace usb
|
|||
|
|
} // namespace usb2can
|
|||
|
|
|
|||
|
|
#endif // USB2CAN_LINUX_USB_DEVICE_H
|