Merge pull request #4276 from ameerj/master

input_common: Add more libusb error checks for GC Adapter
This commit is contained in:
bunnei 2020-07-08 15:34:37 -04:00 committed by GitHub
commit 713e204a8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,9 +24,13 @@ Adapter::Adapter() {
LOG_INFO(Input, "GC Adapter Initialization started"); LOG_INFO(Input, "GC Adapter Initialization started");
current_status = NO_ADAPTER_DETECTED; current_status = NO_ADAPTER_DETECTED;
libusb_init(&libusb_ctx);
const int init_res = libusb_init(&libusb_ctx);
if (init_res == LIBUSB_SUCCESS) {
StartScanThread(); StartScanThread();
} else {
LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res);
}
} }
GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) {
@ -211,11 +215,18 @@ void Adapter::Setup() {
adapter_controllers_status.fill(ControllerTypes::None); adapter_controllers_status.fill(ControllerTypes::None);
// pointer to list of connected usb devices // pointer to list of connected usb devices
libusb_device** devices; libusb_device** devices{};
// populate the list of devices, get the count // populate the list of devices, get the count
const std::size_t device_count = libusb_get_device_list(libusb_ctx, &devices); const ssize_t device_count = libusb_get_device_list(libusb_ctx, &devices);
if (device_count < 0) {
LOG_ERROR(Input, "libusb_get_device_list failed with error: {}", device_count);
detect_thread_running = false; // Stop the loop constantly checking for gc adapter
// TODO: For hotplug+gc adapter checkbox implementation, revert this.
return;
}
if (devices != nullptr) {
for (std::size_t index = 0; index < device_count; ++index) { for (std::size_t index = 0; index < device_count; ++index) {
if (CheckDeviceAccess(devices[index])) { if (CheckDeviceAccess(devices[index])) {
// GC Adapter found and accessible, registering it // GC Adapter found and accessible, registering it
@ -223,6 +234,8 @@ void Adapter::Setup() {
break; break;
} }
} }
libusb_free_device_list(devices, 1);
}
} }
bool Adapter::CheckDeviceAccess(libusb_device* device) { bool Adapter::CheckDeviceAccess(libusb_device* device) {
@ -279,7 +292,13 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) {
void Adapter::GetGCEndpoint(libusb_device* device) { void Adapter::GetGCEndpoint(libusb_device* device) {
libusb_config_descriptor* config = nullptr; libusb_config_descriptor* config = nullptr;
libusb_get_config_descriptor(device, 0, &config); const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config);
if (config_descriptor_return != LIBUSB_SUCCESS) {
LOG_ERROR(Input, "libusb_get_config_descriptor failed with error = {}",
config_descriptor_return);
return;
}
for (u8 ic = 0; ic < config->bNumInterfaces; ic++) { for (u8 ic = 0; ic < config->bNumInterfaces; ic++) {
const libusb_interface* interfaceContainer = &config->interface[ic]; const libusb_interface* interfaceContainer = &config->interface[ic];
for (int i = 0; i < interfaceContainer->num_altsetting; i++) { for (int i = 0; i < interfaceContainer->num_altsetting; i++) {