-
Notifications
You must be signed in to change notification settings - Fork 407
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
Add hotplug support #238
Comments
Technically there are some Windows codes are out there to support hotplug. For Linux you can consider libudev. For macOS there are similar codes as well. libusb has hotplug support under Linux and macOS but not Windows (complicated due to libusb Windows implementation). |
For Windows, not so sure pywinusb can shed some light. Or the codes here. I think Hidsharp also supports hotplug. |
I'd say that hotplug support for each platform is pretty much straight-forward (I've done it for macOS and Windows several years ago in a propriatary project) it just has to be implemented |
#299 seems to work pretty well under Windows. The API function names are now similar to libusb and I think they are fine. |
Hi,sir. I found a little error about nullptr in https://github.com/DJm00n/hidapi/blob/device_notify/windows/hid.c#L1036 |
I noticed, the plugged devices before my testing program startup also can be callback. |
Once hotplug is registered - it has to report all existing devices. The likely alternative you probably want to have is:
Or the other way around (first register callback, then enumerate existing devices). In either case you're having a race condition if the device is plugged at the moment you're registering a callback (and that is happening more often than you could imagine). The only 100% reliable way to use device hotplug events - is to use only hotplug callback events to get a list of all devices (existing and new). |
yes. |
Just to understand this issue: Are you saying that hidapi is not USB hotplug ready? Then how to deal with disconnecting devices (on MacOS this might cause app and even system crashes)? |
As of right now - only by using OS-specific API in your application directly.
HIDAPI handles those internally (e.g.: 1) - no crashes are expected. |
I am using a library (ZWO EFW SDK) that in turn uses hidapi. I get crashes constantly when plugging and unplugging the device wildly:
More context at https://bbs.astronomy-imaging-camera.com/d/15289-efwgetnum-on-macos-not-thread-safe/5 (That thread is full of assumptions, however, sorry...) |
|
Oh, additionally: not only |
Hmm. That simple C ten-liner that I have linked above results in multiple threads:
so I assume they are using threads there. I don't think they are accessing hidapi from multiple threads. If this doesn't have anything to do with hotplugging, maybe I should open another issue. At least it happens while hot plugging... |
Yes please. |
No need for another issue: I have fixed the problem by replacing the ar packaged version of hidapi within that SDK with a fresh recompile from GitHub. Companies keep their software closed to hide ugly truths... Thank you for your help! |
I noticed that the current branch only supports hotplug on windows, so I added a prototype implementation for libusb in #645. It's a pretty straightforward one, taking huge chunks from Windows implementation. |
Added a hidraw implementation (#647 ) that copies a lot from the libusb one (I specifically tried to move the major differences into separate functions, so that the general callback list management is the same exact code). Did not run any tests yet, that will have to wait until at least the weekends. I don't have a Mac to build or test the last one... It's not going to stop me, but it's gonna take a bit longer than it took for the others. UPD: Ran some tests on hidraw version, fixed some errors, it works fine now. I still have to ask someone to double-check everything, up to and including code-style. |
I have made a mock-up for a MacOS implementation of the hotplug feature in #653 . Even if it can not be used as-is, it will simplify the work to get this completed. I beieve this completes the list of platforms that have to have the hotplug support for the hotplug feature to be included into the next release of hidapi. However, this one I am the least sure of, as it took me a whole two weeks and I stil couldn't set up a proper buid environment for it, so I can not even check if it builds (github CI might become my only way to test it), let alone check if it works. |
@k1-801 please excuse me for not looking into your PRs. Really appreciate all the work you've done. Got extremely busy lately. |
@Youw sorry to disturb you, but is there any chance of getting a review? |
Thant's for the reminder. I actually do have a few hours today. Will take a look. |
I think it's best to rebase the |
Good point. @Youw |
@Youw is there anything else that needs to be done or anything else needed from my side to get the feature reviewed, merged and then considered to be included in the next release? I understand that there's nothing worse than releasing an untested feature, but so far we've gotten very good results on 3 out of the 4 backends, libusb being the only tricky one. |
The libusb PR now works as well. |
As for the practical applications of this feature, at this moment the project: https://gitlab.com/CalcProgrammer1/OpenRGB/-/merge_requests/1963 is waiting for this feature to be released. |
I just found out that there is actually now support for a netbsd backend in hidapi, and the Unfortunately, I couldn't find any information on how to get device notifications on netbsd. |
A small concern here: calling any Register or Unregister functions from within a callback may (and probably will) cause a deadlock. On Windows, Critical Sections seem to keep an internal counter that allows the same thread to enter on multiple times, while in pthread, unless PTHREAD_MUTEX_RECURSIVE is specified, it will cause a deadlock. Additionally, just unlocking the mutex before calling the callback (or making it recursive) will allow the user to remove elements in such a way that it disturbs the execution flow, which can already happen on Windows. Bad scenarios:
There is a way to circumvent this, by adding a flag to each callback that indicates that it should be removed later when it is considered safe, which is set by the Unregister function if the mutex is already locked at the moment of the call. It seems that, as long as we know for a fact that the mutex is locked by the same thread, it should be safe to read and navigate the list of callbacks and even set flags to it, and the list will not be changed (the mutex is still locked and will not be unloced until we leave the callback and the callback processing part). As for the Register function, I see no problem with additional callbacks being registered from a callback. However, this approach still requires to somehow know for a fact that the mutex is locked by the same thread we are currently in. Another possible resolution would be to warn against the use of Register and Unregister functions inside a callback, however, there are scenarios where the user code could make use of that (a widely used example would be the scenario where the unplug callback is only registered after a device was connected). |
One of the main missing feature of hidapi is the hotplug support.
The text was updated successfully, but these errors were encountered: