Skip to content

Commit 48f3009

Browse files
committed
whb04b: Cleanup
1 parent e450a23 commit 48f3009

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ Usb::Usb(const char* name, OnUsbInputPackageListener& onDataReceivedCallback, Ha
164164
// ----------------------------------------------------------------------
165165
void 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()
414420
bool 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
// ----------------------------------------------------------------------
431447
void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
@@ -580,6 +596,14 @@ void Usb::enableVerboseInit(bool enable)
580596
// ----------------------------------------------------------------------
581597
Usb::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+
// ----------------------------------------------------------------------
715747
void 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
// ----------------------------------------------------------------------
728759
void Usb::teardown()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ class Usb : public UsbRawInputListener
308308
UsbOutPackageData& getOutputPackageData();
309309

310310
private:
311+
void closeHandle();
311312
const uint16_t usbVendorId{0x10ce};
312313
uint16_t usbProductId{0xeb93};
313314
libusb_context * context{nullptr};

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,13 @@ int XhcWhb04b6Component::run()
460460
process();
461461
mUsb.teardown();
462462
}
463+
else if(status == Usb::InitStatus::RETRY)
464+
{
465+
//Just retry
466+
}
463467
else
464468
{
465-
mUsb.teardown();
469+
std::cerr << endl << "Bug: Unhandled return value!" << endl;
466470
}
467471
}
468472
teardownHal();

0 commit comments

Comments
 (0)