blob: b00edb3fdd5f0f605c9d408ff81e9653a4d0f843 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <windows.h>
30#include <winerror.h>
31#include <errno.h>
32#include <usb100.h>
33#include <adb_api.h>
34#include <stdio.h>
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080035#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
David Pursell0b156632015-10-30 11:22:01 -070037#include <memory>
38#include <string>
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "usb.h"
41
42//#define TRACE_USB 1
43#if TRACE_USB
44#define DBG(x...) fprintf(stderr, x)
45#else
46#define DBG(x...)
47#endif
48
David Krause913eb8b2011-03-08 14:10:16 +080049#define MAX_USBFS_BULK_SIZE (1024 * 1024)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51/** Structure usb_handle describes our connection to the usb device via
52 AdbWinApi.dll. This structure is returned from usb_open() routine and
53 is expected in each subsequent call that is accessing the device.
54*/
55struct usb_handle {
56 /// Handle to USB interface
57 ADBAPIHANDLE adb_interface;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080058
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 /// Handle to USB read pipe (endpoint)
60 ADBAPIHANDLE adb_read_pipe;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080061
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 /// Handle to USB write pipe (endpoint)
63 ADBAPIHANDLE adb_write_pipe;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080064
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065 /// Interface name
David Pursell0b156632015-10-30 11:22:01 -070066 std::string interface_name;
67};
68
Aaron Wisneracf78d42018-07-26 10:56:09 -050069class WindowsUsbTransport : public UsbTransport {
David Pursell0b156632015-10-30 11:22:01 -070070 public:
71 WindowsUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
David Anderson03de6452018-09-04 14:32:54 -070072 ~WindowsUsbTransport() override;
David Pursell0b156632015-10-30 11:22:01 -070073
74 ssize_t Read(void* data, size_t len) override;
75 ssize_t Write(const void* data, size_t len) override;
76 int Close() override;
Aaron Wisneracf78d42018-07-26 10:56:09 -050077 int Reset() override;
David Pursell0b156632015-10-30 11:22:01 -070078
79 private:
80 std::unique_ptr<usb_handle> handle_;
81
82 DISALLOW_COPY_AND_ASSIGN(WindowsUsbTransport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083};
84
85/// Class ID assigned to the device by androidusb.sys
86static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
87
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088/// Checks if interface (device) matches certain criteria
89int recognized_device(usb_handle* handle, ifc_match_func callback);
90
91/// Opens usb interface (device) by interface (device) name.
David Pursell0b156632015-10-30 11:22:01 -070092std::unique_ptr<usb_handle> do_usb_open(const wchar_t* interface_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093
94/// Cleans up opened usb handle
95void usb_cleanup_handle(usb_handle* handle);
96
97/// Cleans up (but don't close) opened usb handle
98void usb_kick(usb_handle* handle);
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100
David Pursell0b156632015-10-30 11:22:01 -0700101std::unique_ptr<usb_handle> do_usb_open(const wchar_t* interface_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 // Allocate our handle
David Pursell0b156632015-10-30 11:22:01 -0700103 std::unique_ptr<usb_handle> ret(new usb_handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
105 // Create interface.
106 ret->adb_interface = AdbCreateInterfaceByName(interface_name);
107
David Pursell0b156632015-10-30 11:22:01 -0700108 if (nullptr == ret->adb_interface) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 errno = GetLastError();
Josh Gaof22e9532018-05-31 21:24:14 -0700110 DBG("failed to open interface %S\n", interface_name);
David Pursell0b156632015-10-30 11:22:01 -0700111 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 }
113
114 // Open read pipe (endpoint)
115 ret->adb_read_pipe =
116 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
117 AdbOpenAccessTypeReadWrite,
118 AdbOpenSharingModeReadWrite);
David Pursell0b156632015-10-30 11:22:01 -0700119 if (nullptr != ret->adb_read_pipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 // Open write pipe (endpoint)
121 ret->adb_write_pipe =
122 AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
123 AdbOpenAccessTypeReadWrite,
124 AdbOpenSharingModeReadWrite);
David Pursell0b156632015-10-30 11:22:01 -0700125 if (nullptr != ret->adb_write_pipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 // Save interface name
127 unsigned long name_len = 0;
128
129 // First get expected name length
130 AdbGetInterfaceName(ret->adb_interface,
David Pursell0b156632015-10-30 11:22:01 -0700131 nullptr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 &name_len,
133 true);
134 if (0 != name_len) {
David Pursell0b156632015-10-30 11:22:01 -0700135 // Now save the name
136 ret->interface_name.resize(name_len);
137 if (AdbGetInterfaceName(ret->adb_interface,
138 &ret->interface_name[0],
139 &name_len,
140 true)) {
141 // We're done at this point
142 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
144 }
145 }
146 }
147
148 // Something went wrong.
149 errno = GetLastError();
David Pursell0b156632015-10-30 11:22:01 -0700150 usb_cleanup_handle(ret.get());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 SetLastError(errno);
152
David Pursell0b156632015-10-30 11:22:01 -0700153 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
David Pursell0b156632015-10-30 11:22:01 -0700156ssize_t WindowsUsbTransport::Write(const void* data, size_t len) {
Jiebing Libbb79812012-08-07 14:29:21 +0800157 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 unsigned long written = 0;
159 unsigned count = 0;
160 int ret;
161
Josh Gaof22e9532018-05-31 21:24:14 -0700162 DBG("usb_write %zu\n", len);
David Pursell0b156632015-10-30 11:22:01 -0700163 if (nullptr != handle_) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 // Perform write
165 while(len > 0) {
David Krause913eb8b2011-03-08 14:10:16 +0800166 int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
David Pursell0b156632015-10-30 11:22:01 -0700167 ret = AdbWriteEndpointSync(handle_->adb_write_pipe, const_cast<void*>(data), xfer,
168 &written, time_out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 errno = GetLastError();
170 DBG("AdbWriteEndpointSync returned %d, errno: %d\n", ret, errno);
171 if (ret == 0) {
172 // assume ERROR_INVALID_HANDLE indicates we are disconnected
173 if (errno == ERROR_INVALID_HANDLE)
David Pursell0b156632015-10-30 11:22:01 -0700174 usb_kick(handle_.get());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 return -1;
176 }
177
178 count += written;
179 len -= written;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700180 data = (const char *)data + written;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
182 if (len == 0)
183 return count;
184 }
185 } else {
186 DBG("usb_write NULL handle\n");
187 SetLastError(ERROR_INVALID_HANDLE);
188 }
189
190 DBG("usb_write failed: %d\n", errno);
191
192 return -1;
193}
194
David Pursell0b156632015-10-30 11:22:01 -0700195ssize_t WindowsUsbTransport::Read(void* data, size_t len) {
Jiebing Libbb79812012-08-07 14:29:21 +0800196 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 unsigned long read = 0;
198 int ret;
199
Josh Gaof22e9532018-05-31 21:24:14 -0700200 DBG("usb_read %zu\n", len);
David Pursell0b156632015-10-30 11:22:01 -0700201 if (nullptr != handle_) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 while (1) {
David Krause913eb8b2011-03-08 14:10:16 +0800203 int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
David Pursell0b156632015-10-30 11:22:01 -0700205 ret = AdbReadEndpointSync(handle_->adb_read_pipe, data, xfer, &read, time_out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 errno = GetLastError();
207 DBG("usb_read got: %ld, expected: %d, errno: %d\n", read, xfer, errno);
208 if (ret) {
209 return read;
Jiebing Libbb79812012-08-07 14:29:21 +0800210 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 // assume ERROR_INVALID_HANDLE indicates we are disconnected
212 if (errno == ERROR_INVALID_HANDLE)
David Pursell0b156632015-10-30 11:22:01 -0700213 usb_kick(handle_.get());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 break;
215 }
216 // else we timed out - try again
217 }
218 } else {
219 DBG("usb_read NULL handle\n");
220 SetLastError(ERROR_INVALID_HANDLE);
221 }
222
223 DBG("usb_read failed: %d\n", errno);
224
225 return -1;
226}
227
228void usb_cleanup_handle(usb_handle* handle) {
229 if (NULL != handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 if (NULL != handle->adb_write_pipe)
231 AdbCloseHandle(handle->adb_write_pipe);
232 if (NULL != handle->adb_read_pipe)
233 AdbCloseHandle(handle->adb_read_pipe);
234 if (NULL != handle->adb_interface)
235 AdbCloseHandle(handle->adb_interface);
236
David Pursell0b156632015-10-30 11:22:01 -0700237 handle->interface_name.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 handle->adb_write_pipe = NULL;
239 handle->adb_read_pipe = NULL;
240 handle->adb_interface = NULL;
241 }
242}
243
244void usb_kick(usb_handle* handle) {
245 if (NULL != handle) {
246 usb_cleanup_handle(handle);
247 } else {
248 SetLastError(ERROR_INVALID_HANDLE);
249 errno = ERROR_INVALID_HANDLE;
250 }
251}
252
David Anderson03de6452018-09-04 14:32:54 -0700253WindowsUsbTransport::~WindowsUsbTransport() {
254 Close();
255}
256
David Pursell0b156632015-10-30 11:22:01 -0700257int WindowsUsbTransport::Close() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 DBG("usb_close\n");
259
David Pursell0b156632015-10-30 11:22:01 -0700260 if (nullptr != handle_) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 // Cleanup handle
David Pursell0b156632015-10-30 11:22:01 -0700262 usb_cleanup_handle(handle_.get());
263 handle_.reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 }
265
266 return 0;
267}
268
Aaron Wisneracf78d42018-07-26 10:56:09 -0500269int WindowsUsbTransport::Reset() {
270 DBG("usb_reset currently unsupported\n\n");
271 // TODO, this is a bit complicated since it is using ADB
272 return -1;
273}
274
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275int recognized_device(usb_handle* handle, ifc_match_func callback) {
276 struct usb_ifc_info info;
277 USB_DEVICE_DESCRIPTOR device_desc;
278 USB_INTERFACE_DESCRIPTOR interf_desc;
279
280 if (NULL == handle)
281 return 0;
282
283 // Check vendor and product id first
Josh Gaof22e9532018-05-31 21:24:14 -0700284 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) {
285 DBG("skipping device %x:%x\n", device_desc.idVendor, device_desc.idProduct);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 return 0;
287 }
288
289 // Then check interface properties
Josh Gaof22e9532018-05-31 21:24:14 -0700290 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) {
291 DBG("skipping device %x:%x, failed to find interface\n", device_desc.idVendor,
292 device_desc.idProduct);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 return 0;
294 }
295
296 // Must have two endpoints
297 if (2 != interf_desc.bNumEndpoints) {
Josh Gaof22e9532018-05-31 21:24:14 -0700298 DBG("skipping device %x:%x, incorrect number of endpoints\n", device_desc.idVendor,
299 device_desc.idProduct);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 return 0;
301 }
302
303 info.dev_vendor = device_desc.idVendor;
304 info.dev_product = device_desc.idProduct;
305 info.dev_class = device_desc.bDeviceClass;
306 info.dev_subclass = device_desc.bDeviceSubClass;
307 info.dev_protocol = device_desc.bDeviceProtocol;
308 info.ifc_class = interf_desc.bInterfaceClass;
309 info.ifc_subclass = interf_desc.bInterfaceSubClass;
310 info.ifc_protocol = interf_desc.bInterfaceProtocol;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700311 info.writable = 1;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800312
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 // read serial number (if there is one)
314 unsigned long serial_number_len = sizeof(info.serial_number);
315 if (!AdbGetSerialNumber(handle->adb_interface, info.serial_number,
316 &serial_number_len, true)) {
317 info.serial_number[0] = 0;
318 }
319
Scott Anderson13081c62012-04-06 12:39:30 -0700320 info.device_path[0] = 0;
321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 if (callback(&info) == 0) {
Josh Gaof22e9532018-05-31 21:24:14 -0700323 DBG("skipping device %x:%x, not selected by callback\n", device_desc.idVendor,
324 device_desc.idProduct);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 return 1;
326 }
327
Josh Gaof22e9532018-05-31 21:24:14 -0700328 DBG("found device %x:%x (%s)\n", device_desc.idVendor, device_desc.idProduct,
329 info.serial_number);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 return 0;
331}
332
David Pursell0b156632015-10-30 11:22:01 -0700333static std::unique_ptr<usb_handle> find_usb_device(ifc_match_func callback) {
334 std::unique_ptr<usb_handle> handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 char entry_buffer[2048];
336 char interf_name[2048];
337 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
338 unsigned long entry_buffer_size = sizeof(entry_buffer);
339 char* copy_name;
340
341 // Enumerate all present and active interfaces.
342 ADBAPIHANDLE enum_handle =
343 AdbEnumInterfaces(usb_class_id, true, true, true);
344
345 if (NULL == enum_handle)
346 return NULL;
347
348 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
349 // TODO(vchtchetkine): FIXME - temp hack converting wchar_t into char.
350 // It would be better to change AdbNextInterface so it will return
351 // interface name as single char string.
352 const wchar_t* wchar_name = next_interface->device_name;
353 for(copy_name = interf_name;
354 L'\0' != *wchar_name;
355 wchar_name++, copy_name++) {
356 *copy_name = (char)(*wchar_name);
357 }
358 *copy_name = '\0';
359
Josh Gaof22e9532018-05-31 21:24:14 -0700360 DBG("attempting to open interface %S\n", next_interface->device_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 handle = do_usb_open(next_interface->device_name);
362 if (NULL != handle) {
363 // Lets see if this interface (device) belongs to us
David Pursell0b156632015-10-30 11:22:01 -0700364 if (recognized_device(handle.get(), callback)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 // found it!
366 break;
367 } else {
David Pursell0b156632015-10-30 11:22:01 -0700368 usb_cleanup_handle(handle.get());
369 handle.reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 }
371 }
372
373 entry_buffer_size = sizeof(entry_buffer);
374 }
375
376 AdbCloseHandle(enum_handle);
377 return handle;
378}
379
Aaron Wisneracf78d42018-07-26 10:56:09 -0500380UsbTransport* usb_open(ifc_match_func callback, uint32_t) {
David Pursell0b156632015-10-30 11:22:01 -0700381 std::unique_ptr<usb_handle> handle = find_usb_device(callback);
382 return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383}