OSDN Git Service

merge from donut
[android-x86/development.git] / host / windows / usb / winusb / adb_winusb_interface.h
1 /*\r
2  * Copyright (C) 2009 The Android Open Source Project\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #ifndef ANDROID_USB_API_ADB_WINUSB_INTERFACE_H__\r
18 #define ANDROID_USB_API_ADB_WINUSB_INTERFACE_H__\r
19 /** \file\r
20   This file consists of declaration of class AdbWinUsbInterfaceObject\r
21   that encapsulates an interface on our USB device that is accessible\r
22   via WinUsb API.\r
23 */\r
24 \r
25 #include "..\api\adb_interface.h"\r
26 \r
27 /** \brief Encapsulates an interface on our USB device that is accessible\r
28   via WinUsb API.\r
29 */\r
30 class AdbWinUsbInterfaceObject : public AdbInterfaceObject {\r
31  public:\r
32   /** \brief Constructs the object.\r
33 \r
34     @param[in] interf_name Name of the interface\r
35   */\r
36   explicit AdbWinUsbInterfaceObject(const wchar_t* interf_name);\r
37 \r
38  protected:\r
39   /** \brief Destructs the object.\r
40 \r
41    We hide destructor in order to prevent ourseves from accidentaly allocating\r
42    instances on the stack. If such attemp occur, compiler will error.\r
43   */\r
44   virtual ~AdbWinUsbInterfaceObject();\r
45 \r
46   //\r
47   // Virtual overrides\r
48   //\r
49 \r
50  public:\r
51   /** \brief Releases the object.\r
52 \r
53     If refcount drops to zero as the result of this release, the object is\r
54     destroyed in this method. As a general rule, objects must not be touched\r
55     after this method returns even if returned value is not zero. We override\r
56     this method in order to make sure that objects of this class are deleted\r
57     in contect of the DLL they were created in. The problem is that since\r
58     objects of this class were created in context of AdbWinUsbApi module, they\r
59     are allocated from the heap assigned to that module. Now, if these objects\r
60     are deleted outside of AdbWinUsbApi module, this will lead to the heap\r
61     corruption in the module that deleted these objects. Since all objects of\r
62     this class are deleted in the Release method only, by overriding it we make\r
63     sure that we free memory in the context of the module where it was\r
64     allocated.\r
65     @return Value of the reference counter after object is released in this\r
66             method.\r
67   */\r
68   virtual LONG Release();\r
69 \r
70   /** \brief Creates handle to this object.\r
71 \r
72     In this call a handle for this object is generated and object is added\r
73     to the AdbObjectHandleMap. We override this method in order to initialize\r
74     WinUsb API for the given interface.\r
75     @return A handle to this object on success or NULL on an error.\r
76             If NULL is returned GetLastError() provides extended error\r
77             information. ERROR_GEN_FAILURE is set if an attempt was\r
78             made to create already opened object.\r
79   */\r
80   virtual ADBAPIHANDLE CreateHandle();\r
81 \r
82   /** \brief This method is called when handle to this object gets closed.\r
83 \r
84     In this call object is deleted from the AdbObjectHandleMap. We override\r
85     this method in order close WinUsb handle created in CreateHandle method\r
86     of this class.\r
87     @return true on success or false if object is already closed. If\r
88             false is returned GetLastError() provides extended error\r
89             information.\r
90   */\r
91   virtual bool CloseHandle();\r
92 \r
93   //\r
94   // Abstract overrides\r
95   //\r
96 \r
97  public:\r
98   /** \brief Gets serial number for interface's device.\r
99 \r
100     @param[out] buffer Buffer for the serail number string. Can be NULL in\r
101            which case buffer_char_size will contain number of characters\r
102            required for the string.\r
103     @param[in,out] buffer_char_size On the way in supplies size (in characters)\r
104            of the buffer. On the way out, if method failed and GetLastError\r
105            reports ERROR_INSUFFICIENT_BUFFER, will contain number of characters\r
106            required for the name.\r
107     @param[in] ansi If true the name will be returned as single character\r
108            string. Otherwise name will be returned as wide character string.\r
109     @return true on success, false on failure. If false is returned\r
110             GetLastError() provides extended error information.\r
111   */\r
112   virtual bool GetSerialNumber(void* buffer,\r
113                                unsigned long* buffer_char_size,\r
114                                bool ansi);\r
115 \r
116   /** \brief Gets information about an endpoint on this interface.\r
117 \r
118     @param[in] endpoint_index Zero-based endpoint index. There are two\r
119            shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX\r
120            and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide infor about\r
121            (default?) bulk write and read endpoints respectively.\r
122     @param[out] info Upon successful completion will have endpoint information.\r
123     @return true on success, false on failure. If false is returned\r
124             GetLastError() provides extended error information.\r
125   */\r
126   virtual bool GetEndpointInformation(UCHAR endpoint_index,\r
127                                       AdbEndpointInformation* info);\r
128 \r
129   /** \brief Opens an endpoint on this interface.\r
130 \r
131     @param[in] endpoint_index Zero-based endpoint index. There are two\r
132            shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX\r
133            and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide infor about\r
134            (default?) bulk write and read endpoints respectively.\r
135     @param[in] access_type Desired access type. In the current implementation\r
136            this parameter has no effect on the way endpoint is opened. It's\r
137            always read / write access.\r
138     @param[in] sharing_mode Desired share mode. In the current implementation\r
139            this parameter has no effect on the way endpoint is opened. It's\r
140            always shared for read / write.\r
141     @return Handle to the opened endpoint object or NULL on failure.\r
142             If NULL is returned GetLastError() provides extended information\r
143             about the error that occurred.\r
144   */\r
145   virtual ADBAPIHANDLE OpenEndpoint(UCHAR endpoint_index,\r
146                                     AdbOpenAccessType access_type,\r
147                                     AdbOpenSharingMode sharing_mode);\r
148 \r
149   //\r
150   // Operations\r
151   //\r
152 \r
153  protected:\r
154   /** \brief Opens an endpoint on this interface.\r
155 \r
156     @param[in] endpoint_id Endpoint (pipe) address on the device.\r
157     @param[in] endpoint_index Zero-based endpoint index.\r
158     @return Handle to the opened endpoint object or NULL on failure.\r
159             If NULL is returned GetLastError() provides extended information\r
160             about the error that occurred.\r
161   */\r
162   ADBAPIHANDLE OpenEndpoint(UCHAR endpoint_id, UCHAR endpoint_index);\r
163 \r
164  public:\r
165   /// Gets handle to the USB device\r
166   HANDLE usb_device_handle() const {\r
167     return usb_device_handle_;\r
168   }\r
169 \r
170   /// Gets interface handle used by WinUSB API\r
171   WINUSB_INTERFACE_HANDLE winusb_handle() const {\r
172     return winusb_handle_;\r
173   }\r
174 \r
175   /// Gets current interface number.\r
176   UCHAR interface_number() const {\r
177     return interface_number_;\r
178   }\r
179 \r
180  protected:\r
181   /// Handle to the USB device\r
182   HANDLE                        usb_device_handle_;\r
183 \r
184   /// Interface handle used by WinUSB API\r
185   WINUSB_INTERFACE_HANDLE       winusb_handle_;\r
186 \r
187   /// Current interface number. This value is obtained via call to\r
188   /// WinUsb_GetCurrentAlternateSetting and is used in WinUsb_Xxx\r
189   /// calls that require interface number.\r
190   UCHAR                         interface_number_;\r
191 \r
192   /// Index for the default bulk read endpoint\r
193   UCHAR                         def_read_endpoint_;\r
194 \r
195   /// ID for the default bulk read endpoint\r
196   UCHAR                         read_endpoint_id_;\r
197 \r
198   /// Index for the default bulk write endpoint\r
199   UCHAR                         def_write_endpoint_;\r
200 \r
201   /// ID for the default bulk write endpoint\r
202   UCHAR                         write_endpoint_id_;\r
203 };\r
204 \r
205 #endif  // ANDROID_USB_API_ADB_WINUSB_INTERFACE_H__\r