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

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: addressed comments Created 6 years, 9 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"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread_restrictions.h" 10 #include "base/threading/thread_restrictions.h"
11 #include "base/tuple.h" 11 #include "device/hid/hid_connection_mac.h"
12 #include "device/hid/hid_service.h"
13 #include "device/hid/hid_service_mac.h"
14 #include "net/base/io_buffer.h"
15
16 #include <CoreFoundation/CoreFoundation.h>
17 #include <IOKit/hid/IOHIDManager.h>
18 12
19 namespace device { 13 namespace device {
20 14
21 HidConnectionMac::HidConnectionMac(HidServiceMac* service, 15 HidConnectionMac::HidConnectionMac(HidDeviceInfo device_info)
22 HidDeviceInfo device_info, 16 : HidConnection(device_info), device_(device_info.device_id) {
23 IOHIDDeviceRef device)
24 : HidConnection(device_info),
25 service_(service),
26 device_(device),
27 disconnected_(false) {
28 DCHECK(thread_checker_.CalledOnValidThread()); 17 DCHECK(thread_checker_.CalledOnValidThread());
29 18
30 message_loop_ = base::MessageLoopProxy::current(); 19 message_loop_ = base::MessageLoopProxy::current();
31 20
32 CFRetain(device); 21 DCHECK(device_.get());
33 inbound_buffer_.reset((uint8_t*) malloc(device_info.input_report_size + 1)); 22 CFRetain(device_.get());
Mark Mentovai 2014/02/26 17:41:44 I get that this is done because device_ is a Scope
Ken Rockot(use gerrit already) 2014/02/26 22:36:39 Done.
34 IOHIDDeviceRegisterInputReportCallback( 23 inbound_buffer_.reset((uint8_t*)malloc(device_info.input_report_size));
35 device_.get(), 24 IOHIDDeviceRegisterInputReportCallback(device_.get(),
36 inbound_buffer_.get(), 25 inbound_buffer_.get(),
37 device_info.input_report_size + 1, 26 device_info.input_report_size,
38 &HidConnectionMac::InputReportCallback, 27 &HidConnectionMac::InputReportCallback,
39 this); 28 this);
40 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); 29 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone);
41 } 30 }
31
32 void HidConnectionMac::Read(scoped_refptr<net::IOBufferWithSize> buffer,
33 const IOCallback& callback) {
34 DCHECK(thread_checker_.CalledOnValidThread());
35 if (!device_) {
36 callback.Run(false, 0);
37 return;
38 }
39 PendingHidRead read;
40 read.buffer = buffer;
41 read.callback = callback;
42 pending_reads_.push(read);
43 ProcessReadQueue();
44 }
45
46 void HidConnectionMac::Write(uint8_t report_id,
47 scoped_refptr<net::IOBufferWithSize> buffer,
48 const IOCallback& callback) {
49 DCHECK(thread_checker_.CalledOnValidThread());
50 WriteReport(kIOHIDReportTypeOutput, report_id, buffer, callback);
51 }
52
53 void HidConnectionMac::GetFeatureReport(
54 uint8_t report_id,
55 scoped_refptr<net::IOBufferWithSize> buffer,
56 const IOCallback& callback) {
57 DCHECK(thread_checker_.CalledOnValidThread());
58 if (device_info().feature_report_size == 0) {
59 callback.Run(false, 0);
60 return;
61 }
62
63 if (buffer->size() < device_info().feature_report_size) {
64 callback.Run(false, 0);
65 return;
66 }
67
68 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data());
69 CFIndex feature_report_size = device_info().feature_report_size;
70 IOReturn result = IOHIDDeviceGetReport(device_,
71 kIOHIDReportTypeFeature,
72 report_id,
73 feature_report_buffer,
74 &feature_report_size);
75 if (result == kIOReturnSuccess)
76 callback.Run(true, feature_report_size);
77 else
78 callback.Run(false, 0);
79 }
80
81 void HidConnectionMac::SendFeatureReport(
82 uint8_t report_id,
83 scoped_refptr<net::IOBufferWithSize> buffer,
84 const IOCallback& callback) {
85 DCHECK(thread_checker_.CalledOnValidThread());
86 WriteReport(kIOHIDReportTypeFeature, report_id, buffer, callback);
87 }
88
42 HidConnectionMac::~HidConnectionMac() { 89 HidConnectionMac::~HidConnectionMac() {
Mark Mentovai 2014/02/26 17:41:44 Usually the destructor would immediately follow th
Ken Rockot(use gerrit already) 2014/02/26 22:36:39 There's a conflict between this convention and the
43 DCHECK(thread_checker_.CalledOnValidThread()); 90 DCHECK(thread_checker_.CalledOnValidThread());
44 91
45 while (read_queue_.size()) { 92 while (!pending_reads_.empty()) {
46 read_queue_.front().c.Run(false, 0); 93 pending_reads_.front().callback.Run(false, 0);
47 read_queue_.pop(); 94 pending_reads_.pop();
48 } 95 }
49 96
50 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); 97 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone);
51 } 98 }
52 99
53 void HidConnectionMac::InputReportCallback(void * context, 100 void HidConnectionMac::InputReportCallback(void* context,
54 IOReturn result, 101 IOReturn result,
55 void * sender, 102 void* sender,
56 IOHIDReportType type, 103 IOHIDReportType type,
57 uint32_t reportID, 104 uint32_t report_id,
58 uint8_t * report, 105 uint8_t* report_bytes,
59 CFIndex reportLength) { 106 CFIndex report_length) {
60 HidConnectionMac* connection = reinterpret_cast<HidConnectionMac*>(context); 107 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context);
61 size_t length = reportLength + (reportID != 0); 108 // If a report ID was received, inject it into a copy of the received
62 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(length)); 109 // report. This is consistent with how input reports are received on
63 if (reportID) { 110 // other platforms.
64 buffer->data()[0] = reportID; 111 scoped_refptr<net::IOBufferWithSize> buffer;
65 memcpy(buffer->data() + 1, report, reportLength); 112 if (report_id != 0) {
113 buffer = new net::IOBufferWithSize(report_length + 1);
114 buffer->data()[0] = static_cast<uint8_t>(report_id);
115 memcpy(buffer->data() + 1, report_bytes, report_length);
66 } else { 116 } else {
67 memcpy(buffer->data(), report, reportLength); 117 buffer = new net::IOBufferWithSize(report_length);
118 memcpy(buffer->data(), report_bytes, report_length);
68 } 119 }
69 connection->message_loop_->PostTask( 120 connection->message_loop_->PostTask(
70 FROM_HERE, 121 FROM_HERE,
71 base::Bind(&HidConnectionMac::ProcessInputReport, 122 base::Bind(
72 connection, 123 &HidConnectionMac::ProcessInputReport, connection, type, buffer));
73 type,
74 buffer,
75 length));
76 } 124 }
77 125
78 void HidConnectionMac::ProcessReadQueue() { 126 void HidConnectionMac::ProcessReadQueue() {
79 DCHECK(thread_checker_.CalledOnValidThread()); 127 DCHECK(thread_checker_.CalledOnValidThread());
80 128 while (pending_reads_.size() && pending_reports_.size()) {
81 while(read_queue_.size() && input_reports_.size()) { 129 PendingHidRead read = pending_reads_.front();
82 PendingRead read = read_queue_.front(); 130 pending_reads_.pop();
83 read_queue_.pop(); 131 PendingHidReport report = pending_reports_.front();
84 PendingReport report = input_reports_.front(); 132 if (read.buffer->size() < report.buffer->size()) {
85 133 read.callback.Run(false, report.buffer->size());
86 if (read.b < report.second) {
87 read.c.Run(false, report.second);
88 } else { 134 } else {
89 memcpy(read.a->data(), report.first->data(), report.second); 135 memcpy(read.buffer->data(), report.buffer->data(), report.buffer->size());
90 input_reports_.pop(); 136 pending_reports_.pop();
91 read.c.Run(true, report.second); 137 read.callback.Run(true, report.buffer->size());
92 } 138 }
93 } 139 }
94 } 140 }
95 141
96 void HidConnectionMac::ProcessInputReport(IOHIDReportType type, 142 void HidConnectionMac::ProcessInputReport(
97 scoped_refptr<net::IOBuffer> report, 143 IOHIDReportType type,
98 CFIndex reportLength) { 144 scoped_refptr<net::IOBufferWithSize> buffer) {
99 DCHECK(thread_checker_.CalledOnValidThread()); 145 DCHECK(thread_checker_.CalledOnValidThread());
100 146 PendingHidReport report;
101 input_reports_.push(std::make_pair(report, reportLength)); 147 report.buffer = buffer;
148 pending_reports_.push(report);
102 ProcessReadQueue(); 149 ProcessReadQueue();
103 } 150 }
104 151
105 void HidConnectionMac::WriteReport(IOHIDReportType type, 152 void HidConnectionMac::WriteReport(IOHIDReportType type,
106 scoped_refptr<net::IOBuffer> buffer, 153 uint8_t report_id,
107 size_t size, 154 scoped_refptr<net::IOBufferWithSize> buffer,
108 const IOCallback& callback) { 155 const IOCallback& callback) {
109 DCHECK(thread_checker_.CalledOnValidThread()); 156 DCHECK(thread_checker_.CalledOnValidThread());
110 if (disconnected_ || !device_) { 157 if (!device_) {
111 callback.Run(false, 0); 158 callback.Run(false, 0);
112 return; 159 return;
113 } 160 }
114 const unsigned char* data_to_send = 161 IOReturn res = IOHIDDeviceSetReport(
115 reinterpret_cast<const unsigned char*>(buffer->data()); 162 device_.get(),
116 size_t length_to_send = size; 163 type,
117 if (data_to_send[0] == 0x0) { 164 report_id,
118 /* Not using numbered Reports. 165 reinterpret_cast<uint8_t*>(buffer->data()),
119 Don't send the report number. */ 166 buffer->size());
120 ++data_to_send;
121 --length_to_send;
122 }
123 IOReturn res = IOHIDDeviceSetReport(device_.get(),
124 type,
125 buffer->data()[0], /* Report ID*/
126 data_to_send,
127 length_to_send);
128 if (res != kIOReturnSuccess) { 167 if (res != kIOReturnSuccess) {
129 callback.Run(false, 0); 168 callback.Run(false, 0);
130 } else { 169 } else {
131 callback.Run(true, size); 170 callback.Run(true, buffer->size());
132 } 171 }
133 } 172 }
134 173
135 void HidConnectionMac::Read(scoped_refptr<net::IOBuffer> buffer,
136 size_t size,
137 const IOCallback& callback) {
138 DCHECK(thread_checker_.CalledOnValidThread());
139 if (disconnected_ || !device_) {
140 callback.Run(false, 0);
141 return;
142 }
143 read_queue_.push(MakeTuple(buffer, size, callback));
144 ProcessReadQueue();
145 }
146
147 void HidConnectionMac::Write(scoped_refptr<net::IOBuffer> buffer,
148 size_t size,
149 const IOCallback& callback) {
150 DCHECK(thread_checker_.CalledOnValidThread());
151 WriteReport(kIOHIDReportTypeOutput, buffer, size, callback);
152 }
153
154 void HidConnectionMac::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
155 size_t size,
156 const IOCallback& callback) {
157 DCHECK(thread_checker_.CalledOnValidThread());
158 WriteReport(kIOHIDReportTypeFeature, buffer, size, callback);
159 }
160
161 void HidConnectionMac::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer,
162 size_t size,
163 const IOCallback& callback) {
164 DCHECK(thread_checker_.CalledOnValidThread());
165 if (disconnected_ || !device_ || device_info_.feature_report_size == 0) {
166 callback.Run(false, 0);
167 return;
168 }
169
170 if (device_info_.feature_report_size != 0 &&
171 device_info_.feature_report_size != size) {
172 callback.Run(false, 0);
173 return;
174 }
175
176 CFIndex len = device_info_.feature_report_size;
177 IOReturn res = IOHIDDeviceGetReport(device_,
178 kIOHIDReportTypeFeature,
179 0,
180 (uint8_t*) buffer->data(),
181 &len);
182 if (res == kIOReturnSuccess)
183 callback.Run(true, len);
184 else
185 callback.Run(false, 0);
186 }
187
188 } // namespace device 174 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698