1 // Copyright 2015 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.
5 #ifndef DEVICE_USB_DEVICE_IMPL_H_
6 #define DEVICE_USB_DEVICE_IMPL_H_
8 #include "base/callback_forward.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "device/devices_app/usb/public/interfaces/device.mojom.h"
13 #include "device/usb/usb_device_handle.h"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/callback.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
25 // Implementation of the public Device interface. Instances of this class are
26 // constructed by DeviceManagerImpl and are strongly bound to their MessagePipe
28 class DeviceImpl
: public Device
{
30 DeviceImpl(scoped_refptr
<UsbDevice
> device
,
31 mojo::InterfaceRequest
<Device
> request
);
32 ~DeviceImpl() override
;
35 using MojoTransferInCallback
=
36 mojo::Callback
<void(TransferStatus
, mojo::Array
<uint8_t>)>;
38 using MojoTransferOutCallback
= mojo::Callback
<void(TransferStatus
)>;
40 // Closes the device if it's open. This will always set |device_handle_| to
44 // Handles completion of an open request.
45 void OnOpen(const OpenCallback
& callback
,
46 scoped_refptr
<device::UsbDeviceHandle
> handle
);
48 // Handles completion of an inbound transfer on the UsbDeviceHandle.
49 void OnTransferIn(const MojoTransferInCallback
& callback
,
50 UsbTransferStatus status
,
51 scoped_refptr
<net::IOBuffer
> data
,
54 // Handles completion of an outbound transfer on the UsbDeviceHandle.
55 void OnTransferOut(const MojoTransferOutCallback
& callback
,
56 UsbTransferStatus status
,
57 scoped_refptr
<net::IOBuffer
> data
,
60 // Handles completion of an inbound isochronous transfer on the
62 void OnIsochronousTransferIn(const IsochronousTransferInCallback
& callback
,
63 uint32_t packet_length
,
64 UsbTransferStatus status
,
65 scoped_refptr
<net::IOBuffer
> data
,
68 // Handles completion of an outbound isochronous transfer on the
70 void OnIsochronousTransferOut(const MojoTransferOutCallback
& callback
,
71 UsbTransferStatus status
,
72 scoped_refptr
<net::IOBuffer
> data
,
75 // Device implementation:
76 void GetDeviceInfo(const GetDeviceInfoCallback
& callback
) override
;
77 void GetConfiguration(const GetConfigurationCallback
& callback
) override
;
78 void Open(const OpenCallback
& callback
) override
;
79 void Close(const CloseCallback
& callback
) override
;
80 void SetConfiguration(uint8_t value
,
81 const SetConfigurationCallback
& callback
) override
;
82 void ClaimInterface(uint8_t interface_number
,
83 const ClaimInterfaceCallback
& callback
) override
;
84 void ReleaseInterface(uint8_t interface_number
,
85 const ReleaseInterfaceCallback
& callback
) override
;
86 void SetInterfaceAlternateSetting(
87 uint8_t interface_number
,
88 uint8_t alternate_setting
,
89 const SetInterfaceAlternateSettingCallback
& callback
) override
;
90 void Reset(const ResetCallback
& callback
) override
;
91 void ClearHalt(uint8_t endpoint
, const ClearHaltCallback
& callback
) override
;
92 void ControlTransferIn(ControlTransferParamsPtr params
,
95 const ControlTransferInCallback
& callback
) override
;
96 void ControlTransferOut(ControlTransferParamsPtr params
,
97 mojo::Array
<uint8_t> data
,
99 const ControlTransferOutCallback
& callback
) override
;
100 void GenericTransferIn(uint8_t endpoint_number
,
103 const GenericTransferInCallback
& callback
) override
;
104 void GenericTransferOut(uint8_t endpoint_number
,
105 mojo::Array
<uint8_t> data
,
107 const GenericTransferOutCallback
& callback
) override
;
108 void IsochronousTransferIn(
109 uint8_t endpoint_number
,
110 uint32_t num_packets
,
111 uint32_t packet_length
,
113 const IsochronousTransferInCallback
& callback
) override
;
114 void IsochronousTransferOut(
115 uint8_t endpoint_number
,
116 mojo::Array
<mojo::Array
<uint8_t>> packets
,
118 const IsochronousTransferOutCallback
& callback
) override
;
120 mojo::StrongBinding
<Device
> binding_
;
122 scoped_refptr
<UsbDevice
> device_
;
123 // The device handle. Will be null before the device is opened and after it
125 scoped_refptr
<UsbDeviceHandle
> device_handle_
;
127 base::WeakPtrFactory
<DeviceImpl
> weak_factory_
;
129 DISALLOW_COPY_AND_ASSIGN(DeviceImpl
);
133 } // namespace device
135 #endif // DEVICE_USB_DEVICE_IMPL_H_