blob: 9b2d444edb652bb1406640b4f46340390438bb78 [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}
Amit Arorae9e16b02010-08-03 10:15:20 +053033
34void get_sensor_info(char *path, char *fname, char *sensor, int verbose)
35{
36 FILE *filep;
37 char filename[PATH_MAX];
38 char **items = NULL, **suffix = NULL;
39 char *item, result[NAME_MAX], *num;
40 int ret, count = 0;
41
42 (void)verbose; // get rid of warning
43
44 sprintf(filename, "%s/%s", path, fname);
45
Amit Kucheriaa0adae42011-01-12 10:54:23 -060046 if (!strcmp(sensor, "in")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053047 items = (char **)items_in;
48 suffix = (char **)suffix_in;
49 }
50
Amit Kucheriaa0adae42011-01-12 10:54:23 -060051 if (!strcmp(sensor, "temp")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053052 items = (char **)items_temp;
53 suffix = (char **)suffix_temp;
54 }
55
Amit Kucheriaa0adae42011-01-12 10:54:23 -060056 if (!strcmp(sensor, "fan")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053057 items = (char **)items_fan;
58 suffix = (char **)suffix_fan;
59 }
Amit Kucheriaa0adae42011-01-12 10:54:23 -060060 if (!strcmp(sensor, "pwm")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053061 items = (char **)items_pwm;
62 suffix = (char **)suffix_pwm;
63 }
64
65
66 if (!items || !suffix)
67 return;
68
69 item = strrchr(fname, '_');
Amit Kucheriaa0adae42011-01-12 10:54:23 -060070 if (!item)
Amit Arorae9e16b02010-08-03 10:15:20 +053071 return;
72
Amit Kucheriaa0adae42011-01-12 10:54:23 -060073 if (item)
Amit Arorae9e16b02010-08-03 10:15:20 +053074 item++;
75
Amit Kucheriaa0adae42011-01-12 10:54:23 -060076 if (item > (fname + strlen(fname)))
Amit Arorae9e16b02010-08-03 10:15:20 +053077 return;
78
79 num = get_num(fname, sensor);
Amit Arorae9e16b02010-08-03 10:15:20 +053080 filep = fopen(filename, "r");
81
Amit Kucheriaa0adae42011-01-12 10:54:23 -060082 if (!filep)
Amit Arorae9e16b02010-08-03 10:15:20 +053083 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +053084 ret = fscanf(filep, "%s", result);
Amit Arorae9e16b02010-08-03 10:15:20 +053085 fclose(filep);
Amit Arora97006e52010-10-28 11:56:08 +053086
Amit Kucheriaa0adae42011-01-12 10:54:23 -060087 if (ret != 1)
Amit Arorae9e16b02010-08-03 10:15:20 +053088 goto exit;
89
Amit Kucheriaa0adae42011-01-12 10:54:23 -060090 while (strcmp(items[count], "")) {
91 if (!strcmp(items[count], item))
Amit Arorae9e16b02010-08-03 10:15:20 +053092 printf("\'temp\' %s sensor %s\t\t%d%s\n",
93 num, items[count], atoi(result)/1000,
94 suffix[count]);
95 count++;
96 }
Amit Arorae9e16b02010-08-03 10:15:20 +053097exit:
98 free(num);
99 return;
100}
Amit Arora17552782010-12-02 12:23:14 +0530101
102int read_and_print_sensor_info(int verbose)
103{
104 DIR *dir, *subdir;
105 int len, found = 0;
106 char filename[PATH_MAX], devpath[PATH_MAX];
107 char device[PATH_MAX];
108 struct dirent *item, *subitem;
109
Amit Arora422c52f2010-12-02 16:22:29 +0530110 printf("\nSensor Information:\n");
111 printf("******************\n");
112
Amit Arora17552782010-12-02 12:23:14 +0530113 sprintf(filename, "%s", "/sys/class/hwmon");
114 dir = opendir(filename);
115 if (!dir)
116 return errno;
117
118 while ((item = readdir(dir))) {
119 if (item->d_name[0] == '.') /* skip the hidden files */
120 continue;
121
122 found = 1;
123
124 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
125 sprintf(devpath, "%s/device", filename);
126
127 len = readlink(devpath, device, PATH_MAX - 1);
128
129 if (len < 0)
130 strcpy(devpath, filename);
131 else
132 device[len] = '\0';
133
134 subdir = opendir(devpath);
135
136 printf("\nSensor Information for %s :\n", item->d_name);
137 fflush(stdin);
138
139 while ((subitem = readdir(subdir))) {
140 if (subitem->d_name[0] == '.') /* skip hidden files */
141 continue;
142
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600143 if (!strncmp(subitem->d_name, "in", 2))
Amit Arora17552782010-12-02 12:23:14 +0530144 get_sensor_info(devpath, subitem->d_name, "in",
145 verbose);
146 else if (!strncmp(subitem->d_name, "temp", 4))
147 get_sensor_info(devpath, subitem->d_name,
148 "temp", verbose);
149 else if (!strncmp(subitem->d_name, "fan", 4))
150 get_sensor_info(devpath, subitem->d_name,
151 "fan", verbose);
152 else if (!strncmp(subitem->d_name, "pwm", 4))
153 get_sensor_info(devpath, subitem->d_name,
154 "pwm", verbose);
155
156 }
157
158 closedir(subdir);
159 }
160 closedir(dir);
161
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600162 if (!found && verbose) {
Amit Arora17552782010-12-02 12:23:14 +0530163 printf("Could not find sensor information!");
164 printf(" Looks like /sys/class/hwmon is empty.\n");
165 }
166
Amit Arora422c52f2010-12-02 16:22:29 +0530167 printf("\n");
168
Amit Arora17552782010-12-02 12:23:14 +0530169 return 0;
170}