Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 1 | #include <getopt.h> |
| 2 | |
| 3 | #include "powerdebug.h" |
| 4 | |
| 5 | |
| 6 | int numregulators; |
| 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 | |
| 118 | |
| 119 | int read_regulator_info(int verbose) |
| 120 | { |
| 121 | FILE *file = NULL; |
| 122 | DIR *regdir, *dir; |
| 123 | int len, count = 0, ret = 0; |
| 124 | char line[1024], filename[1024], *fptr; |
| 125 | struct dirent *item, *ritem; |
| 126 | |
| 127 | regdir = opendir("/sys/class/regulator"); |
| 128 | if (!regdir) |
| 129 | return(1); |
| 130 | while((item = readdir(regdir))) { |
| 131 | if (strlen(item->d_name) < 3) |
| 132 | continue; |
| 133 | |
| 134 | if (strncmp(item->d_name, "regulator", 9)) |
| 135 | continue; |
| 136 | |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame^] | 137 | len = sprintf(filename, "/sys/class/regulator/%s", |
| 138 | item->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 139 | |
| 140 | dir = opendir(filename); |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame^] | 141 | if (!dir) |
Amit Arora | 7ef178c | 2010-08-03 15:57:21 +0530 | [diff] [blame] | 142 | continue; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 143 | |
| 144 | count++; |
| 145 | if (count > numregulators) { |
| 146 | ret = 1; |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame^] | 147 | goto exit; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 148 | } |
| 149 | |
Amit Arora | fccac91 | 2010-08-03 16:15:09 +0530 | [diff] [blame] | 150 | strcpy(regulators_info[count-1].name, item->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 151 | while((ritem = readdir(dir))) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 152 | if (strlen(ritem->d_name) < 3) |
| 153 | continue; |
| 154 | |
| 155 | sprintf(filename + len, "/%s", ritem->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 156 | |
| 157 | file = fopen(filename, "r"); |
| 158 | if (!file) |
| 159 | continue; |
| 160 | |
| 161 | memset(line, 0, 1024); |
| 162 | fptr = fgets(line, 1024, file); |
| 163 | fclose(file); |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame^] | 164 | if (!fptr) |
Amit Arora | 7ef178c | 2010-08-03 15:57:21 +0530 | [diff] [blame] | 165 | continue; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 166 | |
| 167 | if (!strcmp(ritem->d_name, "name")) |
| 168 | strcpy(regulators_info[count-1].name, fptr); |
| 169 | if (!strcmp(ritem->d_name, "state")) |
| 170 | strcpy(regulators_info[count-1].state, fptr); |
| 171 | if (!strcmp(ritem->d_name, "status")) |
| 172 | strcpy(regulators_info[count-1].status, fptr); |
| 173 | |
| 174 | /* Read following _only_ if verbose option specified */ |
| 175 | if(!verbose) |
| 176 | continue; |
| 177 | |
| 178 | if (!strcmp(ritem->d_name, "type")) |
| 179 | strcpy(regulators_info[count-1].type, fptr); |
| 180 | if (!strcmp(ritem->d_name, "opmode")) |
| 181 | strcpy(regulators_info[count-1].opmode, fptr); |
| 182 | |
| 183 | if (!strcmp(ritem->d_name, "microvolts")) |
| 184 | regulators_info[count-1].microvolts = atoi(fptr); |
Amit Arora | 83faf0e | 2010-08-05 12:27:42 +0530 | [diff] [blame] | 185 | if (!strcmp(ritem->d_name, "min_microvolts")) |
| 186 | regulators_info[count-1].min_microvolts = atoi(fptr); |
| 187 | if (!strcmp(ritem->d_name, "max_microvolts")) |
| 188 | regulators_info[count-1].max_microvolts = atoi(fptr); |
| 189 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 190 | if (!strcmp(ritem->d_name, "microamps")) |
| 191 | regulators_info[count-1].microamps = atoi(fptr); |
Amit Arora | 83faf0e | 2010-08-05 12:27:42 +0530 | [diff] [blame] | 192 | if (!strcmp(ritem->d_name, "min_microamps")) |
| 193 | regulators_info[count-1].min_microamps = atoi(fptr); |
| 194 | if (!strcmp(ritem->d_name, "max_microamps")) |
| 195 | regulators_info[count-1].max_microamps = atoi(fptr); |
| 196 | if (!strcmp(ritem->d_name, "requested_microamps")) |
| 197 | regulators_info[count-1].requested_microamps = atoi(fptr); |
| 198 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 199 | if (!strcmp(ritem->d_name, "num_users")) |
| 200 | regulators_info[count-1].num_users = atoi(fptr); |
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; |
| 216 | int regulators = 0, sensors = 0, verbose = 0; |
| 217 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 218 | /* |
| 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 Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 226 | */ |
| 227 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 228 | 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 Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 267 | } |
| 268 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 269 | |
| 270 | /* Need atleast one option specified */ |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 271 | if (!regulators && !sensors) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 272 | 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 | |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame^] | 286 | exit(0); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 287 | } |