Skip to content

Commit f1b3a6d

Browse files
committed
Added a new command line flag to display messages about the temperature and the set fan level. The algorithm for setting a new fan level has been slightly redesigned, now it changes only if the current temperature goes beyond the minimum or maximum.
1 parent 82758e5 commit f1b3a6d

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

tf.c

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25+
#include <argp.h>
26+
#include <libconfig.h>
2527
#include <signal.h>
26-
#include <string.h>
2728
#include <stdlib.h>
28-
#include <libconfig.h>
29+
#include <string.h>
2930
#include <unistd.h>
30-
#include <argp.h>
3131

3232
// Process status
3333
int run = 1;
3434

3535
// Config vars
3636
const char *temperature_f;
3737
const char *fan_f;
38-
int sleep_t;
38+
int sleep_t;
3939

4040
// Struct level items
4141
typedef struct level_e {
@@ -53,9 +53,10 @@ const char doc[] = "tf - tiny program for fan control Thinkpad notebooks";
5353

5454
// Arguments
5555
static struct argp_option options[] = {
56-
{"config", 'c', "CONFIG", 0, "Path to config file (default: /etc/tf.cfg)"},
57-
{"info", 'i', 0, 0, "Displaying messages about temperature and fan level (default: false)"},
58-
{0}
56+
{"config", 'c', "CONFIG", 0, "Path to config file (default: /etc/tf.cfg)"},
57+
{"info", 'i', 0, 0,
58+
"Displaying messages about temperature and fan level (default: false)"},
59+
{0}
5960
};
6061

6162
struct arguments {
@@ -66,13 +67,18 @@ struct arguments {
6667
// Parsing arguments
6768
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
6869
struct arguments *arguments = state->input;
69-
70+
7071
switch (key) {
71-
case 'c': arguments->config = arg; break;
72-
case 'i': arguments->info = 1; break;
73-
default: return ARGP_ERR_UNKNOWN;
72+
case 'c':
73+
arguments->config = arg;
74+
break;
75+
case 'i':
76+
arguments->info = 1;
77+
break;
78+
default:
79+
return ARGP_ERR_UNKNOWN;
7480
}
75-
81+
7682
return 0;
7783
}
7884

@@ -85,17 +91,17 @@ void fan_write(int level) {
8591
fprintf(stderr, "Failed to open %s.\n", fan_f);
8692
exit(1);
8793
}
88-
94+
8995
if (level == -1) {
9096
fprintf(fan, "level auto\n");
9197
fclose(fan);
9298
return;
9399
}
94-
100+
95101
// Message for fan_write
96102
char message[12] = {'\0'};
97103
snprintf(message, sizeof(message), "level %d", level);
98-
104+
99105
fprintf(fan, "%s\n", message);
100106
fclose(fan);
101107
}
@@ -108,10 +114,10 @@ int get_temp() {
108114
exit(1);
109115
}
110116

111-
int temp_c;
117+
int temp_c;
112118
fscanf(temp, "%d", &temp_c);
113119
temp_c /= 1000;
114-
120+
115121
fclose(temp);
116122

117123
return temp_c;
@@ -126,22 +132,22 @@ void kill_handler(int sig) {
126132
void signals_init() {
127133
// Signal set setup
128134
sigset_t set;
129-
sigemptyset(&set);
130-
sigaddset(&set, SIGINT);
135+
sigemptyset(&set);
136+
sigaddset(&set, SIGINT);
131137
sigaddset(&set, SIGTERM);
132-
138+
133139
// Sigaction setup
134140
struct sigaction act;
135141
memset(&act, 0, sizeof(act));
136142
act.sa_handler = kill_handler;
137143
act.sa_mask = set;
138-
144+
139145
// SIGINT
140146
if (sigaction(SIGINT, &act, NULL) < 0) {
141147
fprintf(stderr, "Failed setup handler for SIGINT signal.\n");
142148
exit(1);
143149
}
144-
150+
145151
// SIGTERM
146152
if (sigaction(SIGTERM, &act, NULL) < 0) {
147153
fprintf(stderr, "Failed setup handler for SIGTERM signal.\n");
@@ -152,71 +158,71 @@ void signals_init() {
152158
int main(int argc, char **argv) {
153159
// Init signals
154160
signals_init();
155-
161+
156162
// Arguments
157163
struct arguments arguments;
158164
arguments.config = "/etc/tf.cfg";
159165
arguments.info = 0;
160166
argp_parse(&argp, argc, argv, 0, 0, &arguments);
161-
167+
162168
// Init config
163169
config_t cfg;
164170
config_setting_t *level_settings;
165171
config_init(&cfg);
166-
172+
167173
// Read config file
168174
if (!config_read_file(&cfg, arguments.config)) {
169175
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
170-
config_error_line(&cfg), config_error_text(&cfg));
176+
config_error_line(&cfg), config_error_text(&cfg));
171177
config_destroy(&cfg);
172178
return 1;
173179
}
174-
180+
175181
// Read temperature param from config
176182
if (!config_lookup_string(&cfg, "temperature", &temperature_f)) {
177183
fprintf(stderr, "Failed read temperature param from config file.\n");
178184
config_destroy(&cfg);
179185
return 1;
180186
}
181-
187+
182188
// Read fan param from config
183189
if (!config_lookup_string(&cfg, "fan", &fan_f)) {
184190
fprintf(stderr, "Failed read fan param from config file.\n");
185191
config_destroy(&cfg);
186192
return 1;
187193
}
188-
194+
189195
// Read sleep param from config
190196
if (!config_lookup_int(&cfg, "sleep", &sleep_t)) {
191197
fprintf(stderr, "Failed read sleep param from config file.\n");
192198
config_destroy(&cfg);
193199
return 1;
194200
}
195-
201+
196202
// Read levels params from config
197203
level_settings = config_lookup(&cfg, "levels");
198204
if (level_settings != NULL) {
199205
int count = config_setting_length(level_settings);
200-
206+
201207
if ((count < 8) || (count > 8)) {
202208
fprintf(stderr, "The count of levels must be 8 (0-7).\n");
203209
config_destroy(&cfg);
204210
return 1;
205211
}
206-
212+
207213
levels = malloc(count * sizeof(level_e));
208-
214+
209215
for (int i = 0; i < count; i++) {
210216
config_setting_t *level = config_setting_get_elem(level_settings, i);
211-
217+
212218
int min, max;
213-
219+
214220
if (!(config_setting_lookup_int(level, "min", &min) &&
215-
config_setting_lookup_int(level, "max", &max))) {
216-
fprintf(stderr, "Failed read level param from level%d", i);
217-
return 1;
221+
config_setting_lookup_int(level, "max", &max))) {
222+
fprintf(stderr, "Failed read level param from level%d", i);
223+
return 1;
218224
}
219-
225+
220226
levels[i].min = min;
221227
levels[i].max = max;
222228
}
@@ -225,40 +231,41 @@ int main(int argc, char **argv) {
225231
config_destroy(&cfg);
226232
return 1;
227233
}
228-
234+
229235
// Current fan level
230236
unsigned short current_level = 0;
231-
237+
232238
// Install level 0
233239
fan_write(current_level);
234-
240+
235241
while (run) {
236-
//Current cpu temp
242+
// Current cpu temp
237243
int cpu = get_temp();
238-
244+
239245
// Get min and max for current level
240246
int current_min = levels[current_level].min;
241247
int current_max = levels[current_level].max;
242248

243249
// CPU temp control
244250
if (!(cpu > current_min && cpu < current_max)) {
245251
if (current_level < 7 && cpu >= current_max) {
246-
current_level++;
247-
fan_write(current_level);
252+
current_level++;
253+
fan_write(current_level);
248254
} else if (current_level > 0 && cpu <= current_min) {
249-
current_level--;
250-
fan_write(current_level);
255+
current_level--;
256+
fan_write(current_level);
251257
}
252-
258+
253259
if (arguments.info) {
254-
fprintf(stdout, "Current temperature %d. Level %d is set\n", cpu, current_level);
255-
fflush(stdout);
260+
fprintf(stdout, "Current temperature %d. Level %d is set\n", cpu,
261+
current_level);
262+
fflush(stdout);
256263
}
257264
}
258-
265+
259266
sleep(sleep_t);
260267
}
261-
268+
262269
fan_write(-1);
263270
config_destroy(&cfg);
264271
free(levels);

0 commit comments

Comments
 (0)