Depending on the devices or drivers, open() may return
LIBUSB_ERROR_ACCESS
LIBUSB_ERROR_NOT_FOUND
LIBUSB_ERROR_NOT_SUPPORTED
for devices "out of reach" to libusb.
We want to keep only one device open for simplicity, but instead of
closing the last opened device already before /trying/ to open a new
one, close it only if the new one was opened successfully. This way
we may keep the device open longer, and in the simple case where no
later arriving devices could be opened, until it is detached again.
Closes#1550
Signed-off-by: Tormod Volden <[email protected]>
If no device ID is specified, and for instance a hub is plugged, many
devices will be attached (and later detached) at the same time. The old
code counting only 2 events would then exit prematurely.
By counting attach and detach events separately, we preserve the old
behaviour in the single-device case, but also allow more complex
sequences to be handled in a way that mostly appears intuitive.
This is not fool-proof, and we can still end up leaving after a surprise
detachment of a "pre-existing" device while we have one of the "new"
devices open. In this case print a warning.
References #1455
Signed-off-by: Tormod Volden <[email protected]>
I spent some time trying to figure out why, when I run this example,
it's not showing any events, until I looked into the code and saw it has
some hardcoded VID:PID.
Most people probably don't have the specific device hardcoded here, and
it seems better to default to showing events for any device unless
overridden.
While at it, also updated device detached message to match the attached
one to show VID:PID.
Closes#1350
[Tormod: Print error strings on failures]
Signed-off-by: Tormod Volden <[email protected]>
This commit updates all test and example code to use the newer
libusb_init_context() function instead of libusb_init().
Signed-off-by: Nathan Hjelm <[email protected]>
[Tormod: Update umockdev.c as well]
Signed-off-by: Tormod Volden <[email protected]>
Device leak occurred if either of the following occured:
1) Detach followed by attach
2) Two consecutive attach
Signed-off-by: Chris Dickens <[email protected]>
Problem detected by the Coverity tool
CID 1042539 (#1 of 1): Unchecked return value (CHECKED_RETURN)8.
check_return: Calling function "libusb_handle_events(libusb_context *)"
without checking return value (as is done elsewhere 6 out of 7 times).
Problem detected by the Coverity tool
CID 1042538 (#1 of 1): Unchecked return value (CHECKED_RETURN)4.
check_return: Calling function "libusb_init(libusb_context **)" without
checking return value (as is done elsewhere 11 out of 12 times).
The internal API is changing as follows:
- Adding two new functions. usbi_connect_device, and usbi_disconnect_device.
Backends must call these functions to add them to the context's device list
at one of two places: initial enumeration (done at init), and on device
attach and removal. These functions need to be called once per context.
- Backends that support hotplug should not provide a get_device_list funtion.
This function is now deprecated and will likely be removed once all backends
support hotplug.
The external API is changing as follows:
- Two new functions have been added to register and deregister callbacks for
hotplug notification: libusb_hotplug_register_callback(),
libusb_hotplug_deregister_callback(). Hotplug callbacks are called by
libusb_handle_events(). Details of the new API can be found in libusb.h.
- A new capability check has been added to check for hotplug support. See
LIBUSB_CAP_HAS_HOTPLUG.
Aa suggested by Xiaofan add new example has been added to show how to use
the new external hotplug API. See examples/hotplugtest.c.
Signed-off-by: Hans de Goede <[email protected]>