blob: f5fcd4013c3d6349cf4a7c5c81a44d4be15fe576 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora39f29542010-09-14 12:03:22 +05303 *
4 * This file is part of PowerDebug.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors:
12 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
13 * - initial API and implementation
14 *******************************************************************************/
15
Amit Arorae9e16b02010-08-03 10:15:20 +053016#include "powerdebug.h"
17#include "sensor.h"
18
19char *get_num(char *fname, char *sensor)
20{
21 char tmpstr[NAME_MAX];
22 char *str;
23
24 strcpy(tmpstr, (fname+strlen(sensor)));
25
26 str = strrchr(tmpstr, '_');
27 str[0] = '\0';
28
29 str = strdup(tmpstr);
30 return str;
31}
Amit Arorae9e16b02010-08-03 10:15:20 +053032
33void get_sensor_info(char *path, char *fname, char *sensor, int verbose)
34{
35 FILE *filep;
36 char filename[PATH_MAX];
37 char **items = NULL, **suffix = NULL;
38 char *item, result[NAME_MAX], *num;
39 int ret, count = 0;
40
41 (void)verbose; // get rid of warning
42
43 sprintf(filename, "%s/%s", path, fname);
44
Amit Kucheriaa0adae42011-01-12 10:54:23 -060045 if (!strcmp(sensor, "in")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053046 items = (char **)items_in;
47 suffix = (char **)suffix_in;
48 }
49
Amit Kucheriaa0adae42011-01-12 10:54:23 -060050 if (!strcmp(sensor, "temp")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053051 items = (char **)items_temp;
52 suffix = (char **)suffix_temp;
53 }
54
Amit Kucheriaa0adae42011-01-12 10:54:23 -060055 if (!strcmp(sensor, "fan")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053056 items = (char **)items_fan;
57 suffix = (char **)suffix_fan;
58 }
Amit Kucheriaa0adae42011-01-12 10:54:23 -060059 if (!strcmp(sensor, "pwm")) {
Amit Arorae9e16b02010-08-03 10:15:20 +053060 items = (char **)items_pwm;
61 suffix = (char **)suffix_pwm;
62 }
63
64
65 if (!items || !suffix)
66 return;
67
68 item = strrchr(fname, '_');
Amit Kucheriaa0adae42011-01-12 10:54:23 -060069 if (!item)
Amit Arorae9e16b02010-08-03 10:15:20 +053070 return;
71
Amit Kucheriaa0adae42011-01-12 10:54:23 -060072 if (item)
Amit Arorae9e16b02010-08-03 10:15:20 +053073 item++;
74
Amit Kucheriaa0adae42011-01-12 10:54:23 -060075 if (item > (fname + strlen(fname)))
Amit Arorae9e16b02010-08-03 10:15:20 +053076 return;
77
78 num = get_num(fname, sensor);
Amit Arorae9e16b02010-08-03 10:15:20 +053079 filep = fopen(filename, "r");
80
Amit Kucheriaa0adae42011-01-12 10:54:23 -060081 if (!filep)
Amit Arorae9e16b02010-08-03 10:15:20 +053082 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +053083 ret = fscanf(filep, "%s", result);
Amit Arorae9e16b02010-08-03 10:15:20 +053084 fclose(filep);
Amit Arora97006e52010-10-28 11:56:08 +053085
Amit Kucheriaa0adae42011-01-12 10:54:23 -060086 if (ret != 1)
Amit Arorae9e16b02010-08-03 10:15:20 +053087 goto exit;
88
Amit Kucheriaa0adae42011-01-12 10:54:23 -060089 while (strcmp(items[count], "")) {
90 if (!strcmp(items[count], item))
Amit Arorae9e16b02010-08-03 10:15:20 +053091 printf("\'temp\' %s sensor %s\t\t%d%s\n",
92 num, items[count], atoi(result)/1000,
93 suffix[count]);
94 count++;
95 }
Amit Arorae9e16b02010-08-03 10:15:20 +053096exit:
97 free(num);
98 return;
99}
Amit Arora17552782010-12-02 12:23:14 +0530100
101int read_and_print_sensor_info(int verbose)
102{
103 DIR *dir, *subdir;
104 int len, found = 0;
105 char filename[PATH_MAX], devpath[PATH_MAX];
106 char device[PATH_MAX];
107 struct dirent *item, *subitem;
108
Amit Arora422c52f2010-12-02 16:22:29 +0530109 printf("\nSensor Information:\n");
110 printf("******************\n");
111
Amit Arora17552782010-12-02 12:23:14 +0530112 sprintf(filename, "%s", "/sys/class/hwmon");
113 dir = opendir(filename);
114 if (!dir)
115 return errno;
116
117 while ((item = readdir(dir))) {
118 if (item->d_name[0] == '.') /* skip the hidden files */
119 continue;
120
121 found = 1;
122
123 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
124 sprintf(devpath, "%s/device", filename);
125
126 len = readlink(devpath, device, PATH_MAX - 1);
127
128 if (len < 0)
129 strcpy(devpath, filename);
130 else
131 device[len] = '\0';
132
133 subdir = opendir(devpath);
134
135 printf("\nSensor Information for %s :\n", item->d_name);
136 fflush(stdin);
137
138 while ((subitem = readdir(subdir))) {
139 if (subitem->d_name[0] == '.') /* skip hidden files */
140 continue;
141
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600142 if (!strncmp(subitem->d_name, "in", 2))
Amit Arora17552782010-12-02 12:23:14 +0530143 get_sensor_info(devpath, subitem->d_name, "in",
144 verbose);
145 else if (!strncmp(subitem->d_name, "temp", 4))
146 get_sensor_info(devpath, subitem->d_name,
147 "temp", verbose);
148 else if (!strncmp(subitem->d_name, "fan", 4))
149 get_sensor_info(devpath, subitem->d_name,
150 "fan", verbose);
151 else if (!strncmp(subitem->d_name, "pwm", 4))
152 get_sensor_info(devpath, subitem->d_name,
153 "pwm", verbose);
154
155 }
156
157 closedir(subdir);
158 }
159 closedir(dir);
160
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600161 if (!found && verbose) {
Amit Arora17552782010-12-02 12:23:14 +0530162 printf("Could not find sensor information!");
163 printf(" Looks like /sys/class/hwmon is empty.\n");
164 }
165
Amit Arora422c52f2010-12-02 16:22:29 +0530166 printf("\n");
167
Amit Arora17552782010-12-02 12:23:14 +0530168 return 0;
169}