Debian Codegraph @

src/platform/macos_iokit/iokit.rs

raw symbols SCIP
1 //! Utilities for using IOKit APIs.
2 //!
3 //! Based on Kate Temkin's [usrs](https://github.com/ktemkin/usrs)
4 //! licensed under MIT OR Apache-2.0.
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 // Safety: `handle` must be an IOObject handle. Ownership is transferred.
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 // Safety: `handle` must be an IOService handle. Ownership is transferred.
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 // Safety: `handle` must be an IoIterator of IoService. Ownership is transferred.
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 /// Helper for calling IOKit function pointers.
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 /// Wrapper around a **IOCFPluginInterface that automatically releases it.
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 /// Fetches the inner pointer for passing to IOKit functions.
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 /// Alias that select the "version 500" (IOKit 5.0.0) version of UsbDevice, which means
106 /// that we support macOS versions back to 10.7.3, which is currently every version that Rust
107 /// supports. Use this instead of touching the iokit_c structure; this may be bumped to
108 /// (compatible) newer versions of the struct as Rust's support changes.
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 }