@@ -462,7 +462,9 @@ bool Usb::setupAsyncTransfer()
462462 // ! timeout[ms]
463463 750 );
464464 int r = libusb_submit_transfer (inTransfer);
465- assert (0 == r);
465+ if (r != 0 ){
466+ std::cerr << " libusb_submit_transfer failed with " << r << endl;
467+ }
466468 return (0 == r);
467469}
468470// ----------------------------------------------------------------------
@@ -538,7 +540,10 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
538540
539541 if (mIsRunning )
540542 {
541- setupAsyncTransfer ();
543+ if (!setupAsyncTransfer ())
544+ {
545+ requestTermination ();
546+ }
542547 }
543548
544549 break ;
@@ -547,7 +552,10 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
547552 case (LIBUSB_TRANSFER_TIMED_OUT):
548553 if (mIsRunning )
549554 {
550- setupAsyncTransfer ();
555+ if (!setupAsyncTransfer ())
556+ {
557+ requestTermination ();
558+ }
551559 }
552560 break ;
553561
@@ -610,7 +618,7 @@ void Usb::enableVerboseInit(bool enable)
610618 }
611619}
612620// ----------------------------------------------------------------------
613- bool Usb::init ()
621+ Usb::InitStatus Usb::init ()
614622{
615623 if (getDoReconnect ())
616624 {
@@ -626,11 +634,11 @@ bool Usb::init()
626634 }
627635
628636 *verboseInitOut << " init usb context ..." ;
629- int r = libusb_init (&context);
637+ int r = libusb_init (&context); // This function is deprecated. However, Debian 12 doesn't have it, so we leave it in for now.
630638 if (r != 0 )
631639 {
632640 std::cerr << endl << " failed to initialize usb context" << endl;
633- return false ;
641+ return InitStatus::RETRY ;
634642 }
635643 *verboseInitOut << " ok" << endl;
636644
@@ -664,53 +672,62 @@ bool Usb::init()
664672 if (devicesCount < 0 )
665673 {
666674 std::cerr << endl << " failed to get device list" << endl;
667- return false ;
675+ return InitStatus::RETRY ;
668676 }
669677
670678 deviceHandle = libusb_open_device_with_vid_pid (context, usbVendorId, usbProductId);
671679 libusb_free_device_list (devicesReference, 1 );
672680 *verboseInitOut << " ." << std::flush;
673- if (! isDeviceOpen () )
681+ if (deviceHandle == nullptr )
674682 {
675683 *verboseInitOut << " ." << std::flush;
676684 if (isWaitWithTimeout)
677685 {
678686 *verboseInitOut << " ." << std::flush;
679687 if ((mWaitSecs --) <= 0 )
680688 {
681- std::cerr << endl << " timeout exceeded, exiting" << endl;
682- return false ;
689+ std::cerr << endl << " Configured timeout exceeded, exiting" << endl;
690+ return InitStatus::EXIT ;
683691 }
684692 }
685693 sleep (1 );
686694 }
687- } while (! isDeviceOpen () && mIsRunning );
695+ } while (deviceHandle == nullptr && mIsRunning );
688696 *verboseInitOut << " ok" << endl
689697 << " init " << mName << " device found" << endl;
690698
691- if (isDeviceOpen () )
699+ if (deviceHandle != nullptr )
692700 {
693701 *verboseInitOut << " init detaching active kernel driver ..." ;
694- if (libusb_kernel_driver_active (deviceHandle, 0 ) == 1 )
702+ r = libusb_kernel_driver_active (deviceHandle, 0 );
703+ if (r == 1 )
695704 {
696- int r = libusb_detach_kernel_driver (deviceHandle, 0 );
697- assert (0 == r);
705+ r = libusb_detach_kernel_driver (deviceHandle, 0 );
706+ if (r < 0 ){
707+ std::cerr << " libusb_detach_kernel_driver failed with " << r << endl;
708+ return InitStatus::RETRY;
709+ }
698710 *verboseInitOut << " ok" << endl;
699711 }
700- else
712+ else if (r == 0 )
701713 {
702714 *verboseInitOut << " already detached" << endl;
703715 }
716+ else
717+ {
718+ std::cerr << " libusb_kernel_driver_active failed with " << r << endl;
719+ return InitStatus::RETRY;
720+ }
704721 *verboseInitOut << " init claiming interface ..." ;
705- int r = libusb_claim_interface (deviceHandle, 0 );
722+ r = libusb_claim_interface (deviceHandle, 0 );
706723 if (r != 0 )
707724 {
708725 std::cerr << endl << " failed to claim interface" << endl;
709- return false ;
726+ return InitStatus::RETRY ;
710727 }
711728 *verboseInitOut << " ok" << endl;
712729 }
713- return true ;
730+ return InitStatus::OK ;
714731}
715732// ----------------------------------------------------------------------
716733void Usb::setWaitWithTimeout (uint8_t waitSecs)
0 commit comments