| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use core_foundation_sys::uuid::CFUUIDBytes; |
| 7 |
use io_kit_sys::{ret::IOReturn, IOIteratorNext, IOObjectRelease}; |
| 8 |
use std::io::ErrorKind; |
| 9 |
|
| 10 |
use crate::Error; |
| 11 |
|
| 12 |
use super::iokit_c::{self, CFUUIDGetUUIDBytes, IOCFPlugInInterface}; |
| 13 |
|
| 14 |
pub(crate) struct IoObject(u32); |
| 15 |
|
| 16 |
impl IoObject { |
| 17 |
|
| 18 |
pub unsafe fn new(handle: u32) -> IoObject { |
| 19 |
IoObject(handle) |
| 20 |
} |
| 21 |
pub fn get(&self) -> u32 { |
| 22 |
self.0 |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl Drop for IoObject { |
| 27 |
fn drop(&mut self) { |
| 28 |
unsafe { |
| 29 |
IOObjectRelease(self.0); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub(crate) struct IoService(IoObject); |
| 35 |
|
| 36 |
impl IoService { |
| 37 |
|
| 38 |
pub unsafe fn new(handle: u32) -> IoService { |
| 39 |
IoService(IoObject(handle)) |
| 40 |
} |
| 41 |
pub fn get(&self) -> u32 { |
| 42 |
self.0 .0 |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
pub(crate) struct IoServiceIterator(IoObject); |
| 47 |
|
| 48 |
impl IoServiceIterator { |
| 49 |
|
| 50 |
pub unsafe fn new(handle: u32) -> IoServiceIterator { |
| 51 |
IoServiceIterator(IoObject::new(handle)) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl Iterator for IoServiceIterator { |
| 56 |
type Item = IoService; |
| 57 |
|
| 58 |
fn next(&mut self) -> Option<Self::Item> { |
| 59 |
unsafe { |
| 60 |
let handle = IOIteratorNext(self.0.get()); |
| 61 |
if handle != 0 { |
| 62 |
Some(IoService::new(handle)) |
| 63 |
} else { |
| 64 |
None |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
macro_rules! call_iokit_function { |
| 72 |
($ptr:expr, $function:ident($($args:expr),*)) => {{ |
| 73 |
use std::ffi::c_void; |
| 74 |
let func = (**$ptr).$function.expect("function pointer from IOKit was null"); |
| 75 |
func($ptr as *mut c_void, $($args),*) |
| 76 |
}}; |
| 77 |
} |
| 78 |
pub(crate) use call_iokit_function; |
| 79 |
|
| 80 |
|
| 81 |
#[derive(Debug)] |
| 82 |
pub(crate) struct PluginInterface { |
| 83 |
interface: *mut *mut IOCFPlugInInterface, |
| 84 |
} |
| 85 |
|
| 86 |
impl PluginInterface { |
| 87 |
pub(crate) fn new(interface: *mut *mut IOCFPlugInInterface) -> Self { |
| 88 |
Self { interface } |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
pub(crate) fn get(&self) -> *mut *mut IOCFPlugInInterface { |
| 93 |
self.interface |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl Drop for PluginInterface { |
| 98 |
fn drop(&mut self) { |
| 99 |
unsafe { |
| 100 |
call_iokit_function!(self.interface, Release()); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
pub(crate) type UsbDevice = iokit_c::IOUSBDeviceStruct500; |
| 110 |
pub(crate) type UsbInterface = iokit_c::IOUSBInterfaceStruct500; |
| 111 |
|
| 112 |
pub(crate) fn usb_device_type_id() -> CFUUIDBytes { |
| 113 |
unsafe { CFUUIDGetUUIDBytes(iokit_c::kIOUSBDeviceInterfaceID500()) } |
| 114 |
} |
| 115 |
|
| 116 |
pub(crate) fn usb_interface_type_id() -> CFUUIDBytes { |
| 117 |
unsafe { CFUUIDGetUUIDBytes(iokit_c::kIOUSBInterfaceInterfaceID500()) } |
| 118 |
} |
| 119 |
|
| 120 |
pub(crate) fn check_iokit_return(r: IOReturn) -> Result<(), Error> { |
| 121 |
#[allow(non_upper_case_globals)] |
| 122 |
#[deny(unreachable_patterns)] |
| 123 |
match r { |
| 124 |
io_kit_sys::ret::kIOReturnSuccess => Ok(()), |
| 125 |
io_kit_sys::ret::kIOReturnExclusiveAccess => Err(Error::new( |
| 126 |
ErrorKind::Other, |
| 127 |
"could not be opened for exclusive access", |
| 128 |
)), |
| 129 |
io_kit_sys::ret::kIOReturnNotFound => Err(Error::new(ErrorKind::NotFound, "not found")), |
| 130 |
_ => Err(Error::from_raw_os_error(r)), |
| 131 |
} |
| 132 |
} |