Examples: Ported testlibusb from libus-0.1 to libusb-1.0
authorAnil Nair <anilcoll90@gmail.com>
Sat, 2 Apr 2016 00:59:11 +0000 (06:29 +0530)
committerNathan Hjelm <hjelmn@me.com>
Fri, 22 Jul 2016 04:08:44 +0000 (22:08 -0600)
This commit is based on Nathan's branch
https://github.com/hjelmn/libusb-darwin/blob/master/examples/testlibusb1.c

Closes #178

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
examples/Makefile.am
examples/testlibusb.c [new file with mode: 0755]
libusb/version_nano.h

index 8081927..6185d00 100644 (file)
@@ -1,7 +1,7 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libusb
 LDADD = ../libusb/libusb-1.0.la
 
-noinst_PROGRAMS = listdevs xusb fxload hotplugtest
+noinst_PROGRAMS = listdevs xusb fxload hotplugtest testlibusb
 
 if HAVE_SIGACTION
 noinst_PROGRAMS += dpfp
diff --git a/examples/testlibusb.c b/examples/testlibusb.c
new file mode 100755 (executable)
index 0000000..b7068c9
--- /dev/null
@@ -0,0 +1,273 @@
+/*\r
+* Test suite program based of libusb-0.1-compat testlibusb\r
+* Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>\r
+*\r
+* This library is free software; you can redistribute it and/or\r
+* modify it under the terms of the GNU Lesser General Public\r
+* License as published by the Free Software Foundation; either\r
+* version 2.1 of the License, or (at your option) any later version.\r
+*\r
+* This library is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+* Lesser General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU Lesser General Public\r
+* License along with this library; if not, write to the Free Software\r
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\r
+*/\r
+\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include "libusb.h"\r
+\r
+int verbose = 0;\r
+\r
+static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)\r
+{\r
+       printf("      USB 3.0 Endpoint Companion:\n");\r
+       printf("        bMaxBurst:        %d\n", ep_comp->bMaxBurst);\r
+       printf("        bmAttributes:     0x%02x\n", ep_comp->bmAttributes);\r
+       printf("        wBytesPerInterval: %d\n", ep_comp->wBytesPerInterval);\r
+}\r
+\r
+static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)\r
+{\r
+       int i, ret;\r
+\r
+       printf("      Endpoint:\n");\r
+       printf("        bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);\r
+       printf("        bmAttributes:     %02xh\n", endpoint->bmAttributes);\r
+       printf("        wMaxPacketSize:   %d\n", endpoint->wMaxPacketSize);\r
+       printf("        bInterval:        %d\n", endpoint->bInterval);\r
+       printf("        bRefresh:         %d\n", endpoint->bRefresh);\r
+       printf("        bSynchAddress:    %d\n", endpoint->bSynchAddress);\r
+\r
+       for (i = 0; i < endpoint->extra_length;) {\r
+               if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {\r
+                       struct libusb_ss_endpoint_companion_descriptor *ep_comp;\r
+\r
+                       ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);\r
+                       if (LIBUSB_SUCCESS != ret) {\r
+                               continue;\r
+                       }\r
+\r
+                       print_endpoint_comp(ep_comp);\r
+\r
+                       libusb_free_ss_endpoint_companion_descriptor(ep_comp);\r
+               }\r
+\r
+               i += endpoint->extra[i];\r
+       }\r
+}\r
+\r
+static void print_altsetting(const struct libusb_interface_descriptor *interface)\r
+{\r
+       int i;\r
+\r
+       printf("    Interface:\n");\r
+       printf("      bInterfaceNumber:   %d\n", interface->bInterfaceNumber);\r
+       printf("      bAlternateSetting:  %d\n", interface->bAlternateSetting);\r
+       printf("      bNumEndpoints:      %d\n", interface->bNumEndpoints);\r
+       printf("      bInterfaceClass:    %d\n", interface->bInterfaceClass);\r
+       printf("      bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);\r
+       printf("      bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);\r
+       printf("      iInterface:         %d\n", interface->iInterface);\r
+\r
+       for (i = 0; i < interface->bNumEndpoints; i++)\r
+               print_endpoint(&interface->endpoint[i]);\r
+}\r
+\r
+static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)\r
+{\r
+       printf("    USB 2.0 Extension Capabilities:\n");\r
+       printf("      bDevCapabilityType: %d\n", usb_2_0_ext_cap->bDevCapabilityType);\r
+       printf("      bmAttributes:       0x%x\n", usb_2_0_ext_cap->bmAttributes);\r
+}\r
+\r
+static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)\r
+{\r
+       printf("    USB 3.0 Capabilities:\n");\r
+       printf("      bDevCapabilityType: %d\n", ss_usb_cap->bDevCapabilityType);\r
+       printf("      bmAttributes:       0x%x\n", ss_usb_cap->bmAttributes);\r
+       printf("      wSpeedSupported:    0x%x\n", ss_usb_cap->wSpeedSupported);\r
+       printf("      bFunctionalitySupport: %d\n", ss_usb_cap->bFunctionalitySupport);\r
+       printf("      bU1devExitLat:      %d\n", ss_usb_cap->bU1DevExitLat);\r
+       printf("      bU2devExitLat:      %d\n", ss_usb_cap->bU2DevExitLat);\r
+}\r
+\r
+static void print_bos(libusb_device_handle *handle)\r
+{\r
+       struct libusb_bos_descriptor *bos;\r
+       int ret;\r
+\r
+       ret = libusb_get_bos_descriptor(handle, &bos);\r
+       if (0 > ret) {\r
+               return;\r
+       }\r
+       \r
+       printf("  Binary Object Store (BOS):\n");\r
+       printf("    wTotalLength:       %d\n", bos->wTotalLength);\r
+       printf("    bNumDeviceCaps:     %d\n", bos->bNumDeviceCaps);\r
+               \r
+       if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {\r
+       \r
+               struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;\r
+               ret =  libusb_get_usb_2_0_extension_descriptor(NULL, bos->dev_capability[0],&usb_2_0_extension);\r
+               if (0 > ret) {\r
+                       return;\r
+               }\r
+               \r
+                print_2_0_ext_cap(usb_2_0_extension);\r
+                libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);\r
+        }\r
+       \r
+       if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {\r
+       \r
+               struct libusb_ss_usb_device_capability_descriptor *dev_cap;\r
+               ret = libusb_get_ss_usb_device_capability_descriptor(NULL, bos->dev_capability[0],&dev_cap);\r
+               if (0 > ret) {\r
+                       return;\r
+               }\r
+               \r
+               print_ss_usb_cap(dev_cap);\r
+               libusb_free_ss_usb_device_capability_descriptor(dev_cap);\r
+        }\r
+        \r
+       libusb_free_bos_descriptor(bos);\r
+}\r
+\r
+static void print_interface(const struct libusb_interface *interface)\r
+{\r
+       int i;\r
+\r
+       for (i = 0; i < interface->num_altsetting; i++)\r
+               print_altsetting(&interface->altsetting[i]);\r
+}\r
+\r
+static void print_configuration(struct libusb_config_descriptor *config)\r
+{\r
+       int i;\r
+\r
+       printf("  Configuration:\n");\r
+       printf("    wTotalLength:         %d\n", config->wTotalLength);\r
+       printf("    bNumInterfaces:       %d\n", config->bNumInterfaces);\r
+       printf("    bConfigurationValue:  %d\n", config->bConfigurationValue);\r
+       printf("    iConfiguration:       %d\n", config->iConfiguration);\r
+       printf("    bmAttributes:         %02xh\n", config->bmAttributes);\r
+       printf("    MaxPower:             %d\n", config->MaxPower);\r
+\r
+       for (i = 0; i < config->bNumInterfaces; i++)\r
+               print_interface(&config->interface[i]);\r
+}\r
+\r
+static int print_device(libusb_device *dev, int level)\r
+{\r
+       struct libusb_device_descriptor desc;\r
+       libusb_device_handle *handle = NULL;\r
+       char description[256];\r
+       char string[256];\r
+       int ret, i;\r
+\r
+       ret = libusb_get_device_descriptor(dev, &desc);\r
+       if (ret < 0) {\r
+               fprintf(stderr, "failed to get device descriptor");\r
+               return -1;\r
+       }\r
+\r
+       ret = libusb_open(dev, &handle);\r
+       if (LIBUSB_SUCCESS == ret) {\r
+               if (desc.iManufacturer) {\r
+                       ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));\r
+                       if (ret > 0)\r
+                               snprintf(description, sizeof(description), "%s - ", string);\r
+                       else\r
+                               snprintf(description, sizeof(description), "%04X - ",\r
+                               desc.idVendor);\r
+               }\r
+               else\r
+                       snprintf(description, sizeof(description), "%04X - ",\r
+                       desc.idVendor);\r
+\r
+               if (desc.iProduct) {\r
+                       ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));\r
+                       if (ret > 0)\r
+                               snprintf(description + strlen(description), sizeof(description) -\r
+                               strlen(description), "%s", string);\r
+                       else\r
+                               snprintf(description + strlen(description), sizeof(description) -\r
+                               strlen(description), "%04X", desc.idProduct);\r
+               }\r
+               else\r
+                       snprintf(description + strlen(description), sizeof(description) -\r
+                       strlen(description), "%04X", desc.idProduct);\r
+       }\r
+       else {\r
+               snprintf(description, sizeof(description), "%04X - %04X",\r
+                       desc.idVendor, desc.idProduct);\r
+       }\r
+\r
+       printf("%.*sDev (bus %d, device %d): %s\n", level * 2, "                    ",\r
+               libusb_get_bus_number(dev), libusb_get_device_address(dev), description);\r
+\r
+       if (handle && verbose) {\r
+               if (desc.iSerialNumber) {\r
+                       ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));\r
+                       if (ret > 0)\r
+                               printf("%.*s  - Serial Number: %s\n", level * 2,\r
+                               "                    ", string);\r
+               }\r
+       }\r
+\r
+       if (verbose) {\r
+               for (i = 0; i < desc.bNumConfigurations; i++) {\r
+                       struct libusb_config_descriptor *config;\r
+                       ret = libusb_get_config_descriptor(dev, i, &config);\r
+                       if (LIBUSB_SUCCESS != ret) {\r
+                               printf("  Couldn't retrieve descriptors\n");\r
+                               continue;\r
+                       }\r
+\r
+                       print_configuration(config);\r
+\r
+                       libusb_free_config_descriptor(config);\r
+               }\r
+\r
+                \r
+               if (handle && desc.bcdUSB >= 0x0201) {\r
+                       print_bos(handle);\r
+               }\r
+       }\r
+\r
+       if (handle)\r
+               libusb_close(handle);\r
+\r
+       return 0;\r
+}\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+       libusb_device **devs;\r
+       ssize_t cnt;\r
+       int r, i;\r
+\r
+       if (argc > 1 && !strcmp(argv[1], "-v"))\r
+               verbose = 1;\r
+\r
+       r = libusb_init(NULL);\r
+       if (r < 0)\r
+               return r;\r
+\r
+       cnt = libusb_get_device_list(NULL, &devs);\r
+       if (cnt < 0)\r
+               return (int)cnt;\r
+\r
+       for (i = 0; devs[i]; ++i) {\r
+               print_device(devs[i], 0);\r
+       }\r
+\r
+       libusb_free_device_list(devs, 1);\r
+\r
+       libusb_exit(NULL);\r
+       return 0;\r
+}\r
index 7b2b87c..d9f9843 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11115
+#define LIBUSB_NANO 11117