29 lines
1.2 KiB
Swift
29 lines
1.2 KiB
Swift
import Foundation
|
|
import IOKit
|
|
import Darwin
|
|
|
|
/// Cached local model metadata, without serial numbers or network/device names.
|
|
nonisolated enum HardwareIdentity {
|
|
static let displayName: String = {
|
|
let entry = IORegistryEntryFromPath(kIOMainPortDefault, "IODeviceTree:/product")
|
|
var product: String?
|
|
if entry != 0 {
|
|
defer { IOObjectRelease(entry) }
|
|
if let data = IORegistryEntryCreateCFProperty(entry, "product-name" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? Data {
|
|
product = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .controlCharacters)
|
|
}
|
|
}
|
|
let model = string("hw.model")
|
|
let chip = string("machdep.cpu.brand_string")
|
|
return [product?.isEmpty == false ? product : model, chip].compactMap { $0 }.joined(separator: " · ")
|
|
}()
|
|
|
|
private static func string(_ key: String) -> String? {
|
|
var count = 0
|
|
guard sysctlbyname(key, nil, &count, nil, 0) == 0, count > 0 else { return nil }
|
|
var bytes = [CChar](repeating: 0, count: count)
|
|
guard sysctlbyname(key, &bytes, &count, nil, 0) == 0 else { return nil }
|
|
return String(cString: bytes)
|
|
}
|
|
}
|