diff options
Diffstat (limited to 'src/osx/smc.cpp')
-rw-r--r-- | src/osx/smc.cpp | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/src/osx/smc.cpp b/src/osx/smc.cpp index a9c849b..6c483db 100644 --- a/src/osx/smc.cpp +++ b/src/osx/smc.cpp @@ -4,7 +4,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -18,6 +18,9 @@ tab-size = 4 #include "smc.hpp" +static constexpr size_t MaxIndexCount = sizeof("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") - 1; +static constexpr const char *KeyIndexes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + static UInt32 _strtoul(char *str, int size, int base) { UInt32 total = 0; int i; @@ -34,20 +37,18 @@ static UInt32 _strtoul(char *str, int size, int base) { static void _ultostr(char *str, UInt32 val) { str[0] = '\0'; - sprintf(str, "%c%c%c%c", - (unsigned int)val >> 24, - (unsigned int)val >> 16, - (unsigned int)val >> 8, - (unsigned int)val); + snprintf(str, 5, "%c%c%c%c", + (unsigned int)val >> 24, + (unsigned int)val >> 16, + (unsigned int)val >> 8, + (unsigned int)val); } namespace Cpu { SMCConnection::SMCConnection() { - IOMasterPort(kIOMasterPortDefault, &masterPort); - CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC"); - result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator); + result = IOServiceGetMatchingServices(0, matchingDictionary, &iterator); if (result != kIOReturnSuccess) { throw std::runtime_error("failed to get AppleSMC"); } @@ -65,7 +66,7 @@ namespace Cpu { } } SMCConnection::~SMCConnection() { - IOServiceClose(conn); + IOServiceClose(conn); } long long SMCConnection::getSMCTemp(char *key) { @@ -92,12 +93,15 @@ namespace Cpu { long long SMCConnection::getTemp(int core) { char key[] = SMC_KEY_CPU_TEMP; if (core >= 0) { - snprintf(key, 5, "TC%1dc", core); + if ((size_t)core > MaxIndexCount) { + return -1; + } + snprintf(key, 5, "TC%1cc", KeyIndexes[core]); } long long result = getSMCTemp(key); if (result == -1) { // try again with C - snprintf(key, 5, "TC%1dC", core); + snprintf(key, 5, "TC%1dC", KeyIndexes[core]); result = getSMCTemp(key); } return result; @@ -132,7 +136,7 @@ namespace Cpu { return kIOReturnSuccess; } - + kern_return_t SMCConnection::SMCCall(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure) { size_t structureInputSize; size_t structureOutputSize; @@ -141,10 +145,10 @@ namespace Cpu { structureOutputSize = sizeof(SMCKeyData_t); return IOConnectCallStructMethod(conn, index, - // inputStructure - inputStructure, structureInputSize, - // ouputStructure - outputStructure, &structureOutputSize); + // inputStructure + inputStructure, structureInputSize, + // ouputStructure + outputStructure, &structureOutputSize); } } // namespace Cpu |