OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/hid/hid_connection_mac.h" | 5 #include "device/hid/hid_connection_mac.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/threading/thread_restrictions.h" |
10 #include "device/hid/hid_connection_mac.h" | 11 #include "device/hid/hid_connection_mac.h" |
11 | 12 |
12 namespace device { | 13 namespace device { |
13 | 14 |
14 HidConnectionMac::HidConnectionMac(HidDeviceInfo device_info) | 15 HidConnectionMac::HidConnectionMac(HidDeviceInfo device_info) |
15 : HidConnection(device_info), | 16 : HidConnection(device_info), |
16 device_(device_info.device_id, base::scoped_policy::RETAIN) { | 17 device_(device_info.device_id, base::scoped_policy::RETAIN) { |
| 18 DCHECK(thread_checker_.CalledOnValidThread()); |
| 19 |
17 message_loop_ = base::MessageLoopProxy::current(); | 20 message_loop_ = base::MessageLoopProxy::current(); |
18 | 21 |
19 DCHECK(device_.get()); | 22 DCHECK(device_.get()); |
20 inbound_buffer_.reset((uint8_t*)malloc(device_info.max_input_report_size)); | 23 inbound_buffer_.reset((uint8_t*)malloc(device_info.input_report_size)); |
21 IOHIDDeviceRegisterInputReportCallback(device_.get(), | 24 IOHIDDeviceRegisterInputReportCallback(device_.get(), |
22 inbound_buffer_.get(), | 25 inbound_buffer_.get(), |
23 device_info.max_input_report_size, | 26 device_info.input_report_size, |
24 &HidConnectionMac::InputReportCallback, | 27 &HidConnectionMac::InputReportCallback, |
25 this); | 28 this); |
26 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); | 29 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); |
27 } | 30 } |
28 | 31 |
29 HidConnectionMac::~HidConnectionMac() { | 32 HidConnectionMac::~HidConnectionMac() { |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 34 |
| 35 while (!pending_reads_.empty()) { |
| 36 pending_reads_.front().callback.Run(false, 0); |
| 37 pending_reads_.pop(); |
| 38 } |
| 39 |
30 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); | 40 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); |
31 Flush(); | |
32 } | 41 } |
33 | 42 |
34 void HidConnectionMac::PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer, | 43 void HidConnectionMac::Read(scoped_refptr<net::IOBufferWithSize> buffer, |
35 const IOCallback& callback) { | 44 const IOCallback& callback) { |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); |
36 if (!device_) { | 46 if (!device_) { |
37 callback.Run(false, 0); | 47 callback.Run(false, 0); |
38 return; | 48 return; |
39 } | 49 } |
40 | 50 PendingHidRead read; |
41 PendingHidRead pending_read; | 51 read.buffer = buffer; |
42 pending_read.buffer = buffer; | 52 read.callback = callback; |
43 pending_read.callback = callback; | 53 pending_reads_.push(read); |
44 pending_reads_.push(pending_read); | |
45 ProcessReadQueue(); | 54 ProcessReadQueue(); |
46 } | 55 } |
47 | 56 |
48 void HidConnectionMac::PlatformWrite( | 57 void HidConnectionMac::Write(uint8_t report_id, |
| 58 scoped_refptr<net::IOBufferWithSize> buffer, |
| 59 const IOCallback& callback) { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback); |
| 62 } |
| 63 |
| 64 void HidConnectionMac::GetFeatureReport( |
49 uint8_t report_id, | 65 uint8_t report_id, |
50 scoped_refptr<net::IOBufferWithSize> buffer, | 66 scoped_refptr<net::IOBufferWithSize> buffer, |
51 const IOCallback& callback) { | 67 const IOCallback& callback) { |
52 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
53 } | 69 if (device_info().feature_report_size == 0) { |
| 70 callback.Run(false, 0); |
| 71 return; |
| 72 } |
54 | 73 |
55 void HidConnectionMac::PlatformGetFeatureReport( | 74 if (buffer->size() < device_info().feature_report_size) { |
56 uint8_t report_id, | |
57 scoped_refptr<net::IOBufferWithSize> buffer, | |
58 const IOCallback& callback) { | |
59 if (!device_) { | |
60 callback.Run(false, 0); | 75 callback.Run(false, 0); |
61 return; | 76 return; |
62 } | 77 } |
63 | 78 |
64 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data()); | 79 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data()); |
65 CFIndex max_feature_report_size = device_info().max_feature_report_size; | 80 CFIndex feature_report_size = device_info().feature_report_size; |
66 IOReturn result = IOHIDDeviceGetReport(device_, | 81 IOReturn result = IOHIDDeviceGetReport(device_, |
67 kIOHIDReportTypeFeature, | 82 kIOHIDReportTypeFeature, |
68 report_id, | 83 report_id, |
69 feature_report_buffer, | 84 feature_report_buffer, |
70 &max_feature_report_size); | 85 &feature_report_size); |
71 if (result == kIOReturnSuccess) | 86 if (result == kIOReturnSuccess) |
72 callback.Run(true, max_feature_report_size); | 87 callback.Run(true, feature_report_size); |
73 else | 88 else |
74 callback.Run(false, 0); | 89 callback.Run(false, 0); |
75 } | 90 } |
76 | 91 |
77 void HidConnectionMac::PlatformSendFeatureReport( | 92 void HidConnectionMac::SendFeatureReport( |
78 uint8_t report_id, | 93 uint8_t report_id, |
79 scoped_refptr<net::IOBufferWithSize> buffer, | 94 scoped_refptr<net::IOBufferWithSize> buffer, |
80 const IOCallback& callback) { | 95 const IOCallback& callback) { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); |
81 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback); | 97 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback); |
82 } | 98 } |
83 | 99 |
84 void HidConnectionMac::InputReportCallback(void* context, | 100 void HidConnectionMac::InputReportCallback(void* context, |
85 IOReturn result, | 101 IOReturn result, |
86 void* sender, | 102 void* sender, |
87 IOHIDReportType type, | 103 IOHIDReportType type, |
88 uint32_t report_id, | 104 uint32_t report_id, |
89 uint8_t* report_bytes, | 105 uint8_t* report_bytes, |
90 CFIndex report_length) { | 106 CFIndex report_length) { |
91 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context); | 107 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context); |
92 // report_id is already contained in report_bytes | 108 // report_id is already contained in report_bytes |
93 scoped_refptr<net::IOBufferWithSize> buffer; | 109 scoped_refptr<net::IOBufferWithSize> buffer; |
94 buffer = new net::IOBufferWithSize(report_length); | 110 buffer = new net::IOBufferWithSize(report_length); |
95 memcpy(buffer->data(), report_bytes, report_length); | 111 memcpy(buffer->data(), report_bytes, report_length); |
96 | 112 |
97 connection->message_loop_->PostTask( | 113 connection->message_loop_->PostTask( |
98 FROM_HERE, | 114 FROM_HERE, |
99 base::Bind(&HidConnectionMac::ProcessInputReport, connection, buffer)); | 115 base::Bind( |
| 116 &HidConnectionMac::ProcessInputReport, connection, type, buffer)); |
| 117 } |
| 118 |
| 119 void HidConnectionMac::ProcessReadQueue() { |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 while (pending_reads_.size() && pending_reports_.size()) { |
| 122 PendingHidRead read = pending_reads_.front(); |
| 123 pending_reads_.pop(); |
| 124 PendingHidReport report = pending_reports_.front(); |
| 125 if (read.buffer->size() < report.buffer->size()) { |
| 126 read.callback.Run(false, report.buffer->size()); |
| 127 } else { |
| 128 memcpy(read.buffer->data(), report.buffer->data(), report.buffer->size()); |
| 129 pending_reports_.pop(); |
| 130 read.callback.Run(true, report.buffer->size()); |
| 131 } |
| 132 } |
| 133 } |
| 134 |
| 135 void HidConnectionMac::ProcessInputReport( |
| 136 IOHIDReportType type, |
| 137 scoped_refptr<net::IOBufferWithSize> buffer) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 PendingHidReport report; |
| 140 report.buffer = buffer; |
| 141 pending_reports_.push(report); |
| 142 ProcessReadQueue(); |
100 } | 143 } |
101 | 144 |
102 void HidConnectionMac::WriteReport(IOHIDReportType type, | 145 void HidConnectionMac::WriteReport(IOHIDReportType type, |
103 uint8_t report_id, | 146 uint8_t report_id, |
104 scoped_refptr<net::IOBufferWithSize> buffer, | 147 scoped_refptr<net::IOBufferWithSize> buffer, |
105 const IOCallback& callback) { | 148 const IOCallback& callback) { |
| 149 DCHECK(thread_checker_.CalledOnValidThread()); |
106 if (!device_) { | 150 if (!device_) { |
107 callback.Run(false, 0); | 151 callback.Run(false, 0); |
108 return; | 152 return; |
109 } | 153 } |
110 | |
111 scoped_refptr<net::IOBufferWithSize> output_buffer; | 154 scoped_refptr<net::IOBufferWithSize> output_buffer; |
112 if (report_id != 0) { | 155 if (report_id != 0) { |
113 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); | 156 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
114 output_buffer->data()[0] = static_cast<uint8_t>(report_id); | 157 output_buffer->data()[0] = static_cast<uint8_t>(report_id); |
115 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); | 158 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
116 } else { | 159 } else { |
117 output_buffer = new net::IOBufferWithSize(buffer->size()); | 160 output_buffer = new net::IOBufferWithSize(buffer->size()); |
118 memcpy(output_buffer->data(), buffer->data(), buffer->size()); | 161 memcpy(output_buffer->data(), buffer->data(), buffer->size()); |
119 } | 162 } |
120 IOReturn res = | 163 IOReturn res = |
121 IOHIDDeviceSetReport(device_.get(), | 164 IOHIDDeviceSetReport(device_.get(), |
122 type, | 165 type, |
123 report_id, | 166 report_id, |
124 reinterpret_cast<uint8_t*>(output_buffer->data()), | 167 reinterpret_cast<uint8_t*>(output_buffer->data()), |
125 output_buffer->size()); | 168 output_buffer->size()); |
126 if (res != kIOReturnSuccess) { | 169 if (res != kIOReturnSuccess) { |
127 callback.Run(false, 0); | 170 callback.Run(false, 0); |
128 } else { | 171 } else { |
129 callback.Run(true, output_buffer->size()); | 172 callback.Run(true, output_buffer->size()); |
130 } | 173 } |
131 } | 174 } |
132 | 175 |
133 void HidConnectionMac::Flush() { | |
134 while (!pending_reads_.empty()) { | |
135 pending_reads_.front().callback.Run(false, 0); | |
136 pending_reads_.pop(); | |
137 } | |
138 } | |
139 | |
140 void HidConnectionMac::ProcessInputReport( | |
141 scoped_refptr<net::IOBufferWithSize> buffer) { | |
142 DCHECK(thread_checker().CalledOnValidThread()); | |
143 PendingHidReport report; | |
144 report.buffer = buffer; | |
145 pending_reports_.push(report); | |
146 ProcessReadQueue(); | |
147 } | |
148 | |
149 void HidConnectionMac::ProcessReadQueue() { | |
150 DCHECK(thread_checker().CalledOnValidThread()); | |
151 while (pending_reads_.size() && pending_reports_.size()) { | |
152 PendingHidRead read = pending_reads_.front(); | |
153 PendingHidReport report = pending_reports_.front(); | |
154 | |
155 if (read.buffer->size() < report.buffer->size()) { | |
156 read.callback.Run(false, 0); | |
157 pending_reads_.pop(); | |
158 } else { | |
159 memcpy(read.buffer->data(), report.buffer->data(), report.buffer->size()); | |
160 pending_reports_.pop(); | |
161 | |
162 if (CompleteRead(report.buffer, read.callback)) { | |
163 pending_reads_.pop(); | |
164 } | |
165 } | |
166 } | |
167 } | |
168 | |
169 } // namespace device | 176 } // namespace device |
OLD | NEW |