Skip to content

Commit

Permalink
BTReal: Improve error handling for device opening
Browse files Browse the repository at this point in the history
This commit attempts to improve error handling for device opening by
reducing panic alert spam when opening one or several devices fails.
Currently, Dolphin shows a panic alert for every device that we fail
to open, and another panic alert at the end if no usable device was
found. That is certainly a bit excessive -- we should only keep the
very last panic alert (the one that is shown if everything fails)
and we can just put the error for the last device open operation there.

This also changes the PanicAlert to a CriticalAlert to ensure the
message is visible even if the user has disabled regular panic alerts.

The message has also been reworded and should hopefully be clearer.
  • Loading branch information
leoetlino committed Mar 15, 2020
1 parent d297080 commit 73e7f2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <utility>
#include <vector>

#include <fmt/format.h>
#include <libusb.h>

#include "Common/ChunkFile.h"
Expand Down Expand Up @@ -81,6 +82,7 @@ IPCCommandResult BluetoothReal::Open(const OpenRequest& request)
if (!m_context.IsValid())
return GetDefaultReply(IPC_EACCES);

m_last_open_error.clear();
m_context.GetDeviceList([this](libusb_device* device) {
libusb_device_descriptor device_descriptor;
libusb_get_device_descriptor(device, &device_descriptor);
Expand Down Expand Up @@ -115,8 +117,19 @@ IPCCommandResult BluetoothReal::Open(const OpenRequest& request)

if (m_handle == nullptr)
{
PanicAlertT("Bluetooth passthrough mode is enabled, "
"but no usable Bluetooth USB device was found. Aborting.");
if (m_last_open_error.empty())
{
CriticalAlertT(
"Could not find any usable Bluetooth USB adapter for Bluetooth Passthrough.\n\n"
"The emulated console will now stop.");
}
else
{
CriticalAlertT("Could not find any usable Bluetooth USB adapter for Bluetooth Passthrough.\n"
"The following error occurred when Dolphin tried to use an adapter:\n%s\n\n"
"The emulated console will now stop.",
m_last_open_error.c_str());
}
Core::QueueHostJob(Core::Stop);
return GetDefaultReply(IPC_ENOENT);
}
Expand Down Expand Up @@ -547,7 +560,8 @@ bool BluetoothReal::OpenDevice(libusb_device* device)
const int ret = libusb_open(m_device, &m_handle);
if (ret != 0)
{
PanicAlertT("Failed to open Bluetooth device: %s", libusb_error_name(ret));
m_last_open_error = fmt::format(Common::GetStringT("Failed to open Bluetooth device: {}"),
libusb_error_name(ret));
return false;
}

Expand All @@ -560,15 +574,16 @@ bool BluetoothReal::OpenDevice(libusb_device* device)
result = libusb_detach_kernel_driver(m_handle, INTERFACE);
if (result < 0 && result != LIBUSB_ERROR_NOT_FOUND && result != LIBUSB_ERROR_NOT_SUPPORTED)
{
PanicAlertT("Failed to detach kernel driver for BT passthrough: %s",
libusb_error_name(result));
m_last_open_error =
fmt::format(Common::GetStringT("Failed to detach kernel driver for BT passthrough: {}"),
libusb_error_name(result));
return false;
}
}
#endif
if (libusb_claim_interface(m_handle, INTERFACE) < 0)
{
PanicAlertT("Failed to claim interface for BT passthrough");
m_last_open_error = Common::GetStringT("Failed to claim interface for BT passthrough");
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Core/IOS/USB/Bluetooth/BTReal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class BluetoothReal final : public BluetoothBase
std::atomic<SyncButtonState> m_sync_button_state{SyncButtonState::Unpressed};
Common::Timer m_sync_button_held_timer;

std::string m_last_open_error;

LibusbUtils::Context m_context;
libusb_device* m_device = nullptr;
libusb_device_handle* m_handle = nullptr;
Expand Down

0 comments on commit 73e7f2a

Please sign in to comment.