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.
5 #ifndef DEVICE_HID_HID_CONNECTION_H_
6 #define DEVICE_HID_HID_CONNECTION_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/threading/thread_checker.h"
13 #include "device/hid/hid_device_info.h"
14 #include "net/base/io_buffer.h"
18 class HidConnection
: public base::RefCountedThreadSafe
<HidConnection
> {
20 enum SpecialReportIds
{
25 typedef base::Callback
<
26 void(bool success
, scoped_refptr
<net::IOBuffer
> buffer
, size_t size
)>
28 typedef base::Callback
<void(bool success
)> WriteCallback
;
30 scoped_refptr
<HidDeviceInfo
> device_info() const { return device_info_
; }
31 bool has_protected_collection() const { return has_protected_collection_
; }
32 const base::ThreadChecker
& thread_checker() const { return thread_checker_
; }
33 bool closed() const { return closed_
; }
35 // Closes the connection. This must be called before the object is freed.
38 // The report ID (or 0 if report IDs are not supported by the device) is
39 // always returned in the first byte of the buffer.
40 void Read(const ReadCallback
& callback
);
42 // The report ID (or 0 if report IDs are not supported by the device) is
43 // always expected in the first byte of the buffer.
44 void Write(scoped_refptr
<net::IOBuffer
> buffer
,
46 const WriteCallback
& callback
);
48 // The buffer will contain whatever report data was received from the device.
49 // This may include the report ID. The report ID is not stripped because a
50 // device may respond with other data in place of the report ID.
51 void GetFeatureReport(uint8_t report_id
, const ReadCallback
& callback
);
53 // The report ID (or 0 if report IDs are not supported by the device) is
54 // always expected in the first byte of the buffer.
55 void SendFeatureReport(scoped_refptr
<net::IOBuffer
> buffer
,
57 const WriteCallback
& callback
);
60 friend class base::RefCountedThreadSafe
<HidConnection
>;
62 explicit HidConnection(scoped_refptr
<HidDeviceInfo
> device_info
);
63 virtual ~HidConnection();
65 virtual void PlatformClose() = 0;
66 virtual void PlatformRead(const ReadCallback
& callback
) = 0;
67 virtual void PlatformWrite(scoped_refptr
<net::IOBuffer
> buffer
,
69 const WriteCallback
& callback
) = 0;
70 virtual void PlatformGetFeatureReport(uint8_t report_id
,
71 const ReadCallback
& callback
) = 0;
72 virtual void PlatformSendFeatureReport(scoped_refptr
<net::IOBuffer
> buffer
,
74 const WriteCallback
& callback
) = 0;
76 // PlatformRead implementation must call this method on read
77 // success, rather than directly running the callback.
78 // In case incoming buffer is empty or protected, it is filtered
79 // and this method returns false. Otherwise it runs the callback
81 bool CompleteRead(scoped_refptr
<net::IOBuffer
> buffer
,
83 const ReadCallback
& callback
);
86 bool IsReportIdProtected(uint8_t report_id
);
88 scoped_refptr
<HidDeviceInfo
> device_info_
;
89 bool has_protected_collection_
;
90 base::ThreadChecker thread_checker_
;
93 DISALLOW_COPY_AND_ASSIGN(HidConnection
);
96 struct PendingHidReport
{
100 scoped_refptr
<net::IOBuffer
> buffer
;
104 struct PendingHidRead
{
108 HidConnection::ReadCallback callback
;
111 } // namespace device
113 #endif // DEVICE_HID_HID_CONNECTION_H_