Skip to content

Commit

Permalink
examples/hotplugtest: Close old device only if new one was opened
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Tormod Volden <[email protected]>
  • Loading branch information
tormodvolden committed Aug 13, 2024
1 parent 32e063d commit 8313b1f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions examples/hotplugtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ libusb_device_handle *handle = NULL;
static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
libusb_device_handle *new_handle;
int rc;

(void)ctx;
Expand All @@ -46,13 +47,13 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev,
libusb_strerror((enum libusb_error)rc));
}

if (handle) {
libusb_close (handle);
handle = NULL;
}

rc = libusb_open (dev, &handle);
if (LIBUSB_SUCCESS != rc) {
rc = libusb_open (dev, &new_handle);
if (LIBUSB_SUCCESS == rc) {
if (handle) {
libusb_close (handle);
}
handle = new_handle;
} else {
fprintf (stderr, "No access to device: %s\n",
libusb_strerror((enum libusb_error)rc));
}
Expand Down

0 comments on commit 8313b1f

Please sign in to comment.