File: collect_data.c

package info (click to toggle)
gkrellm-thinkbat 0.2.2-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 92 kB
  • ctags: 67
  • sloc: ansic: 336; makefile: 68
file content (134 lines) | stat: -rw-r--r-- 3,458 bytes parent folder | download
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
/* collect_data.c - gkrellm-ThinkBat a Thinkpad battery monitor for Gkrellm2
 *
 * Copyright (C) 2006 Rasto Sramek <rasto@ksp.sk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <fcntl.h>
#include <math.h>

#define BUF_LEN 32
#define FILE_LEN 128

#define SMAPI_PATH "/sys/devices/platform/smapi/"
#define BATTERY "BAT0/"
#define DESIGN_CAPACITY "design_capacity"
#define REMAINING_CAPACITY "remaining_capacity"
#define POWER_AVG "power_avg"
#define POWER_NOW "power_now"
#define LAST_FULL "last_full_capacity"
#define AC "ac_connected"

static int design, remaining, last_full, power_avg, power_now, ac;
//int N = 0;

// Read all data (so we don't do it more times each tick
void read_battery_data()
{
	char file[FILE_LEN];

	sprintf(file, "%s%s%s", SMAPI_PATH, BATTERY, DESIGN_CAPACITY);
	design = read_from_file(file);

	sprintf(file, "%s%s%s", SMAPI_PATH, BATTERY, REMAINING_CAPACITY);
	remaining = read_from_file(file);

	sprintf(file, "%s%s%s", SMAPI_PATH, BATTERY, POWER_NOW);
	power_now = read_from_file(file);
	
	sprintf(file, "%s%s%s", SMAPI_PATH, BATTERY, POWER_AVG);
	power_avg = read_from_file(file);
	
	sprintf(file, "%s%s%s", SMAPI_PATH, BATTERY, LAST_FULL);
	last_full = read_from_file(file);
	
	sprintf(file, "%s%s", SMAPI_PATH, AC);
	ac = read_from_file(file);
/*
	//DEBUG
	fprintf(stderr, "%d %d\n", N, remaining);
	N++;*/
}

// Remaining capacity, last full capacity, divide them
int get_percent_remaining()
{
	return(floor(((double)remaining/(double)last_full)*100));
}

int get_ac_power()
{
	return(ac);
}

// type == 0 - avg, type==1 - now
float get_power_change(int type)
{
	if(type)
		return(fabs((double)power_now/1000.0));
	else
		return(fabs((double)power_avg/1000.0));
}

// Read design capacity, remaining capacity, loading/unloading power, divide. Check loading/unloading
// type == 0 - avg, type==1 - now
int get_time_remaining(int type)
{
	double hours;
	if(type) {
		if(power_now < 0) {	// unloading
			hours = (double)remaining/(double)abs(power_now);
		} else if(power_now > 0) { 		// loading
			hours = (double)(last_full-remaining)/(double)power_now;
		} else {
			hours = 0;
		}
	} else {
		if(power_avg < 0) {	// unloading
			hours = (double)remaining/(double)abs(power_avg);
		} else if(power_avg > 0) { 		// loading
			hours = (double)(last_full-remaining)/(double)power_avg;
		} else {
			hours = 0;
		}
	}
	hours *= 60;
	return(floor(hours));
}

int read_from_file(char *file)
{
	int fd, data;
	char buff[BUF_LEN], dump[10];

	fd = open(file, O_RDONLY);
	if(fd < 0) {
		perror("open");
		return fd;
	}
	if(read(fd, buff, BUF_LEN) <= 0) {
		fprintf(stderr, "thinkbat: couldn't read from %s, or file empty\n", file);
		close(fd);
		return fd;
	}

	sscanf(buff, "%d %s", &data, dump);

	close(fd);

	return data;
}