Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: device/hid/hid_connection_mac.cc

Issue 317783010: chrome.hid: enrich model with report IDs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix hid.idl comment Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/hid/hid_connection_mac.h ('k') | device/hid/hid_connection_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "device/hid/hid_connection_mac.h" 10 #include "device/hid/hid_connection_mac.h"
12 11
13 namespace device { 12 namespace device {
14 13
15 HidConnectionMac::HidConnectionMac(HidDeviceInfo device_info) 14 HidConnectionMac::HidConnectionMac(HidDeviceInfo device_info)
16 : HidConnection(device_info), 15 : HidConnection(device_info),
17 device_(device_info.device_id, base::scoped_policy::RETAIN) { 16 device_(device_info.device_id, base::scoped_policy::RETAIN) {
18 DCHECK(thread_checker_.CalledOnValidThread());
19
20 message_loop_ = base::MessageLoopProxy::current(); 17 message_loop_ = base::MessageLoopProxy::current();
21 18
22 DCHECK(device_.get()); 19 DCHECK(device_.get());
23 inbound_buffer_.reset((uint8_t*)malloc(device_info.input_report_size)); 20 inbound_buffer_.reset((uint8_t*)malloc(device_info.max_input_report_size));
24 IOHIDDeviceRegisterInputReportCallback(device_.get(), 21 IOHIDDeviceRegisterInputReportCallback(device_.get(),
25 inbound_buffer_.get(), 22 inbound_buffer_.get(),
26 device_info.input_report_size, 23 device_info.max_input_report_size,
27 &HidConnectionMac::InputReportCallback, 24 &HidConnectionMac::InputReportCallback,
28 this); 25 this);
29 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); 26 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone);
30 } 27 }
31 28
32 HidConnectionMac::~HidConnectionMac() { 29 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
40 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); 30 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone);
31 Flush();
41 } 32 }
42 33
43 void HidConnectionMac::Read(scoped_refptr<net::IOBufferWithSize> buffer, 34 void HidConnectionMac::PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer,
44 const IOCallback& callback) { 35 const IOCallback& callback) {
45 DCHECK(thread_checker_.CalledOnValidThread());
46 if (!device_) { 36 if (!device_) {
47 callback.Run(false, 0); 37 callback.Run(false, 0);
48 return; 38 return;
49 } 39 }
50 PendingHidRead read; 40
51 read.buffer = buffer; 41 PendingHidRead pending_read;
52 read.callback = callback; 42 pending_read.buffer = buffer;
53 pending_reads_.push(read); 43 pending_read.callback = callback;
44 pending_reads_.push(pending_read);
54 ProcessReadQueue(); 45 ProcessReadQueue();
55 } 46 }
56 47
57 void HidConnectionMac::Write(uint8_t report_id, 48 void HidConnectionMac::PlatformWrite(
58 scoped_refptr<net::IOBufferWithSize> buffer, 49 uint8_t report_id,
59 const IOCallback& callback) { 50 scoped_refptr<net::IOBufferWithSize> buffer,
60 DCHECK(thread_checker_.CalledOnValidThread()); 51 const IOCallback& callback) {
61 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback); 52 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback);
62 } 53 }
63 54
64 void HidConnectionMac::GetFeatureReport( 55 void HidConnectionMac::PlatformGetFeatureReport(
65 uint8_t report_id, 56 uint8_t report_id,
66 scoped_refptr<net::IOBufferWithSize> buffer, 57 scoped_refptr<net::IOBufferWithSize> buffer,
67 const IOCallback& callback) { 58 const IOCallback& callback) {
68 DCHECK(thread_checker_.CalledOnValidThread()); 59 if (!device_) {
69 if (device_info().feature_report_size == 0) {
70 callback.Run(false, 0);
71 return;
72 }
73
74 if (buffer->size() < device_info().feature_report_size) {
75 callback.Run(false, 0); 60 callback.Run(false, 0);
76 return; 61 return;
77 } 62 }
78 63
79 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data()); 64 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data());
80 CFIndex feature_report_size = device_info().feature_report_size; 65 CFIndex max_feature_report_size = device_info().max_feature_report_size;
81 IOReturn result = IOHIDDeviceGetReport(device_, 66 IOReturn result = IOHIDDeviceGetReport(device_,
82 kIOHIDReportTypeFeature, 67 kIOHIDReportTypeFeature,
83 report_id, 68 report_id,
84 feature_report_buffer, 69 feature_report_buffer,
85 &feature_report_size); 70 &max_feature_report_size);
86 if (result == kIOReturnSuccess) 71 if (result == kIOReturnSuccess)
87 callback.Run(true, feature_report_size); 72 callback.Run(true, max_feature_report_size);
88 else 73 else
89 callback.Run(false, 0); 74 callback.Run(false, 0);
90 } 75 }
91 76
92 void HidConnectionMac::SendFeatureReport( 77 void HidConnectionMac::PlatformSendFeatureReport(
93 uint8_t report_id, 78 uint8_t report_id,
94 scoped_refptr<net::IOBufferWithSize> buffer, 79 scoped_refptr<net::IOBufferWithSize> buffer,
95 const IOCallback& callback) { 80 const IOCallback& callback) {
96 DCHECK(thread_checker_.CalledOnValidThread());
97 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback); 81 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback);
98 } 82 }
99 83
100 void HidConnectionMac::InputReportCallback(void* context, 84 void HidConnectionMac::InputReportCallback(void* context,
101 IOReturn result, 85 IOReturn result,
102 void* sender, 86 void* sender,
103 IOHIDReportType type, 87 IOHIDReportType type,
104 uint32_t report_id, 88 uint32_t report_id,
105 uint8_t* report_bytes, 89 uint8_t* report_bytes,
106 CFIndex report_length) { 90 CFIndex report_length) {
107 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context); 91 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context);
108 // report_id is already contained in report_bytes 92 // report_id is already contained in report_bytes
109 scoped_refptr<net::IOBufferWithSize> buffer; 93 scoped_refptr<net::IOBufferWithSize> buffer;
110 buffer = new net::IOBufferWithSize(report_length); 94 buffer = new net::IOBufferWithSize(report_length);
111 memcpy(buffer->data(), report_bytes, report_length); 95 memcpy(buffer->data(), report_bytes, report_length);
112 96
113 connection->message_loop_->PostTask( 97 connection->message_loop_->PostTask(
114 FROM_HERE, 98 FROM_HERE,
115 base::Bind( 99 base::Bind(&HidConnectionMac::ProcessInputReport, connection, buffer));
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();
143 } 100 }
144 101
145 void HidConnectionMac::WriteReport(IOHIDReportType type, 102 void HidConnectionMac::WriteReport(IOHIDReportType type,
146 uint8_t report_id, 103 uint8_t report_id,
147 scoped_refptr<net::IOBufferWithSize> buffer, 104 scoped_refptr<net::IOBufferWithSize> buffer,
148 const IOCallback& callback) { 105 const IOCallback& callback) {
149 DCHECK(thread_checker_.CalledOnValidThread());
150 if (!device_) { 106 if (!device_) {
151 callback.Run(false, 0); 107 callback.Run(false, 0);
152 return; 108 return;
153 } 109 }
110
154 scoped_refptr<net::IOBufferWithSize> output_buffer; 111 scoped_refptr<net::IOBufferWithSize> output_buffer;
155 if (report_id != 0) { 112 if (report_id != 0) {
156 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); 113 output_buffer = new net::IOBufferWithSize(buffer->size() + 1);
157 output_buffer->data()[0] = static_cast<uint8_t>(report_id); 114 output_buffer->data()[0] = static_cast<uint8_t>(report_id);
158 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); 115 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size());
159 } else { 116 } else {
160 output_buffer = new net::IOBufferWithSize(buffer->size()); 117 output_buffer = new net::IOBufferWithSize(buffer->size());
161 memcpy(output_buffer->data(), buffer->data(), buffer->size()); 118 memcpy(output_buffer->data(), buffer->data(), buffer->size());
162 } 119 }
163 IOReturn res = 120 IOReturn res =
164 IOHIDDeviceSetReport(device_.get(), 121 IOHIDDeviceSetReport(device_.get(),
165 type, 122 type,
166 report_id, 123 report_id,
167 reinterpret_cast<uint8_t*>(output_buffer->data()), 124 reinterpret_cast<uint8_t*>(output_buffer->data()),
168 output_buffer->size()); 125 output_buffer->size());
169 if (res != kIOReturnSuccess) { 126 if (res != kIOReturnSuccess) {
170 callback.Run(false, 0); 127 callback.Run(false, 0);
171 } else { 128 } else {
172 callback.Run(true, output_buffer->size()); 129 callback.Run(true, output_buffer->size());
173 } 130 }
174 } 131 }
175 132
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
176 } // namespace device 169 } // namespace device
OLDNEW
« no previous file with comments | « device/hid/hid_connection_mac.h ('k') | device/hid/hid_connection_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698