diff options
author | Daniel Drake <dsd@gentoo.org> | 2008-03-06 23:25:20 +0000 |
---|---|---|
committer | Daniel Drake <dsd@gentoo.org> | 2008-03-06 23:25:20 +0000 |
commit | 9cfdb494fccac53a4277da7c8b6d15f1a72a4959 (patch) | |
tree | a6aa93d20fe9768534074b8c5792999bf45f9add /examples/dpfp.c | |
parent | 77cea822788e024c848c7f554915f771a2dc1e0f (diff) | |
download | libusb-9cfdb494fccac53a4277da7c8b6d15f1a72a4959.tar.gz |
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.
Diffstat (limited to 'examples/dpfp.c')
-rw-r--r-- | examples/dpfp.c | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/examples/dpfp.c b/examples/dpfp.c index d99b3af..87a7da1 100644 --- a/examples/dpfp.c +++ b/examples/dpfp.c @@ -21,6 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include <errno.h> #include <signal.h> #include <string.h> #include <stdio.h> @@ -78,19 +79,38 @@ static struct libusb_bulk_transfer irqtrf = { .length = sizeof(irqbuf), }; -static struct libusb_dev *find_dpfp_device(void) +static int find_dpfp_device(void) { - struct libusb_dev *dev; + struct libusb_device *dev; + struct libusb_device *found = NULL; + struct libusb_device **devs; + size_t i = 0; + int r; + + r = libusb_get_device_list(&devs); + if (r < 0) + return r; - libusb_find_devices(); + while ((dev = devs[i++]) != NULL) { + struct libusb_dev_descriptor *desc = libusb_device_get_descriptor(dev); + if (desc->idVendor == 0x05ba && desc->idProduct == 0x000a) { + found = dev; + break; + } + } - for (dev = libusb_get_devices(); dev; dev = libusb_dev_next(dev)) { - struct libusb_dev_descriptor *desc = libusb_dev_get_descriptor(dev); - if (desc->idVendor == 0x05ba && desc->idProduct == 0x000a) - return dev; + if (!found) { + r = -ENODEV; + goto out; } - return NULL; + devh = libusb_open(dev); + if (!devh) + r = -EIO; + +out: + libusb_free_device_list(devs, 1); + return r; } static int print_f0_data(void) @@ -469,7 +489,6 @@ static void sighandler(int signum) int main(void) { - struct libusb_dev *dev; struct sigaction sigact; int r = 1; @@ -479,19 +498,11 @@ int main(void) exit(1); } - dev = find_dpfp_device(); - if (!dev) { - fprintf(stderr, "No device found\n"); - goto out; - } - printf("found device\n"); - - devh = libusb_open(dev); - if (!devh) { - fprintf(stderr, "Could not open device\n"); + r = find_dpfp_device(); + if (r < 0) { + fprintf(stderr, "Could not find/open device\n"); goto out; } - printf("opened device\n"); r = libusb_claim_interface(devh, 0); if (r < 0) { |