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_IO_COMPLETION_H__ 18 #define ANDROID_USB_API_ADB_WINUSB_IO_COMPLETION_H__ 19 /** \file 20 This file consists of declaration of class AdbWinUsbIOCompletion that 21 encapsulates a wrapper around OVERLAPPED Win32 structure returned from 22 asynchronous I/O requests issued via WinUsb API. 23 */ 24 25 #include "..\api\adb_io_completion.h" 26 #include "adb_winusb_endpoint_object.h" 27 28 /** \brief Encapsulates encapsulates a wrapper around OVERLAPPED Win32 29 structure returned from asynchronous I/O requests issued via WinUsb 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 AdbWinUsbIOCompletion : public AdbIOCompletion { 36 public: 37 /** \brief Constructs the object 38 39 @param[in] parent_io_obj Parent WinUsb I/O object 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 */ 47 AdbWinUsbIOCompletion(AdbWinUsbEndpointObject* parent_io_obj, 48 ULONG expected_trans_size, 49 HANDLE event_hndl); 50 51 protected: 52 /** \brief Destructs the object. 53 54 We hide destructor in order to prevent ourseves from accidentaly allocating 55 instances on the stack. If such attemp occur, compiler will error. 56 */ 57 virtual ~AdbWinUsbIOCompletion(); 58 59 // 60 // Virtual overrides 61 // 62 63 public: 64 /** \brief Releases the object. 65 66 If refcount drops to zero as the result of this release, the object is 67 destroyed in this method. As a general rule, objects must not be touched 68 after this method returns even if returned value is not zero. We override 69 this method in order to make sure that objects of this class are deleted 70 in contect of the DLL they were created in. The problem is that since 71 objects of this class were created in context of AdbWinUsbApi module, they 72 are allocated from the heap assigned to that module. Now, if these objects 73 are deleted outside of AdbWinUsbApi module, this will lead to the heap 74 corruption in the module that deleted these objects. Since all objects of 75 this class are deleted in the Release method only, by overriding it we make 76 sure that we free memory in the context of the module where it was 77 allocated. 78 @return Value of the reference counter after object is released in this 79 method. 80 */ 81 virtual LONG Release(); 82 83 // 84 // Abstract overrides 85 // 86 87 public: 88 /** \brief Gets overlapped I/O result 89 90 This method uses WinUsb_GetOverlappedResult to get results of the 91 overlapped I/O operation. 92 @param[out] ovl_data Buffer for the copy of this object's OVERLAPPED 93 structure. Can be NULL. 94 @param[out] bytes_transferred Pointer to a variable that receives the 95 number of bytes that were actually transferred by a read or write 96 operation. See SDK doc on GetOvelappedResult for more information. 97 Unlike regular GetOvelappedResult call this parameter can be NULL. 98 @param[in] wait If this parameter is true, the method does not return 99 until the operation has been completed. If this parameter is false 100 and the operation is still pending, the method returns false and 101 the GetLastError function returns ERROR_IO_INCOMPLETE. 102 @return true if I/O has been completed or false on failure or if request 103 is not yet completed. If false is returned GetLastError() provides 104 extended error information. If GetLastError returns 105 ERROR_IO_INCOMPLETE it means that I/O is not yet completed. 106 */ 107 virtual bool GetOvelappedIoResult(LPOVERLAPPED ovl_data, 108 ULONG* bytes_transferred, 109 bool wait); 110 111 public: 112 /// Gets WinUsb parent object 113 AdbWinUsbEndpointObject* parent_winusb_io_object() const { 114 return reinterpret_cast<AdbWinUsbEndpointObject*>(parent_io_object()); 115 } 116 }; 117 118 #endif // ANDROID_USB_API_ADB_WINUSB_IO_COMPLETION_H__ 119