-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibbattery.c
299 lines (257 loc) · 7.44 KB
/
libbattery.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- linux-c -*-
*
* Distribute under LGPL v2 or (at your option) later version.
*
* Copyright (C) 2017 Pavel Machek <pavel@ucw.cz>
*
* Thanks to Sam Lantinga and his SDL2.
* Thanks to Marek Belisko.
*/
#include <stdio.h>
#include <string.h>
#include <alloca.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include <dirent.h>
#include "battery.h"
#define max(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define min(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
static const char *sys_class_power_supply_path = "/sys/class/power_supply";
static int
open_power_file(const char *base, const char *node, const char *key)
{
const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3;
char *path = (char *) alloca(pathlen);
if (path == NULL) {
return -1; /* oh well. */
}
snprintf(path, pathlen, "%s/%s/%s", base, node, key);
return open(path, O_RDONLY);
}
static bool
read_power_file(const char *base, const char *node, const char *key,
char *buf, size_t buflen)
{
ssize_t br = 0;
const int fd = open_power_file(base, node, key);
if (fd == -1) {
return false;
}
br = read(fd, buf, buflen-1);
close(fd);
if (br < 0) {
return false;
}
buf[br] = '\0'; /* null-terminate the string. */
return true;
}
static bool
int_string(char *str, int *val)
{
char *endptr = NULL;
*val = (int) strtol(str, &endptr, 0);
return ((*str != '\0') && (*endptr == '\0'));
}
/* calculate remaining fuel level (in %) of a LiIon battery assuming
* a standard chemistry model
* The first reference found on the web seems to be a forum post
* by "SilverFox" from 04-16-2008. It appears to be attributed to Sanyo.
* http://www.candlepowerforums.com/vb/showthread.php?115871-Li-Ion-State-of-Charge-and-Voltage-Measurements#post2440539
* The linear interpplation below 19.66% was suggested by Pavel Machek.
*
* @mV: voltage measured outside the battery
* @mA: current flowing out of the battery
* @mOhm: assumed series resitance of the battery
*
* returns value between 0 and 100
*/
static inline int fuel_level_LiIon(int mV, int mA, int mOhm)
{
int u;
/* internal battery voltage is higher than measured when discharging */
mV += (mOhm * mA) /1000;
/* apply first part of formula */
u = 3870000 - (14523 * (37835 - 10 * mV));
/* use linear approx. below 3.756V => 19.66% assuming 3.3V => 0% */
if (u < 0) {
return max(((mV - 3300) * ((3756 - 3300) * 1966)) / 100000, 0);
}
/* apply second part of formula */
return min((int)(1966 + sqrt(u))/100, 100);
}
double battery_estimate(struct battery_info *i)
{
int mA = 100;
if (!isnan(i->current)) {
mA = i->current * 1000;
printf("Have current %d mA\n", mA);
}
return fuel_level_LiIon(i->voltage * 1000, mA, 50) / 100.;
}
bool
battery_fill_info(struct battery_info *i)
{
const char *base = sys_class_power_supply_path;
struct dirent *dent;
DIR *dirp;
dirp = opendir(base);
if (!dirp) {
return false;
}
i->state = NO_BATTERY; /* assume we're just plugged in. */
i->seconds = NAN;
i->fraction = NAN;
i->voltage = NAN;
i->current = NAN;
i->temperature = NAN;
while ((dent = readdir(dirp)) != NULL) {
const char *name = dent->d_name;
bool choose = false;
char str[64];
enum battery_state st;
int secs = -1;
int pct = -1;
int vlt = -1;
int cur = -999999999;
int temp = -999999999;
if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
continue; /* skip these, of course. */
}
if (!read_power_file(base, name, "type", str, sizeof (str))) {
continue; /* Don't know _what_ we're looking at. Give up on it. */
}
if (strcmp(str, "Battery\n") != 0) {
continue; /* we don't care about UPS and such. */
}
if (!strcmp(name, "rx51-battery")) {
/* Nokia N900 has rx51-battery and bq27200-0; both have type=Battery,
and unfortunately both refer to same battery.
*/
continue;
}
/* if the scope is "device," it might be something like a PS4
controller reporting its own battery, and not something that powers
the system. Most system batteries don't list a scope at all; we
assume it's a system battery if not specified. */
if (read_power_file(base, name, "scope", str, sizeof (str))) {
if (strcmp(str, "device\n") == 0) {
continue; /* skip external devices with their own batteries. */
}
}
/* some drivers don't offer this, so if it's not explicitly reported assume it's present. */
if (read_power_file(base, name, "present", str, sizeof (str)) && (strcmp(str, "0\n") == 0)) {
st = NO_BATTERY;
} else if (!read_power_file(base, name, "status", str, sizeof (str))) {
st = UNKNOWN; /* uh oh */
} else if (strcmp(str, "Charging\n") == 0) {
st = CHARGING;
} else if (strcmp(str, "Discharging\n") == 0) {
st = ON_BATTERY;
} else if ((strcmp(str, "Full\n") == 0) || (strcmp(str, "Not charging\n") == 0)) {
st = FULL;
} else {
st = UNKNOWN; /* uh oh */
}
if (read_power_file(base, name, "capacity", str, sizeof (str))) {
pct = atoi(str);
pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
}
if (read_power_file(base, name, "voltage_now", str, sizeof (str))) {
vlt = atoi(str);
}
if (read_power_file(base, name, "current_now", str, sizeof (str))) {
cur = atoi(str);
}
if (read_power_file(base, name, "temp", str, sizeof (str))) {
temp = atoi(str);
}
if (read_power_file(base, name, "time_to_empty_now", str, sizeof (str))) {
secs = atoi(str);
secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
}
/*
* We pick the battery that claims to have the most minutes left.
* (failing a report of minutes, we'll take the highest percent.)
*/
if ((secs < 0) && (isnan(i->seconds))) {
if ((pct < 0) && (isnan(i->fraction))) {
choose = true; /* at least we know there's a battery. */
} else if (pct > i->fraction * 100) {
choose = true;
}
} else if (secs > i->seconds) {
choose = true;
}
if (choose) {
printf("choosen: %s\n", name);
if (secs != -1)
i->seconds = secs;
else
i->seconds = NAN;
if (pct != -1)
i->fraction = pct/100.;
else
i->fraction = NAN;
i->state = st;
if (vlt != -1)
i->voltage = vlt/1000000.;
else
i->voltage = NAN;
if (cur != -999999999)
i->current = cur/1000000.;
else
i->current = NAN;
if (temp != -999999999)
i->temperature = temp/10.;
else
i->temperature = NAN;
}
}
closedir(dirp);
return true; /* don't look any further. */
}
struct battery *battery_init(void)
{
return NULL;
}
double battery_fraction(struct battery *b)
{
struct battery_info i = {0, };
i.battery = b;
if (!battery_fill_info(&i))
return -1;
battery_dump(&i);
if (isnan(i.fraction)) {
return battery_estimate(&i);
}
return i.fraction;
}
char *battery_state_string(enum battery_state s)
{
switch (s) {
case NO_BATTERY: return "no battery";
case UNKNOWN: return "unknown";
case CHARGING: return "charging";
case ON_BATTERY: return "on battery";
case FULL: return "full";
default: return "internal error";
}
}
void battery_dump(struct battery_info *i)
{
printf("Battery %.0f %%\n", i->fraction * 100);
printf("Seconds %.0f\n", i->seconds);
printf("State %d -- %s\n", i->state, battery_state_string(i->state));
printf("Voltage %.2f V\n", i->voltage);
printf("Current %.3f A\n", i->current);
printf("Temperature %.1f C\n", i->temperature);
}