File: temp.c

package info (click to toggle)
wmtemp 0.0.6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 180 kB
  • ctags: 95
  • sloc: ansic: 777; makefile: 82
file content (92 lines) | stat: -rw-r--r-- 1,925 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sensors.h>

#include "temp.h"

static FILE *f = NULL;
static char sensors = 0;

static const sensors_chip_name *chip_name = NULL;
static const sensors_feature_data *feature;

char *cpu_feature_name = "temp1";
char *sys_feature_name = "temp2";

temperature_t t_type = CELCIUS;

static int cpu_feature = 0;
static int sys_feature = 0;

void temp_deinit() {
	if (f != NULL) {
		fclose(f);
	}

	if (sensors) {
		sensors_cleanup();
	}
}

void temp_init(const char *filename) {
	const sensors_chip_name *name;
	int chip_nr = 0, f1, f2;
	
	atexit(temp_deinit);

	f = fopen(filename, "r");
	if (f == NULL) {
		fprintf(stderr, "could not open configfile %s: %s\n", filename,
				strerror(errno));
		exit(1);
	}

	if (sensors_init(f)) {
		fprintf(stderr, "could not initialize sensors\n");
		exit(1);
	}

	sensors = 1;

	while ((name = sensors_get_detected_chips(&chip_nr)) != NULL &&
			chip_name == NULL) {
		f1 = f2 = 0;

		while ((feature = sensors_get_all_features(*name, &f1, &f2)) != NULL) {
			if (sensors_get_ignored(*name, feature->number) != 0) {
				if (strcmp(feature->name, cpu_feature_name) == 0) {
					cpu_feature = feature->number;
					chip_name = name;
				} else if (strcmp(feature->name, sys_feature_name) == 0) {
					sys_feature = feature->number;
					chip_name = name;
				}
			}
		}
	}

	if (chip_name == NULL) {
		fprintf(stderr, "could not find a suitable chip\n");
		exit(1);
	}
}

void temp_getusage(unsigned int *cpu_temp, unsigned int *sys_temp) {
	double cpu, sys;

	sensors_get_feature(*chip_name, cpu_feature, &cpu);
	sensors_get_feature(*chip_name, sys_feature, &sys);

	if (t_type == FAHRENHEIT) {
		cpu = TO_FAHRENHEIT(cpu);
		sys = TO_FAHRENHEIT(sys);
	} else if (t_type == KELVIN) {
		cpu = TO_KELVIN(cpu);
		sys = TO_KELVIN(sys);
	}
	
	*cpu_temp = (unsigned int)(cpu);
	*sys_temp = (unsigned int)(sys);
}