Skip to content

Fix: can not get all core temp with more than 10 cores #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@

#include "smc.h"

#if (MAC_OS_X_VERSION_MAX_ALLOWED < 120000) // Before macOS 12 Monterey
#define kIOMainPortDefault kIOMasterPortDefault
#endif

static io_connect_t conn;

UInt8 coreNums[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

UInt32 strtoulWithSize(const char *str, int size, int base) {
UInt32 total = 0;
int i;
Expand Down Expand Up @@ -56,7 +62,7 @@ kern_return_t SMCOpen(void) {
io_object_t device;

CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
result = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iterator);
result = IOServiceGetMatchingServices(kIOMainPortDefault, matchingDictionary, &iterator);
if (result != kIOReturnSuccess) {
printf("Error: IOServiceGetMatchingServices() = %08x\n", result);
return 1;
Expand Down Expand Up @@ -156,29 +162,29 @@ double SMCGetTemperature(char *key) {
}

int getTemperatureSMCKeySize(unsigned long core) {
return snprintf(NULL, 0, "%s%lu%c", SMC_CPU_CORE_TEMP_PREFIX, core, SMC_CPU_CORE_TEMP_SUFFIX_NEW);
return snprintf(NULL, 0, "%s%c%c", SMC_CPU_CORE_TEMP_PREFIX, coreNums[core], SMC_CPU_CORE_TEMP_SUFFIX_NEW);
}

void getOldSMCTemperatureKeyTemplate(char *key) {
sprintf(key, "%s%%lu%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_OLD);
sprintf(key, "%s%%c%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_OLD);
}

void getNewSMCTemperatureKeyTemplate(char *key) {
sprintf(key, "%s%%lu%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_NEW);
sprintf(key, "%s%%c%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_NEW);
}

double getTemperatureKeyTemplate(unsigned long core, char *templateKey) {
getNewSMCTemperatureKeyTemplate(templateKey);

char key[getTemperatureSMCKeySize(core)];
sprintf(key, templateKey, core);
sprintf(key, templateKey, coreNums[core]);

double temperature = SMCGetTemperature(key);

if (temperature == 0) {
// We must use the old key
getOldSMCTemperatureKeyTemplate(templateKey);
sprintf(key, templateKey, core);
sprintf(key, templateKey, coreNums[core]);
temperature = SMCGetTemperature(key);
}

Expand Down Expand Up @@ -322,7 +328,7 @@ int main(int argc, char *argv[]) {

for (int i = 1; i < coreCount; ++i) {
char key[getTemperatureSMCKeySize(coreList[i])];
sprintf(key, templateKey, coreList[i]);
sprintf(key, templateKey, coreNums[coreList[i]]);
double temperature = SMCGetTemperature(key);

if (temperature == 0) {
Expand Down