File: adb_api.cpp

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (533 lines) | stat: -rw-r--r-- 18,109 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** \file
  This file consists of implementation of rotines that are exported
  from this DLL.
*/

#include "stdafx.h"
#include "adb_api.h"
#include "adb_object_handle.h"
#include "adb_interface_enum.h"
#include "adb_interface.h"
#include "adb_legacy_interface.h"
#include "adb_endpoint_object.h"
#include "adb_io_completion.h"
#include "adb_helper_routines.h"
#include "adb_winusb_api.h"

/** \brief Points to InstantiateWinUsbInterface exported from AdbWinUsbApi.dll.

  This variable is initialized with the actual address in DllMain routine for
  this DLL on DLL_PROCESS_ATTACH event.
  @see PFN_INSTWINUSBINTERFACE for more information.
*/
PFN_INSTWINUSBINTERFACE InstantiateWinUsbInterface = NULL;

ADBAPIHANDLE __cdecl AdbEnumInterfaces(GUID class_id,
                               bool exclude_not_present,
                               bool exclude_removed,
                               bool active_only) {
  AdbInterfaceEnumObject* enum_obj = NULL;
  ADBAPIHANDLE ret = NULL;

  try {
    // Instantiate and initialize enum object
    enum_obj = new AdbInterfaceEnumObject();

    if (enum_obj->InitializeEnum(class_id,
                                 exclude_not_present,
                                 exclude_removed,
                                 active_only)) {
      // After successful initialization we can create handle.
      ret = enum_obj->CreateHandle();
    }
  } catch (...) {
    SetLastError(ERROR_OUTOFMEMORY);
  }

  if (NULL != enum_obj)
    enum_obj->Release();

  return ret;
}

bool __cdecl AdbNextInterface(ADBAPIHANDLE adb_handle,
                      AdbInterfaceInfo* info,
                      unsigned long* size) {
  if (NULL == size) {
    SetLastError(ERROR_INVALID_PARAMETER);
    return false;
  }

  // Lookup AdbInterfaceEnumObject object for the handle
  AdbInterfaceEnumObject* adb_ienum_object =
    LookupObject<AdbInterfaceEnumObject>(adb_handle);
  if (NULL == adb_ienum_object)
    return false;

  // Everything is verified. Pass it down to the object
  bool ret = adb_ienum_object->Next(info, size);

  adb_ienum_object->Release();

  return ret;
}

bool __cdecl AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle) {
  // Lookup AdbInterfaceEnumObject object for the handle
  AdbInterfaceEnumObject* adb_ienum_object =
    LookupObject<AdbInterfaceEnumObject>(adb_handle);
  if (NULL == adb_ienum_object)
    return false;

  // Everything is verified. Pass it down to the object
  bool ret = adb_ienum_object->Reset();

  adb_ienum_object->Release();

  return ret;
}

ADBAPIHANDLE __cdecl AdbCreateInterfaceByName(
    const wchar_t* interface_name) {
  AdbInterfaceObject* obj = NULL;
  ADBAPIHANDLE ret = NULL;

  try {
    // Instantiate interface object, depending on the USB driver type.
    if (IsLegacyInterface(interface_name)) {
      // We have legacy USB driver underneath us.
      obj = new AdbLegacyInterfaceObject(interface_name);
    } else {
      // We have WinUsb driver underneath us. Make sure that AdbWinUsbApi.dll
      // is loaded and its InstantiateWinUsbInterface routine address has
      // been cached.
      if (NULL != InstantiateWinUsbInterface) {
        obj = InstantiateWinUsbInterface(interface_name);
        if (NULL == obj) {
          return NULL;
        }
      } else {
        return NULL;
      }
    }

    // Create handle for it
    ret = obj->CreateHandle();
  } catch (...) {
    SetLastError(ERROR_OUTOFMEMORY);
  }

  if (NULL != obj)
    obj->Release();

  return ret;
}

