diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-02-28 21:26:14 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2024-02-28 21:26:14 +0000 |
commit | 5a77d36f70c85ba4e7cb945038acf704e9fd8531 (patch) | |
tree | fb17f5ca24925515f5ab736101ba199cb6ec194d /examples/hotplugtest.c | |
parent | 62660570fd3c6ad4b8b3ebf57f59f3d84b342fc3 (diff) | |
parent | 360128458561b5e51cdeb2588f01056a3b0d2307 (diff) | |
download | libusb-5a77d36f70c85ba4e7cb945038acf704e9fd8531.tar.gz |
Merge "Snap for 11510257 from 9e9370211c8d37301902a086424a176f351669f4 to simpleperf-release" into simpleperf-releasesimpleperf-release
Diffstat (limited to 'examples/hotplugtest.c')
-rw-r--r-- | examples/hotplugtest.c | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/examples/hotplugtest.c b/examples/hotplugtest.c index 94f7e56..3e092cf 100644 --- a/examples/hotplugtest.c +++ b/examples/hotplugtest.c @@ -37,12 +37,14 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, (void)user_data; rc = libusb_get_device_descriptor(dev, &desc); - if (LIBUSB_SUCCESS != rc) { - fprintf (stderr, "Error getting device descriptor\n"); + if (LIBUSB_SUCCESS == rc) { + printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct); + } else { + printf ("Device attached\n"); + fprintf (stderr, "Error getting device descriptor: %s\n", + libusb_strerror((enum libusb_error)rc)); } - printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct); - if (handle) { libusb_close (handle); handle = NULL; @@ -50,7 +52,8 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, rc = libusb_open (dev, &handle); if (LIBUSB_SUCCESS != rc) { - fprintf (stderr, "Error opening device\n"); + fprintf (stderr, "No access to device: %s\n", + libusb_strerror((enum libusb_error)rc)); } done++; @@ -60,12 +63,22 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) { + struct libusb_device_descriptor desc; + int rc; + (void)ctx; (void)dev; (void)event; (void)user_data; - printf ("Device detached\n"); + rc = libusb_get_device_descriptor(dev, &desc); + if (LIBUSB_SUCCESS == rc) { + printf ("Device detached: %04x:%04x\n", desc.idVendor, desc.idProduct); + } else { + printf ("Device detached\n"); + fprintf (stderr, "Error getting device descriptor: %s\n", + libusb_strerror((enum libusb_error)rc)); + } if (handle) { libusb_close (handle); @@ -83,14 +96,15 @@ int main(int argc, char *argv[]) int product_id, vendor_id, class_id; int rc; - vendor_id = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : 0x045a; - product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : 0x5005; + vendor_id = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY; + product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY; class_id = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY; - rc = libusb_init (NULL); - if (rc < 0) + rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0); + if (LIBUSB_SUCCESS != rc) { - printf("failed to initialise libusb: %s\n", libusb_error_name(rc)); + printf ("failed to initialise libusb: %s\n", + libusb_strerror((enum libusb_error)rc)); return EXIT_FAILURE; } @@ -118,8 +132,9 @@ int main(int argc, char *argv[]) while (done < 2) { rc = libusb_handle_events (NULL); - if (rc < 0) - printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc)); + if (LIBUSB_SUCCESS != rc) + printf ("libusb_handle_events() failed: %s\n", + libusb_strerror((enum libusb_error)rc)); } if (handle) { |