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

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

Issue 161823002: Clean up HID backend and API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: linux headers Created 6 years, 10 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 | Annotate | Revision Log
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/callback.h"
9 #include "base/mac/foundation_util.h" 8 #include "base/mac/foundation_util.h"
10 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
11 #include "base/tuple.h" 10 #include "device/hid/hid_connection_mac.h"
12 #include "device/hid/hid_service.h"
13 #include "device/hid/hid_service_mac.h" 11 #include "device/hid/hid_service_mac.h"
14 #include "net/base/io_buffer.h" 12 #include "net/base/io_buffer.h"
15 13
16 #include <CoreFoundation/CoreFoundation.h> 14 namespace device {
17 #include <IOKit/hid/IOHIDManager.h>
18 15
19 namespace device { 16 PendingHidReport::PendingHidReport() : size(0) {}
17
18 PendingHidReport::~PendingHidReport() {}
19
20 PendingHidRead::PendingHidRead() : size(0) {}
21
22 PendingHidRead::~PendingHidRead() {}
20 23
21 HidConnectionMac::HidConnectionMac(HidServiceMac* service, 24 HidConnectionMac::HidConnectionMac(HidServiceMac* service,
22 HidDeviceInfo device_info, 25 HidDeviceInfo device_info,
23 IOHIDDeviceRef device) 26 IOHIDDeviceRef device)
24 : HidConnection(device_info), 27 : HidConnection(device_info), service_(service), device_(device) {
25 service_(service),
26 device_(device),
27 disconnected_(false) {
28 DCHECK(thread_checker_.CalledOnValidThread()); 28 DCHECK(thread_checker_.CalledOnValidThread());
29 29
30 message_loop_ = base::MessageLoopProxy::current(); 30 message_loop_ = base::MessageLoopProxy::current();
31 31
32 CFRetain(device); 32 CFRetain(device);
33 inbound_buffer_.reset((uint8_t*) malloc(device_info.input_report_size + 1)); 33 inbound_buffer_.reset((uint8_t*) malloc(device_info.input_report_size + 1));
34 IOHIDDeviceRegisterInputReportCallback( 34 IOHIDDeviceRegisterInputReportCallback(
35 device_.get(), 35 device_.get(),
36 inbound_buffer_.get(), 36 inbound_buffer_.get(),
37 device_info.input_report_size + 1, 37 device_info.input_report_size + 1,
38 &HidConnectionMac::InputReportCallback, 38 &HidConnectionMac::InputReportCallback,
39 this); 39 this);
40 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); 40 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone);
41 } 41 }
42
42 HidConnectionMac::~HidConnectionMac() { 43 HidConnectionMac::~HidConnectionMac() {
43 DCHECK(thread_checker_.CalledOnValidThread()); 44 DCHECK(thread_checker_.CalledOnValidThread());
44 45
45 while (read_queue_.size()) { 46 while (!pending_reads_.empty()) {
46 read_queue_.front().c.Run(false, 0); 47 pending_reads_.front().callback.Run(false, 0);
47 read_queue_.pop(); 48 pending_reads_.pop();
48 } 49 }
49 50
50 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); 51 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone);
51 } 52 }
52 53
53 void HidConnectionMac::InputReportCallback(void * context, 54 void HidConnectionMac::InputReportCallback(void* context,
54 IOReturn result, 55 IOReturn result,
55 void * sender, 56 void* sender,
56 IOHIDReportType type, 57 IOHIDReportType type,
57 uint32_t reportID, 58 uint32_t report_id,
58 uint8_t * report, 59 uint8_t* report_bytes,
59 CFIndex reportLength) { 60 CFIndex reportLength) {
60 HidConnectionMac* connection = reinterpret_cast<HidConnectionMac*>(context); 61 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context);
61 size_t length = reportLength + (reportID != 0); 62
63 // A non-zero report ID requires an extra byte.
Mark Mentovai 2014/02/19 22:48:15 What’s special about 0 that you don’t need to put
Ken Rockot(use gerrit already) 2014/02/21 02:15:36 When working with a specific raw device, the devel
64 size_t length = reportLength + (report_id != 0 ? 1 : 0);
62 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(length)); 65 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(length));
63 if (reportID) { 66 if (report_id) {
64 buffer->data()[0] = reportID; 67 buffer->data()[0] = report_id;
Mark Mentovai 2014/02/19 22:48:15 report_id is uint32_t. This will truncate it. Is d
Ken Rockot(use gerrit already) 2014/02/21 02:15:36 The protocol only uses a single byte for a Report
65 memcpy(buffer->data() + 1, report, reportLength); 68 memcpy(buffer->data() + 1, report_bytes, reportLength);
66 } else { 69 } else {
67 memcpy(buffer->data(), report, reportLength); 70 memcpy(buffer->data(), report_bytes, reportLength);
68 } 71 }
69 connection->message_loop_->PostTask( 72 connection->message_loop_->PostTask(
70 FROM_HERE, 73 FROM_HERE,
71 base::Bind(&HidConnectionMac::ProcessInputReport, 74 base::Bind(&HidConnectionMac::ProcessInputReport,
72 connection, 75 connection,
73 type, 76 type,
74 buffer, 77 buffer,
75 length)); 78 length));
76 } 79 }
77 80
78 void HidConnectionMac::ProcessReadQueue() { 81 void HidConnectionMac::ProcessReadQueue() {
79 DCHECK(thread_checker_.CalledOnValidThread()); 82 DCHECK(thread_checker_.CalledOnValidThread());
80 83 while (pending_reads_.size() && pending_reports_.size()) {
81 while(read_queue_.size() && input_reports_.size()) { 84 PendingHidRead read = pending_reads_.front();
82 PendingRead read = read_queue_.front(); 85 pending_reads_.pop();
83 read_queue_.pop(); 86 PendingHidReport report = pending_reports_.front();
84 PendingReport report = input_reports_.front(); 87 if (read.size < report.size) {
85 88 read.callback.Run(false, report.size);
86 if (read.b < report.second) {
87 read.c.Run(false, report.second);
88 } else { 89 } else {
89 memcpy(read.a->data(), report.first->data(), report.second); 90 memcpy(read.buffer->data(), report.buffer->data(), report.size);
90 input_reports_.pop(); 91 pending_reports_.pop();
91 read.c.Run(true, report.second); 92 read.callback.Run(true, report.size);
92 } 93 }
93 } 94 }
94 } 95 }
95 96
96 void HidConnectionMac::ProcessInputReport(IOHIDReportType type, 97 void HidConnectionMac::ProcessInputReport(IOHIDReportType type,
97 scoped_refptr<net::IOBuffer> report, 98 scoped_refptr<net::IOBuffer> buffer,
98 CFIndex reportLength) { 99 CFIndex reportLength) {
99 DCHECK(thread_checker_.CalledOnValidThread()); 100 DCHECK(thread_checker_.CalledOnValidThread());
100 101 PendingHidReport report;
101 input_reports_.push(std::make_pair(report, reportLength)); 102 report.buffer = buffer;
103 report.size = reportLength;
104 pending_reports_.push(report);
102 ProcessReadQueue(); 105 ProcessReadQueue();
103 } 106 }
104 107
105 void HidConnectionMac::WriteReport(IOHIDReportType type, 108 void HidConnectionMac::WriteReport(IOHIDReportType type,
106 scoped_refptr<net::IOBuffer> buffer, 109 scoped_refptr<net::IOBuffer> buffer,
107 size_t size, 110 size_t size,
108 const IOCallback& callback) { 111 const IOCallback& callback) {
109 DCHECK(thread_checker_.CalledOnValidThread()); 112 DCHECK(thread_checker_.CalledOnValidThread());
110 if (disconnected_ || !device_) { 113 if (!device_) {
111 callback.Run(false, 0); 114 callback.Run(false, 0);
112 return; 115 return;
113 } 116 }
114 const unsigned char* data_to_send = 117 const unsigned char* data_to_send =
115 reinterpret_cast<const unsigned char*>(buffer->data()); 118 reinterpret_cast<const unsigned char*>(buffer->data());
116 size_t length_to_send = size; 119 size_t length_to_send = size;
117 if (data_to_send[0] == 0x0) { 120 uint32_t report_id = static_cast<uint32_t>(buffer->data()[0]);
Mark Mentovai 2014/02/19 22:48:15 Similar questions about interpretation ambiguity a
Ken Rockot(use gerrit already) 2014/02/21 02:15:36 There does not appear to be a great way of determi
118 /* Not using numbered Reports. 121 if (report_id == 0) {
119 Don't send the report number. */ 122 // If the report ID is 0, don't actually send a leading report number.
120 ++data_to_send; 123 ++data_to_send;
121 --length_to_send; 124 --length_to_send;
122 } 125 }
123 IOReturn res = IOHIDDeviceSetReport(device_.get(), 126 IOReturn res = IOHIDDeviceSetReport(
124 type, 127 device_.get(), type, report_id, data_to_send, length_to_send);
125 buffer->data()[0], /* Report ID*/
126 data_to_send,
127 length_to_send);
128 if (res != kIOReturnSuccess) { 128 if (res != kIOReturnSuccess) {
129 callback.Run(false, 0); 129 callback.Run(false, 0);
130 } else { 130 } else {
131 callback.Run(true, size); 131 callback.Run(true, size);
132 } 132 }
133 } 133 }
134 134
135 void HidConnectionMac::Read(scoped_refptr<net::IOBuffer> buffer, 135 void HidConnectionMac::Read(scoped_refptr<net::IOBuffer> buffer,
136 size_t size, 136 size_t size,
137 const IOCallback& callback) { 137 const IOCallback& callback) {
138 DCHECK(thread_checker_.CalledOnValidThread()); 138 DCHECK(thread_checker_.CalledOnValidThread());
139 if (disconnected_ || !device_) { 139 if (!device_) {
140 callback.Run(false, 0); 140 callback.Run(false, 0);
141 return; 141 return;
142 } 142 }
143 read_queue_.push(MakeTuple(buffer, size, callback)); 143 PendingHidRead read;
144 read.buffer = buffer;
145 read.size = size;
146 read.callback = callback;
147 pending_reads_.push(read);
144 ProcessReadQueue(); 148 ProcessReadQueue();
145 } 149 }
146 150
147 void HidConnectionMac::Write(scoped_refptr<net::IOBuffer> buffer, 151 void HidConnectionMac::Write(scoped_refptr<net::IOBuffer> buffer,
148 size_t size, 152 size_t size,
149 const IOCallback& callback) { 153 const IOCallback& callback) {
150 DCHECK(thread_checker_.CalledOnValidThread()); 154 DCHECK(thread_checker_.CalledOnValidThread());
151 WriteReport(kIOHIDReportTypeOutput, buffer, size, callback); 155 WriteReport(kIOHIDReportTypeOutput, buffer, size, callback);
152 } 156 }
153 157
154 void HidConnectionMac::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer, 158 void HidConnectionMac::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
155 size_t size, 159 size_t size,
156 const IOCallback& callback) { 160 const IOCallback& callback) {
157 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
158 WriteReport(kIOHIDReportTypeFeature, buffer, size, callback); 162 WriteReport(kIOHIDReportTypeFeature, buffer, size, callback);
159 } 163 }
160 164
161 void HidConnectionMac::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer, 165 void HidConnectionMac::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer,
162 size_t size, 166 size_t size,
163 const IOCallback& callback) { 167 const IOCallback& callback) {
164 DCHECK(thread_checker_.CalledOnValidThread()); 168 DCHECK(thread_checker_.CalledOnValidThread());
165 if (disconnected_ || !device_ || device_info_.feature_report_size == 0) { 169 if (!device_ || device_info_.feature_report_size == 0) {
166 callback.Run(false, 0); 170 callback.Run(false, 0);
167 return; 171 return;
168 } 172 }
169 173
170 if (device_info_.feature_report_size != 0 && 174 if (device_info_.feature_report_size != 0 &&
171 device_info_.feature_report_size != size) { 175 device_info_.feature_report_size != size) {
172 callback.Run(false, 0); 176 callback.Run(false, 0);
173 return; 177 return;
174 } 178 }
175 179
176 CFIndex len = device_info_.feature_report_size; 180 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data());
177 IOReturn res = IOHIDDeviceGetReport(device_, 181 CFIndex feature_report_size = device_info_.feature_report_size;
178 kIOHIDReportTypeFeature, 182 IOReturn result = IOHIDDeviceGetReport(device_,
179 0, 183 kIOHIDReportTypeFeature,
180 (uint8_t*) buffer->data(), 184 0,
181 &len); 185 feature_report_buffer,
182 if (res == kIOReturnSuccess) 186 &feature_report_size);
183 callback.Run(true, len); 187 if (result == kIOReturnSuccess)
188 callback.Run(true, feature_report_size);
184 else 189 else
185 callback.Run(false, 0); 190 callback.Run(false, 0);
186 } 191 }
187 192
188 } // namespace device 193 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698