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