Skip to content

Commit ee1c3ca

Browse files
committed
whb04b: Remove teardown and checks
1 parent 23c28e1 commit ee1c3ca

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ Usb::InitStatus Usb::init()
662662
if (devicesCount < 0)
663663
{
664664
std::cerr << endl << "failed to get device list" << endl;
665-
teardown();
665+
closeLibusb();
666666
mDoReconnect=true;
667667
return InitStatus::RETRY;
668668
}
@@ -679,7 +679,7 @@ Usb::InitStatus Usb::init()
679679
if ((mWaitSecs--) <= 0)
680680
{
681681
std::cerr << endl << "Configured timeout exceeded, exiting" << endl;
682-
teardown();
682+
closeLibusb();
683683
return InitStatus::EXIT;
684684
}
685685
}
@@ -700,7 +700,7 @@ Usb::InitStatus Usb::init()
700700
{
701701
std::cerr << "libusb_detach_kernel_driver failed with " << r << endl;
702702
closeHandle();
703-
teardown();
703+
closeLibusb();
704704
mDoReconnect=true;
705705
return InitStatus::RETRY;
706706
}
@@ -714,7 +714,7 @@ Usb::InitStatus Usb::init()
714714
{
715715
std::cerr << "libusb_kernel_driver_active failed with " << r << endl;
716716
closeHandle();
717-
teardown();
717+
closeLibusb();
718718
mDoReconnect=true;
719719
return InitStatus::RETRY;
720720
}
@@ -724,7 +724,7 @@ Usb::InitStatus Usb::init()
724724
{
725725
std::cerr << endl << "failed to claim interface" << endl;
726726
closeHandle();
727-
teardown();
727+
closeLibusb();
728728
mDoReconnect=true;
729729
return InitStatus::RETRY;
730730
}
@@ -735,6 +735,12 @@ Usb::InitStatus Usb::init()
735735
// ----------------------------------------------------------------------
736736
void Usb::process()
737737
{
738+
if(context == nullptr)
739+
{
740+
std::cerr << endl << "Bug: process context is closed!" << endl;
741+
return;
742+
}
743+
738744
struct timeval timeout;
739745
timeout.tv_sec = 0;
740746
timeout.tv_usec = 200 * 1000;
@@ -755,12 +761,38 @@ void Usb::process()
755761
// ----------------------------------------------------------------------
756762
void Usb::closeHandle()
757763
{
764+
if(deviceHandle == nullptr)
765+
{
766+
std::cerr << endl << "Bug: closeHandle allready closed!" << endl;
767+
return;
768+
}
758769
libusb_close(deviceHandle);
759770
deviceHandle = nullptr;
760771
}
761772
// ----------------------------------------------------------------------
773+
void Usb::closeLibusb()
774+
{
775+
if(context == nullptr)
776+
{
777+
std::cerr << endl << "Bug: closeLibusb allready closed!" << endl;
778+
return;
779+
}
780+
libusb_exit(context);
781+
context = nullptr;
782+
}
783+
// ----------------------------------------------------------------------
762784
void Usb::close()
763785
{
786+
if(context == nullptr)
787+
{
788+
std::cerr << endl << "Bug: close context allready closed!" << endl;
789+
return;
790+
}
791+
if(deviceHandle == nullptr)
792+
{
793+
std::cerr << endl << "Bug: close deviceHandle allready closed!" << endl;
794+
return;
795+
}
764796
struct timeval tv;
765797
tv.tv_sec = 1;
766798
tv.tv_usec = 0;
@@ -775,12 +807,7 @@ void Usb::close()
775807
std::cerr << endl << "Error: close() libusb_release_interface returned " << r << endl;
776808
}
777809
closeHandle();
778-
}
779-
// ----------------------------------------------------------------------
780-
void Usb::teardown()
781-
{
782-
libusb_exit(context);
783-
context = nullptr;
810+
closeLibusb();
784811
}
785812
// ----------------------------------------------------------------------
786813
void Usb::setWaitWithTimeout(uint8_t waitSecs)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ class Usb : public UsbRawInputListener
301301
InitStatus init();
302302
void process();
303303
void close();
304-
void teardown();
305304
void setWaitWithTimeout(uint8_t waitSecs);
306305

307306
UsbOutPackageData& getOutputPackageData();
308307

309308
private:
310309
void closeHandle();
310+
void closeLibusb();
311311
const uint16_t usbVendorId{0x10ce};
312312
uint16_t usbProductId{0xeb93};
313313
libusb_context * context{nullptr};

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,10 @@ int XhcWhb04b6Component::run()
454454
{
455455
*mInitCout << " ok" << endl;
456456
process(); //process loops until disconnect/stopped
457-
mUsb.teardown();
458457
}
459458
else
460459
{
461460
mUsb.close();
462-
mUsb.teardown();
463461
}
464462
}
465463
}

0 commit comments

Comments
 (0)