[aravis/wip/emmanuel/usb3vision] usb: find USB devices



commit dbec80726eee92dea8b97e8e07ae9f490b826570
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Thu Mar 31 17:58:46 2016 +0200

    usb: find USB devices

 src/arvuvinterface.c |  113 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 112 insertions(+), 1 deletions(-)
---
diff --git a/src/arvuvinterface.c b/src/arvuvinterface.c
index 1359008..82f6a56 100644
--- a/src/arvuvinterface.c
+++ b/src/arvuvinterface.c
@@ -1,6 +1,6 @@
 /* Aravis - Digital camera library
  *
- * Copyright © 2009-2010 Emmanuel Pacaud
+ * Copyright © 2009-2016 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -28,6 +28,17 @@
 #include <arvuvinterface.h>
 #include <arvdebug.h>
 #include <arvmisc.h>
+#include <libusb.h>
+#include <stdio.h>
+
+#define ARV_UV_INTERFACE_DEVICE_CLASS                  0xef
+#define ARV_UV_INTERFACE_DEVICE_SUBCLASS               0x02
+#define ARV_UV_INTERFACE_DEVICE_PROTOCOL               0x01
+#define ARV_UV_INTERFACE_INTERFACE_CLASS               0xef
+#define ARV_UV_INTERFACE_INTERFACE_SUBCLASS            0x05
+#define ARV_UV_INTERFACE_CONTROL_INTERFACE_PROTOCOL    0x00
+#define ARV_UV_INTERFACE_EVENT_INTERFACE_PROTOCOL      0x01
+#define ARV_UV_INTERFACE_STREAMING_INTERFACE_PROTOCOL  0x02
 
 /* ArvUvInterface implementation */
 
@@ -35,11 +46,107 @@ static GObjectClass *parent_class = NULL;
 
 struct _ArvUvInterfacePrivate {
        GHashTable *devices;
+       libusb_context *usb;
 };
 
+
+#if 0
+static void
+printdev (libusb_device *device)
+{
+       struct libusb_device_descriptor desc;
+       struct libusb_config_descriptor *config;
+       const struct libusb_interface *inter;
+       const struct libusb_interface_descriptor *interdesc;
+       int r, i, j;
+
+       r = libusb_get_device_descriptor (device, &desc);
+
+       if (r < 0) {
+               printf ("Failed to get device descriptor\n");
+               return;
+       }
+
+       printf ("VendorID:          0x%04x\n", desc.idVendor);
+       printf ("ProductID:         0x%04x\n", desc.idProduct);
+       printf ("Device Class:      0x%02x\n", (int) desc.bDeviceClass);
+       printf ("Device SubClass:   0x%02x\n", (int) desc.bDeviceSubClass);
+       printf ("Protocol:          0x%02x\n", (int) desc.bDeviceProtocol);
+
+       libusb_get_config_descriptor (device, 0, &config);
+
+       printf ("Nbr of Interfaces: %d\n", (int)config->bNumInterfaces);
+       for (i = 0; i< (int) config->bNumInterfaces; i++) {
+               inter = &config->interface[i];
+               for (j = 0; j < inter->num_altsetting; j++) {
+                       interdesc = &inter->altsetting[j];
+                       printf ("  Interface Class:    0x%02x\n", (int) interdesc->bInterfaceClass);
+                       printf ("  Interface SubClass: 0x%02x\n", (int) interdesc->bInterfaceSubClass);
+                       printf ("  Interface Protocol: 0x%02x\n", (int) interdesc->bInterfaceProtocol);
+               }
+       }
+       libusb_free_config_descriptor (config);
+}
+#endif
+
+gboolean
+_usb_device_is_usb3vision (libusb_device *device)
+{
+       struct libusb_device_descriptor desc;
+       struct libusb_config_descriptor *config;
+       const struct libusb_interface *inter;
+       const struct libusb_interface_descriptor *interdesc;
+       gboolean success = TRUE;
+       int r, i, j;
+
+       r = libusb_get_device_descriptor (device, &desc);
+       if (r < 0) {
+               g_warning ("Failed to get device descriptor");
+               return FALSE;
+       }
+
+       if (desc.bDeviceClass != ARV_UV_INTERFACE_DEVICE_CLASS ||
+           desc.bDeviceSubClass != ARV_UV_INTERFACE_DEVICE_SUBCLASS ||
+           desc.bDeviceProtocol != ARV_UV_INTERFACE_DEVICE_PROTOCOL)
+               return FALSE;
+
+       libusb_get_config_descriptor (device, 0, &config);
+       for (i = 0; i< (int) config->bNumInterfaces; i++) {
+               inter = &config->interface[i];
+               for (j = 0; j < inter->num_altsetting; j++) {
+                       interdesc = &inter->altsetting[j];
+                       if (interdesc->bInterfaceClass != ARV_UV_INTERFACE_INTERFACE_CLASS ||
+                           interdesc->bInterfaceSubClass != ARV_UV_INTERFACE_INTERFACE_SUBCLASS)
+                               success = FALSE;
+               }
+       }
+       libusb_free_config_descriptor (config);
+
+       return success;
+}
+
 static void
 arv_uv_interface_update_device_list (ArvInterface *interface, GArray *device_ids)
 {
+       ArvUvInterface *uv_interface = ARV_UV_INTERFACE (interface);
+       libusb_device **devices;
+       unsigned uv_count = 0;
+       unsigned count;
+       unsigned i;
+
+       count = libusb_get_device_list(uv_interface->priv->usb, &devices);
+       if (count < 0)
+               return;
+
+       for (i = 0; i < count; i++)
+               if (_usb_device_is_usb3vision (devices[i]))
+                   uv_count++;
+
+       arv_debug_interface ("Found %d USB3Vision device%s (among %d USB device%ss)\n",
+                            uv_count , uv_count > 1 ? "s" : "",
+                            count, count > 1 ? "s" : "");
+
+       libusb_free_device_list (devices, 1);
 }
 
 static ArvDevice *
@@ -86,12 +193,16 @@ static void
 arv_uv_interface_init (ArvUvInterface *uv_interface)
 {
        uv_interface->priv = G_TYPE_INSTANCE_GET_PRIVATE (uv_interface, ARV_TYPE_UV_INTERFACE, 
ArvUvInterfacePrivate);
+       libusb_init (&uv_interface->priv->usb);
 }
 
 static void
 arv_uv_interface_finalize (GObject *object)
 {
+       ArvUvInterface *uv_interface = ARV_UV_INTERFACE (object);
+
        parent_class->finalize (object);
+       libusb_exit (uv_interface->priv->usb);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]