Home | History | Annotate | Download | only in usb_service
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
      6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/strings/string16.h"
     14 #include "base/threading/thread_checker.h"
     15 #include "components/usb_service/usb_interface.h"
     16 #include "components/usb_service/usb_service_export.h"
     17 #include "net/base/io_buffer.h"
     18 
     19 namespace usb_service {
     20 
     21 class UsbDevice;
     22 
     23 enum UsbTransferStatus {
     24   USB_TRANSFER_COMPLETED = 0,
     25   USB_TRANSFER_ERROR,
     26   USB_TRANSFER_TIMEOUT,
     27   USB_TRANSFER_CANCELLED,
     28   USB_TRANSFER_STALLED,
     29   USB_TRANSFER_DISCONNECT,
     30   USB_TRANSFER_OVERFLOW,
     31   USB_TRANSFER_LENGTH_SHORT,
     32 };
     33 
     34 typedef base::Callback<
     35     void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>
     36     UsbTransferCallback;
     37 
     38 // UsbDeviceHandle class provides basic I/O related functionalities.
     39 class USB_SERVICE_EXPORT UsbDeviceHandle
     40     : public base::RefCountedThreadSafe<UsbDeviceHandle> {
     41  public:
     42   enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
     43   enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
     44 
     45   virtual scoped_refptr<UsbDevice> GetDevice() const = 0;
     46 
     47   // Notifies UsbDevice to drop the reference of this object; cancels all the
     48   // flying transfers.
     49   // It is possible that the object has no other reference after this call. So
     50   // if it is called using a raw pointer, it could be invalidated.
     51   // The platform device handle will be closed when UsbDeviceHandle destructs.
     52   virtual void Close() = 0;
     53 
     54   // Device manipulation operations. These methods are blocking and must be
     55   // called on FILE thread.
     56   virtual bool ClaimInterface(const int interface_number) = 0;
     57   virtual bool ReleaseInterface(const int interface_number) = 0;
     58   virtual bool SetInterfaceAlternateSetting(const int interface_number,
     59                                             const int alternate_setting) = 0;
     60   virtual bool ResetDevice() = 0;
     61   virtual bool GetSerial(base::string16* serial) = 0;
     62 
     63   // Async IO. Can be called on any thread.
     64   virtual void ControlTransfer(const UsbEndpointDirection direction,
     65                                const TransferRequestType request_type,
     66                                const TransferRecipient recipient,
     67                                const uint8 request,
     68                                const uint16 value,
     69                                const uint16 index,
     70                                net::IOBuffer* buffer,
     71                                const size_t length,
     72                                const unsigned int timeout,
     73                                const UsbTransferCallback& callback) = 0;
     74 
     75   virtual void BulkTransfer(const UsbEndpointDirection direction,
     76                             const uint8 endpoint,
     77                             net::IOBuffer* buffer,
     78                             const size_t length,
     79                             const unsigned int timeout,
     80                             const UsbTransferCallback& callback) = 0;
     81 
     82   virtual void InterruptTransfer(const UsbEndpointDirection direction,
     83                                  const uint8 endpoint,
     84                                  net::IOBuffer* buffer,
     85                                  const size_t length,
     86                                  const unsigned int timeout,
     87                                  const UsbTransferCallback& callback) = 0;
     88 
     89   virtual void IsochronousTransfer(const UsbEndpointDirection direction,
     90                                    const uint8 endpoint,
     91                                    net::IOBuffer* buffer,
     92                                    const size_t length,
     93                                    const unsigned int packets,
     94                                    const unsigned int packet_length,
     95                                    const unsigned int timeout,
     96                                    const UsbTransferCallback& callback) = 0;
     97 
     98  protected:
     99   friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
    100 
    101   UsbDeviceHandle() {};
    102 
    103   virtual ~UsbDeviceHandle() {};
    104 
    105   DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
    106 };
    107 
    108 }  // namespace usb_service
    109 
    110 #endif  // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
    111