How get libusb_device if I have pid and vid?
- User
How get libusb_device if I have pid and vid?
- Assistant
To obtain a
libusb_devicepointer using known Vendor ID (VID) and Product ID (PID), you need to iterate through the list of connected USB devices, retrieve each device's descriptor, and compare the VID and PID with the ones you're searching for.Here's a step-by-step guide and a code example on how to achieve this:
-
Initialize libusb: Before any operations, ensure that libusb is properly initialized.
-
Get the Device List: Use
libusb_get_device_list()to retrieve the list of all connected USB devices. -
Iterate Through the Device List: Loop through each device in the list to examine its descriptors.
-
Retrieve Device Descriptor: For each device, use
libusb_get_device_descriptor()to get thelibusb_device_descriptor, which contains the VID and PID. -
Compare VID and PID: Check if the
idVendorandidProductmatch the VID and PID you're looking for. -
Handle the Found Device:
- If a match is found, you can:
- Increase the reference count of the device using
libusb_ref_device()to ensure it's not deallocated when freeing the device list. - Use the device as needed.
- Increase the reference count of the device using
- Remember to free the device list after you're done using
libusb_free_device_list().
- If a match is found, you can:
-
Close libusb: After all operations, close the libusb session if it's no longer needed.
Here's an example function demonstrating this process:
#include <libusb-1.0/libusb.h> libusb_device* get_device_by_vid_pid(uint16_t vid, uint16_t pid) { libusb_device **devs; libusb_device *found = NULL; ssize_t cnt, i; int r; // Initialize libusb r = libusb_init(NULL); if (r < 0) { // Initialization failed return NULL; } // Get the list of devices cnt = libusb_get_device_list(NULL, &devs); if (cnt < 0) { // Error in getting device list libusb_exit(NULL); return NULL; } // Iterate over each device for (i = 0; i < cnt; i++) { libusb_device *device = devs[i]; struct libusb_device_descriptor desc; // Get the device descriptor r = libusb_get_device_descriptor(device, &desc); if (r < 0) { // Failed to get descriptor, skip this device continue; } // Compare VID and PID if (desc.idVendor == vid && desc.idProduct == pid) { // Found the device; increase the reference count found = libusb_ref_device(device); break; } } // Free the device list, unref devices not selected libusb_free_device_list(devs, 1); // Don't forget to close libusb if you're done with all operations // libusb_exit(NULL); return found; // Return the found device or NULL if not found }Usage Example:
int main() { uint16_t vid = 0x1234; // Replace with your device's VID uint16_t pid = 0x5678; // Replace with your device's PID libusb_device *device = get_device_by_vid_pid(vid, pid); if (device) { // Device found, proceed with operations // ... // When done, unreference the device libusb_unref_device(device); } else { // Device not found } // Close libusb session libusb_exit(NULL); return 0; }Notes:
-
Reference Counting: When you obtain a
libusb_devicefrom the device list and plan to use it beyond the scope of the list (afterlibusb_free_device_list()is called), you need to increase its reference count usinglibusb_ref_device(). This prevents the device from being deallocated when the list is freed. -
Error Handling: Always check the return values of libusb functions for errors, especially when dealing with device I/O.
-
Thread Safety: If your application is multithreaded, ensure thread safety as libusb functions are not all guaranteed to be thread-safe.
-
Clean Up: Always release resources by unreferencing devices (
libusb_unref_device()) and exiting libusb (libusb_exit()) when they're no longer needed.
By following this method, you can reliably obtain a
libusb_devicepointer using the VID and PID of your USB device. -