#[cfg(target_os = "macos")]
use super::iokit::{Cf, Io};
use crate::device::{ANCHOR_BUF, Anchor, Reading};
#[cfg(target_os = "macos")]
use crate::device::{Raw, canon};
#[cfg(target_os = "macos")]
fn platform_string(
key: &str,
out: &mut [u8; ANCHOR_BUF],
canonical: fn(&[u8], &mut [u8; ANCHOR_BUF]) -> Reading,
) -> Reading {
let Some(platform) = Io::service(c"IOPlatformExpertDevice") else {
return Reading::Absent;
};
let Ok(key) = Cf::string("device identity", key) else {
return Reading::Absent;
};
let Some(value) = platform.property(&key) else {
return Reading::Absent;
};
let mut text = Raw::new();
match value.get().text(&mut text.0) {
Some(bytes) => canonical(bytes, out),
None => Reading::Absent,
}
}
#[cfg(target_os = "macos")]
pub(crate) fn device_anchor(anchor: Anchor, out: &mut [u8; ANCHOR_BUF]) -> Reading {
match anchor {
Anchor::PlatformUuid => platform_string("IOPlatformUUID", out, canon::uuid_text),
Anchor::BoardSerial => platform_string("IOPlatformSerialNumber", out, canon::serial),
_ => Reading::Unsupported,
}
}
#[cfg(not(target_os = "macos"))]
pub(crate) fn device_anchor(_anchor: Anchor, _out: &mut [u8; ANCHOR_BUF]) -> Reading {
Reading::Unsupported
}
#[cfg(all(test, target_os = "macos"))]
mod tests {
use super::*;
#[test]
fn test_device_apple_anchors() {
let mut out = [0u8; ANCHOR_BUF];
assert_eq!(
device_anchor(Anchor::InstallId, &mut out),
Reading::Unsupported
);
assert_eq!(
device_anchor(Anchor::ProductSerial, &mut out),
Reading::Unsupported
);
assert_eq!(
device_anchor(Anchor::PlatformUuid, &mut out),
Reading::Value(16)
);
assert!(matches!(
device_anchor(Anchor::BoardSerial, &mut [0u8; ANCHOR_BUF]),
Reading::Value(_) | Reading::Placeholder | Reading::Absent
));
}
}