-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsmctemp.h
145 lines (130 loc) · 5.19 KB
/
smctemp.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Apple System Management Control (SMC) Tool
* Copyright (C) 2006 devnull
* Portions Copyright (C) 2013 Michael Wilber
* Copyright (C) 2022 narugit
* - Modified Date: 2022/2/22
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef SMCTEMP_H_
#define SMCTEMP_H_
#include <IOKit/IOKitLib.h>
#include <string>
#include <utility>
#include <vector>
#include "smctemp_types.h"
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
namespace smctemp {
const char kVersion[] = "0.6.0";
constexpr char kIOAppleSmcHiddenClassName[] = "AppleSMC";
constexpr char kSmcCmdReadBytes = 5;
constexpr char kSmcCmdReadIndex = 8;
constexpr char kSmcCmdReadKeyInfo = 9;
constexpr uint32_t kKernelIndexSmc = 2;
constexpr int kOpNone = 0;
constexpr int kOpList = 1;
constexpr int kOpReadCpuTemp = 2;
constexpr int kOpReadGpuTemp = 3;
// List of key and name:
// - https://github.com/exelban/stats/blob/6b88eb1f60a0eb5b1a7b51b54f044bf637fd785b/Modules/Sensors/values.swift
// - https://github.com/acidanthera/VirtualSMC/blob/632fec680d996a5dd015afd9acf0ba40f75e69e2/Docs/SMCSensorKeys.txt
#if defined(ARCH_TYPE_X86_64)
// CPU
constexpr UInt32Char_t kSensorTC0D = "TC0D"; // CPU die temperature
constexpr UInt32Char_t kSensorTC0E = "TC0E"; // CPU PECI die filtered temperature
constexpr UInt32Char_t kSensorTC0F = "TC0F"; // CPU PECI die temperature filtered then adjusted
constexpr UInt32Char_t kSensorTC0P = "TC0P"; // CPU proximity temperature
// GPU
constexpr UInt32Char_t kSensorTG0D = "TG0D"; // PCH Die Temp
constexpr UInt32Char_t kSensorTPCD = "TPCD"; // PCH Die Temp (digital)
#elif defined(ARCH_TYPE_ARM64)
// CPU
constexpr UInt32Char_t kSensorTc0a = "Tc0a";
constexpr UInt32Char_t kSensorTc0b = "Tc0b";
constexpr UInt32Char_t kSensorTc0x = "Tc0x";
constexpr UInt32Char_t kSensorTc0z = "Tc0z";
constexpr UInt32Char_t kSensorTp01 = "Tp01";
constexpr UInt32Char_t kSensorTp05 = "Tp05";
constexpr UInt32Char_t kSensorTp09 = "Tp09";
constexpr UInt32Char_t kSensorTp0D = "Tp0D";
constexpr UInt32Char_t kSensorTp0H = "Tp0H";
constexpr UInt32Char_t kSensorTp0L = "Tp0L";
constexpr UInt32Char_t kSensorTp0P = "Tp0P";
constexpr UInt32Char_t kSensorTp0T = "Tp0T";
constexpr UInt32Char_t kSensorTp0X = "Tp0X";
constexpr UInt32Char_t kSensorTp0b = "Tp0b";
constexpr UInt32Char_t kSensorTp0f = "Tp0f";
constexpr UInt32Char_t kSensorTp0j = "Tp0j";
constexpr UInt32Char_t kSensorTp0n = "Tp0n";
constexpr UInt32Char_t kSensorTp0r = "Tp0r";
constexpr UInt32Char_t kSensorTp1h = "Tp1h";
constexpr UInt32Char_t kSensorTp1t = "Tp1t";
constexpr UInt32Char_t kSensorTp1p = "Tp1p";
constexpr UInt32Char_t kSensorTp1l = "Tp1l";
// GPU
constexpr UInt32Char_t kSensorTg05 = "Tg05";
constexpr UInt32Char_t kSensorTg0D = "Tg0D";
constexpr UInt32Char_t kSensorTg0L = "Tg0L";
constexpr UInt32Char_t kSensorTg0P = "Tg0P";
constexpr UInt32Char_t kSensorTg0T = "Tg0T";
constexpr UInt32Char_t kSensorTg0X = "Tg0X";
constexpr UInt32Char_t kSensorTg0b = "Tg0b";
constexpr UInt32Char_t kSensorTg0f = "Tg0f";
constexpr UInt32Char_t kSensorTg0j = "Tg0j";
constexpr UInt32Char_t kSensorTg0v = "Tg0v";
constexpr UInt32Char_t kSensorTg1b = "Tg1b";
constexpr UInt32Char_t kSensorTg4b = "Tg4b";
#endif
class SmcAccessor {
private:
kern_return_t Open();
kern_return_t Close();
kern_return_t ReadSmcVal(const UInt32Char_t key, SmcVal_t& val);
io_connect_t conn_ = 0;
public:
SmcAccessor();
~SmcAccessor();
kern_return_t Call(int index, SmcKeyData_t *inputStructure, SmcKeyData_t *outputStructure);
kern_return_t GetKeyInfo(const uint32_t key, SmcKeyData_keyInfo_t& key_info);
double ReadValue(const UInt32Char_t key);
uint32_t ReadIndexCount();
kern_return_t PrintAll();
void PrintSmcVal(SmcVal_t val);
void PrintByteReadable(SmcVal_t val);
};
class SmcTemp {
private:
double CalculateAverageTemperature(const std::vector<std::string>& sensors,
const std::pair<unsigned int, unsigned int>& limits);
bool StoreValidTemperature(double temperature, std::string file_name);
SmcAccessor smc_accessor_;
bool is_fail_soft_;
const std::string storage_path_ = "/tmp/smctemp/";
const std::string cpu_file_ = "cpu_temperature.txt";
const std::string gpu_file_ = "gpu_temperature.txt";
public:
explicit SmcTemp(bool isFailSoft);
~SmcTemp() = default;
double GetCpuTemp();
double GetGpuTemp();
double GetLastValidCpuTemp();
double GetLastValidGpuTemp();
bool IsValidTemperature(double temperature, const std::pair<unsigned int, unsigned int>& limits);
};
typedef struct {
UInt32Char_t key;
char name[40];
} Sensor_t;
}
#endif //#ifndef SMCTEMP_H_