-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cc
98 lines (88 loc) · 2.74 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <charconv>
#include <iomanip>
#include <iostream>
#include "smctemp.h"
void usage(char* prog) {
std::cout << "Check Temperature by using Apple System Management Control (Smc) tool " << smctemp::kVersion << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << prog << " [options]" << std::endl;
std::cout << " -c : list CPU temperatures (Celsius)" << std::endl;
std::cout << " -g : list GPU temperatures (Celsius)" << std::endl;
std::cout << " -h : help" << std::endl;
std::cout << " -l : list all keys and values" << std::endl;
std::cout << " -v : version" << std::endl;
std::cout << " -n : tries to query the temperature sensors for n times (e.g. -n3)";
std::cout << " (1 second interval) until a valid value is returned" << std::endl;
}
int main(int argc, char *argv[]) {
int c;
unsigned int attempts = 1;
kern_return_t result;
int op = smctemp::kOpNone;
while ((c = getopt(argc, argv, "clvhn:g")) != -1) {
switch(c) {
case 'c':
op = smctemp::kOpReadCpuTemp;
break;
case 'g':
op = smctemp::kOpReadGpuTemp;
break;
case 'n':
if (optarg) {
auto [ptr, ec] = std::from_chars(optarg, optarg + strlen(optarg), attempts);
if (ec != std::errc()) {
std::cerr << "Invalid argument provided for -n (integer is required)" << std::endl;
return 1;
}
}
break;
case 'l':
op = smctemp::kOpList;
break;
case 'v':
std::cout << smctemp::kVersion << std::endl;
return 0;
break;
case 'h':
case '?':
op = smctemp::kOpNone;
break;
}
}
if (op == smctemp::kOpNone) {
usage(argv[0]);
return 1;
}
smctemp::SmcAccessor smc_accessor = smctemp::SmcAccessor();
smctemp::SmcTemp smc_temp = smctemp::SmcTemp();
switch(op) {
case smctemp::kOpList:
result = smc_accessor.PrintAll();
if (result != kIOReturnSuccess) {
std::ios_base::fmtflags ef(std::cerr.flags());
std::cerr << "Error: SmcPrintAll() = "
<< std::hex << result << std::endl;
std::cerr.flags(ef);
}
break;
case smctemp::kOpReadGpuTemp:
case smctemp::kOpReadCpuTemp:
double temp = 0.0;
while (attempts > 0) {
if (op == smctemp::kOpReadCpuTemp) {
temp = smc_temp.GetCpuTemp();
} else if (op == smctemp::kOpReadGpuTemp) {
temp = smc_temp.GetGpuTemp();
}
if (temp > 0.0) {
break;
} else {
usleep(1'000'000);
attempts--;
}
}
std::cout << std::fixed << std::setprecision(1) << temp << std::endl;
break;
}
return 0;
}