From 4df25086d1aa060dcc3b2793bc5be20c7380e0a1 Mon Sep 17 00:00:00 2001 From: Swiftb0y <12380386+Swiftb0y@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:57:05 +0200 Subject: [PATCH] refactor: remove OS-specific hidapi code --- src/controllers/bulk/bulkcontroller.cpp | 48 +++++++------------------ src/controllers/bulk/bulkcontroller.h | 13 ++++--- 2 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/controllers/bulk/bulkcontroller.cpp b/src/controllers/bulk/bulkcontroller.cpp index 24c9e1ee7b03..9f5b150c9ed8 100644 --- a/src/controllers/bulk/bulkcontroller.cpp +++ b/src/controllers/bulk/bulkcontroller.cpp @@ -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(value)) { + if (!ok || m_interfaceNumber != value) { return false; } -#endif // Match found return true; @@ -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) { @@ -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(); @@ -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; diff --git a/src/controllers/bulk/bulkcontroller.h b/src/controllers/bulk/bulkcontroller.h index f7376c8ccf99..5e2b8dc17735 100644 --- a/src/controllers/bulk/bulkcontroller.h +++ b/src/controllers/bulk/bulkcontroller.h @@ -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;