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 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 33 | |
| 34 | void get_sensor_info(char *path, char *fname, char *sensor, int verbose) |
| 35 | { |
| 36 | FILE *filep; |
| 37 | char filename[PATH_MAX]; |
| 38 | char **items = NULL, **suffix = NULL; |
| 39 | char *item, result[NAME_MAX], *num; |
| 40 | int ret, count = 0; |
| 41 | |
| 42 | (void)verbose; // get rid of warning |
| 43 | |
| 44 | sprintf(filename, "%s/%s", path, fname); |
| 45 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 46 | if (!strcmp(sensor, "in")) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 47 | items = (char **)items_in; |
| 48 | suffix = (char **)suffix_in; |
| 49 | } |
| 50 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 51 | if (!strcmp(sensor, "temp")) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 52 | items = (char **)items_temp; |
| 53 | suffix = (char **)suffix_temp; |
| 54 | } |
| 55 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 56 | if (!strcmp(sensor, "fan")) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 57 | items = (char **)items_fan; |
| 58 | suffix = (char **)suffix_fan; |
| 59 | } |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 60 | if (!strcmp(sensor, "pwm")) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 61 | items = (char **)items_pwm; |
| 62 | suffix = (char **)suffix_pwm; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | if (!items || !suffix) |
| 67 | return; |
| 68 | |
| 69 | item = strrchr(fname, '_'); |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 70 | if (!item) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 71 | return; |
| 72 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 73 | if (item) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 74 | item++; |
| 75 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 76 | if (item > (fname + strlen(fname))) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 77 | return; |
| 78 | |
| 79 | num = get_num(fname, sensor); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 80 | filep = fopen(filename, "r"); |
| 81 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 82 | if (!filep) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 83 | goto exit; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 84 | ret = fscanf(filep, "%s", result); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 85 | fclose(filep); |
Amit Arora | 97006e5 | 2010-10-28 11:56:08 +0530 | [diff] [blame] | 86 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 87 | if (ret != 1) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 88 | goto exit; |
| 89 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 90 | while (strcmp(items[count], "")) { |
| 91 | if (!strcmp(items[count], item)) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 92 | printf("\'temp\' %s sensor %s\t\t%d%s\n", |
| 93 | num, items[count], atoi(result)/1000, |
| 94 | suffix[count]); |
| 95 | count++; |
| 96 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 97 | exit: |
| 98 | free(num); |
| 99 | return; |
| 100 | } |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 101 | |
| 102 | int read_and_print_sensor_info(int verbose) |
| 103 | { |
| 104 | DIR *dir, *subdir; |
| 105 | int len, found = 0; |
| 106 | char filename[PATH_MAX], devpath[PATH_MAX]; |
| 107 | char device[PATH_MAX]; |
| 108 | struct dirent *item, *subitem; |
| 109 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 110 | printf("\nSensor Information:\n"); |
| 111 | printf("******************\n"); |
| 112 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 113 | sprintf(filename, "%s", "/sys/class/hwmon"); |
| 114 | dir = opendir(filename); |
| 115 | if (!dir) |
| 116 | return errno; |
| 117 | |
| 118 | while ((item = readdir(dir))) { |
| 119 | if (item->d_name[0] == '.') /* skip the hidden files */ |
| 120 | continue; |
| 121 | |
| 122 | found = 1; |
| 123 | |
| 124 | sprintf(filename, "/sys/class/hwmon/%s", item->d_name); |
| 125 | sprintf(devpath, "%s/device", filename); |
| 126 | |
| 127 | len = readlink(devpath, device, PATH_MAX - 1); |
| 128 | |
| 129 | if (len < 0) |
| 130 | strcpy(devpath, filename); |
| 131 | else |
| 132 | device[len] = '\0'; |
| 133 | |
| 134 | subdir = opendir(devpath); |
| 135 | |
| 136 | printf("\nSensor Information for %s :\n", item->d_name); |
| 137 | fflush(stdin); |
| 138 | |
| 139 | while ((subitem = readdir(subdir))) { |
| 140 | if (subitem->d_name[0] == '.') /* skip hidden files */ |
| 141 | continue; |
| 142 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 143 | if (!strncmp(subitem->d_name, "in", 2)) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 144 | get_sensor_info(devpath, subitem->d_name, "in", |
| 145 | verbose); |
| 146 | else if (!strncmp(subitem->d_name, "temp", 4)) |
| 147 | get_sensor_info(devpath, subitem->d_name, |
| 148 | "temp", verbose); |
| 149 | else if (!strncmp(subitem->d_name, "fan", 4)) |
| 150 | get_sensor_info(devpath, subitem->d_name, |
| 151 | "fan", verbose); |
| 152 | else if (!strncmp(subitem->d_name, "pwm", 4)) |
| 153 | get_sensor_info(devpath, subitem->d_name, |
| 154 | "pwm", verbose); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | closedir(subdir); |
| 159 | } |
| 160 | closedir(dir); |
| 161 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame^] | 162 | if (!found && verbose) { |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 163 | printf("Could not find sensor information!"); |
| 164 | printf(" Looks like /sys/class/hwmon is empty.\n"); |
| 165 | } |
| 166 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 167 | printf("\n"); |
| 168 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 169 | return 0; |
| 170 | } |