aboutsummaryrefslogtreecommitdiff
path: root/sensor.c
blob: f5fcd4013c3d6349cf4a7c5c81a44d4be15fe576 (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
/*******************************************************************************
 * 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"

char *get_num(char *fname, char *sensor)
{
	char tmpstr[NAME_MAX];
	char *str;

	strcpy(tmpstr, (fname+strlen(sensor)));

	str = strrchr(tmpstr, '_');
	str[0] = '\0';

	str = strdup(tmpstr);
	return str;
}

void get_sensor_info(char *path, char *fname, char *sensor, int verbose)
{
	FILE *filep;
	char filename[PATH_MAX];
	char **items = NULL, **suffix = NULL;
	char *item, result[NAME_MAX], *num;
	int ret, count = 0;

	(void)verbose; // get rid of warning

	sprintf(filename, "%s/%s", path, fname);

	if (!strcmp(sensor, "in")) {
		items = (char **)items_in;
		suffix = (char **)suffix_in;
	}

	if (!strcmp(sensor, "temp")) {
		items = (char **)items_temp;
		suffix = (char **)suffix_temp;
	}

	if (!strcmp(sensor, "fan")) {
		items = (char **)items_fan;
		suffix = (char **)suffix_fan;
	}
	if (!strcmp(sensor, "pwm")) {
		items = (char **)items_pwm;
		suffix = (char **)suffix_pwm;
	}


	if (!items || !suffix)
		return;

	item = strrchr(fname, '_');
	if (!item)
		return;

	if (item)
		item++;

	if (item > (fname + strlen(fname)))
		return;

	num = get_num(fname, sensor);
	filep = fopen(filename, "r");

	if (!filep)
		goto exit;
	ret = fscanf(filep, "%s", result);
	fclose(filep);

	if (ret != 1)
		goto exit;

	while (strcmp(items[count], "")) {
		if (!strcmp(items[count], item))
			printf("\'temp\' %s sensor %s\t\t%d%s\n",
				num, items[count], atoi(result)/1000,
				suffix[count]);
		count++;
	}
exit:
	free(num);
	return;
}

int read_and_print_sensor_info(int verbose)
{
	DIR *dir, *subdir;
	int len, found = 0;
	char filename[PATH_MAX], devpath[PATH_MAX];
	char device[PATH_MAX];
	struct dirent *item, *subitem;

	printf("\nSensor Information:\n");
	printf("******************\n");

	sprintf(filename, "%s", "/sys/class/hwmon");
	dir = opendir(filename);
	if (!dir)
		return errno;

	while ((item = readdir(dir))) {
		if (item->d_name[0] == '.')  /* skip the hidden files */
			continue;

		found = 1;

		sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
		sprintf(devpath, "%s/device", filename);

		len = readlink(devpath, device, PATH_MAX - 1);

		if (len < 0)
			strcpy(devpath, filename);
		else
			device[len] = '\0';

		subdir = opendir(devpath);

		printf("\nSensor Information for %s :\n", item->d_name);
		fflush(stdin);

		while ((subitem = readdir(subdir))) {
			if (subitem->d_name[0] == '.') /* skip hidden files */
				continue;

			if (!strncmp(subitem->d_name, "in", 2))
				get_sensor_info(devpath, subitem->d_name, "in",
						verbose);
			else if (!strncmp(subitem->d_name, "temp", 4))
				get_sensor_info(devpath, subitem->d_name,
						"temp", verbose);
			else if (!strncmp(subitem->d_name, "fan", 4))
				get_sensor_info(devpath, subitem->d_name,
						"fan", verbose);
			else if (!strncmp(subitem->d_name, "pwm", 4))
				get_sensor_info(devpath, subitem->d_name,
						"pwm", verbose);

		}

		closedir(subdir);
	}
	closedir(dir);

	if (!found && verbose) {
		printf("Could not find sensor information!");
		printf(" Looks like /sys/class/hwmon is empty.\n");
	}

	printf("\n");

	return 0;
}