Home | History | Annotate | Download | only in winusb
      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   //
     76   // Abstract overrides
     77   //
     78 
     79  protected:
     80   /** \brief Common code for async read / write
     81 
     82     @param[in] is_read Read or write selector.
     83     @param[in,out] buffer Pointer to the buffer for read / write.
     84     @param[in] bytes_to_transfer Number of bytes to be read / written.
     85     @param[out] bytes_transferred Number of bytes read / written. Can be NULL.
     86     @param[in] event_handle Event handle that should be signaled when async I/O
     87            completes. Can be NULL. If it's not NULL this handle will be used to
     88            initialize OVERLAPPED structure for this I/O.
     89     @param[in] time_out A timeout (in milliseconds) required for this I/O to
     90            complete. Zero value in this parameter means that there is no
     91            timeout set for this I/O.
     92     @return A handle to IO completion object or NULL on failure. If NULL is
     93             returned GetLastError() provides extended error information.
     94   */
     95   virtual ADBAPIHANDLE CommonAsyncReadWrite(bool is_read,
     96                                             void* buffer,
     97                                             ULONG bytes_to_transfer,
     98                                             ULONG* bytes_transferred,
     99                                             HANDLE event_handle,
    100                                             ULONG time_out);
    101 
    102   /** \brief Common code for sync read / write
    103 
    104     @param[in] is_read Read or write selector.
    105     @param[in,out] buffer Pointer to the buffer for read / write.
    106     @param[in] bytes_to_transfer Number of bytes to be read / written.
    107     @param[out] bytes_transferred Number of bytes read / written. Can be NULL.
    108     @param[in] time_out A timeout (in milliseconds) required for this I/O to
    109            complete. Zero value in this parameter means that there is no
    110            timeout set for this I/O.
    111     @return true on success, false on failure. If false is returned
    112             GetLastError() provides extended error information.
    113   */
    114   virtual bool CommonSyncReadWrite(bool is_read,
    115                                    void* buffer,
    116                                    ULONG bytes_to_transfer,
    117                                    ULONG* bytes_transferred,
    118                                    ULONG time_out);
    119 
    120   //
    121   // Operations
    122   //
    123 
    124  protected:
    125   /** \brief Sets read / write operation timeout.
    126 
    127     @param[in] timeout Timeout value in milliseconds to use for current read
    128           or write operation. Zero value passed in this parameters indicate
    129           not timeout at all. Note that timeout that is set with this method is
    130           global per endpoint (pipe). I.e. once set, it will be used against
    131           all read / write operations performed on this endpoint, untill
    132           another call to this method modifies it. This is a WinUsb design
    133           flaw. Microsoft is aware of this and (hopefuly) future versions of
    134           WinUsb framework will accept a timeout parameter in WinUsb_Read/Write
    135           routines. For the purposes of ADB this flaw doesn't apperar to be an
    136           issue, since we use single-threaded synchronous read / writes, so
    137           there is no conflict in setting per-endpoint timeouts.
    138     @return true on success, false on failure. If false is returned
    139             GetLastError() provides extended error information.
    140   */
    141   virtual bool SetTimeout(ULONG timeout);
    142 
    143  public:
    144   /// Gets parent WinUsb interface
    145   AdbWinUsbInterfaceObject* parent_winusb_interface() const {
    146     return reinterpret_cast<AdbWinUsbInterfaceObject*>(parent_interface());
    147   }
    148 
    149   /// Gets parent interface WinUsb handle
    150   WINUSB_INTERFACE_HANDLE winusb_handle() const {
    151     return parent_winusb_interface()->winusb_handle();
    152   }
    153 };
    154 
    155 #endif  // ANDROID_USB_API_ADB_WINUSB_ENDPOINT_OBJECT_H__
    156