| 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 |  | 
 | 69 | class WindowsUsbTransport : public Transport { | 
 | 70 |   public: | 
 | 71 |     WindowsUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {} | 
 | 72 |     ~WindowsUsbTransport() override = default; | 
 | 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; | 
 | 77 |  | 
 | 78 |   private: | 
 | 79 |     std::unique_ptr<usb_handle> handle_; | 
 | 80 |  | 
 | 81 |     DISALLOW_COPY_AND_ASSIGN(WindowsUsbTransport); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | }; | 
 | 83 |  | 
 | 84 | /// Class ID assigned to the device by androidusb.sys | 
 | 85 | static const GUID usb_class_id = ANDROID_USB_CLASS_ID; | 
 | 86 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | /// Checks if interface (device) matches certain criteria | 
 | 88 | int recognized_device(usb_handle* handle, ifc_match_func callback); | 
 | 89 |  | 
 | 90 | /// Opens usb interface (device) by interface (device) name. | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 91 | 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] | 92 |  | 
 | 93 | /// Cleans up opened usb handle | 
 | 94 | void usb_cleanup_handle(usb_handle* handle); | 
 | 95 |  | 
 | 96 | /// Cleans up (but don't close) opened usb handle | 
 | 97 | void usb_kick(usb_handle* handle); | 
 | 98 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 100 | 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] | 101 |     // Allocate our handle | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 102 |     std::unique_ptr<usb_handle> ret(new usb_handle); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 |  | 
 | 104 |     // Create interface. | 
 | 105 |     ret->adb_interface = AdbCreateInterfaceByName(interface_name); | 
 | 106 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 107 |     if (nullptr == ret->adb_interface) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 |         errno = GetLastError(); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 109 |         return nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 |     } | 
 | 111 |  | 
 | 112 |     // Open read pipe (endpoint) | 
 | 113 |     ret->adb_read_pipe = | 
 | 114 |         AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, | 
 | 115 |                                    AdbOpenAccessTypeReadWrite, | 
 | 116 |                                    AdbOpenSharingModeReadWrite); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 117 |     if (nullptr != ret->adb_read_pipe) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 |         // Open write pipe (endpoint) | 
 | 119 |         ret->adb_write_pipe = | 
 | 120 |             AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, | 
 | 121 |                                       AdbOpenAccessTypeReadWrite, | 
 | 122 |                                       AdbOpenSharingModeReadWrite); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 123 |         if (nullptr != ret->adb_write_pipe) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 |             // Save interface name | 
 | 125 |             unsigned long name_len = 0; | 
 | 126 |  | 
 | 127 |             // First get expected name length | 
 | 128 |             AdbGetInterfaceName(ret->adb_interface, | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 129 |                           nullptr, | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 |                           &name_len, | 
 | 131 |                           true); | 
 | 132 |             if (0 != name_len) { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 133 |                 // Now save the name | 
 | 134 |                 ret->interface_name.resize(name_len); | 
 | 135 |                 if (AdbGetInterfaceName(ret->adb_interface, | 
 | 136 |                               &ret->interface_name[0], | 
 | 137 |                               &name_len, | 
 | 138 |                               true)) { | 
 | 139 |                     // We're done at this point | 
 | 140 |                     return ret; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 |                 } | 
 | 142 |             } | 
 | 143 |         } | 
 | 144 |     } | 
 | 145 |  | 
 | 146 |     // Something went wrong. | 
 | 147 |     errno = GetLastError(); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 148 |     usb_cleanup_handle(ret.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 |     SetLastError(errno); | 
 | 150 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 151 |     return nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | } | 
 | 153 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 154 | ssize_t WindowsUsbTransport::Write(const void* data, size_t len) { | 
| Jiebing Li | bbb7981 | 2012-08-07 14:29:21 +0800 | [diff] [blame] | 155 |     unsigned long time_out = 5000; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 |     unsigned long written = 0; | 
 | 157 |     unsigned count = 0; | 
 | 158 |     int ret; | 
 | 159 |  | 
 | 160 |     DBG("usb_write %d\n", len); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 161 |     if (nullptr != handle_) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 |         // Perform write | 
 | 163 |         while(len > 0) { | 
| David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 164 |             int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 165 |             ret = AdbWriteEndpointSync(handle_->adb_write_pipe, const_cast<void*>(data), xfer, | 
 | 166 |                                        &written, time_out); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 167 |             errno = GetLastError(); | 
 | 168 |             DBG("AdbWriteEndpointSync returned %d, errno: %d\n", ret, errno); | 
 | 169 |             if (ret == 0) { | 
 | 170 |                 // assume ERROR_INVALID_HANDLE indicates we are disconnected | 
 | 171 |                 if (errno == ERROR_INVALID_HANDLE) | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 172 |                 usb_kick(handle_.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 |                 return -1; | 
 | 174 |             } | 
 | 175 |  | 
 | 176 |             count += written; | 
 | 177 |             len -= written; | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 178 |             data = (const char *)data + written; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 |  | 
 | 180 |             if (len == 0) | 
 | 181 |                 return count; | 
 | 182 |         } | 
 | 183 |     } else { | 
 | 184 |         DBG("usb_write NULL handle\n"); | 
 | 185 |         SetLastError(ERROR_INVALID_HANDLE); | 
 | 186 |     } | 
 | 187 |  | 
 | 188 |     DBG("usb_write failed: %d\n", errno); | 
 | 189 |  | 
 | 190 |     return -1; | 
 | 191 | } | 
 | 192 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 193 | ssize_t WindowsUsbTransport::Read(void* data, size_t len) { | 
| Jiebing Li | bbb7981 | 2012-08-07 14:29:21 +0800 | [diff] [blame] | 194 |     unsigned long time_out = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 |     unsigned long read = 0; | 
 | 196 |     int ret; | 
 | 197 |  | 
 | 198 |     DBG("usb_read %d\n", len); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 199 |     if (nullptr != handle_) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 |         while (1) { | 
| David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 201 |             int 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] | 202 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 203 |             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] | 204 |             errno = GetLastError(); | 
 | 205 |             DBG("usb_read got: %ld, expected: %d, errno: %d\n", read, xfer, errno); | 
 | 206 |             if (ret) { | 
 | 207 |                 return read; | 
| Jiebing Li | bbb7981 | 2012-08-07 14:29:21 +0800 | [diff] [blame] | 208 |             } else { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 |                 // assume ERROR_INVALID_HANDLE indicates we are disconnected | 
 | 210 |                 if (errno == ERROR_INVALID_HANDLE) | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 211 |                     usb_kick(handle_.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 212 |                 break; | 
 | 213 |             } | 
 | 214 |             // else we timed out - try again | 
 | 215 |         } | 
 | 216 |     } else { | 
 | 217 |         DBG("usb_read NULL handle\n"); | 
 | 218 |         SetLastError(ERROR_INVALID_HANDLE); | 
 | 219 |     } | 
 | 220 |  | 
 | 221 |     DBG("usb_read failed: %d\n", errno); | 
 | 222 |  | 
 | 223 |     return -1; | 
 | 224 | } | 
 | 225 |  | 
 | 226 | void usb_cleanup_handle(usb_handle* handle) { | 
 | 227 |     if (NULL != handle) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 |         if (NULL != handle->adb_write_pipe) | 
 | 229 |             AdbCloseHandle(handle->adb_write_pipe); | 
 | 230 |         if (NULL != handle->adb_read_pipe) | 
 | 231 |             AdbCloseHandle(handle->adb_read_pipe); | 
 | 232 |         if (NULL != handle->adb_interface) | 
 | 233 |             AdbCloseHandle(handle->adb_interface); | 
 | 234 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 235 |         handle->interface_name.clear(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 |         handle->adb_write_pipe = NULL; | 
 | 237 |         handle->adb_read_pipe = NULL; | 
 | 238 |         handle->adb_interface = NULL; | 
 | 239 |     } | 
 | 240 | } | 
 | 241 |  | 
 | 242 | void usb_kick(usb_handle* handle) { | 
 | 243 |     if (NULL != handle) { | 
 | 244 |         usb_cleanup_handle(handle); | 
 | 245 |     } else { | 
 | 246 |         SetLastError(ERROR_INVALID_HANDLE); | 
 | 247 |         errno = ERROR_INVALID_HANDLE; | 
 | 248 |     } | 
 | 249 | } | 
 | 250 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 251 | int WindowsUsbTransport::Close() { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 252 |     DBG("usb_close\n"); | 
 | 253 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 254 |     if (nullptr != handle_) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 |         // Cleanup handle | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 256 |         usb_cleanup_handle(handle_.get()); | 
 | 257 |         handle_.reset(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 |     } | 
 | 259 |  | 
 | 260 |     return 0; | 
 | 261 | } | 
 | 262 |  | 
 | 263 | int recognized_device(usb_handle* handle, ifc_match_func callback) { | 
 | 264 |     struct usb_ifc_info info; | 
 | 265 |     USB_DEVICE_DESCRIPTOR device_desc; | 
 | 266 |     USB_INTERFACE_DESCRIPTOR interf_desc; | 
 | 267 |  | 
 | 268 |     if (NULL == handle) | 
 | 269 |         return 0; | 
 | 270 |  | 
 | 271 |     // Check vendor and product id first | 
 | 272 |     if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, | 
 | 273 |                                  &device_desc)) { | 
 | 274 |         return 0; | 
 | 275 |     } | 
 | 276 |  | 
 | 277 |     // Then check interface properties | 
 | 278 |     if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, | 
 | 279 |                                     &interf_desc)) { | 
 | 280 |         return 0; | 
 | 281 |     } | 
 | 282 |  | 
 | 283 |     // Must have two endpoints | 
 | 284 |     if (2 != interf_desc.bNumEndpoints) { | 
 | 285 |         return 0; | 
 | 286 |     } | 
 | 287 |  | 
 | 288 |     info.dev_vendor = device_desc.idVendor; | 
 | 289 |     info.dev_product = device_desc.idProduct; | 
 | 290 |     info.dev_class = device_desc.bDeviceClass; | 
 | 291 |     info.dev_subclass = device_desc.bDeviceSubClass; | 
 | 292 |     info.dev_protocol = device_desc.bDeviceProtocol; | 
 | 293 |     info.ifc_class = interf_desc.bInterfaceClass; | 
 | 294 |     info.ifc_subclass = interf_desc.bInterfaceSubClass; | 
 | 295 |     info.ifc_protocol = interf_desc.bInterfaceProtocol; | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 296 |     info.writable = 1; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 297 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 |     // read serial number (if there is one) | 
 | 299 |     unsigned long serial_number_len = sizeof(info.serial_number); | 
 | 300 |     if (!AdbGetSerialNumber(handle->adb_interface, info.serial_number, | 
 | 301 |                     &serial_number_len, true)) { | 
 | 302 |         info.serial_number[0] = 0; | 
 | 303 |     } | 
 | 304 |  | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 305 |     info.device_path[0] = 0; | 
 | 306 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 |     if (callback(&info) == 0) { | 
 | 308 |         return 1; | 
 | 309 |     } | 
 | 310 |  | 
 | 311 |     return 0; | 
 | 312 | } | 
 | 313 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 314 | static std::unique_ptr<usb_handle> find_usb_device(ifc_match_func callback) { | 
 | 315 |     std::unique_ptr<usb_handle> handle; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 |     char entry_buffer[2048]; | 
 | 317 |     char interf_name[2048]; | 
 | 318 |     AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); | 
 | 319 |     unsigned long entry_buffer_size = sizeof(entry_buffer); | 
 | 320 |     char* copy_name; | 
 | 321 |  | 
 | 322 |     // Enumerate all present and active interfaces. | 
 | 323 |     ADBAPIHANDLE enum_handle = | 
 | 324 |         AdbEnumInterfaces(usb_class_id, true, true, true); | 
 | 325 |  | 
 | 326 |     if (NULL == enum_handle) | 
 | 327 |         return NULL; | 
 | 328 |  | 
 | 329 |     while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { | 
 | 330 |         // TODO(vchtchetkine): FIXME - temp hack converting wchar_t into char. | 
 | 331 |         // It would be better to change AdbNextInterface so it will return | 
 | 332 |         // interface name as single char string. | 
 | 333 |         const wchar_t* wchar_name = next_interface->device_name; | 
 | 334 |         for(copy_name = interf_name; | 
 | 335 |                 L'\0' != *wchar_name; | 
 | 336 |                 wchar_name++, copy_name++) { | 
 | 337 |             *copy_name = (char)(*wchar_name); | 
 | 338 |         } | 
 | 339 |         *copy_name = '\0'; | 
 | 340 |  | 
 | 341 |         handle = do_usb_open(next_interface->device_name); | 
 | 342 |         if (NULL != handle) { | 
 | 343 |             // Lets see if this interface (device) belongs to us | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 344 |             if (recognized_device(handle.get(), callback)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 345 |                 // found it! | 
 | 346 |                 break; | 
 | 347 |             } else { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 348 |                 usb_cleanup_handle(handle.get()); | 
 | 349 |                 handle.reset(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 |             } | 
 | 351 |         } | 
 | 352 |  | 
 | 353 |         entry_buffer_size = sizeof(entry_buffer); | 
 | 354 |     } | 
 | 355 |  | 
 | 356 |     AdbCloseHandle(enum_handle); | 
 | 357 |     return handle; | 
 | 358 | } | 
 | 359 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 360 | Transport* usb_open(ifc_match_func callback) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 362 |     std::unique_ptr<usb_handle> handle = find_usb_device(callback); | 
 | 363 |     return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | } |