BTReal: Log warnings when libusb calls fail

This commit is contained in:
Pokechu22 2022-06-08 12:46:53 -07:00
parent eeab51e3a4
commit 5d301cc206
1 changed files with 53 additions and 24 deletions

View File

@ -71,7 +71,9 @@ BluetoothRealDevice::~BluetoothRealDevice()
{
SendHCIResetCommand();
WaitForHCICommandComplete(HCI_CMD_RESET);
libusb_release_interface(m_handle, 0);
const int ret = libusb_release_interface(m_handle, 0);
if (ret != LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_release_interface failed: {}", LibusbUtils::ErrorWrap(ret));
libusb_close(m_handle);
libusb_unref_device(m_device);
}
@ -101,12 +103,22 @@ std::optional<IPCReply> BluetoothRealDevice::Open(const OpenRequest& request)
if (IsBluetoothDevice(descriptor) && IsWantedDevice(device_descriptor) && OpenDevice(device))
{
unsigned char manufacturer[50] = {}, product[50] = {}, serial_number[50] = {};
libusb_get_string_descriptor_ascii(m_handle, device_descriptor.iManufacturer, manufacturer,
sizeof(manufacturer));
libusb_get_string_descriptor_ascii(m_handle, device_descriptor.iProduct, product,
sizeof(product));
libusb_get_string_descriptor_ascii(m_handle, device_descriptor.iSerialNumber, serial_number,
sizeof(serial_number));
const int manufacturer_ret = libusb_get_string_descriptor_ascii(
m_handle, device_descriptor.iManufacturer, manufacturer, sizeof(manufacturer));
const int product_ret = libusb_get_string_descriptor_ascii(
m_handle, device_descriptor.iProduct, product, sizeof(product));
const int serial_ret = libusb_get_string_descriptor_ascii(
m_handle, device_descriptor.iSerialNumber, serial_number, sizeof(serial_number));
if (manufacturer_ret < LIBUSB_SUCCESS || product_ret < LIBUSB_SUCCESS ||
serial_ret < LIBUSB_SUCCESS)
{
ERROR_LOG_FMT(IOS_WIIMOTE,
"Failed to get descriptor for device {:04x}:{:04x} (rev {:x}): {}/{}/{}",
device_descriptor.idVendor, device_descriptor.idProduct,
device_descriptor.bcdDevice, LibusbUtils::ErrorWrap(manufacturer_ret),
LibusbUtils::ErrorWrap(product_ret), LibusbUtils::ErrorWrap(serial_ret));
return true;
}
NOTICE_LOG_FMT(IOS_WIIMOTE, "Using device {:04x}:{:04x} (rev {:x}) for Bluetooth: {} {} {}",
device_descriptor.idVendor, device_descriptor.idProduct,
device_descriptor.bcdDevice, reinterpret_cast<char*>(manufacturer),
@ -150,7 +162,9 @@ std::optional<IPCReply> BluetoothRealDevice::Close(u32 fd)
{
if (m_handle)
{
libusb_release_interface(m_handle, 0);
const int ret = libusb_release_interface(m_handle, 0);
if (ret != LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_release_interface failed: {}", LibusbUtils::ErrorWrap(ret));
libusb_close(m_handle);
libusb_unref_device(m_device);
m_handle = nullptr;
@ -211,7 +225,9 @@ std::optional<IPCReply> BluetoothRealDevice::IOCtlV(const IOCtlVRequest& request
};
PendingTransfer pending_transfer{std::move(cmd), std::move(buffer)};
m_current_transfers.emplace(transfer, std::move(pending_transfer));
libusb_submit_transfer(transfer);
const int ret = libusb_submit_transfer(transfer);
if (ret != LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_submit_transfer failed: {}", LibusbUtils::ErrorWrap(ret));
break;
}
// ACL data (incoming or outgoing) and incoming HCI events (respectively)
@ -261,7 +277,9 @@ std::optional<IPCReply> BluetoothRealDevice::IOCtlV(const IOCtlVRequest& request
transfer->user_data = this;
PendingTransfer pending_transfer{std::move(cmd), std::move(buffer)};
m_current_transfers.emplace(transfer, std::move(pending_transfer));
libusb_submit_transfer(transfer);
const int ret = libusb_submit_transfer(transfer);
if (ret != LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_submit_transfer failed: {}", LibusbUtils::ErrorWrap(ret));
break;
}
}
@ -363,7 +381,7 @@ void BluetoothRealDevice::WaitForHCICommandComplete(const u16 opcode)
{
const int ret = libusb_interrupt_transfer(m_handle, HCI_EVENT, buffer.data(),
static_cast<int>(buffer.size()), &actual_length, 20);
if (ret != 0 || actual_length < static_cast<int>(sizeof(packet)))
if (ret != LIBUSB_SUCCESS || actual_length < static_cast<int>(sizeof(packet)))
continue;
std::memcpy(&packet, buffer.data(), sizeof(packet));
if (packet.EventType == HCI_EVENT_COMMAND_COMPL && packet.Opcode == opcode)
@ -376,7 +394,11 @@ void BluetoothRealDevice::SendHCIResetCommand()
u8 packet[3] = {};
const u16 payload[] = {HCI_CMD_RESET};
memcpy(packet, payload, sizeof(payload));
const int ret =
libusb_control_transfer(m_handle, REQUEST_TYPE, 0, 0, 0, packet, sizeof(packet), TIMEOUT);
if (ret < LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_control_transfer failed: {}", LibusbUtils::ErrorWrap(ret));
else
INFO_LOG_FMT(IOS_WIIMOTE, "Sent a reset command to adapter");
}
@ -393,8 +415,11 @@ void BluetoothRealDevice::SendHCIDeleteLinkKeyCommand()
payload.command.bdaddr = {};
payload.command.delete_all = true;
const int ret =
libusb_control_transfer(m_handle, REQUEST_TYPE, 0, 0, 0, reinterpret_cast<u8*>(&payload),
static_cast<u16>(sizeof(payload)), TIMEOUT);
if (ret < LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_control_transfer failed: {}", LibusbUtils::ErrorWrap(ret));
}
bool BluetoothRealDevice::SendHCIStoreLinkKeyCommand()
@ -431,8 +456,10 @@ bool BluetoothRealDevice::SendHCIStoreLinkKeyCommand()
iterator += entry.second.size();
}
libusb_control_transfer(m_handle, REQUEST_TYPE, 0, 0, 0, packet.data(),
const int ret = libusb_control_transfer(m_handle, REQUEST_TYPE, 0, 0, 0, packet.data(),
static_cast<u16>(packet.size()), TIMEOUT);
if (ret < LIBUSB_SUCCESS)
WARN_LOG_FMT(IOS_WIIMOTE, "libusb_control_transfer failed: {}", LibusbUtils::ErrorWrap(ret));
return true;
}
@ -572,10 +599,10 @@ bool BluetoothRealDevice::OpenDevice(libusb_device* device)
{
m_device = libusb_ref_device(device);
const int ret = libusb_open(m_device, &m_handle);
if (ret != 0)
if (ret != LIBUSB_SUCCESS)
{
m_last_open_error =
Common::FmtFormatT("Failed to open Bluetooth device: {0}", libusb_error_name(ret));
Common::FmtFormatT("Failed to open Bluetooth device: {0}", LibusbUtils::ErrorWrap(ret));
return false;
}
@ -583,20 +610,22 @@ bool BluetoothRealDevice::OpenDevice(libusb_device* device)
// https://lists.freebsd.org/pipermail/freebsd-usb/2016-March/014161.html
#ifndef __FreeBSD__
int result = libusb_set_auto_detach_kernel_driver(m_handle, 1);
if (result != 0)
if (result != LIBUSB_SUCCESS)
{
result = libusb_detach_kernel_driver(m_handle, INTERFACE);
if (result < 0 && result != LIBUSB_ERROR_NOT_FOUND && result != LIBUSB_ERROR_NOT_SUPPORTED)
if (result != LIBUSB_SUCCESS && result != LIBUSB_ERROR_NOT_FOUND &&
result != LIBUSB_ERROR_NOT_SUPPORTED)
{
m_last_open_error = Common::FmtFormatT(
"Failed to detach kernel driver for BT passthrough: {0}", libusb_error_name(result));
"Failed to detach kernel driver for BT passthrough: {0}", LibusbUtils::ErrorWrap(result));
return false;
}
}
#endif
if (libusb_claim_interface(m_handle, INTERFACE) < 0)
if (const int result2 = libusb_claim_interface(m_handle, INTERFACE); result2 != LIBUSB_SUCCESS)
{
m_last_open_error = Common::GetStringT("Failed to claim interface for BT passthrough");
m_last_open_error = Common::FmtFormatT("Failed to claim interface for BT passthrough: {0}",
LibusbUtils::ErrorWrap(result2));
return false;
}