| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 12 | *    the documentation and/or other materials provided with the | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | *    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 Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * 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 Ferris | 67a7a4a | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 35 | #include <stdlib.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 37 | #include <memory> | 
|  | 38 | #include <string> | 
|  | 39 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | #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 Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 49 | #define MAX_USBFS_BULK_SIZE (1024 * 1024) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 |  | 
|  | 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 | */ | 
|  | 55 | struct usb_handle { | 
|  | 56 | /// Handle to USB interface | 
|  | 57 | ADBAPIHANDLE  adb_interface; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 58 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | /// Handle to USB read pipe (endpoint) | 
|  | 60 | ADBAPIHANDLE  adb_read_pipe; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 61 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | /// Handle to USB write pipe (endpoint) | 
|  | 63 | ADBAPIHANDLE  adb_write_pipe; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 64 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | /// Interface name | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 66 | std::string interface_name; | 
|  | 67 | }; | 
|  | 68 |  | 
| Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 69 | class WindowsUsbTransport : public UsbTransport { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 70 | public: | 
|  | 71 | WindowsUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {} | 
| David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 72 | ~WindowsUsbTransport() override; | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 73 |  | 
|  | 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 Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 77 | int Reset() override; | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | private: | 
|  | 80 | std::unique_ptr<usb_handle> handle_; | 
|  | 81 |  | 
|  | 82 | DISALLOW_COPY_AND_ASSIGN(WindowsUsbTransport); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | }; | 
|  | 84 |  | 
|  | 85 | /// Class ID assigned to the device by androidusb.sys | 
|  | 86 | static const GUID usb_class_id = ANDROID_USB_CLASS_ID; | 
|  | 87 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | /// Checks if interface (device) matches certain criteria | 
|  | 89 | int recognized_device(usb_handle* handle, ifc_match_func callback); | 
|  | 90 |  | 
|  | 91 | /// Opens usb interface (device) by interface (device) name. | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 92 | std::unique_ptr<usb_handle> do_usb_open(const wchar_t* interface_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | /// Cleans up opened usb handle | 
|  | 95 | void usb_cleanup_handle(usb_handle* handle); | 
|  | 96 |  | 
|  | 97 | /// Cleans up (but don't close) opened usb handle | 
|  | 98 | void usb_kick(usb_handle* handle); | 
|  | 99 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 101 | std::unique_ptr<usb_handle> do_usb_open(const wchar_t* interface_name) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | // Allocate our handle | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 103 | std::unique_ptr<usb_handle> ret(new usb_handle); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 |  | 
|  | 105 | // Create interface. | 
|  | 106 | ret->adb_interface = AdbCreateInterfaceByName(interface_name); | 
|  | 107 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 108 | if (nullptr == ret->adb_interface) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | errno = GetLastError(); | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 110 | DBG("failed to open interface %S\n", interface_name); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 111 | return nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
|  | 114 | // Open read pipe (endpoint) | 
|  | 115 | ret->adb_read_pipe = | 
|  | 116 | AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, | 
|  | 117 | AdbOpenAccessTypeReadWrite, | 
|  | 118 | AdbOpenSharingModeReadWrite); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 119 | if (nullptr != ret->adb_read_pipe) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | // Open write pipe (endpoint) | 
|  | 121 | ret->adb_write_pipe = | 
|  | 122 | AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, | 
|  | 123 | AdbOpenAccessTypeReadWrite, | 
|  | 124 | AdbOpenSharingModeReadWrite); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 125 | if (nullptr != ret->adb_write_pipe) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | // Save interface name | 
|  | 127 | unsigned long name_len = 0; | 
|  | 128 |  | 
|  | 129 | // First get expected name length | 
|  | 130 | AdbGetInterfaceName(ret->adb_interface, | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 131 | nullptr, | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | &name_len, | 
|  | 133 | true); | 
|  | 134 | if (0 != name_len) { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 135 | // 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | } | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | // Something went wrong. | 
|  | 149 | errno = GetLastError(); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 150 | usb_cleanup_handle(ret.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | SetLastError(errno); | 
|  | 152 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 153 | return nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 156 | ssize_t WindowsUsbTransport::Write(const void* data, size_t len) { | 
| Jiebing Li | bbb7981 | 2012-08-07 14:29:21 +0800 | [diff] [blame] | 157 | unsigned long time_out = 5000; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | unsigned long written = 0; | 
|  | 159 | unsigned count = 0; | 
|  | 160 | int ret; | 
|  | 161 |  | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 162 | DBG("usb_write %zu\n", len); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 163 | if (nullptr != handle_) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | // Perform write | 
|  | 165 | while(len > 0) { | 
| David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 166 | int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 167 | ret = AdbWriteEndpointSync(handle_->adb_write_pipe, const_cast<void*>(data), xfer, | 
|  | 168 | &written, time_out); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | 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 Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 174 | usb_kick(handle_.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | return -1; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | count += written; | 
|  | 179 | len -= written; | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 180 | data = (const char *)data + written; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 |  | 
|  | 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 Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 195 | ssize_t WindowsUsbTransport::Read(void* data, size_t len) { | 
| Jiebing Li | bbb7981 | 2012-08-07 14:29:21 +0800 | [diff] [blame] | 196 | unsigned long time_out = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | unsigned long read = 0; | 
| Fernando Lugo | b7eac2d | 2019-05-29 19:33:56 -0700 | [diff] [blame] | 198 | size_t count = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | int ret; | 
|  | 200 |  | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 201 | DBG("usb_read %zu\n", len); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 202 | if (nullptr != handle_) { | 
| Fernando Lugo | b7eac2d | 2019-05-29 19:33:56 -0700 | [diff] [blame] | 203 | while (len > 0) { | 
|  | 204 | size_t xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 206 | ret = AdbReadEndpointSync(handle_->adb_read_pipe, data, xfer, &read, time_out); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | errno = GetLastError(); | 
| Greg Kaiser | 8acf8c2 | 2019-06-03 14:12:42 -0700 | [diff] [blame] | 208 | DBG("usb_read got: %lu, expected: %zu, errno: %d\n", read, xfer, errno); | 
| Fernando Lugo | b7eac2d | 2019-05-29 19:33:56 -0700 | [diff] [blame] | 209 | if (ret == 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | // assume ERROR_INVALID_HANDLE indicates we are disconnected | 
|  | 211 | if (errno == ERROR_INVALID_HANDLE) | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 212 | usb_kick(handle_.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | break; | 
|  | 214 | } | 
| Fernando Lugo | b7eac2d | 2019-05-29 19:33:56 -0700 | [diff] [blame] | 215 | count += read; | 
|  | 216 | len -= read; | 
|  | 217 | data = (char*)data + read; | 
|  | 218 |  | 
|  | 219 | if (xfer != read || len == 0) return count; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | } | 
|  | 221 | } else { | 
|  | 222 | DBG("usb_read NULL handle\n"); | 
|  | 223 | SetLastError(ERROR_INVALID_HANDLE); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | DBG("usb_read failed: %d\n", errno); | 
|  | 227 |  | 
|  | 228 | return -1; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | void usb_cleanup_handle(usb_handle* handle) { | 
|  | 232 | if (NULL != handle) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 233 | if (NULL != handle->adb_write_pipe) | 
|  | 234 | AdbCloseHandle(handle->adb_write_pipe); | 
|  | 235 | if (NULL != handle->adb_read_pipe) | 
|  | 236 | AdbCloseHandle(handle->adb_read_pipe); | 
|  | 237 | if (NULL != handle->adb_interface) | 
|  | 238 | AdbCloseHandle(handle->adb_interface); | 
|  | 239 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 240 | handle->interface_name.clear(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | handle->adb_write_pipe = NULL; | 
|  | 242 | handle->adb_read_pipe = NULL; | 
|  | 243 | handle->adb_interface = NULL; | 
|  | 244 | } | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | void usb_kick(usb_handle* handle) { | 
|  | 248 | if (NULL != handle) { | 
|  | 249 | usb_cleanup_handle(handle); | 
|  | 250 | } else { | 
|  | 251 | SetLastError(ERROR_INVALID_HANDLE); | 
|  | 252 | errno = ERROR_INVALID_HANDLE; | 
|  | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
| David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 256 | WindowsUsbTransport::~WindowsUsbTransport() { | 
|  | 257 | Close(); | 
|  | 258 | } | 
|  | 259 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 260 | int WindowsUsbTransport::Close() { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | DBG("usb_close\n"); | 
|  | 262 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 263 | if (nullptr != handle_) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | // Cleanup handle | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 265 | usb_cleanup_handle(handle_.get()); | 
|  | 266 | handle_.reset(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
|  | 269 | return 0; | 
|  | 270 | } | 
|  | 271 |  | 
| Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 272 | int WindowsUsbTransport::Reset() { | 
|  | 273 | DBG("usb_reset currently unsupported\n\n"); | 
|  | 274 | // TODO, this is a bit complicated since it is using ADB | 
|  | 275 | return -1; | 
|  | 276 | } | 
|  | 277 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | int recognized_device(usb_handle* handle, ifc_match_func callback) { | 
|  | 279 | struct usb_ifc_info info; | 
|  | 280 | USB_DEVICE_DESCRIPTOR device_desc; | 
|  | 281 | USB_INTERFACE_DESCRIPTOR interf_desc; | 
|  | 282 |  | 
|  | 283 | if (NULL == handle) | 
|  | 284 | return 0; | 
|  | 285 |  | 
|  | 286 | // Check vendor and product id first | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 287 | if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) { | 
|  | 288 | DBG("skipping device %x:%x\n", device_desc.idVendor, device_desc.idProduct); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | return 0; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | // Then check interface properties | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 293 | if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) { | 
|  | 294 | DBG("skipping device %x:%x, failed to find interface\n", device_desc.idVendor, | 
|  | 295 | device_desc.idProduct); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 296 | return 0; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | // Must have two endpoints | 
|  | 300 | if (2 != interf_desc.bNumEndpoints) { | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 301 | DBG("skipping device %x:%x, incorrect number of endpoints\n", device_desc.idVendor, | 
|  | 302 | device_desc.idProduct); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 303 | return 0; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | info.dev_vendor = device_desc.idVendor; | 
|  | 307 | info.dev_product = device_desc.idProduct; | 
|  | 308 | info.dev_class = device_desc.bDeviceClass; | 
|  | 309 | info.dev_subclass = device_desc.bDeviceSubClass; | 
|  | 310 | info.dev_protocol = device_desc.bDeviceProtocol; | 
|  | 311 | info.ifc_class = interf_desc.bInterfaceClass; | 
|  | 312 | info.ifc_subclass = interf_desc.bInterfaceSubClass; | 
|  | 313 | info.ifc_protocol = interf_desc.bInterfaceProtocol; | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 314 | info.writable = 1; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 315 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | // read serial number (if there is one) | 
|  | 317 | unsigned long serial_number_len = sizeof(info.serial_number); | 
|  | 318 | if (!AdbGetSerialNumber(handle->adb_interface, info.serial_number, | 
|  | 319 | &serial_number_len, true)) { | 
|  | 320 | info.serial_number[0] = 0; | 
|  | 321 | } | 
|  | 322 |  | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 323 | info.device_path[0] = 0; | 
|  | 324 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | if (callback(&info) == 0) { | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 326 | DBG("skipping device %x:%x, not selected by callback\n", device_desc.idVendor, | 
|  | 327 | device_desc.idProduct); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | return 1; | 
|  | 329 | } | 
|  | 330 |  | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 331 | DBG("found device %x:%x (%s)\n", device_desc.idVendor, device_desc.idProduct, | 
|  | 332 | info.serial_number); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | return 0; | 
|  | 334 | } | 
|  | 335 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 336 | static std::unique_ptr<usb_handle> find_usb_device(ifc_match_func callback) { | 
|  | 337 | std::unique_ptr<usb_handle> handle; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | char entry_buffer[2048]; | 
|  | 339 | char interf_name[2048]; | 
|  | 340 | AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); | 
|  | 341 | unsigned long entry_buffer_size = sizeof(entry_buffer); | 
|  | 342 | char* copy_name; | 
|  | 343 |  | 
|  | 344 | // Enumerate all present and active interfaces. | 
|  | 345 | ADBAPIHANDLE enum_handle = | 
|  | 346 | AdbEnumInterfaces(usb_class_id, true, true, true); | 
|  | 347 |  | 
|  | 348 | if (NULL == enum_handle) | 
|  | 349 | return NULL; | 
|  | 350 |  | 
|  | 351 | while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { | 
|  | 352 | // TODO(vchtchetkine): FIXME - temp hack converting wchar_t into char. | 
|  | 353 | // It would be better to change AdbNextInterface so it will return | 
|  | 354 | // interface name as single char string. | 
|  | 355 | const wchar_t* wchar_name = next_interface->device_name; | 
|  | 356 | for(copy_name = interf_name; | 
|  | 357 | L'\0' != *wchar_name; | 
|  | 358 | wchar_name++, copy_name++) { | 
|  | 359 | *copy_name = (char)(*wchar_name); | 
|  | 360 | } | 
|  | 361 | *copy_name = '\0'; | 
|  | 362 |  | 
| Josh Gao | f22e953 | 2018-05-31 21:24:14 -0700 | [diff] [blame] | 363 | DBG("attempting to open interface %S\n", next_interface->device_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | handle = do_usb_open(next_interface->device_name); | 
|  | 365 | if (NULL != handle) { | 
|  | 366 | // Lets see if this interface (device) belongs to us | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 367 | if (recognized_device(handle.get(), callback)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | // found it! | 
|  | 369 | break; | 
|  | 370 | } else { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 371 | usb_cleanup_handle(handle.get()); | 
|  | 372 | handle.reset(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | } | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | entry_buffer_size = sizeof(entry_buffer); | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | AdbCloseHandle(enum_handle); | 
|  | 380 | return handle; | 
|  | 381 | } | 
|  | 382 |  | 
| Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 383 | UsbTransport* usb_open(ifc_match_func callback, uint32_t) { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 384 | std::unique_ptr<usb_handle> handle = find_usb_device(callback); | 
|  | 385 | return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | } |