Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2010, Linaro |
| 3 | * Copyright (C) 2010, IBM Corporation |
| 4 | * |
| 5 | * This file is part of PowerDebug. |
| 6 | * |
| 7 | * All rights reserved. This program and the accompanying materials |
| 8 | * are made available under the terms of the Eclipse Public License v1.0 |
| 9 | * which accompanies this distribution, and is available at |
| 10 | * http://www.eclipse.org/legal/epl-v10.html |
| 11 | * |
| 12 | * Contributors: |
| 13 | * Amit Arora <amit.arora@linaro.org> (IBM Corporation) |
| 14 | * - initial API and implementation |
| 15 | *******************************************************************************/ |
| 16 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 17 | #include "powerdebug.h" |
| 18 | #include "sensor.h" |
| 19 | |
| 20 | char *get_num(char *fname, char *sensor) |
| 21 | { |
| 22 | char tmpstr[NAME_MAX]; |
| 23 | char *str; |
| 24 | |
| 25 | strcpy(tmpstr, (fname+strlen(sensor))); |
| 26 | |
| 27 | str = strrchr(tmpstr, '_'); |
| 28 | str[0] = '\0'; |
| 29 | |
| 30 | str = strdup(tmpstr); |
| 31 | return str; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | void get_sensor_info(char *path, char *fname, char *sensor, int verbose) |
| 36 | { |
| 37 | FILE *filep; |
| 38 | char filename[PATH_MAX]; |
| 39 | char **items = NULL, **suffix = NULL; |
| 40 | char *item, result[NAME_MAX], *num; |
| 41 | int ret, count = 0; |
| 42 | |
| 43 | (void)verbose; // get rid of warning |
| 44 | |
| 45 | sprintf(filename, "%s/%s", path, fname); |
| 46 | |
| 47 | if(!strcmp(sensor, "in")) { |
| 48 | items = (char **)items_in; |
| 49 | suffix = (char **)suffix_in; |
| 50 | } |
| 51 | |
| 52 | if(!strcmp(sensor, "temp")) { |
| 53 | items = (char **)items_temp; |
| 54 | suffix = (char **)suffix_temp; |
| 55 | } |
| 56 | |
| 57 | if(!strcmp(sensor, "fan")) { |
| 58 | items = (char **)items_fan; |
| 59 | suffix = (char **)suffix_fan; |
| 60 | } |
| 61 | if(!strcmp(sensor, "pwm")) { |
| 62 | items = (char **)items_pwm; |
| 63 | suffix = (char **)suffix_pwm; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | if (!items || !suffix) |
| 68 | return; |
| 69 | |
| 70 | item = strrchr(fname, '_'); |
| 71 | if(!item) |
| 72 | return; |
| 73 | |
| 74 | if(item) |
| 75 | item++; |
| 76 | |
| 77 | if(item > (fname + strlen(fname))) |
| 78 | return; |
| 79 | |
| 80 | num = get_num(fname, sensor); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 81 | filep = fopen(filename, "r"); |
| 82 | |
| 83 | if(!filep) |
| 84 | goto exit; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 85 | ret = fscanf(filep, "%s", result); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 86 | fclose(filep); |
Amit Arora | 97006e5 | 2010-10-28 11:56:08 +0530 | [diff] [blame] | 87 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 88 | if(ret != 1) |
| 89 | goto exit; |
| 90 | |
| 91 | while(strcmp(items[count], "")) { |
| 92 | if(!strcmp(items[count], item)) |
| 93 | printf("\'temp\' %s sensor %s\t\t%d%s\n", |
| 94 | num, items[count], atoi(result)/1000, |
| 95 | suffix[count]); |
| 96 | count++; |
| 97 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 98 | exit: |
| 99 | free(num); |
| 100 | return; |
| 101 | } |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 102 | |
| 103 | int read_and_print_sensor_info(int verbose) |
| 104 | { |
| 105 | DIR *dir, *subdir; |
| 106 | int len, found = 0; |
| 107 | char filename[PATH_MAX], devpath[PATH_MAX]; |
| 108 | char device[PATH_MAX]; |
| 109 | struct dirent *item, *subitem; |
| 110 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 111 | printf("\nSensor Information:\n"); |
| 112 | printf("******************\n"); |
| 113 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 114 | sprintf(filename, "%s", "/sys/class/hwmon"); |
| 115 | dir = opendir(filename); |
| 116 | if (!dir) |
| 117 | return errno; |
| 118 | |
| 119 | while ((item = readdir(dir))) { |
| 120 | if (item->d_name[0] == '.') /* skip the hidden files */ |
| 121 | continue; |
| 122 | |
| 123 | found = 1; |
| 124 | |
| 125 | sprintf(filename, "/sys/class/hwmon/%s", item->d_name); |
| 126 | sprintf(devpath, "%s/device", filename); |
| 127 | |
| 128 | len = readlink(devpath, device, PATH_MAX - 1); |
| 129 | |
| 130 | if (len < 0) |
| 131 | strcpy(devpath, filename); |
| 132 | else |
| 133 | device[len] = '\0'; |
| 134 | |
| 135 | subdir = opendir(devpath); |
| 136 | |
| 137 | printf("\nSensor Information for %s :\n", item->d_name); |
| 138 | fflush(stdin); |
| 139 | |
| 140 | while ((subitem = readdir(subdir))) { |
| 141 | if (subitem->d_name[0] == '.') /* skip hidden files */ |
| 142 | continue; |
| 143 | |
| 144 | if(!strncmp(subitem->d_name, "in", 2)) |
| 145 | get_sensor_info(devpath, subitem->d_name, "in", |
| 146 | verbose); |
| 147 | else if (!strncmp(subitem->d_name, "temp", 4)) |
| 148 | get_sensor_info(devpath, subitem->d_name, |
| 149 | "temp", verbose); |
| 150 | else if (!strncmp(subitem->d_name, "fan", 4)) |
| 151 | get_sensor_info(devpath, subitem->d_name, |
| 152 | "fan", verbose); |
| 153 | else if (!strncmp(subitem->d_name, "pwm", 4)) |
| 154 | get_sensor_info(devpath, subitem->d_name, |
| 155 | "pwm", verbose); |
| 156 | |
| 157 | } |
| 158 | |
| 159 | closedir(subdir); |
| 160 | } |
| 161 | closedir(dir); |
| 162 | |
| 163 | if(!found && verbose) { |
| 164 | printf("Could not find sensor information!"); |
| 165 | printf(" Looks like /sys/class/hwmon is empty.\n"); |
| 166 | } |
| 167 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 168 | printf("\n"); |
| 169 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 170 | return 0; |
| 171 | } |