Rework device discovery API
libusb_find_devices and libusb_get_devices are no more
libusb_get_device_list obtains a list of libusb_device structures for all
known devices in the system.
Each libusb_device now has a reference count, defaulting to 1 on
instantiation. The reference count of 1 refers to the fact that it is
present in the list in this scenario.
Opening a device adds a pointer to the libusb_device structure in the
handle, so that also adds a reference. Closing the device removes that
reference.
The function to free the device list can optionally unref all the devices
inside.
In future we will make the libusb_device instances all "global" so that if
the app calls get_device_list twice it actually gets the same libusb_device
structure references back. This way we can start to track disconnects, and
we can investigate adding a unique "session ID" to each libusb_device, an
identifier guaranteed to be unique to that device until reboot.
diff --git a/examples/lsusb.c b/examples/lsusb.c
index c511b29..d592fee 100644
--- a/examples/lsusb.c
+++ b/examples/lsusb.c
@@ -21,29 +21,32 @@
#include <libusb/libusb.h>
-void print_devs(libusb_dev *devs)
+void print_devs(libusb_device **devs)
{
- libusb_dev *dev;
+ libusb_device *dev;
+ int i;
- for (dev = devs; dev; dev = libusb_dev_next(dev)) {
- struct libusb_dev_descriptor *desc = libusb_dev_get_descriptor(dev);
+ while ((dev = devs[i++]) != NULL) {
+ struct libusb_dev_descriptor *desc = libusb_device_get_descriptor(dev);
printf("%04x:%04x\n", desc->idVendor, desc->idProduct);
}
}
int main(void)
{
- libusb_dev *devs;
+ libusb_device **devs;
int r;
r = libusb_init();
if (r < 0)
return r;
- libusb_find_devices();
- devs = libusb_get_devices();
+ r = libusb_get_device_list(&devs);
+ if (r < 0)
+ return r;
print_devs(devs);
+ libusb_free_device_list(devs, 1);
libusb_exit();
return 0;