summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDaniel Drake <dsd@gentoo.org>2008-03-06 23:25:20 +0000
committerDaniel Drake <dsd@gentoo.org>2008-03-06 23:25:20 +0000
commit9cfdb494fccac53a4277da7c8b6d15f1a72a4959 (patch)
treea6aa93d20fe9768534074b8c5792999bf45f9add /examples
parent77cea822788e024c848c7f554915f771a2dc1e0f (diff)
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')
-rw-r--r--examples/dpfp.c51
-rw-r--r--examples/lsusb.c17
2 files changed, 41 insertions, 27 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) {
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;