我想在我的C ++应用程序中使用libusb库。我创建了包装libusb函数的类。您可以看到libusb API分为八个部分:
http://libusb.sourceforge.net/api-1.0/modules.html
对我来说,最必要的部分是:
我还将“设备处理和枚举”部分拆分为“设备信息”和“设备处理”。要使用libusb函数,需要初始化libusb库(调用libusb_init
函数),将其初始化的libusb_context *
存储在某处,然后将调用函数(libusb_get_device_list
,libusb_open_device_with_vid_pid
)存储到获取并存储某个“设备指针”(在libusb中称为libusb_device*
或libusb_device_handle*
),然后最终调用函数以获取设备信息或执行一些USB传输(例如:libusb_set_configuration
, libusb_get_bus_number
)。请注意,这些函数需要“设备指针”:libusb_device_handle*
或libusb_device_*
作为第一个参数。您可以在此处查看初始化/打开/控制设备的整个过程:
http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/
目前我的解决方案包括以下课程:
libusb_context *
,libusb_device *
,libusb_device_handle *
LibusbControlAPI标题:
class LibusbControlAPI
{
public:
int GetConfiguration(DevHandlePtr deviceHandle, int& activeConfiguration);
int SetConfiguration(DevHandlePtr deviceHandle, int targetConfiguration);
int ClaimInterface(DevHandlePtr deviceHandle, int interfaceNumber);
int ReleaseInterface(DevHandlePtr deviceHandle, int interfaceNumber);
int SetInterfaceAlternateSetting(DevHandlePtr deviceHandle, int interfaceNumber, int alternateSetting);
int ClearHalt(DevHandlePtr deviceHandle, USBEndpoint endpoint);
int SendControlTransfer(DevHandlePtr deviceHandle,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char * buffer,
uint16_t wLength,
unsigned int timeout);
int SendInterruptTransfer(DevHandlePtr deviceHandle,
USBEndpoint endpoint,
unsigned char * buffer,
int length,
int & transferred,
unsigned int timeout);
int SendBlukTransfer(DevHandlePtr deviceHandle,
USBEndpoint endpoint,
unsigned char * buffer,
int length,
int & transferred,
unsigned int timeout);
};
cpp文件的一部分:
int LibusbControlAPI::GetConfiguration(DevHandlePtr deviceHandle, int & activeConfiguration)
{
int result;
int * configuration = nullptr;
if (!deviceHandle->m_opened)
return 1;
result = libusb_get_configuration(deviceHandle->m_libusbData->m_libusbDeviceHandle, configuration);
if (result != 0)
return 1;
activeConfiguration = *configuration;
return 0;
}
类似于LibusbInfoAPI标题,我有:
class LibusbInfoAPI
{
public:
int GetBusNumber(DevHandlePtr deviceHandle, uint8_t& busNumber);
int GetPortNumber(DevHandlePtr deviceHandle, uint8_t& portNumber);
int GetDeviceAddress(DevHandlePtr deviceHandle, uint8_t& deviceAddress);
int GetDeviceSpeed(DevHandlePtr deviceHandle, USBSpeed& usbSpeed);
int GetMaxPacketSize(DevHandlePtr deviceHandle, USBEndpoint endpoint, int& maxPacketSize);
int GetMaxIsoPacketSize(DevHandlePtr deviceHandle, USBEndpoint endpoint, int& isoMaxPacketSize);
};
...和LibusbHandlingAPI(在这个类中我存储libusb结构,在CreateDevice
函数中我返回一个指针,用于LibusbControlAPI
和LibusbInfoAPI
):
class LibusbHandlingAPI
{
private:
typedef std::vector<DevHandlePtr> DevHandlePtrVect;
DevHandlePtrVect m_deviceHandleVect;
LibusbContextPtr m_libusbContext;
public:
DevHandlePtr CreateDevice(uint16_t vendorId, uint16_t productId);
DevHandlePtr CreateDevice(uint16_t vendorId, uint16_t productId, uint8_t busNumber, uint8_t * portNumbers, int portNumbersLength);
void DestroyDevice(DevHandlePtr deviceHandle);
int OpenDevice(DevHandlePtr deviceHandle);
int CloseDevice(DevHandlePtr deviceHandle);
int InitializeLibrary(SystemLibraryLogLevel logLevel);
void DestroyLibrary(void);
};
所以,我的问题是: