diff options
author | Hans de Goede <hdegoede@redhat.com> | 2020-09-07 11:12:16 +0200 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2020-09-14 09:48:14 +0200 |
commit | dca3e7ea3659769e5097aae6ad0e1e8e8610890c (patch) | |
tree | e76a1cf32f09c2491352754d2832ba403ca395b0 | |
parent | f6068e83c4f5e5fba16b23b6a87f1f6d7ab7200a (diff) | |
download | libusb-dca3e7ea3659769e5097aae6ad0e1e8e8610890c.tar.gz |
examples/testlibusb: Add support for testing libusb_wrap_sys_device()
Sometimes it is useful to be able to test libusb_wrap_sys_device(),
add support for this to the testlibusb example
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
-rwxr-xr-x | examples/testlibusb.c | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/examples/testlibusb.c b/examples/testlibusb.c index 8031e75..f9e095a 100755 --- a/examples/testlibusb.c +++ b/examples/testlibusb.c @@ -160,10 +160,9 @@ static void print_configuration(struct libusb_config_descriptor *config) print_interface(&config->interface[i]); } -static void print_device(libusb_device *dev) +static void print_device(libusb_device *dev, libusb_device_handle *handle) { struct libusb_device_descriptor desc; - libusb_device_handle *handle = NULL; unsigned char string[256]; int ret; uint8_t i; @@ -178,8 +177,10 @@ static void print_device(libusb_device *dev) libusb_get_bus_number(dev), libusb_get_device_address(dev), desc.idVendor, desc.idProduct); - ret = libusb_open(dev, &handle); - if (LIBUSB_SUCCESS == ret) { + if (!handle) + libusb_open(dev, &handle); + + if (handle) { if (desc.iManufacturer) { ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string)); if (ret > 0) @@ -222,28 +223,78 @@ static void print_device(libusb_device *dev) libusb_close(handle); } +#ifdef __linux__ +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +static int test_wrapped_device(const char *device_name) +{ + libusb_device_handle *handle; + int r, fd; + + fd = open(device_name, O_RDWR); + if (fd < 0) { + printf("Error could not open %s: %s\n", device_name, strerror(errno)); + return 1; + } + r = libusb_wrap_sys_device(NULL, fd, &handle); + if (r) { + printf("Error wrapping device: %s: %s\n", device_name, libusb_strerror(r)); + close(fd); + return 1; + } + print_device(libusb_get_device(handle), handle); + close(fd); + return 0; +} +#else +static int test_wrapped_device(const char *device_name) +{ + printf("Testing wrapped devices is not supported on your platform\n"); + return 1; +} +#endif + int main(int argc, char *argv[]) { + const char *device_name = NULL; libusb_device **devs; ssize_t cnt; int r, i; - if (argc > 1 && !strcmp(argv[1], "-v")) - verbose = 1; + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-v")) { + verbose = 1; + } else if (!strcmp(argv[i], "-d") && (i + 1) < argc) { + i++; + device_name = argv[i]; + } else { + printf("Usage %s [-v] [-d </dev/bus/usb/...>]\n", argv[0]); + printf("Note use -d to test libusb_wrap_sys_device()\n"); + return 1; + } + } r = libusb_init(NULL); if (r < 0) return r; - cnt = libusb_get_device_list(NULL, &devs); - if (cnt < 0) - return (int)cnt; + if (device_name) { + r = test_wrapped_device(device_name); + } else { + cnt = libusb_get_device_list(NULL, &devs); + if (cnt < 0) { + libusb_exit(NULL); + return 1; + } - for (i = 0; devs[i]; i++) - print_device(devs[i]); + for (i = 0; devs[i]; i++) + print_device(devs[i], NULL); - libusb_free_device_list(devs, 1); + libusb_free_device_list(devs, 1); + } libusb_exit(NULL); - return 0; + return r; } |