blob: d0e0e46d1afd8629498e8fa418490de282edb0fc [file] [log] [blame]
Amit Arorae9e16b02010-08-03 10:15:20 +05301#include <getopt.h>
2
3#include "powerdebug.h"
4
5
6int numregulators;
7
8int init_regulator_ds(void)
9{
10 DIR *regdir;
11 struct dirent *item;
12
13 regdir = opendir("/sys/class/regulator");
14 if (!regdir)
15 return(1);
16 while((item = readdir(regdir))) {
17 if (strncmp(item->d_name, "regulator", 9))
18 continue;
19
20 numregulators++;
21 }
22 closedir(regdir);
23
24 regulators_info = (struct regulator_info *)malloc(numregulators*
25 sizeof(struct regulator_info));
26 if (!regulators_info) {
27 fprintf(stderr, "init_regulator_ds: Not enough memory to read information for %d regulators!\n",
28 numregulators);
29 return(1);
30 }
31
32 return(0);
33}
34
35int read_and_print_sensor_info(int verbose)
36{
37 DIR *dir, *subdir;
38 int len;
39 char filename[PATH_MAX], devpath[PATH_MAX];
40 char device[PATH_MAX];
41 struct dirent *item, *subitem;
42
43/*
44 1. readdir /sys/class/hwmon
45 2. foreach subdir in above, do following
46 a) check if its virtual or physical (check for device)
47 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
48 i> search for "_" (strrchr... check for last)
49 ii> check the *item* depending on type
50 i.e for "in" type items=min, max, input, label
51 for "fan" type items=min,max,input,div,target,label
52 for "pwm" type items=enable, mode, freq
53 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
54 for "curr" type items=max,min,input
55 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
56 for "energy" type items=input
57 for "intrusion" type items=alarm,beep
58*/
59
60 sprintf(filename, "%s", "/sys/class/hwmon");
61
62 dir = opendir(filename);
63 if (!dir)
64 return errno;
65
66 while ((item = readdir(dir))) {
67 if (item->d_name[0] == '.') /* skip the hidden files */
68 continue;
69
70 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
71 sprintf(devpath, "%s/device", filename);
72
73 len = readlink(devpath, device, PATH_MAX - 1);
74
75 if (len < 0)
76 strcpy(devpath, filename);
77 else
78 device[len] = '\0';
79
80 subdir = opendir(devpath);
81
82 printf("\nSensor Information for %s :\n", item->d_name);
83 fflush(stdin);
84 while ((subitem = readdir(subdir))) {
85 if (subitem->d_name[0] == '.') /* skip hidden files */
86 continue;
87
88 if(!strncmp(subitem->d_name, "in", 2))
89 get_sensor_info(devpath, subitem->d_name, "in",
90 verbose);
91 else if (!strncmp(subitem->d_name, "temp", 4))
92 get_sensor_info(devpath, subitem->d_name,
93 "temp", verbose);
94 else if (!strncmp(subitem->d_name, "fan", 4))
95 get_sensor_info(devpath, subitem->d_name,
96 "fan", verbose);
97 else if (!strncmp(subitem->d_name, "pwm", 4))
98 get_sensor_info(devpath, subitem->d_name,
99 "pwm", verbose);
100
101 }
102
103 closedir(subdir);
104 }
105 closedir(dir);
106
107 return 0;
108}
109
110
111int read_regulator_info(int verbose)
112{
113 FILE *file = NULL;
114 DIR *regdir, *dir;
115 int len, count = 0, ret = 0;
116 char line[1024], filename[1024], *fptr;
117 struct dirent *item, *ritem;
118
119 regdir = opendir("/sys/class/regulator");
120 if (!regdir)
121 return(1);
122 while((item = readdir(regdir))) {
123 if (strlen(item->d_name) < 3)
124 continue;
125
126 if (strncmp(item->d_name, "regulator", 9))
127 continue;
128
129 len = sprintf(filename, "/sys/class/regulator/%s",item->d_name);
130
131 dir = opendir(filename);
132 if (!dir) {
Amit Arora7ef178c2010-08-03 15:57:21 +0530133 //ret = 1;
134 //goto exit2;
135 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530136 }
137
138 count++;
139 if (count > numregulators) {
140 ret = 1;
141 goto exit1;
142 }
143
144 while((ritem = readdir(dir))) {
145 strcpy(regulators_info[count-1].name, item->d_name);
146 if (strlen(ritem->d_name) < 3)
147 continue;
148
149 sprintf(filename + len, "/%s", ritem->d_name);
150 // printf("Inside %s directory. Opening file %s!\n", item->d_name, ritem->d_name);
151
152 file = fopen(filename, "r");
153 if (!file)
154 continue;
155
156 memset(line, 0, 1024);
157 fptr = fgets(line, 1024, file);
158 fclose(file);
159 if (!fptr) {
Amit Arora7ef178c2010-08-03 15:57:21 +0530160 //ret = 1;
161 //goto exit1;
162 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530163 }
164 // printf("Read file %s, data=%s, count = %d\n", filename, fptr, count);
165
166 if (!strcmp(ritem->d_name, "name"))
167 strcpy(regulators_info[count-1].name, fptr);
168 if (!strcmp(ritem->d_name, "state"))
169 strcpy(regulators_info[count-1].state, fptr);
170 if (!strcmp(ritem->d_name, "status"))
171 strcpy(regulators_info[count-1].status, fptr);
172
173 /* Read following _only_ if verbose option specified */
174 if(!verbose)
175 continue;
176
177 if (!strcmp(ritem->d_name, "type"))
178 strcpy(regulators_info[count-1].type, fptr);
179 if (!strcmp(ritem->d_name, "opmode"))
180 strcpy(regulators_info[count-1].opmode, fptr);
181
182 if (!strcmp(ritem->d_name, "microvolts"))
183 regulators_info[count-1].microvolts = atoi(fptr);
184 if (!strcmp(ritem->d_name, "microamps"))
185 regulators_info[count-1].microamps = atoi(fptr);
186 if (!strcmp(ritem->d_name, "num_users"))
187 regulators_info[count-1].num_users = atoi(fptr);
188
189 }
190exit1:
191 closedir(dir);
192 if (ret)
193 break;
194 }
Amit Arora7ef178c2010-08-03 15:57:21 +0530195//exit2:
Amit Arorae9e16b02010-08-03 10:15:20 +0530196 closedir(regdir);
197
198 return ret;
199}
200
201
202int main(int argc, char **argv)
203{
204 int c;
205 int regulators = 0, sensors = 0, verbose = 0;
206
207 /* Options:
208 * -r : regulator
209 * -s : sensors
210 * -v : verbose
211 * no option / default : all
212 */
213
214 while ((c = getopt (argc, argv, "rsvh")) != -1)
215 switch (c)
216 {
217 case 'r':
218 regulators = 1;
219 break;
220 case 's':
221 sensors = 1;
222 break;
223 case 'v':
224 verbose = 1;
225 break;
226 case 'h':
227 usage (argv);
228 case '?':
229 fprintf (stderr, "Unknown option %c'.\n", optopt);
230 return 1;
231 default:
232 usage(argv);
233 }
234
235 /* By default print both regulator and sensor information */
236 if (!regulators && !sensors) {
237 /* What should be the default behavior ?
238 regulators = 1;
239 sensors = 1;
240 */
241 usage(argv);
242 }
243
244 init_regulator_ds();
245
246 if (regulators) {
247 read_regulator_info(verbose);
248 print_regulator_info(verbose);
249 }
250
251 if (sensors) {
252 read_and_print_sensor_info(verbose);
253 }
254
255 return 0;
256
257}