| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
extern crate libc; |
| 6 |
extern crate log; |
| 7 |
|
| 8 |
use crate::transport::device_selector::DeviceSelectorEvent; |
| 9 |
use crate::transport::platform::iokit::*; |
| 10 |
use crate::util::io_err; |
| 11 |
use core_foundation::base::*; |
| 12 |
use core_foundation::runloop::*; |
| 13 |
use runloop::RunLoop; |
| 14 |
use std::collections::HashMap; |
| 15 |
use std::os::raw::c_void; |
| 16 |
use std::sync::mpsc::{channel, Receiver, Sender}; |
| 17 |
use std::{io, slice}; |
| 18 |
|
| 19 |
struct DeviceData { |
| 20 |
tx: Sender<Vec<u8>>, |
| 21 |
runloop: RunLoop, |
| 22 |
} |
| 23 |
|
| 24 |
pub struct Monitor<F> |
| 25 |
where |
| 26 |
F: Fn( |
| 27 |
(IOHIDDeviceRef, Receiver<Vec<u8>>), |
| 28 |
Sender<DeviceSelectorEvent>, |
| 29 |
Sender<crate::StatusUpdate>, |
| 30 |
&dyn Fn() -> bool, |
| 31 |
) + Send |
| 32 |
+ Sync |
| 33 |
+ 'static, |
| 34 |
{ |
| 35 |
manager: IOHIDManagerRef, |
| 36 |
// Keep alive until the monitor goes away. |
| 37 |
_matcher: IOHIDDeviceMatcher, |
| 38 |
map: HashMap<IOHIDDeviceRef, DeviceData>, |
| 39 |
new_device_cb: F, |
| 40 |
selector_sender: Sender<DeviceSelectorEvent>, |
| 41 |
status_sender: Sender<crate::StatusUpdate>, |
| 42 |
} |
| 43 |
|
| 44 |
impl<F> Monitor<F> |
| 45 |
where |
| 46 |
F: Fn( |
| 47 |
(IOHIDDeviceRef, Receiver<Vec<u8>>), |
| 48 |
Sender<DeviceSelectorEvent>, |
| 49 |
Sender<crate::StatusUpdate>, |
| 50 |
&dyn Fn() -> bool, |
| 51 |
) + Send |
| 52 |
+ Sync |
| 53 |
+ 'static, |
| 54 |
{ |
| 55 |
pub fn new( |
| 56 |
new_device_cb: F, |
| 57 |
selector_sender: Sender<DeviceSelectorEvent>, |
| 58 |
status_sender: Sender<crate::StatusUpdate>, |
| 59 |
) -> Self { |
| 60 |
let manager = unsafe { IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone) }; |
| 61 |
|
| 62 |
// Match FIDO devices only. |
| 63 |
let _matcher = IOHIDDeviceMatcher::new(); |
| 64 |
unsafe { IOHIDManagerSetDeviceMatching(manager, _matcher.dict.as_concrete_TypeRef()) }; |
| 65 |
|
| 66 |
Self { |
| 67 |
manager, |
| 68 |
_matcher, |
| 69 |
new_device_cb, |
| 70 |
map: HashMap::new(), |
| 71 |
selector_sender, |
| 72 |
status_sender, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
pub fn start(&mut self) -> io::Result<()> { |
| 77 |
let context = self as *mut Self as *mut c_void; |
| 78 |
|
| 79 |
unsafe { |
| 80 |
IOHIDManagerRegisterDeviceMatchingCallback( |
| 81 |
self.manager, |
| 82 |
Monitor::<F>::on_device_matching, |
| 83 |
context, |
| 84 |
); |
| 85 |
IOHIDManagerRegisterDeviceRemovalCallback( |
| 86 |
self.manager, |
| 87 |
Monitor::<F>::on_device_removal, |
| 88 |
context, |
| 89 |
); |
| 90 |
IOHIDManagerRegisterInputReportCallback( |
| 91 |
self.manager, |
| 92 |
Monitor::<F>::on_input_report, |
| 93 |
context, |
| 94 |
); |
| 95 |
|
| 96 |
IOHIDManagerScheduleWithRunLoop( |
| 97 |
self.manager, |
| 98 |
CFRunLoopGetCurrent(), |
| 99 |
kCFRunLoopDefaultMode, |
| 100 |
); |
| 101 |
|
| 102 |
let rv = IOHIDManagerOpen(self.manager, kIOHIDManagerOptionNone); |
| 103 |
if rv == 0 { |
| 104 |
Ok(()) |
| 105 |
} else { |
| 106 |
Err(io_err(&format!("Couldn't open HID Manager, rv={rv}"))) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
pub fn stop(&mut self) { |
| 112 |
// Remove all devices. |
| 113 |
while !self.map.is_empty() { |
| 114 |
let device_ref = *self.map.keys().next().unwrap(); |
| 115 |
self.remove_device(device_ref); |
| 116 |
} |
| 117 |
|
| 118 |
// Close the manager and its devices. |
| 119 |
unsafe { IOHIDManagerClose(self.manager, kIOHIDManagerOptionNone) }; |
| 120 |
} |
| 121 |
|
| 122 |
fn remove_device(&mut self, device_ref: IOHIDDeviceRef) { |
| 123 |
if let Some(DeviceData { tx, runloop }) = self.map.remove(&device_ref) { |
| 124 |
let _ = self |
| 125 |
.selector_sender |
| 126 |
.send(DeviceSelectorEvent::DeviceRemoved(device_ref)); |
| 127 |
// Dropping `tx` will make Device::read() fail eventually. |
| 128 |
drop(tx); |
| 129 |
|
| 130 |
// Wait until the runloop stopped. |
| 131 |
runloop.cancel(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
extern "C" fn on_input_report( |
| 136 |
context: *mut c_void, |
| 137 |
_: IOReturn, |
| 138 |
device_ref: IOHIDDeviceRef, |
| 139 |
_: IOHIDReportType, |
| 140 |
_: u32, |
| 141 |
report: *mut u8, |
| 142 |
report_len: CFIndex, |
| 143 |
) { |
| 144 |
let this = unsafe { &mut *(context as *mut Self) }; |
| 145 |
let mut send_failed = false; |
| 146 |
|
| 147 |
// Ignore the report if we can't find a device for it. |
| 148 |
if let Some(DeviceData { tx, .. }) = this.map.get(&device_ref) { |
| 149 |
let data = unsafe { slice::from_raw_parts(report, report_len as usize).to_vec() }; |
| 150 |
send_failed = tx.send(data).is_err(); |
| 151 |
} |
| 152 |
|
| 153 |
// Remove the device if sending fails. |
| 154 |
if send_failed { |
| 155 |
this.remove_device(device_ref); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
extern "C" fn on_device_matching( |
| 160 |
context: *mut c_void, |
| 161 |
_: IOReturn, |
| 162 |
_: *mut c_void, |
| 163 |
device_ref: IOHIDDeviceRef, |
| 164 |
) { |
| 165 |
let this = unsafe { &mut *(context as *mut Self) }; |
| 166 |
let _ = this |
| 167 |
.selector_sender |
| 168 |
.send(DeviceSelectorEvent::DevicesAdded(vec![device_ref])); |
| 169 |
let selector_sender = this.selector_sender.clone(); |
| 170 |
let status_sender = this.status_sender.clone(); |
| 171 |
let (tx, rx) = channel(); |
| 172 |
let f = &this.new_device_cb; |
| 173 |
|
| 174 |
// Create a new per-device runloop. |
| 175 |
let runloop = RunLoop::new(move |alive| { |
| 176 |
// Ensure that the runloop is still alive. |
| 177 |
if alive() { |
| 178 |
f((device_ref, rx), selector_sender, status_sender, alive); |
| 179 |
} |
| 180 |
}); |
| 181 |
|
| 182 |
if let Ok(runloop) = runloop { |
| 183 |
this.map.insert(device_ref, DeviceData { tx, runloop }); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
extern "C" fn on_device_removal( |
| 188 |
context: *mut c_void, |
| 189 |
_: IOReturn, |
| 190 |
_: *mut c_void, |
| 191 |
device_ref: IOHIDDeviceRef, |
| 192 |
) { |
| 193 |
let this = unsafe { &mut *(context as *mut Self) }; |
| 194 |
this.remove_device(device_ref); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
impl<F> Drop for Monitor<F> |
| 199 |
where |
| 200 |
F: Fn( |
| 201 |
(IOHIDDeviceRef, Receiver<Vec<u8>>), |
| 202 |
Sender<DeviceSelectorEvent>, |
| 203 |
Sender<crate::StatusUpdate>, |
| 204 |
&dyn Fn() -> bool, |
| 205 |
) + Send |
| 206 |
+ Sync |
| 207 |
+ 'static, |
| 208 |
{ |
| 209 |
fn drop(&mut self) { |
| 210 |
unsafe { CFRelease(self.manager as *mut c_void) }; |
| 211 |
} |
| 212 |
} |