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 class AdbInterfaceObject that
19   encapsulates a generic interface on our USB device.
20 */
21 
22 #include "stdafx.h"
23 #include "adb_interface.h"
24 
AdbInterfaceObject(const wchar_t * interf_name)25 AdbInterfaceObject::AdbInterfaceObject(const wchar_t* interf_name)
26     : AdbObjectHandle(AdbObjectTypeInterface),
27       interface_name_(interf_name) {
28   ATLASSERT(NULL != interf_name);
29 }
30 
~AdbInterfaceObject()31 AdbInterfaceObject::~AdbInterfaceObject() {
32 }
33 
GetInterfaceName(void * buffer,unsigned long * buffer_char_size,bool ansi)34 bool AdbInterfaceObject::GetInterfaceName(void* buffer,
35                                           unsigned long* buffer_char_size,
36                                           bool ansi) {
37   if (NULL == buffer_char_size) {
38     SetLastError(ERROR_INVALID_PARAMETER);
39     return false;
40   }
41 
42   // Lets see if buffer is big enough
43   ULONG name_len = static_cast<ULONG>(interface_name_.length() + 1);
44   if ((NULL == buffer) || (*buffer_char_size < name_len)) {
45     *buffer_char_size = name_len;
46     SetLastError(ERROR_INSUFFICIENT_BUFFER);
47     return false;
48   }
49 
50   if (!ansi) {
51     // If user asked for wide char name just return it
52     wcscpy(reinterpret_cast<wchar_t*>(buffer), interface_name().c_str());
53     return true;
54   }
55 
56   // We need to convert name from wide char to ansi string
57   int res = WideCharToMultiByte(CP_ACP,
58                                 0,
59                                 interface_name().c_str(),
60                                 static_cast<int>(name_len),
61                                 reinterpret_cast<PSTR>(buffer),
62                                 static_cast<int>(*buffer_char_size),
63                                 NULL,
64                                 NULL);
65   return (res != 0);
66 }
67 
GetUsbDeviceDescriptor(USB_DEVICE_DESCRIPTOR * desc)68 bool AdbInterfaceObject::GetUsbDeviceDescriptor(USB_DEVICE_DESCRIPTOR* desc) {
69   if (!IsOpened()) {
70     SetLastError(ERROR_INVALID_HANDLE);
71     return false;
72   }
73 
74   if (NULL == desc) {
75     SetLastError(ERROR_INVALID_PARAMETER);
76     return false;
77   }
78 
79   CopyMemory(desc, usb_device_descriptor(), sizeof(USB_DEVICE_DESCRIPTOR));
80 
81   return true;
82 }
83 
GetUsbConfigurationDescriptor(USB_CONFIGURATION_DESCRIPTOR * desc)84 bool AdbInterfaceObject::GetUsbConfigurationDescriptor(
85     USB_CONFIGURATION_DESCRIPTOR* desc) {
86   if (!IsOpened()) {
87     SetLastError(ERROR_INVALID_HANDLE);
88     return false;
89   }
90 
91   if (NULL == desc) {
92     SetLastError(ERROR_INVALID_PARAMETER);
93     return false;
94   }
95 
96   CopyMemory(desc, usb_config_descriptor(),
97              sizeof(USB_CONFIGURATION_DESCRIPTOR));
98 
99   return true;
100 }
101 
GetUsbInterfaceDescriptor(USB_INTERFACE_DESCRIPTOR * desc)102 bool AdbInterfaceObject::GetUsbInterfaceDescriptor(
103     USB_INTERFACE_DESCRIPTOR* desc) {
104   if (!IsOpened()) {
105     SetLastError(ERROR_INVALID_HANDLE);
106     return false;
107   }
108 
109   if (NULL == desc) {
110     SetLastError(ERROR_INVALID_PARAMETER);
111     return false;
112   }
113 
114   CopyMemory(desc, usb_interface_descriptor(), sizeof(USB_INTERFACE_DESCRIPTOR));
115 
116   return true;
117 }
118