OSDN Git Service

Merge change 6506
[android-x86/development.git] / host / windows / usb / winusb / adb_winusb_endpoint_object.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_ENDPOINT_OBJECT_H__\r
18 #define ANDROID_USB_API_ADB_WINUSB_ENDPOINT_OBJECT_H__\r
19 /** \file\r
20   This file consists of declaration of class AdbWinUsbEndpointObject that\r
21   encapsulates a handle opened to a WinUsb endpoint on our device.\r
22 */\r
23 \r
24 #include "..\api\adb_endpoint_object.h"\r
25 #include "adb_winusb_interface.h"\r
26 \r
27 /** Class AdbWinUsbEndpointObject encapsulates a handle opened to an endpoint on\r
28   our device.\r
29 */\r
30 class AdbWinUsbEndpointObject : public AdbEndpointObject {\r
31  public:\r
32   /** \brief Constructs the object\r
33 \r
34     @param[in] interface Parent WinUsb interface for this object.\r
35     @param[in] endpoint_id Endpoint ID (endpoint address) on the device.\r
36     @param[in] endpoint_index Zero-based endpoint index in the interface's\r
37           array of endpoints.\r
38   */\r
39   AdbWinUsbEndpointObject(AdbWinUsbInterfaceObject* parent_interf,\r
40                           UCHAR endpoint_id,\r
41                           UCHAR endpoint_index);\r
42 \r
43  protected:\r
44   /** \brief Destructs the object.\r
45 \r
46     We hide destructor in order to prevent ourseves from accidentaly allocating\r
47     instances on the stack. If such attemp occur, compiler will error.\r
48   */\r
49   virtual ~AdbWinUsbEndpointObject();\r
50 \r
51   //\r
52   // Virtual overrides\r
53   //\r
54 \r
55  public:\r
56   /** \brief Releases the object.\r
57 \r
58     If refcount drops to zero as the result of this release, the object is\r
59     destroyed in this method. As a general rule, objects must not be touched\r
60     after this method returns even if returned value is not zero. We override\r
61     this method in order to make sure that objects of this class are deleted\r
62     in contect of the DLL they were created in. The problem is that since\r
63     objects of this class were created in context of AdbWinUsbApi module, they\r
64     are allocated from the heap assigned to that module. Now, if these objects\r
65     are deleted outside of AdbWinUsbApi module, this will lead to the heap\r
66     corruption in the module that deleted these objects. Since all objects of\r
67     this class are deleted in the Release method only, by overriding it we make\r
68     sure that we free memory in the context of the module where it was\r
69     allocated.\r
70     @return Value of the reference counter after object is released in this\r
71             method.\r
72   */\r
73   virtual LONG Release();\r
74 \r
75   //\r
76   // Abstract overrides\r
77   //\r
78 \r
79  protected:\r
80   /** \brief Common code for async read / write\r
81 \r
82     @param[in] is_read Read or write selector.\r
83     @param[in,out] buffer Pointer to the buffer for read / write.\r
84     @param[in] bytes_to_transfer Number of bytes to be read / written.\r
85     @param[out] bytes_transferred Number of bytes read / written. Can be NULL.\r
86     @param[in] event_handle Event handle that should be signaled when async I/O\r
87            completes. Can be NULL. If it's not NULL this handle will be used to\r
88            initialize OVERLAPPED structure for this I/O.\r
89     @param[in] time_out A timeout (in milliseconds) required for this I/O to\r
90            complete. Zero value in this parameter means that there is no\r
91            timeout set for this I/O.\r
92     @return A handle to IO completion object or NULL on failure. If NULL is\r
93             returned GetLastError() provides extended error information.\r
94   */\r
95   virtual ADBAPIHANDLE CommonAsyncReadWrite(bool is_read,\r
96                                             void* buffer,\r
97                                             ULONG bytes_to_transfer,\r
98                                             ULONG* bytes_transferred,\r
99                                             HANDLE event_handle,\r
100                                             ULONG time_out);\r
101 \r
102   /** \brief Common code for sync read / write\r
103 \r
104     @param[in] is_read Read or write selector.\r
105     @param[in,out] buffer Pointer to the buffer for read / write.\r
106     @param[in] bytes_to_transfer Number of bytes to be read / written.\r
107     @param[out] bytes_transferred Number of bytes read / written. Can be NULL.\r
108     @param[in] time_out A timeout (in milliseconds) required for this I/O to\r
109            complete. Zero value in this parameter means that there is no\r
110            timeout set for this I/O.\r
111     @return true on success, false on failure. If false is returned\r
112             GetLastError() provides extended error information.\r
113   */\r
114   virtual bool CommonSyncReadWrite(bool is_read,\r
115                                    void* buffer,\r
116                                    ULONG bytes_to_transfer,\r
117                                    ULONG* bytes_transferred,\r
118                                    ULONG time_out);\r
119 \r
120   //\r
121   // Operations\r
122   //\r
123 \r
124  protected:\r
125   /** \brief Sets read / write operation timeout.\r
126 \r
127     @param[in] timeout Timeout value in milliseconds to use for current read\r
128           or write operation. Zero value passed in this parameters indicate\r
129           not timeout at all. Note that timeout that is set with this method is\r
130           global per endpoint (pipe). I.e. once set, it will be used against\r
131           all read / write operations performed on this endpoint, untill\r
132           another call to this method modifies it. This is a WinUsb design\r
133           flaw. Microsoft is aware of this and (hopefuly) future versions of\r
134           WinUsb framework will accept a timeout parameter in WinUsb_Read/Write\r
135           routines. For the purposes of ADB this flaw doesn't apperar to be an\r
136           issue, since we use single-threaded synchronous read / writes, so\r
137           there is no conflict in setting per-endpoint timeouts.\r
138     @return true on success, false on failure. If false is returned\r
139             GetLastError() provides extended error information.\r
140   */\r
141   virtual bool SetTimeout(ULONG timeout);\r
142 \r
143  public:\r
144   /// Gets parent WinUsb interface \r
145   AdbWinUsbInterfaceObject* parent_winusb_interface() const {\r
146     return reinterpret_cast<AdbWinUsbInterfaceObject*>(parent_interface());\r
147   }\r
148 \r
149   /// Gets parent interface WinUsb handle\r
150   WINUSB_INTERFACE_HANDLE winusb_handle() const {\r
151     return parent_winusb_interface()->winusb_handle();\r
152   }\r
153 };\r
154 \r
155 #endif  // ANDROID_USB_API_ADB_WINUSB_ENDPOINT_OBJECT_H__\r