aboutsummaryrefslogtreecommitdiff
path: root/powerdebug.c
blob: dcf1a596154d0dc13fae1665ba62432c356827d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <getopt.h>

#include "powerdebug.h"


int numregulators;

int init_regulator_ds(void)
{
	DIR *regdir;
	struct dirent *item;

	regdir = opendir("/sys/class/regulator");
	if (!regdir)
		return(1);
	while((item = readdir(regdir))) {
		if (strncmp(item->d_name, "regulator", 9))
			continue;

		numregulators++;
	}
	closedir(regdir);

	regulators_info = (struct regulator_info *)malloc(numregulators*
						sizeof(struct regulator_info));
	if (!regulators_info) {
		fprintf(stderr, "init_regulator_ds: Not enough memory to read information for %d regulators!\n",
			numregulators);
		return(1);
	}

	return(0);	
}

int read_and_print_sensor_info(int verbose)
{
	DIR *dir, *subdir;
	int len;
	char filename[PATH_MAX], devpath[PATH_MAX];
	char device[PATH_MAX];
	struct dirent *item, *subitem;

/* 
	1. readdir /sys/class/hwmon
	2. foreach subdir in above, do following
	  a) check if its virtual or physical (check for device)
	  b) for each *type* (in, temp, pwm, fan, curr, power, energy)
		i> search for "_" (strrchr... check for last)
		ii> check the *item* depending on type
		    i.e for "in" type items=min, max, input, label
		 	for "fan" type items=min,max,input,div,target,label
			for "pwm" type items=enable, mode, freq
			for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
			for "curr" type items=max,min,input
			for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
			for "energy" type items=input
			for "intrusion" type items=alarm,beep
*/

	sprintf(filename, "%s", "/sys/class/hwmon");

	dir = opendir(filename);
	if (!dir)
		return errno;

	while ((item = readdir(dir))) {
		if (item->d_name[0] == '.')  /* skip the hidden files */
			continue;

		sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
		sprintf(devpath, "%s/device", filename);

		len = readlink(devpath, device, PATH_MAX - 1);

		if (len < 0)
			strcpy(devpath, filename);
		else
			device[len] = '\0';

		subdir = opendir(devpath);

		printf("\nSensor Information for %s :\n", item->d_name);
		fflush(stdin);
		while ((subitem = readdir(subdir))) {
			if (subitem->d_name[0] == '.') /* skip hidden files */
				continue;

			if(!strncmp(subitem->d_name, "in", 2))
				get_sensor_info(devpath, subitem->d_name, "in",
						verbose);
			else if (!strncmp(subitem->d_name, "temp", 4))
				get_sensor_info(devpath, subitem->d_name,
						"temp", verbose);
			else if (!strncmp(subitem->d_name, "fan", 4))
				get_sensor_info(devpath, subitem->d_name,
						"fan", verbose);
			else if (!strncmp(subitem->d_name, "pwm", 4))
				get_sensor_info(devpath, subitem->d_name,
						"pwm", verbose);

		}

		closedir(subdir);
	}
	closedir(dir);

	return 0;
}


int read_regulator_info(int verbose)
{
	FILE *file = NULL;
	DIR *regdir, *dir;
	int len, count = 0, ret = 0;
	char line[1024], filename[1024], *fptr;
	struct dirent *item, *ritem;

	regdir = opendir("/sys/class/regulator");
	if (!regdir)
		return(1);
	while((item = readdir(regdir))) {
		if (strlen(item->d_name) < 3)
			continue;

		if (strncmp(item->d_name, "regulator", 9))
			continue;

		len = sprintf(filename, "/sys/class/regulator/%s",item->d_name);

		dir = opendir(filename);
		if (!dir) {
			ret = 1;
			goto exit2;
		}

		count++;
		if (count > numregulators) {
			ret = 1;
			goto exit1;
		}

		while((ritem = readdir(dir))) {
			strcpy(regulators_info[count-1].name, item->d_name);
			if (strlen(ritem->d_name) < 3)
				continue;

			sprintf(filename + len, "/%s", ritem->d_name);
			// printf("Inside %s directory. Opening file %s!\n", item->d_name, ritem->d_name);

			file = fopen(filename, "r");
			if (!file)
				continue;

			memset(line, 0, 1024);
			fptr = fgets(line, 1024, file);
			fclose(file);
			if (!fptr) {
				ret = 1;
				goto exit1;
			}
			// printf("Read file %s, data=%s, count = %d\n", filename, fptr, count);

			if (!strcmp(ritem->d_name, "name"))
				strcpy(regulators_info[count-1].name, fptr);
			if (!strcmp(ritem->d_name, "state"))
				strcpy(regulators_info[count-1].state, fptr);
			if (!strcmp(ritem->d_name, "status"))
				strcpy(regulators_info[count-1].status, fptr);

			/* Read following _only_ if verbose option specified */
			if(!verbose)
				continue;

			if (!strcmp(ritem->d_name, "type"))
				strcpy(regulators_info[count-1].type, fptr);
			if (!strcmp(ritem->d_name, "opmode"))
				strcpy(regulators_info[count-1].opmode, fptr);

			if (!strcmp(ritem->d_name, "microvolts"))
				regulators_info[count-1].microvolts = atoi(fptr);
			if (!strcmp(ritem->d_name, "microamps"))
				regulators_info[count-1].microamps = atoi(fptr);
			if (!strcmp(ritem->d_name, "num_users"))
				regulators_info[count-1].num_users = atoi(fptr);

		}
exit1:
		closedir(dir);
		if (ret)
			break;
	}	
exit2:
	closedir(regdir);

	return ret;
}


int main(int argc, char **argv)
{
	int c;
	int regulators = 0, sensors = 0, verbose = 0;

	/* Options:
	 * -r : regulator
	 * -s : sensors
	 * -v : verbose
	 * no option / default : all
	 */

	while ((c = getopt (argc, argv, "rsvh")) != -1)
	switch (c)
	{
	case 'r':
		regulators = 1;
		break;
	case 's':
		sensors = 1;
		break;
	case 'v':
		verbose = 1;
		break;
	case 'h':
		usage (argv);
	case '?':
		fprintf (stderr, "Unknown option %c'.\n", optopt);
		return 1;
	default:
		usage(argv);
	}

	/* By default print both regulator and sensor information */
	if (!regulators && !sensors) {
		/*  What should be the default behavior ?
		regulators = 1;
		sensors = 1;
		*/
		usage(argv);
	}

	init_regulator_ds();

	if (regulators) {
		read_regulator_info(verbose);
		print_regulator_info(verbose);
	}

	if (sensors) {
		read_and_print_sensor_info(verbose);
	}

	return 0;

}