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

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: Filter reports + refactor HID connections (ongoing) Created 6 years, 6 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
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 : HidConnection2(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);
41 } 31 }
42 32
43 void HidConnectionMac::Read(scoped_refptr<net::IOBufferWithSize> buffer, 33 void HidConnectionMac::PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer,
44 const IOCallback& callback) { 34 const IOCallback& callback) {
45 DCHECK(thread_checker_.CalledOnValidThread());
46 if (!device_) { 35 if (!device_) {
47 callback.Run(false, 0); 36 callback.Run(false, 0);
48 return; 37 return;
49 } 38 }
50 PendingHidRead read; 39
51 read.buffer = buffer; 40 HidConnection2::PlatformRead(buffer, callback);
52 read.callback = callback;
53 pending_reads_.push(read);
54 ProcessReadQueue();
55 } 41 }
56 42
57 void HidConnectionMac::Write(uint8_t report_id, 43 void HidConnectionMac::PlatformWrite(
58 scoped_refptr<net::IOBufferWithSize> buffer, 44 uint8_t report_id,
59 const IOCallback& callback) { 45 scoped_refptr<net::IOBufferWithSize> buffer,
60 DCHECK(thread_checker_.CalledOnValidThread()); 46 const IOCallback& callback) {
61 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback); 47 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback);
62 } 48 }
63 49
64 void HidConnectionMac::GetFeatureReport( 50 void HidConnectionMac::PlatformGetFeatureReport(
65 uint8_t report_id, 51 uint8_t report_id,
66 scoped_refptr<net::IOBufferWithSize> buffer, 52 scoped_refptr<net::IOBufferWithSize> buffer,
67 const IOCallback& callback) { 53 const IOCallback& callback) {
68 DCHECK(thread_checker_.CalledOnValidThread()); 54 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); 55 callback.Run(false, 0);
76 return; 56 return;
77 } 57 }
78 58
79 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data()); 59 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data());
80 CFIndex feature_report_size = device_info().feature_report_size; 60 CFIndex max_feature_report_size = device_info().max_feature_report_size;
81 IOReturn result = IOHIDDeviceGetReport(device_, 61 IOReturn result = IOHIDDeviceGetReport(device_,
82 kIOHIDReportTypeFeature, 62 kIOHIDReportTypeFeature,
83 report_id, 63 report_id,
84 feature_report_buffer, 64 feature_report_buffer,
85 &feature_report_size); 65 &max_feature_report_size);
86 if (result == kIOReturnSuccess) 66 if (result == kIOReturnSuccess)
87 callback.Run(true, feature_report_size); 67 callback.Run(true, max_feature_report_size);
88 else 68 else
89 callback.Run(false, 0); 69 callback.Run(false, 0);
90 } 70 }
91 71
92 void HidConnectionMac::SendFeatureReport( 72 void HidConnectionMac::PlatformSendFeatureReport(
93 uint8_t report_id, 73 uint8_t report_id,
94 scoped_refptr<net::IOBufferWithSize> buffer, 74 scoped_refptr<net::IOBufferWithSize> buffer,
95 const IOCallback& callback) { 75 const IOCallback& callback) {
96 DCHECK(thread_checker_.CalledOnValidThread());
97 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback); 76 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback);
98 } 77 }
99 78
100 void HidConnectionMac::InputReportCallback(void* context, 79 void HidConnectionMac::InputReportCallback(void* context,
101 IOReturn result, 80 IOReturn result,
102 void* sender, 81 void* sender,
103 IOHIDReportType type, 82 IOHIDReportType type,
104 uint32_t report_id, 83 uint32_t report_id,
105 uint8_t* report_bytes, 84 uint8_t* report_bytes,
106 CFIndex report_length) { 85 CFIndex report_length) {
107 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context); 86 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context);
108 // report_id is already contained in report_bytes 87 // report_id is already contained in report_bytes
109 scoped_refptr<net::IOBufferWithSize> buffer; 88 scoped_refptr<net::IOBufferWithSize> buffer;
110 buffer = new net::IOBufferWithSize(report_length); 89 buffer = new net::IOBufferWithSize(report_length);
111 memcpy(buffer->data(), report_bytes, report_length); 90 memcpy(buffer->data(), report_bytes, report_length);
112 91
113 connection->message_loop_->PostTask( 92 connection->message_loop_->PostTask(
114 FROM_HERE, 93 FROM_HERE,
115 base::Bind( 94 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 } 95 }
144 96
145 void HidConnectionMac::WriteReport(IOHIDReportType type, 97 void HidConnectionMac::WriteReport(IOHIDReportType type,
146 uint8_t report_id, 98 uint8_t report_id,
147 scoped_refptr<net::IOBufferWithSize> buffer, 99 scoped_refptr<net::IOBufferWithSize> buffer,
148 const IOCallback& callback) { 100 const IOCallback& callback) {
149 DCHECK(thread_checker_.CalledOnValidThread());
150 if (!device_) { 101 if (!device_) {
151 callback.Run(false, 0); 102 callback.Run(false, 0);
152 return; 103 return;
153 } 104 }
105
154 scoped_refptr<net::IOBufferWithSize> output_buffer; 106 scoped_refptr<net::IOBufferWithSize> output_buffer;
155 if (report_id != 0) { 107 if (report_id != 0) {
156 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); 108 output_buffer = new net::IOBufferWithSize(buffer->size() + 1);
157 output_buffer->data()[0] = static_cast<uint8_t>(report_id); 109 output_buffer->data()[0] = static_cast<uint8_t>(report_id);
158 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); 110 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size());
159 } else { 111 } else {
160 output_buffer = new net::IOBufferWithSize(buffer->size()); 112 output_buffer = new net::IOBufferWithSize(buffer->size());
161 memcpy(output_buffer->data(), buffer->data(), buffer->size()); 113 memcpy(output_buffer->data(), buffer->data(), buffer->size());
162 } 114 }
163 IOReturn res = 115 IOReturn res =
164 IOHIDDeviceSetReport(device_.get(), 116 IOHIDDeviceSetReport(device_.get(),
165 type, 117 type,
166 report_id, 118 report_id,
167 reinterpret_cast<uint8_t*>(output_buffer->data()), 119 reinterpret_cast<uint8_t*>(output_buffer->data()),
168 output_buffer->size()); 120 output_buffer->size());
169 if (res != kIOReturnSuccess) { 121 if (res != kIOReturnSuccess) {
170 callback.Run(false, 0); 122 callback.Run(false, 0);
171 } else { 123 } else {
172 callback.Run(true, output_buffer->size()); 124 callback.Run(true, output_buffer->size());
173 } 125 }
174 } 126 }
175 127
176 } // namespace device 128 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698