aboutsummaryrefslogtreecommitdiff
path: root/sensor.c
blob: f386f0c292c4104ca691c1d81d2bbd9fa992387d (plain)
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
/*******************************************************************************
 * Copyright (C) 2010, Linaro Limited.
 *
 * This file is part of PowerDebug.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Amit Arora <amit.arora@linaro.org> (IBM Corporation)
 *       - initial API and implementation
 *******************************************************************************/

#include "powerdebug.h"
#include "sensor.h"
#include "tree.h"
#include "utils.h"

#define SYSFS_SENSOR "/sys/class/hwmon"

static struct tree *sensor_tree;

struct temp_info {
	char name[NAME_MAX];
	int temp;
};

struct fan_info {
	char name[NAME_MAX];
	int rpms;
};

struct sensor_info {
	char name[NAME_MAX];
	struct temp_info *temperatures;
	struct fan_info *fans;
	short nrtemps;
	short nrfans;
};

static int sensor_dump_cb(struct tree *tree, void *data)
{
	int i;
	struct sensor_info *sensor = tree->private;

	if (!strlen(sensor->name))
		return 0;

	printf("%s\n", sensor->name);

	for (i = 0; i < sensor->nrtemps; i++)
		printf(" %s %.1f °C\n", sensor->temperatures[i].name,
		       (float)sensor->temperatures[i].temp / 1000);

	for (i = 0; i < sensor->nrfans; i++)
		printf(" %s %d rpm\n", sensor->fans[i].name,
		       sensor->fans[i].rpms);

	return 0;
}

int sensor_dump(void)
{
	printf("\nSensor Information:\n");
	printf("*******************\n\n");

	return tree_for_each(sensor_tree, sensor_dump_cb, NULL);
}

static struct sensor_info *sensor_alloc(void)
{
	struct sensor_info *sensor;

	sensor = malloc(sizeof(*sensor));
	if (sensor)
		memset(sensor, 0, sizeof(*sensor));

	return sensor;
}

static int read_sensor_cb(struct tree *tree, void *data)
{
	DIR *dir;
	int value;
        struct dirent dirent, *direntp;
	struct sensor_info *sensor = tree->private;

	int nrtemps = 0;
	int nrfans = 0;

	dir = opendir(tree->path);
	if (!dir)
		return -1;

	file_read_value(tree->path, "name", "%s", sensor->name);

	while (!readdir_r(dir, &dirent, &direntp)) {

                if (!direntp)
                        break;

		if (direntp->d_type != DT_REG)
			continue;

		if (!strncmp(direntp->d_name, "temp", 4)) {

			if (file_read_value(tree->path, direntp->d_name, "%d",
					    &value))
				continue;

			sensor->temperatures =
				realloc(sensor->temperatures,
					sizeof(struct temp_info) * (nrtemps + 1));
			if (!sensor->temperatures)
				continue;

			strcpy(sensor->temperatures[nrtemps].name,
			       direntp->d_name);
			sensor->temperatures[nrtemps].temp = value;

			nrtemps++;
		}

		if (!strncmp(direntp->d_name, "fan", 3)) {

			if (file_read_value(tree->path, direntp->d_name, "%d",
					    &value))
				continue;

			sensor->fans =
				realloc(sensor->fans,
					sizeof(struct temp_info) * (nrfans + 1));
			if (!sensor->fans)
				continue;

			strcpy(sensor->fans[nrfans].name,
			       direntp->d_name);
			sensor->fans[nrfans].rpms = value;

			nrfans++;
		}
	}

	sensor->nrtemps = nrtemps;
	sensor->nrfans = nrfans;

	closedir(dir);

	return 0;
}

static int fill_sensor_cb(struct tree *t, void *data)
{
	struct sensor_info *sensor;

	sensor = sensor_alloc();
	if (!sensor)
		return -1;

	t->private = sensor;

	if (!t->parent)
		return 0;

	return read_sensor_cb(t, data);
}

static int fill_sensor_tree(void)
{
	return tree_for_each(sensor_tree, fill_sensor_cb, NULL);
}

static int sensor_filter_cb(const char *name)
{
	/* let's ignore some directories in order to avoid to be
	 * pulled inside the sysfs circular symlinks mess/hell
	 * (choose the word which fit better)
	 */
	if (!strcmp(name, "subsystem"))
		return 1;

	if (!strcmp(name, "driver"))
		return 1;

	if (!strcmp(name, "hwmon"))
		return 1;

	if (!strcmp(name, "power"))
		return 1;

	return 0;
}

int sensor_init(void)
{
	sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb);
	if (!sensor_tree)
		return -1;

	return fill_sensor_tree();
}