blob: 050a2d15f3e115770515439dbb4e8c9a0c523a01 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
2 * Copyright (C) 2010, Linaro
3 * Copyright (C) 2010, IBM Corporation
4 *
5 * This file is part of PowerDebug.
6 *
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors:
13 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
14 * - initial API and implementation
15 *******************************************************************************/
16
Amit Arorae9e16b02010-08-03 10:15:20 +053017#include "powerdebug.h"
18#include "sensor.h"
19
20char *get_num(char *fname, char *sensor)
21{
22 char tmpstr[NAME_MAX];
23 char *str;
24
25 strcpy(tmpstr, (fname+strlen(sensor)));
26
27 str = strrchr(tmpstr, '_');
28 str[0] = '\0';
29
30 str = strdup(tmpstr);
31 return str;
32}
33
34
35void get_sensor_info(char *path, char *fname, char *sensor, int verbose)
36{
37 FILE *filep;
38 char filename[PATH_MAX];
39 char **items = NULL, **suffix = NULL;
40 char *item, result[NAME_MAX], *num;
41 int ret, count = 0;
42
43 (void)verbose; // get rid of warning
44
45 sprintf(filename, "%s/%s", path, fname);
46
47 if(!strcmp(sensor, "in")) {
48 items = (char **)items_in;
49 suffix = (char **)suffix_in;
50 }
51
52 if(!strcmp(sensor, "temp")) {
53 items = (char **)items_temp;
54 suffix = (char **)suffix_temp;
55 }
56
57 if(!strcmp(sensor, "fan")) {
58 items = (char **)items_fan;
59 suffix = (char **)suffix_fan;
60 }
61 if(!strcmp(sensor, "pwm")) {
62 items = (char **)items_pwm;
63 suffix = (char **)suffix_pwm;
64 }
65
66
67 if (!items || !suffix)
68 return;
69
70 item = strrchr(fname, '_');
71 if(!item)
72 return;
73
74 if(item)
75 item++;
76
77 if(item > (fname + strlen(fname)))
78 return;
79
80 num = get_num(fname, sensor);
Amit Arorae9e16b02010-08-03 10:15:20 +053081 filep = fopen(filename, "r");
82
83 if(!filep)
84 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +053085 ret = fscanf(filep, "%s", result);
Amit Arorae9e16b02010-08-03 10:15:20 +053086 fclose(filep);
Amit Arora97006e52010-10-28 11:56:08 +053087
Amit Arorae9e16b02010-08-03 10:15:20 +053088 if(ret != 1)
89 goto exit;
90
91 while(strcmp(items[count], "")) {
92 if(!strcmp(items[count], item))
93 printf("\'temp\' %s sensor %s\t\t%d%s\n",
94 num, items[count], atoi(result)/1000,
95 suffix[count]);
96 count++;
97 }
Amit Arorae9e16b02010-08-03 10:15:20 +053098exit:
99 free(num);
100 return;
101}
Amit Arora17552782010-12-02 12:23:14 +0530102
103int read_and_print_sensor_info(int verbose)
104{
105 DIR *dir, *subdir;
106 int len, found = 0;
107 char filename[PATH_MAX], devpath[PATH_MAX];
108 char device[PATH_MAX];
109 struct dirent *item, *subitem;
110
111 sprintf(filename, "%s", "/sys/class/hwmon");
112 dir = opendir(filename);
113 if (!dir)
114 return errno;
115
116 while ((item = readdir(dir))) {
117 if (item->d_name[0] == '.') /* skip the hidden files */
118 continue;
119
120 found = 1;
121
122 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
123 sprintf(devpath, "%s/device", filename);
124
125 len = readlink(devpath, device, PATH_MAX - 1);
126
127 if (len < 0)
128 strcpy(devpath, filename);
129 else
130 device[len] = '\0';
131
132 subdir = opendir(devpath);
133
134 printf("\nSensor Information for %s :\n", item->d_name);
135 fflush(stdin);
136
137 while ((subitem = readdir(subdir))) {
138 if (subitem->d_name[0] == '.') /* skip hidden files */
139 continue;
140
141 if(!strncmp(subitem->d_name, "in", 2))
142 get_sensor_info(devpath, subitem->d_name, "in",
143 verbose);
144 else if (!strncmp(subitem->d_name, "temp", 4))
145 get_sensor_info(devpath, subitem->d_name,
146 "temp", verbose);
147 else if (!strncmp(subitem->d_name, "fan", 4))
148 get_sensor_info(devpath, subitem->d_name,
149 "fan", verbose);
150 else if (!strncmp(subitem->d_name, "pwm", 4))
151 get_sensor_info(devpath, subitem->d_name,
152 "pwm", verbose);
153
154 }
155
156 closedir(subdir);
157 }
158 closedir(dir);
159
160 if(!found && verbose) {
161 printf("Could not find sensor information!");
162 printf(" Looks like /sys/class/hwmon is empty.\n");
163 }
164
165 return 0;
166}