| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/hid/hid_connection_mac.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "device/hid/hid_service.h" |
| 12 #include "device/hid/hid_service_mac.h" |
| 13 #include "net/base/io_buffer.h" |
| 14 |
| 15 #include <CoreFoundation/CoreFoundation.h> |
| 16 #include <IOKit/hid/IOHIDManager.h> |
| 17 |
| 18 namespace device { |
| 19 |
| 20 HidConnectionMac::HidConnectionMac(HidServiceMac* service, |
| 21 HidDeviceInfo device_info, |
| 22 IOHIDDeviceRef device) |
| 23 : HidConnection(device_info), |
| 24 service_(service), |
| 25 device_(device), |
| 26 disconnected_(false) { |
| 27 DCHECK(thread_checker_.CalledOnValidThread()); |
| 28 message_loop_ = base::MessageLoopProxy::current(); |
| 29 |
| 30 CFRetain(device); |
| 31 inbound_buffer_.reset((uint8_t*) malloc(device_info.input_report_size + 1)); |
| 32 IOHIDDeviceRegisterInputReportCallback( |
| 33 device_.get(), |
| 34 inbound_buffer_.get(), |
| 35 device_info.input_report_size + 1, |
| 36 &HidConnectionMac::InputReportCallback, |
| 37 this); |
| 38 IOHIDDeviceOpen(device_, kIOHIDOptionsTypeNone); |
| 39 } |
| 40 HidConnectionMac::~HidConnectionMac() { |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 |
| 43 while (read_queue_.size()) { |
| 44 read_queue_.front().Run(false, NULL, 0); |
| 45 read_queue_.pop(); |
| 46 } |
| 47 |
| 48 IOHIDDeviceClose(device_, kIOHIDOptionsTypeNone); |
| 49 } |
| 50 |
| 51 void HidConnectionMac::InputReportCallback(void * context, |
| 52 IOReturn result, |
| 53 void * sender, |
| 54 IOHIDReportType type, |
| 55 uint32_t reportID, |
| 56 uint8_t * report, |
| 57 CFIndex reportLength) { |
| 58 HidConnectionMac* connection = reinterpret_cast<HidConnectionMac*>(context); |
| 59 size_t length = reportLength + (reportID != 0); |
| 60 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(length)); |
| 61 if (reportID) { |
| 62 buffer->data()[0] = reportID; |
| 63 memcpy(buffer->data() + 1, report, reportLength); |
| 64 } else { |
| 65 memcpy(buffer->data(), report, reportLength); |
| 66 } |
| 67 connection->message_loop_->PostTask( |
| 68 FROM_HERE, |
| 69 base::Bind(&HidConnectionMac::ProcessInputReport, |
| 70 connection, |
| 71 type, |
| 72 buffer, |
| 73 length)); |
| 74 } |
| 75 |
| 76 void HidConnectionMac::ProcessReadQueue() { |
| 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 78 |
| 79 while(read_queue_.size() && input_reports_.size()) { |
| 80 HidIOCallback callback = read_queue_.front(); |
| 81 read_queue_.pop(); |
| 82 std::pair<scoped_refptr<net::IOBuffer>, size_t> report = |
| 83 input_reports_.front(); |
| 84 input_reports_.pop(); |
| 85 callback.Run(true, report.first, report.second); |
| 86 } |
| 87 } |
| 88 |
| 89 void HidConnectionMac::ProcessInputReport(IOHIDReportType type, |
| 90 scoped_refptr<net::IOBuffer> report, |
| 91 CFIndex reportLength) { |
| 92 DCHECK(thread_checker_.CalledOnValidThread()); |
| 93 |
| 94 input_reports_.push(std::make_pair(report, reportLength)); |
| 95 ProcessReadQueue(); |
| 96 } |
| 97 |
| 98 void HidConnectionMac::WriteReport(IOHIDReportType type, |
| 99 scoped_refptr<net::IOBuffer> buffer, |
| 100 size_t size, |
| 101 const HidIOCallback& callback) { |
| 102 DCHECK(thread_checker_.CalledOnValidThread()); |
| 103 if (disconnected_ || !device_) { |
| 104 callback.Run(false, NULL, 0); |
| 105 return; |
| 106 } |
| 107 const unsigned char* data_to_send = |
| 108 reinterpret_cast<const unsigned char*>(buffer->data()); |
| 109 size_t length_to_send = size; |
| 110 if (data_to_send[0] == 0x0) { |
| 111 /* Not using numbered Reports. |
| 112 Don't send the report number. */ |
| 113 ++data_to_send; |
| 114 --length_to_send; |
| 115 } |
| 116 IOReturn res = IOHIDDeviceSetReport(device_.get(), |
| 117 type, |
| 118 buffer->data()[0], /* Report ID*/ |
| 119 data_to_send, |
| 120 length_to_send); |
| 121 if (res != kIOReturnSuccess) { |
| 122 callback.Run(false, NULL, 0); |
| 123 } else { |
| 124 callback.Run(true, NULL, size); |
| 125 } |
| 126 } |
| 127 |
| 128 void HidConnectionMac::Read(const HidIOCallback& callback) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 if (disconnected_ || !device_) { |
| 131 callback.Run(false, NULL, 0); |
| 132 return; |
| 133 } |
| 134 read_queue_.push(callback); |
| 135 ProcessReadQueue(); |
| 136 } |
| 137 |
| 138 void HidConnectionMac::Write(scoped_refptr<net::IOBuffer> buffer, |
| 139 size_t size, |
| 140 const HidIOCallback& callback) { |
| 141 DCHECK(thread_checker_.CalledOnValidThread()); |
| 142 WriteReport(kIOHIDReportTypeOutput, buffer, size, callback); |
| 143 } |
| 144 |
| 145 void HidConnectionMac::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer, |
| 146 size_t size, |
| 147 const HidIOCallback& callback) { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 WriteReport(kIOHIDReportTypeFeature, buffer, size, callback); |
| 150 } |
| 151 |
| 152 void HidConnectionMac::GetFeatureReport(const HidIOCallback& callback) { |
| 153 DCHECK(thread_checker_.CalledOnValidThread()); |
| 154 if (disconnected_ || !device_ || device_info_.feature_report_size == 0) { |
| 155 callback.Run(false, NULL, 0); |
| 156 return; |
| 157 } |
| 158 |
| 159 scoped_refptr<net::IOBuffer> buffer( |
| 160 new net::IOBuffer(device_info_.feature_report_size)); |
| 161 CFIndex len = device_info_.feature_report_size; |
| 162 |
| 163 IOReturn res = IOHIDDeviceGetReport(device_, |
| 164 kIOHIDReportTypeFeature, |
| 165 0, |
| 166 (uint8_t*) buffer->data(), |
| 167 &len); |
| 168 if (res == kIOReturnSuccess) |
| 169 callback.Run(true, buffer, len); |
| 170 else |
| 171 callback.Run(false, NULL, 0); |
| 172 } |
| 173 |
| 174 } // namespace device |
| OLD | NEW |