summaryrefslogtreecommitdiffstats
path: root/components/usb_service
diff options
context:
space:
mode:
authorreillyg@chromium.org <reillyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-20 18:13:59 +0000
committerreillyg@chromium.org <reillyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-20 18:13:59 +0000
commit3782a82b5d0c8f59a660d73b1bc9aada527dd6d8 (patch)
tree2da1871fbfbccc1bbfc6afa8d6c4358a36d5f8aa /components/usb_service
parentfc3f8c208f78c6b5127d4f5ac866ef1a46088c89 (diff)
downloadchromium_src-3782a82b5d0c8f59a660d73b1bc9aada527dd6d8.zip
chromium_src-3782a82b5d0c8f59a660d73b1bc9aada527dd6d8.tar.gz
chromium_src-3782a82b5d0c8f59a660d73b1bc9aada527dd6d8.tar.bz2
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
Diffstat (limited to 'components/usb_service')
-rw-r--r--components/usb_service/usb_context.cc10
-rw-r--r--components/usb_service/usb_device_handle_impl.cc41
-rw-r--r--components/usb_service/usb_device_impl.cc18
-rw-r--r--components/usb_service/usb_error.cc15
-rw-r--r--components/usb_service/usb_error.h18
-rw-r--r--components/usb_service/usb_service_impl.cc18
6 files changed, 101 insertions, 19 deletions
diff --git a/components/usb_service/usb_context.cc b/components/usb_service/usb_context.cc
index be8313e..67403c8 100644
--- a/components/usb_service/usb_context.cc
+++ b/components/usb_service/usb_context.cc
@@ -7,6 +7,7 @@
#include "base/logging.h"
#include "base/synchronization/waitable_event.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/libusb.h"
@@ -54,10 +55,13 @@ void UsbContext::UsbEventHandler::ThreadMain() {
VLOG(1) << "UsbEventHandler started.";
if (running_) {
start_polling_.Signal();
- libusb_handle_events(context_);
}
- while (running_)
- libusb_handle_events(context_);
+ while (running_) {
+ const int rv = libusb_handle_events(context_);
+ if (rv != LIBUSB_SUCCESS) {
+ LOG(WARNING) << "Failed to handle events: " << ConvertErrorToString(rv);
+ }
+ }
VLOG(1) << "UsbEventHandler shutting down.";
}
diff --git a/components/usb_service/usb_device_handle_impl.cc b/components/usb_service/usb_device_handle_impl.cc
index e3b8987..8b8382d 100644
--- a/components/usb_service/usb_device_handle_impl.cc
+++ b/components/usb_service/usb_device_handle_impl.cc
@@ -13,6 +13,7 @@
#include "base/synchronization/lock.h"
#include "components/usb_service/usb_context.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_service.h"
#include "content/public/browser/browser_thread.h"
@@ -158,7 +159,11 @@ UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() {
}
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 {
@@ -344,13 +349,15 @@ bool UsbDeviceHandleImpl::SetInterfaceAlternateSetting(
return false;
const int rv = libusb_set_interface_alt_setting(
handle_, interface_number, alternate_setting);
- if (rv == 0) {
+ if (rv == LIBUSB_SUCCESS) {
claimed_interfaces_[interface_number]->set_alternate_setting(
alternate_setting);
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() {
@@ -358,7 +365,11 @@ bool UsbDeviceHandleImpl::ResetDevice() {
if (!device_)
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) {
@@ -366,8 +377,12 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
PlatformUsbDevice device = libusb_get_device(handle_);
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;
+ }
if (desc.iSerialNumber == 0)
return false;
@@ -381,8 +396,11 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
0,
reinterpret_cast<unsigned char*>(&langid[0]),
sizeof(langid));
- if (size < 0)
+ if (size < 0) {
+ LOG(ERROR) << "Failed to get language IDs: "
+ << ConvertErrorToString(size);
return false;
+ }
int language_count = (size - 2) / 2;
@@ -395,6 +413,11 @@ bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
langid[i],
reinterpret_cast<unsigned char*>(&text[0]),
sizeof(text));
+ if (size < 0) {
+ LOG(ERROR) << "Failed to get serial number (langid " << langid[i] << "): "
+ << ConvertErrorToString(size);
+ continue;
+ }
if (size <= 2)
continue;
if ((text[0] >> 8) != LIBUSB_DT_STRING)
@@ -626,9 +649,11 @@ void UsbDeviceHandleImpl::SubmitTransfer(
// it requires an interface we didn't claim.
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;
} else {
+ LOG(ERROR) << "Failed to submit transfer: " << ConvertErrorToString(rv);
message_loop_proxy->PostTask(
FROM_HERE,
base::Bind(
diff --git a/components/usb_service/usb_device_impl.cc b/components/usb_service/usb_device_impl.cc
index 43c8c66..bbae9c8 100644
--- a/components/usb_service/usb_device_impl.cc
+++ b/components/usb_service/usb_device_impl.cc
@@ -9,6 +9,7 @@
#include "base/stl_util.h"
#include "components/usb_service/usb_context.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 "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h"
@@ -93,7 +94,7 @@ void UsbDeviceImpl::RequestUsbAcess(
scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
DCHECK(thread_checker_.CalledOnValidThread());
PlatformUsbDeviceHandle handle;
- int rv = libusb_open(platform_device_, &handle);
+ const int rv = libusb_open(platform_device_, &handle);
if (LIBUSB_SUCCESS == rv) {
scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces();
if (!interfaces)
@@ -102,8 +103,10 @@ scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
new UsbDeviceHandleImpl(context_, this, handle, interfaces);
handles_.push_back(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) {
@@ -124,12 +127,15 @@ scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() {
DCHECK(thread_checker_.CalledOnValidThread());
PlatformUsbConfigDescriptor platform_config;
- const int list_result =
+ const int rv =
libusb_get_active_config_descriptor(platform_device_, &platform_config);
- if (list_result == 0)
+ if (rv == LIBUSB_SUCCESS) {
return new UsbConfigDescriptorImpl(platform_config);
-
- return NULL;
+ } else {
+ LOG(ERROR) << "Failed to get config descriptor: "
+ << ConvertErrorToString(rv);
+ return NULL;
+ }
}
void UsbDeviceImpl::OnDisconnect() {
diff --git a/components/usb_service/usb_error.cc b/components/usb_service/usb_error.cc
new file mode 100644
index 0000000..d3e9960
--- /dev/null
+++ b/components/usb_service/usb_error.cc
@@ -0,0 +1,15 @@
+// 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;
diff --git a/components/usb_service/usb_error.h b/components/usb_service/usb_error.h
new file mode 100644
index 0000000..b718d67
--- /dev/null
+++ b/components/usb_service/usb_error.h
@@ -0,0 +1,18 @@
+// 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_
diff --git a/components/usb_service/usb_service_impl.cc b/components/usb_service/usb_service_impl.cc
index bafb51b..4a3e88a 100644
--- a/components/usb_service/usb_service_impl.cc
+++ b/components/usb_service/usb_service_impl.cc
@@ -12,6 +12,7 @@
#include "base/stl_util.h"
#include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_impl.h"
+#include "components/usb_service/usb_error.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h"
@@ -102,6 +103,10 @@ void UsbServiceImpl::RefreshDevices() {
libusb_device** platform_devices = NULL;
const ssize_t device_count =
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::vector<PlatformUsbDevice> disconnected_devices;
@@ -110,9 +115,14 @@ void UsbServiceImpl::RefreshDevices() {
for (ssize_t i = 0; i < device_count; ++i) {
if (!ContainsKey(devices_, platform_devices[i])) {
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.
- if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor))
+ if (rv != LIBUSB_SUCCESS) {
+ LOG(WARNING) << "Failed to get device descriptor: "
+ << ConvertErrorToString(rv);
continue;
+ }
UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
platform_devices[i],
descriptor.idVendor,
@@ -148,8 +158,12 @@ UsbService* UsbService::GetInstance() {
UsbService* instance = g_usb_service_instance.Get().get();
if (!instance) {
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;
+ }
if (!context)
return NULL;