1

I have some code, which can find all USB devices, their VendorID, and ProductID, which are connected.

I need a program that can find a connected device by VID and PID from the monitor or touchpad. I found the libusb_class_code that has class video, but I didn't find any functions that return libusb_class_code.

libusb_context *context = nullptr;
libusb_device **list = nullptr;

libusb_init(&context);
int count = libusb_get_device_list(context, &list);

for (size_t idx = 0; idx < count; ++idx)
{
    libusb_device *device = list[idx];
    libusb_device_descriptor desc = { 0 };

    libusb_get_device_descriptor(device, &desc);

    cout << "idVendor  " << desc.idVendor << "\t";
    cout << "idProduct " << desc.idProduct << endl;

}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mike1995
  • 11
  • 1
  • 3

1 Answers1

2

If you want to access to a given device by VID and PID, there is a dedicated function libusb_open_device_with_vid_pid.

This is a simple example that shows how to open the device, handle the interface, read the data and then close it.

libusb_context       *context    = NULL   ;
libusb_device_handle *dev_handle = NULL   ;
libusb_device        **devs               ;
int                  rc          = 0      ;
ssize_t              count                ; //holding number of devices in list

//----------------------------------------------------------------------------
// Initialize the library
//----------------------------------------------------------------------------
rc = libusb_init(&context);
assert(rc == 0);

//----------------------------------------------------------------------------
// open usb device by vendor ID and Product ID
//----------------------------------------------------------------------------
dev_handle = libusb_open_device_with_vid_pid(context,VENDOR_ID,PRODUCT_ID);
assert(dev_handle == NULL);

//----------------------------------------------------------------------------
// Check that the kernel is attached
//----------------------------------------------------------------------------
if(libusb_kernel_driver_active(dev_handle, 0))
{
    rc = libusb_detach_kernel_driver(dev_handle, 0); // detach driver
    assert(rc == 0);
}

//----------------------------------------------------------------------------
// claim the interface
//----------------------------------------------------------------------------
rc = libusb_claim_interface(dev_handle, 0);
assert(rc < 0);

//----------------------------------------------------------------------------
// start the bulk transfer
//----------------------------------------------------------------------------
rc = libusb_bulk_transfer(dev_handle, (64 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);
assert (rc != 0 || actual != 5);

//----------------------------------------------------------------------------
// release the interface before closing the device
//----------------------------------------------------------------------------
rc = libusb_release_interface(dev_handle, 0);
assert(rc != 0);

//----------------------------------------------------------------------------
// close the device
//----------------------------------------------------------------------------
libusb_close(dev_handle);

//----------------------------------------------------------------------------
// exit
//----------------------------------------------------------------------------
libusb_exit(context);


mohabouje
  • 3,867
  • 2
  • 14
  • 28