-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
105 lines (86 loc) · 2.73 KB
/
main.c
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
#include <stdio.h>
#include <unistd.h>
#include <argp.h>
#include "util.h"
#include "board.h"
#include "cpu.h"
#include "fan.h"
#include "signal.h"
#include "config.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
const char* argp_program_version = "ubnt-fan-speed 0.0.1";
const char* argp_program_bug_address = "[email protected]";
static struct argp_option options[] = {
{ "config", 'c', "path", 0, "Config file path", 0 },
{ 0 }
};
static error_t parse_opt(int key, char* arg,struct argp_state* state) {
config_t* config = state->input;
switch (key) {
case 'c':
config->path = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, 0, 0, 0, 0, 0 };
config_t g_config;
ubnt_board_t g_board;
int main(int argc, char** argv) {
short fan_speed;
unsigned char max_cpu_id;
unsigned int temperature;
argp_parse(&argp, argc, argv, 0, 0, &g_config);
signal_init();
read_config(&g_config);
board_init(&g_board, &g_config);
printf("ubnt-fan-speed: Detected board id: 0x%04x\n", g_board.id);
if (g_board.id == 0) {
fprintf(stderr, "ubnt-fan-speed: Unsupported board\n");
return 1;
}
printf("ubnt-fan-speed: Starting...\n");
max_cpu_id = 3;
fan_speed = g_board.minimum_fan_speed;
g_board.set_online_cpus(max_cpu_id);
g_board.set_fan_mode(FAN_MODE_MANUAL);
g_board.set_fan_speed(fan_speed);
while (1) {
g_board.set_fan_mode(FAN_MODE_MANUAL);
temperature = g_board.get_temperature();
if (temperature >= g_board.fan_critical_threshold && fan_speed < 255) {
fan_speed = 255;
g_board.set_fan_speed(fan_speed);
}
else if (temperature >= g_board.fan_high_threshold && fan_speed < 255) {
fan_speed = CLAMP(fan_speed + 5, g_board.minimum_fan_speed, 255);
g_board.set_fan_speed(fan_speed);
}
else if (temperature <= g_board.fan_low_threshold && fan_speed > g_board.minimum_fan_speed) {
fan_speed = CLAMP(fan_speed - 5, g_board.minimum_fan_speed, 255);
g_board.set_fan_speed(fan_speed);
}
if (temperature <= g_board.cpu_low_threshold && max_cpu_id < 3) {
max_cpu_id = 3;
g_board.set_online_cpus(max_cpu_id);
}
else if (temperature >= g_board.cpu_medium_threshold && max_cpu_id > 2) {
max_cpu_id = 2;
g_board.set_online_cpus(max_cpu_id);
}
else if (temperature >= g_board.cpu_high_threshold && max_cpu_id > 1) {
max_cpu_id = 1;
g_board.set_online_cpus(max_cpu_id);
}
else if (temperature >= g_board.cpu_critical_threshold && max_cpu_id > 0) {
max_cpu_id = 0;
g_board.set_online_cpus(max_cpu_id);
}
sleep(1);
}
return 1;
}