Skip to content

Commit 8252750

Browse files
committed
whb04b: Error handling
1 parent ee628c8 commit 8252750

3 files changed

Lines changed: 38 additions & 33 deletions

File tree

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ bool Usb::isWaitForPendantBeforeHalEnabled() const
135135
return isWaitWithTimeout;
136136
}
137137
// ----------------------------------------------------------------------
138-
bool Usb::getDoReconnect() const
139-
{
140-
return mDoReconnect;
141-
}
142-
// ----------------------------------------------------------------------
143138
void Usb::setUsbProductId(uint16_t usbProductId)
144139
{
145140
this->usbProductId = usbProductId;
@@ -436,6 +431,7 @@ bool Usb::setupAsyncTransfer()
436431
int r = libusb_submit_transfer(inTransfer);
437432
if(r != 0){
438433
std::cerr << "libusb_submit_transfer failed with " << r << endl;
434+
mDoReconnect = true;
439435
return false;
440436
}
441437
else
@@ -518,7 +514,7 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
518514
{
519515
if(!setupAsyncTransfer())
520516
{
521-
requestTermination();
517+
transferFailed = true;
522518
}
523519
}
524520

@@ -530,7 +526,7 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
530526
{
531527
if(!setupAsyncTransfer())
532528
{
533-
requestTermination();
529+
transferFailed = true;
534530
}
535531
}
536532
break;
@@ -543,12 +539,12 @@ void Usb::onUsbDataReceived(struct libusb_transfer* transfer)
543539
case (LIBUSB_TRANSFER_OVERFLOW):
544540
case (LIBUSB_TRANSFER_ERROR):
545541
std::cerr << "transfer error: " << transfer->status << endl;
546-
requestTermination();
542+
transferFailed = true;
547543
break;
548544

549545
default:
550546
std::cerr << "unknown transfer status: " << transfer->status << endl;
551-
requestTermination();
547+
transferFailed = true;
552548
break;
553549
}
554550
//libusb_free_transfer(transfer);
@@ -604,7 +600,7 @@ Usb::InitStatus Usb::init()
604600
{
605601
std::cerr << endl << "Bug: Not cleaned up deviceHandle properly before calling init again!" << endl;
606602
}
607-
if (getDoReconnect())
603+
if (mDoReconnect)
608604
{
609605
int pauseSecs = 3;
610606
*verboseInitOut << "init pausing " << pauseSecs << "s, waiting for device to be gone ...";
@@ -681,11 +677,11 @@ Usb::InitStatus Usb::init()
681677
sleep(1);
682678
}
683679
} while (deviceHandle == nullptr && mIsRunning);
684-
*verboseInitOut << " ok" << endl
685-
<< "init " << mName << " device found" << endl;
686680

687681
if (deviceHandle != nullptr)
688682
{
683+
*verboseInitOut << " ok" << endl
684+
<< "init " << mName << " device found" << endl;
689685
*verboseInitOut << "init detaching active kernel driver ...";
690686
r = libusb_kernel_driver_active(deviceHandle, 0);
691687
if (r == 1)
@@ -727,7 +723,7 @@ Usb::InitStatus Usb::init()
727723
return InitStatus::OK;
728724
}
729725
// ----------------------------------------------------------------------
730-
void Usb::handleTimeouts()
726+
void Usb::process()
731727
{
732728
struct timeval timeout;
733729
timeout.tv_sec = 0;
@@ -736,7 +732,14 @@ void Usb::handleTimeouts()
736732
int r = libusb_handle_events_timeout_completed(context, &timeout, nullptr);
737733
if(r != 0)
738734
{
739-
std::cerr << endl << "Error: handleTimeouts() libusb_handle_events_timeout_completed returned " << r << endl;
735+
std::cerr << endl << "Error: process() libusb_handle_events_timeout_completed returned " << r << endl;
736+
}
737+
738+
if(transferFailed)
739+
{
740+
close();
741+
transferFailed = false;
742+
mDoReconnect = true;
740743
}
741744
}
742745
// ----------------------------------------------------------------------

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ class Usb : public UsbRawInputListener
287287
void setUsbProductId(uint16_t usbProductId);
288288
bool isDeviceOpen() const;
289289
bool isWaitForPendantBeforeHalEnabled() const;
290-
bool getDoReconnect() const;
291290
void onUsbDataReceived(struct libusb_transfer* transfer) override;
292291
void setSimulationMode(bool isSimulationMode);
293292
void setIsRunning(bool enableRunning);
@@ -300,7 +299,7 @@ class Usb : public UsbRawInputListener
300299
void enableVerboseRx(bool enable);
301300
void enableVerboseInit(bool enable);
302301
InitStatus init();
303-
void handleTimeouts();
302+
void process();
304303
void close();
305304
void teardown();
306305
void setWaitWithTimeout(uint8_t waitSecs);
@@ -318,6 +317,7 @@ class Usb : public UsbRawInputListener
318317
bool mIsSimulationMode{false};
319318
SleepDetect sleepState;
320319
bool mIsRunning{false};
320+
bool transferFailed{false};
321321
UsbInPackageBuffer inputPackageBuffer;
322322
UsbOutPackageBuffer outputPackageBuffer;
323323
UsbOutPackageData outputPackageData;

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,18 @@ int XhcWhb04b6Component::run()
450450
if (mUsb.isDeviceOpen())
451451
{
452452
*mInitCout << "init enabling reception ...";
453-
if (!mUsb.setupAsyncTransfer())
453+
if (mUsb.setupAsyncTransfer())
454454
{
455-
std::cerr << endl << "failed to enable reception" << endl;
456-
return EXIT_FAILURE;
455+
*mInitCout << " ok" << endl;
456+
process(); //process loops until disconnect/stopped
457+
mUsb.teardown();
458+
}
459+
else
460+
{
461+
mUsb.close();
462+
mUsb.teardown();
457463
}
458-
*mInitCout << " ok" << endl;
459464
}
460-
process();
461-
mUsb.teardown();
462465
}
463466
else if(status == Usb::InitStatus::RETRY)
464467
{
@@ -487,23 +490,22 @@ void XhcWhb04b6Component::setSimulationMode(bool enableSimulationMode)
487490
// ----------------------------------------------------------------------
488491
void XhcWhb04b6Component::process()
489492
{
490-
if (mUsb.isDeviceOpen())
493+
while (isRunning() && mUsb.isDeviceOpen())
491494
{
492-
while (isRunning() && !mUsb.getDoReconnect())
495+
if (mHal.isSimulationModeEnabled())
493496
{
494-
mUsb.handleTimeouts();
495-
if (mHal.isSimulationModeEnabled())
496-
{
497-
linuxcncSimulate();
498-
}
499-
updateDisplay();
497+
linuxcncSimulate();
500498
}
501499
updateDisplay();
502-
503-
mHal.setIsPendantConnected(false);
504-
*mInitCout << "connection lost, cleaning up" << endl;
500+
mUsb.process();
501+
}
502+
if(mUsb.isDeviceOpen())
503+
{
505504
mUsb.close();
506505
}
506+
507+
mHal.setIsPendantConnected(false);
508+
*mInitCout << "connection lost, cleaning up" << endl;
507509
}
508510
// ----------------------------------------------------------------------
509511
void XhcWhb04b6Component::enableVerbosePendant(bool enable)

0 commit comments

Comments
 (0)