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 const 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_
; }
34 // The report ID (or 0 if report IDs are not supported by the device) is
35 // always returned in the first byte of the buffer.
36 void Read(const ReadCallback
& callback
);
38 // The report ID (or 0 if report IDs are not supported by the device) is
39 // always expected in the first byte of the buffer.
40 void Write(scoped_refptr
<net::IOBuffer
> buffer
,
42 const WriteCallback
& callback
);
44 // The report ID is not returned in the buffer.
45 void GetFeatureReport(uint8_t report_id
, const ReadCallback
& callback
);
47 // The report ID (or 0 if report IDs are not supported by the device) is
48 // always expected in the first byte of the buffer.
49 void SendFeatureReport(scoped_refptr
<net::IOBuffer
> buffer
,
51 const WriteCallback
& callback
);
54 friend class base::RefCountedThreadSafe
<HidConnection
>;
56 explicit HidConnection(const HidDeviceInfo
& device_info
);
57 virtual ~HidConnection();
59 virtual void PlatformRead(const ReadCallback
& callback
) = 0;
60 virtual void PlatformWrite(scoped_refptr
<net::IOBuffer
> buffer
,
62 const WriteCallback
& callback
) = 0;
63 virtual void PlatformGetFeatureReport(uint8_t report_id
,
64 const ReadCallback
& callback
) = 0;
65 virtual void PlatformSendFeatureReport(scoped_refptr
<net::IOBuffer
> buffer
,
67 const WriteCallback
& callback
) = 0;
69 // PlatformRead implementation must call this method on read
70 // success, rather than directly running the callback.
71 // In case incoming buffer is empty or protected, it is filtered
72 // and this method returns false. Otherwise it runs the callback
74 bool CompleteRead(scoped_refptr
<net::IOBuffer
> buffer
,
76 const ReadCallback
& callback
);
79 bool IsReportIdProtected(uint8_t report_id
);
81 const HidDeviceInfo device_info_
;
82 bool has_protected_collection_
;
83 base::ThreadChecker thread_checker_
;
85 DISALLOW_COPY_AND_ASSIGN(HidConnection
);
88 struct PendingHidReport
{
92 scoped_refptr
<net::IOBuffer
> buffer
;
96 struct PendingHidRead
{
100 HidConnection::ReadCallback callback
;
103 } // namespace device
105 #endif // DEVICE_HID_HID_CONNECTION_H_