adb_api.cpp revision 3e44f3b231c027f01290367049f2244514f22d16
1/*
2 * Copyright (C) 2006 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
17/** \file
18  This file consists of implementation of rotines that are exported
19  from this DLL.
20*/
21
22#include "stdafx.h"
23#include "adb_api.h"
24#include "adb_object_handle.h"
25#include "adb_interface_enum.h"
26#include "adb_interface.h"
27#include "adb_winusb_interface.h"
28#include "adb_legacy_interface.h"
29#include "adb_endpoint_object.h"
30#include "adb_io_completion.h"
31#include "adb_helper_routines.h"
32
33ADBAPIHANDLE __cdecl AdbEnumInterfaces(GUID class_id,
34                               bool exclude_not_present,
35                               bool exclude_removed,
36                               bool active_only) {
37  AdbInterfaceEnumObject* enum_obj = NULL;
38  ADBAPIHANDLE ret = NULL;
39
40  try {
41    // Instantiate and initialize enum object
42    enum_obj = new AdbInterfaceEnumObject();
43
44    if (enum_obj->InitializeEnum(class_id,
45                                 exclude_not_present,
46                                 exclude_removed,
47                                 active_only)) {
48      // After successful initialization we can create handle.
49      ret = enum_obj->CreateHandle();
50    }
51  } catch (...) {
52    SetLastError(ERROR_OUTOFMEMORY);
53  }
54
55  if (NULL != enum_obj)
56    enum_obj->Release();
57
58  return ret;
59}
60
61bool __cdecl AdbNextInterface(ADBAPIHANDLE adb_handle,
62                      AdbInterfaceInfo* info,
63                      unsigned long* size) {
64  if (NULL == size) {
65    SetLastError(ERROR_INVALID_PARAMETER);
66    return false;
67  }
68
69  // Lookup AdbInterfaceEnumObject object for the handle
70  AdbInterfaceEnumObject* adb_ienum_object =
71    LookupObject<AdbInterfaceEnumObject>(adb_handle);
72  if (NULL == adb_ienum_object)
73    return false;
74
75  // Everything is verified. Pass it down to the object
76  bool ret = adb_ienum_object->Next(info, size);
77
78  adb_ienum_object->Release();
79
80  return ret;
81}
82
83bool __cdecl AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle) {
84  // Lookup AdbInterfaceEnumObject object for the handle
85  AdbInterfaceEnumObject* adb_ienum_object =
86    LookupObject<AdbInterfaceEnumObject>(adb_handle);
87  if (NULL == adb_ienum_object)
88    return false;
89
90  // Everything is verified. Pass it down to the object
91  bool ret = adb_ienum_object->Reset();
92
93  adb_ienum_object->Release();
94
95  return ret;
96}
97
98ADBAPIHANDLE __cdecl AdbCreateInterfaceByName(
99    const wchar_t* interface_name) {
100  AdbInterfaceObject* obj = NULL;
101  ADBAPIHANDLE ret = NULL;
102
103  try {
104    // Instantiate object
105    if (IsLegacyInterface(interface_name)) {
106      obj = new AdbLegacyInterfaceObject(interface_name);
107    } else {
108      obj = new AdbWinUsbInterfaceObject(interface_name);
109    }
110
111    // Create handle for it
112    ret = obj->CreateHandle();
113  } catch (...) {
114    SetLastError(ERROR_OUTOFMEMORY);
115  }
116
117  if (NULL != obj)
118    obj->Release();
119
120  return ret;
121}
122
123ADBAPIHANDLE __cdecl AdbCreateInterface(GUID class_id,
124                                unsigned short vendor_id,
125                                unsigned short product_id,
126                                unsigned char interface_id) {
127  // Enumerate all active interfaces for the given class
128  AdbEnumInterfaceArray interfaces;
129
130  if (!EnumerateDeviceInterfaces(class_id,
131                                 DIGCF_DEVICEINTERFACE | DIGCF_PRESENT,
132                                 true,
133                                 true,
134                                 &interfaces)) {
135    return NULL;
136  }
137
138  if (interfaces.empty()) {
139    SetLastError(ERROR_DEVICE_NOT_AVAILABLE);
140    return NULL;
141  }
142
143  // Now iterate over active interfaces looking for the name match.
144  // The name is formatted as such:
145  // "\\\\?\\usb#vid_xxxx&pid_xxxx&mi_xx#123456789abcdef#{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
146  // where
147  //    vid_xxxx is for the vendor id (xxxx are hex for the given vendor id),
148  //    pid_xxxx is for the product id (xxxx are hex for the given product id)
149  //    mi_xx is for the interface id  (xx are hex for the given interface id)
150  // EnumerateDeviceInterfaces will guarantee that returned interface names
151  // will have our class id at the end of the name (those last XXXes in the
152  // format). So, we only need to match the beginning of the name
153  wchar_t match_name[64];
154  if (0xFF == interface_id) {
155    // No interface id for the name.
156    swprintf(match_name, L"\\\\?\\usb#vid_%04x&pid_%04x#",
157             vendor_id, product_id);
158  } else {
159    // With interface id for the name.
160    swprintf(match_name, L"\\\\?\\usb#vid_%04x&pid_%04x&mi_%02x#",
161             vendor_id, product_id, interface_id);
162  }
163  size_t match_len = wcslen(match_name);
164
165  for (AdbEnumInterfaceArray::iterator it = interfaces.begin();
166       it != interfaces.end(); it++) {
167    const AdbInstanceEnumEntry& next_interface = *it;
168    if (0 == _wcsnicmp(match_name,
169                      next_interface.device_name().c_str(),
170                      match_len)) {
171      // Found requested interface among active interfaces.
172      return AdbCreateInterfaceByName(next_interface.device_name().c_str());
173    }
174  }
175
176  SetLastError(ERROR_DEVICE_NOT_AVAILABLE);
177  return NULL;
178}
179
180bool __cdecl AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
181                         void* buffer,
182                         unsigned long* buffer_char_size,
183                         bool ansi) {
184  // Lookup interface object for the handle
185  AdbInterfaceObject* adb_object =
186    LookupObject<AdbInterfaceObject>(adb_interface);
187
188  if (NULL != adb_object) {
189    // Dispatch call to the found object
190    bool ret = adb_object->GetInterfaceName(buffer, buffer_char_size, ansi);
191    adb_object->Release();
192    return ret;
193  } else {
194    SetLastError(ERROR_INVALID_HANDLE);
195    return false;
196  }
197}
198
199bool __cdecl AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
200                        void* buffer,
201                        unsigned long* buffer_char_size,
202                        bool ansi) {
203  // Lookup interface object for the handle
204  AdbInterfaceObject* adb_object =
205    LookupObject<AdbInterfaceObject>(adb_interface);
206
207  if (NULL != adb_object) {
208    // Dispatch call to the found object
209    bool ret = adb_object->GetSerialNumber(buffer, buffer_char_size, ansi);
210    adb_object->Release();
211    return ret;
212  } else {
213    SetLastError(ERROR_INVALID_HANDLE);
214    return false;
215  }
216}
217
218bool __cdecl AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
219                               USB_DEVICE_DESCRIPTOR* desc) {
220  // Lookup interface object for the handle
221  AdbInterfaceObject* adb_object =
222    LookupObject<AdbInterfaceObject>(adb_interface);
223
224  if (NULL != adb_object) {
225    // Dispatch close to the found object
226    bool ret = adb_object->GetUsbDeviceDescriptor(desc);
227    adb_object->Release();
228    return ret;
229  } else {
230    SetLastError(ERROR_INVALID_HANDLE);
231    return false;
232  }
233}
234
235bool __cdecl AdbGetUsbConfigurationDescriptor(ADBAPIHANDLE adb_interface,
236                                      USB_CONFIGURATION_DESCRIPTOR* desc) {
237  // Lookup interface object for the handle
238  AdbInterfaceObject* adb_object =
239    LookupObject<AdbInterfaceObject>(adb_interface);
240
241  if (NULL != adb_object) {
242    // Dispatch close to the found object
243    bool ret = adb_object->GetUsbConfigurationDescriptor(desc);
244    adb_object->Release();
245    return ret;
246  } else {
247    SetLastError(ERROR_INVALID_HANDLE);
248    return false;
249  }
250}
251
252bool __cdecl AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
253                                  USB_INTERFACE_DESCRIPTOR* desc) {
254  // Lookup interface object for the handle
255  AdbInterfaceObject* adb_object =
256    LookupObject<AdbInterfaceObject>(adb_interface);
257
258  if (NULL != adb_object) {
259    // Dispatch close to the found object
260    bool ret = adb_object->GetUsbInterfaceDescriptor(desc);
261    adb_object->Release();
262    return ret;
263  } else {
264    SetLastError(ERROR_INVALID_HANDLE);
265    return false;
266  }
267}
268
269bool __cdecl AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
270                               UCHAR endpoint_index,
271                               AdbEndpointInformation* info) {
272  // Lookup interface object for the handle
273  AdbInterfaceObject* adb_object =
274    LookupObject<AdbInterfaceObject>(adb_interface);
275
276  if (NULL != adb_object) {
277    // Dispatch close to the found object
278    bool ret = adb_object->GetEndpointInformation(endpoint_index, info);
279    adb_object->Release();
280    return ret;
281  } else {
282    SetLastError(ERROR_INVALID_HANDLE);
283    return false;
284  }
285}
286
287bool __cdecl AdbGetDefaultBulkReadEndpointInformation(ADBAPIHANDLE adb_interface,
288                                              AdbEndpointInformation* info) {
289  return AdbGetEndpointInformation(adb_interface,
290                                   ADB_QUERY_BULK_READ_ENDPOINT_INDEX,
291                                   info);
292}
293
294bool __cdecl AdbGetDefaultBulkWriteEndpointInformation(ADBAPIHANDLE adb_interface,
295                                               AdbEndpointInformation* info) {
296  return AdbGetEndpointInformation(adb_interface,
297                                   ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX,
298                                   info);
299}
300
301ADBAPIHANDLE __cdecl AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
302                             unsigned char endpoint_index,
303                             AdbOpenAccessType access_type,
304                             AdbOpenSharingMode sharing_mode) {
305  // Lookup interface object for the handle
306  AdbInterfaceObject* adb_object =
307    LookupObject<AdbInterfaceObject>(adb_interface);
308
309  if (NULL != adb_object) {
310    // Dispatch close to the found object
311    ADBAPIHANDLE ret =
312      adb_object->OpenEndpoint(endpoint_index, access_type, sharing_mode);
313    adb_object->Release();
314    return ret;
315  } else {
316    SetLastError(ERROR_INVALID_HANDLE);
317    return NULL;
318  }
319}
320
321ADBAPIHANDLE __cdecl AdbOpenDefaultBulkReadEndpoint(ADBAPIHANDLE adb_interface,
322                                            AdbOpenAccessType access_type,
323                                            AdbOpenSharingMode sharing_mode) {
324  return AdbOpenEndpoint(adb_interface,
325                         ADB_QUERY_BULK_READ_ENDPOINT_INDEX,
326                         access_type,
327                         sharing_mode);
328}
329
330ADBAPIHANDLE __cdecl AdbOpenDefaultBulkWriteEndpoint(ADBAPIHANDLE adb_interface,
331                                             AdbOpenAccessType access_type,
332                                             AdbOpenSharingMode sharing_mode) {
333  return AdbOpenEndpoint(adb_interface,
334                         ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX,
335                         access_type,
336                         sharing_mode);
337}
338
339ADBAPIHANDLE __cdecl AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint) {
340  // Lookup endpoint object for the handle
341  AdbEndpointObject* adb_object =
342    LookupObject<AdbEndpointObject>(adb_endpoint);
343
344  if (NULL != adb_object) {
345    // Dispatch the call to the found object
346    ADBAPIHANDLE ret = adb_object->GetParentInterfaceHandle();
347    adb_object->Release();
348    return ret;
349  } else {
350    SetLastError(ERROR_INVALID_HANDLE);
351    return NULL;
352  }
353}
354
355bool __cdecl AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
356                                 AdbEndpointInformation* info) {
357  // Lookup endpoint object for the handle
358  AdbEndpointObject* adb_object =
359    LookupObject<AdbEndpointObject>(adb_endpoint);
360
361  if (NULL != adb_object) {
362    // Dispatch the call to the found object
363    bool ret = adb_object->GetEndpointInformation(info);
364    adb_object->Release();
365    return ret;
366  } else {
367    SetLastError(ERROR_INVALID_HANDLE);
368    return false;
369  }
370}
371
372ADBAPIHANDLE __cdecl AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
373                                  void* buffer,
374                                  unsigned long bytes_to_read,
375                                  unsigned long* bytes_read,
376                                  unsigned long time_out,
377                                  HANDLE event_handle) {
378  // Lookup endpoint object for the handle
379  AdbEndpointObject* adb_object =
380    LookupObject<AdbEndpointObject>(adb_endpoint);
381
382  if (NULL != adb_object) {
383    // Dispatch the call to the found object
384    ADBAPIHANDLE ret = adb_object->AsyncRead(buffer,
385                                             bytes_to_read,
386                                             bytes_read,
387                                             event_handle,
388                                             time_out);
389    adb_object->Release();
390    return ret;
391  } else {
392    SetLastError(ERROR_INVALID_HANDLE);
393    return NULL;
394  }
395}
396
397ADBAPIHANDLE __cdecl AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
398                                   void* buffer,
399                                   unsigned long bytes_to_write,
400                                   unsigned long* bytes_written,
401                                   unsigned long time_out,
402                                   HANDLE event_handle) {
403  // Lookup endpoint object for the handle
404  AdbEndpointObject* adb_object =
405    LookupObject<AdbEndpointObject>(adb_endpoint);
406
407  if (NULL != adb_object) {
408    // Dispatch the call to the found object
409    ADBAPIHANDLE ret = adb_object->AsyncWrite(buffer,
410                                              bytes_to_write,
411                                              bytes_written,
412                                              event_handle,
413                                              time_out);
414    adb_object->Release();
415    return ret;
416  } else {
417    SetLastError(ERROR_INVALID_HANDLE);
418    return false;
419  }
420}
421
422bool __cdecl AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
423                         void* buffer,
424                         unsigned long bytes_to_read,
425                         unsigned long* bytes_read,
426                         unsigned long time_out) {
427  // Lookup endpoint object for the handle
428  AdbEndpointObject* adb_object =
429    LookupObject<AdbEndpointObject>(adb_endpoint);
430
431  if (NULL != adb_object) {
432    // Dispatch the call to the found object
433    bool ret =
434      adb_object->SyncRead(buffer, bytes_to_read, bytes_read, time_out);
435    adb_object->Release();
436    return ret;
437  } else {
438    SetLastError(ERROR_INVALID_HANDLE);
439    return NULL;
440  }
441}
442
443bool __cdecl AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
444                          void* buffer,
445                          unsigned long bytes_to_write,
446                          unsigned long* bytes_written,
447                          unsigned long time_out) {
448  // Lookup endpoint object for the handle
449  AdbEndpointObject* adb_object =
450    LookupObject<AdbEndpointObject>(adb_endpoint);
451
452  if (NULL != adb_object) {
453    // Dispatch the call to the found object
454    bool ret =
455      adb_object->SyncWrite(buffer, bytes_to_write, bytes_written, time_out);
456    adb_object->Release();
457    return ret;
458  } else {
459    SetLastError(ERROR_INVALID_HANDLE);
460    return false;
461  }
462}
463
464bool __cdecl AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
465                             LPOVERLAPPED overlapped,
466                             unsigned long* bytes_transferred,
467                             bool wait) {
468  // Lookup endpoint object for the handle
469  AdbIOCompletion* adb_object =
470    LookupObject<AdbIOCompletion>(adb_io_completion);
471
472  if (NULL != adb_object) {
473    // Dispatch the call to the found object
474    bool ret =
475      adb_object->GetOvelappedIoResult(overlapped, bytes_transferred, wait);
476    adb_object->Release();
477    return ret;
478  } else {
479    SetLastError(ERROR_INVALID_HANDLE);
480    return false;
481  }
482}
483
484bool __cdecl AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion) {
485  // Lookup endpoint object for the handle
486  AdbIOCompletion* adb_object =
487    LookupObject<AdbIOCompletion>(adb_io_completion);
488
489  if (NULL != adb_object) {
490    // Dispatch the call to the found object
491    bool ret =
492      adb_object->IsCompleted();
493    adb_object->Release();
494    return ret;
495  } else {
496    SetLastError(ERROR_INVALID_HANDLE);
497    return true;
498  }
499}
500
501bool __cdecl AdbCloseHandle(ADBAPIHANDLE adb_handle) {
502  // Lookup object for the handle
503  AdbObjectHandle* adb_object = AdbObjectHandle::Lookup(adb_handle);
504
505  if (NULL != adb_object) {
506    // Dispatch close to the found object
507    bool ret = adb_object->CloseHandle();
508    adb_object->Release();
509    return ret;
510  } else {
511    SetLastError(ERROR_INVALID_HANDLE);
512    return false;
513  }
514}
515