Skip to content

Commit 6a63380

Browse files
committed
usb: Retry a few times with libusb_open()
On some platforms (UsbDk) libusb_open() will return errors for a short while immediately after libusb_close(). Retry a few times. Also, fix libusb device reference handling across open and close. Fix #812.
1 parent 6431323 commit 6a63380

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/libfreenect2.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ class Freenect2Impl
459459
{
460460
unsigned char buffer[1024];
461461
r = libusb_get_string_descriptor_ascii(dev_handle, dev_desc.iSerialNumber, buffer, sizeof(buffer));
462+
// keep the ref until determined not kinect
463+
libusb_ref_device(dev);
462464
libusb_close(dev_handle);
463465

464466
if(r > LIBUSB_SUCCESS)
@@ -474,6 +476,7 @@ class Freenect2Impl
474476
}
475477
else
476478
{
479+
libusb_unref_device(dev);
477480
LOG_ERROR << "failed to get serial number of Kinect v2: " << PrintBusAndDevice(dev, r);
478481
}
479482
}
@@ -1006,7 +1009,17 @@ Freenect2Device *Freenect2Impl::openDevice(int idx, const PacketPipeline *pipeli
10061009
return device;
10071010
}
10081011

1009-
int r = libusb_open(dev.dev, &dev_handle);
1012+
int r;
1013+
for (int i = 0; i < 10; i++)
1014+
{
1015+
r = libusb_open(dev.dev, &dev_handle);
1016+
if(r == LIBUSB_SUCCESS)
1017+
{
1018+
break;
1019+
}
1020+
LOG_INFO << "device unavailable right now, retrying";
1021+
this_thread::sleep_for(chrono::milliseconds(100));
1022+
}
10101023

10111024
if(r != LIBUSB_SUCCESS)
10121025
{

0 commit comments

Comments
 (0)