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