@@ -598,6 +598,7 @@ Usb::InitStatus Usb::init()
598598 if (r != 0 )
599599 {
600600 std::cerr << endl << " failed to initialize usb context" << endl;
601+ mDoReconnect =true ;
601602 return InitStatus::RETRY;
602603 }
603604 *verboseInitOut << " ok" << endl;
@@ -632,6 +633,7 @@ Usb::InitStatus Usb::init()
632633 if (devicesCount < 0 )
633634 {
634635 std::cerr << endl << " failed to get device list" << endl;
636+ mDoReconnect =true ;
635637 return InitStatus::RETRY;
636638 }
637639
@@ -665,6 +667,9 @@ Usb::InitStatus Usb::init()
665667 r = libusb_detach_kernel_driver (deviceHandle, 0 );
666668 if (r < 0 ){
667669 std::cerr << " libusb_detach_kernel_driver failed with " << r << endl;
670+ libusb_close (deviceHandle);
671+ deviceHandle = nullptr ;
672+ mDoReconnect =true ;
668673 return InitStatus::RETRY;
669674 }
670675 *verboseInitOut << " ok" << endl;
@@ -676,19 +681,26 @@ Usb::InitStatus Usb::init()
676681 else
677682 {
678683 std::cerr << " libusb_kernel_driver_active failed with " << r << endl;
684+ libusb_close (deviceHandle);
685+ deviceHandle = nullptr ;
686+ mDoReconnect =true ;
679687 return InitStatus::RETRY;
680688 }
681689 *verboseInitOut << " init claiming interface ..." ;
682690 r = libusb_claim_interface (deviceHandle, 0 );
683691 if (r != 0 )
684692 {
685693 std::cerr << endl << " failed to claim interface" << endl;
694+ libusb_close (deviceHandle);
695+ deviceHandle = nullptr ;
696+ mDoReconnect =true ;
686697 return InitStatus::RETRY;
687698 }
688699 *verboseInitOut << " ok" << endl;
689700 }
690701 return InitStatus::OK;
691702}
703+ // ----------------------------------------------------------------------
692704void Usb::handleTimeouts ()
693705{
694706 struct timeval timeout;
@@ -699,6 +711,7 @@ void Usb::handleTimeouts()
699711 assert ((r == LIBUSB_SUCCESS) || (r == LIBUSB_ERROR_NO_DEVICE) || (r == LIBUSB_ERROR_BUSY) ||
700712 (r == LIBUSB_ERROR_TIMEOUT) || (r == LIBUSB_ERROR_INTERRUPTED));
701713}
714+ // ----------------------------------------------------------------------
702715void Usb::close ()
703716{
704717 struct timeval tv;
@@ -711,6 +724,7 @@ void Usb::close()
711724 libusb_close (deviceHandle);
712725 deviceHandle = nullptr ;
713726}
727+ // ----------------------------------------------------------------------
714728void Usb::teardown ()
715729{
716730 libusb_exit (context);
0 commit comments