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