Commit Graph
15 Commits
Author SHA1 Message Date
Sonatique a790a6df79 example/hotplugtest: Ignore unavailable devices (#1425)
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.
2025-08-20 18:15:15 +02:00
Tormod Volden f52a9c0c4d examples/hotplugtest: Close old device only if new one was opened
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]>
2025-06-09 15:43:48 +02:00
Tormod Volden 6c41069e77 examples/hotplugtest: Allow multiple devices attaching
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]>
2025-06-09 15:43:16 +02:00
Tormod Volden 853a5eac64 examples/hotplugtest: Tone down message about failed open
References #1425

Signed-off-by: Tormod Volden <[email protected]>
2024-01-31 12:32:56 +01:00
Ingvar Stepanyan 0a2f511914 examples/hotplugtest: Match any device by default
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]>
2023-11-27 23:06:32 +01:00
Nathan Hjelm 3c33e499a0 Update tests and example to use the new libusb_init_context() function
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]>
2023-01-20 11:04:14 +01:00
Chris Dickens 0abebc57a5 Fix typos detected by codespell and manual inspection
Signed-off-by: Chris Dickens <[email protected]>
2020-08-18 15:49:22 -07:00
Sean McBride 830a9cb27d Fix unused parameter warnings
Signed-off-by: Chris Dickens <[email protected]>
2017-12-28 22:40:30 -08:00
Ludovic Rousseau 964cff46cc examples: fix 3 compiler warnings
examples/hotplugtest.c:76:28: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
        vendor_id  = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a;
                   ~              ^~~~~~~~~~~~~~~~~~~~~~~~~
examples/hotplugtest.c:77:28: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
        product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005;
                   ~              ^~~~~~~~~~~~~~~~~~~~~~~~~
examples/hotplugtest.c:78:28: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
        class_id   = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
                   ~              ^~~~~~~~~~~~~~~~~~~~~~~~~
2016-03-06 13:42:13 +01:00
Ludovic Rousseau e10a652b4b Examples: fix typo in Nathan Hjelm email 2016-01-06 10:20:24 +01:00
Chris Dickens 30376161e5 Examples: Fix device leak in hotplugtest
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]>
2015-01-12 00:12:02 -08:00
Ludovic Rousseau d4dabfd14e examples: check the value returned by libusb_handle_events()
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).
2013-09-28 12:16:13 +02:00
Ludovic Rousseau aa00030b78 examples: check the value returned by libusb_init()
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).
2013-09-28 12:14:13 +02:00
Nathan Hjelm 57256dd159 Add some editor meta-comments for proper tab usage
Signed-off-by: Hans de Goede <[email protected]>
2013-05-15 20:43:35 +02:00
Nathan Hjelm 7801ff94fa Add hotplug support.
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]>
2013-05-15 17:28:06 +02:00