@@ -135,11 +135,6 @@ bool Usb::isWaitForPendantBeforeHalEnabled() const
135135 return isWaitWithTimeout;
136136}
137137// ----------------------------------------------------------------------
138- bool Usb::getDoReconnect () const
139- {
140- return mDoReconnect ;
141- }
142- // ----------------------------------------------------------------------
143138void Usb::setUsbProductId (uint16_t usbProductId)
144139{
145140 this ->usbProductId = usbProductId;
@@ -436,6 +431,7 @@ bool Usb::setupAsyncTransfer()
436431 int r = libusb_submit_transfer (inTransfer);
437432 if (r != 0 ){
438433 std::cerr << " libusb_submit_transfer failed with " << r << endl;
434+ mDoReconnect = true ;
439435 return false ;
440436 }
441437 else
@@ -518,7 +514,7 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
518514 {
519515 if (!setupAsyncTransfer ())
520516 {
521- requestTermination () ;
517+ transferFailed = true ;
522518 }
523519 }
524520
@@ -530,7 +526,7 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
530526 {
531527 if (!setupAsyncTransfer ())
532528 {
533- requestTermination () ;
529+ transferFailed = true ;
534530 }
535531 }
536532 break ;
@@ -543,12 +539,12 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
543539 case (LIBUSB_TRANSFER_OVERFLOW):
544540 case (LIBUSB_TRANSFER_ERROR):
545541 std::cerr << " transfer error: " << transfer->status << endl;
546- requestTermination () ;
542+ transferFailed = true ;
547543 break ;
548544
549545 default :
550546 std::cerr << " unknown transfer status: " << transfer->status << endl;
551- requestTermination () ;
547+ transferFailed = true ;
552548 break ;
553549 }
554550 // libusb_free_transfer(transfer);
@@ -604,7 +600,7 @@ Usb::InitStatus Usb::init()
604600 {
605601 std::cerr << endl << " Bug: Not cleaned up deviceHandle properly before calling init again!" << endl;
606602 }
607- if (getDoReconnect () )
603+ if (mDoReconnect )
608604 {
609605 int pauseSecs = 3 ;
610606 *verboseInitOut << " init pausing " << pauseSecs << " s, waiting for device to be gone ..." ;
@@ -681,11 +677,11 @@ Usb::InitStatus Usb::init()
681677 sleep (1 );
682678 }
683679 } while (deviceHandle == nullptr && mIsRunning );
684- *verboseInitOut << " ok" << endl
685- << " init " << mName << " device found" << endl;
686680
687681 if (deviceHandle != nullptr )
688682 {
683+ *verboseInitOut << " ok" << endl
684+ << " init " << mName << " device found" << endl;
689685 *verboseInitOut << " init detaching active kernel driver ..." ;
690686 r = libusb_kernel_driver_active (deviceHandle, 0 );
691687 if (r == 1 )
@@ -727,7 +723,7 @@ Usb::InitStatus Usb::init()
727723 return InitStatus::OK;
728724}
729725// ----------------------------------------------------------------------
730- void Usb::handleTimeouts ()
726+ void Usb::process ()
731727{
732728 struct timeval timeout;
733729 timeout.tv_sec = 0 ;
@@ -736,7 +732,14 @@ void Usb::handleTimeouts()
736732 int r = libusb_handle_events_timeout_completed (context, &timeout, nullptr );
737733 if (r != 0 )
738734 {
739- std::cerr << endl << " Error: handleTimeouts() libusb_handle_events_timeout_completed returned " << r << endl;
735+ std::cerr << endl << " Error: process() libusb_handle_events_timeout_completed returned " << r << endl;
736+ }
737+
738+ if (transferFailed)
739+ {
740+ close ();
741+ transferFailed = false ;
742+ mDoReconnect = true ;
740743 }
741744}
742745// ----------------------------------------------------------------------
0 commit comments