From a64d3ce918a3badb38a11d7df5a4e6aa0487f9e8 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 19 Aug 2020 22:08:45 +0200 Subject: [PATCH 01/20] make it more flexible and keep default behaviour --- README.md | 16 +++ smc.c | 283 +++++++++++++++++++++++++++++++++++++++++++++++------- smc.h | 6 ++ 3 files changed, 268 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index bef51b0..95643dd 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,25 @@ clib install lavoiesl/osx-cpu-temp ### Options + * `-h` Print command line options. * `-C` Output temperature in Celsius (default). * `-F` Output temperature in Fahrenheit. + * `-c [key]` Print CPU temperature, optionally providing the SMC key. + * `-g [key]` Print GPU temperature, optionally providing the SMC key. * `-f` Output fan speed. + * `-t key` Print temperature value of given SMC key. + * `-r key` Print raw value of given SMC key. + * `-a` Print ALL available SMC keys (without description!). + +### Explore keys +* see https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCKeys.txt +* see https://app.assembla.com/wiki/show/fakesmc + +Print the raw value of any key, if it exists +```shell script +osx-cpu-temp -r '#KEY' # number of keys +osx-cpu-temp -r TC0P # CPU proximity sensor +``` ## Maintainer diff --git a/smc.c b/smc.c index 80926b4..6bb1220 100644 --- a/smc.c +++ b/smc.c @@ -32,9 +32,9 @@ UInt32 _strtoul(char* str, int size, int base) for (i = 0; i < size; i++) { if (base == 16) - total += str[i] << (size - 1 - i) * 8; + total += (str[i] << (size - 1 - i) * 8); else - total += (unsigned char)(str[i] << (size - 1 - i) * 8); + total += ((unsigned char)(str[i]) << (size - 1 - i) * 8); } return total; } @@ -138,6 +138,114 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) return kIOReturnSuccess; } +UInt32 SMCReadIndexCount(void) +{ + SMCVal_t val; + + SMCReadKey("#KEY", &val); + return _strtoul((char *)val.bytes, val.dataSize, 10); +} + +double SMCNormalizeFloat(SMCVal_t val) +{ + if (strcmp(val.dataType, DATATYPE_SP78) == 0) { + return ((SInt16)ntohs(*(UInt16*)val.bytes)) / 256.0; + } + if (strcmp(val.dataType, DATATYPE_SP5A) == 0) { + return ((SInt16)ntohs(*(UInt16*)val.bytes)) / 1024.0; + } + if (strcmp(val.dataType, DATATYPE_FPE2) == 0) { + return ntohs(*(UInt16*)val.bytes) / 4.0; + } + if (strcmp(val.dataType, DATATYPE_FP88) == 0) { + return ntohs(*(UInt16*)val.bytes) / 256.0; + } + return -1.f; +} + +int SMCNormalizeInt(SMCVal_t val) +{ + if (strcmp(val.dataType, DATATYPE_UINT8) == 0 || strcmp(val.dataType, DATATYPE_UINT16) == 0 || strcmp(val.dataType, DATATYPE_UINT32) == 0) { + return (int) _strtoul((char *)val.bytes, val.dataSize, 10); + } + if (strcmp(val.dataType, DATATYPE_SI16) == 0) { + return ntohs(*(SInt16*)val.bytes); + } + + if (strcmp(val.dataType, DATATYPE_HEX) == 0 || strcmp(val.dataType, DATATYPE_FLAG) == 0) { + printf("Hex value = 0x"); + int i; + for (i = 0; i < val.dataSize; i++) + printf("%02x ", (unsigned char) val.bytes[i]); + printf("\n"); + return (int) _strtoul((char *)val.bytes, val.dataSize, 10); + } + return -1; +} + +char* SMCNormalizeText(SMCVal_t val) +{ + char result[val.dataSize + 2]; + if (strcmp(val.dataType, DATATYPE_CH8) == 0) { + int i; + for (i = 0; i < val.dataSize; i++) { + result[i] = (unsigned char)val.bytes[i]; + } + result[i+1] = 0; + return strdup(result); + } + + // convert anything else to text + double f = SMCNormalizeFloat(val); + if (f != -1.0) { + snprintf(result, 10, "%0.1f", f); + return strdup(result); + } + int i = SMCNormalizeInt(val); + if (i != -1) { + snprintf(result, 10, "%d", i); + return strdup(result); + } + + return strdup(result); +} + +kern_return_t SMCPrintAll(void) +{ + kern_return_t result; + SMCKeyData_t inputStructure; + SMCKeyData_t outputStructure; + + int totalKeys, i; + UInt32Char_t key; + SMCVal_t val; + + totalKeys = SMCReadIndexCount(); + printf("Total keys = %d\n", totalKeys); + + for (i = 0; i < totalKeys; i++) + { + memset(&inputStructure, 0, sizeof(SMCKeyData_t)); + memset(&outputStructure, 0, sizeof(SMCKeyData_t)); + memset(&val, 0, sizeof(SMCVal_t)); + + inputStructure.data8 = SMC_CMD_READ_INDEX; + inputStructure.data32 = i; + + result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure); + if (result != kIOReturnSuccess) + continue; + + _ultostr(key, outputStructure.key); + + SMCReadKey(key, &val); + char* txt = SMCNormalizeText(val); + printf("key = %s type = %s value = %s\n", key, val.dataType, txt); + } + + return kIOReturnSuccess; +} + double SMCGetTemperature(char* key) { SMCVal_t val; @@ -147,15 +255,11 @@ double SMCGetTemperature(char* key) if (result == kIOReturnSuccess) { // read succeeded - check returned value 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 intValue / 256.0; - } + return SMCNormalizeFloat(val); } } // read failed - return 0.0; + return -1.0; } double SMCGetFanSpeed(char* key) @@ -167,15 +271,43 @@ double SMCGetFanSpeed(char* key) if (result == kIOReturnSuccess) { // read succeeded - check returned value if (val.dataSize > 0) { - if (strcmp(val.dataType, DATATYPE_FPE2) == 0) { - // convert fpe2 value to rpm - int intValue = (unsigned char)val.bytes[0] * 256 + (unsigned char)val.bytes[1]; - return intValue / 4.0; - } + return SMCNormalizeFloat(val); + } + } + // read failed + return -1.0; +} + +float SMCGetFanRPM(char* key) +{ + SMCVal_t val; + kern_return_t result; + + result = SMCReadKey(key, &val); + if (result == kIOReturnSuccess) { + // read succeeded - check returned value + if (val.dataSize > 0) { + return SMCNormalizeFloat(val); } } // read failed - return 0.0; + return -1.f; +} + +double SMCGetPower(char* key) +{ + SMCVal_t val; + kern_return_t result; + + result = SMCReadKey(key, &val); + if (result == kIOReturnSuccess) { + // read succeeded - check returned value + if (val.dataSize > 0) { + return SMCNormalizeFloat(val); + } + } + // read failed + return -1.f; } double convertToFahrenheit(double celsius) @@ -184,9 +316,9 @@ double convertToFahrenheit(double celsius) } // Requires SMCOpen() -void readAndPrintCpuTemp(int show_title, char scale) +void readAndPrintCpuTemp(char* key, int show_title, char scale) { - double temperature = SMCGetTemperature(SMC_KEY_CPU_TEMP); + double temperature = SMCGetTemperature(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -199,9 +331,9 @@ void readAndPrintCpuTemp(int show_title, char scale) } // Requires SMCOpen() -void readAndPrintGpuTemp(int show_title, char scale) +void readAndPrintGpuTemp(char* key, int show_title, char scale) { - double temperature = SMCGetTemperature(SMC_KEY_GPU_TEMP); + double temperature = SMCGetTemperature(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -213,23 +345,46 @@ void readAndPrintGpuTemp(int show_title, char scale) printf("%s%0.1f °%c\n", title, temperature, scale); } -float SMCGetFanRPM(char* key) +// Requires SMCOpen() +void readAndPrintTemp(char* key, char scale) +{ + double temperature = SMCGetTemperature(key); + if (scale == 'F') { + temperature = convertToFahrenheit(temperature); + } + printf("%s = %0.1f °%c\n", key, temperature, scale); +} + +// Requires SMCOpen() +void readAndPrintRawValue(char* key) { SMCVal_t val; kern_return_t result; + float f; + int i; + char* c; result = SMCReadKey(key, &val); if (result == kIOReturnSuccess) { // read succeeded - check returned value if (val.dataSize > 0) { - if (strcmp(val.dataType, DATATYPE_FPE2) == 0) { - // convert fpe2 value to RPM - return ntohs(*(UInt16*)val.bytes) / 4.0; + f = SMCNormalizeFloat(val); + if (f != -1.0) { + printf("%s = %0.1f\n", key, f); + return; + } + i = SMCNormalizeInt(val); + if (i != -1) { + printf("%s = %d\n", key, i); + return; + } + c = SMCNormalizeText(val); + if (strcmp(c, "") != 0) { + printf("%s = %s\n", key, c); + return; } } } - // read failed - return -1.f; } // Requires SMCOpen() @@ -243,7 +398,7 @@ void readAndPrintFanRPMs(void) result = SMCReadKey("FNum", &val); if (result == kIOReturnSuccess) { - totalFans = _strtoul((char*)val.bytes, val.dataSize, 10); + totalFans = SMCNormalizeInt(val); printf("Num fans: %d\n", totalFans); for (i = 0; i < totalFans; i++) { @@ -296,45 +451,90 @@ void readAndPrintFanRPMs(void) } } +void readAndPrintAll(void) +{ + kern_return_t result; + result = SMCPrintAll(); + if (result != kIOReturnSuccess) { + printf("Error reading keys: %d", result); + } +} + int main(int argc, char* argv[]) { char scale = 'C'; int cpu = 0; int fan = 0; int gpu = 0; + int tmp = 0; + int raw = 0; + int all = 0; + char* key; int c; - while ((c = getopt(argc, argv, "CFcfgh?")) != -1) { + while ((c = getopt(argc, argv, ":CFc:g:ft:r:ah?")) != -1) { switch (c) { case 'F': case 'C': scale = c; break; case 'c': + // optionally allow to pass the SMC Key (**TC0P**, TC0D, TCXC, ...) cpu = 1; + key = optarg; + break; + case 'g': + // optionally allow to pass the SMC Key (**TG0P**, TG0D, TCGC, ...) + gpu = 1; + key = optarg; break; case 'f': fan = 1; break; - case 'g': - gpu = 1; + case 't': + tmp = 1; + key = optarg; + break; + case 'r': + raw = 1; + key = optarg; + break; + // all option, see: https://github.com/hholtmann/smcFanControl/blob/875c68b0d36fbda40d2bf745fc43dcb40523360b/smc-command/smc.c#L485 + case 'a': + all = 1; + break; + case ':': + // optional arguments, set defaults + switch (optopt) { + case 'c': + key = SMC_KEY_CPU_TEMP; + break; + case 'g': + key = SMC_KEY_GPU_TEMP; + break; + default: + printf("Error: '-%c' requires an argument", optopt); + return -1; + } break; case 'h': case '?': printf("usage: osx-cpu-temp \n"); printf("Options:\n"); - printf(" -F Display temperatures in degrees Fahrenheit.\n"); - printf(" -C Display temperatures in degrees Celsius (Default).\n"); - printf(" -c Display CPU temperature (Default).\n"); - printf(" -g Display GPU temperature.\n"); - printf(" -f Display fan speeds.\n"); - printf(" -h Display this help.\n"); + printf(" -F Display temperatures in degrees Fahrenheit.\n"); + printf(" -C Display temperatures in degrees Celsius (Default).\n"); + printf(" -c Display CPU temperature (Default).\n"); + printf(" -g Display GPU temperature.\n"); + printf(" -f Display fan speeds.\n"); + printf(" -t key Display temperature of given SMC key"); + printf(" -r key Display raw value of given SMC key"); + printf(" -h Display this help.\n"); printf("\nIf more than one of -c, -f, or -g are specified, titles will be added\n"); return -1; } } - if (!fan && !gpu) { + if (!fan && !gpu && !tmp && !raw & !all) { cpu = 1; } @@ -343,14 +543,23 @@ int main(int argc, char* argv[]) SMCOpen(); if (cpu) { - readAndPrintCpuTemp(show_title, scale); + readAndPrintCpuTemp(key, show_title, scale); } if (gpu) { - readAndPrintGpuTemp(show_title, scale); + readAndPrintGpuTemp(key, show_title, scale); } if (fan) { readAndPrintFanRPMs(); } + if (tmp) { + readAndPrintTemp(key, scale); + } + if (raw) { + readAndPrintRawValue(key); + } + if (all) { + readAndPrintAll(); + } SMCClose(); return 0; diff --git a/smc.h b/smc.h index 6bad78f..301514b 100644 --- a/smc.h +++ b/smc.h @@ -33,10 +33,16 @@ #define SMC_CMD_READ_VERS 12 #define DATATYPE_FPE2 "fpe2" +#define DATATYPE_FP88 "fp88" #define DATATYPE_UINT8 "ui8 " #define DATATYPE_UINT16 "ui16" #define DATATYPE_UINT32 "ui32" #define DATATYPE_SP78 "sp78" +#define DATATYPE_SP5A "sp5a" +#define DATATYPE_SI16 "si16" +#define DATATYPE_HEX "hex_" +#define DATATYPE_FLAG "flag" +#define DATATYPE_CH8 "ch8*" // key values #define SMC_KEY_CPU_TEMP "TC0P" From efa658e07aef192cf27374a3d43398d0da514012 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 19 Aug 2020 22:12:49 +0200 Subject: [PATCH 02/20] update usage text --- smc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/smc.c b/smc.c index 6bb1220..54df632 100644 --- a/smc.c +++ b/smc.c @@ -521,14 +521,14 @@ int main(int argc, char* argv[]) case '?': printf("usage: osx-cpu-temp \n"); printf("Options:\n"); - printf(" -F Display temperatures in degrees Fahrenheit.\n"); - printf(" -C Display temperatures in degrees Celsius (Default).\n"); - printf(" -c Display CPU temperature (Default).\n"); - printf(" -g Display GPU temperature.\n"); - printf(" -f Display fan speeds.\n"); - printf(" -t key Display temperature of given SMC key"); - printf(" -r key Display raw value of given SMC key"); - printf(" -h Display this help.\n"); + printf(" -F Display temperatures in degrees Fahrenheit.\n"); + printf(" -C Display temperatures in degrees Celsius (Default).\n"); + printf(" -c [key] Display CPU temperature [of given key] (Default).\n"); + printf(" -g [key] Display GPU temperature [of given key].\n"); + printf(" -f Display fan speeds.\n"); + printf(" -t key Display temperature of given SMC key"); + printf(" -r key Display raw value of given SMC key"); + printf(" -h Display this help.\n"); printf("\nIf more than one of -c, -f, or -g are specified, titles will be added\n"); return -1; } From b12663a148779c60fc69e15e5768bf159055b577 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 19 Aug 2020 22:19:43 +0200 Subject: [PATCH 03/20] lint-fix --- smc.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/smc.c b/smc.c index 54df632..48a6f65 100644 --- a/smc.c +++ b/smc.c @@ -143,7 +143,7 @@ UInt32 SMCReadIndexCount(void) SMCVal_t val; SMCReadKey("#KEY", &val); - return _strtoul((char *)val.bytes, val.dataSize, 10); + return _strtoul((char*)val.bytes, val.dataSize, 10); } double SMCNormalizeFloat(SMCVal_t val) @@ -166,7 +166,7 @@ double SMCNormalizeFloat(SMCVal_t val) int SMCNormalizeInt(SMCVal_t val) { if (strcmp(val.dataType, DATATYPE_UINT8) == 0 || strcmp(val.dataType, DATATYPE_UINT16) == 0 || strcmp(val.dataType, DATATYPE_UINT32) == 0) { - return (int) _strtoul((char *)val.bytes, val.dataSize, 10); + return (int)_strtoul((char*)val.bytes, val.dataSize, 10); } if (strcmp(val.dataType, DATATYPE_SI16) == 0) { return ntohs(*(SInt16*)val.bytes); @@ -176,9 +176,9 @@ int SMCNormalizeInt(SMCVal_t val) printf("Hex value = 0x"); int i; for (i = 0; i < val.dataSize; i++) - printf("%02x ", (unsigned char) val.bytes[i]); + printf("%02x ", (unsigned char)val.bytes[i]); printf("\n"); - return (int) _strtoul((char *)val.bytes, val.dataSize, 10); + return (int)_strtoul((char*)val.bytes, val.dataSize, 10); } return -1; } @@ -191,7 +191,7 @@ char* SMCNormalizeText(SMCVal_t val) for (i = 0; i < val.dataSize; i++) { result[i] = (unsigned char)val.bytes[i]; } - result[i+1] = 0; + result[i + 1] = 0; return strdup(result); } @@ -213,18 +213,17 @@ char* SMCNormalizeText(SMCVal_t val) kern_return_t SMCPrintAll(void) { kern_return_t result; - SMCKeyData_t inputStructure; - SMCKeyData_t outputStructure; + SMCKeyData_t inputStructure; + SMCKeyData_t outputStructure; - int totalKeys, i; - UInt32Char_t key; - SMCVal_t val; + int totalKeys, i; + UInt32Char_t key; + SMCVal_t val; totalKeys = SMCReadIndexCount(); printf("Total keys = %d\n", totalKeys); - for (i = 0; i < totalKeys; i++) - { + for (i = 0; i < totalKeys; i++) { memset(&inputStructure, 0, sizeof(SMCKeyData_t)); memset(&outputStructure, 0, sizeof(SMCKeyData_t)); memset(&val, 0, sizeof(SMCVal_t)); @@ -238,9 +237,9 @@ kern_return_t SMCPrintAll(void) _ultostr(key, outputStructure.key); - SMCReadKey(key, &val); - char* txt = SMCNormalizeText(val); - printf("key = %s type = %s value = %s\n", key, val.dataType, txt); + SMCReadKey(key, &val); + char* txt = SMCNormalizeText(val); + printf("key = %s type = %s value = %s\n", key, val.dataType, txt); } return kIOReturnSuccess; From 91180f15f8750c28d9436a353285e09ce3ea1ecd Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Thu, 20 Aug 2020 23:18:57 +0200 Subject: [PATCH 04/20] remove duplicate code unify float/double --- smc.c | 74 ++++++++++++----------------------------------------------- 1 file changed, 15 insertions(+), 59 deletions(-) diff --git a/smc.c b/smc.c index 48a6f65..7f9f24f 100644 --- a/smc.c +++ b/smc.c @@ -140,9 +140,13 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) UInt32 SMCReadIndexCount(void) { + kern_return_t result; SMCVal_t val; - SMCReadKey("#KEY", &val); + result = SMCReadKey("#KEY", &val); + if (result != kIOReturnSuccess) { + return 0; + } return _strtoul((char*)val.bytes, val.dataSize, 10); } @@ -245,23 +249,7 @@ kern_return_t SMCPrintAll(void) return kIOReturnSuccess; } -double SMCGetTemperature(char* key) -{ - SMCVal_t val; - kern_return_t result; - - result = SMCReadKey(key, &val); - if (result == kIOReturnSuccess) { - // read succeeded - check returned value - if (val.dataSize > 0) { - return SMCNormalizeFloat(val); - } - } - // read failed - return -1.0; -} - -double SMCGetFanSpeed(char* key) +double SMCGetDouble(char* key) { SMCVal_t val; kern_return_t result; @@ -277,38 +265,6 @@ double SMCGetFanSpeed(char* key) return -1.0; } -float SMCGetFanRPM(char* key) -{ - SMCVal_t val; - kern_return_t result; - - result = SMCReadKey(key, &val); - if (result == kIOReturnSuccess) { - // read succeeded - check returned value - if (val.dataSize > 0) { - return SMCNormalizeFloat(val); - } - } - // read failed - return -1.f; -} - -double SMCGetPower(char* key) -{ - SMCVal_t val; - kern_return_t result; - - result = SMCReadKey(key, &val); - if (result == kIOReturnSuccess) { - // read succeeded - check returned value - if (val.dataSize > 0) { - return SMCNormalizeFloat(val); - } - } - // read failed - return -1.f; -} - double convertToFahrenheit(double celsius) { return (celsius * (9.0 / 5.0)) + 32.0; @@ -317,7 +273,7 @@ double convertToFahrenheit(double celsius) // Requires SMCOpen() void readAndPrintCpuTemp(char* key, int show_title, char scale) { - double temperature = SMCGetTemperature(key); + double temperature = SMCGetDouble(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -332,7 +288,7 @@ void readAndPrintCpuTemp(char* key, int show_title, char scale) // Requires SMCOpen() void readAndPrintGpuTemp(char* key, int show_title, char scale) { - double temperature = SMCGetTemperature(key); + double temperature = SMCGetDouble(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -347,7 +303,7 @@ void readAndPrintGpuTemp(char* key, int show_title, char scale) // Requires SMCOpen() void readAndPrintTemp(char* key, char scale) { - double temperature = SMCGetTemperature(key); + double temperature = SMCGetDouble(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -359,7 +315,7 @@ void readAndPrintRawValue(char* key) { SMCVal_t val; kern_return_t result; - float f; + double f; int i; char* c; @@ -409,28 +365,28 @@ void readAndPrintFanRPMs(void) char* name = val.bytes + 4; sprintf(key, "F%dAc", i); - float actual_speed = SMCGetFanRPM(key); + double actual_speed = SMCGetDouble(key); if (actual_speed < 0.f) { continue; } sprintf(key, "F%dMn", i); - float minimum_speed = SMCGetFanRPM(key); + double minimum_speed = SMCGetDouble(key); if (minimum_speed < 0.f) { continue; } sprintf(key, "F%dMx", i); - float maximum_speed = SMCGetFanRPM(key); + double maximum_speed = SMCGetDouble(key); if (maximum_speed < 0.f) { continue; } - float rpm = actual_speed - minimum_speed; + double rpm = actual_speed - minimum_speed; if (rpm < 0.f) { rpm = 0.f; } - float pct = rpm / (maximum_speed - minimum_speed); + double pct = rpm / (maximum_speed - minimum_speed); pct *= 100.f; printf("Fan %d - %s at %.0f RPM (%.0f%%)\n", i, name, rpm, pct); From a4aebccac33a632b8840877dda947c2fafbe9f4a Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Thu, 20 Aug 2020 23:37:43 +0200 Subject: [PATCH 05/20] skip key on error --- smc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/smc.c b/smc.c index 7f9f24f..533d8c2 100644 --- a/smc.c +++ b/smc.c @@ -241,7 +241,10 @@ kern_return_t SMCPrintAll(void) _ultostr(key, outputStructure.key); - SMCReadKey(key, &val); + result = SMCReadKey(key, &val); + if (result != kIOReturnSuccess) + continue; + char* txt = SMCNormalizeText(val); printf("key = %s type = %s value = %s\n", key, val.dataType, txt); } From 9ea8553c1e3fd8c8b7ebac219fcb27f95991ebdc Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Sat, 19 Jun 2021 22:10:43 +0200 Subject: [PATCH 06/20] fix: missing newlines --- smc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smc.c b/smc.c index 533d8c2..492bbe2 100644 --- a/smc.c +++ b/smc.c @@ -484,8 +484,8 @@ int main(int argc, char* argv[]) printf(" -c [key] Display CPU temperature [of given key] (Default).\n"); printf(" -g [key] Display GPU temperature [of given key].\n"); printf(" -f Display fan speeds.\n"); - printf(" -t key Display temperature of given SMC key"); - printf(" -r key Display raw value of given SMC key"); + printf(" -t key Display temperature of given SMC key.\n"); + printf(" -r key Display raw value of given SMC key.\n"); printf(" -h Display this help.\n"); printf("\nIf more than one of -c, -f, or -g are specified, titles will be added\n"); return -1; From 41fea2b5a1a3258faa48488f84b1e536dccea041 Mon Sep 17 00:00:00 2001 From: Sebastien Lavoie Date: Tue, 27 Jun 2023 09:10:33 -0400 Subject: [PATCH 07/20] Rename kIOMasterPortDefault to kIOMainPortDefault, as the former is deprecated --- smc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/smc.c b/smc.c index 492bbe2..615e10e 100644 --- a/smc.c +++ b/smc.c @@ -1,6 +1,6 @@ /* - * Apple System Management Control (SMC) Tool - * Copyright (C) 2006 devnull + * Apple System Management Control (SMC) Tool + * Copyright (C) 2006 devnull * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -56,7 +56,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; From ff5cdfa2f446092a0a1ae966e14c866216adbf9a Mon Sep 17 00:00:00 2001 From: Isidoro Ghezzi Date: Thu, 18 Mar 2021 13:36:28 +0100 Subject: [PATCH 08/20] smc.h fix header guard --- smc.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/smc.h b/smc.h index 301514b..29eec85 100644 --- a/smc.h +++ b/smc.h @@ -19,7 +19,6 @@ #ifndef __SMC_H__ #define __SMC_H__ -#endif #define VERSION "0.01" @@ -98,3 +97,5 @@ typedef struct { double SMCGetTemperature(char* key); kern_return_t SMCSetFanRpm(char* key, int rpm); int SMCGetFanRpm(char* key); + +#endif From 71258a178e3290e4a9cd96c5b82fe11a11deb225 Mon Sep 17 00:00:00 2001 From: Isidoro Ghezzi Date: Thu, 18 Mar 2021 13:39:17 +0100 Subject: [PATCH 09/20] smc.h: remove `__` from header guard --- smc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smc.h b/smc.h index 29eec85..d54513e 100644 --- a/smc.h +++ b/smc.h @@ -17,8 +17,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef __SMC_H__ -#define __SMC_H__ +#ifndef SMC_H +#define SMC_H #define VERSION "0.01" From 1aa0cf0c5a4b11b986e6e30783d4685ea4bb90dd Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:36:38 +0200 Subject: [PATCH 10/20] resolve conflicts --- smc.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/smc.c b/smc.c index 615e10e..5f2872d 100644 --- a/smc.c +++ b/smc.c @@ -138,7 +138,12 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) return kIOReturnSuccess; } +<<<<<<< HEAD UInt32 SMCReadIndexCount(void) +======= +// Requires SMCOpen() +double SMCGetTemperature(char* key) +>>>>>>> 180cff1 (Refactor readAndPrintTemperature) { kern_return_t result; SMCVal_t val; @@ -150,6 +155,7 @@ UInt32 SMCReadIndexCount(void) return _strtoul((char*)val.bytes, val.dataSize, 10); } +<<<<<<< HEAD double SMCNormalizeFloat(SMCVal_t val) { if (strcmp(val.dataType, DATATYPE_SP78) == 0) { @@ -253,6 +259,10 @@ kern_return_t SMCPrintAll(void) } double SMCGetDouble(char* key) +======= +// Requires SMCOpen() +double SMCGetFanSpeed(char* key) +>>>>>>> 180cff1 (Refactor readAndPrintTemperature) { SMCVal_t val; kern_return_t result; @@ -273,21 +283,38 @@ double convertToFahrenheit(double celsius) return (celsius * (9.0 / 5.0)) + 32.0; } +<<<<<<< HEAD // Requires SMCOpen() void readAndPrintCpuTemp(char* key, int show_title, char scale) { double temperature = SMCGetDouble(key); +======= +double readTemperature(char* key, char scale) +{ + double temperature = SMCGetTemperature(key); +>>>>>>> 180cff1 (Refactor readAndPrintTemperature) if (scale == 'F') { temperature = convertToFahrenheit(temperature); } + return temperature; +} + +void readAndPrintTemperature(char* title, char* key, char scale) +{ + double temperature = readTemperature(key, scale); + printf("%s%0.1f °%c\n", title, temperature, scale); +} +void readAndPrintCpuTemp(bool show_title, char scale) +{ char* title = ""; if (show_title) { title = "CPU: "; } - printf("%s%0.1f °%c\n", title, temperature, scale); + readAndPrintTemperature(title, SMC_KEY_CPU_TEMP, scale); } +<<<<<<< HEAD // Requires SMCOpen() void readAndPrintGpuTemp(char* key, int show_title, char scale) { @@ -296,11 +323,15 @@ void readAndPrintGpuTemp(char* key, int show_title, char scale) temperature = convertToFahrenheit(temperature); } +======= +void readAndPrintGpuTemp(bool show_title, char scale) +{ +>>>>>>> 180cff1 (Refactor readAndPrintTemperature) char* title = ""; if (show_title) { title = "GPU: "; } - printf("%s%0.1f °%c\n", title, temperature, scale); + readAndPrintTemperature(title, SMC_KEY_GPU_TEMP, scale); } // Requires SMCOpen() @@ -447,7 +478,7 @@ int main(int argc, char* argv[]) key = optarg; break; case 'f': - fan = 1; + fan = true; break; case 't': tmp = 1; @@ -458,7 +489,7 @@ int main(int argc, char* argv[]) key = optarg; break; // all option, see: https://github.com/hholtmann/smcFanControl/blob/875c68b0d36fbda40d2bf745fc43dcb40523360b/smc-command/smc.c#L485 - case 'a': + case 'A': all = 1; break; case ':': @@ -483,9 +514,11 @@ int main(int argc, char* argv[]) printf(" -C Display temperatures in degrees Celsius (Default).\n"); printf(" -c [key] Display CPU temperature [of given key] (Default).\n"); printf(" -g [key] Display GPU temperature [of given key].\n"); + printf(" -a Display ambient temperature.\n"); printf(" -f Display fan speeds.\n"); printf(" -t key Display temperature of given SMC key.\n"); printf(" -r key Display raw value of given SMC key.\n"); + printf(" -A Display ALL SMC keys\n") printf(" -h Display this help.\n"); printf("\nIf more than one of -c, -f, or -g are specified, titles will be added\n"); return -1; @@ -496,7 +529,7 @@ int main(int argc, char* argv[]) cpu = 1; } - int show_title = fan + gpu + cpu > 1; + bool show_title = fan + gpu + cpu > 1; SMCOpen(); From 142ce70db4e78af1407442b148e456dacaae4a10 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:39:24 +0200 Subject: [PATCH 11/20] fix --- smc.c | 56 ++++++++++++++++++-------------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/smc.c b/smc.c index 5f2872d..2f14b35 100644 --- a/smc.c +++ b/smc.c @@ -138,12 +138,8 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) return kIOReturnSuccess; } -<<<<<<< HEAD -UInt32 SMCReadIndexCount(void) -======= // Requires SMCOpen() double SMCGetTemperature(char* key) ->>>>>>> 180cff1 (Refactor readAndPrintTemperature) { kern_return_t result; SMCVal_t val; @@ -155,7 +151,6 @@ double SMCGetTemperature(char* key) return _strtoul((char*)val.bytes, val.dataSize, 10); } -<<<<<<< HEAD double SMCNormalizeFloat(SMCVal_t val) { if (strcmp(val.dataType, DATATYPE_SP78) == 0) { @@ -259,10 +254,6 @@ kern_return_t SMCPrintAll(void) } double SMCGetDouble(char* key) -======= -// Requires SMCOpen() -double SMCGetFanSpeed(char* key) ->>>>>>> 180cff1 (Refactor readAndPrintTemperature) { SMCVal_t val; kern_return_t result; @@ -283,50 +274,34 @@ double convertToFahrenheit(double celsius) return (celsius * (9.0 / 5.0)) + 32.0; } -<<<<<<< HEAD -// Requires SMCOpen() -void readAndPrintCpuTemp(char* key, int show_title, char scale) -{ - double temperature = SMCGetDouble(key); -======= double readTemperature(char* key, char scale) { double temperature = SMCGetTemperature(key); ->>>>>>> 180cff1 (Refactor readAndPrintTemperature) if (scale == 'F') { temperature = convertToFahrenheit(temperature); } return temperature; } -void readAndPrintTemperature(char* title, char* key, char scale) +void readAndPrintTemperature(char* title, bool show_title, char* key, char scale, bool show_scale) { + if (!show_title) { + title = ""; + } + double temperature = readTemperature(key, scale); - printf("%s%0.1f °%c\n", title, temperature, scale); -} -void readAndPrintCpuTemp(bool show_title, char scale) -{ - char* title = ""; - if (show_title) { - title = "CPU: "; + if (show_scale) { + printf("%s%0.1f °%c\n", title, temperature, scale); + } else { + printf("%s%0.1f\n", title, temperature); } +<<<<<<< HEAD readAndPrintTemperature(title, SMC_KEY_CPU_TEMP, scale); } -<<<<<<< HEAD -// Requires SMCOpen() -void readAndPrintGpuTemp(char* key, int show_title, char scale) -{ - double temperature = SMCGetDouble(key); - if (scale == 'F') { - temperature = convertToFahrenheit(temperature); - } - -======= void readAndPrintGpuTemp(bool show_title, char scale) { ->>>>>>> 180cff1 (Refactor readAndPrintTemperature) char* title = ""; if (show_title) { title = "GPU: "; @@ -452,6 +427,7 @@ void readAndPrintAll(void) int main(int argc, char* argv[]) { char scale = 'C'; + bool show_scale = true; int cpu = 0; int fan = 0; int gpu = 0; @@ -461,12 +437,15 @@ int main(int argc, char* argv[]) char* key; int c; - while ((c = getopt(argc, argv, ":CFc:g:ft:r:ah?")) != -1) { + while ((c = getopt(argc, argv, ":CFc:g:ft:r:aAh?")) != -1) { switch (c) { case 'F': case 'C': scale = c; break; + case 'T': + show_scale = false; + break; case 'c': // optionally allow to pass the SMC Key (**TC0P**, TC0D, TCXC, ...) cpu = 1; @@ -512,6 +491,7 @@ int main(int argc, char* argv[]) printf("Options:\n"); printf(" -F Display temperatures in degrees Fahrenheit.\n"); printf(" -C Display temperatures in degrees Celsius (Default).\n"); + printf(" -T Do not display the units for temperatures.\n"); printf(" -c [key] Display CPU temperature [of given key] (Default).\n"); printf(" -g [key] Display GPU temperature [of given key].\n"); printf(" -a Display ambient temperature.\n"); @@ -534,10 +514,10 @@ int main(int argc, char* argv[]) SMCOpen(); if (cpu) { - readAndPrintCpuTemp(key, show_title, scale); + readAndPrintTemperature("CPU: ", show_title, SMC_KEY_CPU_TEMP, scale, show_scale); } if (gpu) { - readAndPrintGpuTemp(key, show_title, scale); + readAndPrintTemperature("GPU: ", show_title, SMC_KEY_GPU_TEMP, scale, show_scale); } if (fan) { readAndPrintFanRPMs(); From dda37ad3636a36a0cdb28b8b1f457d5bc203dad2 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:41:29 +0200 Subject: [PATCH 12/20] resolve conflicts --- README.md | 28 ++++++++++++++-------------- smc.c | 8 +++++++- smc.h | 3 ++- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 95643dd..2ecd303 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Outputs current CPU temperature for OSX. -## Usage +## Usage ### Compiling @@ -37,15 +37,15 @@ clib install lavoiesl/osx-cpu-temp ### Options - * `-h` Print command line options. - * `-C` Output temperature in Celsius (default). - * `-F` Output temperature in Fahrenheit. - * `-c [key]` Print CPU temperature, optionally providing the SMC key. - * `-g [key]` Print GPU temperature, optionally providing the SMC key. - * `-f` Output fan speed. - * `-t key` Print temperature value of given SMC key. - * `-r key` Print raw value of given SMC key. - * `-a` Print ALL available SMC keys (without description!). +* Output + * `-c` Display CPU temperature (Default). + * `-g` Display GPU temperature. + * `-a` Display ambient temperature. + * `-f` Display fan speeds. +* Format + * `-C` Display temperatures in degrees Celsius (Default). + * `-F` Display temperatures in degrees Fahrenheit. + * `-T` Do not display the units for temperatures. ### Explore keys * see https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCKeys.txt @@ -57,16 +57,16 @@ osx-cpu-temp -r '#KEY' # number of keys osx-cpu-temp -r TC0P # CPU proximity sensor ``` -## Maintainer +## Maintainer Sébastien Lavoie -### Source +### Source -Apple System Management Control (SMC) Tool +Apple System Management Control (SMC) Tool Copyright (C) 2006 -### Inspiration +### Inspiration * https://www.eidac.de/smcfancontrol/ * https://github.com/hholtmann/smcFanControl/tree/master/smc-command diff --git a/smc.c b/smc.c index 2f14b35..4920b62 100644 --- a/smc.c +++ b/smc.c @@ -485,6 +485,9 @@ int main(int argc, char* argv[]) return -1; } break; + case 'a': + amb = true; + break; case 'h': case '?': printf("usage: osx-cpu-temp \n"); @@ -509,7 +512,7 @@ int main(int argc, char* argv[]) cpu = 1; } - bool show_title = fan + gpu + cpu > 1; + bool show_title = fan + gpu + cpu + amb > 1; SMCOpen(); @@ -519,6 +522,9 @@ int main(int argc, char* argv[]) if (gpu) { readAndPrintTemperature("GPU: ", show_title, SMC_KEY_GPU_TEMP, scale, show_scale); } + if (amb) { + readAndPrintTemperature("Ambient: ", show_title, SMC_KEY_AMBIENT_TEMP, scale, show_scale); + } if (fan) { readAndPrintFanRPMs(); } diff --git a/smc.h b/smc.h index d54513e..84a73a6 100644 --- a/smc.h +++ b/smc.h @@ -1,6 +1,6 @@ /* * Apple System Management Control (SMC) Tool - * Copyright (C) 2006 devnull + * Copyright (C) 2006 devnull * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -46,6 +46,7 @@ // key values #define SMC_KEY_CPU_TEMP "TC0P" #define SMC_KEY_GPU_TEMP "TG0P" +#define SMC_KEY_AMBIENT_TEMP "TA0V" #define SMC_KEY_FAN0_RPM_CUR "F0Ac" typedef struct { From b2568fb041cadf14010552bb7c00ebab4a40ba62 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:45:24 +0200 Subject: [PATCH 13/20] resolve conflicts --- README.md | 16 +++++- smc.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 164 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2ecd303..a4ed62c 100644 --- a/README.md +++ b/README.md @@ -42,10 +42,24 @@ clib install lavoiesl/osx-cpu-temp * `-g` Display GPU temperature. * `-a` Display ambient temperature. * `-f` Display fan speeds. + * `-c [key]` Print CPU temperature, optionally providing the SMC key. + * `-g [key]` Print GPU temperature, optionally providing the SMC key. + * `-t key` Print temperature value of given SMC key. + * `-r key` Print raw value of given SMC key. * Format * `-C` Display temperatures in degrees Celsius (Default). * `-F` Display temperatures in degrees Fahrenheit. - * `-T` Do not display the units for temperatures. + * `-T` Do not display the units for temperatures. + +### Explore keys +* see https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCKeys.txt +* see https://app.assembla.com/wiki/show/fakesmc + +Print the raw value of any key, if it exists +```shell script +osx-cpu-temp -r '#KEY' # number of keys +osx-cpu-temp -r TC0P # CPU proximity sensor +``` ### Explore keys * see https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCKeys.txt diff --git a/smc.c b/smc.c index 4920b62..ffa2697 100644 --- a/smc.c +++ b/smc.c @@ -138,17 +138,129 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) return kIOReturnSuccess; } +UInt32 SMCReadIndexCount(void) +{ + SMCVal_t val; + + SMCReadKey("#KEY", &val); + return _strtoul((char *)val.bytes, val.dataSize, 10); +} + +double SMCNormalizeFloat(SMCVal_t val) +{ + if (strcmp(val.dataType, DATATYPE_SP78) == 0) { + return ((SInt16)ntohs(*(UInt16*)val.bytes)) / 256.0; + } + if (strcmp(val.dataType, DATATYPE_SP5A) == 0) { + return ((SInt16)ntohs(*(UInt16*)val.bytes)) / 1024.0; + } + if (strcmp(val.dataType, DATATYPE_FPE2) == 0) { + return ntohs(*(UInt16*)val.bytes) / 4.0; + } + if (strcmp(val.dataType, DATATYPE_FP88) == 0) { + return ntohs(*(UInt16*)val.bytes) / 256.0; + } + return -1.f; +} + +int SMCNormalizeInt(SMCVal_t val) +{ + if (strcmp(val.dataType, DATATYPE_UINT8) == 0 || strcmp(val.dataType, DATATYPE_UINT16) == 0 || strcmp(val.dataType, DATATYPE_UINT32) == 0) { + return (int) _strtoul((char *)val.bytes, val.dataSize, 10); + } + if (strcmp(val.dataType, DATATYPE_SI16) == 0) { + return ntohs(*(SInt16*)val.bytes); + } + + if (strcmp(val.dataType, DATATYPE_HEX) == 0 || strcmp(val.dataType, DATATYPE_FLAG) == 0) { + printf("Hex value = 0x"); + int i; + for (i = 0; i < val.dataSize; i++) + printf("%02x ", (unsigned char) val.bytes[i]); + printf("\n"); + return (int) _strtoul((char *)val.bytes, val.dataSize, 10); + } + return -1; +} + +char* SMCNormalizeText(SMCVal_t val) +{ + char result[val.dataSize + 2]; + if (strcmp(val.dataType, DATATYPE_CH8) == 0) { + int i; + for (i = 0; i < val.dataSize; i++) { + result[i] = (unsigned char)val.bytes[i]; + } + result[i+1] = 0; + return strdup(result); + } + + // convert anything else to text + double f = SMCNormalizeFloat(val); + if (f != -1.0) { + snprintf(result, 10, "%0.1f", f); + return strdup(result); + } + int i = SMCNormalizeInt(val); + if (i != -1) { + snprintf(result, 10, "%d", i); + return strdup(result); + } + + return strdup(result); +} + +kern_return_t SMCPrintAll(void) +{ + kern_return_t result; + SMCKeyData_t inputStructure; + SMCKeyData_t outputStructure; + + int totalKeys, i; + UInt32Char_t key; + SMCVal_t val; + + totalKeys = SMCReadIndexCount(); + printf("Total keys = %d\n", totalKeys); + + for (i = 0; i < totalKeys; i++) + { + memset(&inputStructure, 0, sizeof(SMCKeyData_t)); + memset(&outputStructure, 0, sizeof(SMCKeyData_t)); + memset(&val, 0, sizeof(SMCVal_t)); + + inputStructure.data8 = SMC_CMD_READ_INDEX; + inputStructure.data32 = i; + + result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure); + if (result != kIOReturnSuccess) + continue; + + _ultostr(key, outputStructure.key); + + SMCReadKey(key, &val); + char* txt = SMCNormalizeText(val); + printf("key = %s type = %s value = %s\n", key, val.dataType, txt); + } + + return kIOReturnSuccess; +} + // Requires SMCOpen() double SMCGetTemperature(char* key) { kern_return_t result; SMCVal_t val; - result = SMCReadKey("#KEY", &val); - if (result != kIOReturnSuccess) { - return 0; + result = SMCReadKey(key, &val); + if (result == kIOReturnSuccess) { + // read succeeded - check returned value + if (val.dataSize > 0) { + return SMCNormalizeFloat(val); + } } - return _strtoul((char*)val.bytes, val.dataSize, 10); + // read failed + return -1.0; } double SMCNormalizeFloat(SMCVal_t val) @@ -269,6 +381,38 @@ double SMCGetDouble(char* key) return -1.0; } +float SMCGetFanRPM(char* key) +{ + SMCVal_t val; + kern_return_t result; + + result = SMCReadKey(key, &val); + if (result == kIOReturnSuccess) { + // read succeeded - check returned value + if (val.dataSize > 0) { + return SMCNormalizeFloat(val); + } + } + // read failed + return -1.f; +} + +double SMCGetPower(char* key) +{ + SMCVal_t val; + kern_return_t result; + + result = SMCReadKey(key, &val); + if (result == kIOReturnSuccess) { + // read succeeded - check returned value + if (val.dataSize > 0) { + return SMCNormalizeFloat(val); + } + } + // read failed + return -1.f; +} + double convertToFahrenheit(double celsius) { return (celsius * (9.0 / 5.0)) + 32.0; @@ -296,7 +440,6 @@ void readAndPrintTemperature(char* title, bool show_title, char* key, char scale } else { printf("%s%0.1f\n", title, temperature); } -<<<<<<< HEAD readAndPrintTemperature(title, SMC_KEY_CPU_TEMP, scale); } @@ -486,7 +629,7 @@ int main(int argc, char* argv[]) } break; case 'a': - amb = true; + amb = 1; break; case 'h': case '?': From 1d02932ff1400e442544093f3e5cba0b4c7ce7d4 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:47:55 +0200 Subject: [PATCH 14/20] resolve conflicts --- smc.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/smc.c b/smc.c index ffa2697..2dbfa50 100644 --- a/smc.c +++ b/smc.c @@ -140,6 +140,7 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) UInt32 SMCReadIndexCount(void) { + kern_return_t result; SMCVal_t val; SMCReadKey("#KEY", &val); @@ -413,6 +414,8 @@ double SMCGetPower(char* key) return -1.f; } +======= +>>>>>>> ef86749 (resolve conflicts) double convertToFahrenheit(double celsius) { return (celsius * (9.0 / 5.0)) + 32.0; @@ -420,7 +423,7 @@ double convertToFahrenheit(double celsius) double readTemperature(char* key, char scale) { - double temperature = SMCGetTemperature(key); + double temperature = SMCGetDouble(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -429,8 +432,9 @@ double readTemperature(char* key, char scale) void readAndPrintTemperature(char* title, bool show_title, char* key, char scale, bool show_scale) { - if (!show_title) { - title = ""; + double temperature = SMCGetDouble(key); + if (scale == 'F') { + temperature = convertToFahrenheit(temperature); } double temperature = readTemperature(key, scale); From 411e700e6e12b422be0d7a8423644ccfc7323ab4 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:49:08 +0200 Subject: [PATCH 15/20] resolve conflicts --- smc.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/smc.c b/smc.c index 2dbfa50..124f47b 100644 --- a/smc.c +++ b/smc.c @@ -414,8 +414,6 @@ double SMCGetPower(char* key) return -1.f; } -======= ->>>>>>> ef86749 (resolve conflicts) double convertToFahrenheit(double celsius) { return (celsius * (9.0 / 5.0)) + 32.0; @@ -646,9 +644,9 @@ int main(int argc, char* argv[]) printf(" -g [key] Display GPU temperature [of given key].\n"); printf(" -a Display ambient temperature.\n"); printf(" -f Display fan speeds.\n"); - printf(" -t key Display temperature of given SMC key.\n"); - printf(" -r key Display raw value of given SMC key.\n"); - printf(" -A Display ALL SMC keys\n") + printf(" -A Display all SMC keys") + printf(" -t key Display temperature of given SMC key\n"); + printf(" -r key Display raw value of given SMC key\n"); printf(" -h Display this help.\n"); printf("\nIf more than one of -c, -f, or -g are specified, titles will be added\n"); return -1; From a3b24d8e9554b5eefcb8df8c7103041cebdd1309 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 19:56:17 +0200 Subject: [PATCH 16/20] dedupe code --- smc.c | 84 ----------------------------------------------------------- 1 file changed, 84 deletions(-) diff --git a/smc.c b/smc.c index 124f47b..b583f8a 100644 --- a/smc.c +++ b/smc.c @@ -264,70 +264,6 @@ double SMCGetTemperature(char* key) return -1.0; } -double SMCNormalizeFloat(SMCVal_t val) -{ - if (strcmp(val.dataType, DATATYPE_SP78) == 0) { - return ((SInt16)ntohs(*(UInt16*)val.bytes)) / 256.0; - } - if (strcmp(val.dataType, DATATYPE_SP5A) == 0) { - return ((SInt16)ntohs(*(UInt16*)val.bytes)) / 1024.0; - } - if (strcmp(val.dataType, DATATYPE_FPE2) == 0) { - return ntohs(*(UInt16*)val.bytes) / 4.0; - } - if (strcmp(val.dataType, DATATYPE_FP88) == 0) { - return ntohs(*(UInt16*)val.bytes) / 256.0; - } - return -1.f; -} - -int SMCNormalizeInt(SMCVal_t val) -{ - if (strcmp(val.dataType, DATATYPE_UINT8) == 0 || strcmp(val.dataType, DATATYPE_UINT16) == 0 || strcmp(val.dataType, DATATYPE_UINT32) == 0) { - return (int)_strtoul((char*)val.bytes, val.dataSize, 10); - } - if (strcmp(val.dataType, DATATYPE_SI16) == 0) { - return ntohs(*(SInt16*)val.bytes); - } - - if (strcmp(val.dataType, DATATYPE_HEX) == 0 || strcmp(val.dataType, DATATYPE_FLAG) == 0) { - printf("Hex value = 0x"); - int i; - for (i = 0; i < val.dataSize; i++) - printf("%02x ", (unsigned char)val.bytes[i]); - printf("\n"); - return (int)_strtoul((char*)val.bytes, val.dataSize, 10); - } - return -1; -} - -char* SMCNormalizeText(SMCVal_t val) -{ - char result[val.dataSize + 2]; - if (strcmp(val.dataType, DATATYPE_CH8) == 0) { - int i; - for (i = 0; i < val.dataSize; i++) { - result[i] = (unsigned char)val.bytes[i]; - } - result[i + 1] = 0; - return strdup(result); - } - - // convert anything else to text - double f = SMCNormalizeFloat(val); - if (f != -1.0) { - snprintf(result, 10, "%0.1f", f); - return strdup(result); - } - int i = SMCNormalizeInt(val); - if (i != -1) { - snprintf(result, 10, "%d", i); - return strdup(result); - } - - return strdup(result); -} - kern_return_t SMCPrintAll(void) { kern_return_t result; @@ -430,7 +366,6 @@ double readTemperature(char* key, char scale) void readAndPrintTemperature(char* title, bool show_title, char* key, char scale, bool show_scale) { - double temperature = SMCGetDouble(key); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } @@ -445,25 +380,6 @@ void readAndPrintTemperature(char* title, bool show_title, char* key, char scale readAndPrintTemperature(title, SMC_KEY_CPU_TEMP, scale); } -void readAndPrintGpuTemp(bool show_title, char scale) -{ - char* title = ""; - if (show_title) { - title = "GPU: "; - } - readAndPrintTemperature(title, SMC_KEY_GPU_TEMP, scale); -} - -// Requires SMCOpen() -void readAndPrintTemp(char* key, char scale) -{ - double temperature = SMCGetDouble(key); - if (scale == 'F') { - temperature = convertToFahrenheit(temperature); - } - printf("%s = %0.1f °%c\n", key, temperature, scale); -} - // Requires SMCOpen() void readAndPrintRawValue(char* key) { From f5cb94646c3775e1e3caa20e725a39c15ae432f2 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 20:46:31 +0200 Subject: [PATCH 17/20] fix merge issues --- smc.c | 48 ++++-------------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/smc.c b/smc.c index b583f8a..ca496a9 100644 --- a/smc.c +++ b/smc.c @@ -140,7 +140,6 @@ kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t* val) UInt32 SMCReadIndexCount(void) { - kern_return_t result; SMCVal_t val; SMCReadKey("#KEY", &val); @@ -264,44 +263,6 @@ double SMCGetTemperature(char* key) return -1.0; } -kern_return_t SMCPrintAll(void) -{ - kern_return_t result; - SMCKeyData_t inputStructure; - SMCKeyData_t outputStructure; - - int totalKeys, i; - UInt32Char_t key; - SMCVal_t val; - - totalKeys = SMCReadIndexCount(); - printf("Total keys = %d\n", totalKeys); - - for (i = 0; i < totalKeys; i++) { - memset(&inputStructure, 0, sizeof(SMCKeyData_t)); - memset(&outputStructure, 0, sizeof(SMCKeyData_t)); - memset(&val, 0, sizeof(SMCVal_t)); - - inputStructure.data8 = SMC_CMD_READ_INDEX; - inputStructure.data32 = i; - - result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure); - if (result != kIOReturnSuccess) - continue; - - _ultostr(key, outputStructure.key); - - result = SMCReadKey(key, &val); - if (result != kIOReturnSuccess) - continue; - - char* txt = SMCNormalizeText(val); - printf("key = %s type = %s value = %s\n", key, val.dataType, txt); - } - - return kIOReturnSuccess; -} - double SMCGetDouble(char* key) { SMCVal_t val; @@ -366,18 +327,16 @@ double readTemperature(char* key, char scale) void readAndPrintTemperature(char* title, bool show_title, char* key, char scale, bool show_scale) { + double temperature = readTemperature(key, scale); if (scale == 'F') { temperature = convertToFahrenheit(temperature); } - double temperature = readTemperature(key, scale); - if (show_scale) { printf("%s%0.1f °%c\n", title, temperature, scale); } else { printf("%s%0.1f\n", title, temperature); } - readAndPrintTemperature(title, SMC_KEY_CPU_TEMP, scale); } // Requires SMCOpen() @@ -492,6 +451,7 @@ int main(int argc, char* argv[]) int cpu = 0; int fan = 0; int gpu = 0; + int amb = 0; int tmp = 0; int raw = 0; int all = 0; @@ -560,7 +520,7 @@ int main(int argc, char* argv[]) printf(" -g [key] Display GPU temperature [of given key].\n"); printf(" -a Display ambient temperature.\n"); printf(" -f Display fan speeds.\n"); - printf(" -A Display all SMC keys") + printf(" -A Display all SMC keys"); printf(" -t key Display temperature of given SMC key\n"); printf(" -r key Display raw value of given SMC key\n"); printf(" -h Display this help.\n"); @@ -590,7 +550,7 @@ int main(int argc, char* argv[]) readAndPrintFanRPMs(); } if (tmp) { - readAndPrintTemp(key, scale); + readAndPrintTemperature(key, show_title, key, scale, show_scale); } if (raw) { readAndPrintRawValue(key); From 654ece4d3903a355d396e552532fe251d2ebb779 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 20:51:39 +0200 Subject: [PATCH 18/20] cleanup --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a4ed62c..d7a160d 100644 --- a/README.md +++ b/README.md @@ -38,18 +38,16 @@ clib install lavoiesl/osx-cpu-temp ### Options * Output - * `-c` Display CPU temperature (Default). - * `-g` Display GPU temperature. - * `-a` Display ambient temperature. - * `-f` Display fan speeds. * `-c [key]` Print CPU temperature, optionally providing the SMC key. * `-g [key]` Print GPU temperature, optionally providing the SMC key. + * `-a` Display ambient temperature. + * `-f` Display fan speeds. * `-t key` Print temperature value of given SMC key. * `-r key` Print raw value of given SMC key. * Format * `-C` Display temperatures in degrees Celsius (Default). * `-F` Display temperatures in degrees Fahrenheit. - * `-T` Do not display the units for temperatures. + * `-T` Do not display the units for temperatures. ### Explore keys * see https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCKeys.txt From 5ea26b97e25b79f94ab3d83d99f040951da221d8 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 22:00:53 +0200 Subject: [PATCH 19/20] fix optional params --- smc.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/smc.c b/smc.c index ca496a9..8b1fc29 100644 --- a/smc.c +++ b/smc.c @@ -328,9 +328,6 @@ double readTemperature(char* key, char scale) void readAndPrintTemperature(char* title, bool show_title, char* key, char scale, bool show_scale) { double temperature = readTemperature(key, scale); - if (scale == 'F') { - temperature = convertToFahrenheit(temperature); - } if (show_scale) { printf("%s%0.1f °%c\n", title, temperature, scale); @@ -458,11 +455,12 @@ int main(int argc, char* argv[]) char* key; int c; - while ((c = getopt(argc, argv, ":CFc:g:ft:r:aAh?")) != -1) { + while ((c = getopt(argc, argv, ":CFTc:g:afAt:r:h?")) != -1) { switch (c) { case 'F': + scale = 'F'; + break; case 'C': - scale = c; break; case 'T': show_scale = false; @@ -470,16 +468,33 @@ int main(int argc, char* argv[]) case 'c': // optionally allow to pass the SMC Key (**TC0P**, TC0D, TCXC, ...) cpu = 1; - key = optarg; + if (optarg[0] == '-') { + key = SMC_KEY_CPU_TEMP; + optind -= 1; + } else { + key = optarg; + } break; case 'g': // optionally allow to pass the SMC Key (**TG0P**, TG0D, TCGC, ...) gpu = 1; - key = optarg; + if (optarg[0] == '-') { + key = SMC_KEY_GPU_TEMP; + optind -= 1; + } else { + key = optarg; + } + break; + case 'a': + amb = 1; break; case 'f': fan = true; break; + // all option, see: https://github.com/hholtmann/smcFanControl/blob/875c68b0d36fbda40d2bf745fc43dcb40523360b/smc-command/smc.c#L485 + case 'A': + all = 1; + break; case 't': tmp = 1; key = optarg; @@ -488,17 +503,15 @@ int main(int argc, char* argv[]) raw = 1; key = optarg; break; - // all option, see: https://github.com/hholtmann/smcFanControl/blob/875c68b0d36fbda40d2bf745fc43dcb40523360b/smc-command/smc.c#L485 - case 'A': - all = 1; - break; case ':': // optional arguments, set defaults switch (optopt) { case 'c': + cpu = 1; key = SMC_KEY_CPU_TEMP; break; case 'g': + gpu = 1; key = SMC_KEY_GPU_TEMP; break; default: @@ -506,9 +519,6 @@ int main(int argc, char* argv[]) return -1; } break; - case 'a': - amb = 1; - break; case 'h': case '?': printf("usage: osx-cpu-temp \n"); @@ -529,7 +539,7 @@ int main(int argc, char* argv[]) } } - if (!fan && !gpu && !tmp && !raw & !all) { + if (!fan && !gpu && !amb && !tmp && !raw && !all) { cpu = 1; } From 016a43ce53361d04952b46fd135cf68852d10482 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Wed, 28 Jun 2023 22:10:15 +0200 Subject: [PATCH 20/20] fix optional title --- smc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/smc.c b/smc.c index 8b1fc29..1ccb1cc 100644 --- a/smc.c +++ b/smc.c @@ -327,6 +327,10 @@ double readTemperature(char* key, char scale) void readAndPrintTemperature(char* title, bool show_title, char* key, char scale, bool show_scale) { + if (!show_title) { + title = ""; + } + double temperature = readTemperature(key, scale); if (show_scale) {