Skip to content
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

Added an API call to check the connection status of a device handle #1456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Quantx
Copy link

@Quantx Quantx commented Feb 2, 2024

Aside from hot-plug events there is no good way to quickly check if a device handle is still attached to the system. I believe the only way to currently check if a device handle is still attached in a non-blocking manner is by attempting to claim an already claimed interface. This new API call adds a convenient method by which to check this.

int libusb_check_connected(libusb_device_handle * dev_handle)

@seanm
Copy link
Contributor

seanm commented Feb 2, 2024

This kind of API is prone to TOCTOU problems, so I'm not sure it's advisable to add. With multithreading and hotplug, the answer it gives can be incorrect an instant later.

libusb/core.c Outdated Show resolved Hide resolved
@mcuee mcuee added the core Related to common codes label Feb 3, 2024
@Quantx
Copy link
Author

Quantx commented Feb 3, 2024

The TOCTOU issue is addressed by the fact that all other API calls will also report LIBUSB_ERROR_NO_DEVICE if the device was disconnected between a request to libusb_check_connected() and the subsequent API call. The reason I want to see this API call added is because there is currently no way to synchronously check whether a device handle is still connected without also causing some other side effect. While you can create a mechanism with hot-plug events to achieve a similar result, I think that is a lot of work to acquire a piece of information that could easily be returned to the user by this API call. Furthermore, on platforms where hot-plug events are not supported, there is currently no dedicated API call to detect whether or not a device is still connected.

@seanm
Copy link
Contributor

seanm commented Feb 3, 2024

Yeah, sounds reasonable enough.

Could you squash your commits?

@Quantx
Copy link
Author

Quantx commented Feb 4, 2024

I've squashed it

libusb/core.c Outdated Show resolved Hide resolved
@mcuee
Copy link
Member

mcuee commented Feb 15, 2024

It may be good to add this to one of the existing examples as well to make it easy to test the new function.

@mcuee mcuee added the API API changes label Feb 15, 2024
@Quantx
Copy link
Author

Quantx commented Feb 20, 2024

I've added a call to libusb_check_connected() inside one of the basic open/close tests. Let me know if that is sufficient or if you think there's a better test case I should use instead.

@Quantx Quantx requested a review from seanm February 20, 2024 02:20
@Quantx
Copy link
Author

Quantx commented Mar 17, 2024

If there are no further comments or concerns regarding this PR can it please be merged?

@mcuee
Copy link
Member

mcuee commented Mar 19, 2024

If there are no further comments or concerns regarding this PR can it please be merged?

We will need to have the review and approval of the current active developers, basically @tormodvolden and/or @hjelmn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API changes core Related to common codes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants