Chromium Code Reviews| 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/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" | |
| 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(HidServiceMac* service, |
| 22 HidDeviceInfo device_info, | 16 HidDeviceInfo device_info, |
| 23 IOHIDDeviceRef device) | 17 IOHIDDeviceRef device) |
| 24 : HidConnection(device_info), | 18 : HidConnection(device_info), service_(service), device_(device) { |
| 25 service_(service), | |
| 26 device_(device), | |
| 27 disconnected_(false) { | |
| 28 DCHECK(thread_checker_.CalledOnValidThread()); | 19 DCHECK(thread_checker_.CalledOnValidThread()); |
| 29 | 20 |
| 30 message_loop_ = base::MessageLoopProxy::current(); | 21 message_loop_ = base::MessageLoopProxy::current(); |
| 31 | 22 |
| 32 CFRetain(device); | 23 CFRetain(device); |
| 33 inbound_buffer_.reset((uint8_t*) malloc(device_info.input_report_size + 1)); | 24 inbound_buffer_.reset((uint8_t*) malloc(device_info.input_report_size + 1)); |
| 34 IOHIDDeviceRegisterInputReportCallback( | 25 IOHIDDeviceRegisterInputReportCallback( |
| 35 device_.get(), | 26 device_.get(), |
| 36 inbound_buffer_.get(), | 27 inbound_buffer_.get(), |
| 37 device_info.input_report_size + 1, | 28 device_info.input_report_size + 1, |
| 38 &HidConnectionMac::InputReportCallback, | 29 &HidConnectionMac::InputReportCallback, |
| 39 this); | 30 this); |
| 40 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); | 31 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); |
| 41 } | 32 } |
| 33 | |
| 42 HidConnectionMac::~HidConnectionMac() { | 34 HidConnectionMac::~HidConnectionMac() { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | 35 DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 | 36 |
| 45 while (read_queue_.size()) { | 37 while (!pending_reads_.empty()) { |
| 46 read_queue_.front().c.Run(false, 0); | 38 pending_reads_.front().callback.Run(false, 0); |
| 47 read_queue_.pop(); | 39 pending_reads_.pop(); |
| 48 } | 40 } |
| 49 | 41 |
| 50 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); | 42 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); |
| 51 } | 43 } |
| 52 | 44 |
| 53 void HidConnectionMac::InputReportCallback(void * context, | 45 void HidConnectionMac::InputReportCallback(void* context, |
| 54 IOReturn result, | 46 IOReturn result, |
| 55 void * sender, | 47 void* sender, |
| 56 IOHIDReportType type, | 48 IOHIDReportType type, |
| 57 uint32_t reportID, | 49 uint32_t report_id, |
| 58 uint8_t * report, | 50 uint8_t* report_bytes, |
| 59 CFIndex reportLength) { | 51 CFIndex reportLength) { |
| 60 HidConnectionMac* connection = reinterpret_cast<HidConnectionMac*>(context); | 52 HidConnectionMac* connection = static_cast<HidConnectionMac*>(context); |
| 61 size_t length = reportLength + (reportID != 0); | 53 |
| 62 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(length)); | 54 // A non-zero report ID requires an extra byte. |
| 63 if (reportID) { | 55 size_t length = reportLength + (report_id != 0 ? 1 : 0); |
| 64 buffer->data()[0] = reportID; | 56 scoped_refptr<net::IOBufferWithSize> buffer( |
| 65 memcpy(buffer->data() + 1, report, reportLength); | 57 new net::IOBufferWithSize(length)); |
| 58 if (report_id) { | |
| 59 buffer->data()[0] = report_id; | |
| 60 memcpy(buffer->data() + 1, report_bytes, reportLength); | |
| 66 } else { | 61 } else { |
| 67 memcpy(buffer->data(), report, reportLength); | 62 memcpy(buffer->data(), report_bytes, reportLength); |
| 68 } | 63 } |
| 69 connection->message_loop_->PostTask( | 64 connection->message_loop_->PostTask( |
| 70 FROM_HERE, | 65 FROM_HERE, |
| 71 base::Bind(&HidConnectionMac::ProcessInputReport, | 66 base::Bind( |
| 72 connection, | 67 &HidConnectionMac::ProcessInputReport, connection, type, buffer)); |
| 73 type, | |
| 74 buffer, | |
| 75 length)); | |
| 76 } | 68 } |
| 77 | 69 |
| 78 void HidConnectionMac::ProcessReadQueue() { | 70 void HidConnectionMac::ProcessReadQueue() { |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 | 72 while (pending_reads_.size() && pending_reports_.size()) { |
| 81 while(read_queue_.size() && input_reports_.size()) { | 73 PendingHidRead read = pending_reads_.front(); |
| 82 PendingRead read = read_queue_.front(); | 74 pending_reads_.pop(); |
| 83 read_queue_.pop(); | 75 PendingHidReport report = pending_reports_.front(); |
| 84 PendingReport report = input_reports_.front(); | 76 if (read.buffer->size() < report.buffer->size()) { |
| 85 | 77 read.callback.Run(false, report.buffer->size()); |
| 86 if (read.b < report.second) { | |
| 87 read.c.Run(false, report.second); | |
| 88 } else { | 78 } else { |
| 89 memcpy(read.a->data(), report.first->data(), report.second); | 79 memcpy(read.buffer->data(), report.buffer->data(), report.buffer->size()); |
| 90 input_reports_.pop(); | 80 pending_reports_.pop(); |
| 91 read.c.Run(true, report.second); | 81 read.callback.Run(true, report.buffer->size()); |
| 92 } | 82 } |
| 93 } | 83 } |
| 94 } | 84 } |
| 95 | 85 |
| 96 void HidConnectionMac::ProcessInputReport(IOHIDReportType type, | 86 void HidConnectionMac::ProcessInputReport( |
| 97 scoped_refptr<net::IOBuffer> report, | 87 IOHIDReportType type, |
| 98 CFIndex reportLength) { | 88 scoped_refptr<net::IOBufferWithSize> buffer) { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 | 90 PendingHidReport report; |
| 101 input_reports_.push(std::make_pair(report, reportLength)); | 91 report.buffer = buffer; |
| 92 pending_reports_.push(report); | |
| 102 ProcessReadQueue(); | 93 ProcessReadQueue(); |
| 103 } | 94 } |
| 104 | 95 |
| 105 void HidConnectionMac::WriteReport(IOHIDReportType type, | 96 void HidConnectionMac::WriteReport(IOHIDReportType type, |
| 106 scoped_refptr<net::IOBuffer> buffer, | 97 scoped_refptr<net::IOBufferWithSize> buffer, |
| 107 size_t size, | |
| 108 const IOCallback& callback) { | 98 const IOCallback& callback) { |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 110 if (disconnected_ || !device_) { | 100 if (!device_) { |
| 111 callback.Run(false, 0); | 101 callback.Run(false, 0); |
| 112 return; | 102 return; |
| 113 } | 103 } |
| 114 const unsigned char* data_to_send = | 104 const unsigned char* data_to_send = |
| 115 reinterpret_cast<const unsigned char*>(buffer->data()); | 105 reinterpret_cast<const unsigned char*>(buffer->data()); |
| 116 size_t length_to_send = size; | 106 size_t length_to_send = buffer->size(); |
| 117 if (data_to_send[0] == 0x0) { | 107 uint32_t report_id = static_cast<uint32_t>(buffer->data()[0]); |
| 118 /* Not using numbered Reports. | 108 if (report_id == 0) { |
| 119 Don't send the report number. */ | 109 // If the report ID is 0, don't actually send a leading report number. |
| 120 ++data_to_send; | 110 ++data_to_send; |
|
Mark Mentovai
2014/02/21 17:48:18
Weird indentation in this block.
Ken Rockot(use gerrit already)
2014/02/22 01:17:04
Done.
| |
| 121 --length_to_send; | 111 --length_to_send; |
| 122 } | 112 } |
| 123 IOReturn res = IOHIDDeviceSetReport(device_.get(), | 113 IOReturn res = IOHIDDeviceSetReport( |
| 124 type, | 114 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) { | 115 if (res != kIOReturnSuccess) { |
| 129 callback.Run(false, 0); | 116 callback.Run(false, 0); |
| 130 } else { | 117 } else { |
| 131 callback.Run(true, size); | 118 callback.Run(true, buffer->size()); |
| 132 } | 119 } |
| 133 } | 120 } |
| 134 | 121 |
| 135 void HidConnectionMac::Read(scoped_refptr<net::IOBuffer> buffer, | 122 void HidConnectionMac::Read(scoped_refptr<net::IOBufferWithSize> buffer, |
| 136 size_t size, | |
| 137 const IOCallback& callback) { | 123 const IOCallback& callback) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 if (disconnected_ || !device_) { | 125 if (!device_) { |
| 140 callback.Run(false, 0); | 126 callback.Run(false, 0); |
| 141 return; | 127 return; |
| 142 } | 128 } |
| 143 read_queue_.push(MakeTuple(buffer, size, callback)); | 129 PendingHidRead read; |
| 130 read.buffer = buffer; | |
| 131 read.callback = callback; | |
| 132 pending_reads_.push(read); | |
| 144 ProcessReadQueue(); | 133 ProcessReadQueue(); |
| 145 } | 134 } |
| 146 | 135 |
| 147 void HidConnectionMac::Write(scoped_refptr<net::IOBuffer> buffer, | 136 void HidConnectionMac::Write(scoped_refptr<net::IOBufferWithSize> buffer, |
| 148 size_t size, | |
| 149 const IOCallback& callback) { | 137 const IOCallback& callback) { |
| 150 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 151 WriteReport(kIOHIDReportTypeOutput, buffer, size, callback); | 139 WriteReport(kIOHIDReportTypeOutput, buffer, callback); |
| 152 } | 140 } |
| 153 | 141 |
| 154 void HidConnectionMac::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer, | 142 void HidConnectionMac::SendFeatureReport( |
| 155 size_t size, | 143 scoped_refptr<net::IOBufferWithSize> buffer, |
| 156 const IOCallback& callback) { | 144 const IOCallback& callback) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); | 145 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 WriteReport(kIOHIDReportTypeFeature, buffer, size, callback); | 146 WriteReport(kIOHIDReportTypeFeature, buffer, callback); |
| 159 } | 147 } |
| 160 | 148 |
| 161 void HidConnectionMac::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer, | 149 void HidConnectionMac::GetFeatureReport( |
| 162 size_t size, | 150 scoped_refptr<net::IOBufferWithSize> buffer, |
| 163 const IOCallback& callback) { | 151 const IOCallback& callback) { |
| 164 DCHECK(thread_checker_.CalledOnValidThread()); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 if (disconnected_ || !device_ || device_info_.feature_report_size == 0) { | 153 if (!device_ || device_info().feature_report_size == 0) { |
| 166 callback.Run(false, 0); | 154 callback.Run(false, 0); |
| 167 return; | 155 return; |
| 168 } | 156 } |
| 169 | 157 |
| 170 if (device_info_.feature_report_size != 0 && | 158 if (device_info().feature_report_size != 0 && |
| 171 device_info_.feature_report_size != size) { | 159 device_info().feature_report_size != buffer->size()) { |
| 172 callback.Run(false, 0); | 160 callback.Run(false, 0); |
| 173 return; | 161 return; |
| 174 } | 162 } |
| 175 | 163 |
| 176 CFIndex len = device_info_.feature_report_size; | 164 uint8_t* feature_report_buffer = reinterpret_cast<uint8_t*>(buffer->data()); |
| 177 IOReturn res = IOHIDDeviceGetReport(device_, | 165 CFIndex feature_report_size = device_info().feature_report_size; |
| 178 kIOHIDReportTypeFeature, | 166 IOReturn result = IOHIDDeviceGetReport(device_, |
| 179 0, | 167 kIOHIDReportTypeFeature, |
| 180 (uint8_t*) buffer->data(), | 168 0, |
| 181 &len); | 169 feature_report_buffer, |
| 182 if (res == kIOReturnSuccess) | 170 &feature_report_size); |
| 183 callback.Run(true, len); | 171 if (result == kIOReturnSuccess) |
| 172 callback.Run(true, feature_report_size); | |
| 184 else | 173 else |
| 185 callback.Run(false, 0); | 174 callback.Run(false, 0); |
| 186 } | 175 } |
| 187 | 176 |
| 188 } // namespace device | 177 } // namespace device |
| OLD | NEW |