Libusb is opened according to the serialnumber of the device

Source: Internet
Author: User

In the Libusb open source source, there are based on VID and PID to open the interface of the device. However, this is not the only device. Because there are a lot of devices vid and PID are the same, such as the iphone.

But the serial number of the device is unique, so according to the reference vid and PID open source code, the following is the opening of a designated serialnumber device.

libusb_device_handle* Libusb_open_device_with_serialnumber (
libusb_context* ctx, const char* serial_number) {
struct libusb_device** devs;
struct libusb_device* dev;
struct libusb_device_handle* handle = NULL;
Char string[128];
Char serialnumber[128] = "";
uint8_t String_index[3];
size_t i = 0;
int r = 0;

if (Serial_number)
strcpy (SerialNumber, Serial_number);

if (Libusb_get_device_list (CTX, &devs) < 0)
return NULL;

while (dev = devs[i++])! = NULL) {
struct Libusb_device_descriptor desc;
r = Libusb_get_device_descriptor (Dev, &desc);

if (R < 0)
Goto out;

STRING_INDEX[2] = Desc.iserialnumber;
r = Libusb_open (Dev, &handle);

if (r < 0) {
handle = NULL;
Break
}

if (Libusb_get_string_descriptor_ascii (handle, string_index[2], (unsigned char*) string, $) >= 0) {
if (!strcmp (SerialNumber, String))
Break
}

Libusb_close (handle);
handle = NULL;
}

Out
Libusb_free_device_list (devs, 1);
return handle;
}

Libusb is opened according to the serialnumber of the device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.