summaryrefslogtreecommitdiffstats
path: root/src/osx/smc.cpp
diff options
context:
space:
mode:
authorJos Dehaes <jos.dehaes@gmail.com>2021-12-21 23:15:02 +0100
committerJos Dehaes <jos.dehaes@gmail.com>2021-12-21 23:15:02 +0100
commit96ac1149d15743d148c51714b496e389a71fe8f4 (patch)
tree6430aad3e2c05d6328e1cf1e75159e037d5fec2c /src/osx/smc.cpp
parentd4f5112d23c8d7b21032f6d6b840f6f1c2a96301 (diff)
fix CPU temp fallback on macOS
Diffstat (limited to 'src/osx/smc.cpp')
-rw-r--r--src/osx/smc.cpp33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/osx/smc.cpp b/src/osx/smc.cpp
index 7386dbf..54fa422 100644
--- a/src/osx/smc.cpp
+++ b/src/osx/smc.cpp
@@ -50,32 +50,39 @@ namespace Cpu {
IOServiceClose(conn);
}
+ long long SMCConnection::getSMCTemp(char *key) {
+ SMCVal_t val;
+ kern_return_t result;
+ result = SMCReadKey(key, &val);
+ if (result == kIOReturnSuccess) {
+ if (val.dataSize > 0) {
+ if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
+ // convert sp78 value to temperature
+ int intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1];
+ return static_cast<long long>(intValue / 256.0);
+ }
+ }
+ }
+ return -1;
+ }
+
// core means physical core in SMC, while in core map it's cpu threads :-/ Only an issue on hackintosh?
// this means we can only get the T per physical core
// another issue with the SMC API is that the key is always 4 chars -> what with systems with more than 9 physical cores?
// no Mac models with more than 18 threads are released, so no problem so far
// according to VirtualSMC docs (hackintosh fake SMC) the enumeration follows with alphabetic chars - not implemented yet here (nor in VirtualSMC)
long long SMCConnection::getTemp(int core) {
- SMCVal_t val;
- kern_return_t result;
char key[] = SMC_KEY_CPU_TEMP;
if (core >= 0) {
snprintf(key, 5, "TC%1dc", core);
}
- result = SMCReadKey(key, &val);
- if (result != kIOReturnSuccess) {
+ long long result = getSMCTemp(key);
+ if (result == -1) {
// try again with C
snprintf(key, 5, "TC%1dC", core);
- result = SMCReadKey(key, &val);
+ result = getSMCTemp(key);
}
- if (result == kIOReturnSuccess) {
- if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
- // convert sp78 value to temperature
- int intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1];
- return static_cast<long long>(intValue / 256.0);
- }
- }
- return -1;
+ return result;
}
kern_return_t SMCConnection::SMCReadKey(UInt32Char_t key, SMCVal_t *val) {