adb_interface.h revision 5c11852110eeb03dc5a69111354b383f98d15336
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#ifndef ANDROID_USB_API_ADB_INTERFACE_H__
18#define ANDROID_USB_API_ADB_INTERFACE_H__
19/** \file
20  This file consists of declaration of class AdbInterfaceObject that
21  encapsulates an interface on our USB device.
22*/
23
24#include "adb_object_handle.h"
25
26/** Class AdbInterfaceObject encapsulates an interface on our USB device.
27*/
28class AdbInterfaceObject : public AdbObjectHandle {
29 public:
30  /** \brief Constructs the object
31
32    @param interf_name[in] Name of the interface
33  */
34  explicit AdbInterfaceObject(const wchar_t* interf_name);
35
36 protected:
37  /** \brief Destructs the object.
38
39   We hide destructor in order to prevent ourseves from accidentaly allocating
40   instances on the stack. If such attemp occur, compiler will error.
41  */
42  virtual ~AdbInterfaceObject();
43
44 public:
45  /** \brief Creates handle to this object
46
47    In this call a handle for this object is generated and object is added
48    to the AdbObjectHandleMap. We override this method in order to verify that
49    interface indeed exists and gather device, interface and pipe properties.
50    If this step succeeds then and only then AdbObjectHandle::CreateHandle
51    will be called.
52    @return A handle to this object on success or NULL on an error.
53            If NULL is returned GetLastError() provides extended error
54            information. ERROR_GEN_FAILURE is set if an attempt was
55            made to create already opened object.
56  */
57  virtual ADBAPIHANDLE CreateHandle();
58
59  /** \brief Gets interface device name.
60
61    @param buffer[out] Buffer for the name. Can be NULL in which case
62           buffer_char_size will contain number of characters required to fit
63           the name.
64    @param buffer_char_size[in/out] On the way in supplies size (in characters)
65           of the buffer. On the way out if method failed and GetLastError
66           reports ERROR_INSUFFICIENT_BUFFER will contain number of characters
67           required to fit the name.
68    @param ansi[in] If true the name will be returned as single character
69           string. Otherwise name will be returned as wide character string.
70    @return 'true' on success, 'false' on failure. If 'false' is returned
71            GetLastError() provides extended error information.
72  */
73  bool GetInterfaceName(void* buffer,
74                        unsigned long* buffer_char_size,
75                        bool ansi);
76
77  /** \brief Gets serial number for interface's device.
78
79    @param buffer[out] Buffer for the serail number string. Can be NULL in
80           which case buffer_char_size will contain number of characters
81           required for the string.
82    @param buffer_char_size[in/out] On the way in supplies size (in characters)
83           of the buffer. On the way out, if method failed and GetLastError
84           reports ERROR_INSUFFICIENT_BUFFER, will contain number of characters
85           required for the name.
86    @param ansi[in] If 'true' the name will be returned as single character
87           string. Otherwise name will be returned as wide character string.
88    @return 'true' on success, 'false' on failure. If 'false' is returned
89            GetLastError() provides extended error information.
90  */
91  bool GetSerialNumber(void* buffer,
92                       unsigned long* buffer_char_size,
93                       bool ansi);
94
95  /** \brief Gets device descriptor for the USB device associated with
96    this interface.
97
98    @param desc[out] Upon successful completion will have usb device
99           descriptor.
100    @return 'true' on success, 'false' on failure. If 'false' is returned
101            GetLastError() provides extended error information.
102  */
103  bool GetUsbDeviceDescriptor(USB_DEVICE_DESCRIPTOR* desc);
104
105  /** \brief Gets descriptor for the selected USB device configuration.
106
107    @param desc[out] Upon successful completion will have usb device
108           configuration descriptor.
109    @return 'true' on success, 'false' on failure. If 'false' is returned
110            GetLastError() provides extended error information.
111  */
112  bool GetUsbConfigurationDescriptor(USB_CONFIGURATION_DESCRIPTOR* desc);
113
114  /** \brief Gets descriptor for this interface.
115
116    @param desc[out] Upon successful completion will have interface
117           descriptor.
118    @return 'true' on success, 'false' on failure. If 'false' is returned
119            GetLastError() provides extended error information.
120  */
121  bool GetUsbInterfaceDescriptor(USB_INTERFACE_DESCRIPTOR* desc);
122
123  /** \brief Gets information about an endpoint on this interface.
124
125    @param endpoint_index[in] Zero-based endpoint index. There are two
126           shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX
127           and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide infor about
128           (default?) bulk write and read endpoints respectively.
129    @param info[out] Upon successful completion will have endpoint information.
130    @return 'true' on success, 'false' on failure. If 'false' is returned
131            GetLastError() provides extended error information.
132  */
133  bool GetEndpointInformation(UCHAR endpoint_index, AdbEndpointInformation* info);
134
135  /** \brief Opens an endpoint on this interface.
136
137    @param endpoint_index[in] Zero-based endpoint index. There are two
138           shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX
139           and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide infor about
140           (default?) bulk write and read endpoints respectively.
141    @param access_type[in] Desired access type. In the current implementation
142           this parameter has no effect on the way endpoint is opened. It's
143           always read / write access.
144    @param sharing_mode[in] Desired share mode. In the current implementation
145           this parameter has no effect on the way endpoint is opened. It's
146           always shared for read / write.
147    @return Handle to the opened endpoint object or NULL on failure.
148            If NULL is returned GetLastError() provides extended information
149            about the error that occurred.
150  */
151  ADBAPIHANDLE OpenEndpoint(UCHAR endpoint_index,
152                            AdbOpenAccessType access_type,
153                            AdbOpenSharingMode sharing_mode);
154
155  /** \brief Opens an endpoint on this interface.
156
157    @param endpoint_name[in] Endpoint file name.
158    @param access_type[in] Desired access type. In the current implementation
159           this parameter has no effect on the way endpoint is opened. It's
160           always read / write access.
161    @param sharing_mode[in] Desired share mode. In the current implementation
162           this parameter has no effect on the way endpoint is opened. It's
163           always shared for read / write.
164    @return Handle to the opened endpoint object or NULL on failure.
165            If NULL is returned GetLastError() provides extended information
166            about the error that occurred.
167  */
168  ADBAPIHANDLE OpenEndpoint(const wchar_t* endpoint_name,
169                            AdbOpenAccessType access_type,
170                            AdbOpenSharingMode sharing_mode);
171
172 private:
173  /** \brief Caches device descriptor for the USB device associated with
174    this interface.
175
176    This method is called from CreateHandle method to cache some interface
177    information.
178    @param usb_device_handle[in] Handle to USB device.
179    @return 'true' on success, 'false' on failure. If 'false' is returned
180            GetLastError() provides extended error information.
181  */
182  bool CacheUsbDeviceDescriptor(HANDLE usb_device_handle);
183
184  /** \brief Caches descriptor for the selected USB device configuration.
185
186    This method is called from CreateHandle method to cache some interface
187    information.
188    @param usb_device_handle[in] Handle to USB device.
189    @return 'true' on success, 'false' on failure. If 'false' is returned
190            GetLastError() provides extended error information.
191  */
192  bool CacheUsbConfigurationDescriptor(HANDLE usb_device_handle);
193
194  /** \brief Caches descriptor for this interface.
195
196    This method is called from CreateHandle method to cache some interface
197    information.
198    @param usb_device_handle[in] Handle to USB device.
199    @return 'true' on success, 'false' on failure. If 'false' is returned
200            GetLastError() provides extended error information.
201  */
202  bool CacheUsbInterfaceDescriptor(HANDLE usb_device_handle);
203
204 public:
205  /// Gets name of the USB interface (device name) for this object
206  const std::wstring& interface_name() const {
207    return interface_name_;
208  }
209
210  // This is a helper for extracting object from the AdbObjectHandleMap
211  static AdbObjectType Type() {
212    return AdbObjectTypeInterface;
213  }
214
215  /// Gets cached usb device descriptor
216  const USB_DEVICE_DESCRIPTOR* usb_device_descriptor() const {
217    return &usb_device_descriptor_;
218  }
219
220  /// Gets cached usb configuration descriptor
221  const USB_CONFIGURATION_DESCRIPTOR* usb_config_descriptor() const {
222    return &usb_config_descriptor_;
223  }
224
225  /// Gets cached usb interface descriptor
226  const USB_INTERFACE_DESCRIPTOR* usb_interface_descriptor() const {
227    return &usb_interface_descriptor_;
228  }
229
230private:
231  /// Name of the USB interface (device name) for this object
232  std::wstring                  interface_name_;
233
234  /// Cached usb device descriptor
235  USB_DEVICE_DESCRIPTOR         usb_device_descriptor_;
236
237  /// Cached usb configuration descriptor
238  USB_CONFIGURATION_DESCRIPTOR  usb_config_descriptor_;
239
240  /// Cached usb interface descriptor
241  USB_INTERFACE_DESCRIPTOR      usb_interface_descriptor_;
242};
243
244#endif  // ANDROID_USB_API_ADB_INTERFACE_H__
245