Add libusb and libuvc
[rtmpclient.git] / app / src / main / jni / libusb-1.0.22 / examples / testlibusb.c
1 /*
2 * Test suite program based of libusb-0.1-compat testlibusb
3 * Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "libusb.h"
23
24 #if defined(_MSC_VER) && (_MSC_VER < 1900)
25 #define snprintf _snprintf
26 #endif
27
28 int verbose = 0;
29
30 static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
31 {
32         printf("      USB 3.0 Endpoint Companion:\n");
33         printf("        bMaxBurst:        %d\n", ep_comp->bMaxBurst);
34         printf("        bmAttributes:     0x%02x\n", ep_comp->bmAttributes);
35         printf("        wBytesPerInterval: %d\n", ep_comp->wBytesPerInterval);
36 }
37
38 static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
39 {
40         int i, ret;
41
42         printf("      Endpoint:\n");
43         printf("        bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
44         printf("        bmAttributes:     %02xh\n", endpoint->bmAttributes);
45         printf("        wMaxPacketSize:   %d\n", endpoint->wMaxPacketSize);
46         printf("        bInterval:        %d\n", endpoint->bInterval);
47         printf("        bRefresh:         %d\n", endpoint->bRefresh);
48         printf("        bSynchAddress:    %d\n", endpoint->bSynchAddress);
49
50         for (i = 0; i < endpoint->extra_length;) {
51                 if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {
52                         struct libusb_ss_endpoint_companion_descriptor *ep_comp;
53
54                         ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
55                         if (LIBUSB_SUCCESS != ret) {
56                                 continue;
57                         }
58
59                         print_endpoint_comp(ep_comp);
60
61                         libusb_free_ss_endpoint_companion_descriptor(ep_comp);
62                 }
63
64                 i += endpoint->extra[i];
65         }
66 }
67
68 static void print_altsetting(const struct libusb_interface_descriptor *interface)
69 {
70         uint8_t i;
71
72         printf("    Interface:\n");
73         printf("      bInterfaceNumber:   %d\n", interface->bInterfaceNumber);
74         printf("      bAlternateSetting:  %d\n", interface->bAlternateSetting);
75         printf("      bNumEndpoints:      %d\n", interface->bNumEndpoints);
76         printf("      bInterfaceClass:    %d\n", interface->bInterfaceClass);
77         printf("      bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
78         printf("      bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
79         printf("      iInterface:         %d\n", interface->iInterface);
80
81         for (i = 0; i < interface->bNumEndpoints; i++)
82                 print_endpoint(&interface->endpoint[i]);
83 }
84
85 static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)
86 {
87         printf("    USB 2.0 Extension Capabilities:\n");
88         printf("      bDevCapabilityType: %d\n", usb_2_0_ext_cap->bDevCapabilityType);
89         printf("      bmAttributes:       0x%x\n", usb_2_0_ext_cap->bmAttributes);
90 }
91
92 static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
93 {
94         printf("    USB 3.0 Capabilities:\n");
95         printf("      bDevCapabilityType: %d\n", ss_usb_cap->bDevCapabilityType);
96         printf("      bmAttributes:       0x%x\n", ss_usb_cap->bmAttributes);
97         printf("      wSpeedSupported:    0x%x\n", ss_usb_cap->wSpeedSupported);
98         printf("      bFunctionalitySupport: %d\n", ss_usb_cap->bFunctionalitySupport);
99         printf("      bU1devExitLat:      %d\n", ss_usb_cap->bU1DevExitLat);
100         printf("      bU2devExitLat:      %d\n", ss_usb_cap->bU2DevExitLat);
101 }
102
103 static void print_bos(libusb_device_handle *handle)
104 {
105         struct libusb_bos_descriptor *bos;
106         int ret;
107
108         ret = libusb_get_bos_descriptor(handle, &bos);
109         if (0 > ret) {
110                 return;
111         }
112
113         printf("  Binary Object Store (BOS):\n");
114         printf("    wTotalLength:       %d\n", bos->wTotalLength);
115         printf("    bNumDeviceCaps:     %d\n", bos->bNumDeviceCaps);
116
117         if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
118
119                 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
120                 ret =  libusb_get_usb_2_0_extension_descriptor(NULL, bos->dev_capability[0],&usb_2_0_extension);
121                 if (0 > ret) {
122                         return;
123                 }
124
125                 print_2_0_ext_cap(usb_2_0_extension);
126                 libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
127         }
128
129         if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
130
131                 struct libusb_ss_usb_device_capability_descriptor *dev_cap;
132                 ret = libusb_get_ss_usb_device_capability_descriptor(NULL, bos->dev_capability[0],&dev_cap);
133                 if (0 > ret) {
134                         return;
135                 }
136
137                 print_ss_usb_cap(dev_cap);
138                 libusb_free_ss_usb_device_capability_descriptor(dev_cap);
139         }
140
141         libusb_free_bos_descriptor(bos);
142 }
143
144 static void print_interface(const struct libusb_interface *interface)
145 {
146         int i;
147
148         for (i = 0; i < interface->num_altsetting; i++)
149                 print_altsetting(&interface->altsetting[i]);
150 }
151
152 static void print_configuration(struct libusb_config_descriptor *config)
153 {
154         uint8_t i;
155
156         printf("  Configuration:\n");
157         printf("    wTotalLength:         %d\n", config->wTotalLength);
158         printf("    bNumInterfaces:       %d\n", config->bNumInterfaces);
159         printf("    bConfigurationValue:  %d\n", config->bConfigurationValue);
160         printf("    iConfiguration:       %d\n", config->iConfiguration);
161         printf("    bmAttributes:         %02xh\n", config->bmAttributes);
162         printf("    MaxPower:             %d\n", config->MaxPower);
163
164         for (i = 0; i < config->bNumInterfaces; i++)
165                 print_interface(&config->interface[i]);
166 }
167
168 static int print_device(libusb_device *dev, int level)
169 {
170         struct libusb_device_descriptor desc;
171         libusb_device_handle *handle = NULL;
172         char description[256];
173         char string[256];
174         int ret;
175         uint8_t i;
176
177         ret = libusb_get_device_descriptor(dev, &desc);
178         if (ret < 0) {
179                 fprintf(stderr, "failed to get device descriptor");
180                 return -1;
181         }
182
183         ret = libusb_open(dev, &handle);
184         if (LIBUSB_SUCCESS == ret) {
185                 if (desc.iManufacturer) {
186                         ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
187                         if (ret > 0)
188                                 snprintf(description, sizeof(description), "%s - ", string);
189                         else
190                                 snprintf(description, sizeof(description), "%04X - ",
191                                 desc.idVendor);
192                 }
193                 else
194                         snprintf(description, sizeof(description), "%04X - ",
195                         desc.idVendor);
196
197                 if (desc.iProduct) {
198                         ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
199                         if (ret > 0)
200                                 snprintf(description + strlen(description), sizeof(description) -
201                                 strlen(description), "%s", string);
202                         else
203                                 snprintf(description + strlen(description), sizeof(description) -
204                                 strlen(description), "%04X", desc.idProduct);
205                 }
206                 else
207                         snprintf(description + strlen(description), sizeof(description) -
208                         strlen(description), "%04X", desc.idProduct);
209         }
210         else {
211                 snprintf(description, sizeof(description), "%04X - %04X",
212                         desc.idVendor, desc.idProduct);
213         }
214
215         printf("%.*sDev (bus %d, device %d): %s\n", level * 2, "                    ",
216                 libusb_get_bus_number(dev), libusb_get_device_address(dev), description);
217
218         if (handle && verbose) {
219                 if (desc.iSerialNumber) {
220                         ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
221                         if (ret > 0)
222                                 printf("%.*s  - Serial Number: %s\n", level * 2,
223                                 "                    ", string);
224                 }
225         }
226
227         if (verbose) {
228                 for (i = 0; i < desc.bNumConfigurations; i++) {
229                         struct libusb_config_descriptor *config;
230                         ret = libusb_get_config_descriptor(dev, i, &config);
231                         if (LIBUSB_SUCCESS != ret) {
232                                 printf("  Couldn't retrieve descriptors\n");
233                                 continue;
234                         }
235
236                         print_configuration(config);
237
238                         libusb_free_config_descriptor(config);
239                 }
240
241                 if (handle && desc.bcdUSB >= 0x0201) {
242                         print_bos(handle);
243                 }
244         }
245
246         if (handle)
247                 libusb_close(handle);
248
249         return 0;
250 }
251
252 int main(int argc, char *argv[])
253 {
254         libusb_device **devs;
255         ssize_t cnt;
256         int r, i;
257
258         if (argc > 1 && !strcmp(argv[1], "-v"))
259                 verbose = 1;
260
261         r = libusb_init(NULL);
262         if (r < 0)
263                 return r;
264
265         cnt = libusb_get_device_list(NULL, &devs);
266         if (cnt < 0)
267                 return (int)cnt;
268
269         for (i = 0; devs[i]; ++i) {
270                 print_device(devs[i], 0);
271         }
272
273         libusb_free_device_list(devs, 1);
274
275         libusb_exit(NULL);
276         return 0;
277 }