From 15cbb5c8f9ec1e64a137f93b032e8b5dc629817f Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 8 Jun 2022 12:17:37 -0700 Subject: [PATCH] Log warnings when LibusbUtils::GetDeviceList fails --- Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp | 7 ++++++- Source/Core/Core/IOS/USB/Host.cpp | 4 +++- Source/Core/Core/LibusbUtils.cpp | 16 ++++++++-------- Source/Core/Core/LibusbUtils.h | 2 +- Source/Core/InputCommon/GCAdapter.cpp | 4 +++- Source/Core/UICommon/USBUtils.cpp | 5 ++++- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp index 12def23084..8619d8766e 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp @@ -84,7 +84,7 @@ std::optional BluetoothRealDevice::Open(const OpenRequest& request) return IPCReply(IPC_EACCES); m_last_open_error.clear(); - m_context.GetDeviceList([this](libusb_device* device) { + const int ret = m_context.GetDeviceList([this](libusb_device* device) { libusb_device_descriptor device_descriptor; libusb_get_device_descriptor(device, &device_descriptor); auto config_descriptor = LibusbUtils::MakeConfigDescriptor(device); @@ -116,6 +116,11 @@ std::optional BluetoothRealDevice::Open(const OpenRequest& request) } return true; }); + if (ret != LIBUSB_SUCCESS) + { + m_last_open_error = + Common::FmtFormatT("GetDeviceList failed: {0}", LibusbUtils::ErrorWrap(ret)); + } if (m_handle == nullptr) { diff --git a/Source/Core/Core/IOS/USB/Host.cpp b/Source/Core/Core/IOS/USB/Host.cpp index 0fdaafb611..132f8e5247 100644 --- a/Source/Core/Core/IOS/USB/Host.cpp +++ b/Source/Core/Core/IOS/USB/Host.cpp @@ -121,7 +121,7 @@ bool USBHost::AddNewDevices(std::set& new_devices, DeviceChangeHooks& hooks if (m_context.IsValid()) { - m_context.GetDeviceList([&](libusb_device* device) { + const int ret = m_context.GetDeviceList([&](libusb_device* device) { libusb_device_descriptor descriptor; libusb_get_device_descriptor(device, &descriptor); if (whitelist.count({descriptor.idVendor, descriptor.idProduct}) == 0) @@ -137,6 +137,8 @@ bool USBHost::AddNewDevices(std::set& new_devices, DeviceChangeHooks& hooks hooks.emplace(GetDeviceById(id), ChangeEvent::Inserted); return true; }); + if (ret != LIBUSB_SUCCESS) + WARN_LOG_FMT(IOS_USB, "GetDeviceList failed: {}", LibusbUtils::ErrorWrap(ret)); } #endif return true; diff --git a/Source/Core/Core/LibusbUtils.cpp b/Source/Core/Core/LibusbUtils.cpp index b905219207..f716cc4884 100644 --- a/Source/Core/Core/LibusbUtils.cpp +++ b/Source/Core/Core/LibusbUtils.cpp @@ -47,16 +47,16 @@ public: libusb_exit(m_context); } - libusb_context* GetContext() { return m_context; } + libusb_context* GetContext() const { return m_context; } - bool GetDeviceList(GetDeviceListCallback callback) + int GetDeviceList(GetDeviceListCallback callback) const { std::lock_guard lock{m_device_list_mutex}; libusb_device** list; ssize_t count = libusb_get_device_list(m_context, &list); if (count < 0) - return false; + return static_cast(count); for (ssize_t i = 0; i < count; ++i) { @@ -64,7 +64,7 @@ public: break; } libusb_free_device_list(list, 1); - return true; + return LIBUSB_SUCCESS; } private: @@ -81,7 +81,7 @@ private: } libusb_context* m_context = nullptr; - std::mutex m_device_list_mutex; + mutable std::mutex m_device_list_mutex; Common::Flag m_event_thread_running; std::thread m_event_thread; }; @@ -89,8 +89,8 @@ private: class Context::Impl { public: - libusb_context* GetContext() { return nullptr; } - bool GetDeviceList(GetDeviceListCallback callback) { return false; } + libusb_context* GetContext() const { return nullptr; } + int GetDeviceList(GetDeviceListCallback callback) const { return -1; } }; #endif @@ -110,7 +110,7 @@ bool Context::IsValid() const return m_impl->GetContext() != nullptr; } -bool Context::GetDeviceList(GetDeviceListCallback callback) +int Context::GetDeviceList(GetDeviceListCallback callback) const { return m_impl->GetDeviceList(std::move(callback)); } diff --git a/Source/Core/Core/LibusbUtils.h b/Source/Core/Core/LibusbUtils.h index aaa77de74b..4fe3c0289f 100644 --- a/Source/Core/Core/LibusbUtils.h +++ b/Source/Core/Core/LibusbUtils.h @@ -31,7 +31,7 @@ public: bool IsValid() const; // Only valid if the context is valid. - bool GetDeviceList(GetDeviceListCallback callback); + int GetDeviceList(GetDeviceListCallback callback) const; private: class Impl; diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index cf1b103f2f..993e032bf3 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -462,7 +462,7 @@ static void Setup() s_controller_type.fill(ControllerType::None); s_controller_rumble.fill(0); - s_libusb_context->GetDeviceList([](libusb_device* device) { + const int ret = s_libusb_context->GetDeviceList([](libusb_device* device) { if (CheckDeviceAccess(device)) { // Only connect to a single adapter in case the user has multiple connected @@ -471,6 +471,8 @@ static void Setup() } return true; }); + if (ret != LIBUSB_SUCCESS) + WARN_LOG_FMT(CONTROLLERINTERFACE, "Failed to get device list: {}", LibusbUtils::ErrorWrap(ret)); if (s_status != ADAPTER_DETECTED && prev_status != s_status && s_detect_callback != nullptr) s_detect_callback(); diff --git a/Source/Core/UICommon/USBUtils.cpp b/Source/Core/UICommon/USBUtils.cpp index b33dc740e0..2264afe82c 100644 --- a/Source/Core/UICommon/USBUtils.cpp +++ b/Source/Core/UICommon/USBUtils.cpp @@ -11,6 +11,7 @@ #endif #include "Common/CommonTypes.h" +#include "Common/Logging/Log.h" #include "Core/LibusbUtils.h" // Because opening and getting the device name from devices is slow, especially on Windows @@ -45,13 +46,15 @@ std::map, std::string> GetInsertedDevices() if (!context.IsValid()) return devices; - context.GetDeviceList([&](libusb_device* device) { + const int ret = context.GetDeviceList([&](libusb_device* device) { libusb_device_descriptor descr; libusb_get_device_descriptor(device, &descr); const std::pair vid_pid{descr.idVendor, descr.idProduct}; devices[vid_pid] = GetDeviceName(vid_pid); return true; }); + if (ret != LIBUSB_SUCCESS) + WARN_LOG_FMT(COMMON, "GetDeviceList failed: {}", LibusbUtils::ErrorWrap(ret)); #endif return devices;