blob: b8cc5cf9366dfcc9a25cee5cce73d701306990c3 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
19#include "sysdeps.h"
20
Dan Albert76649012015-02-24 15:51:19 -080021#include <winsock2.h> // winsock.h *must* be included before windows.h.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <adb_api.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdio.h>
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080025#include <stdlib.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <usb100.h>
27#include <windows.h>
28#include <winerror.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080031#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33/** Structure usb_handle describes our connection to the usb device via
34 AdbWinApi.dll. This structure is returned from usb_open() routine and
35 is expected in each subsequent call that is accessing the device.
Spencer Lowa5b06b02015-07-22 16:17:07 -070036
37 Most members are protected by usb_lock, except for adb_{read,write}_pipe which
38 rely on AdbWinApi.dll's handle validation and AdbCloseHandle(endpoint)'s
39 ability to break a thread out of pipe IO.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040*/
41struct usb_handle {
42 /// Previous entry in the list of opened usb handles
43 usb_handle *prev;
44
45 /// Next entry in the list of opened usb handles
46 usb_handle *next;
47
48 /// Handle to USB interface
49 ADBAPIHANDLE adb_interface;
50
51 /// Handle to USB read pipe (endpoint)
52 ADBAPIHANDLE adb_read_pipe;
53
54 /// Handle to USB write pipe (endpoint)
55 ADBAPIHANDLE adb_write_pipe;
56
57 /// Interface name
58 char* interface_name;
59
60 /// Mask for determining when to use zero length packets
61 unsigned zero_mask;
62};
63
64/// Class ID assigned to the device by androidusb.sys
65static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
66
67/// List of opened usb handles
68static usb_handle handle_list = {
69 .prev = &handle_list,
70 .next = &handle_list,
71};
72
73/// Locker for the list of opened usb handles
74ADB_MUTEX_DEFINE( usb_lock );
75
76/// Checks if there is opened usb handle in handle_list for this device.
77int known_device(const char* dev_name);
78
79/// Checks if there is opened usb handle in handle_list for this device.
80/// usb_lock mutex must be held before calling this routine.
81int known_device_locked(const char* dev_name);
82
83/// Registers opened usb handle (adds it to handle_list).
84int register_new_device(usb_handle* handle);
85
86/// Checks if interface (device) matches certain criteria
87int recognized_device(usb_handle* handle);
88
89/// Enumerates present and available interfaces (devices), opens new ones and
90/// registers usb transport for them.
91void find_devices();
92
Spencer Lowa5b06b02015-07-22 16:17:07 -070093/// Kicks all USB devices
94static void kick_devices();
95
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096/// Entry point for thread that polls (every second) for new usb interfaces.
97/// This routine calls find_devices in infinite loop.
98void* device_poll_thread(void* unused);
99
100/// Initializes this module
101void usb_init();
102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103/// Opens usb interface (device) by interface (device) name.
104usb_handle* do_usb_open(const wchar_t* interface_name);
105
106/// Writes data to the opened usb handle
107int usb_write(usb_handle* handle, const void* data, int len);
108
109/// Reads data using the opened usb handle
110int usb_read(usb_handle *handle, void* data, int len);
111
112/// Cleans up opened usb handle
113void usb_cleanup_handle(usb_handle* handle);
114
115/// Cleans up (but don't close) opened usb handle
116void usb_kick(usb_handle* handle);
117
118/// Closes opened usb handle
119int usb_close(usb_handle* handle);
120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121int known_device_locked(const char* dev_name) {
122 usb_handle* usb;
123
124 if (NULL != dev_name) {
125 // Iterate through the list looking for the name match.
126 for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
127 // In Windows names are not case sensetive!
128 if((NULL != usb->interface_name) &&
129 (0 == stricmp(usb->interface_name, dev_name))) {
130 return 1;
131 }
132 }
133 }
134
135 return 0;
136}
137
138int known_device(const char* dev_name) {
139 int ret = 0;
140
141 if (NULL != dev_name) {
142 adb_mutex_lock(&usb_lock);
143 ret = known_device_locked(dev_name);
144 adb_mutex_unlock(&usb_lock);
145 }
146
147 return ret;
148}
149
150int register_new_device(usb_handle* handle) {
151 if (NULL == handle)
152 return 0;
153
154 adb_mutex_lock(&usb_lock);
155
156 // Check if device is already in the list
157 if (known_device_locked(handle->interface_name)) {
158 adb_mutex_unlock(&usb_lock);
159 return 0;
160 }
161
162 // Not in the list. Add this handle to the list.
163 handle->next = &handle_list;
164 handle->prev = handle_list.prev;
165 handle->prev->next = handle;
166 handle->next->prev = handle;
167
168 adb_mutex_unlock(&usb_lock);
169
170 return 1;
171}
172
173void* device_poll_thread(void* unused) {
174 D("Created device thread\n");
175
176 while(1) {
177 find_devices();
178 adb_sleep_ms(1000);
179 }
180
181 return NULL;
182}
183
Spencer Lowa5b06b02015-07-22 16:17:07 -0700184static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam,
185 LPARAM lParam) {
186 switch (uMsg) {
187 case WM_POWERBROADCAST:
188 switch (wParam) {
189 case PBT_APMRESUMEAUTOMATIC:
190 // Resuming from sleep or hibernation, so kick all existing USB devices
191 // and then allow the device_poll_thread to redetect USB devices from
192 // scratch. If we don't do this, existing USB devices will never respond
193 // to us because they'll be waiting for the connect/auth handshake.
194 D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, "
195 "so kicking all USB devices\n");
196 kick_devices();
197 return TRUE;
198 }
199 }
200 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
201}
202
203static void* _power_notification_thread(void* unused) {
204 // This uses a thread with its own window message pump to get power
205 // notifications. If adb runs from a non-interactive service account, this
206 // might not work (not sure). If that happens to not work, we could use
207 // heavyweight WMI APIs to get power notifications. But for the common case
208 // of a developer's interactive session, a window message pump is more
209 // appropriate.
210 D("Created power notification thread\n");
211
212 // Window class names are process specific.
213 static const WCHAR kPowerNotificationWindowClassName[] =
214 L"PowerNotificationWindow";
215
216 // Get the HINSTANCE corresponding to the module that _power_window_proc
217 // is in (the main module).
218 const HINSTANCE instance = GetModuleHandleW(NULL);
219 if (!instance) {
220 // This is such a common API call that this should never fail.
221 fatal("GetModuleHandleW failed: %s",
222 SystemErrorCodeToString(GetLastError()).c_str());
223 }
224
225 WNDCLASSEXW wndclass;
226 memset(&wndclass, 0, sizeof(wndclass));
227 wndclass.cbSize = sizeof(wndclass);
228 wndclass.lpfnWndProc = _power_window_proc;
229 wndclass.hInstance = instance;
230 wndclass.lpszClassName = kPowerNotificationWindowClassName;
231 if (!RegisterClassExW(&wndclass)) {
232 fatal("RegisterClassExW failed: %s",
233 SystemErrorCodeToString(GetLastError()).c_str());
234 }
235
236 if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
237 L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
238 NULL, NULL, instance, NULL)) {
239 fatal("CreateWindowExW failed: %s",
240 SystemErrorCodeToString(GetLastError()).c_str());
241 }
242
243 MSG msg;
244 while (GetMessageW(&msg, NULL, 0, 0)) {
245 TranslateMessage(&msg);
246 DispatchMessageW(&msg);
247 }
248
249 // GetMessageW() will return false if a quit message is posted. We don't
250 // do that, but it might be possible for that to occur when logging off or
251 // shutting down. Not a big deal since the whole process will be going away
252 // soon anyway.
253 D("Power notification thread exiting\n");
254
255 return NULL;
256}
257
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258void usb_init() {
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700259 if (!adb_thread_create(device_poll_thread, nullptr)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700260 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 Projectdd7bc332009-03-03 19:32:55 -0800264 }
265}
266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267usb_handle* do_usb_open(const wchar_t* interface_name) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700268 unsigned long name_len = 0;
269
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 // Allocate our handle
Spencer Lowa5b06b02015-07-22 16:17:07 -0700271 usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
272 if (NULL == ret) {
273 D("Could not allocate %u bytes for usb_handle: %s\n", sizeof(usb_handle),
274 strerror(errno));
275 goto fail;
276 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
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 Projectdd7bc332009-03-03 19:32:55 -0800284 if (NULL == ret->adb_interface) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700285 D("AdbCreateInterfaceByName failed: %s\n",
286 SystemErrorCodeToString(GetLastError()).c_str());
287 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 }
289
290 // Open read pipe (endpoint)
291 ret->adb_read_pipe =
292 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
293 AdbOpenAccessTypeReadWrite,
294 AdbOpenSharingModeReadWrite);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700295 if (NULL == ret->adb_read_pipe) {
296 D("AdbOpenDefaultBulkReadEndpoint failed: %s\n",
297 SystemErrorCodeToString(GetLastError()).c_str());
298 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
300
Spencer Lowa5b06b02015-07-22 16:17:07 -0700301 // 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) {
307 D("AdbOpenDefaultBulkWriteEndpoint failed: %s\n",
308 SystemErrorCodeToString(GetLastError()).c_str());
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,
317 true);
318 if (0 == name_len) {
319 D("AdbGetInterfaceName returned name length of zero: %s\n",
320 SystemErrorCodeToString(GetLastError()).c_str());
321 goto fail;
322 }
323
324 ret->interface_name = (char*)malloc(name_len);
325 if (NULL == ret->interface_name) {
326 D("Could not allocate %lu bytes for interface_name: %s\n", name_len,
327 strerror(errno));
328 goto fail;
329 }
330
331 // Now save the name
332 if (!AdbGetInterfaceName(ret->adb_interface,
333 ret->interface_name,
334 &name_len,
335 true)) {
336 D("AdbGetInterfaceName failed: %s\n",
337 SystemErrorCodeToString(GetLastError()).c_str());
338 goto fail;
339 }
340
341 // We're done at this point
342 return ret;
343
344fail:
345 if (NULL != ret) {
346 usb_cleanup_handle(ret);
347 free(ret);
348 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
350 return NULL;
351}
352
353int usb_write(usb_handle* handle, const void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800354 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 unsigned long written = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700356 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357
358 D("usb_write %d\n", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700359 if (NULL == handle) {
360 D("usb_write was passed NULL handle\n");
361 err = EINVAL;
362 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 }
364
Spencer Lowa5b06b02015-07-22 16:17:07 -0700365 // Perform write
366 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
367 (void*)data,
368 (unsigned long)len,
369 &written,
370 time_out)) {
371 D("AdbWriteEndpointSync failed: %s\n",
372 SystemErrorCodeToString(GetLastError()).c_str());
373 err = EIO;
374 goto fail;
375 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Spencer Lowa5b06b02015-07-22 16:17:07 -0700377 // Make sure that we've written what we were asked to write
378 D("usb_write got: %ld, expected: %d\n", written, len);
379 if (written != (unsigned long)len) {
380 // If this occurs, this code should be changed to repeatedly call
381 // AdbWriteEndpointSync() until all bytes are written.
382 D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld\n",
383 len, written);
384 err = EIO;
385 goto fail;
386 }
387
388 if (handle->zero_mask && (len & handle->zero_mask) == 0) {
389 // Send a zero length packet
390 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
391 (void*)data,
392 0,
393 &written,
394 time_out)) {
395 D("AdbWriteEndpointSync of zero length packet failed: %s\n",
396 SystemErrorCodeToString(GetLastError()).c_str());
397 err = EIO;
398 goto fail;
399 }
400 }
401
402 return 0;
403
404fail:
405 // Any failure should cause us to kick the device instead of leaving it a
406 // zombie state with potential to hang.
407 if (NULL != handle) {
408 D("Kicking device due to error in usb_write\n");
409 usb_kick(handle);
410 }
411
412 D("usb_write failed\n");
413 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 return -1;
415}
416
417int usb_read(usb_handle *handle, void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800418 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 unsigned long read = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700420 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
422 D("usb_read %d\n", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700423 if (NULL == handle) {
424 D("usb_read was passed NULL handle\n");
425 err = EINVAL;
426 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 }
428
Spencer Lowa5b06b02015-07-22 16:17:07 -0700429 while (len > 0) {
430 if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
431 time_out)) {
432 D("AdbReadEndpointSync failed: %s\n",
433 SystemErrorCodeToString(GetLastError()).c_str());
434 err = EIO;
435 goto fail;
436 }
437 D("usb_read got: %ld, expected: %d\n", read, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438
Spencer Lowa5b06b02015-07-22 16:17:07 -0700439 data = (char *)data + read;
440 len -= read;
441 }
442
443 return 0;
444
445fail:
446 // Any failure should cause us to kick the device instead of leaving it a
447 // zombie state with potential to hang.
448 if (NULL != handle) {
449 D("Kicking device due to error in usb_read\n");
450 usb_kick(handle);
451 }
452
453 D("usb_read failed\n");
454 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return -1;
456}
457
Spencer Lowa5b06b02015-07-22 16:17:07 -0700458// Wrapper around AdbCloseHandle() that logs diagnostics.
459static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
460 if (!AdbCloseHandle(adb_handle)) {
461 D("AdbCloseHandle(%p) failed: %s\n", adb_handle,
462 SystemErrorCodeToString(GetLastError()).c_str());
463 }
464}
465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466void usb_cleanup_handle(usb_handle* handle) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700467 D("usb_cleanup_handle\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 if (NULL != handle) {
469 if (NULL != handle->interface_name)
470 free(handle->interface_name);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700471 // AdbCloseHandle(pipe) will break any threads out of pending IO calls and
472 // wait until the pipe no longer uses the interface. Then we can
473 // AdbCloseHandle() the interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 if (NULL != handle->adb_write_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700475 _adb_close_handle(handle->adb_write_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 if (NULL != handle->adb_read_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700477 _adb_close_handle(handle->adb_read_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 if (NULL != handle->adb_interface)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700479 _adb_close_handle(handle->adb_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480
481 handle->interface_name = NULL;
482 handle->adb_write_pipe = NULL;
483 handle->adb_read_pipe = NULL;
484 handle->adb_interface = NULL;
485 }
486}
487
Spencer Lowa5b06b02015-07-22 16:17:07 -0700488static void usb_kick_locked(usb_handle* handle) {
489 // The reason the lock must be acquired before calling this function is in
490 // case multiple threads are trying to kick the same device at the same time.
491 usb_cleanup_handle(handle);
492}
493
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494void usb_kick(usb_handle* handle) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700495 D("usb_kick\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 if (NULL != handle) {
497 adb_mutex_lock(&usb_lock);
498
Spencer Lowa5b06b02015-07-22 16:17:07 -0700499 usb_kick_locked(handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500
501 adb_mutex_unlock(&usb_lock);
502 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700503 errno = EINVAL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
505}
506
507int usb_close(usb_handle* handle) {
508 D("usb_close\n");
509
510 if (NULL != handle) {
511 // Remove handle from the list
512 adb_mutex_lock(&usb_lock);
513
514 if ((handle->next != handle) && (handle->prev != handle)) {
515 handle->next->prev = handle->prev;
516 handle->prev->next = handle->next;
517 handle->prev = handle;
518 handle->next = handle;
519 }
520
521 adb_mutex_unlock(&usb_lock);
522
523 // Cleanup handle
524 usb_cleanup_handle(handle);
525 free(handle);
526 }
527
528 return 0;
529}
530
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531int recognized_device(usb_handle* handle) {
532 if (NULL == handle)
533 return 0;
534
535 // Check vendor and product id first
536 USB_DEVICE_DESCRIPTOR device_desc;
537
538 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
539 &device_desc)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700540 D("AdbGetUsbDeviceDescriptor failed: %s\n",
541 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 return 0;
543 }
544
545 // Then check interface properties
546 USB_INTERFACE_DESCRIPTOR interf_desc;
547
548 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
549 &interf_desc)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700550 D("AdbGetUsbInterfaceDescriptor failed: %s\n",
551 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552 return 0;
553 }
554
555 // Must have two endpoints
556 if (2 != interf_desc.bNumEndpoints) {
557 return 0;
558 }
559
560 if (is_adb_interface(device_desc.idVendor, device_desc.idProduct,
561 interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) {
562
563 if(interf_desc.bInterfaceProtocol == 0x01) {
564 AdbEndpointInformation endpoint_info;
565 // assuming zero is a valid bulk endpoint ID
566 if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
567 handle->zero_mask = endpoint_info.max_packet_size - 1;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700568 D("device zero_mask: 0x%x\n", handle->zero_mask);
569 } else {
570 D("AdbGetEndpointInformation failed: %s\n",
571 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 }
573 }
574
575 return 1;
576 }
577
578 return 0;
579}
580
581void find_devices() {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200582 usb_handle* handle = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 char entry_buffer[2048];
584 char interf_name[2048];
585 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
586 unsigned long entry_buffer_size = sizeof(entry_buffer);
587 char* copy_name;
588
589 // Enumerate all present and active interfaces.
590 ADBAPIHANDLE enum_handle =
591 AdbEnumInterfaces(usb_class_id, true, true, true);
592
Spencer Lowa5b06b02015-07-22 16:17:07 -0700593 if (NULL == enum_handle) {
594 D("AdbEnumInterfaces failed: %s\n",
595 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 return;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700597 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598
599 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
600 // TODO: FIXME - temp hack converting wchar_t into char.
601 // It would be better to change AdbNextInterface so it will return
602 // interface name as single char string.
603 const wchar_t* wchar_name = next_interface->device_name;
604 for(copy_name = interf_name;
605 L'\0' != *wchar_name;
606 wchar_name++, copy_name++) {
607 *copy_name = (char)(*wchar_name);
608 }
609 *copy_name = '\0';
610
611 // Lets see if we already have this device in the list
612 if (!known_device(interf_name)) {
613 // This seems to be a new device. Open it!
614 handle = do_usb_open(next_interface->device_name);
615 if (NULL != handle) {
616 // Lets see if this interface (device) belongs to us
617 if (recognized_device(handle)) {
618 D("adding a new device %s\n", interf_name);
619 char serial_number[512];
620 unsigned long serial_number_len = sizeof(serial_number);
621 if (AdbGetSerialNumber(handle->adb_interface,
622 serial_number,
623 &serial_number_len,
624 true)) {
625 // Lets make sure that we don't duplicate this device
626 if (register_new_device(handle)) {
Scott Andersone109d262012-04-20 11:21:14 -0700627 register_usb_transport(handle, serial_number, NULL, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 } else {
629 D("register_new_device failed for %s\n", interf_name);
630 usb_cleanup_handle(handle);
631 free(handle);
632 }
633 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700634 D("cannot get serial number: %s\n",
635 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 usb_cleanup_handle(handle);
637 free(handle);
638 }
639 } else {
640 usb_cleanup_handle(handle);
641 free(handle);
642 }
643 }
644 }
645
646 entry_buffer_size = sizeof(entry_buffer);
647 }
648
Spencer Lowa5b06b02015-07-22 16:17:07 -0700649 if (GetLastError() != ERROR_NO_MORE_ITEMS) {
650 // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
651 D("AdbNextInterface failed: %s\n",
652 SystemErrorCodeToString(GetLastError()).c_str());
653 }
654
655 _adb_close_handle(enum_handle);
656}
657
658static void kick_devices() {
659 // Need to acquire lock to safely walk the list which might be modified
660 // by another thread.
661 adb_mutex_lock(&usb_lock);
662 for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
663 usb_kick_locked(usb);
664 }
665 adb_mutex_unlock(&usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666}