blob: 2bd5c691cf63fb322ae77f45ce09376450a3e42d [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
Amit Arorafccac912010-08-03 16:15:09 +0530144 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530145 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530146 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);
Amit Arora83faf0e2010-08-05 12:27:42 +0530184 if (!strcmp(ritem->d_name, "min_microvolts"))
185 regulators_info[count-1].min_microvolts = atoi(fptr);
186 if (!strcmp(ritem->d_name, "max_microvolts"))
187 regulators_info[count-1].max_microvolts = atoi(fptr);
188
Amit Arorae9e16b02010-08-03 10:15:20 +0530189 if (!strcmp(ritem->d_name, "microamps"))
190 regulators_info[count-1].microamps = atoi(fptr);
Amit Arora83faf0e2010-08-05 12:27:42 +0530191 if (!strcmp(ritem->d_name, "min_microamps"))
192 regulators_info[count-1].min_microamps = atoi(fptr);
193 if (!strcmp(ritem->d_name, "max_microamps"))
194 regulators_info[count-1].max_microamps = atoi(fptr);
195 if (!strcmp(ritem->d_name, "requested_microamps"))
196 regulators_info[count-1].requested_microamps = atoi(fptr);
197
Amit Arorae9e16b02010-08-03 10:15:20 +0530198 if (!strcmp(ritem->d_name, "num_users"))
199 regulators_info[count-1].num_users = atoi(fptr);
Amit Arorae9e16b02010-08-03 10:15:20 +0530200 }
201exit1:
202 closedir(dir);
203 if (ret)
204 break;
205 }
Amit Arora7ef178c2010-08-03 15:57:21 +0530206//exit2:
Amit Arorae9e16b02010-08-03 10:15:20 +0530207 closedir(regdir);
208
209 return ret;
210}
211
212
213int main(int argc, char **argv)
214{
215 int c;
216 int regulators = 0, sensors = 0, verbose = 0;
217
Amit Arorafefe8bf2010-08-05 13:31:20 +0530218 /*
219 * Options:
220 * -r, --regulator : regulator
221 * -s, --sensor : sensors
222 * -v, --verbose : verbose
223 * -V, --version : version
224 * -h, --help : help
225 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530226 */
227
Amit Arorafefe8bf2010-08-05 13:31:20 +0530228 while (1) {
229 int optindex = 0;
230 static struct option long_options[] = {
231 {"regulator", 0, 0, 'r'},
232 {"sensor", 0, 0, 's'},
233 {"verbose", 0, 0, 'v'},
234 {"version", 0, 0, 'V'},
235 {"help", 0, 0, 'h'},
236 {0, 0, 0, 0}
237 };
238
239 c = getopt_long(argc, argv, "rsvVh", long_options, &optindex);
240 if (c == -1)
241 break;
242
243 switch (c) {
244 case 'r':
245 regulators = 1;
246 break;
247 case 's':
248 sensors = 1;
249 break;
250 case 'v':
251 verbose = 1;
252 break;
253 case 'V':
254 version();
255 break;
256 case 'h':
257 usage(argv);
258 break;
259 case '?':
260 fprintf (stderr, "%s: Unknown option %c'.\n",
261 argv[0], optopt);
262 exit(1);
263 default:
264 usage(argv);
265 break;
266 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530267 }
268
Amit Arorafefe8bf2010-08-05 13:31:20 +0530269
270 /* Need atleast one option specified */
Amit Arorae9e16b02010-08-03 10:15:20 +0530271 if (!regulators && !sensors) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530272 usage(argv);
273 }
274
275 init_regulator_ds();
276
277 if (regulators) {
278 read_regulator_info(verbose);
279 print_regulator_info(verbose);
280 }
281
282 if (sensors) {
283 read_and_print_sensor_info(verbose);
284 }
285
286 return 0;
287
288}