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_ENDPOINT_OBJECT_H__
18 #define ANDROID_USB_API_ADB_ENDPOINT_OBJECT_H__
19 /** \file
20   This file consists of declaration of class AdbEndpointObject that
21   encapsulates a handle opened to an endpoint on our device.
22 */
23 
24 #include "adb_interface.h"
25 
26 /** Class AdbEndpointObject encapsulates a handle opened to an endpoint on
27   our device.
28 
29   This class implement functionality that is common for both, WinUsb and
30   legacy APIs.
31 */
32 class ADBWIN_API_CLASS AdbEndpointObject : public AdbObjectHandle {
33  public:
34   /** \brief Constructs the object
35 
36     @param[in] interface Parent interface for this object. Interface will be
37            referenced in this object's constructur and released in the
38            destructor.
39     @param[in] endpoint_id Endpoint ID (endpoint address) on the device.
40     @param[in] endpoint_index Zero-based endpoint index in the interface's
41           array of endpoints.
42   */
43   AdbEndpointObject(AdbInterfaceObject* parent_interf,
44                     UCHAR endpoint_id,
45                     UCHAR endpoint_index);
46 
47  protected:
48   /** \brief Destructs the object.
49 
50     We hide destructor in order to prevent ourseves from accidentaly allocating
51     instances on the stack. If such attemp occur, compiler will error.
52   */
53   virtual ~AdbEndpointObject();
54 
55   //
56   // Abstract
57   //
58 
59  protected:
60   /** \brief Common code for async read / write
61 
62     @param[in] is_read Read or write selector.
63     @param[in,out] buffer Pointer to the buffer for read / write.
64     @param[in] bytes_to_transfer Number of bytes to be read / written.
65     @param[out] bytes_transferred Number of bytes read / written. Can be NULL.
66     @param[in] event_handle Event handle that should be signaled when async I/O
67            completes. Can be NULL. If it's not NULL this handle will be used to
68            initialize OVERLAPPED structure for this I/O.
69     @param[in] time_out A timeout (in milliseconds) required for this I/O to
70            complete. Zero value in this parameter means that there is no
71            timeout set for this I/O.
72     @return A handle to IO completion object or NULL on failure. If NULL is
73             returned GetLastError() provides extended error information.
74   */
75   virtual ADBAPIHANDLE CommonAsyncReadWrite(bool is_read,
76                                             void* buffer,
77                                             ULONG bytes_to_transfer,
78                                             ULONG* bytes_transferred,
79                                             HANDLE event_handle,
80                                             ULONG time_out) = 0;
81 
82   /** \brief Common code for sync read / write
83 
84     @param[in] is_read Read or write selector.
85     @param[in,out] buffer Pointer to the buffer for read / write.
86     @param[in] bytes_to_transfer Number of bytes to be read / written.
87     @param[out] bytes_transferred Number of bytes read / written. Can be NULL.
88     @param[in] time_out A timeout (in milliseconds) required for this I/O to
89            complete. Zero value in this parameter means that there is no
90            timeout set for this I/O.
91     @return true on success, false on failure. If false is returned
92             GetLastError() provides extended error information.
93   */
94   virtual bool CommonSyncReadWrite(bool is_read,
95                                    void* buffer,
96                                    ULONG bytes_to_transfer,
97                                    ULONG* bytes_transferred,
98                                    ULONG time_out) = 0;
99 
100   //
101   // Operations
102   //
103 
104  public:
105   /** \brief Gets information about this endpoint.
106 
107     @param[out] info Upon successful completion will have endpoint information.
108     @return true on success, false on failure. If false is returned
109             GetLastError() provides extended error information.
110   */
111   virtual bool GetEndpointInformation(AdbEndpointInformation* info);
112 
113   /** \brief Reads from opened I/O object asynchronously
114 
115     @param[out] buffer Pointer to the buffer that receives the data.
116     @param[in] bytes_to_read Number of bytes to be read.
117     @param[out] bytes_read Number of bytes read. Can be NULL.
118     @param[in] event_handle Event handle that should be signaled when async I/O
119            completes. Can be NULL. If it's not NULL this handle will be used to
120            initialize OVERLAPPED structure for this I/O.
121     @param[in] time_out A timeout (in milliseconds) required for this I/O to
122            complete. Zero value in this parameter means that there is no
123            timeout set for this I/O.
124     @return A handle to IO completion object or NULL on failure. If NULL is
125             returned GetLastError() provides extended error information.
126   */
127   virtual ADBAPIHANDLE AsyncRead(void* buffer,
128                                  ULONG bytes_to_read,
129                                  ULONG* bytes_read,
130                                  HANDLE event_handle,
131                                  ULONG time_out);
132 
133   /** \brief Writes to opened I/O object asynchronously
134 
135     @param[in] buffer Pointer to the buffer containing the data to be written.
136     @param[in] bytes_to_write Number of bytes to be written.
137     @param[out] bytes_written Number of bytes written. Can be NULL.
138     @param[in] event_handle Event handle that should be signaled when async I/O
139            completes. Can be NULL. If it's not NULL this handle will be used to
140            initialize OVERLAPPED structure for this I/O.
141     @param[in] time_out A timeout (in milliseconds) required for this I/O to
142            complete. Zero value in this parameter means that there is no
143            timeout set for this I/O.
144     @return A handle to IO completion object or NULL on failure. If NULL is
145             returned GetLastError() provides extended error information.
146   */
147   virtual ADBAPIHANDLE AsyncWrite(void* buffer,
148                                   ULONG bytes_to_write,
149                                   ULONG* bytes_written,
150                                   HANDLE event_handle,
151                                   ULONG time_out);
152 
153   /** \brief Reads from opened I/O object synchronously
154 
155     @param[out] buffer Pointer to the buffer that receives the data.
156     @param[in] bytes_to_read Number of bytes to be read.
157     @param[out] bytes_read Number of bytes read. Can be NULL.
158     @param[in] time_out A timeout (in milliseconds) required for this I/O to
159            complete. Zero value in this parameter means that there is no
160            timeout set for this I/O.
161     @return true on success and false on failure. If false is
162             returned GetLastError() provides extended error information.
163   */
164   virtual bool SyncRead(void* buffer,
165                         ULONG bytes_to_read,
166                         ULONG* bytes_read,
167                         ULONG time_out);
168 
169   /** \brief Writes to opened I/O object synchronously
170 
171     @param[in] buffer Pointer to the buffer containing the data to be written.
172     @param[in] bytes_to_write Number of bytes to be written.
173     @param[out] bytes_written Number of bytes written. Can be NULL.
174     @param[in] time_out A timeout (in milliseconds) required for this I/O to
175            complete. Zero value in this parameter means that there is no
176            timeout set for this I/O.
177     @return true on success and false on failure. If false is
178             returned GetLastError() provides extended error information.
179   */
180   virtual bool SyncWrite(void* buffer,
181                          ULONG bytes_to_write,
182                          ULONG* bytes_written,
183                          ULONG time_out);
184 
185  public:
186   /// This is a helper for extracting object from the AdbObjectHandleMap
Type()187   static AdbObjectType Type() {
188     return AdbObjectTypeEndpoint;
189   }
190 
191   /// Gets parent interface
parent_interface()192   AdbInterfaceObject* parent_interface() const {
193     return parent_interface_;
194   }
195   /// Gets this endpoint ID
endpoint_id()196   UCHAR endpoint_id() const {
197     return endpoint_id_;
198   }
199 
200   /// Gets this endpoint index on the interface
endpoint_index()201   UCHAR endpoint_index() const {
202     return endpoint_index_;
203   }
204 
205   /// Gets parent interface handle
GetParentInterfaceHandle()206   ADBAPIHANDLE GetParentInterfaceHandle() const {
207     return (NULL != parent_interface()) ? parent_interface()->adb_handle() :
208                                           NULL;
209   }
210 
211  protected:
212   /// Parent interface
213   AdbInterfaceObject* parent_interface_;
214 
215   /// This endpoint id
216   UCHAR               endpoint_id_;
217 
218   /// This endpoint index on the interface
219   UCHAR               endpoint_index_;
220 };
221 
222 #endif  // ANDROID_USB_API_ADB_ENDPOINT_OBJECT_H__
223