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
|
// 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_HID_HID_CONNECTION_H_
#define DEVICE_HID_HID_CONNECTION_H_
#include <stdint.h>
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_checker.h"
#include "device/hid/hid_device_info.h"
#include "net/base/io_buffer.h"
namespace device {
class HidConnection : public base::RefCountedThreadSafe<HidConnection> {
public:
enum SpecialReportIds {
kNullReportId = 0x00,
kAnyReportId = 0xFF,
};
typedef base::Callback<
void(bool success, scoped_refptr<net::IOBuffer> buffer, size_t size)>
ReadCallback;
typedef base::Callback<void(bool success)> WriteCallback;
scoped_refptr<HidDeviceInfo> device_info() const { return device_info_; }
bool has_protected_collection() const { return has_protected_collection_; }
const base::ThreadChecker& thread_checker() const { return thread_checker_; }
bool closed() const { return closed_; }
// Closes the connection. This must be called before the object is freed.
void Close();
// The report ID (or 0 if report IDs are not supported by the device) is
// always returned in the first byte of the buffer.
void Read(const ReadCallback& callback);
// The report ID (or 0 if report IDs are not supported by the device) is
// always expected in the first byte of the buffer.
void Write(scoped_refptr<net::IOBuffer> buffer,
size_t size,
const WriteCallback& callback);
// The buffer will contain whatever report data was received from the device.
// This may include the report ID. The report ID is not stripped because a
// device may respond with other data in place of the report ID.
void GetFeatureReport(uint8_t report_id, const ReadCallback& callback);
// The report ID (or 0 if report IDs are not supported by the device) is
// always expected in the first byte of the buffer.
void SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
size_t size,
const WriteCallback& callback);
protected:
friend class base::RefCountedThreadSafe<HidConnection>;
explicit HidConnection(scoped_refptr<HidDeviceInfo> device_info);
virtual ~HidConnection();
virtual void PlatformClose() = 0;
virtual void PlatformRead(const ReadCallback& callback) = 0;
virtual void PlatformWrite(scoped_refptr<net::IOBuffer> buffer,
size_t size,
const WriteCallback& callback) = 0;
virtual void PlatformGetFeatureReport(uint8_t report_id,
const ReadCallback& callback) = 0;
virtual void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
size_t size,
const WriteCallback& callback) = 0;
// PlatformRead implementation must call this method on read
// success, rather than directly running the callback.
// In case incoming buffer is empty or protected, it is filtered
// and this method returns false. Otherwise it runs the callback
// and returns true.
bool CompleteRead(scoped_refptr<net::IOBuffer> buffer,
size_t size,
const ReadCallback& callback);
private:
bool IsReportIdProtected(uint8_t report_id);
scoped_refptr<HidDeviceInfo> device_info_;
bool has_protected_collection_;
base::ThreadChecker thread_checker_;
bool closed_;
DISALLOW_COPY_AND_ASSIGN(HidConnection);
};
struct PendingHidReport {
PendingHidReport();
~PendingHidReport();
scoped_refptr<net::IOBuffer> buffer;
size_t size;
};
struct PendingHidRead {
PendingHidRead();
~PendingHidRead();
HidConnection::ReadCallback callback;
};
} // namespace device
#endif // DEVICE_HID_HID_CONNECTION_H_
|