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_LEGACY_IO_COMPLETION_H__
18 #define ANDROID_USB_API_ADB_LEGACY_IO_COMPLETION_H__
19 /** \file
20   This file consists of declaration of class AdbLegacyIOCompletion that
21   encapsulates a wrapper around OVERLAPPED Win32 structure returned from
22   asynchronous I/O requests issued via legacy USB API.
23 */
24 
25 #include "adb_io_completion.h"
26 #include "adb_legacy_endpoint_object.h"
27 
28 /** \brief Encapsulates a wrapper around OVERLAPPED Win32 structure returned
29   from asynchronous I/O requests issued via legacy USB API.
30 
31   A handle to this object is returned to the caller of each successful
32   asynchronous I/O request. Just like all other handles this handle
33   must be closed after it's no longer needed.
34 */
35 class AdbLegacyIOCompletion : public AdbIOCompletion {
36  public:
37   /** \brief Constructs the object.
38 
39     @param[in] parent_io_obj Parent legacy endpoint that created this
40            instance.
41     @param[in] expected_trans_size Number of bytes expected to be transferred
42           with the I/O.
43     @param[in] event_hndl Event handle that should be signaled when I/O
44            completes. Can be NULL. If it's not NULL this handle will be
45            used to initialize OVERLAPPED structure for this object.
46     @param[in] is_write_ctl Flag indicating whether or not this completion
47            object is created for ADB_IOCTL_BULK_WRITE I/O.
48   */
49   AdbLegacyIOCompletion(AdbLegacyEndpointObject* parent_io_obj,
50                         ULONG expected_trans_size,
51                         HANDLE event_hndl,
52                         bool is_write_ctl);
53 
54  protected:
55   /** \brief Destructs the object.
56 
57     We hide destructor in order to prevent ourseves from accidentaly allocating
58     instances on the stack. If such attemp occur, compiler will error.
59   */
60   virtual ~AdbLegacyIOCompletion();
61 
62   //
63   // Abstract overrides
64   //
65 
66  public:
67   /** \brief Gets overlapped I/O result
68 
69     This method uses GetOverlappedResult to get results of the overlapped I/O
70     operation.
71     @param[out] ovl_data Buffer for the copy of this object's OVERLAPPED
72            structure. Can be NULL.
73     @param[out] bytes_transferred Pointer to a variable that receives the
74            number of bytes that were actually transferred by a read or write
75            operation. See SDK doc on GetOvelappedResult for more information.
76            Unlike regular GetOvelappedResult call this parameter can be NULL.
77     @param[in] wait If this parameter is true, the method does not return
78            until the operation has been completed. If this parameter is false
79            and the operation is still pending, the method returns false and
80            the GetLastError function returns ERROR_IO_INCOMPLETE.
81     @return true if I/O has been completed or false on failure or if request
82            is not yet completed. If false is returned GetLastError() provides
83            extended error information. If GetLastError returns
84            ERROR_IO_INCOMPLETE it means that I/O is not yet completed.
85   */
86   virtual bool GetOvelappedIoResult(LPOVERLAPPED ovl_data,
87                                     ULONG* bytes_transferred,
88                                     bool wait);
89 
90  public:
91   /// Gets parent legacy endpoint.
parent_legacy_io_object()92   AdbLegacyEndpointObject* parent_legacy_io_object() const {
93     return reinterpret_cast<AdbLegacyEndpointObject*>(parent_io_object());
94   }
95 
96   /// Gets write IOCTL flag.
is_write_ioctl()97   bool is_write_ioctl() const {
98     return is_write_ioctl_;
99   }
100 
101   /// Gets address for ADB_IOCTL_BULK_WRITE output buffer.
transferred_bytes_ptr()102   ULONG* transferred_bytes_ptr() {
103     ATLASSERT(is_write_ioctl());
104     return &transferred_bytes_;
105   }
106 
107  protected:
108   /// Recepient for number of transferred bytes in write IOCTL.
109   ULONG         transferred_bytes_;
110 
111   /// Write IOCTL flag.
112   bool          is_write_ioctl_;
113 };
114 
115 #endif  // ANDROID_USB_API_ADB_LEGACY_IO_COMPLETION_H__
116