Skip to content

Commit

Permalink
refactor: remove OS-specific hidapi code
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Sep 13, 2024
1 parent 46cf164 commit 4df2508
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
48 changes: 13 additions & 35 deletions src/controllers/bulk/bulkcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,10 @@ bool BulkController::matchProductInfo(const ProductInfo& product) {
return false;
}

#if defined(__WINDOWS__) || defined(__APPLE__)
value = product.interface_number.toInt(&ok, 16);
if (!ok || m_interfaceNumber != static_cast<unsigned int>(value)) {
if (!ok || m_interfaceNumber != value) {
return false;
}
#endif

// Match found
return true;
Expand All @@ -161,9 +159,7 @@ int BulkController::open() {
}
m_inEndpointAddr = pDevice->endpoints.in_epaddr;
m_outEndpointAddr = pDevice->endpoints.out_epaddr;
#if defined(__WINDOWS__) || defined(__APPLE__)
m_interfaceNumber = pDevice->endpoints.interface_number;
#endif

// XXX: we should enumerate devices and match vendor, product, and serial
if (m_phandle == nullptr) {
Expand All @@ -175,31 +171,18 @@ int BulkController::open() {
qCWarning(m_logBase) << "Unable to open USB Bulk device" << getName();
return -1;
}

#if defined(__WINDOWS__) || defined(__APPLE__)
if (m_interfaceNumber && libusb_kernel_driver_active(m_phandle, m_interfaceNumber) == 1) {
qCDebug(m_logBase) << "Found a driver active for" << getName();
if (libusb_detach_kernel_driver(m_phandle, 0) == 0)
qCDebug(m_logBase) << "Kernel driver detached for" << getName();
else {
qCWarning(m_logBase) << "Couldn't detach kernel driver for" << getName();
libusb_close(m_phandle);
return -1;
}
if (libusb_set_auto_detach_kernel_driver(m_phandle, true) == LIBUSB_ERROR_NOT_SUPPORTED) {
qCDebug(m_logBase) << "unable to automatically detach kernel driver for" << getName();
}

if (m_interfaceNumber) {
int ret = libusb_claim_interface(m_phandle, m_interfaceNumber);
if (ret < 0) {
qCWarning(m_logBase) << "Cannot claim interface for" << getName()
<< ":" << libusb_error_name(ret);
libusb_close(m_phandle);
return -1;
} else {
qCDebug(m_logBase) << "Claimed interface for" << getName();
}
if (int ret = libusb_claim_interface(m_phandle, m_interfaceNumber); ret < 0) {
qCWarning(m_logBase) << "Cannot claim interface for" << getName()
<< ":" << libusb_error_name(ret);
libusb_close(m_phandle);
return -1;
} else {
qCDebug(m_logBase) << "Claimed interface for" << getName();
}
#endif

setOpen(true);
startEngine();
Expand Down Expand Up @@ -254,15 +237,10 @@ int BulkController::close() {
stopEngine();

// Close device
#if defined(__WINDOWS__) || defined(__APPLE__)
if (m_interfaceNumber) {
int ret = libusb_release_interface(m_phandle, m_interfaceNumber);
if (ret < 0) {
qCWarning(m_logBase) << "Cannot release interface for" << getName()
<< ":" << libusb_error_name(ret);
}
if (int ret = libusb_release_interface(m_phandle, m_interfaceNumber); ret < 0) {
qCWarning(m_logBase) << "Cannot release interface for" << getName()
<< ":" << libusb_error_name(ret);
}
#endif
qCInfo(m_logBase) << " Closing device";
libusb_close(m_phandle);
m_phandle = nullptr;
Expand Down
13 changes: 6 additions & 7 deletions src/controllers/bulk/bulkcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ class BulkController : public Controller {

// Local copies of things we need from desc

unsigned short m_vendorId;
unsigned short m_productId;
unsigned char m_inEndpointAddr;
unsigned char m_outEndpointAddr;
#if defined(__WINDOWS__) || defined(__APPLE__)
unsigned int m_interfaceNumber;
#endif
std::uint16_t m_vendorId;
std::uint16_t m_productId;
std::uint8_t m_inEndpointAddr;
std::uint8_t m_outEndpointAddr;
std::uint8_t m_interfaceNumber;

QString m_manufacturer;
QString m_product;

Expand Down

0 comments on commit 4df2508

Please sign in to comment.