| 1 |
use std::{ |
| 2 |
ffi::{c_char, c_void}, |
| 3 |
io::ErrorKind, |
| 4 |
mem::ManuallyDrop, |
| 5 |
sync::Mutex, |
| 6 |
task::{Context, Poll, Waker}, |
| 7 |
}; |
| 8 |
|
| 9 |
use atomic_waker::AtomicWaker; |
| 10 |
use core_foundation::{base::TCFType, dictionary::CFDictionary, runloop::CFRunLoopSource}; |
| 11 |
use io_kit_sys::{ |
| 12 |
kIOMasterPortDefault, |
| 13 |
keys::{kIOFirstMatchNotification, kIOTerminatedNotification}, |
| 14 |
ret::kIOReturnSuccess, |
| 15 |
types::io_iterator_t, |
| 16 |
usb::lib::kIOUSBDeviceClassName, |
| 17 |
IONotificationPort, IONotificationPortCreate, IONotificationPortDestroy, |
| 18 |
IONotificationPortGetRunLoopSource, IOServiceAddMatchingNotification, IOServiceMatching, |
| 19 |
}; |
| 20 |
use log::debug; |
| 21 |
use slab::Slab; |
| 22 |
|
| 23 |
use crate::{hotplug::HotplugEvent, DeviceId, Error}; |
| 24 |
|
| 25 |
use super::{ |
| 26 |
enumeration::{get_registry_id, probe_device}, |
| 27 |
events::{add_event_source, EventRegistration}, |
| 28 |
iokit::IoServiceIterator, |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
static WAKERS: Mutex<Slab<AtomicWaker>> = Mutex::new(Slab::new()); |
| 33 |
|
| 34 |
|
| 35 |
struct SlabWaker(usize); |
| 36 |
|
| 37 |
impl SlabWaker { |
| 38 |
fn new() -> SlabWaker { |
| 39 |
SlabWaker(WAKERS.lock().unwrap().insert(AtomicWaker::new())) |
| 40 |
} |
| 41 |
|
| 42 |
fn register(&self, w: &Waker) { |
| 43 |
WAKERS.lock().unwrap()[self.0].register(w); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl Drop for SlabWaker { |
| 48 |
fn drop(&mut self) { |
| 49 |
WAKERS.lock().unwrap().remove(self.0); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
pub(crate) struct MacHotplugWatch { |
| 54 |
waker_id: SlabWaker, |
| 55 |
terminated_iter: IoServiceIterator, |
| 56 |
matched_iter: IoServiceIterator, |
| 57 |
_registration: EventRegistration, |
| 58 |
_notification_port: NotificationPort, |
| 59 |
} |
| 60 |
|
| 61 |
struct NotificationPort(*mut IONotificationPort); |
| 62 |
|
| 63 |
impl NotificationPort { |
| 64 |
fn new() -> NotificationPort { |
| 65 |
unsafe { NotificationPort(IONotificationPortCreate(kIOMasterPortDefault)) } |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl Drop for NotificationPort { |
| 70 |
fn drop(&mut self) { |
| 71 |
unsafe { IONotificationPortDestroy(self.0) } |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
unsafe impl Send for NotificationPort {} |
| 76 |
|
| 77 |
impl MacHotplugWatch { |
| 78 |
pub(crate) fn new() -> Result<Self, Error> { |
| 79 |
let waker_id = SlabWaker::new(); |
| 80 |
|
| 81 |
let dictionary = unsafe { |
| 82 |
let d = IOServiceMatching(kIOUSBDeviceClassName); |
| 83 |
if d.is_null() { |
| 84 |
return Err(Error::new(ErrorKind::Other, "IOServiceMatching failed")); |
| 85 |
} |
| 86 |
CFDictionary::wrap_under_create_rule(d) |
| 87 |
}; |
| 88 |
|
| 89 |
let notification_port = NotificationPort::new(); |
| 90 |
let terminated_iter = register_notification( |
| 91 |
¬ification_port, |
| 92 |
&dictionary, |
| 93 |
&waker_id, |
| 94 |
kIOTerminatedNotification, |
| 95 |
)?; |
| 96 |
let matched_iter = register_notification( |
| 97 |
¬ification_port, |
| 98 |
&dictionary, |
| 99 |
&waker_id, |
| 100 |
kIOFirstMatchNotification, |
| 101 |
)?; |
| 102 |
|
| 103 |
let source = unsafe { |
| 104 |
CFRunLoopSource::wrap_under_get_rule(IONotificationPortGetRunLoopSource( |
| 105 |
notification_port.0, |
| 106 |
)) |
| 107 |
}; |
| 108 |
let registration = add_event_source(source); |
| 109 |
|
| 110 |
Ok(MacHotplugWatch { |
| 111 |
waker_id, |
| 112 |
terminated_iter, |
| 113 |
matched_iter, |
| 114 |
_registration: registration, |
| 115 |
_notification_port: notification_port, |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
pub fn poll_next(&mut self, cx: &mut Context) -> Poll<HotplugEvent> { |
| 120 |
self.waker_id.register(cx.waker()); |
| 121 |
|
| 122 |
while let Some(s) = self.matched_iter.next() { |
| 123 |
if let Some(dev) = probe_device(s) { |
| 124 |
return Poll::Ready(HotplugEvent::Connected(dev)); |
| 125 |
} else { |
| 126 |
debug!("failed to probe connected device"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if let Some(s) = self.terminated_iter.next() { |
| 131 |
if let Some(registry_id) = get_registry_id(&s) { |
| 132 |
debug!("device {registry_id} disconnected"); |
| 133 |
let id = DeviceId(registry_id); |
| 134 |
return Poll::Ready(HotplugEvent::Disconnected(id)); |
| 135 |
} else { |
| 136 |
debug!("failed to get registry ID for disconnected device") |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
Poll::Pending |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
unsafe impl Sync for MacHotplugWatch {} |
| 147 |
|
| 148 |
fn register_notification( |
| 149 |
port: &NotificationPort, |
| 150 |
dictionary: &CFDictionary, |
| 151 |
waker: &SlabWaker, |
| 152 |
event: *const i8, |
| 153 |
) -> Result<IoServiceIterator, Error> { |
| 154 |
assert!(event == kIOFirstMatchNotification || event == kIOTerminatedNotification); |
| 155 |
unsafe { |
| 156 |
let mut iter = 0; |
| 157 |
let r = IOServiceAddMatchingNotification( |
| 158 |
port.0, |
| 159 |
event as *mut c_char, |
| 160 |
ManuallyDrop::new(dictionary.clone()).as_concrete_TypeRef(), |
| 161 |
callback, |
| 162 |
waker.0 as *mut c_void, |
| 163 |
&mut iter, |
| 164 |
); |
| 165 |
|
| 166 |
if r != kIOReturnSuccess { |
| 167 |
return Err(Error::new( |
| 168 |
ErrorKind::Other, |
| 169 |
"Failed to register notification", |
| 170 |
)); |
| 171 |
} |
| 172 |
let mut iter = IoServiceIterator::new(iter); |
| 173 |
|
| 174 |
|
| 175 |
while let Some(_) = iter.next() {} |
| 176 |
|
| 177 |
Ok(iter) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
unsafe extern "C" fn callback(refcon: *mut c_void, _iterator: io_iterator_t) { |
| 182 |
debug!("hotplug event callback"); |
| 183 |
let id = refcon as usize; |
| 184 |
if let Some(waker) = WAKERS.lock().unwrap().get(id) { |
| 185 |
waker.wake() |
| 186 |
} |
| 187 |
} |