summaryrefslogtreecommitdiff
path: root/chromium/device/usb/usb_device_handle.h
blob: d592a8d4b2e48ce868a30262308cf1047708db94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_USB_USB_DEVICE_HANDLE_H_
#define DEVICE_USB_USB_DEVICE_HANDLE_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <vector>

#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string16.h"
#include "base/threading/thread_checker.h"
#include "device/usb/public/interfaces/device.mojom.h"
#include "device/usb/usb_descriptors.h"

namespace net {
class IOBuffer;
}

namespace device {

class UsbDevice;

using UsbTransferStatus = mojom::UsbTransferStatus;
using UsbControlTransferType = mojom::UsbControlTransferType;
using UsbControlTransferRecipient = mojom::UsbControlTransferRecipient;

// UsbDeviceHandle class provides basic I/O related functionalities.
class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> {
 public:
  struct IsochronousPacket {
    uint32_t length;
    uint32_t transferred_length;
    UsbTransferStatus status;
  };

  using ResultCallback = base::Callback<void(bool)>;
  using TransferCallback = base::Callback<
      void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>;
  using IsochronousTransferCallback =
      base::Callback<void(scoped_refptr<net::IOBuffer>,
                          const std::vector<IsochronousPacket>& packets)>;

  virtual scoped_refptr<UsbDevice> GetDevice() const = 0;

  // Notifies UsbDevice to drop the reference of this object; cancels all the
  // flying transfers.
  // It is possible that the object has no other reference after this call. So
  // if it is called using a raw pointer, it could be invalidated.
  // The platform device handle will be closed when UsbDeviceHandle destructs.
  virtual void Close() = 0;

  // Device manipulation operations.
  virtual void SetConfiguration(int configuration_value,
                                const ResultCallback& callback) = 0;
  virtual void ClaimInterface(int interface_number,
                              const ResultCallback& callback) = 0;
  virtual void ReleaseInterface(int interface_number,
                                const ResultCallback& callback) = 0;
  virtual void SetInterfaceAlternateSetting(int interface_number,
                                            int alternate_setting,
                                            const ResultCallback& callback) = 0;
  virtual void ResetDevice(const ResultCallback& callback) = 0;
  virtual void ClearHalt(uint8_t endpoint, const ResultCallback& callback) = 0;

  // The transfer functions may be called from any thread. The provided callback
  // will be run on the caller's thread.
  virtual void ControlTransfer(UsbTransferDirection direction,
                               UsbControlTransferType request_type,
                               UsbControlTransferRecipient recipient,
                               uint8_t request,
                               uint16_t value,
                               uint16_t index,
                               scoped_refptr<net::IOBuffer> buffer,
                               size_t length,
                               unsigned int timeout,
                               const TransferCallback& callback) = 0;

  virtual void IsochronousTransferIn(
      uint8_t endpoint_number,
      const std::vector<uint32_t>& packet_lengths,
      unsigned int timeout,
      const IsochronousTransferCallback& callback) = 0;

  virtual void IsochronousTransferOut(
      uint8_t endpoint_number,
      scoped_refptr<net::IOBuffer> buffer,
      const std::vector<uint32_t>& packet_lengths,
      unsigned int timeout,
      const IsochronousTransferCallback& callback) = 0;

  virtual void GenericTransfer(UsbTransferDirection direction,
                               uint8_t endpoint_number,
                               scoped_refptr<net::IOBuffer> buffer,
                               size_t length,
                               unsigned int timeout,
                               const TransferCallback& callback) = 0;

  // Gets the interface containing |endpoint_address|. Returns nullptr if no
  // claimed interface contains that endpoint.
  virtual const UsbInterfaceDescriptor* FindInterfaceByEndpoint(
      uint8_t endpoint_address) = 0;

 protected:
  friend class base::RefCountedThreadSafe<UsbDeviceHandle>;

  UsbDeviceHandle();
  virtual ~UsbDeviceHandle();

 private:
  DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
};

}  // namespace device

#endif  // DEVICE_USB_USB_DEVICE_HANDLE_H_