-
Notifications
You must be signed in to change notification settings - Fork 97
Implement asynchronous hotplug #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
29b33ed to
ffa956c
Compare
|
Somehow the hotplug feature does not seem to work under macOS, tested with macOS Ventura 13.3.1 using Mac Mini M1 (2020). |
|
Same thing that it does not seem to anything under Linux. Tested under Ubuntu 20.04. The original hotplug example works fine. |
I haven't tested this on macOS on the Apple M1, but I can give that a try if it still doesn't work.
Something might have changed in libusb since I last looked at this, as it seems to end up calling the callback immediately during registration, whereas I remember it doing this after calling the register function. Basically, |
|
Great, now it works under Ubuntu Linux. It should work under macOS as well but I will test tomorrow. |
|
Just something obvous, the behavior under Windows is also correct, as libusb does not support hotplug yet and it will not support in the near future due to lack of Windows developers. One potential workaround is to implement rusb's own hotplug mechanism under Windows but that may need some efforts. |
|
Now it works under macOS as well. |
|
@StephanvanSchaik |
This is based off the code for PR #155, so the first two commits are the same. The rest of this PR implements an asynchronous hotplug API.
It introduces
HotplugBuilderthat wraps theHotplugBuilderfrom therusbcrate. Instead of accepting anything implementing theHotplugtrait,HotplugBuilder::register()registers its ownHotplugobject implementing that trait.HotplugBuilder::register()also sets up a channel to propagate any hotplug events when devices arrive or leave. Whenever any of the callbacks get invoked, the appropriateHotplugEventis created and sent to the channel. TheRegistrationobject returned byHotplugBuilder::register()has a functionnext_event()to await hotplug events by simply polling the channel for hotplug events.The API shouldn't have the same restrictions as the hotplug API in the
rusbcrate, as the callbacks simply send theDeviceobject to the channel. That way no other operations are performed on theseDeviceobjects, and after retrieving theseDeviceobjects by polling the channel these restrictions should no longer apply..This also adds an example (
examples/async_hotplug.rs) that shows how to use the API.