~ubuntu-branches/ubuntu/oneiric/powerdebug/oneiric

« back to all changes in this revision

Viewing changes to sensor.c

  • Committer: Bazaar Package Importer
  • Author(s): Amit Kucheria
  • Date: 2011-01-24 17:58:46 UTC
  • Revision ID: james.westby@ubuntu.com-20110124175846-bz13uqdo4h0wl3wo
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2010, Linaro Limited.
 
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
 
 
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
}
 
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
 
 
45
        if (!strcmp(sensor, "in")) {
 
46
                items = (char **)items_in;
 
47
                suffix = (char **)suffix_in;
 
48
        }
 
49
 
 
50
        if (!strcmp(sensor, "temp")) {
 
51
                items = (char **)items_temp;
 
52
                suffix = (char **)suffix_temp;
 
53
        }
 
54
 
 
55
        if (!strcmp(sensor, "fan")) {
 
56
                items = (char **)items_fan;
 
57
                suffix = (char **)suffix_fan;
 
58
        }
 
59
        if (!strcmp(sensor, "pwm")) {
 
60
                items = (char **)items_pwm;
 
61
                suffix = (char **)suffix_pwm;
 
62
        }
 
63
 
 
64
 
 
65
        if (!items || !suffix)
 
66
                return;
 
67
 
 
68
        item = strrchr(fname, '_');
 
69
        if (!item)
 
70
                return;
 
71
 
 
72
        if (item)
 
73
                item++;
 
74
 
 
75
        if (item > (fname + strlen(fname)))
 
76
                return;
 
77
 
 
78
        num = get_num(fname, sensor);
 
79
        filep = fopen(filename, "r");
 
80
 
 
81
        if (!filep)
 
82
                goto exit;
 
83
        ret = fscanf(filep, "%s", result);
 
84
        fclose(filep);
 
85
 
 
86
        if (ret != 1)
 
87
                goto exit;
 
88
 
 
89
        while (strcmp(items[count], "")) {
 
90
                if (!strcmp(items[count], item))
 
91
                        printf("\'temp\' %s sensor %s\t\t%d%s\n",
 
92
                                num, items[count], atoi(result)/1000,
 
93
                                suffix[count]);
 
94
                count++;
 
95
        }
 
96
exit:
 
97
        free(num);
 
98
        return;
 
99
}
 
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
 
 
109
        printf("\nSensor Information:\n");
 
110
        printf("******************\n");
 
111
 
 
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
 
 
142
                        if (!strncmp(subitem->d_name, "in", 2))
 
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
 
 
161
        if (!found && verbose) {
 
162
                printf("Could not find sensor information!");
 
163
                printf(" Looks like /sys/class/hwmon is empty.\n");
 
164
        }
 
165
 
 
166
        printf("\n");
 
167
 
 
168
        return 0;
 
169
}