-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasus_fanmode.cpp
297 lines (235 loc) · 6.38 KB
/
asus_fanmode.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* asus_fanmode.cpp
*
* Created on: Nov 12, 2019
* Author: Leonid Maksymchuk
*/
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include "config.hpp"
#include "file.hpp"
#define FAILCNT_LIMIT 5
#define FILEBUF_SIZE 32
#define FORMATBUF_SIZE 1024
static struct Config {
std::string sCPUTemp;
unsigned nCPUTempDivider;
std::string sFanMode;
unsigned nModeSilent;
unsigned nModeNormal;
unsigned nModeOverboost;
unsigned nPollInterval;
unsigned nNormalHigh;
unsigned nNormalLow;
unsigned nOverboostHigh;
unsigned nOverboostLow;
} s_cCfg;
static bool s_bRunning = true;
std::string format(const char* pcFmt, ...) {
char vcBuf[FORMATBUF_SIZE];
va_list vaArgs;
va_start(vaArgs, pcFmt);
int nLen = vsprintf(vcBuf, pcFmt, vaArgs);
return std::string(vcBuf, nLen);
}
bool readString(File& rfFile, std::string& rsStr) {
if (!rfFile.isValid()) {
return false;
}
ssize_t nSize;
char vcBuf[FILEBUF_SIZE + 1];
if ((nSize = rfFile.read(vcBuf, FILEBUF_SIZE)) <= 0) {
return false;
}
rsStr = std::string(vcBuf, nSize);
return true;
}
bool searchCPUHwmon() {
int nIndex = 0;
while (true) {
std::string sPath, sName;
sPath = format("/sys/class/hwmon/hwmon%d/", nIndex);
File fName(sPath + "name");
if (!fName.isValid() || !readString(fName, sName)) {
break;
}
if (sName.compare(0, 7, "k10temp") == 0 ||
sName.compare(0, 8, "coretemp") == 0) {
s_cCfg.sCPUTemp = sPath + "temp1_input";
return true;
}
nIndex++;
}
return false;
}
bool readUint(File& rfFile, unsigned& rnValue) {
if (!rfFile.isValid()) {
return false;
}
ssize_t nSize;
char vcBuf[FILEBUF_SIZE + 1];
if ((nSize = rfFile.read(vcBuf, FILEBUF_SIZE)) <= 0) {
return false;
}
vcBuf[nSize] = '\0';
rnValue = strtoul(vcBuf, nullptr, 10);
return true;
}
bool searchFanMode() {
const std::string vsPaths[2] = {
"/sys/devices/platform/asus-nb-wmi/fan_boost_mode",
"/sys/devices/platform/asus-nb-wmi/throttle_thermal_policy"
};
for (int n = 0; n < 2; n++) {
File fFanMode(vsPaths[n]);
unsigned nValue;
if (fFanMode.isValid() && readUint(fFanMode, nValue)) {
s_cCfg.sFanMode = vsPaths[n];
return true;
}
}
return false;
}
bool readTemp(unsigned& rnTemp) {
File fTemp(s_cCfg.sCPUTemp);
return readUint(fTemp, rnTemp);
}
bool readFanMode(unsigned& rnMode) {
File fFanMode(s_cCfg.sFanMode);
return readUint(fFanMode, rnMode);
}
bool writeFanMode(unsigned nMode) {
File fFanMode(s_cCfg.sFanMode, O_WRONLY);
if (!fFanMode.isValid()) {
return false;
}
char vcBuf[12];
int nLength = sprintf(vcBuf, "%u", nMode);
return fFanMode.write(vcBuf, nLength) == nLength;
}
const char* fanModeToStr(unsigned nMode) {
if (nMode == s_cCfg.nModeSilent) {
return "Silent";
}
else if (nMode == s_cCfg.nModeNormal) {
return "Normal";
}
else if (nMode == s_cCfg.nModeOverboost) {
return "Overboost";
}
return "Unknown";
}
int printerr(const char* pcFmt, ...) {
va_list vaArgs;
va_start(vaArgs, pcFmt);
int nLen = vfprintf(stderr, pcFmt, vaArgs);
nLen += fprintf(stderr, " Error: %s\n", strerror(errno));
va_end(vaArgs);
return nLen;
}
void processSigTerm(int nSignal) {
fprintf(stderr, "Terminating...\n");
s_bRunning = false;
}
int main() {
signal(SIGINT, processSigTerm);
signal(SIGTERM, processSigTerm);
ConfigParser cpConf;
cpConf.addOption(s_cCfg.sCPUTemp, "cpu_temp");
cpConf.addOption(s_cCfg.nCPUTempDivider, "cpu_temp_divider");
cpConf.addOption(s_cCfg.sFanMode, "fan_mode");
cpConf.addOption(s_cCfg.nModeSilent, "mode_silent");
cpConf.addOption(s_cCfg.nModeNormal, "mode_normal");
cpConf.addOption(s_cCfg.nModeOverboost, "mode_overboost");
cpConf.addOption(s_cCfg.nPollInterval, "poll");
cpConf.addOption(s_cCfg.nNormalHigh, "normal_high");
cpConf.addOption(s_cCfg.nNormalLow, "normal_low");
cpConf.addOption(s_cCfg.nOverboostHigh, "overboost_high");
cpConf.addOption(s_cCfg.nOverboostLow, "overboost_low");
if (!cpConf.loadConfig("/etc/asus_fanmode.conf") &&
!cpConf.loadConfig("asus_fanmode.conf")) {
fprintf(stderr, "Cannot find configuration file!\n");
return ENOENT;
}
if (s_cCfg.sCPUTemp.empty()) {
if (!searchCPUHwmon()) {
fprintf(stderr, "Cannot find CPU hwmon sysfs entry!\n");
return ENODEV;
}
printf("Found CPU hwmon: %s\n", s_cCfg.sCPUTemp.c_str());
}
if (s_cCfg.sFanMode.empty()) {
if (!searchFanMode()) {
fprintf(stderr, "Cannot find Fan Mode sysfs entry!\n");
return ENODEV;
}
printf("Found Fan Mode: %s\n", s_cCfg.sFanMode.c_str());
}
unsigned nCurrentTemp;
unsigned nCurrentMode;
int nFailCounter = 0;
if (!readTemp(nCurrentTemp) || (nCurrentTemp / s_cCfg.nCPUTempDivider) == 0) {
printerr("Cannot read CPU temperature: %s\n", s_cCfg.sCPUTemp.c_str());
return ENODEV;
}
printf("Current CPU temperature: %u\n", nCurrentTemp / s_cCfg.nCPUTempDivider);
if (!readFanMode(nCurrentMode) || nCurrentMode > 2) {
printerr("Cannot read fan mode: %s\n", s_cCfg.sFanMode.c_str());
return ENODEV;
}
printf("Current Fan Mode: %s\n", fanModeToStr(nCurrentMode));
fflush(stdout);
while (s_bRunning) {
if (!readTemp(nCurrentTemp)) {
printerr("Cannot read CPU temperature!\n");
nFailCounter++;
}
else if (!readFanMode(nCurrentMode)) {
printerr("Cannot read fan mode!\n");
nFailCounter++;
}
else {
unsigned nTemp = nCurrentTemp / s_cCfg.nCPUTempDivider;
unsigned nTargetMode = nCurrentMode;
if (nCurrentMode == s_cCfg.nModeSilent) {
if (nTemp >= s_cCfg.nOverboostHigh) {
nTargetMode = s_cCfg.nModeOverboost;
}
else if (nTemp >= s_cCfg.nNormalHigh) {
nTargetMode = s_cCfg.nModeNormal;
}
}
else if (nCurrentMode == s_cCfg.nModeNormal) {
if (nTemp >= s_cCfg.nOverboostHigh) {
nTargetMode = s_cCfg.nModeOverboost;
}
else if (nTemp < s_cCfg.nNormalLow) {
nTargetMode = s_cCfg.nModeSilent;
}
}
else if (nCurrentMode == s_cCfg.nModeOverboost) {
if (nTemp < s_cCfg.nOverboostLow) {
nTargetMode = s_cCfg.nModeNormal;
}
}
if (nTargetMode != nCurrentMode) {
printf("Switching fan mode to \'%s\', temperature: %u\n",
fanModeToStr(nTargetMode), nTemp);
fflush(stdout);
if (!writeFanMode(nTargetMode)) {
printerr("Cannot switch fan mode!\n");
nFailCounter++;
}
}
}
if (nFailCounter > FAILCNT_LIMIT) {
fprintf(stderr, "Too many errors. Exiting.\n");
return EIO;
}
usleep(s_cCfg.nPollInterval * 1000);
}
return 0;
}