-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathHardware.swift
More file actions
83 lines (71 loc) · 2.51 KB
/
Hardware.swift
File metadata and controls
83 lines (71 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// Hardware.swift
// Mist
//
// Created by Nindi Gill on 31/5/2022.
//
import Foundation
/// Hardware Struct used to retrieve Hardware information.
enum Hardware {
/// Hardware Architecture (Apple Silicon or Intel).
static var architecture: Architecture? {
#if arch(arm64)
return .appleSilicon
#elseif arch(x86_64)
return .intel
#else
return nil
#endif
}
/// Hardware Board ID (Intel).
static var boardID: String? {
architecture == .intel ? registryProperty(for: "board-id") : nil
}
/// Hardware Device ID (Apple Silicon or Intel T2).
static var deviceID: String? {
switch architecture {
case .appleSilicon:
registryProperty(for: "compatible")?.components(separatedBy: "\0").first?.uppercased()
case .intel:
registryProperty(for: "bridge-model")?.uppercased()
default:
nil
}
}
/// Hardware Model Identifier (Apple Silicon or Intel).
static var modelIdentifier: String? {
registryProperty(for: "model")
}
/// Retrieves the IOKit Registry **IOPlatformExpertDevice** entity property for the provided key.
///
/// - Parameters:
/// - key: The key for the entity property.
///
/// - Returns: The entity property for the provided key.
private static func registryProperty(for key: String) -> String? {
#if os(Linux)
// Cannot use Darwin/XNU kernel APIs on Linux
return nil;
#else
let entry: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
defer {
IOObjectRelease(entry)
}
var properties: Unmanaged<CFMutableDictionary>?
guard
IORegistryEntryCreateCFProperties(entry, &properties, kCFAllocatorDefault, 0) == KERN_SUCCESS,
let properties: Unmanaged<CFMutableDictionary> = properties else {
return nil
}
let nsDictionary: NSDictionary = properties.takeRetainedValue() as NSDictionary
guard
let dictionary: [String: Any] = nsDictionary as? [String: Any],
dictionary.keys.contains(key),
let data: Data = IORegistryEntryCreateCFProperty(entry, key as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? Data else {
return nil
}
let string: String = .init(decoding: data, as: UTF8.self)
return string.trimmingCharacters(in: CharacterSet(["\0"]))
#endif
}
}