blob: 78846d39e76898bc55a46ea346f4a47790e7fd2f [file] [log] [blame]
[email protected]b5508682014-04-22 17:10:011// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]1c39a6b2012-04-27 16:09:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
reillygd77718d2014-09-04 00:57:565#include "device/usb/usb_service.h"
[email protected]1c39a6b2012-04-27 16:09:576
[email protected]ad2263f2014-05-05 13:57:507#include <map>
[email protected]d165a6442013-08-08 19:38:118#include <set>
[email protected]22ac24e2012-10-03 17:56:059
[email protected]e1954872014-04-13 17:43:2910#include "base/lazy_instance.h"
[email protected]ad2263f2014-05-05 13:57:5011#include "base/message_loop/message_loop.h"
reillyge471fab2014-08-29 01:58:4312#include "base/single_thread_task_runner.h"
[email protected]1c39a6b2012-04-27 16:09:5713#include "base/stl_util.h"
reillygd77718d2014-09-04 00:57:5614#include "device/usb/usb_context.h"
15#include "device/usb/usb_device_impl.h"
16#include "device/usb/usb_error.h"
[email protected]62c76b52013-01-08 19:04:3117#include "third_party/libusb/src/libusb/libusb.h"
[email protected]1c39a6b2012-04-27 16:09:5718
reillygd77718d2014-09-04 00:57:5619namespace device {
[email protected]b5508682014-04-22 17:10:0120
[email protected]99dcfca2013-08-05 01:28:5921namespace {
22
[email protected]e1954872014-04-13 17:43:2923base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
24 LAZY_INSTANCE_INITIALIZER;
[email protected]88a05cf42012-05-16 21:36:2325
[email protected]99dcfca2013-08-05 01:28:5926} // namespace
27
[email protected]ad2263f2014-05-05 13:57:5028typedef struct libusb_device* PlatformUsbDevice;
29typedef struct libusb_context* PlatformUsbContext;
[email protected]5764dcce2013-09-05 20:43:3930
reillygd77718d2014-09-04 00:57:5631class UsbServiceImpl : public UsbService,
32 private base::MessageLoop::DestructionObserver {
[email protected]ad2263f2014-05-05 13:57:5033 public:
reillyge471fab2014-08-29 01:58:4334 explicit UsbServiceImpl(
35 PlatformUsbContext context,
36 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
[email protected]ad2263f2014-05-05 13:57:5037 virtual ~UsbServiceImpl();
[email protected]7ad4583b2014-01-16 00:10:2638
[email protected]ad2263f2014-05-05 13:57:5039 private:
reillygd77718d2014-09-04 00:57:5640 // device::UsbService implementation
[email protected]ad2263f2014-05-05 13:57:5041 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE;
42 virtual void GetDevices(
43 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE;
44
45 // base::MessageLoop::DestructionObserver implementation.
46 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
47
48 // Enumerate USB devices from OS and Update devices_ map.
49 void RefreshDevices();
50
51 scoped_refptr<UsbContext> context_;
reillyge471fab2014-08-29 01:58:4352 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
53 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
[email protected]ad2263f2014-05-05 13:57:5054
55 // TODO(ikarienator): Figure out a better solution.
56 uint32 next_unique_id_;
57
58 // The map from PlatformUsbDevices to UsbDevices.
[email protected]42c39c92014-05-07 14:21:3159 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> > DeviceMap;
[email protected]ad2263f2014-05-05 13:57:5060 DeviceMap devices_;
61
62 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
63};
64
65scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) {
[email protected]e1954872014-04-13 17:43:2966 DCHECK(CalledOnValidThread());
67 RefreshDevices();
68 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
69 if (it->second->unique_id() == unique_id)
70 return it->second;
71 }
72 return NULL;
[email protected]96816d22013-07-26 11:33:1773}
74
[email protected]ad2263f2014-05-05 13:57:5075void UsbServiceImpl::GetDevices(
76 std::vector<scoped_refptr<UsbDevice> >* devices) {
[email protected]e1954872014-04-13 17:43:2977 DCHECK(CalledOnValidThread());
[email protected]d165a6442013-08-08 19:38:1178 STLClearObject(devices);
79 RefreshDevices();
[email protected]62269732013-07-02 19:51:3180
[email protected]320a7b72013-09-05 01:51:2781 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
[email protected]d165a6442013-08-08 19:38:1182 devices->push_back(it->second);
[email protected]62269732013-07-02 19:51:3183 }
84}
85
[email protected]ad2263f2014-05-05 13:57:5086void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
[email protected]e1954872014-04-13 17:43:2987 DCHECK(CalledOnValidThread());
88 g_usb_service_instance.Get().reset(NULL);
89}
[email protected]22ac24e2012-10-03 17:56:0590
reillyge471fab2014-08-29 01:58:4391UsbServiceImpl::UsbServiceImpl(
92 PlatformUsbContext context,
93 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
94 : context_(new UsbContext(context)),
95 ui_task_runner_(ui_task_runner),
96 next_unique_id_(0) {
[email protected]e1954872014-04-13 17:43:2997 base::MessageLoop::current()->AddDestructionObserver(this);
98}
99
[email protected]ad2263f2014-05-05 13:57:50100UsbServiceImpl::~UsbServiceImpl() {
[email protected]e1954872014-04-13 17:43:29101 base::MessageLoop::current()->RemoveDestructionObserver(this);
[email protected]320a7b72013-09-05 01:51:27102 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
[email protected]e1954872014-04-13 17:43:29103 it->second->OnDisconnect();
[email protected]22ac24e2012-10-03 17:56:05104 }
[email protected]22ac24e2012-10-03 17:56:05105}
106
[email protected]ad2263f2014-05-05 13:57:50107void UsbServiceImpl::RefreshDevices() {
[email protected]e1954872014-04-13 17:43:29108 DCHECK(CalledOnValidThread());
[email protected]d165a6442013-08-08 19:38:11109
110 libusb_device** platform_devices = NULL;
111 const ssize_t device_count =
112 libusb_get_device_list(context_->context(), &platform_devices);
[email protected]3782a822014-06-20 18:13:59113 if (device_count < 0) {
[email protected]7ce1c932014-06-26 06:48:44114 VLOG(1) << "Failed to get device list: "
reillygd77718d2014-09-04 00:57:56115 << ConvertPlatformUsbErrorToString(device_count);
[email protected]3782a822014-06-20 18:13:59116 }
[email protected]d165a6442013-08-08 19:38:11117
118 std::set<UsbDevice*> connected_devices;
[email protected]e1954872014-04-13 17:43:29119 std::vector<PlatformUsbDevice> disconnected_devices;
[email protected]d165a6442013-08-08 19:38:11120
121 // Populates new devices.
122 for (ssize_t i = 0; i < device_count; ++i) {
123 if (!ContainsKey(devices_, platform_devices[i])) {
124 libusb_device_descriptor descriptor;
[email protected]3782a822014-06-20 18:13:59125 const int rv =
126 libusb_get_device_descriptor(platform_devices[i], &descriptor);
[email protected]d165a6442013-08-08 19:38:11127 // This test is needed. A valid vendor/produce pair is required.
[email protected]3782a822014-06-20 18:13:59128 if (rv != LIBUSB_SUCCESS) {
[email protected]7ce1c932014-06-26 06:48:44129 VLOG(1) << "Failed to get device descriptor: "
reillygd77718d2014-09-04 00:57:56130 << ConvertPlatformUsbErrorToString(rv);
[email protected]d165a6442013-08-08 19:38:11131 continue;
[email protected]3782a822014-06-20 18:13:59132 }
[email protected]42c39c92014-05-07 14:21:31133 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
reillyge471fab2014-08-29 01:58:43134 ui_task_runner_,
[email protected]42c39c92014-05-07 14:21:31135 platform_devices[i],
136 descriptor.idVendor,
137 descriptor.idProduct,
138 ++next_unique_id_);
[email protected]d165a6442013-08-08 19:38:11139 devices_[platform_devices[i]] = new_device;
140 connected_devices.insert(new_device);
141 } else {
142 connected_devices.insert(devices_[platform_devices[i]].get());
143 }
[email protected]1c39a6b2012-04-27 16:09:57144 }
[email protected]22ac24e2012-10-03 17:56:05145
[email protected]d165a6442013-08-08 19:38:11146 // Find disconnected devices.
147 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
148 if (!ContainsKey(connected_devices, it->second)) {
149 disconnected_devices.push_back(it->first);
150 }
[email protected]22ac24e2012-10-03 17:56:05151 }
152
[email protected]d165a6442013-08-08 19:38:11153 // Remove disconnected devices from devices_.
154 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
155 // UsbDevice will be destroyed after this. The corresponding
156 // PlatformUsbDevice will be unref'ed during this process.
157 devices_.erase(disconnected_devices[i]);
158 }
159
160 libusb_free_device_list(platform_devices, true);
[email protected]22ac24e2012-10-03 17:56:05161}
[email protected]b5508682014-04-22 17:10:01162
[email protected]ad2263f2014-05-05 13:57:50163// static
reillyge471fab2014-08-29 01:58:43164UsbService* UsbService::GetInstance(
165 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
[email protected]ad2263f2014-05-05 13:57:50166 UsbService* instance = g_usb_service_instance.Get().get();
167 if (!instance) {
168 PlatformUsbContext context = NULL;
[email protected]3782a822014-06-20 18:13:59169
170 const int rv = libusb_init(&context);
171 if (rv != LIBUSB_SUCCESS) {
reillygd77718d2014-09-04 00:57:56172 VLOG(1) << "Failed to initialize libusb: "
173 << ConvertPlatformUsbErrorToString(rv);
[email protected]ad2263f2014-05-05 13:57:50174 return NULL;
[email protected]3782a822014-06-20 18:13:59175 }
[email protected]ad2263f2014-05-05 13:57:50176 if (!context)
177 return NULL;
178
reillyge471fab2014-08-29 01:58:43179 instance = new UsbServiceImpl(context, ui_task_runner);
[email protected]ad2263f2014-05-05 13:57:50180 g_usb_service_instance.Get().reset(instance);
181 }
182 return instance;
183}
184
185// static
186void UsbService::SetInstanceForTest(UsbService* instance) {
[email protected]ad2263f2014-05-05 13:57:50187 g_usb_service_instance.Get().reset(instance);
188}
189
reillygd77718d2014-09-04 00:57:56190} // namespace device