[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 5 | #include "device/usb/usb_service.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 6 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 7 | #include <map> |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 8 | #include <set> |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 9 | |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 13 | #include "base/stl_util.h" |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 14 | #include "device/usb/usb_context.h" |
| 15 | #include "device/usb/usb_device_impl.h" |
| 16 | #include "device/usb/usb_error.h" |
[email protected] | 62c76b5 | 2013-01-08 19:04:31 | [diff] [blame] | 17 | #include "third_party/libusb/src/libusb/libusb.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 18 | |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 19 | namespace device { |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 20 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 21 | namespace { |
| 22 | |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 23 | base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = |
| 24 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | 88a05cf4 | 2012-05-16 21:36:23 | [diff] [blame] | 25 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 26 | } // namespace |
| 27 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 28 | typedef struct libusb_device* PlatformUsbDevice; |
| 29 | typedef struct libusb_context* PlatformUsbContext; |
[email protected] | 5764dcce | 2013-09-05 20:43:39 | [diff] [blame] | 30 | |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 31 | class UsbServiceImpl : public UsbService, |
| 32 | private base::MessageLoop::DestructionObserver { |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 33 | public: |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 34 | explicit UsbServiceImpl( |
| 35 | PlatformUsbContext context, |
| 36 | scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 37 | virtual ~UsbServiceImpl(); |
[email protected] | 7ad4583b | 2014-01-16 00:10:26 | [diff] [blame] | 38 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 39 | private: |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 40 | // device::UsbService implementation |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 41 | 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_; |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 52 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 53 | scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 54 | |
| 55 | // TODO(ikarienator): Figure out a better solution. |
| 56 | uint32 next_unique_id_; |
| 57 | |
| 58 | // The map from PlatformUsbDevices to UsbDevices. |
[email protected] | 42c39c9 | 2014-05-07 14:21:31 | [diff] [blame] | 59 | typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> > DeviceMap; |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 60 | DeviceMap devices_; |
| 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl); |
| 63 | }; |
| 64 | |
| 65 | scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 66 | 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] | 96816d2 | 2013-07-26 11:33:17 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 75 | void UsbServiceImpl::GetDevices( |
| 76 | std::vector<scoped_refptr<UsbDevice> >* devices) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 77 | DCHECK(CalledOnValidThread()); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 78 | STLClearObject(devices); |
| 79 | RefreshDevices(); |
[email protected] | 6226973 | 2013-07-02 19:51:31 | [diff] [blame] | 80 | |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 81 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 82 | devices->push_back(it->second); |
[email protected] | 6226973 | 2013-07-02 19:51:31 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 86 | void UsbServiceImpl::WillDestroyCurrentMessageLoop() { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 87 | DCHECK(CalledOnValidThread()); |
| 88 | g_usb_service_instance.Get().reset(NULL); |
| 89 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 90 | |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 91 | UsbServiceImpl::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] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 97 | base::MessageLoop::current()->AddDestructionObserver(this); |
| 98 | } |
| 99 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 100 | UsbServiceImpl::~UsbServiceImpl() { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 101 | base::MessageLoop::current()->RemoveDestructionObserver(this); |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 102 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 103 | it->second->OnDisconnect(); |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 104 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 107 | void UsbServiceImpl::RefreshDevices() { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 108 | DCHECK(CalledOnValidThread()); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 109 | |
| 110 | libusb_device** platform_devices = NULL; |
| 111 | const ssize_t device_count = |
| 112 | libusb_get_device_list(context_->context(), &platform_devices); |
[email protected] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 113 | if (device_count < 0) { |
[email protected] | 7ce1c93 | 2014-06-26 06:48:44 | [diff] [blame] | 114 | VLOG(1) << "Failed to get device list: " |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 115 | << ConvertPlatformUsbErrorToString(device_count); |
[email protected] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 116 | } |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 117 | |
| 118 | std::set<UsbDevice*> connected_devices; |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 119 | std::vector<PlatformUsbDevice> disconnected_devices; |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 120 | |
| 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] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 125 | const int rv = |
| 126 | libusb_get_device_descriptor(platform_devices[i], &descriptor); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 127 | // This test is needed. A valid vendor/produce pair is required. |
[email protected] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 128 | if (rv != LIBUSB_SUCCESS) { |
[email protected] | 7ce1c93 | 2014-06-26 06:48:44 | [diff] [blame] | 129 | VLOG(1) << "Failed to get device descriptor: " |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 130 | << ConvertPlatformUsbErrorToString(rv); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 131 | continue; |
[email protected] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 132 | } |
[email protected] | 42c39c9 | 2014-05-07 14:21:31 | [diff] [blame] | 133 | UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 134 | ui_task_runner_, |
[email protected] | 42c39c9 | 2014-05-07 14:21:31 | [diff] [blame] | 135 | platform_devices[i], |
| 136 | descriptor.idVendor, |
| 137 | descriptor.idProduct, |
| 138 | ++next_unique_id_); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 139 | 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] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 144 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 145 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 146 | // 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] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 153 | // 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] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 161 | } |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 162 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 163 | // static |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 164 | UsbService* UsbService::GetInstance( |
| 165 | scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 166 | UsbService* instance = g_usb_service_instance.Get().get(); |
| 167 | if (!instance) { |
| 168 | PlatformUsbContext context = NULL; |
[email protected] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 169 | |
| 170 | const int rv = libusb_init(&context); |
| 171 | if (rv != LIBUSB_SUCCESS) { |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 172 | VLOG(1) << "Failed to initialize libusb: " |
| 173 | << ConvertPlatformUsbErrorToString(rv); |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 174 | return NULL; |
[email protected] | 3782a82 | 2014-06-20 18:13:59 | [diff] [blame] | 175 | } |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 176 | if (!context) |
| 177 | return NULL; |
| 178 | |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 179 | instance = new UsbServiceImpl(context, ui_task_runner); |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 180 | g_usb_service_instance.Get().reset(instance); |
| 181 | } |
| 182 | return instance; |
| 183 | } |
| 184 | |
| 185 | // static |
| 186 | void UsbService::SetInstanceForTest(UsbService* instance) { |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 187 | g_usb_service_instance.Get().reset(instance); |
| 188 | } |
| 189 | |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame^] | 190 | } // namespace device |