ADBAPIHANDLE __cdecl AdbCreateInterface(GUID class_id,
                                unsigned short vendor_id,
                                unsigned short product_id,
                                unsigned char interface_id) {
  // Enumerate all active interfaces for the given class
  AdbEnumInterfaceArray interfaces;

  if (!EnumerateDeviceInterfaces(class_id,
                                 DIGCF_DEVICEINTERFACE | DIGCF_PRESENT,
                                 true,
                                 true,
                                 &interfaces)) {
    return NULL;
  }

  if (interfaces.empty()) {
    SetLastError(ERROR_DEVICE_NOT_AVAILABLE);
    return NULL;
  }

  // Now iterate over active interfaces looking for the name match.
  // The name is formatted as such:
  // "\\\\?\\usb#vid_xxxx&pid_xxxx&mi_xx#123456789abcdef#{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
  // where
  //    vid_xxxx is for the vendor id (xxxx are hex for the given vendor id),
  //    pid_xxxx is for the product id (xxxx are hex for the given product id)
  //    mi_xx is for the interface id  (xx are hex for the given interface id)
  // EnumerateDeviceInterfaces will guarantee that returned interface names
  // will have our class id at the end of the name (those last XXXes in the
  // format). So, we only need to match the beginning of the name
  wchar_t match_name[64];
  if (0xFF == interface_id) {
    // No interface id for the name.
    swprintf(match_name, L"\\\\?\\usb#vid_%04x&pid_%04x#",
             vendor_id, product_id);
  } else {
    // With interface id for the name.
    swprintf(match_name, L"\\\\?\\usb#vid_%04x&pid_%04x&mi_%02x#",
             vendor_id, product_id, interface_id);
  }
  size_t match_len = wcslen(match_name);

  for (AdbEnumInterfaceArray::iterator it = interfaces.begin();
       it != interfaces.end(); it++) {
    const AdbInstanceEnumEntry& next_interface = *it;
    if (0 == _wcsnicmp(match_name,
                      next_interface.device_name().c_str(),
                      match_len)) {
      // Found requested interface among active interfaces.
      return AdbCreateInterfaceByName(next_interface.device_name().c_str());
    }
  }

  SetLastError(ERROR_DEVICE_NOT_AVAILABLE);
  return NULL;
}

bool __cdecl AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
                         void* buffer,
                         unsigned long* buffer_char_size,
                         bool ansi) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch call to the found object
    bool ret = adb_object->GetInterfaceName(buffer, buffer_char_size, ansi);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
                        void* buffer,
                        unsigned long* buffer_char_size,
                        bool ansi) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch call to the found object
    bool ret = adb_object->GetSerialNumber(buffer, buffer_char_size, ansi);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
                               USB_DEVICE_DESCRIPTOR* desc) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch close to the found object
    bool ret = adb_object->GetUsbDeviceDescriptor(desc);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetUsbConfigurationDescriptor(ADBAPIHANDLE adb_interface,
                                      USB_CONFIGURATION_DESCRIPTOR* desc) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch close to the found object
    bool ret = adb_object->GetUsbConfigurationDescriptor(desc);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
                                  USB_INTERFACE_DESCRIPTOR* desc) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch close to the found object
    bool ret = adb_object->GetUsbInterfaceDescriptor(desc);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
                               UCHAR endpoint_index,
                               AdbEndpointInformation* info) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch close to the found object
    bool ret = adb_object->GetEndpointInformation(endpoint_index, info);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetDefaultBulkReadEndpointInformation(ADBAPIHANDLE adb_interface,
                                              AdbEndpointInformation* info) {
  return AdbGetEndpointInformation(adb_interface,
                                   ADB_QUERY_BULK_READ_ENDPOINT_INDEX,
                                   info);
}

bool __cdecl AdbGetDefaultBulkWriteEndpointInformation(ADBAPIHANDLE adb_interface,
                                               AdbEndpointInformation* info) {
  return AdbGetEndpointInformation(adb_interface,
                                   ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX,
                                   info);
}

ADBAPIHANDLE __cdecl AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
                             unsigned char endpoint_index,
                             AdbOpenAccessType access_type,
                             AdbOpenSharingMode sharing_mode) {
  // Lookup interface object for the handle
  AdbInterfaceObject* adb_object =
    LookupObject<AdbInterfaceObject>(adb_interface);

  if (NULL != adb_object) {
    // Dispatch close to the found object
    ADBAPIHANDLE ret =
      adb_object->OpenEndpoint(endpoint_index, access_type, sharing_mode);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return NULL;
  }
}

