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