| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG USB | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include "sysdeps.h" | 
|  | 20 |  | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <winsock2.h>  // winsock.h *must* be included before windows.h. | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 22 | #include <windows.h> | 
|  | 23 | #include <usb100.h> | 
|  | 24 | #include <winerror.h> | 
|  | 25 |  | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <errno.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <stdio.h> | 
| Christopher Ferris | 67a7a4a | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 28 | #include <stdlib.h> | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | #include <mutex> | 
|  | 31 |  | 
|  | 32 | #include <adb_api.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 |  | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 34 | #include <android-base/errors.h> | 
|  | 35 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include "adb.h" | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 37 | #include "transport.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | /** Structure usb_handle describes our connection to the usb device via | 
|  | 40 | AdbWinApi.dll. This structure is returned from usb_open() routine and | 
|  | 41 | is expected in each subsequent call that is accessing the device. | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | Most members are protected by usb_lock, except for adb_{read,write}_pipe which | 
|  | 44 | rely on AdbWinApi.dll's handle validation and AdbCloseHandle(endpoint)'s | 
|  | 45 | ability to break a thread out of pipe IO. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | */ | 
|  | 47 | struct usb_handle { | 
|  | 48 | /// Previous entry in the list of opened usb handles | 
|  | 49 | usb_handle *prev; | 
|  | 50 |  | 
|  | 51 | /// Next entry in the list of opened usb handles | 
|  | 52 | usb_handle *next; | 
|  | 53 |  | 
|  | 54 | /// Handle to USB interface | 
|  | 55 | ADBAPIHANDLE  adb_interface; | 
|  | 56 |  | 
|  | 57 | /// Handle to USB read pipe (endpoint) | 
|  | 58 | ADBAPIHANDLE  adb_read_pipe; | 
|  | 59 |  | 
|  | 60 | /// Handle to USB write pipe (endpoint) | 
|  | 61 | ADBAPIHANDLE  adb_write_pipe; | 
|  | 62 |  | 
|  | 63 | /// Interface name | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 64 | wchar_t*      interface_name; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 |  | 
|  | 66 | /// Mask for determining when to use zero length packets | 
|  | 67 | unsigned zero_mask; | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | /// Class ID assigned to the device by androidusb.sys | 
|  | 71 | static const GUID usb_class_id = ANDROID_USB_CLASS_ID; | 
|  | 72 |  | 
|  | 73 | /// List of opened usb handles | 
|  | 74 | static usb_handle handle_list = { | 
|  | 75 | .prev = &handle_list, | 
|  | 76 | .next = &handle_list, | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | /// Locker for the list of opened usb handles | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 80 | static std::mutex& usb_lock = *new std::mutex(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 |  | 
|  | 82 | /// Checks if there is opened usb handle in handle_list for this device. | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 83 | int known_device(const wchar_t* dev_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | /// Checks if there is opened usb handle in handle_list for this device. | 
|  | 86 | /// usb_lock mutex must be held before calling this routine. | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 87 | int known_device_locked(const wchar_t* dev_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | /// Registers opened usb handle (adds it to handle_list). | 
|  | 90 | int register_new_device(usb_handle* handle); | 
|  | 91 |  | 
|  | 92 | /// Checks if interface (device) matches certain criteria | 
|  | 93 | int recognized_device(usb_handle* handle); | 
|  | 94 |  | 
|  | 95 | /// Enumerates present and available interfaces (devices), opens new ones and | 
|  | 96 | /// registers usb transport for them. | 
|  | 97 | void find_devices(); | 
|  | 98 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 99 | /// Kicks all USB devices | 
|  | 100 | static void kick_devices(); | 
|  | 101 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | /// Entry point for thread that polls (every second) for new usb interfaces. | 
|  | 103 | /// This routine calls find_devices in infinite loop. | 
| Josh Gao | b5fea14 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 104 | static void device_poll_thread(void*); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 |  | 
|  | 106 | /// Initializes this module | 
|  | 107 | void usb_init(); | 
|  | 108 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | /// Opens usb interface (device) by interface (device) name. | 
|  | 110 | usb_handle* do_usb_open(const wchar_t* interface_name); | 
|  | 111 |  | 
|  | 112 | /// Writes data to the opened usb handle | 
|  | 113 | int usb_write(usb_handle* handle, const void* data, int len); | 
|  | 114 |  | 
|  | 115 | /// Reads data using the opened usb handle | 
|  | 116 | int usb_read(usb_handle *handle, void* data, int len); | 
|  | 117 |  | 
|  | 118 | /// Cleans up opened usb handle | 
|  | 119 | void usb_cleanup_handle(usb_handle* handle); | 
|  | 120 |  | 
|  | 121 | /// Cleans up (but don't close) opened usb handle | 
|  | 122 | void usb_kick(usb_handle* handle); | 
|  | 123 |  | 
|  | 124 | /// Closes opened usb handle | 
|  | 125 | int usb_close(usb_handle* handle); | 
|  | 126 |  | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 127 | int known_device_locked(const wchar_t* dev_name) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | usb_handle* usb; | 
|  | 129 |  | 
|  | 130 | if (NULL != dev_name) { | 
|  | 131 | // Iterate through the list looking for the name match. | 
|  | 132 | for(usb = handle_list.next; usb != &handle_list; usb = usb->next) { | 
|  | 133 | // In Windows names are not case sensetive! | 
|  | 134 | if((NULL != usb->interface_name) && | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 135 | (0 == wcsicmp(usb->interface_name, dev_name))) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | return 1; | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | return 0; | 
|  | 142 | } | 
|  | 143 |  | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 144 | int known_device(const wchar_t* dev_name) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | int ret = 0; | 
|  | 146 |  | 
|  | 147 | if (NULL != dev_name) { | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 148 | std::lock_guard<std::mutex> lock(usb_lock); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | ret = known_device_locked(dev_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | return ret; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | int register_new_device(usb_handle* handle) { | 
|  | 156 | if (NULL == handle) | 
|  | 157 | return 0; | 
|  | 158 |  | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 159 | std::lock_guard<std::mutex> lock(usb_lock); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 |  | 
|  | 161 | // Check if device is already in the list | 
|  | 162 | if (known_device_locked(handle->interface_name)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | return 0; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | // Not in the list. Add this handle to the list. | 
|  | 167 | handle->next = &handle_list; | 
|  | 168 | handle->prev = handle_list.prev; | 
|  | 169 | handle->prev->next = handle; | 
|  | 170 | handle->next->prev = handle; | 
|  | 171 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | return 1; | 
|  | 173 | } | 
|  | 174 |  | 
| Josh Gao | b5fea14 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 175 | void device_poll_thread(void*) { | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 176 | adb_thread_setname("Device Poll"); | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 177 | D("Created device thread"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 |  | 
|  | 179 | while(1) { | 
|  | 180 | find_devices(); | 
|  | 181 | adb_sleep_ms(1000); | 
|  | 182 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 185 | static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, | 
|  | 186 | LPARAM lParam) { | 
|  | 187 | switch (uMsg) { | 
|  | 188 | case WM_POWERBROADCAST: | 
|  | 189 | switch (wParam) { | 
|  | 190 | case PBT_APMRESUMEAUTOMATIC: | 
|  | 191 | // Resuming from sleep or hibernation, so kick all existing USB devices | 
|  | 192 | // and then allow the device_poll_thread to redetect USB devices from | 
|  | 193 | // scratch. If we don't do this, existing USB devices will never respond | 
|  | 194 | // to us because they'll be waiting for the connect/auth handshake. | 
|  | 195 | D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, " | 
|  | 196 | "so kicking all USB devices\n"); | 
|  | 197 | kick_devices(); | 
|  | 198 | return TRUE; | 
|  | 199 | } | 
|  | 200 | } | 
|  | 201 | return DefWindowProcW(hwnd, uMsg, wParam, lParam); | 
|  | 202 | } | 
|  | 203 |  | 
| Josh Gao | b5fea14 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 204 | static void _power_notification_thread(void*) { | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 205 | // This uses a thread with its own window message pump to get power | 
|  | 206 | // notifications. If adb runs from a non-interactive service account, this | 
|  | 207 | // might not work (not sure). If that happens to not work, we could use | 
|  | 208 | // heavyweight WMI APIs to get power notifications. But for the common case | 
|  | 209 | // of a developer's interactive session, a window message pump is more | 
|  | 210 | // appropriate. | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 211 | D("Created power notification thread"); | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 212 | adb_thread_setname("Power Notifier"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 213 |  | 
|  | 214 | // Window class names are process specific. | 
|  | 215 | static const WCHAR kPowerNotificationWindowClassName[] = | 
|  | 216 | L"PowerNotificationWindow"; | 
|  | 217 |  | 
|  | 218 | // Get the HINSTANCE corresponding to the module that _power_window_proc | 
|  | 219 | // is in (the main module). | 
|  | 220 | const HINSTANCE instance = GetModuleHandleW(NULL); | 
|  | 221 | if (!instance) { | 
|  | 222 | // This is such a common API call that this should never fail. | 
|  | 223 | fatal("GetModuleHandleW failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 224 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | WNDCLASSEXW wndclass; | 
|  | 228 | memset(&wndclass, 0, sizeof(wndclass)); | 
|  | 229 | wndclass.cbSize = sizeof(wndclass); | 
|  | 230 | wndclass.lpfnWndProc = _power_window_proc; | 
|  | 231 | wndclass.hInstance = instance; | 
|  | 232 | wndclass.lpszClassName = kPowerNotificationWindowClassName; | 
|  | 233 | if (!RegisterClassExW(&wndclass)) { | 
|  | 234 | fatal("RegisterClassExW failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 235 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
|  | 238 | if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName, | 
|  | 239 | L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, | 
|  | 240 | NULL, NULL, instance, NULL)) { | 
|  | 241 | fatal("CreateWindowExW failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 242 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
|  | 245 | MSG msg; | 
|  | 246 | while (GetMessageW(&msg, NULL, 0, 0)) { | 
|  | 247 | TranslateMessage(&msg); | 
|  | 248 | DispatchMessageW(&msg); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | // GetMessageW() will return false if a quit message is posted. We don't | 
|  | 252 | // do that, but it might be possible for that to occur when logging off or | 
|  | 253 | // shutting down. Not a big deal since the whole process will be going away | 
|  | 254 | // soon anyway. | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 255 | D("Power notification thread exiting"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | void usb_init() { | 
| Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 259 | if (!adb_thread_create(device_poll_thread, nullptr)) { | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 260 | fatal_errno("cannot create device poll thread"); | 
|  | 261 | } | 
|  | 262 | if (!adb_thread_create(_power_notification_thread, nullptr)) { | 
|  | 263 | fatal_errno("cannot create power notification thread"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | } | 
|  | 265 | } | 
|  | 266 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | usb_handle* do_usb_open(const wchar_t* interface_name) { | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 268 | unsigned long name_len = 0; | 
|  | 269 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | // Allocate our handle | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 271 | usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle)); | 
|  | 272 | if (NULL == ret) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 273 | D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle), | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 274 | strerror(errno)); | 
|  | 275 | goto fail; | 
|  | 276 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 |  | 
|  | 278 | // Set linkers back to the handle | 
|  | 279 | ret->next = ret; | 
|  | 280 | ret->prev = ret; | 
|  | 281 |  | 
|  | 282 | // Create interface. | 
|  | 283 | ret->adb_interface = AdbCreateInterfaceByName(interface_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | if (NULL == ret->adb_interface) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 285 | D("AdbCreateInterfaceByName failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 286 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 287 | goto fail; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
|  | 290 | // Open read pipe (endpoint) | 
|  | 291 | ret->adb_read_pipe = | 
|  | 292 | AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, | 
|  | 293 | AdbOpenAccessTypeReadWrite, | 
|  | 294 | AdbOpenSharingModeReadWrite); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 295 | if (NULL == ret->adb_read_pipe) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 296 | D("AdbOpenDefaultBulkReadEndpoint failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 297 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 298 | goto fail; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 301 | // Open write pipe (endpoint) | 
|  | 302 | ret->adb_write_pipe = | 
|  | 303 | AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, | 
|  | 304 | AdbOpenAccessTypeReadWrite, | 
|  | 305 | AdbOpenSharingModeReadWrite); | 
|  | 306 | if (NULL == ret->adb_write_pipe) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 307 | D("AdbOpenDefaultBulkWriteEndpoint failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 308 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 309 | goto fail; | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | // Save interface name | 
|  | 313 | // First get expected name length | 
|  | 314 | AdbGetInterfaceName(ret->adb_interface, | 
|  | 315 | NULL, | 
|  | 316 | &name_len, | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 317 | false); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 318 | if (0 == name_len) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 319 | D("AdbGetInterfaceName returned name length of zero: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 320 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 321 | goto fail; | 
|  | 322 | } | 
|  | 323 |  | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 324 | ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0])); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 325 | if (NULL == ret->interface_name) { | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 326 | D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno)); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 327 | goto fail; | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | // Now save the name | 
|  | 331 | if (!AdbGetInterfaceName(ret->adb_interface, | 
|  | 332 | ret->interface_name, | 
|  | 333 | &name_len, | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 334 | false)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 335 | D("AdbGetInterfaceName failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 336 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 337 | goto fail; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | // We're done at this point | 
|  | 341 | return ret; | 
|  | 342 |  | 
|  | 343 | fail: | 
|  | 344 | if (NULL != ret) { | 
|  | 345 | usb_cleanup_handle(ret); | 
|  | 346 | free(ret); | 
|  | 347 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 |  | 
|  | 349 | return NULL; | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | int usb_write(usb_handle* handle, const void* data, int len) { | 
| Jack Ren | 1c4b760 | 2011-08-12 18:39:04 +0800 | [diff] [blame] | 353 | unsigned long time_out = 5000; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 354 | unsigned long written = 0; | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 355 | int err = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 357 | D("usb_write %d", len); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 358 | if (NULL == handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 359 | D("usb_write was passed NULL handle"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 360 | err = EINVAL; | 
|  | 361 | goto fail; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 364 | // Perform write | 
|  | 365 | if (!AdbWriteEndpointSync(handle->adb_write_pipe, | 
|  | 366 | (void*)data, | 
|  | 367 | (unsigned long)len, | 
|  | 368 | &written, | 
|  | 369 | time_out)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 370 | D("AdbWriteEndpointSync failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 371 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 372 | err = EIO; | 
|  | 373 | goto fail; | 
|  | 374 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 375 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 376 | // Make sure that we've written what we were asked to write | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 377 | D("usb_write got: %ld, expected: %d", written, len); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 378 | if (written != (unsigned long)len) { | 
|  | 379 | // If this occurs, this code should be changed to repeatedly call | 
|  | 380 | // AdbWriteEndpointSync() until all bytes are written. | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 381 | D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld", | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 382 | len, written); | 
|  | 383 | err = EIO; | 
|  | 384 | goto fail; | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | if (handle->zero_mask && (len & handle->zero_mask) == 0) { | 
|  | 388 | // Send a zero length packet | 
|  | 389 | if (!AdbWriteEndpointSync(handle->adb_write_pipe, | 
|  | 390 | (void*)data, | 
|  | 391 | 0, | 
|  | 392 | &written, | 
|  | 393 | time_out)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 394 | D("AdbWriteEndpointSync of zero length packet failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 395 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 396 | err = EIO; | 
|  | 397 | goto fail; | 
|  | 398 | } | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | return 0; | 
|  | 402 |  | 
|  | 403 | fail: | 
|  | 404 | // Any failure should cause us to kick the device instead of leaving it a | 
|  | 405 | // zombie state with potential to hang. | 
|  | 406 | if (NULL != handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 407 | D("Kicking device due to error in usb_write"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 408 | usb_kick(handle); | 
|  | 409 | } | 
|  | 410 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 411 | D("usb_write failed"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 412 | errno = err; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | return -1; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | int usb_read(usb_handle *handle, void* data, int len) { | 
| Jack Ren | 1c4b760 | 2011-08-12 18:39:04 +0800 | [diff] [blame] | 417 | unsigned long time_out = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 418 | unsigned long read = 0; | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 419 | int err = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 420 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 421 | D("usb_read %d", len); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 422 | if (NULL == handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 423 | D("usb_read was passed NULL handle"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 424 | err = EINVAL; | 
|  | 425 | goto fail; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | } | 
|  | 427 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 428 | while (len > 0) { | 
|  | 429 | if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read, | 
|  | 430 | time_out)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 431 | D("AdbReadEndpointSync failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 432 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 433 | err = EIO; | 
|  | 434 | goto fail; | 
|  | 435 | } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 436 | D("usb_read got: %ld, expected: %d", read, len); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 438 | data = (char *)data + read; | 
|  | 439 | len -= read; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | return 0; | 
|  | 443 |  | 
|  | 444 | fail: | 
|  | 445 | // Any failure should cause us to kick the device instead of leaving it a | 
|  | 446 | // zombie state with potential to hang. | 
|  | 447 | if (NULL != handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 448 | D("Kicking device due to error in usb_read"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 449 | usb_kick(handle); | 
|  | 450 | } | 
|  | 451 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 452 | D("usb_read failed"); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 453 | errno = err; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 454 | return -1; | 
|  | 455 | } | 
|  | 456 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 457 | // Wrapper around AdbCloseHandle() that logs diagnostics. | 
|  | 458 | static void _adb_close_handle(ADBAPIHANDLE adb_handle) { | 
|  | 459 | if (!AdbCloseHandle(adb_handle)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 460 | D("AdbCloseHandle(%p) failed: %s", adb_handle, | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 461 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 462 | } | 
|  | 463 | } | 
|  | 464 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 | void usb_cleanup_handle(usb_handle* handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 466 | D("usb_cleanup_handle"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 467 | if (NULL != handle) { | 
|  | 468 | if (NULL != handle->interface_name) | 
|  | 469 | free(handle->interface_name); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 470 | // AdbCloseHandle(pipe) will break any threads out of pending IO calls and | 
|  | 471 | // wait until the pipe no longer uses the interface. Then we can | 
|  | 472 | // AdbCloseHandle() the interface. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 473 | if (NULL != handle->adb_write_pipe) | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 474 | _adb_close_handle(handle->adb_write_pipe); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 475 | if (NULL != handle->adb_read_pipe) | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 476 | _adb_close_handle(handle->adb_read_pipe); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 477 | if (NULL != handle->adb_interface) | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 478 | _adb_close_handle(handle->adb_interface); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 |  | 
|  | 480 | handle->interface_name = NULL; | 
|  | 481 | handle->adb_write_pipe = NULL; | 
|  | 482 | handle->adb_read_pipe = NULL; | 
|  | 483 | handle->adb_interface = NULL; | 
|  | 484 | } | 
|  | 485 | } | 
|  | 486 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 487 | static void usb_kick_locked(usb_handle* handle) { | 
|  | 488 | // The reason the lock must be acquired before calling this function is in | 
|  | 489 | // case multiple threads are trying to kick the same device at the same time. | 
|  | 490 | usb_cleanup_handle(handle); | 
|  | 491 | } | 
|  | 492 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | void usb_kick(usb_handle* handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 494 | D("usb_kick"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | if (NULL != handle) { | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 496 | std::lock_guard<std::mutex> lock(usb_lock); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 497 | usb_kick_locked(handle); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 498 | } else { | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 499 | errno = EINVAL; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | } | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | int usb_close(usb_handle* handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 504 | D("usb_close"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 |  | 
|  | 506 | if (NULL != handle) { | 
|  | 507 | // Remove handle from the list | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 508 | { | 
|  | 509 | std::lock_guard<std::mutex> lock(usb_lock); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 |  | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 511 | if ((handle->next != handle) && (handle->prev != handle)) { | 
|  | 512 | handle->next->prev = handle->prev; | 
|  | 513 | handle->prev->next = handle->next; | 
|  | 514 | handle->prev = handle; | 
|  | 515 | handle->next = handle; | 
|  | 516 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 517 | } | 
|  | 518 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | // Cleanup handle | 
|  | 520 | usb_cleanup_handle(handle); | 
|  | 521 | free(handle); | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | return 0; | 
|  | 525 | } | 
|  | 526 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 527 | int recognized_device(usb_handle* handle) { | 
|  | 528 | if (NULL == handle) | 
|  | 529 | return 0; | 
|  | 530 |  | 
|  | 531 | // Check vendor and product id first | 
|  | 532 | USB_DEVICE_DESCRIPTOR device_desc; | 
|  | 533 |  | 
|  | 534 | if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, | 
|  | 535 | &device_desc)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 536 | D("AdbGetUsbDeviceDescriptor failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 537 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | return 0; | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | // Then check interface properties | 
|  | 542 | USB_INTERFACE_DESCRIPTOR interf_desc; | 
|  | 543 |  | 
|  | 544 | if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, | 
|  | 545 | &interf_desc)) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 546 | D("AdbGetUsbInterfaceDescriptor failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 547 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | return 0; | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | // Must have two endpoints | 
|  | 552 | if (2 != interf_desc.bNumEndpoints) { | 
|  | 553 | return 0; | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | if (is_adb_interface(device_desc.idVendor, device_desc.idProduct, | 
|  | 557 | interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) { | 
|  | 558 |  | 
|  | 559 | if(interf_desc.bInterfaceProtocol == 0x01) { | 
|  | 560 | AdbEndpointInformation endpoint_info; | 
|  | 561 | // assuming zero is a valid bulk endpoint ID | 
|  | 562 | if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) { | 
|  | 563 | handle->zero_mask = endpoint_info.max_packet_size - 1; | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 564 | D("device zero_mask: 0x%x", handle->zero_mask); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 565 | } else { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 566 | D("AdbGetEndpointInformation failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 567 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 568 | } | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | return 1; | 
|  | 572 | } | 
|  | 573 |  | 
|  | 574 | return 0; | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | void find_devices() { | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 578 | usb_handle* handle = NULL; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | char entry_buffer[2048]; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); | 
|  | 581 | unsigned long entry_buffer_size = sizeof(entry_buffer); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 582 |  | 
|  | 583 | // Enumerate all present and active interfaces. | 
|  | 584 | ADBAPIHANDLE enum_handle = | 
|  | 585 | AdbEnumInterfaces(usb_class_id, true, true, true); | 
|  | 586 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 587 | if (NULL == enum_handle) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 588 | D("AdbEnumInterfaces failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 589 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | return; | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 591 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 592 |  | 
|  | 593 | while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 594 | // Lets see if we already have this device in the list | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 595 | if (!known_device(next_interface->device_name)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 596 | // This seems to be a new device. Open it! | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 597 | handle = do_usb_open(next_interface->device_name); | 
|  | 598 | if (NULL != handle) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 | // Lets see if this interface (device) belongs to us | 
|  | 600 | if (recognized_device(handle)) { | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 601 | D("adding a new device %ls", next_interface->device_name); | 
|  | 602 |  | 
|  | 603 | // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug in | 
|  | 604 | // adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the | 
|  | 605 | // last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the | 
|  | 606 | // end of a stack buffer in the best case, and in the unlikely case of a long serial | 
|  | 607 | // number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the | 
|  | 608 | // resulting string, but we should avoid the bad reads in the first place. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 609 | char serial_number[512]; | 
|  | 610 | unsigned long serial_number_len = sizeof(serial_number); | 
|  | 611 | if (AdbGetSerialNumber(handle->adb_interface, | 
|  | 612 | serial_number, | 
|  | 613 | &serial_number_len, | 
|  | 614 | true)) { | 
|  | 615 | // Lets make sure that we don't duplicate this device | 
|  | 616 | if (register_new_device(handle)) { | 
| Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 617 | register_usb_transport(handle, serial_number, NULL, 1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 618 | } else { | 
| Spencer Low | bb29000 | 2015-11-12 20:13:21 -0800 | [diff] [blame] | 619 | D("register_new_device failed for %ls", next_interface->device_name); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | usb_cleanup_handle(handle); | 
|  | 621 | free(handle); | 
|  | 622 | } | 
|  | 623 | } else { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 624 | D("cannot get serial number: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 625 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 626 | usb_cleanup_handle(handle); | 
|  | 627 | free(handle); | 
|  | 628 | } | 
|  | 629 | } else { | 
|  | 630 | usb_cleanup_handle(handle); | 
|  | 631 | free(handle); | 
|  | 632 | } | 
|  | 633 | } | 
|  | 634 | } | 
|  | 635 |  | 
|  | 636 | entry_buffer_size = sizeof(entry_buffer); | 
|  | 637 | } | 
|  | 638 |  | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 639 | if (GetLastError() != ERROR_NO_MORE_ITEMS) { | 
|  | 640 | // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration. | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 641 | D("AdbNextInterface failed: %s", | 
| David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 642 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 643 | } | 
|  | 644 |  | 
|  | 645 | _adb_close_handle(enum_handle); | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | static void kick_devices() { | 
|  | 649 | // Need to acquire lock to safely walk the list which might be modified | 
|  | 650 | // by another thread. | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 651 | std::lock_guard<std::mutex> lock(usb_lock); | 
| Spencer Low | a5b06b0 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 652 | for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) { | 
|  | 653 | usb_kick_locked(usb); | 
|  | 654 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 655 | } |