Created
October 27, 2017 20:30
-
-
Save s4y/5f5317561b8581c1308a1c5d54293d6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CoreFoundation/CoreFoundation.h> | |
#include <IOKit/IOKitLib.h> | |
typedef struct { | |
char key[4]; | |
char ignored[24]; | |
uint32_t size; | |
char ignored2[10]; | |
char cmd; | |
char ignored3[5]; | |
uint16_t val; | |
char ignored4[30]; | |
} smc_msg_t; | |
static uint16_t smbus_read_int(io_connect_t connect) { | |
smc_msg_t in = {{'R', 'T', 'S', 'P'}}; | |
smc_msg_t out = {{0}}; | |
size_t outSz = sizeof(out); | |
in.cmd = 5; | |
in.size = 4; | |
kern_return_t ret = IOConnectCallStructMethod(connect, 2, &in, sizeof(in), &out, &outSz); | |
if (ret) { | |
fprintf(stderr, "IOConnectCallStructMethod error %d", ret); | |
exit(2); | |
} | |
return (out.val>>8) | (out.val<<8); | |
} | |
int main(int argc, char *argv[]) { | |
const io_service_t smcService = IOServiceGetMatchingService( | |
kIOMasterPortDefault, IOServiceMatching("AppleSMC") | |
); | |
io_connect_t connect = 0; | |
if (IOServiceOpen(smcService, mach_task_self_, 0, &connect) != kIOReturnSuccess) { | |
fputs("Failed to open the SMC.", stderr); | |
exit(1); | |
} | |
while (true) { | |
printf("%.2f\n", smbus_read_int(connect) / 100.0); | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment