Skip to content

Commit 010897f

Browse files
committed
whb04b: Cleanup
XhcWhb04b6Component should not even include libusb
1 parent 6024cd7 commit 010897f

4 files changed

Lines changed: 33 additions & 72 deletions

File tree

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,6 @@ bool Usb::isDeviceOpen() const
140140
return deviceHandle != nullptr;
141141
}
142142
// ----------------------------------------------------------------------
143-
libusb_context** Usb::getContextReference()
144-
{
145-
return &context;
146-
}
147-
// ----------------------------------------------------------------------
148-
libusb_context* Usb::getContext()
149-
{
150-
return context;
151-
}
152-
// ----------------------------------------------------------------------
153-
void Usb::setContext(libusb_context* context)
154-
{
155-
this->context = context;
156-
}
157-
// ----------------------------------------------------------------------
158-
libusb_device_handle* Usb::getDeviceHandle()
159-
{
160-
return deviceHandle;
161-
}
162-
// ----------------------------------------------------------------------
163-
void Usb::setDeviceHandle(libusb_device_handle* deviceHandle)
164-
{
165-
this->deviceHandle = deviceHandle;
166-
}
167-
// ----------------------------------------------------------------------
168143
bool Usb::isWaitForPendantBeforeHalEnabled() const
169144
{
170145
return isWaitWithTimeout;
@@ -729,6 +704,33 @@ Usb::InitStatus Usb::init()
729704
}
730705
return InitStatus::OK;
731706
}
707+
void Usb::handleTimeouts()
708+
{
709+
struct timeval timeout;
710+
timeout.tv_sec = 0;
711+
timeout.tv_usec = 200 * 1000;
712+
713+
int r = libusb_handle_events_timeout_completed(context, &timeout, nullptr);
714+
assert((r == LIBUSB_SUCCESS) || (r == LIBUSB_ERROR_NO_DEVICE) || (r == LIBUSB_ERROR_BUSY) ||
715+
(r == LIBUSB_ERROR_TIMEOUT) || (r == LIBUSB_ERROR_INTERRUPTED));
716+
}
717+
void Usb::close()
718+
{
719+
struct timeval tv;
720+
tv.tv_sec = 1;
721+
tv.tv_usec = 0;
722+
int r = libusb_handle_events_timeout_completed(context, &tv, nullptr);
723+
assert(0 == r);
724+
r = libusb_release_interface(deviceHandle, 0);
725+
assert((0 == r) || (r == LIBUSB_ERROR_NO_DEVICE));
726+
libusb_close(deviceHandle);
727+
deviceHandle = nullptr;
728+
}
729+
void Usb::teardown()
730+
{
731+
libusb_exit(context);
732+
context = nullptr;
733+
}
732734
// ----------------------------------------------------------------------
733735
void Usb::setWaitWithTimeout(uint8_t waitSecs)
734736
{

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,6 @@ class Usb : public UsbRawInputListener
288288
uint16_t getUsbProductId() const;
289289
void setUsbProductId(uint16_t usbProductId);
290290
bool isDeviceOpen() const;
291-
libusb_context** getContextReference();
292-
libusb_context* getContext();
293-
void setContext(libusb_context* context);
294-
libusb_device_handle* getDeviceHandle();
295-
void setDeviceHandle(libusb_device_handle* deviceHandle);
296291
bool isWaitForPendantBeforeHalEnabled() const;
297292
bool getDoReconnect() const;
298293
void setDoReconnect(bool doReconnect);
@@ -308,6 +303,9 @@ class Usb : public UsbRawInputListener
308303
void enableVerboseRx(bool enable);
309304
void enableVerboseInit(bool enable);
310305
InitStatus init();
306+
void handleTimeouts();
307+
void close();
308+
void teardown();
311309
void setWaitWithTimeout(uint8_t waitSecs);
312310

313311
UsbOutPackageData& getOutputPackageData();

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

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <assert.h>
2424
#include <iostream>
2525
#include <iomanip>
26-
#include <libusb.h>
2726
#include <bitset>
2827

2928

@@ -459,7 +458,7 @@ int XhcWhb04b6Component::run()
459458
*mInitCout << " ok" << endl;
460459
}
461460
process();
462-
teardownUsb();
461+
mUsb.teardown();
463462
}
464463
}
465464
teardownHal();
@@ -483,34 +482,13 @@ void XhcWhb04b6Component::setSimulationMode(bool enableSimulationMode)
483482
mUsb.setSimulationMode(mIsSimulationMode);
484483
}
485484
// ----------------------------------------------------------------------
486-
void XhcWhb04b6Component::setUsbContext(libusb_context* context)
487-
{
488-
mUsb.setContext(context);
489-
}
490-
// ----------------------------------------------------------------------
491-
libusb_device_handle* XhcWhb04b6Component::getUsbDeviceHandle()
492-
{
493-
return mUsb.getDeviceHandle();
494-
}
495-
// ----------------------------------------------------------------------
496-
libusb_context* XhcWhb04b6Component::getUsbContext()
497-
{
498-
return mUsb.getContext();
499-
}
500-
// ----------------------------------------------------------------------
501485
void XhcWhb04b6Component::process()
502486
{
503487
if (mUsb.isDeviceOpen())
504488
{
505489
while (isRunning() && !mUsb.getDoReconnect())
506490
{
507-
struct timeval timeout;
508-
timeout.tv_sec = 0;
509-
timeout.tv_usec = 200 * 1000;
510-
511-
int r = libusb_handle_events_timeout_completed(getUsbContext(), &timeout, nullptr);
512-
assert((r == LIBUSB_SUCCESS) || (r == LIBUSB_ERROR_NO_DEVICE) || (r == LIBUSB_ERROR_BUSY) ||
513-
(r == LIBUSB_ERROR_TIMEOUT) || (r == LIBUSB_ERROR_INTERRUPTED));
491+
mUsb.handleTimeouts();
514492
if (mHal.isSimulationModeEnabled())
515493
{
516494
linuxcncSimulate();
@@ -521,24 +499,10 @@ void XhcWhb04b6Component::process()
521499

522500
mHal.setIsPendantConnected(false);
523501
*mInitCout << "connection lost, cleaning up" << endl;
524-
struct timeval tv;
525-
tv.tv_sec = 1;
526-
tv.tv_usec = 0;
527-
int r = libusb_handle_events_timeout_completed(getUsbContext(), &tv, nullptr);
528-
assert(0 == r);
529-
r = libusb_release_interface(getUsbDeviceHandle(), 0);
530-
assert((0 == r) || (r == LIBUSB_ERROR_NO_DEVICE));
531-
libusb_close(getUsbDeviceHandle());
532-
mUsb.setDeviceHandle(nullptr);
502+
mUsb.close();
533503
}
534504
}
535505
// ----------------------------------------------------------------------
536-
void XhcWhb04b6Component::teardownUsb()
537-
{
538-
libusb_exit(getUsbContext());
539-
mUsb.setContext(nullptr);
540-
}
541-
// ----------------------------------------------------------------------
542506
void XhcWhb04b6Component::enableVerbosePendant(bool enable)
543507
{
544508
mPendant.enableVerbose(enable);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ class XhcWhb04b6Component :
4242
virtual ~XhcWhb04b6Component();
4343
void process();
4444
void teardownUsb();
45-
void setUsbContext(libusb_context* context);
46-
libusb_device_handle* getUsbDeviceHandle();
47-
libusb_context* getUsbContext();
4845
//! \return the name as specified to \ref XhcWhb04b6Component
4946
const char* getName() const;
5047
//! \return the name as specified to \ref Hal

0 commit comments

Comments
 (0)