| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::mem; |
| 5 |
|
| 6 |
use anyhow::{anyhow, bail}; |
| 7 |
use core_foundation::{ |
| 8 |
base::{CFType, TCFType, ToVoid, kCFAllocatorDefault}, |
| 9 |
dictionary::{ |
| 10 |
CFDictionary, CFDictionaryGetTypeID, CFDictionaryRef, CFMutableDictionary, |
| 11 |
CFMutableDictionaryRef, |
| 12 |
}, |
| 13 |
number::{CFNumber, CFNumberGetTypeID}, |
| 14 |
string::{CFString, CFStringGetTypeID}, |
| 15 |
}; |
| 16 |
use mach2::kern_return; |
| 17 |
|
| 18 |
use super::bindings::*; |
| 19 |
|
| 20 |
|
| 21 |
#[derive(Debug)] |
| 22 |
pub struct IoObject(io_object_t); |
| 23 |
|
| 24 |
impl IoObject { |
| 25 |
|
| 26 |
pub fn properties(&self) -> anyhow::Result<CFDictionary<CFString, CFType>> { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
unsafe { |
| 31 |
let mut props = mem::MaybeUninit::<CFMutableDictionaryRef>::uninit(); |
| 32 |
|
| 33 |
let result = IORegistryEntryCreateCFProperties( |
| 34 |
self.0, |
| 35 |
props.as_mut_ptr(), |
| 36 |
kCFAllocatorDefault, |
| 37 |
0, |
| 38 |
); |
| 39 |
|
| 40 |
if result != kern_return::KERN_SUCCESS { |
| 41 |
bail!("IORegistryEntryCreateCFProperties failed, error code {result}.") |
| 42 |
} else { |
| 43 |
let props = props.assume_init(); |
| 44 |
Ok(CFMutableDictionary::wrap_under_create_rule(props).to_immutable()) |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
pub fn service_parent(&self) -> anyhow::Result<IoObject> { |
| 52 |
let mut parent: io_registry_entry_t = 0; |
| 53 |
|
| 54 |
|
| 55 |
let result = unsafe { |
| 56 |
IORegistryEntryGetParentEntry(self.0, kIOServicePlane.as_ptr().cast(), &mut parent) |
| 57 |
}; |
| 58 |
|
| 59 |
if result != kern_return::KERN_SUCCESS { |
| 60 |
bail!("IORegistryEntryGetParentEntry failed, error code {result}.") |
| 61 |
} else { |
| 62 |
Ok(parent.into()) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
impl From<io_object_t> for IoObject { |
| 77 |
fn from(obj: io_object_t) -> IoObject { |
| 78 |
IoObject(obj) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl Drop for IoObject { |
| 83 |
fn drop(&mut self) { |
| 84 |
|
| 85 |
|
| 86 |
let result = unsafe { IOObjectRelease(self.0) }; |
| 87 |
assert_eq!(result, kern_return::KERN_SUCCESS); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub fn get_dict( |
| 92 |
dict: &CFDictionary<CFString, CFType>, raw_key: &'static str, |
| 93 |
) -> anyhow::Result<CFDictionary<CFString, CFType>> { |
| 94 |
let key = CFString::from_static_string(raw_key); |
| 95 |
|
| 96 |
dict.find(&key) |
| 97 |
.map(|value_ref| { |
| 98 |
|
| 99 |
unsafe { |
| 100 |
debug_assert!(value_ref.type_of() == CFDictionaryGetTypeID()); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
let ptr = value_ref.to_void() as CFDictionaryRef; |
| 105 |
|
| 106 |
|
| 107 |
unsafe { CFDictionary::wrap_under_get_rule(ptr) } |
| 108 |
}) |
| 109 |
.ok_or_else(|| anyhow!("missing key")) |
| 110 |
} |
| 111 |
|
| 112 |
pub fn get_i64( |
| 113 |
dict: &CFDictionary<CFString, CFType>, raw_key: &'static str, |
| 114 |
) -> anyhow::Result<i64> { |
| 115 |
let key = CFString::from_static_string(raw_key); |
| 116 |
|
| 117 |
dict.find(&key) |
| 118 |
.and_then(|value_ref| { |
| 119 |
|
| 120 |
unsafe { |
| 121 |
debug_assert!(value_ref.type_of() == CFNumberGetTypeID()); |
| 122 |
} |
| 123 |
value_ref.downcast::<CFNumber>() |
| 124 |
}) |
| 125 |
.and_then(|number| number.to_i64()) |
| 126 |
.ok_or_else(|| anyhow!("missing key")) |
| 127 |
} |
| 128 |
|
| 129 |
pub fn get_string( |
| 130 |
dict: &CFDictionary<CFString, CFType>, raw_key: &'static str, |
| 131 |
) -> anyhow::Result<String> { |
| 132 |
let key = CFString::from_static_string(raw_key); |
| 133 |
|
| 134 |
dict.find(&key) |
| 135 |
.and_then(|value_ref| { |
| 136 |
|
| 137 |
unsafe { |
| 138 |
debug_assert!(value_ref.type_of() == CFStringGetTypeID()); |
| 139 |
} |
| 140 |
|
| 141 |
value_ref.downcast::<CFString>() |
| 142 |
}) |
| 143 |
.map(|cf_string| cf_string.to_string()) |
| 144 |
.ok_or_else(|| anyhow!("missing key")) |
| 145 |
} |