Commit 3782a82b authored by reillyg@chromium.org's avatar reillyg@chromium.org

Log errors from libusb.

Convert numeric error code from libusb into human readable strings and
log them. Some of these errors should, in addition, be passed up to the
running script.

BUG=

Review URL: https://codereview.chromium.org/344793009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278769 0039d316-1c4b-4281-b951-d872f2087c98
parent fc3f8c20
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
'usb_service/usb_device_handle_impl.cc', 'usb_service/usb_device_handle_impl.cc',
'usb_service/usb_device_handle_impl.h', 'usb_service/usb_device_handle_impl.h',
'usb_service/usb_device_handle.h', 'usb_service/usb_device_handle.h',
'usb_service/usb_error.cc',
'usb_service/usb_error.h',
'usb_service/usb_interface.h', 'usb_service/usb_interface.h',
'usb_service/usb_interface_impl.cc', 'usb_service/usb_interface_impl.cc',
'usb_service/usb_interface_impl.h', 'usb_service/usb_interface_impl.h',
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "components/usb_service/usb_error.h"
#include "third_party/libusb/src/libusb/interrupt.h" #include "third_party/libusb/src/libusb/interrupt.h"
#include "third_party/libusb/src/libusb/libusb.h" #include "third_party/libusb/src/libusb/libusb.h"
...@@ -54,10 +55,13 @@ void UsbContext::UsbEventHandler::ThreadMain() { ...@@ -54,10 +55,13 @@ void UsbContext::UsbEventHandler::ThreadMain() {
VLOG(1) << "UsbEventHandler started."; VLOG(1) << "UsbEventHandler started.";
if (running_) { if (running_) {
start_polling_.Signal(); start_polling_.Signal();
libusb_handle_events(context_);
} }
while (running_) while (running_) {
libusb_handle_events(context_); const int rv = libusb_handle_events(context_);
if (rv != LIBUSB_SUCCESS) {
LOG(WARNING) << "Failed to handle events: " << ConvertErrorToString(rv);
}
}
VLOG(1) << "UsbEventHandler shutting down."; VLOG(1) << "UsbEventHandler shutting down.";
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_impl.h" #include "components/usb_service/usb_device_impl.h"
#include "components/usb_service/usb_error.h"
#include "components/usb_service/usb_interface.h" #include "components/usb_service/usb_interface.h"
#include "components/usb_service/usb_service.h" #include "components/usb_service/usb_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
...@@ -158,7 +159,11 @@ UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() { ...@@ -158,7 +159,11 @@ UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() {
} }
bool UsbDeviceHandleImpl::InterfaceClaimer::Claim() const { bool UsbDeviceHandleImpl::InterfaceClaimer::Claim() const {
return libusb_claim_interface(handle_->handle(), interface_number_) == 0; const int rv = libusb_claim_interface(handle_->handle(), interface_number_);
if (rv != LIBUSB_SUCCESS) {
LOG(ERROR) << "Failed to claim interface: " << ConvertErrorToString(rv);
}
return rv == LIBUSB_SUCCESS;
} }
struct UsbDeviceHandleImpl::Transfer { struct UsbDeviceHandleImpl::Transfer {
...@@ -344,13 +349,15 @@ bool UsbDeviceHandleImpl::SetInterfaceAlternateSetting( ...@@ -344,13 +349,15 @@ bool UsbDeviceHandleImpl::SetInterfaceAlternateSetting(
return false; return false;
const int rv = libusb_set_interface_alt_setting( const int rv = libusb_set_interface_alt_setting(
handle_, interface_number, alternate_setting); handle_, interface_number, alternate_setting);
if (rv == 0) { if (rv == LIBUSB_SUCCESS) {
claimed_interfaces_[interface_number]->set_alternate_setting( claimed_interfaces_[interface_number]->set_alternate_setting(
alternate_setting); alternate_setting);
RefreshEndpointMap(); RefreshEndpointMap();
return true; } else {
LOG(ERROR) << "Failed to set interface (" << interface_number
<< ", " << alternate_setting << "): " << ConvertErrorToString(rv);
} }
return false; return rv == LIBUSB_SUCCESS;
} }
bool UsbDeviceHandleImpl::ResetDevice() { bool UsbDeviceHandleImpl::ResetDevice() {
...@@ -358,7 +365,11 @@ bool UsbDeviceHandleImpl::ResetDevice() { ...@@ -358,7 +365,11 @@ bool UsbDeviceHandleImpl::ResetDevice() {
if (!device_) if (!device_)
return false; return false;
return libusb_reset_device(handle_) == 0; const int rv = libusb_reset_device(handle_);
if (rv != LIBUSB_SUCCESS) {
LOG(ERROR) << "Failed to reset device: " << ConvertErrorToString(rv);
}
return rv == LIBUSB_SUCCESS;
} }
bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) { bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
...@@ -366,8 +377,12 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) { ...@@ -366,8 +377,12 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
PlatformUsbDevice device = libusb_get_device(handle_); PlatformUsbDevice device = libusb_get_device(handle_);
libusb_device_descriptor desc; libusb_device_descriptor desc;
if (libusb_get_device_descriptor(device, &desc) != LIBUSB_SUCCESS) const int rv = libusb_get_device_descriptor(device, &desc);
if (rv != LIBUSB_SUCCESS) {
LOG(ERROR) << "Failed to read device descriptor: "
<< ConvertErrorToString(rv);
return false; return false;
}
if (desc.iSerialNumber == 0) if (desc.iSerialNumber == 0)
return false; return false;
...@@ -381,8 +396,11 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) { ...@@ -381,8 +396,11 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
0, 0,
reinterpret_cast<unsigned char*>(&langid[0]), reinterpret_cast<unsigned char*>(&langid[0]),
sizeof(langid)); sizeof(langid));
if (size < 0) if (size < 0) {
LOG(ERROR) << "Failed to get language IDs: "
<< ConvertErrorToString(size);
return false; return false;
}
int language_count = (size - 2) / 2; int language_count = (size - 2) / 2;
...@@ -395,6 +413,11 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) { ...@@ -395,6 +413,11 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
langid[i], langid[i],
reinterpret_cast<unsigned char*>(&text[0]), reinterpret_cast<unsigned char*>(&text[0]),
sizeof(text)); sizeof(text));
if (size < 0) {
LOG(ERROR) << "Failed to get serial number (langid " << langid[i] << "): "
<< ConvertErrorToString(size);
continue;
}
if (size <= 2) if (size <= 2)
continue; continue;
if ((text[0] >> 8) != LIBUSB_DT_STRING) if ((text[0] >> 8) != LIBUSB_DT_STRING)
...@@ -626,9 +649,11 @@ void UsbDeviceHandleImpl::SubmitTransfer( ...@@ -626,9 +649,11 @@ void UsbDeviceHandleImpl::SubmitTransfer(
// it requires an interface we didn't claim. // it requires an interface we didn't claim.
transfer.claimed_interface = GetClaimedInterfaceForEndpoint(handle->endpoint); transfer.claimed_interface = GetClaimedInterfaceForEndpoint(handle->endpoint);
if (libusb_submit_transfer(handle) == LIBUSB_SUCCESS) { const int rv = libusb_submit_transfer(handle);
if (rv == LIBUSB_SUCCESS) {
transfers_[handle] = transfer; transfers_[handle] = transfer;
} else { } else {
LOG(ERROR) << "Failed to submit transfer: " << ConvertErrorToString(rv);
message_loop_proxy->PostTask( message_loop_proxy->PostTask(
FROM_HERE, FROM_HERE,
base::Bind( base::Bind(
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_handle_impl.h" #include "components/usb_service/usb_device_handle_impl.h"
#include "components/usb_service/usb_error.h"
#include "components/usb_service/usb_interface_impl.h" #include "components/usb_service/usb_interface_impl.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h" #include "third_party/libusb/src/libusb/libusb.h"
...@@ -93,7 +94,7 @@ void UsbDeviceImpl::RequestUsbAcess( ...@@ -93,7 +94,7 @@ void UsbDeviceImpl::RequestUsbAcess(
scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
PlatformUsbDeviceHandle handle; PlatformUsbDeviceHandle handle;
int rv = libusb_open(platform_device_, &handle); const int rv = libusb_open(platform_device_, &handle);
if (LIBUSB_SUCCESS == rv) { if (LIBUSB_SUCCESS == rv) {
scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces(); scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces();
if (!interfaces) if (!interfaces)
...@@ -102,8 +103,10 @@ scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { ...@@ -102,8 +103,10 @@ scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
new UsbDeviceHandleImpl(context_, this, handle, interfaces); new UsbDeviceHandleImpl(context_, this, handle, interfaces);
handles_.push_back(device_handle); handles_.push_back(device_handle);
return device_handle; return device_handle;
} else {
LOG(ERROR) << "Failed to open device: " << ConvertErrorToString(rv);
return NULL;
} }
return NULL;
} }
bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) {
...@@ -124,12 +127,15 @@ scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() { ...@@ -124,12 +127,15 @@ scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
PlatformUsbConfigDescriptor platform_config; PlatformUsbConfigDescriptor platform_config;
const int list_result = const int rv =
libusb_get_active_config_descriptor(platform_device_, &platform_config); libusb_get_active_config_descriptor(platform_device_, &platform_config);
if (list_result == 0) if (rv == LIBUSB_SUCCESS) {
return new UsbConfigDescriptorImpl(platform_config); return new UsbConfigDescriptorImpl(platform_config);
} else {
return NULL; LOG(ERROR) << "Failed to get config descriptor: "
<< ConvertErrorToString(rv);
return NULL;
}
} }
void UsbDeviceImpl::OnDisconnect() { void UsbDeviceImpl::OnDisconnect() {
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/usb_service/usb_error.h"
#include "third_party/libusb/src/libusb/libusb.h"
namespace usb_service {
std::string ConvertErrorToString(int errcode) {
return libusb_strerror(static_cast<libusb_error>(errcode));
}
} // namespace usb_service;
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_USB_SERVICE_USB_ERROR_H_
#define COMPONENTS_USB_SERVICE_USB_ERROR_H_
#include <string>
namespace usb_service {
// A number of libusb functions which return a member of enum libusb_error
// return an int instead so for convenience this function takes an int.
std::string ConvertErrorToString(int errcode);
} // namespace usb_service;
#endif // COMPONENTS_USB_SERVICE_USB_ERROR_H_
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_impl.h" #include "components/usb_service/usb_device_impl.h"
#include "components/usb_service/usb_error.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h" #include "third_party/libusb/src/libusb/libusb.h"
...@@ -102,6 +103,10 @@ void UsbServiceImpl::RefreshDevices() { ...@@ -102,6 +103,10 @@ void UsbServiceImpl::RefreshDevices() {
libusb_device** platform_devices = NULL; libusb_device** platform_devices = NULL;
const ssize_t device_count = const ssize_t device_count =
libusb_get_device_list(context_->context(), &platform_devices); libusb_get_device_list(context_->context(), &platform_devices);
if (device_count < 0) {
LOG(ERROR) << "Failed to get device list: " <<
ConvertErrorToString(device_count);
}
std::set<UsbDevice*> connected_devices; std::set<UsbDevice*> connected_devices;
std::vector<PlatformUsbDevice> disconnected_devices; std::vector<PlatformUsbDevice> disconnected_devices;
...@@ -110,9 +115,14 @@ void UsbServiceImpl::RefreshDevices() { ...@@ -110,9 +115,14 @@ void UsbServiceImpl::RefreshDevices() {
for (ssize_t i = 0; i < device_count; ++i) { for (ssize_t i = 0; i < device_count; ++i) {
if (!ContainsKey(devices_, platform_devices[i])) { if (!ContainsKey(devices_, platform_devices[i])) {
libusb_device_descriptor descriptor; libusb_device_descriptor descriptor;
const int rv =
libusb_get_device_descriptor(platform_devices[i], &descriptor);
// This test is needed. A valid vendor/produce pair is required. // This test is needed. A valid vendor/produce pair is required.
if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor)) if (rv != LIBUSB_SUCCESS) {
LOG(WARNING) << "Failed to get device descriptor: "
<< ConvertErrorToString(rv);
continue; continue;
}
UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
platform_devices[i], platform_devices[i],
descriptor.idVendor, descriptor.idVendor,
...@@ -148,8 +158,12 @@ UsbService* UsbService::GetInstance() { ...@@ -148,8 +158,12 @@ UsbService* UsbService::GetInstance() {
UsbService* instance = g_usb_service_instance.Get().get(); UsbService* instance = g_usb_service_instance.Get().get();
if (!instance) { if (!instance) {
PlatformUsbContext context = NULL; PlatformUsbContext context = NULL;
if (libusb_init(&context) != LIBUSB_SUCCESS)
const int rv = libusb_init(&context);
if (rv != LIBUSB_SUCCESS) {
LOG(ERROR) << "Failed to initialize libusb: " << ConvertErrorToString(rv);
return NULL; return NULL;
}
if (!context) if (!context)
return NULL; return NULL;
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
'src/libusb/io.c', 'src/libusb/io.c',
'src/libusb/libusb.h', 'src/libusb/libusb.h',
'src/libusb/libusbi.h', 'src/libusb/libusbi.h',
'src/libusb/strerror.c',
'src/libusb/sync.c', 'src/libusb/sync.c',
'src/libusb/version.h', 'src/libusb/version.h',
'src/libusb/version_nano.h', 'src/libusb/version_nano.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment