@@ -164,6 +164,12 @@ Usb::Usb(const char* name, OnUsbInputPackageListener& onDataReceivedCallback, Ha
164164// ----------------------------------------------------------------------
165165void Usb::sendDisplayData ()
166166{
167+ if (deviceHandle == nullptr )
168+ {
169+ std::cerr << endl << " Bug: sendDisplayData: deviceHandle is null!" << endl;
170+ return ;
171+ }
172+
167173 outputPackageBuffer.asBlocks .init (&outputPackageData);
168174
169175 if (mIsSimulationMode )
@@ -414,6 +420,12 @@ void Usb::requestTermination()
414420bool Usb::setupAsyncTransfer ()
415421{
416422 assert (inTransfer != nullptr );
423+ if (deviceHandle == nullptr )
424+ {
425+ std::cerr << endl << " Bug: setupAsyncTransfer: deviceHandle is null!" << endl;
426+ return false ;
427+ }
428+
417429 libusb_fill_bulk_transfer (inTransfer, deviceHandle,
418430 (0x1 | LIBUSB_ENDPOINT_IN), inputPackageBuffer.asBuffer ,
419431 sizeof (inputPackageBuffer.asBuffer ), mRawDataCallback ,
@@ -424,8 +436,12 @@ bool Usb::setupAsyncTransfer()
424436 int r = libusb_submit_transfer (inTransfer);
425437 if (r != 0 ){
426438 std::cerr << " libusb_submit_transfer failed with " << r << endl;
439+ return false ;
440+ }
441+ else
442+ {
443+ return true ;
427444 }
428- return (0 == r);
429445}
430446// ----------------------------------------------------------------------
431447void Usb::onUsbDataReceived (struct libusb_transfer * transfer)
@@ -580,6 +596,14 @@ void Usb::enableVerboseInit(bool enable)
580596// ----------------------------------------------------------------------
581597Usb::InitStatus Usb::init ()
582598{
599+ if (context != nullptr )
600+ {
601+ std::cerr << endl << " Bug: Not cleaned up context properly before calling init again!" << endl;
602+ }
603+ if (deviceHandle != nullptr )
604+ {
605+ std::cerr << endl << " Bug: Not cleaned up deviceHandle properly before calling init again!" << endl;
606+ }
583607 if (getDoReconnect ())
584608 {
585609 int pauseSecs = 3 ;
@@ -633,6 +657,7 @@ Usb::InitStatus Usb::init()
633657 if (devicesCount < 0 )
634658 {
635659 std::cerr << endl << " failed to get device list" << endl;
660+ teardown ();
636661 mDoReconnect =true ;
637662 return InitStatus::RETRY;
638663 }
@@ -649,6 +674,7 @@ Usb::InitStatus Usb::init()
649674 if ((mWaitSecs --) <= 0 )
650675 {
651676 std::cerr << endl << " Configured timeout exceeded, exiting" << endl;
677+ teardown ();
652678 return InitStatus::EXIT;
653679 }
654680 }
@@ -667,8 +693,8 @@ Usb::InitStatus Usb::init()
667693 r = libusb_detach_kernel_driver (deviceHandle, 0 );
668694 if (r < 0 ){
669695 std::cerr << " libusb_detach_kernel_driver failed with " << r << endl;
670- libusb_close (deviceHandle );
671- deviceHandle = nullptr ;
696+ closeHandle ( );
697+ teardown () ;
672698 mDoReconnect =true ;
673699 return InitStatus::RETRY;
674700 }
@@ -681,8 +707,8 @@ Usb::InitStatus Usb::init()
681707 else
682708 {
683709 std::cerr << " libusb_kernel_driver_active failed with " << r << endl;
684- libusb_close (deviceHandle );
685- deviceHandle = nullptr ;
710+ closeHandle ( );
711+ teardown () ;
686712 mDoReconnect =true ;
687713 return InitStatus::RETRY;
688714 }
@@ -691,8 +717,8 @@ Usb::InitStatus Usb::init()
691717 if (r != 0 )
692718 {
693719 std::cerr << endl << " failed to claim interface" << endl;
694- libusb_close (deviceHandle );
695- deviceHandle = nullptr ;
720+ closeHandle ( );
721+ teardown () ;
696722 mDoReconnect =true ;
697723 return InitStatus::RETRY;
698724 }
@@ -712,6 +738,12 @@ void Usb::handleTimeouts()
712738 (r == LIBUSB_ERROR_TIMEOUT) || (r == LIBUSB_ERROR_INTERRUPTED));
713739}
714740// ----------------------------------------------------------------------
741+ void Usb::closeHandle ()
742+ {
743+ libusb_close (deviceHandle);
744+ deviceHandle = nullptr ;
745+ }
746+ // ----------------------------------------------------------------------
715747void Usb::close ()
716748{
717749 struct timeval tv;
@@ -721,8 +753,7 @@ void Usb::close()
721753 assert (0 == r);
722754 r = libusb_release_interface (deviceHandle, 0 );
723755 assert ((0 == r) || (r == LIBUSB_ERROR_NO_DEVICE));
724- libusb_close (deviceHandle);
725- deviceHandle = nullptr ;
756+ closeHandle ();
726757}
727758// ----------------------------------------------------------------------
728759void Usb::teardown ()
0 commit comments