summaryrefslogtreecommitdiffstats
path: root/src/measurement/sysfs.cpp
blob: c5a7a12d08db2f3cea10746d16591e28465c610c (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
/*
 * Copyright (c) 2011 Anssi Hannula
 *
 * 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:
 *	Anssi Hannula <anssi.hannula@iki.fi>
 */
#include "measurement.h"
#include "sysfs.h"
#include "../lib.h"
#include <string.h>
#include <stdio.h>
#include <limits.h>

sysfs_power_meter::sysfs_power_meter(const char *power_supply_name)
{
	rate = 0.0;
	capacity = 0.0;
	pt_strcpy(name, power_supply_name);
}

bool sysfs_power_meter::get_sysfs_attr(const char *attribute, int *value)
{
	char filename[PATH_MAX];
	bool ok;

	snprintf(filename, sizeof(filename), "/sys/class/power_supply/%s/%s", name, attribute);
	*value = read_sysfs(filename, &ok);

	return ok;
}

bool sysfs_power_meter::is_present()
{
	int present = 0;

	if (!get_sysfs_attr("present", &present))
		return true; /* assume always present */

	return present;
}

double sysfs_power_meter::get_voltage()
{
	int voltage;

	if (!get_sysfs_attr("voltage_now", &voltage))
		return -1.0;

	/* µV to V */
	return voltage / 1000000.0;
}

bool sysfs_power_meter::set_rate_from_power()
{
	int power;

	if (!get_sysfs_attr("power_now", &power))
		return false;

	/* µW to W */
	rate = power / 1000000.0;
	return true;
}

bool sysfs_power_meter::set_rate_from_current(double voltage)
{
	int current;

	if (!get_sysfs_attr("current_now", &current))
		return false;

	/* current: µA
	 * voltage: V
	 * rate: W */
	rate = (current / 1000000.0) * voltage;
	return true;
}

bool sysfs_power_meter::set_capacity_from_energy()
{
	int energy;

	if (!get_sysfs_attr("energy_now", &energy))
		return false;

	/* µWh to J */
	capacity = energy / 1000000.0 * 3600.0;
	return true;
}

bool sysfs_power_meter::set_capacity_from_charge(double voltage)
{
	int charge;

	if (!get_sysfs_attr("charge_now", &charge))
		return false;

	/* charge: µAh
	 * voltage: V
	 * capacity: J */
	capacity = (charge / 1000000.0) * voltage * 3600.0;
	return true;
}

void sysfs_power_meter::measure()
{
	bool got_rate = false;
	bool got_capacity = false;

	rate = 0.0;
	capacity = 0.0;
	this->set_discharging(false);

	if (!is_present())
		return;
	/** do not jump over. we may have discharging battery */
	if (read_sysfs_string("/sys/class/power_supply/%s/status", name) == "Discharging")
		this->set_discharging(true);

	got_rate = set_rate_from_power();
	got_capacity = set_capacity_from_energy();

	if (!got_rate || !got_capacity) {
		double voltage = get_voltage();
		if (voltage < 0.0)
			return;
		if (!got_rate)
			set_rate_from_current(voltage);
		if (!got_capacity)
			set_capacity_from_charge(voltage);
	}
}

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

void sysfs_power_meter::start_measurement(void)
{
	/* Battery state is generally a lagging indication, lets only measure at the end */
}