aboutsummaryrefslogtreecommitdiff
path: root/examples/hotplugtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/hotplugtest.c')
-rw-r--r--examples/hotplugtest.c41
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) {