ADBAPIHANDLE __cdecl AdbOpenDefaultBulkReadEndpoint(ADBAPIHANDLE adb_interface,
                                            AdbOpenAccessType access_type,
                                            AdbOpenSharingMode sharing_mode) {
  return AdbOpenEndpoint(adb_interface,
                         ADB_QUERY_BULK_READ_ENDPOINT_INDEX,
                         access_type,
                         sharing_mode);
}

ADBAPIHANDLE __cdecl AdbOpenDefaultBulkWriteEndpoint(ADBAPIHANDLE adb_interface,
                                             AdbOpenAccessType access_type,
                                             AdbOpenSharingMode sharing_mode) {
  return AdbOpenEndpoint(adb_interface,
                         ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX,
                         access_type,
                         sharing_mode);
}

ADBAPIHANDLE __cdecl AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint) {
  // Lookup endpoint object for the handle
  AdbEndpointObject* adb_object =
    LookupObject<AdbEndpointObject>(adb_endpoint);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    ADBAPIHANDLE ret = adb_object->GetParentInterfaceHandle();
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return NULL;
  }
}

bool __cdecl AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
                                 AdbEndpointInformation* info) {
  // Lookup endpoint object for the handle
  AdbEndpointObject* adb_object =
    LookupObject<AdbEndpointObject>(adb_endpoint);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    bool ret = adb_object->GetEndpointInformation(info);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

ADBAPIHANDLE __cdecl AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
                                  void* buffer,
                                  unsigned long bytes_to_read,
                                  unsigned long* bytes_read,
                                  unsigned long time_out,
                                  HANDLE event_handle) {
  // Lookup endpoint object for the handle
  AdbEndpointObject* adb_object =
    LookupObject<AdbEndpointObject>(adb_endpoint);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    ADBAPIHANDLE ret = adb_object->AsyncRead(buffer,
                                             bytes_to_read,
                                             bytes_read,
                                             event_handle,
                                             time_out);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return NULL;
  }
}

ADBAPIHANDLE __cdecl AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
                                   void* buffer,
                                   unsigned long bytes_to_write,
                                   unsigned long* bytes_written,
                                   unsigned long time_out,
                                   HANDLE event_handle) {
  // Lookup endpoint object for the handle
  AdbEndpointObject* adb_object =
    LookupObject<AdbEndpointObject>(adb_endpoint);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    ADBAPIHANDLE ret = adb_object->AsyncWrite(buffer,
                                              bytes_to_write,
                                              bytes_written,
                                              event_handle,
                                              time_out);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
                         void* buffer,
                         unsigned long bytes_to_read,
                         unsigned long* bytes_read,
                         unsigned long time_out) {
  // Lookup endpoint object for the handle
  AdbEndpointObject* adb_object =
    LookupObject<AdbEndpointObject>(adb_endpoint);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    bool ret =
      adb_object->SyncRead(buffer, bytes_to_read, bytes_read, time_out);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return NULL;
  }
}

bool __cdecl AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
                          void* buffer,
                          unsigned long bytes_to_write,
                          unsigned long* bytes_written,
                          unsigned long time_out) {
  // Lookup endpoint object for the handle
  AdbEndpointObject* adb_object =
    LookupObject<AdbEndpointObject>(adb_endpoint);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    bool ret =
      adb_object->SyncWrite(buffer, bytes_to_write, bytes_written, time_out);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
                             LPOVERLAPPED overlapped,
                             unsigned long* bytes_transferred,
                             bool wait) {
  // Lookup endpoint object for the handle
  AdbIOCompletion* adb_object =
    LookupObject<AdbIOCompletion>(adb_io_completion);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    bool ret =
      adb_object->GetOvelappedIoResult(overlapped, bytes_transferred, wait);
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}

bool __cdecl AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion) {
  // Lookup endpoint object for the handle
  AdbIOCompletion* adb_object =
    LookupObject<AdbIOCompletion>(adb_io_completion);

  if (NULL != adb_object) {
    // Dispatch the call to the found object
    bool ret =
      adb_object->IsCompleted();
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return true;
  }
}

bool __cdecl AdbCloseHandle(ADBAPIHANDLE adb_handle) {
  // Lookup object for the handle
  AdbObjectHandle* adb_object = AdbObjectHandle::Lookup(adb_handle);

  if (NULL != adb_object) {
    // Dispatch close to the found object
    bool ret = adb_object->CloseHandle();
    adb_object->Release();
    return ret;
  } else {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
}