Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f03477181b | ||
|
|
ad2695aa4e | ||
|
|
a274927c2f | ||
|
|
dba5af397f | ||
|
|
558a19f6ae |
@@ -0,0 +1,84 @@
|
||||
name: MacMonitor CI/CD Pipeline
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- develop
|
||||
- 'milestone/**'
|
||||
- 'feat/**'
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
- develop
|
||||
- 'milestone/**'
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
name: Build & Test (Intel x86_64)
|
||||
runs-on: [macos, intel]
|
||||
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Inspect Toolchain & Environment
|
||||
run: |
|
||||
echo "=== Host Architecture ==="
|
||||
uname -m
|
||||
echo "=== macOS Version ==="
|
||||
sw_vers
|
||||
echo "=== Active Xcode Version ==="
|
||||
xcodebuild -version
|
||||
echo "=== Available macOS SDKs ==="
|
||||
xcrun --show-sdk-path
|
||||
|
||||
- name: Generate Xcode Project
|
||||
run: |
|
||||
if command -v xcodegen &> /dev/null; then
|
||||
xcodegen generate
|
||||
else
|
||||
echo "xcodegen not preinstalled, using tracked MacMonitor.xcodeproj"
|
||||
fi
|
||||
|
||||
- name: Run SwiftLint Linting
|
||||
run: |
|
||||
if command -v swiftlint &> /dev/null; then
|
||||
swiftlint lint --reporter emoji
|
||||
else
|
||||
echo "swiftlint not found, skipping lint step"
|
||||
fi
|
||||
|
||||
- name: Build MacMonitor Scheme
|
||||
run: |
|
||||
set -o pipefail
|
||||
if command -v xcbeautify &> /dev/null; then
|
||||
xcodebuild clean build \
|
||||
-scheme MacMonitor \
|
||||
-destination 'generic/platform=macOS,arch=x86_64' \
|
||||
CODE_SIGNING_ALLOWED=NO | xcbeautify
|
||||
else
|
||||
xcodebuild clean build \
|
||||
-scheme MacMonitor \
|
||||
-destination 'generic/platform=macOS,arch=x86_64' \
|
||||
CODE_SIGNING_ALLOWED=NO
|
||||
fi
|
||||
|
||||
- name: Run Unit Tests
|
||||
run: |
|
||||
set -o pipefail
|
||||
if command -v xcbeautify &> /dev/null; then
|
||||
xcodebuild test \
|
||||
-scheme MacMonitor \
|
||||
-destination 'platform=macOS,arch=x86_64' \
|
||||
CODE_SIGNING_ALLOWED=NO | xcbeautify
|
||||
else
|
||||
xcodebuild test \
|
||||
-scheme MacMonitor \
|
||||
-destination 'platform=macOS,arch=x86_64' \
|
||||
CODE_SIGNING_ALLOWED=NO
|
||||
fi
|
||||
@@ -8,12 +8,15 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
1879A3DCF6E305D7F2CD472E /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2345555FF9780B02A90CC7A9 /* CoreFoundation.framework */; };
|
||||
1B3F1C8ABF0ADFAA290FF6F6 /* MMSMCParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */; };
|
||||
3C0AC14F803C813287D8A663 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 007F48A5A599AB4E8D216D16 /* IOKit.framework */; };
|
||||
88387CDAE2AC1CC74EF89B57 /* MacMonitorApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADEA6BD38BA8D887B1DB64A9 /* MacMonitorApp.swift */; };
|
||||
9BCE74C5454F81EF80492E75 /* MacMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */; };
|
||||
A7609CE56D49EC3419987468 /* SystemTelemetryStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B4E94FB2886C60D5548A2A /* SystemTelemetryStore.swift */; };
|
||||
A9FCAEC0BF27A419DB148C4A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E142AE61F9B87757997870DD /* ContentView.swift */; };
|
||||
CD91FC663B19DA1FEA3D49D7 /* MMAppleSMCClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 5555436CD3CB8A73F5D6504F /* MMAppleSMCClient.m */; };
|
||||
EAFAF5A10502D4ACC64F4720 /* MMTelemetryCoordinator.m in Sources */ = {isa = PBXBuildFile; fileRef = F9F9AD1268F906761032AF4D /* MMTelemetryCoordinator.m */; };
|
||||
EC9FA3E4A119277B89E73F8D /* MMSMCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 940D608A1AF80F75584E744E /* MMSMCTests.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
@@ -28,12 +31,18 @@
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
007F48A5A599AB4E8D216D16 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; };
|
||||
1AD9EEE571CD904BBCD1C96B /* MMSMCParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMSMCParser.h; sourceTree = "<group>"; };
|
||||
2345555FF9780B02A90CC7A9 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
|
||||
2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacMonitorTests.swift; sourceTree = "<group>"; };
|
||||
32340166F6100A08105308D3 /* MMTelemetryCoordinator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryCoordinator.h; sourceTree = "<group>"; };
|
||||
375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMSMCParser.m; sourceTree = "<group>"; };
|
||||
5555436CD3CB8A73F5D6504F /* MMAppleSMCClient.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMAppleSMCClient.m; sourceTree = "<group>"; };
|
||||
5635101F862DE680856BDE6E /* MMTelemetryDomain.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryDomain.h; sourceTree = "<group>"; };
|
||||
56E285A0A1746AE659981F22 /* MacMonitorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MacMonitorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
6D0E5CAF25DFA9A1D2A698A0 /* MMTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryProvider.h; sourceTree = "<group>"; };
|
||||
726005CB5A6C86E7D80D3CA0 /* MMAppleSMCClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMAppleSMCClient.h; sourceTree = "<group>"; };
|
||||
8FBB436F3D9472194D8C6EDD /* MMSMCDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMSMCDefines.h; sourceTree = "<group>"; };
|
||||
940D608A1AF80F75584E744E /* MMSMCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMSMCTests.swift; sourceTree = "<group>"; };
|
||||
ADEA6BD38BA8D887B1DB64A9 /* MacMonitorApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacMonitorApp.swift; sourceTree = "<group>"; };
|
||||
E142AE61F9B87757997870DD /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||
E4B4E94FB2886C60D5548A2A /* SystemTelemetryStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemTelemetryStore.swift; sourceTree = "<group>"; };
|
||||
@@ -69,6 +78,7 @@
|
||||
children = (
|
||||
4D8D8B40632C525CBC1D0E43 /* App */,
|
||||
54787E6270EA7B9A1BE11359 /* Core */,
|
||||
9844EC526E3527F6A582179F /* Hardware */,
|
||||
3E4A0B53C583BD0B7A68EACE /* UI */,
|
||||
);
|
||||
path = Sources;
|
||||
@@ -102,6 +112,14 @@
|
||||
path = Core;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
9844EC526E3527F6A582179F /* Hardware */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
E012FBB26EF9E35A2FAF87E2 /* AppleSMC */,
|
||||
);
|
||||
path = Hardware;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
C80C3EEE8F912F4B214102E3 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -115,10 +133,23 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */,
|
||||
940D608A1AF80F75584E744E /* MMSMCTests.swift */,
|
||||
);
|
||||
path = Tests;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E012FBB26EF9E35A2FAF87E2 /* AppleSMC */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
726005CB5A6C86E7D80D3CA0 /* MMAppleSMCClient.h */,
|
||||
5555436CD3CB8A73F5D6504F /* MMAppleSMCClient.m */,
|
||||
8FBB436F3D9472194D8C6EDD /* MMSMCDefines.h */,
|
||||
1AD9EEE571CD904BBCD1C96B /* MMSMCParser.h */,
|
||||
375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */,
|
||||
);
|
||||
path = AppleSMC;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E56B76815ED75B28A13D6814 /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -203,6 +234,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
EC9FA3E4A119277B89E73F8D /* MMSMCTests.swift in Sources */,
|
||||
9BCE74C5454F81EF80492E75 /* MacMonitorTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -212,6 +244,8 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
A9FCAEC0BF27A419DB148C4A /* ContentView.swift in Sources */,
|
||||
CD91FC663B19DA1FEA3D49D7 /* MMAppleSMCClient.m in Sources */,
|
||||
1B3F1C8ABF0ADFAA290FF6F6 /* MMSMCParser.m in Sources */,
|
||||
EAFAF5A10502D4ACC64F4720 /* MMTelemetryCoordinator.m in Sources */,
|
||||
88387CDAE2AC1CC74EF89B57 /* MacMonitorApp.swift in Sources */,
|
||||
A7609CE56D49EC3419987468 /* SystemTelemetryStore.swift in Sources */,
|
||||
|
||||
@@ -6,4 +6,9 @@
|
||||
#import "MMTelemetryProvider.h"
|
||||
#import "MMTelemetryCoordinator.h"
|
||||
|
||||
// Hardware Abstraction - AppleSMC
|
||||
#import "MMSMCDefines.h"
|
||||
#import "MMSMCParser.h"
|
||||
#import "MMAppleSMCClient.h"
|
||||
|
||||
#endif /* MacMonitor_Bridging_Header_h */
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#ifndef MMAppleSMCClient_h
|
||||
#define MMAppleSMCClient_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MMSMCDefines.h"
|
||||
#import "MMSMCParser.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* MMAppleSMCClient
|
||||
* Thread-safe singleton providing direct communication with the AppleSMC driver
|
||||
* on Intel Mac platforms via IOKit user client calls.
|
||||
*/
|
||||
@interface MMAppleSMCClient : NSObject
|
||||
|
||||
@property (class, readonly, strong) MMAppleSMCClient *sharedClient;
|
||||
|
||||
/**
|
||||
* Whether the AppleSMC IOKit driver connection is open and active.
|
||||
*/
|
||||
@property (nonatomic, readonly, getter=isAvailable) BOOL available;
|
||||
|
||||
/**
|
||||
* Total number of SMC keys advertised by hardware (#KEY), or 0 if unavailable.
|
||||
*/
|
||||
@property (nonatomic, readonly) NSInteger totalKeyCount;
|
||||
|
||||
/**
|
||||
* Explicitly opens the IOKit AppleSMC connection.
|
||||
*/
|
||||
- (BOOL)openWithError:(NSError **)error;
|
||||
|
||||
/**
|
||||
* Closes the active IOKit AppleSMC connection.
|
||||
*/
|
||||
- (void)close;
|
||||
|
||||
/**
|
||||
* Reads raw bytes for a given 4-character SMC key (e.g. "TC0P", "F0Ac").
|
||||
*/
|
||||
- (nullable NSData *)readBytesForKey:(NSString *)key error:(NSError **)error;
|
||||
|
||||
/**
|
||||
* Reads and automatically decodes a numeric value according to its SMC type descriptor.
|
||||
*/
|
||||
- (nullable NSNumber *)readNumericValueForKey:(NSString *)key error:(NSError **)error;
|
||||
|
||||
/**
|
||||
* Returns the FourCC data type string for an SMC key (e.g. "sp78", "fpe2", "ui16").
|
||||
*/
|
||||
- (nullable NSString *)readDataTypeForKey:(NSString *)key error:(NSError **)error;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMAppleSMCClient_h */
|
||||
@@ -0,0 +1,245 @@
|
||||
#import "MMAppleSMCClient.h"
|
||||
#import <IOKit/IOKitLib.h>
|
||||
#import <os/lock.h>
|
||||
|
||||
@interface MMAppleSMCClient () {
|
||||
os_unfair_lock _lock;
|
||||
io_connect_t _connection;
|
||||
NSMutableDictionary<NSString *, NSValue *> *_keyInfoCache;
|
||||
BOOL _opened;
|
||||
NSInteger _totalKeyCount;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MMAppleSMCClient
|
||||
|
||||
+ (instancetype)sharedClient {
|
||||
static MMAppleSMCClient *instance = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[self alloc] init];
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_lock = OS_UNFAIR_LOCK_INIT;
|
||||
_keyInfoCache = [NSMutableDictionary dictionary];
|
||||
[self openWithError:nil];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self close];
|
||||
}
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
os_unfair_lock_lock(&_lock);
|
||||
BOOL avail = (_connection != IO_OBJECT_NULL && _opened);
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return avail;
|
||||
}
|
||||
|
||||
- (NSInteger)totalKeyCount {
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NSInteger count = _totalKeyCount;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return count;
|
||||
}
|
||||
|
||||
- (BOOL)openWithError:(NSError **)error {
|
||||
os_unfair_lock_lock(&_lock);
|
||||
if (_opened && _connection != IO_OBJECT_NULL) {
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return YES;
|
||||
}
|
||||
|
||||
CFMutableDictionaryRef matching = IOServiceMatching("AppleSMCClient");
|
||||
if (!matching) {
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:-1
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Failed to create AppleSMCClient matching dictionary"}];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault, matching);
|
||||
if (service == IO_OBJECT_NULL) {
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:-2
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"AppleSMCClient service not found (running on non-Mac or unsupported VM)"}];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
kern_return_t kr = IOServiceOpen(service, mach_task_self(), 0, &_connection);
|
||||
IOObjectRelease(service);
|
||||
|
||||
if (kr != KERN_SUCCESS || _connection == IO_OBJECT_NULL) {
|
||||
_connection = IO_OBJECT_NULL;
|
||||
_opened = NO;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:kr
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"IOServiceOpen failed with code: 0x%x", kr]}];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
_opened = YES;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
|
||||
// Probe total keys count
|
||||
NSNumber *keysCount = [self readNumericValueForKey:@"#KEY" error:nil];
|
||||
if (keysCount) {
|
||||
os_unfair_lock_lock(&_lock);
|
||||
_totalKeyCount = [keysCount integerValue];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
os_unfair_lock_lock(&_lock);
|
||||
if (_connection != IO_OBJECT_NULL) {
|
||||
IOServiceClose(_connection);
|
||||
_connection = IO_OBJECT_NULL;
|
||||
}
|
||||
_opened = NO;
|
||||
[_keyInfoCache removeAllObjects];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (BOOL)callSMCWithInput:(const SMCKeyData_t *)input output:(SMCKeyData_t *)output {
|
||||
if (_connection == IO_OBJECT_NULL) return NO;
|
||||
|
||||
size_t inputSize = sizeof(SMCKeyData_t);
|
||||
size_t outputSize = sizeof(SMCKeyData_t);
|
||||
|
||||
kern_return_t kr = IOConnectCallStructMethod(
|
||||
_connection,
|
||||
kSMCHandleYPCEvent,
|
||||
input,
|
||||
inputSize,
|
||||
output,
|
||||
&outputSize
|
||||
);
|
||||
|
||||
return (kr == KERN_SUCCESS && output->result == 0);
|
||||
}
|
||||
|
||||
- (BOOL)fetchKeyInfo:(NSString *)key info:(SMCKeyInfoData *)outInfo {
|
||||
if (key.length != 4) return NO;
|
||||
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NSValue *cached = _keyInfoCache[key];
|
||||
if (cached) {
|
||||
[cached getValue:outInfo size:sizeof(SMCKeyInfoData)];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return YES;
|
||||
}
|
||||
|
||||
SMCKeyData_t input = {0};
|
||||
SMCKeyData_t output = {0};
|
||||
|
||||
input.key = MMSMCToFourCharCode([key UTF8String]);
|
||||
input.data8 = kSMCGetKeyInfo;
|
||||
|
||||
BOOL success = [self callSMCWithInput:&input output:&output];
|
||||
if (success) {
|
||||
*outInfo = output.keyInfo;
|
||||
_keyInfoCache[key] = [NSValue value:outInfo withObjCType:@encode(SMCKeyInfoData)];
|
||||
}
|
||||
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return success;
|
||||
}
|
||||
|
||||
- (nullable NSData *)readBytesForKey:(NSString *)key error:(NSError **)error {
|
||||
if (![self isAvailable]) {
|
||||
if (![self openWithError:error]) return nil;
|
||||
}
|
||||
|
||||
SMCKeyInfoData info = {0};
|
||||
if (![self fetchKeyInfo:key info:&info]) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:-3
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to retrieve key info for '%@'", key]}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (info.dataSize > 32) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:-4
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Data size %lu exceeds 32 bytes for key '%@'", (unsigned long)info.dataSize, key]}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
os_unfair_lock_lock(&_lock);
|
||||
SMCKeyData_t input = {0};
|
||||
SMCKeyData_t output = {0};
|
||||
|
||||
input.key = MMSMCToFourCharCode([key UTF8String]);
|
||||
input.keyInfo.dataSize = info.dataSize;
|
||||
input.data8 = kSMCReadKey;
|
||||
|
||||
BOOL success = [self callSMCWithInput:&input output:&output];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
|
||||
if (!success) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:-5
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"SMC read command failed for key '%@'", key]}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [NSData dataWithBytes:output.bytes length:info.dataSize];
|
||||
}
|
||||
|
||||
- (nullable NSString *)readDataTypeForKey:(NSString *)key error:(NSError **)error {
|
||||
SMCKeyInfoData info = {0};
|
||||
if (![self fetchKeyInfo:key info:&info]) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.SMC"
|
||||
code:-3
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to retrieve key info for '%@'", key]}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
char typeStr[5] = {0};
|
||||
MMSMCFromFourCharCode(info.dataType, typeStr);
|
||||
return [NSString stringWithUTF8String:typeStr];
|
||||
}
|
||||
|
||||
- (nullable NSNumber *)readNumericValueForKey:(NSString *)key error:(NSError **)error {
|
||||
SMCKeyInfoData info = {0};
|
||||
if (![self fetchKeyInfo:key info:&info]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSData *data = [self readBytesForKey:key error:error];
|
||||
if (!data) return nil;
|
||||
|
||||
char typeStr[5] = {0};
|
||||
MMSMCFromFourCharCode(info.dataType, typeStr);
|
||||
NSString *dataType = [NSString stringWithUTF8String:typeStr];
|
||||
|
||||
return [MMSMCParser decodeValueWithDataType:dataType bytes:data.bytes size:data.length];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef MMSMCDefines_h
|
||||
#define MMSMCDefines_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <IOKit/IOKitLib.h>
|
||||
|
||||
#define KERNEL_INDEX_SMC 2
|
||||
|
||||
// SMC Call Selectors
|
||||
#define kSMCUserClientOpen 0
|
||||
#define kSMCUserClientClose 1
|
||||
#define kSMCHandleYPCEvent 2
|
||||
#define kSMCReadKey 5
|
||||
#define kSMCWriteKey 6
|
||||
#define kSMCGetKeyFromIndex 8
|
||||
#define kSMCGetKeyInfo 9
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
typedef struct {
|
||||
unsigned char major;
|
||||
unsigned char minor;
|
||||
unsigned char build;
|
||||
unsigned char reserved[1];
|
||||
unsigned short release;
|
||||
} SMCVersion;
|
||||
|
||||
typedef struct {
|
||||
uint16_t version;
|
||||
uint16_t length;
|
||||
uint32_t cpuPLimit;
|
||||
uint32_t gpuPLimit;
|
||||
uint32_t memPLimit;
|
||||
} SMCPLimitData;
|
||||
|
||||
typedef struct {
|
||||
IOByteCount dataSize;
|
||||
UInt32 dataType;
|
||||
UInt8 dataAttributes;
|
||||
} SMCKeyInfoData;
|
||||
|
||||
typedef struct {
|
||||
UInt32 key;
|
||||
SMCVersion vers;
|
||||
SMCPLimitData pLimitData;
|
||||
SMCKeyInfoData keyInfo;
|
||||
UInt8 result;
|
||||
UInt8 status;
|
||||
UInt8 data8;
|
||||
UInt32 data32;
|
||||
UInt8 bytes[32];
|
||||
} SMCKeyData_t;
|
||||
|
||||
typedef struct {
|
||||
char key[5];
|
||||
UInt32 dataSize;
|
||||
char dataType[5];
|
||||
UInt8 bytes[32];
|
||||
} SMCVal_t;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
static inline UInt32 MMSMCToFourCharCode(const char * _Nonnull str) {
|
||||
UInt32 code = 0;
|
||||
for (int i = 0; i < 4 && str[i] != '\0'; i++) {
|
||||
code = (code << 8) | (UInt8)str[i];
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
static inline void MMSMCFromFourCharCode(UInt32 code, char * _Nonnull outStr) {
|
||||
outStr[0] = (char)((code >> 24) & 0xFF);
|
||||
outStr[1] = (char)((code >> 16) & 0xFF);
|
||||
outStr[2] = (char)((code >> 8) & 0xFF);
|
||||
outStr[3] = (char)(code & 0xFF);
|
||||
outStr[4] = '\0';
|
||||
}
|
||||
|
||||
#endif /* MMSMCDefines_h */
|
||||
@@ -0,0 +1,50 @@
|
||||
#ifndef MMSMCParser_h
|
||||
#define MMSMCParser_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MMSMCParser : NSObject
|
||||
|
||||
/**
|
||||
* Decodes a signed 8.8 fixed-point format (sp78), used widely for Intel Mac temperature sensors.
|
||||
*/
|
||||
+ (float)decodeSP78:(const uint8_t *)bytes;
|
||||
|
||||
/**
|
||||
* Decodes an unsigned 14.2 fixed-point format (fpe2), used for Fan RPM values.
|
||||
*/
|
||||
+ (float)decodeFPE2:(const uint8_t *)bytes;
|
||||
|
||||
/**
|
||||
* Decodes a standard IEEE 754 32-bit floating point value (flt).
|
||||
*/
|
||||
+ (float)decodeFLT:(const uint8_t *)bytes;
|
||||
|
||||
/**
|
||||
* Decodes an unsigned 8-bit integer (ui8).
|
||||
*/
|
||||
+ (uint8_t)decodeUI8:(const uint8_t *)bytes;
|
||||
|
||||
/**
|
||||
* Decodes a big-endian unsigned 16-bit integer (ui16).
|
||||
*/
|
||||
+ (uint16_t)decodeUI16:(const uint8_t *)bytes;
|
||||
|
||||
/**
|
||||
* Decodes a big-endian unsigned 32-bit integer (ui32).
|
||||
*/
|
||||
+ (uint32_t)decodeUI32:(const uint8_t *)bytes;
|
||||
|
||||
/**
|
||||
* Generic parser taking an SMC dataType FourCC string ("sp78", "fpe2", "flt", "ui8 ", "ui16", "ui32", "flag")
|
||||
* and returning a parsed NSNumber.
|
||||
*/
|
||||
+ (nullable NSNumber *)decodeValueWithDataType:(NSString *)dataType bytes:(const uint8_t *)bytes size:(NSUInteger)size;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMSMCParser_h */
|
||||
@@ -0,0 +1,67 @@
|
||||
#import "MMSMCParser.h"
|
||||
#import <string.h>
|
||||
|
||||
@implementation MMSMCParser
|
||||
|
||||
+ (float)decodeSP78:(const uint8_t *)bytes {
|
||||
if (!bytes) return 0.0f;
|
||||
int16_t raw = (int16_t)(((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1]);
|
||||
return (float)raw / 256.0f;
|
||||
}
|
||||
|
||||
+ (float)decodeFPE2:(const uint8_t *)bytes {
|
||||
if (!bytes) return 0.0f;
|
||||
uint16_t raw = ((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1];
|
||||
return (float)raw / 4.0f;
|
||||
}
|
||||
|
||||
+ (float)decodeFLT:(const uint8_t *)bytes {
|
||||
if (!bytes) return 0.0f;
|
||||
float val = 0.0f;
|
||||
memcpy(&val, bytes, sizeof(float));
|
||||
return val;
|
||||
}
|
||||
|
||||
+ (uint8_t)decodeUI8:(const uint8_t *)bytes {
|
||||
if (!bytes) return 0;
|
||||
return bytes[0];
|
||||
}
|
||||
|
||||
+ (uint16_t)decodeUI16:(const uint8_t *)bytes {
|
||||
if (!bytes) return 0;
|
||||
return ((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1];
|
||||
}
|
||||
|
||||
+ (uint32_t)decodeUI32:(const uint8_t *)bytes {
|
||||
if (!bytes) return 0;
|
||||
return ((uint32_t)bytes[0] << 24) |
|
||||
((uint32_t)bytes[1] << 16) |
|
||||
((uint32_t)bytes[2] << 8) |
|
||||
(uint32_t)bytes[3];
|
||||
}
|
||||
|
||||
+ (nullable NSNumber *)decodeValueWithDataType:(NSString *)dataType bytes:(const uint8_t *)bytes size:(NSUInteger)size {
|
||||
if (!bytes || size == 0) return nil;
|
||||
|
||||
NSString *trimmedType = [dataType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
||||
|
||||
if ([trimmedType isEqualToString:@"sp78"]) {
|
||||
if (size >= 2) return @([self decodeSP78:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"fpe2"]) {
|
||||
if (size >= 2) return @([self decodeFPE2:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"flt"]) {
|
||||
if (size >= 4) return @([self decodeFLT:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"ui8"]) {
|
||||
if (size >= 1) return @([self decodeUI8:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"ui16"]) {
|
||||
if (size >= 2) return @([self decodeUI16:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"ui32"]) {
|
||||
if (size >= 4) return @([self decodeUI32:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"flag"]) {
|
||||
if (size >= 1) return @(bytes[0] != 0);
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,73 @@
|
||||
import XCTest
|
||||
@testable import MacMonitor
|
||||
|
||||
final class MMSMCTests: XCTestCase {
|
||||
|
||||
func testSP78TemperatureDecoding() {
|
||||
// 0x36, 0x80 -> 54.5°C in sp78 (signed 8.8 fixed point: 0x36 = 54, 0x80 = 128 / 256 = 0.5)
|
||||
let bytes: [UInt8] = [0x36, 0x80]
|
||||
let temp = MMSMCParser.decodeSP78(bytes)
|
||||
XCTAssertEqual(Double(temp), 54.5, accuracy: 0.001)
|
||||
|
||||
// Negative temperature: -10.25°C -> -10.25 * 256 = -2624 = 0xF5C0
|
||||
let negBytes: [UInt8] = [0xF5, 0xC0]
|
||||
let negTemp = MMSMCParser.decodeSP78(negBytes)
|
||||
XCTAssertEqual(Double(negTemp), -10.25, accuracy: 0.001)
|
||||
}
|
||||
|
||||
func testFPE2FanRPMDecoding() {
|
||||
// 1800 RPM in fpe2 (unsigned 14.2 fixed point: 1800 * 4 = 7200 = 0x1C20)
|
||||
let bytes: [UInt8] = [0x1C, 0x20]
|
||||
let rpm = MMSMCParser.decodeFPE2(bytes)
|
||||
XCTAssertEqual(Double(rpm), 1800.0, accuracy: 0.001)
|
||||
|
||||
// 2450.5 RPM: 2450.5 * 4 = 9802 = 0x264A
|
||||
let bytesFrac: [UInt8] = [0x26, 0x4A]
|
||||
let rpmFrac = MMSMCParser.decodeFPE2(bytesFrac)
|
||||
XCTAssertEqual(Double(rpmFrac), 2450.5, accuracy: 0.001)
|
||||
}
|
||||
|
||||
func testFLTDecoding() {
|
||||
// 3.14159f in IEEE 754 float
|
||||
var original: Float = 3.14159
|
||||
let bytes = withUnsafeBytes(of: &original) { Array($0) }
|
||||
let decoded = MMSMCParser.decodeFLT(bytes)
|
||||
XCTAssertEqual(Double(decoded), 3.14159, accuracy: 0.0001)
|
||||
}
|
||||
|
||||
func testIntegerDecoding() {
|
||||
// UI8
|
||||
let byteUI8: [UInt8] = [0x7F]
|
||||
XCTAssertEqual(MMSMCParser.decodeUI8(byteUI8), 127)
|
||||
|
||||
// UI16 big endian (0x1234 = 4660)
|
||||
let bytesUI16: [UInt8] = [0x12, 0x34]
|
||||
XCTAssertEqual(MMSMCParser.decodeUI16(bytesUI16), 4660)
|
||||
|
||||
// UI32 big endian (0x12345678 = 305419896)
|
||||
let bytesUI32: [UInt8] = [0x12, 0x34, 0x56, 0x78]
|
||||
XCTAssertEqual(MMSMCParser.decodeUI32(bytesUI32), 305419896)
|
||||
}
|
||||
|
||||
func testGenericDecoderWithType() {
|
||||
let tempBytes: [UInt8] = [0x41, 0x00] // 65.0°C in sp78
|
||||
let val = MMSMCParser.decodeValue(withDataType: "sp78", bytes: tempBytes, size: 2)
|
||||
XCTAssertNotNil(val)
|
||||
XCTAssertEqual(val?.doubleValue ?? 0, 65.0, accuracy: 0.001)
|
||||
|
||||
let flagBytes: [UInt8] = [0x01]
|
||||
let flagVal = MMSMCParser.decodeValue(withDataType: "flag", bytes: flagBytes, size: 1)
|
||||
XCTAssertNotNil(flagVal)
|
||||
XCTAssertEqual(flagVal?.boolValue, true)
|
||||
}
|
||||
|
||||
func testSMCClientSingletonAndKeyCount() {
|
||||
let client = MMAppleSMCClient.shared
|
||||
XCTAssertNotNil(client)
|
||||
// On physical Intel Macs, client.isAvailable should be true and totalKeyCount > 0.
|
||||
// In headless environments or VMs without AppleSMC, it gracefully degrades.
|
||||
if client.isAvailable {
|
||||
XCTAssertGreaterThan(client.totalKeyCount, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user