Skip to content

Commit 6024cd7

Browse files
committed
whb04b: Try to improve error handling
1 parent 5ca2199 commit 6024cd7

3 files changed

Lines changed: 62 additions & 37 deletions

File tree

src/hal/user_comps/xhc-whb04b-6/usb.cc

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
// ----------------------------------------------------------------------
716733
void Usb::setWaitWithTimeout(uint8_t waitSecs)

src/hal/user_comps/xhc-whb04b-6/usb.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ class SleepDetect
274274
class Usb : public UsbRawInputListener
275275
{
276276
public:
277+
enum class InitStatus{
278+
EXIT, //Program should exit
279+
RETRY, //Program should retry
280+
OK //All fine
281+
};
277282
static const ConstantUsbPackages ConstantPackages;
278283
//! \param name device string used for printing messages
279284
//! \param onDataReceivedCallback called when received data is ready
@@ -302,7 +307,7 @@ class Usb : public UsbRawInputListener
302307
void enableVerboseTx(bool enable);
303308
void enableVerboseRx(bool enable);
304309
void enableVerboseInit(bool enable);
305-
bool init();
310+
InitStatus init();
306311
void setWaitWithTimeout(uint8_t waitSecs);
307312

308313
UsbOutPackageData& getOutputPackageData();

src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -433,31 +433,34 @@ int XhcWhb04b6Component::run()
433433
mHal.setIsPendantConnected(false);
434434

435435
initWhb();
436-
if (!mUsb.init())
436+
Usb::InitStatus status = mUsb.init();
437+
if (status == Usb::InitStatus::EXIT)
437438
{
438439
return EXIT_FAILURE;
439440
}
440-
441-
mHal.setIsPendantConnected(true);
442-
443-
if (!isHalReady && !mHal.isSimulationModeEnabled())
441+
else if(status == Usb::InitStatus::OK)
444442
{
445-
hal_ready(mHal.getHalComponentId());
446-
isHalReady = true;
447-
}
443+
mHal.setIsPendantConnected(true);
448444

449-
if (mUsb.isDeviceOpen())
450-
{
451-
*mInitCout << "init enabling reception ...";
452-
if (!enableReceiveAsyncTransfer())
445+
if (!isHalReady && !mHal.isSimulationModeEnabled())
446+
{
447+
hal_ready(mHal.getHalComponentId());
448+
isHalReady = true;
449+
}
450+
451+
if (mUsb.isDeviceOpen())
453452
{
454-
std::cerr << endl << "failed to enable reception" << endl;
455-
return EXIT_FAILURE;
453+
*mInitCout << "init enabling reception ...";
454+
if (!enableReceiveAsyncTransfer())
455+
{
456+
std::cerr << endl << "failed to enable reception" << endl;
457+
return EXIT_FAILURE;
458+
}
459+
*mInitCout << " ok" << endl;
456460
}
457-
*mInitCout << " ok" << endl;
461+
process();
462+
teardownUsb();
458463
}
459-
process();
460-
teardownUsb();
461464
}
462465
teardownHal();
463466

0 commit comments

Comments
 (0)