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