77 lines
2.3 KiB
C
77 lines
2.3 KiB
C
|
|
/**
|
|||
|
|
* @file LinuxPlatform.h
|
|||
|
|
* @brief Linux平台实现
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#ifndef USB2CAN_LINUX_PLATFORM_H
|
|||
|
|
#define USB2CAN_LINUX_PLATFORM_H
|
|||
|
|
|
|||
|
|
#include "Platform.h"
|
|||
|
|
|
|||
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|||
|
|
#include <libusb.h>
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
namespace usb2can {
|
|||
|
|
namespace platform {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Linux平台实现类
|
|||
|
|
*/
|
|||
|
|
class LinuxPlatform : public Platform {
|
|||
|
|
public:
|
|||
|
|
LinuxPlatform();
|
|||
|
|
~LinuxPlatform() override;
|
|||
|
|
|
|||
|
|
PlatformType getType() const override;
|
|||
|
|
bool initialize() override;
|
|||
|
|
std::vector<std::string> scanUsbDevices(uint16_t vendorId, uint16_t productId) override;
|
|||
|
|
void* openUsbDevice(const std::string& devicePath) override;
|
|||
|
|
bool closeUsbDevice(void* deviceHandle) override;
|
|||
|
|
int writeUsbData(void* deviceHandle, const uint8_t* data, size_t size, int timeout) override;
|
|||
|
|
int readUsbData(void* deviceHandle, uint8_t* data, size_t size, int timeout) override;
|
|||
|
|
bool getDeviceInfo(void* deviceHandle, DeviceInfo& info) override;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
/**
|
|||
|
|
* @brief Linux设备句柄结构体
|
|||
|
|
*/
|
|||
|
|
struct LinuxDeviceHandle {
|
|||
|
|
libusb_device_handle* usbHandle; ///< USB设备句柄
|
|||
|
|
std::string devicePath; ///< 设备路径
|
|||
|
|
uint8_t inEndpoint; ///< 输入端点
|
|||
|
|
uint8_t outEndpoint; ///< 输出端点
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
libusb_context* usbContext; ///< USB上下文
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取USB设备描述符
|
|||
|
|
* @param usbHandle USB设备句柄
|
|||
|
|
* @param descriptor 设备描述符
|
|||
|
|
* @return 操作成功返回true,否则返回false
|
|||
|
|
*/
|
|||
|
|
bool getUsbDeviceDescriptor(libusb_device_handle* usbHandle, libusb_device_descriptor& descriptor);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取USB设备字符串描述符
|
|||
|
|
* @param usbHandle USB设备句柄
|
|||
|
|
* @param descIndex 描述符索引
|
|||
|
|
* @return 字符串描述符内容
|
|||
|
|
*/
|
|||
|
|
std::string getUsbStringDescriptor(libusb_device_handle* usbHandle, uint8_t descIndex);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 查找USB设备的端点
|
|||
|
|
* @param usbHandle USB设备句柄
|
|||
|
|
* @param inEndpoint 输入端点
|
|||
|
|
* @param outEndpoint 输出端点
|
|||
|
|
* @return 操作成功返回true,否则返回false
|
|||
|
|
*/
|
|||
|
|
bool findUsbEndpoints(libusb_device_handle* usbHandle, uint8_t& inEndpoint, uint8_t& outEndpoint);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace platform
|
|||
|
|
} // namespace usb2can
|
|||
|
|
|
|||
|
|
#endif // USB2CAN_LINUX_PLATFORM_H
|