We use libusbx 1.0.16 in a Cocoa application in order to sync one or more 
devices with a computer. Before upgrading from an earlier version of libusb we 
had a hotplug listener built with IOKit, and then checked with libusb for the 
new device.

Now we would like to trash the IOKit + libusb solution and go with just libusb, 
since libusbx 1.0.16 comes with hotplug support.

I have followed the example for setting up device hotplug support, found 
[here](http://libusbx.sourceforge.net/api-1.0/hotplug.html), and this is what 
my callback currently looks like:

```c
int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, 
libusb_hotplug_event event, void *user_data) {
    static libusb_device_handle *handle = NULL;
    int rc;

    struct libusb_device_descriptor desc;
    (void)libusb_get_device_descriptor(dev, &desc);

    if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
        printf("Got a device\n");

        rc = libusb_open(dev, &handle);
        if (LIBUSB_SUCCESS != rc) {
            printf("Could not open USB device\n");
        }
    }
    else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
        printf("Lost a device\n");

        if (handle) {
            libusb_close(handle);
            handle = NULL;
        }
    }
    
    return 0;
}
```

When my application has finished launching (`-applicationDidFinishLaunching:`) 
I run this snippet to setup a device hotplug listener:

```c
int rc;
rc = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED 
| LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_ENUMERATE, kVendorId, 
kProductId, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, NULL);

if (LIBUSB_SUCCESS != rc) {
    printf("Error creating a hotplug callback\n");
}
```

The results is not what I expect. When the application starts with a device 
connected the log outputs `Got a device`. _But the callback is never called 
when the device is unmounted or remounted, or when a device is mounted for the 
first time after the application has finished loading._

**What I expect**

1. The callback is called on startup if a device is attached
2. The callback is called when a device is detached
3. The callback is called when a device is attached while the application is 
running

Is there something wrong I'm doing in my code? I've read the documentation but 
as far as I can see I'm using the function correctly. And 
`libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)` returns `1`.

---
Reply to this email directly or view it on GitHub:
https://github.com/libusbx/libusbx/issues/138
------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to