-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefan.cpp
More file actions
306 lines (255 loc) · 10.5 KB
/
refan.cpp
File metadata and controls
306 lines (255 loc) · 10.5 KB
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
298
299
300
301
302
303
304
305
306
#include <bits/types/time_t.h>
#include <cmath>
#include <confini.h>
#include <ctime>
#include <stdlib.h>
#include <cstddef>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <ostream>
#include <string>
#include <vector>
#include <math.h>
#include <csignal>
#include <getopt.h>
inline float map(float x, float in_min, float in_max, float out_min, float out_max){
return ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
}
inline float clamp(float x, float min, float max){
return x < min ? min : x > max ? max : x;
}
enum class Verbosity {
FATAL = 0,
ERROR,
WARNING,
INFO,
DEBUG
};
Verbosity global_verbosity = Verbosity::WARNING;
void log(std::string message, Verbosity v){
time_t tt;
time (&tt);
std::string time_str(ctime(&tt));
time_str.pop_back();
switch (v) { //TODO: make logger account for global_verbosity with ifs and whatnot
case Verbosity::FATAL:
std::cout << "\033[31;1m" << time_str << " [F] " << message << "\033[0m\n";
break;
case Verbosity::ERROR:
if(global_verbosity>=Verbosity::ERROR)
std::cout << "\033[31m" << time_str << " [E] " << message << "\033[0m\n";
break;
case Verbosity::WARNING:
if(global_verbosity>=Verbosity::WARNING)
std::cout << "\033[33m" << time_str << " [W] " << message << "\033[0m\n";
break;
case Verbosity::INFO:
if(global_verbosity>=Verbosity::INFO)
std::cout << "\033[36m" << time_str << " [I] " << message << "\033[0m\n";
break;
case Verbosity::DEBUG:
if(global_verbosity>=Verbosity::DEBUG)
std::cout << "\033[37m" << time_str << " [D] " << message << "\033[0m\n";
break;
}
}
typedef struct Fan {
std::string* name = nullptr;
std::string* temp_input_path = nullptr;
std::string* pwm_control_path = nullptr;
std::string* pwm_read_path = nullptr;
std::string* pwm_mode_path = nullptr;
int* min_temp = nullptr;
int* max_temp = nullptr;
int* lc_temp = nullptr;
int* min_pwm = nullptr;
int* max_pwm = nullptr;
int* start_pwm = nullptr;
int* stop_pwm = nullptr;
bool* stopped = nullptr;
} Fan;
std::vector<Fan*> fans;
int uinterval=10;
int fanstep=3;
std::string current_section = "null";
static int dispatch(IniDispatch* const dispatch, void* ){
if(dispatch->type == INI_SECTION) {
std::string section_candidate = dispatch->data;
if ( !(((long long)current_section.find("Fan"))<0) || !(((long long)current_section.find("fan"))<0) ) {
Fan* last = fans.back();
if (fans.size()>0 && (
last->max_pwm == nullptr ||
last->min_pwm == nullptr ||
last->max_temp == nullptr ||
last->min_temp == nullptr ||
last->lc_temp == nullptr ||
last->start_pwm == nullptr ||
last->stop_pwm == nullptr ||
last->temp_input_path == nullptr ||
last->pwm_control_path == nullptr ||
last->pwm_read_path == nullptr ||
last->pwm_mode_path == nullptr
))
{
log(std::string("Fan \"") + current_section + "\"initialisation incomplete, likely because of invalid or missing values in config file\n", Verbosity::ERROR);
log("Not trying to fill empty values automatically because logic isn't implemented yet", Verbosity::FATAL); //TODO: Implement autofilling logic
return -1;
}
}
if (!(((long long)section_candidate.find("Fan"))<0) || !(((long long)section_candidate.find("fan"))<0) ) {
fans.push_back(new Fan());
fans.back()->name = new std::string(section_candidate);
fans.back()->stopped = new bool(false);
fans.back()->lc_temp = new int(0xffffffff);
}
current_section = section_candidate;
}
if(dispatch->type == INI_KEY) {
std::string data = dispatch->data;
if(current_section == "General"){
if(data == "interval"){
uinterval = ini_get_double(dispatch->value) * 1000000;
}
if(data == "step"){
fanstep = ini_get_int(dispatch->value);
}
if(data == "verbosity")
{
global_verbosity=(Verbosity) (ini_get_int(dispatch->value));
}
}
else if (current_section.find("Fan")>0 || current_section.find("fan")>0) {
if(data == "min_pwm")
fans.back()->min_pwm = new int(ini_get_int(dispatch->value));
if(data == "max_pwm")
fans.back()->max_pwm = new int(ini_get_int(dispatch->value));
if(data == "stop_pwm")
fans.back()->stop_pwm = new int(ini_get_int(dispatch->value));
if(data == "start_pwm")
fans.back()->start_pwm = new int(ini_get_int(dispatch->value));
if(data == "min_pwm_temp")
fans.back()->min_temp = new int(ini_get_int(dispatch->value));
if(data == "max_pwm_temp")
fans.back()->max_temp = new int(ini_get_int(dispatch->value));
if(data == "pwm_control_path")
fans.back()->pwm_control_path = new std::string(dispatch->value);
if(data == "pwm_read_path")
fans.back()->pwm_read_path = new std::string(dispatch->value);
if(data == "pwm_mode_path")
fans.back()->pwm_mode_path = new std::string(dispatch->value);
if(data == "temp_sensor_path")
fans.back()->temp_input_path = new std::string(dispatch->value);
}
}
return 0;
}
int fanHandler(Fan* fan){
std::ifstream temp(*fan->temp_input_path, std::ios_base::in);
std::ifstream validate(*fan->pwm_read_path, std::ios_base::in);
std::ofstream control(*fan->pwm_control_path, std::ios_base::out);
char temp_c[16];
temp.read(temp_c, 16);
temp.close();
float temp_f = atof(temp_c)/1000;
if ((!(*fan->lc_temp + fanstep <= temp_f || *fan->lc_temp - fanstep >= temp_f)) && fanstep != -1){ // if threshold is enabled and isn't exceeded
log(*fan->name + " temperature is within the treshold (actual=" + std::to_string(temp_f) + ", threshold=" + std::to_string(*fan->lc_temp) +"), skipping", Verbosity::DEBUG);
return 0;
}
*fan->lc_temp = temp_f;
//logic breakdown:
//fan should start spinning at start_pwm power as soon as temp_f > min_temp
//continue spinning, adjusting the power as temp_f fluctuates
//and stop/slow down as soon as temperature has fallen enough for the pwm value to become less than stop_pwm
//pwm value for writing to the fan control file
float pwm = clamp(round(map(temp_f, (float)*fan->min_temp, (float)*fan->max_temp, (float)*fan->start_pwm, (float)*fan->max_pwm)), *fan->min_pwm, *fan->max_pwm);
if(*fan->stopped){
if(!(pwm>=*fan->start_pwm)){
pwm=0;
}
else{
*fan->stopped = false;
log("Started " + *fan->name, Verbosity::DEBUG);
}
}
if(!*fan->stopped){
if(pwm<=*fan->stop_pwm){
pwm=0;
*fan->stopped = true;
log("Stopped " + *fan->name, Verbosity::DEBUG);
}
}
log("name: " + *fan->name + " pwm: " + std::to_string(pwm).c_str() + " start_pwm: " + std::to_string(*fan->start_pwm) + " stop_pwm: " + std::to_string(*fan->stop_pwm) + " stopped: " + std::to_string(*fan->stopped), Verbosity::DEBUG);
control.write(std::to_string((int)pwm).c_str(), std::to_string((int)pwm).length()); //Write pwm value
control.close();
if (control.fail()) {
log(*fan->name + " write failure, exiting", Verbosity::FATAL);
return -1;
}
char* validate_c = (char*)malloc(sizeof(char) * 16);
validate.read(validate_c, std::max(std::to_string(*fan->max_pwm).length(), std::to_string(*fan->min_pwm).length()));
validate.close();
if (!(pwm == atof(validate_c))){ //Validate pwm value write
log("Validation for " + *fan->name + " yielded " + validate_c + ". Intended value was " + std::to_string((int)pwm), Verbosity::FATAL);
log(*fan->name + " write validation failure, exiting", Verbosity::FATAL);
return -1;
}
return 0;
}
void _reset_fans(){
for (Fan* fan : fans) {
std::ofstream mode(*fan->pwm_mode_path, std::ios_base::out);
mode.write("0", 2);
}
}
void reset_fans(){
_reset_fans();
}
void reset_fans(int sig){
log("Exiting because of signal " + std::to_string(sig), Verbosity::FATAL);
_reset_fans();
exit(sig);
}
int main(int argc, char** argv){
int nr = 1;
if(argc == 1){//check for config
log("No configuration file provided. exiting", Verbosity::FATAL);
exit(-1);
}
if(load_ini_path(argv[1], INI_DEFAULT_FORMAT, NULL, &dispatch, NULL)){ //parse config
log("Config file parsing failed, exiting", Verbosity::FATAL);
exit(-2);
}
//trying hard to set the fans to full speed regardless of circumstance
if (nr>=0) {
std::atexit(&reset_fans);
signal(SIGINT, &reset_fans);
signal(SIGSEGV, &reset_fans);
signal(SIGTERM, &reset_fans);
signal(SIGABRT, &reset_fans);
signal(SIGQUIT, &reset_fans);
signal(SIGKILL, &reset_fans);
signal(SIGPIPE, &reset_fans);
}
for (Fan* fan : fans) { //enable manual control
std::ofstream mode(*fan->pwm_mode_path, std::ios_base::out);
log("Enabling " + *fan->name, Verbosity::INFO);
mode.write("1", 2);
log("Success=" + std::to_string(mode.fail()), Verbosity::DEBUG);
mode.close();
}
log("Waiting for the system to acknowledge the changes...", Verbosity::INFO);
usleep(100000);
log("Beginning the control loop...", Verbosity::INFO);
for (;true;) {
for (Fan* fan : fans) {
if (fanHandler(fan)!=0) {
exit(-3);
}
}
usleep(uinterval);
}
}