Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 1 | #include <getopt.h> |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 2 | #include "powerdebug.h" |
| 3 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 4 | int numregulators; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 5 | int dump; |
| 6 | int ticktime=3; /* in seconds */ |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 7 | |
| 8 | int 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) { |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 27 | fprintf(stderr, "init_regulator_ds: Not enough memory to "); |
| 28 | fprintf(stderr, "read information for %d regulators!\n", |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 29 | numregulators); |
| 30 | return(1); |
| 31 | } |
| 32 | |
| 33 | return(0); |
| 34 | } |
| 35 | |
| 36 | int read_and_print_sensor_info(int verbose) |
| 37 | { |
| 38 | DIR *dir, *subdir; |
Amit Arora | 1c03789 | 2010-08-05 13:46:10 +0530 | [diff] [blame] | 39 | int len, found = 0; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 40 | char filename[PATH_MAX], devpath[PATH_MAX]; |
| 41 | char device[PATH_MAX]; |
| 42 | struct dirent *item, *subitem; |
| 43 | |
| 44 | /* |
| 45 | 1. readdir /sys/class/hwmon |
| 46 | 2. foreach subdir in above, do following |
| 47 | a) check if its virtual or physical (check for device) |
| 48 | b) for each *type* (in, temp, pwm, fan, curr, power, energy) |
| 49 | i> search for "_" (strrchr... check for last) |
| 50 | ii> check the *item* depending on type |
| 51 | i.e for "in" type items=min, max, input, label |
| 52 | for "fan" type items=min,max,input,div,target,label |
| 53 | for "pwm" type items=enable, mode, freq |
| 54 | for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest, |
| 55 | for "curr" type items=max,min,input |
| 56 | for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max |
| 57 | for "energy" type items=input |
| 58 | for "intrusion" type items=alarm,beep |
| 59 | */ |
| 60 | |
| 61 | sprintf(filename, "%s", "/sys/class/hwmon"); |
| 62 | |
| 63 | dir = opendir(filename); |
| 64 | if (!dir) |
| 65 | return errno; |
| 66 | |
| 67 | while ((item = readdir(dir))) { |
| 68 | if (item->d_name[0] == '.') /* skip the hidden files */ |
| 69 | continue; |
| 70 | |
Amit Arora | 1c03789 | 2010-08-05 13:46:10 +0530 | [diff] [blame] | 71 | found = 1; |
| 72 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 73 | sprintf(filename, "/sys/class/hwmon/%s", item->d_name); |
| 74 | sprintf(devpath, "%s/device", filename); |
| 75 | |
| 76 | len = readlink(devpath, device, PATH_MAX - 1); |
| 77 | |
| 78 | if (len < 0) |
| 79 | strcpy(devpath, filename); |
| 80 | else |
| 81 | device[len] = '\0'; |
| 82 | |
| 83 | subdir = opendir(devpath); |
| 84 | |
| 85 | printf("\nSensor Information for %s :\n", item->d_name); |
| 86 | fflush(stdin); |
| 87 | while ((subitem = readdir(subdir))) { |
| 88 | if (subitem->d_name[0] == '.') /* skip hidden files */ |
| 89 | continue; |
| 90 | |
| 91 | if(!strncmp(subitem->d_name, "in", 2)) |
| 92 | get_sensor_info(devpath, subitem->d_name, "in", |
| 93 | verbose); |
| 94 | else if (!strncmp(subitem->d_name, "temp", 4)) |
| 95 | get_sensor_info(devpath, subitem->d_name, |
| 96 | "temp", verbose); |
| 97 | else if (!strncmp(subitem->d_name, "fan", 4)) |
| 98 | get_sensor_info(devpath, subitem->d_name, |
| 99 | "fan", verbose); |
| 100 | else if (!strncmp(subitem->d_name, "pwm", 4)) |
| 101 | get_sensor_info(devpath, subitem->d_name, |
| 102 | "pwm", verbose); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | closedir(subdir); |
| 107 | } |
| 108 | closedir(dir); |
| 109 | |
Amit Arora | 1c03789 | 2010-08-05 13:46:10 +0530 | [diff] [blame] | 110 | if(!found && verbose) { |
| 111 | printf("Could not find sensor information!"); |
| 112 | printf(" Looks like /sys/class/hwmon is empty.\n"); |
| 113 | } |
| 114 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 118 | void read_info_from_dirent(struct dirent *ritem, char *str, int idx) |
| 119 | { |
| 120 | if (!strcmp(ritem->d_name, "name")) |
| 121 | strcpy(regulators_info[idx].name, str); |
| 122 | if (!strcmp(ritem->d_name, "state")) |
| 123 | strcpy(regulators_info[idx].state, str); |
| 124 | if (!strcmp(ritem->d_name, "status")) |
| 125 | strcpy(regulators_info[idx].status, str); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 126 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 127 | if (!strcmp(ritem->d_name, "type")) |
| 128 | strcpy(regulators_info[idx].type, str); |
| 129 | if (!strcmp(ritem->d_name, "opmode")) |
| 130 | strcpy(regulators_info[idx].opmode, str); |
| 131 | |
| 132 | if (!strcmp(ritem->d_name, "microvolts")) |
| 133 | regulators_info[idx].microvolts = atoi(str); |
| 134 | if (!strcmp(ritem->d_name, "min_microvolts")) |
| 135 | regulators_info[idx].min_microvolts = atoi(str); |
| 136 | if (!strcmp(ritem->d_name, "max_microvolts")) |
| 137 | regulators_info[idx].max_microvolts = atoi(str); |
| 138 | |
| 139 | if (!strcmp(ritem->d_name, "microamps")) |
| 140 | regulators_info[idx].microamps = atoi(str); |
| 141 | if (!strcmp(ritem->d_name, "min_microamps")) |
| 142 | regulators_info[idx].min_microamps = atoi(str); |
| 143 | if (!strcmp(ritem->d_name, "max_microamps")) |
| 144 | regulators_info[idx].max_microamps = atoi(str); |
| 145 | if (!strcmp(ritem->d_name, "requested_microamps")) |
| 146 | regulators_info[idx].requested_microamps = atoi(str); |
| 147 | |
| 148 | if (!strcmp(ritem->d_name, "num_users")) |
| 149 | regulators_info[idx].num_users = atoi(str); |
| 150 | } |
| 151 | |
| 152 | int read_regulator_info(void) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 153 | { |
| 154 | FILE *file = NULL; |
| 155 | DIR *regdir, *dir; |
| 156 | int len, count = 0, ret = 0; |
| 157 | char line[1024], filename[1024], *fptr; |
| 158 | struct dirent *item, *ritem; |
| 159 | |
| 160 | regdir = opendir("/sys/class/regulator"); |
| 161 | if (!regdir) |
| 162 | return(1); |
| 163 | while((item = readdir(regdir))) { |
| 164 | if (strlen(item->d_name) < 3) |
| 165 | continue; |
| 166 | |
| 167 | if (strncmp(item->d_name, "regulator", 9)) |
| 168 | continue; |
| 169 | |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 170 | len = sprintf(filename, "/sys/class/regulator/%s", |
| 171 | item->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 172 | |
| 173 | dir = opendir(filename); |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 174 | if (!dir) |
Amit Arora | 7ef178c | 2010-08-03 15:57:21 +0530 | [diff] [blame] | 175 | continue; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 176 | |
| 177 | count++; |
| 178 | if (count > numregulators) { |
| 179 | ret = 1; |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 180 | goto exit; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 181 | } |
| 182 | |
Amit Arora | fccac91 | 2010-08-03 16:15:09 +0530 | [diff] [blame] | 183 | strcpy(regulators_info[count-1].name, item->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 184 | while((ritem = readdir(dir))) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 185 | if (strlen(ritem->d_name) < 3) |
| 186 | continue; |
| 187 | |
| 188 | sprintf(filename + len, "/%s", ritem->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 189 | |
| 190 | file = fopen(filename, "r"); |
| 191 | if (!file) |
| 192 | continue; |
| 193 | |
| 194 | memset(line, 0, 1024); |
| 195 | fptr = fgets(line, 1024, file); |
| 196 | fclose(file); |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 197 | if (!fptr) |
Amit Arora | 7ef178c | 2010-08-03 15:57:21 +0530 | [diff] [blame] | 198 | continue; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 199 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 200 | read_info_from_dirent(ritem, fptr, count - 1); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 201 | } |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 202 | exit: |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 203 | closedir(dir); |
| 204 | if (ret) |
| 205 | break; |
| 206 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 207 | closedir(regdir); |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | int main(int argc, char **argv) |
| 214 | { |
| 215 | int c; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 216 | int firsttime = 1; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 217 | int regulators = 0, sensors = 0, verbose = 0; |
| 218 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 219 | /* |
| 220 | * Options: |
| 221 | * -r, --regulator : regulator |
| 222 | * -s, --sensor : sensors |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 223 | * -d, --dump : dump |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 224 | * -v, --verbose : verbose |
| 225 | * -V, --version : version |
| 226 | * -h, --help : help |
| 227 | * no option / default : show usage! |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 228 | */ |
| 229 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 230 | while (1) { |
| 231 | int optindex = 0; |
| 232 | static struct option long_options[] = { |
| 233 | {"regulator", 0, 0, 'r'}, |
| 234 | {"sensor", 0, 0, 's'}, |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 235 | {"dump", 0, 0, 'd'}, |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 236 | {"verbose", 0, 0, 'v'}, |
| 237 | {"version", 0, 0, 'V'}, |
| 238 | {"help", 0, 0, 'h'}, |
| 239 | {0, 0, 0, 0} |
| 240 | }; |
| 241 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 242 | c = getopt_long(argc, argv, "rsdvVh", long_options, &optindex); |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 243 | if (c == -1) |
| 244 | break; |
| 245 | |
| 246 | switch (c) { |
| 247 | case 'r': |
| 248 | regulators = 1; |
| 249 | break; |
| 250 | case 's': |
| 251 | sensors = 1; |
| 252 | break; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 253 | case 'd': |
| 254 | dump = 1; |
| 255 | break; |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 256 | case 'v': |
| 257 | verbose = 1; |
| 258 | break; |
| 259 | case 'V': |
| 260 | version(); |
| 261 | break; |
| 262 | case 'h': |
| 263 | usage(argv); |
| 264 | break; |
| 265 | case '?': |
| 266 | fprintf (stderr, "%s: Unknown option %c'.\n", |
| 267 | argv[0], optopt); |
| 268 | exit(1); |
| 269 | default: |
| 270 | usage(argv); |
| 271 | break; |
| 272 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 273 | } |
| 274 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 275 | |
| 276 | /* Need atleast one option specified */ |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 277 | if (!regulators && !sensors) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 278 | usage(argv); |
| 279 | } |
| 280 | |
| 281 | init_regulator_ds(); |
| 282 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 283 | while(1) { |
| 284 | int key = 0; |
| 285 | struct timeval tval; |
| 286 | fd_set readfds; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 287 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame^] | 288 | if (!dump) { |
| 289 | if(firsttime) { |
| 290 | init_curses(); |
| 291 | firsttime = 0; |
| 292 | } |
| 293 | create_windows(); |
| 294 | show_header(); |
| 295 | } |
| 296 | |
| 297 | if (regulators) { |
| 298 | read_regulator_info(); |
| 299 | if (!dump) |
| 300 | show_regulator_info(verbose); |
| 301 | else |
| 302 | print_regulator_info(verbose); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | if (sensors) { |
| 307 | read_and_print_sensor_info(verbose); |
| 308 | } |
| 309 | |
| 310 | if (dump) |
| 311 | break; |
| 312 | |
| 313 | FD_ZERO(&readfds); |
| 314 | FD_SET(0, &readfds); |
| 315 | tval.tv_sec = ticktime; |
| 316 | tval.tv_usec = (ticktime - tval.tv_sec) * 1000000; |
| 317 | |
| 318 | key = select(1, &readfds, NULL, NULL, &tval); |
| 319 | |
| 320 | if (key) { |
| 321 | char keychar; |
| 322 | |
| 323 | int keystroke = fgetc(stdin); |
| 324 | if (keystroke == EOF) |
| 325 | exit(0); |
| 326 | |
| 327 | keychar = toupper(keystroke); |
| 328 | if (keychar == 'Q') |
| 329 | exit(0); |
| 330 | if (keychar == 'R') |
| 331 | ticktime = 3; |
| 332 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 333 | } |
| 334 | |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 335 | exit(0); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 336 | } |