aboutsummaryrefslogtreecommitdiff
path: root/src/measurement/power_supply.cpp
blob: f51474058de42bb49f02cf785efc52400d3da985 (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
/*
 * Copyright 2011, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * This program file 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; version 2 of the License.
 *
 * 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 in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 * or just google for it.
 *
 * Authors:
 *	John Mathew <johnx.mathew@intel.com>
 */
#include "measurement.h"
#include "power_supply.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

power_supply::power_supply(const char *supply_name)
{
	rate = 0.0;
	capacity = 0.0;
	voltage = 0.0;
	strncpy(battery_name, supply_name, sizeof(battery_name));
}

/*
POWER_SUPPLY_NAME=msic-battery
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_HEALTH=Cold
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_VOLTAGE_MAX_DESIGN=4200000
POWER_SUPPLY_VOLTAGE_NOW=4119000
POWER_SUPPLY_CURRENT_NOW=-290000
POWER_SUPPLY_CHARGE_NOW=1503000
POWER_SUPPLY_CHARGE_COUNTER=-254923
POWER_SUPPLY_CHARGE_FULL_DESIGN=1500000
POWER_SUPPLY_CHARGE_FULL=1500000
POWER_SUPPLY_CHARGE_AVG=32762000
POWER_SUPPLY_ENERGY_FULL=6300000
POWER_SUPPLY_ENERGY_NOW=6235000
POWER_SUPPLY_CAPACITY_LEVEL=Full
POWER_SUPPLY_CAPACITY=100
POWER_SUPPLY_TEMP=-340
POWER_SUPPLY_MODEL_NAME=CDK0
POWER_SUPPLY_MANUFACTURER=IN

Quoting include/linux/power_supply.h:

All voltages, currents, charges, energies, time and temperatures in µV,
µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
stated. It's driver's job to convert its raw values to units in which
this class operates.
*/

void power_supply::measure(void)
{
	char filename[4096];
	char line[4096];
	ifstream file;

	double _power_rate = 0;
	double _current_rate = 0;
	double _capacity = 0;
	double _voltage = 0;

	rate = 0;
	voltage = 0;
	capacity = 0;

	sprintf(filename, "/sys/class/power_supply/%s/uevent", battery_name);

	file.open(filename, ios::in);
	if (!file)
		return;

	while (file) {
		char *c;
		file.getline(line, 4096);

		if (strstr(line, "PRESENT")) {
			c = strchr(line, '=');
			c++;
			if(*c == '0'){
				printf ("Battery not present");
				return;
			}
		}
		if (strstr(line, "CURRENT_NOW")) {
			c = strchr(line, '=');
			c++;
			if(*c == '-') c++; // ignoring the negative sign
			_current_rate = strtoull(c, NULL, 10);
			if (c) {
				//printf ("CURRENT: %f. \n",_current_rate);
			} else {
				_current_rate = 0;
			}
		}
		if (strstr(line, "POWER_NOW")) {
			c = strchr(line, '=');
			c++;
			_power_rate = strtoull(c, NULL, 10);
			if (c) {
				//printf ("POWER: %f. \n",_power_rate);
			} else {
				_power_rate = 0;
			}
		}
		if (strstr(line, "CAPACITY=")) {
			c = strchr(line, '=');
			c++;
			_capacity = strtoull(c, NULL, 10);
			if (c) {
				//printf ("CAPACITY: %f. \n",_capacity);
			} else {
				_capacity = 0;
			}
		}
		if (strstr(line, "VOLTAGE_NOW")) {
			c = strchr(line, '=');
			c++;
			while (*c == ' ') c++;
			_voltage = strtoull(c, NULL, 10);
			if (c) {
				//printf ("VOLTAGE_NOW: %f. \n",_voltage);
			} else {
				_voltage = 0;
			}
		}
	}
	file.close();

	if(_voltage) {
		_voltage = _voltage / 1000.0;
		voltage = _voltage;
	} else {
		voltage = 0.0;
	}

	if(_power_rate)
	{
		rate = _power_rate / 1000000.0;
	}
	else if(_current_rate) {
		_current_rate = _current_rate / 1000.0;
		rate = _current_rate * _voltage;
	} else {
		rate = 0.0;
	}

	if(_capacity)
		capacity = _capacity;
	else
		capacity = 0.0;
}


void power_supply::end_measurement(void)
{
	measure();
}

void power_supply::start_measurement(void)
{
	/* ACPI battery state is a lagging indication, lets only measure at the end */
}


double power_supply::joules_consumed(void)
{
	return rate;
}