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 DEVICE_USB_USB_DEVICE_HANDLE_H_
6#define DEVICE_USB_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 "device/usb/usb_descriptors.h"
16#include "net/base/io_buffer.h"
17
18namespace device {
19
20class UsbDevice;
21
22enum UsbTransferStatus {
23  USB_TRANSFER_COMPLETED = 0,
24  USB_TRANSFER_ERROR,
25  USB_TRANSFER_TIMEOUT,
26  USB_TRANSFER_CANCELLED,
27  USB_TRANSFER_STALLED,
28  USB_TRANSFER_DISCONNECT,
29  USB_TRANSFER_OVERFLOW,
30  USB_TRANSFER_LENGTH_SHORT,
31};
32
33typedef base::Callback<
34    void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>
35    UsbTransferCallback;
36
37// UsbDeviceHandle class provides basic I/O related functionalities.
38class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> {
39 public:
40  enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
41  enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
42
43  virtual scoped_refptr<UsbDevice> GetDevice() const = 0;
44
45  // Notifies UsbDevice to drop the reference of this object; cancels all the
46  // flying transfers.
47  // It is possible that the object has no other reference after this call. So
48  // if it is called using a raw pointer, it could be invalidated.
49  // The platform device handle will be closed when UsbDeviceHandle destructs.
50  virtual void Close() = 0;
51
52  // Device manipulation operations. These methods are blocking and must be
53  // called on FILE thread.
54  virtual bool ClaimInterface(int interface_number) = 0;
55  virtual bool ReleaseInterface(int interface_number) = 0;
56  virtual bool SetInterfaceAlternateSetting(int interface_number,
57                                            int alternate_setting) = 0;
58  virtual bool ResetDevice() = 0;
59
60  // Gets the string descriptor with the given index from the device, or returns
61  // false. This method is blocking and must be called on the FILE thread.
62  virtual bool GetStringDescriptor(uint8 string_id, base::string16* string) = 0;
63
64  // Async IO. Can be called on any thread.
65  virtual void ControlTransfer(UsbEndpointDirection direction,
66                               TransferRequestType request_type,
67                               TransferRecipient recipient,
68                               uint8 request,
69                               uint16 value,
70                               uint16 index,
71                               net::IOBuffer* buffer,
72                               size_t length,
73                               unsigned int timeout,
74                               const UsbTransferCallback& callback) = 0;
75
76  virtual void BulkTransfer(UsbEndpointDirection direction,
77                            uint8 endpoint,
78                            net::IOBuffer* buffer,
79                            size_t length,
80                            unsigned int timeout,
81                            const UsbTransferCallback& callback) = 0;
82
83  virtual void InterruptTransfer(UsbEndpointDirection direction,
84                                 uint8 endpoint,
85                                 net::IOBuffer* buffer,
86                                 size_t length,
87                                 unsigned int timeout,
88                                 const UsbTransferCallback& callback) = 0;
89
90  virtual void IsochronousTransfer(UsbEndpointDirection direction,
91                                   uint8 endpoint,
92                                   net::IOBuffer* buffer,
93                                   size_t length,
94                                   unsigned int packets,
95                                   unsigned int packet_length,
96                                   unsigned int timeout,
97                                   const UsbTransferCallback& callback) = 0;
98
99 protected:
100  friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
101
102  UsbDeviceHandle() {};
103
104  virtual ~UsbDeviceHandle() {};
105
106  DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
107};
108
109}  // namespace device
110
111#endif  // DEVICE_USB_USB_DEVICE_HANDLE_H_
112