一、前言

        上一篇文章记录到如何在ubuntu 安装开源项目libusb,这篇将记录,如下使用libusb 提供的api 方便的与USB-HID 设备通讯,通讯方式为控制传输。


二、关于libusb 如何查找HID 设备,可以看观看一下我的Linux 下使用libusb 与USB-HID 设备通讯之中断传输

这篇文章,里面有详细记载如何查找HID 设备。


三、libusb_control_transfer() 函数

int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
	uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
	unsigned char *data, uint16_t wLength, unsigned int timeout);

在libusb中控制传输使用的就是上面的函数,对比下面这个Android 中控制传输的方法

 public int controlTransfer(int requestType, int request, int value, int index,
            byte[] buffer, int offset, int length, int timeout) {
        checkBounds(buffer, offset, length);
        return native_control_request(requestType, request, value, index,
                buffer, offset, length, timeout);
    }

比较上面两个方法,可以知道是相似的。因此,对于libusb_control_tranfer() 这个函数的参数详细的讲解以及如何传参,

请跳转到我的另一篇文章,Android USB HID bulkTransfer()参数解析,这篇文章有详细说明。

这里,我直接贴出代码,如下:

3.1 打开设备

int OpenUsbControlTransferDevice()
{
  	int error = 0;

	error = libusb_init(&ctxForControlTransfer);
	if (error < 0)
	{
		if(gUsbDebugEnable)
			PrintError("libusb init failed", __FILE__, __FUNCTION__, __LINE__, error);

		return DEIVCE_ERROR_OPEN_DEVICE;
	}
/*
	if(gUsbDebugEnable)
		libusb_set_debug(ctxForControlTransfer, LIBUSB_LOG_LEVEL_INFO); //set verbosity level to 3, as suggested in the documentation
*/
	mHandleDevForControlTransfer = libusb_open_device_with_vid_pid(ctxForControlTransfer, DEVICE_VID_FOR_CONTRALTRANSFER, DEVICE_PID_FOR_CONTRALTRANSFER);
	if(mHandleDevForControlTransfer == NULL)
	{
		if(gUsbDebugEnable)
			PrintError("libusb open device failed", __FILE__, __FUNCTION__, __LINE__, error);

		return DEIVCE_ERROR_OPEN_DEVICE;
	}
		
	if(libusb_kernel_driver_active(mHandleDevForControlTransfer, 0) == 1)
	{
		libusb_detach_kernel_driver(mHandleDevForControlTransfer, 0);
	}
		
	error = libusb_claim_interface(mHandleDevForControlTransfer, 0);
	if (error < 0) 
	{
		if(gUsbDebugEnable)
			PrintError("libusb claim interface failed ", __FILE__, __FUNCTION__, __LINE__, error);

		return DEIVCE_ERROR_OPEN_DEVICE;
	}
		
	return DEVICE_ERROR_OK;
}

上面,其中有一点需要注意的是,在USB-HID 协议中规定,控制传输默认使用interface 0,因此,在申请接口时,需要注意。

3.2 关闭设备

int CloseUsbControlTransferDevice()
{
	if(mHandleDevForControlTransfer != NULL)
	{
		libusb_release_interface(mHandleDevForControlTransfer, 0);
		libusb_close(mHandleDevForControlTransfer);	
	}

	if(ctxForControlTransfer != NULL)
		libusb_exit(ctxForControlTransfer);

	return DEVICE_ERROR_OK;
}

3.3 Host 主机 --》 HID 设备

error = libusb_control_transfer(mHandleDevForControlTransfer, 0x21, 0x09, 0x301, 0, dataStream,  0x100, timeout);
	if(error < 0)
		return DEVICE_ERROR_USB_SEND_ERROR;
	else
		return DEVICE_ERROR_OK;

3.4 HID 设备 --》 Host 主机

error = libusb_control_transfer(mHandleDevForControlTransfer, 0xA1, 0x01, 0x302, 0, dataStream,  0x100, timeout);	
	if(error < 0)
	{
		return DEVICE_ERROR_USB_REC_ERROR;
	}

关于libusb的控制传输就是记录这么多了,方便下次可以回顾。微笑

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