2014-08-18 22:56:10 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2011-03-25 18:45:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-08-18 22:56:10 +00:00
|
|
|
#include <ctype.h>
|
2011-03-25 18:45:13 +00:00
|
|
|
#include <stdlib.h>
|
2014-08-18 22:56:28 +00:00
|
|
|
#include <unistd.h>
|
2011-03-25 18:45:13 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2014-08-18 22:56:21 +00:00
|
|
|
#include <time.h>
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
#include "libacpi.h"
|
2011-03-25 18:45:13 +00:00
|
|
|
|
|
|
|
extern char *state[];
|
2014-08-18 22:56:28 +00:00
|
|
|
|
|
|
|
#define PROC_DATA_SOURCE 0
|
|
|
|
#define SYSFS_DATA_SOURCE 1
|
|
|
|
static int data_source;
|
2011-03-25 18:45:13 +00:00
|
|
|
|
|
|
|
/* local proto */
|
2014-08-18 22:56:11 +00:00
|
|
|
int acpi_get_design_cap(int batt);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
static int read_sysfs_file(char *node, char *prop, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
char tmp[256];
|
|
|
|
FILE *fp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = snprintf(tmp, sizeof(tmp), "/sys/class/power_supply/%s/%s", node, prop);
|
|
|
|
if (ret >= (int)sizeof(tmp)) {
|
|
|
|
perr("Path too long for %s/%s\n", node, prop);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(tmp, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
perr("Could not open %s/%s\n", node, prop);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fread(buf, 1, buflen - 1, fp);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
perr("Could not read %s/%s\n", node, prop);
|
|
|
|
return -3;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
buf[ret] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
/* initialise the batteries */
|
2014-08-18 22:56:28 +00:00
|
|
|
static int sysfs_init_batteries(global_t *globals)
|
|
|
|
{
|
|
|
|
DIR *battdir;
|
|
|
|
struct dirent *batt;
|
|
|
|
char *name;
|
|
|
|
char *names[MAXBATT];
|
|
|
|
char ps_type[16];
|
|
|
|
int i, j;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
/* now enumerate batteries */
|
|
|
|
globals->battery_count = 0;
|
|
|
|
battdir = opendir("/sys/class/power_supply");
|
|
|
|
if (battdir == NULL) {
|
|
|
|
pfatal("No batteries or ACPI not supported\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while ((batt = readdir(battdir))) {
|
|
|
|
/* there's a serious problem with this code when there's
|
|
|
|
* more than one battery: the readdir won't return the
|
2014-10-05 15:29:59 +00:00
|
|
|
* entries in sorted order, so battery one won't
|
2014-08-18 22:56:28 +00:00
|
|
|
* necessarily be the first one returned. So, we need
|
2014-10-05 15:29:59 +00:00
|
|
|
* to sort them ourselves before adding them to the
|
2014-08-18 22:56:28 +00:00
|
|
|
* batteries array. */
|
|
|
|
name = batt->d_name;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
/* skip ., .. and dotfiles */
|
|
|
|
if (name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (read_sysfs_file(name, "type", ps_type, sizeof(ps_type)) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strncmp("Battery", ps_type, 7) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
names[globals->battery_count] = strdup(name);
|
|
|
|
globals->battery_count++;
|
|
|
|
}
|
|
|
|
closedir(battdir);
|
|
|
|
|
|
|
|
/* A nice quick insertion sort, ala CLR. */
|
|
|
|
{
|
|
|
|
char *tmp1, *tmp2;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
for (i = 1; i < globals->battery_count; i++) {
|
|
|
|
tmp1 = names[i];
|
|
|
|
j = i - 1;
|
|
|
|
while ((j >= 0) && ((strcmp(tmp1, names[j])) < 0)) {
|
|
|
|
tmp2 = names[j+1];
|
|
|
|
names[j+1] = names[j];
|
|
|
|
names[j] = tmp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
for (i = 0; i < globals->battery_count; i++) {
|
|
|
|
snprintf(batteries[i].name, MAX_NAME, "%s", names[i]);
|
|
|
|
pdebug("battery detected at /sys/class/power_supply/%s\n", batteries[i].name);
|
|
|
|
pinfo("found battery %s\n", names[i]);
|
|
|
|
|
|
|
|
if (read_sysfs_file(batteries[i].name, "energy_now", ps_type, sizeof(ps_type)) == 0)
|
|
|
|
batteries[i].sysfs_capa_mode = SYSFS_CAPA_ENERGY;
|
|
|
|
else if (read_sysfs_file(batteries[i].name, "charge_now", ps_type, sizeof(ps_type)) == 0)
|
|
|
|
batteries[i].sysfs_capa_mode = SYSFS_CAPA_CHARGE;
|
|
|
|
else if (read_sysfs_file(batteries[i].name, "capacity", ps_type, sizeof(ps_type)) == 0) {
|
|
|
|
batteries[i].sysfs_capa_mode = SYSFS_CAPA_PERCENT;
|
|
|
|
batteries[i].design_cap = 100;
|
|
|
|
batteries[i].last_full_cap = 100;
|
|
|
|
} else
|
|
|
|
batteries[i].sysfs_capa_mode = SYSFS_CAPA_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tell user some info */
|
|
|
|
pdebug("%d batteries detected\n", globals->battery_count);
|
|
|
|
pinfo("libacpi: found %d batter%s\n", globals->battery_count,
|
|
|
|
(globals->battery_count == 1) ? "y" : "ies");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialise the batteries */
|
|
|
|
static int procfs_init_batteries(global_t *globals)
|
2011-03-25 18:45:13 +00:00
|
|
|
{
|
|
|
|
DIR *battdir;
|
|
|
|
struct dirent *batt;
|
2014-08-18 22:56:19 +00:00
|
|
|
char *name;
|
2014-08-18 22:56:10 +00:00
|
|
|
char *names[MAXBATT];
|
|
|
|
int i, j;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2011-03-25 18:45:13 +00:00
|
|
|
/* now enumerate batteries */
|
2014-08-18 22:56:19 +00:00
|
|
|
globals->battery_count = 0;
|
2011-03-25 18:45:13 +00:00
|
|
|
battdir = opendir("/proc/acpi/battery");
|
|
|
|
if (battdir == NULL) {
|
2014-08-18 22:56:19 +00:00
|
|
|
pfatal("No batteries or ACPI not supported\n");
|
2011-03-25 18:45:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while ((batt = readdir(battdir))) {
|
2014-08-18 22:56:10 +00:00
|
|
|
/* there's a serious problem with this code when there's
|
|
|
|
* more than one battery: the readdir won't return the
|
2014-10-05 15:29:59 +00:00
|
|
|
* entries in sorted order, so battery one won't
|
2014-08-18 22:56:10 +00:00
|
|
|
* necessarily be the first one returned. So, we need
|
2014-10-05 15:29:59 +00:00
|
|
|
* to sort them ourselves before adding them to the
|
2014-08-18 22:56:10 +00:00
|
|
|
* batteries array. */
|
2011-03-25 18:45:13 +00:00
|
|
|
name = batt->d_name;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2011-03-25 18:45:13 +00:00
|
|
|
/* skip . and .. */
|
|
|
|
if (!strncmp(".", name, 1) || !strncmp("..", name, 2))
|
|
|
|
continue;
|
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
names[globals->battery_count] = strdup(name);
|
|
|
|
globals->battery_count++;
|
2011-03-25 18:45:13 +00:00
|
|
|
}
|
|
|
|
closedir(battdir);
|
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* A nice quick insertion sort, ala CLR. */
|
2014-08-18 22:56:19 +00:00
|
|
|
{
|
|
|
|
char *tmp1, *tmp2;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
for (i = 1; i < globals->battery_count; i++) {
|
|
|
|
tmp1 = names[i];
|
|
|
|
j = i - 1;
|
|
|
|
while ((j >= 0) && ((strcmp(tmp1, names[j])) < 0)) {
|
|
|
|
tmp2 = names[j+1];
|
|
|
|
names[j+1] = names[j];
|
|
|
|
names[j] = tmp2;
|
|
|
|
}
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
for (i = 0; i < globals->battery_count; i++) {
|
2014-08-18 22:56:10 +00:00
|
|
|
snprintf(batteries[i].name, MAX_NAME, "%s", names[i]);
|
2014-10-05 15:29:59 +00:00
|
|
|
snprintf(batteries[i].info_file, MAX_NAME,
|
2014-08-18 22:56:10 +00:00
|
|
|
"/proc/acpi/battery/%s/info", names[i]);
|
2014-10-05 15:29:59 +00:00
|
|
|
snprintf(batteries[i].state_file, MAX_NAME,
|
2014-08-18 22:56:10 +00:00
|
|
|
"/proc/acpi/battery/%s/state", names[i]);
|
2014-08-18 22:56:14 +00:00
|
|
|
pdebug("battery detected at %s\n", batteries[i].info_file);
|
|
|
|
pinfo("found battery %s\n", names[i]);
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 18:45:13 +00:00
|
|
|
/* tell user some info */
|
2014-08-18 22:56:19 +00:00
|
|
|
pdebug("%d batteries detected\n", globals->battery_count);
|
|
|
|
pinfo("libacpi: found %d batter%s\n", globals->battery_count,
|
|
|
|
(globals->battery_count == 1) ? "y" : "ies");
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2011-03-25 18:45:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
int init_batteries(global_t *globals)
|
|
|
|
{
|
|
|
|
if (data_source == SYSFS_DATA_SOURCE)
|
|
|
|
return sysfs_init_batteries(globals);
|
|
|
|
else
|
|
|
|
return procfs_init_batteries(globals);
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
/* a stub that just calls the current function */
|
2014-08-18 22:56:21 +00:00
|
|
|
int reinit_batteries(global_t *globals)
|
2014-08-18 22:56:19 +00:00
|
|
|
{
|
|
|
|
pdebug("reinitialising batteries\n");
|
2014-08-18 22:56:21 +00:00
|
|
|
return init_batteries(globals);
|
2014-08-18 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
/* the actual name of the subdirectory under power_supply may
|
|
|
|
* be anything, so we need to read the directory and use the
|
|
|
|
* name we find there. */
|
|
|
|
static int sysfs_init_ac_adapters(global_t *globals)
|
|
|
|
{
|
|
|
|
DIR *acdir;
|
|
|
|
struct dirent *adapter;
|
|
|
|
adapter_t *ap = &globals->adapter;
|
|
|
|
char *name;
|
|
|
|
char ps_type[16];
|
|
|
|
|
|
|
|
acdir = opendir("/sys/class/power_supply");
|
|
|
|
if (acdir == NULL) {
|
|
|
|
pfatal("Unable to open /sys/class/power_supply -"
|
|
|
|
" are you sure this system supports ACPI?\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
name = NULL;
|
|
|
|
while ((adapter = readdir(acdir)) != NULL) {
|
|
|
|
name = adapter->d_name;
|
|
|
|
|
|
|
|
if (name[0] == '.') {
|
|
|
|
name = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_sysfs_file(name, "type", ps_type, sizeof(ps_type)) < 0) {
|
|
|
|
name = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp("Mains", ps_type, 5) == 0) {
|
|
|
|
pdebug("found adapter %s\n", name);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
name = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(acdir);
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
perr("No AC adapter found !\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we'll just use the first adapter we find ... */
|
|
|
|
ap->name = strdup(name);
|
|
|
|
pinfo("libacpi: found ac adapter %s\n", ap->name);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
/* the actual name of the subdirectory under ac_adapter may
|
|
|
|
* be anything, so we need to read the directory and use the
|
|
|
|
* name we find there. */
|
2014-08-18 22:56:28 +00:00
|
|
|
static int procfs_init_ac_adapters(global_t *globals)
|
2014-08-18 22:56:11 +00:00
|
|
|
{
|
|
|
|
DIR *acdir;
|
|
|
|
struct dirent *adapter;
|
2014-08-18 22:56:13 +00:00
|
|
|
adapter_t *ap = &globals->adapter;
|
2014-08-18 22:56:11 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
acdir = opendir("/proc/acpi/ac_adapter");
|
|
|
|
if (acdir == NULL) {
|
2014-08-18 22:56:14 +00:00
|
|
|
pfatal("Unable to open /proc/acpi/ac_adapter -"
|
2014-08-18 22:56:11 +00:00
|
|
|
" are you sure this system supports ACPI?\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
name = NULL;
|
|
|
|
while ((adapter = readdir(acdir)) != NULL) {
|
|
|
|
name = adapter->d_name;
|
|
|
|
|
|
|
|
if (!strncmp(".", name, 1) || !strncmp("..", name, 2))
|
|
|
|
continue;
|
2014-08-18 22:56:14 +00:00
|
|
|
pdebug("found adapter %s\n", name);
|
2014-08-18 22:56:11 +00:00
|
|
|
}
|
2014-08-18 22:56:19 +00:00
|
|
|
closedir(acdir);
|
2014-08-18 22:56:11 +00:00
|
|
|
/* we /should/ only see one filename other than . and .. so
|
|
|
|
* we'll just use the last value name acquires . . . */
|
|
|
|
ap->name = strdup(name);
|
|
|
|
snprintf(ap->state_file, MAX_NAME, "/proc/acpi/ac_adapter/%s/state",
|
|
|
|
ap->name);
|
2014-08-18 22:56:14 +00:00
|
|
|
pinfo("libacpi: found ac adapter %s\n", ap->name);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
int init_ac_adapters(global_t *globals)
|
|
|
|
{
|
|
|
|
if (data_source == SYSFS_DATA_SOURCE)
|
|
|
|
return sysfs_init_ac_adapters(globals);
|
|
|
|
else
|
|
|
|
return procfs_init_ac_adapters(globals);
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
/* stub that does nothing but call the normal init function */
|
2014-08-18 22:56:21 +00:00
|
|
|
int reinit_ac_adapters(global_t *globals)
|
2014-08-18 22:56:19 +00:00
|
|
|
{
|
|
|
|
pdebug("reinitialising ac adapters\n");
|
2014-08-18 22:56:21 +00:00
|
|
|
return init_ac_adapters(globals);
|
2014-08-18 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
/* see if we have ACPI support and check version */
|
2014-08-18 22:56:21 +00:00
|
|
|
int power_init(global_t *globals)
|
2014-08-18 22:56:11 +00:00
|
|
|
{
|
|
|
|
FILE *acpi;
|
|
|
|
char buf[4096];
|
|
|
|
int acpi_ver = 0;
|
|
|
|
int retval;
|
2014-08-18 22:56:27 +00:00
|
|
|
unsigned int version_offset = 0;
|
|
|
|
|
|
|
|
if (!(acpi = fopen("/sys/module/acpi/parameters/acpica_version", "r"))) {
|
|
|
|
if (!(acpi = fopen("/proc/acpi/info", "r"))) {
|
|
|
|
pfatal("This system does not support ACPI\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
version_offset = 25;
|
|
|
|
}
|
2014-08-18 22:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* okay, now see if we got the right version */
|
|
|
|
fread(buf, 4096, 1, acpi);
|
2014-08-18 22:56:27 +00:00
|
|
|
acpi_ver = strtol(buf + version_offset, NULL, 10);
|
2014-08-18 22:56:14 +00:00
|
|
|
pinfo("ACPI version detected: %d\n", acpi_ver);
|
2014-08-18 22:56:11 +00:00
|
|
|
if (acpi_ver < 20020214) {
|
2014-08-18 22:56:14 +00:00
|
|
|
pfatal("This version requires ACPI subsystem version 20020214\n");
|
2014-08-18 22:56:11 +00:00
|
|
|
fclose(acpi);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* yep, all good */
|
|
|
|
fclose(acpi);
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
/* determine data source */
|
|
|
|
if (access("/sys/class/power_supply", R_OK | X_OK) == 0) {
|
|
|
|
data_source = SYSFS_DATA_SOURCE;
|
|
|
|
pinfo("Selecting sysfs as the data source\n");
|
|
|
|
} else {
|
|
|
|
data_source = PROC_DATA_SOURCE;
|
|
|
|
pinfo("Selecting procfs as the data source\n");
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
if (!(retval = init_batteries(globals)))
|
|
|
|
retval = init_ac_adapters(globals);
|
2014-08-18 22:56:11 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
/* reinitialise everything, to deal with changing batteries or ac adapters */
|
2014-08-18 22:56:21 +00:00
|
|
|
int power_reinit(global_t *globals)
|
2014-08-18 22:56:19 +00:00
|
|
|
{
|
|
|
|
FILE *acpi;
|
|
|
|
int retval;
|
|
|
|
|
2014-08-18 22:56:27 +00:00
|
|
|
if (!(acpi = fopen("/sys/module/acpi/parameters/acpica_version", "r"))) {
|
|
|
|
if (!(acpi = fopen("/proc/acpi/info", "r"))) {
|
|
|
|
pfatal("Could not reopen ACPI info file - does this system support ACPI?\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-08-18 22:56:19 +00:00
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
if (!(retval = reinit_batteries(globals)))
|
|
|
|
retval = reinit_ac_adapters(globals);
|
2014-08-18 22:56:19 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
static char *get_value(char *string)
|
2011-03-25 18:45:13 +00:00
|
|
|
{
|
2014-08-18 22:56:10 +00:00
|
|
|
char *retval;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (string == NULL)
|
|
|
|
return NULL;
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
i = 0;
|
|
|
|
while (string[i] != ':') i++;
|
|
|
|
while (!isalnum(string[i])) i++;
|
|
|
|
retval = (string + i);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
static int check_error(char *buf)
|
2014-08-18 22:56:20 +00:00
|
|
|
{
|
|
|
|
if(strstr(buf, "ERROR") != NULL)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
static power_state_t sysfs_get_power_status(global_t *globals)
|
|
|
|
{
|
|
|
|
char online[2];
|
|
|
|
adapter_t *ap = &globals->adapter;
|
|
|
|
|
|
|
|
if (read_sysfs_file(ap->name, "online", online, sizeof(online)) < 0)
|
|
|
|
return PS_ERR;
|
|
|
|
|
|
|
|
if (*online == '1')
|
|
|
|
return AC;
|
|
|
|
else
|
|
|
|
return BATT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static power_state_t procfs_get_power_status(global_t *globals)
|
2014-08-18 22:56:10 +00:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
char buf[1024];
|
|
|
|
char *val;
|
2014-08-18 22:56:13 +00:00
|
|
|
adapter_t *ap = &globals->adapter;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
if ((file = fopen(ap->state_file, "r")) == NULL) {
|
|
|
|
snprintf(buf, 1024, "Could not open state file %s", ap->state_file);
|
|
|
|
perror(buf);
|
2014-08-18 22:56:10 +00:00
|
|
|
return PS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
fgets(buf, 1024, file);
|
|
|
|
fclose(file);
|
|
|
|
val = get_value(buf);
|
|
|
|
if ((strncmp(val, "on-line", 7)) == 0)
|
|
|
|
return AC;
|
|
|
|
else
|
|
|
|
return BATT;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
power_state_t get_power_status(global_t *globals)
|
|
|
|
{
|
|
|
|
if (data_source == SYSFS_DATA_SOURCE)
|
|
|
|
return sysfs_get_power_status(globals);
|
|
|
|
else
|
|
|
|
return procfs_get_power_status(globals);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysfs_get_battery_info(global_t *globals, int batt_no)
|
|
|
|
{
|
|
|
|
battery_t *info = &batteries[batt_no];
|
|
|
|
char buf[32];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* check to see if battery is present */
|
|
|
|
ret = read_sysfs_file(info->name, "present", buf, sizeof(buf));
|
|
|
|
if (ret < 0) {
|
|
|
|
/* interestingly, when the battery is not present, the whole
|
|
|
|
* /sys/class/power_supply/BATn directory does not exist.
|
|
|
|
* Yes, this is broken.
|
|
|
|
*/
|
|
|
|
if (ret == -2)
|
|
|
|
info->present = 0;
|
|
|
|
|
|
|
|
/* reinit batteries, this one went away and it's very
|
|
|
|
possible there just isn't any other one */
|
|
|
|
reinit_batteries(globals);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->present = (*buf == '1');
|
|
|
|
if (!info->present) {
|
|
|
|
pinfo("Battery %s not present\n", info->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get design capacity
|
|
|
|
* note that all these integer values can also contain the
|
|
|
|
* string 'unknown', so we need to check for this. */
|
|
|
|
if (info->sysfs_capa_mode == SYSFS_CAPA_ENERGY) {
|
|
|
|
if (read_sysfs_file(info->name, "energy_full_design", buf, sizeof(buf)) < 0)
|
|
|
|
info->design_cap = -1;
|
|
|
|
else
|
|
|
|
info->design_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
|
|
|
|
/* get last full capacity */
|
|
|
|
if (read_sysfs_file(info->name, "energy_full", buf, sizeof(buf)) < 0)
|
|
|
|
info->last_full_cap = -1;
|
|
|
|
else
|
|
|
|
info->last_full_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
} else if (info->sysfs_capa_mode == SYSFS_CAPA_CHARGE) {
|
|
|
|
/* get design capacity */
|
|
|
|
if (read_sysfs_file(info->name, "charge_full_design", buf, sizeof(buf)) < 0)
|
|
|
|
info->design_cap = -1;
|
|
|
|
else
|
|
|
|
info->design_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
|
|
|
|
/* get last full capacity */
|
|
|
|
if (read_sysfs_file(info->name, "charge_full", buf, sizeof(buf)) < 0)
|
|
|
|
info->last_full_cap = -1;
|
|
|
|
else
|
|
|
|
info->last_full_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
} else if (info->sysfs_capa_mode != SYSFS_CAPA_PERCENT) {
|
|
|
|
info->design_cap = -1;
|
|
|
|
info->last_full_cap = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get design voltage */
|
|
|
|
if (read_sysfs_file(info->name, "voltage_min_design", buf, sizeof(buf)) < 0)
|
|
|
|
info->design_voltage = -1;
|
|
|
|
else
|
|
|
|
info->design_voltage = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
|
|
|
|
/* get charging state */
|
|
|
|
if (read_sysfs_file(info->name, "status", buf, sizeof(buf)) < 0) {
|
|
|
|
info->charge_state = CH_ERR;
|
|
|
|
} else {
|
|
|
|
if (strncmp(buf, "Unknown", 7) == 0)
|
|
|
|
info->charge_state = CH_ERR;
|
|
|
|
else if (strncmp(buf, "Discharging", 11) == 0)
|
|
|
|
info->charge_state = DISCHARGE;
|
|
|
|
else if (strncmp(buf, "Charging", 8) == 0)
|
|
|
|
info->charge_state = CHARGE;
|
|
|
|
else if (strncmp(buf, "Not charging", 12) == 0)
|
|
|
|
info->charge_state = NO_CHARGE;
|
|
|
|
else if (strncmp(buf, "Full", 4) == 0)
|
|
|
|
info->charge_state = FULL; /* DISCHARGE ? as per old comment ... */
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
/* get current rate of burn
|
2014-08-18 22:56:28 +00:00
|
|
|
* note that if it's on AC, this will report 0 */
|
|
|
|
if (read_sysfs_file(info->name, "current_now", buf, sizeof(buf)) < 0)
|
|
|
|
info->present_rate = -1;
|
|
|
|
else {
|
|
|
|
int rate;
|
|
|
|
rate = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
info->present_rate = (rate != 0) ? rate : info->present_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->sysfs_capa_mode == SYSFS_CAPA_ENERGY) {
|
|
|
|
/* get remaining capacity */
|
|
|
|
if (read_sysfs_file(info->name, "energy_now", buf, sizeof(buf)) < 0)
|
|
|
|
info->remaining_cap = -1;
|
|
|
|
else
|
|
|
|
info->remaining_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
|
|
|
|
} else if (info->sysfs_capa_mode == SYSFS_CAPA_CHARGE) {
|
|
|
|
/* get remaining capacity */
|
|
|
|
if (read_sysfs_file(info->name, "charge_now", buf, sizeof(buf)) < 0)
|
|
|
|
info->remaining_cap = -1;
|
|
|
|
else
|
|
|
|
info->remaining_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
} else if (info->sysfs_capa_mode == SYSFS_CAPA_PERCENT) {
|
|
|
|
/* get remaining capacity */
|
|
|
|
if (read_sysfs_file(info->name, "capacity", buf, sizeof(buf)) < 0)
|
|
|
|
info->remaining_cap = -1;
|
|
|
|
else
|
|
|
|
info->remaining_cap = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
} else {
|
|
|
|
info->remaining_cap = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get current voltage */
|
|
|
|
if (read_sysfs_file(info->name, "voltage_now", buf, sizeof(buf)) < 0)
|
|
|
|
info->present_voltage = -1;
|
|
|
|
else
|
|
|
|
info->present_voltage = strtoul(buf, NULL, 10) / 1000;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int procfs_get_battery_info(global_t *globals, int batt_no)
|
2014-08-18 22:56:10 +00:00
|
|
|
{
|
|
|
|
FILE *file;
|
2014-08-18 22:56:11 +00:00
|
|
|
battery_t *info = &batteries[batt_no];
|
2014-08-18 22:56:10 +00:00
|
|
|
char buf[1024];
|
|
|
|
char *entry;
|
|
|
|
int buflen;
|
|
|
|
char *val;
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
globals = globals; /* silencing a warning */
|
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
if ((file = fopen(info->info_file, "r")) == NULL) {
|
|
|
|
/* this is cheating, but string concatenation should work . . . */
|
2014-08-18 22:56:14 +00:00
|
|
|
pfatal("Could not open %s:", info->info_file );
|
2014-08-18 22:56:10 +00:00
|
|
|
perror(NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* grab the contents of the file */
|
|
|
|
buflen = fread(buf, sizeof(buf), 1, file);
|
|
|
|
fclose(file);
|
|
|
|
|
2014-08-18 22:56:20 +00:00
|
|
|
/* check to see if there were any errors reported in the file */
|
|
|
|
if(check_error(buf)) {
|
|
|
|
pinfo("Error reported in file %s - discarding data\n",
|
|
|
|
info->info_file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* check to see if battery is present */
|
|
|
|
entry = strstr(buf, "present:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if ((strncmp(val, "yes", 3)) == 0) {
|
|
|
|
info->present = 1;
|
2011-03-25 18:45:13 +00:00
|
|
|
} else {
|
2014-08-18 22:56:14 +00:00
|
|
|
pinfo("Battery %s not present\n", info->name);
|
2014-08-18 22:56:10 +00:00
|
|
|
info->present = 0;
|
|
|
|
return 0;
|
2011-03-25 18:45:13 +00:00
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* get design capacity
|
|
|
|
* note that all these integer values can also contain the
|
|
|
|
* string 'unknown', so we need to check for this. */
|
|
|
|
entry = strstr(buf, "design capacity:");
|
|
|
|
val = get_value(entry);
|
2014-10-05 15:29:59 +00:00
|
|
|
if (val[0] == 'u')
|
2014-08-18 22:56:10 +00:00
|
|
|
info->design_cap = -1;
|
|
|
|
else
|
|
|
|
info->design_cap = strtoul(val, NULL, 10);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* get last full capacity */
|
|
|
|
entry = strstr(buf, "last full capacity:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if (val[0] == 'u')
|
|
|
|
info->last_full_cap = -1;
|
|
|
|
else
|
|
|
|
info->last_full_cap = strtoul(val, NULL, 10);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* get design voltage */
|
|
|
|
entry = strstr(buf, "design voltage:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if (val[0] == 'u')
|
|
|
|
info->design_voltage = -1;
|
|
|
|
else
|
|
|
|
info->design_voltage = strtoul(val, NULL, 10);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
if ((file = fopen(info->state_file, "r")) == NULL) {
|
2014-08-18 22:56:14 +00:00
|
|
|
perr("Could not open %s:", info->state_file );
|
2014-08-18 22:56:10 +00:00
|
|
|
perror(NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* grab the file contents */
|
2014-08-18 22:56:20 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2014-08-18 22:56:10 +00:00
|
|
|
buflen = fread(buf, sizeof(buf), 1, file);
|
|
|
|
fclose(file);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:20 +00:00
|
|
|
/* check to see if there were any errors reported in the file */
|
|
|
|
if(check_error(buf)) {
|
|
|
|
pinfo("Error reported in file %s - discarding data\n",
|
|
|
|
info->state_file);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-18 22:56:10 +00:00
|
|
|
/* check to see if battery is present */
|
|
|
|
entry = strstr(buf, "present:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if ((strncmp(val, "yes", 3)) == 0) {
|
|
|
|
info->present = 1;
|
|
|
|
} else {
|
|
|
|
info->present = 0;
|
2014-08-18 22:56:14 +00:00
|
|
|
perr("Battery %s no longer present\n", info->name);
|
2014-08-18 22:56:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get charging state */
|
|
|
|
entry = strstr(buf, "charging state:");
|
|
|
|
val = get_value(entry);
|
2014-10-05 15:29:59 +00:00
|
|
|
if (val[0] == 'u')
|
2014-08-18 22:56:11 +00:00
|
|
|
info->charge_state = CH_ERR;
|
|
|
|
else if ((strncmp(val, "discharging", 10)) == 0)
|
|
|
|
info->charge_state = DISCHARGE;
|
2014-08-18 22:56:27 +00:00
|
|
|
else if ((strncmp(val, "charged", 7)) == 0)
|
|
|
|
/* this is a workaround for machines that report
|
|
|
|
* their charge state as 'charged', rather than
|
|
|
|
* what my laptop does, which is go straight to
|
|
|
|
* 'discharging'. dunno which matches the standard */
|
|
|
|
info->charge_state = DISCHARGE;
|
2014-08-18 22:56:10 +00:00
|
|
|
else
|
2014-08-18 22:56:11 +00:00
|
|
|
info->charge_state = CHARGE;
|
2014-08-18 22:56:10 +00:00
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
/* get current rate of burn
|
2014-08-18 22:56:10 +00:00
|
|
|
* note that if it's on AC, this will report 0 */
|
|
|
|
entry = strstr(buf, "present rate:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if (val[0] == 'u') {
|
|
|
|
info->present_rate = -1;
|
|
|
|
} else {
|
|
|
|
int rate;
|
|
|
|
rate = strtoul(val, NULL, 10);
|
|
|
|
if (rate != 0)
|
|
|
|
info->present_rate = rate;
|
2011-03-25 18:45:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* get remaining capacity */
|
|
|
|
entry = strstr(buf, "remaining capacity:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if (val[0] == 'u')
|
|
|
|
info->remaining_cap = -1;
|
|
|
|
else
|
|
|
|
info->remaining_cap = strtoul(val, NULL, 10);
|
|
|
|
|
|
|
|
/* get current voltage */
|
|
|
|
entry = strstr(buf, "present voltage:");
|
|
|
|
val = get_value(entry);
|
|
|
|
if (val[0] == 'u')
|
|
|
|
info->present_voltage = -1;
|
|
|
|
else
|
|
|
|
info->present_voltage = strtoul(val, NULL, 10);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
int get_battery_info(global_t *globals, int batt_no)
|
|
|
|
{
|
|
|
|
if (data_source == SYSFS_DATA_SOURCE)
|
|
|
|
return sysfs_get_battery_info(globals, batt_no);
|
|
|
|
else
|
|
|
|
return procfs_get_battery_info(globals, batt_no);
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/*
|
|
|
|
* 2003-7-1.
|
|
|
|
* In order to make this code more convenient for things other than
|
|
|
|
* just plain old wmacpi-ng I'm breaking the basic functionality
|
2014-10-05 15:29:59 +00:00
|
|
|
* up into several chunks: collecting and collating info for a
|
|
|
|
* single battery, calculating the global info (such as rtime), and
|
2014-08-18 22:56:10 +00:00
|
|
|
* some stuff to provide a similar interface to now.
|
|
|
|
*/
|
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
/* calculate the percentage remaining, using the values of
|
2014-08-18 22:56:11 +00:00
|
|
|
* remaining capacity and last full capacity, as outlined in
|
|
|
|
* the ACPI spec v2.0a, section 3.9.3. */
|
|
|
|
static int calc_remaining_percentage(int batt)
|
|
|
|
{
|
|
|
|
float rcap, lfcap;
|
|
|
|
battery_t *binfo;
|
|
|
|
int retval;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
binfo = &batteries[batt];
|
|
|
|
|
|
|
|
rcap = (float)binfo->remaining_cap;
|
|
|
|
lfcap = (float)binfo->last_full_cap;
|
|
|
|
|
|
|
|
/* we use -1 to indicate that the value is unknown . . . */
|
|
|
|
if (rcap < 0) {
|
2014-08-18 22:56:14 +00:00
|
|
|
perr("unknown percentage value\n");
|
2014-08-18 22:56:11 +00:00
|
|
|
retval = -1;
|
|
|
|
} else {
|
|
|
|
if (lfcap <= 0)
|
|
|
|
lfcap = 1;
|
|
|
|
retval = (int)((rcap/lfcap) * 100.0);
|
2014-08-18 22:56:14 +00:00
|
|
|
pdebug("percent: %d\n", retval);
|
2014-08-18 22:56:11 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
/* check to see if we've been getting bad data from the batteries - if
|
2014-08-18 22:56:23 +00:00
|
|
|
* we get more than some limit we switch to using the remaining capacity
|
2014-08-18 22:56:22 +00:00
|
|
|
* for the calculations. */
|
|
|
|
static enum rtime_mode check_rt_mode(global_t *globals)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int bad_limit = 5;
|
|
|
|
battery_t *binfo;
|
|
|
|
|
|
|
|
/* if we were told what to do, we should keep doing it */
|
|
|
|
if(globals->rt_forced)
|
|
|
|
return globals->rt_mode;
|
|
|
|
|
|
|
|
for(i = 0; i < MAXBATT; i++) {
|
|
|
|
binfo = &batteries[i];
|
|
|
|
if(binfo->present && globals->adapter.power == BATT) {
|
|
|
|
if(binfo->present_rate <= 0) {
|
|
|
|
pdebug("Bad report from %s\n", binfo->name);
|
|
|
|
binfo->bad_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < MAXBATT; i++) {
|
|
|
|
binfo = &batteries[i];
|
|
|
|
if(binfo->bad_count > bad_limit) {
|
|
|
|
if(globals->rt_mode != RT_CAP)
|
|
|
|
pinfo("More than %d bad reports from %s; "
|
|
|
|
"Switching to remaining capacity mode\n",
|
|
|
|
bad_limit, binfo->name);
|
|
|
|
return RT_CAP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RT_RATE;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
/* calculate remaining time until the battery is charged.
|
2014-10-05 15:29:59 +00:00
|
|
|
* when charging, the battery state file reports the
|
|
|
|
* current being used to charge the battery. We can use
|
2014-08-18 22:56:11 +00:00
|
|
|
* this and the remaining capacity to work out how long
|
|
|
|
* until it reaches the last full capacity of the battery.
|
|
|
|
* XXX: make sure this is actually portable . . . */
|
2014-08-18 22:56:22 +00:00
|
|
|
static int calc_charge_time_rate(int batt)
|
2014-08-18 22:56:10 +00:00
|
|
|
{
|
|
|
|
float rcap, lfcap;
|
2014-08-18 22:56:11 +00:00
|
|
|
battery_t *binfo;
|
|
|
|
int charge_time = 0;
|
|
|
|
|
|
|
|
binfo = &batteries[batt];
|
|
|
|
|
|
|
|
if (binfo->charge_state == CHARGE) {
|
|
|
|
if (binfo->present_rate == -1) {
|
2014-08-18 22:56:14 +00:00
|
|
|
perr("unknown present rate\n");
|
2014-08-18 22:56:11 +00:00
|
|
|
charge_time = -1;
|
|
|
|
} else {
|
|
|
|
lfcap = (float)binfo->last_full_cap;
|
|
|
|
rcap = (float)binfo->remaining_cap;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
charge_time = (int)(((lfcap - rcap)/binfo->present_rate) * 60.0);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (binfo->charge_time)
|
|
|
|
charge_time = 0;
|
|
|
|
return charge_time;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
/* we need to calculate the present rate the same way we do in rt_cap
|
2014-10-05 15:29:59 +00:00
|
|
|
* mode, and then use that to estimate charge time. This will
|
2014-08-18 22:56:22 +00:00
|
|
|
* necessarily be even less accurate than it is for remaining time, but
|
|
|
|
* it's just as neessary . . . */
|
|
|
|
static int calc_charge_time_cap(int batt)
|
|
|
|
{
|
|
|
|
static float cap_samples[CAP_SAMPLES];
|
|
|
|
static int time_samples[CAP_SAMPLES];
|
|
|
|
static int sample_count = 0;
|
|
|
|
static int current = 0;
|
|
|
|
static int old = 1;
|
|
|
|
int rtime;
|
|
|
|
int tdiff;
|
|
|
|
float cdiff;
|
|
|
|
float current_rate;
|
|
|
|
battery_t *binfo = &batteries[batt];
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
cap_samples[current] = (float) binfo->remaining_cap;
|
|
|
|
time_samples[current] = time(NULL);
|
|
|
|
|
|
|
|
if (sample_count == 0) {
|
|
|
|
/* we can't do much if we don't have any data . . . */
|
|
|
|
current_rate = 0;
|
|
|
|
} else if (sample_count < CAP_SAMPLES) {
|
|
|
|
/* if we have less than SAMPLES samples so far, we use the first
|
|
|
|
* sample and the current one */
|
|
|
|
cdiff = cap_samples[current] - cap_samples[0];
|
|
|
|
tdiff = time_samples[current] - time_samples[0];
|
|
|
|
current_rate = cdiff/tdiff;
|
|
|
|
} else {
|
|
|
|
/* if we have more than SAMPLES samples, we use the oldest
|
|
|
|
* current one, which at this point is current + 1. This will
|
|
|
|
* wrap the same way that current will wrap, but one cycle
|
|
|
|
* ahead */
|
|
|
|
cdiff = cap_samples[current] - cap_samples[old];
|
|
|
|
tdiff = time_samples[current] - time_samples[old];
|
|
|
|
current_rate = cdiff/(float)tdiff;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
if (current_rate == 0)
|
2014-08-18 22:56:22 +00:00
|
|
|
rtime = 0;
|
|
|
|
else {
|
|
|
|
float cap_left = (float)(binfo->last_full_cap - binfo->remaining_cap);
|
|
|
|
rtime = (int)(cap_left/(current_rate * 60.0));
|
|
|
|
}
|
|
|
|
sample_count++, current++, old++;
|
|
|
|
if (current >= CAP_SAMPLES)
|
|
|
|
current = 0;
|
|
|
|
if (old >= CAP_SAMPLES)
|
|
|
|
old = 0;
|
|
|
|
|
|
|
|
pdebug("cap charge time rem: %d\n", rtime);
|
|
|
|
return rtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int calc_charge_time(global_t *globals, int batt)
|
|
|
|
{
|
|
|
|
int ctime = 0;
|
|
|
|
|
|
|
|
globals->rt_mode = check_rt_mode(globals);
|
|
|
|
|
|
|
|
switch(globals->rt_mode) {
|
|
|
|
case RT_RATE:
|
|
|
|
ctime = calc_charge_time_rate(batt);
|
|
|
|
break;
|
|
|
|
case RT_CAP:
|
|
|
|
ctime = calc_charge_time_cap(batt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ctime;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
void acquire_batt_info(global_t *globals, int batt)
|
2014-08-18 22:56:11 +00:00
|
|
|
{
|
|
|
|
battery_t *binfo;
|
2014-08-18 22:56:13 +00:00
|
|
|
adapter_t *ap = &globals->adapter;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:28 +00:00
|
|
|
get_battery_info(globals, batt);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
binfo = &batteries[batt];
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
if (!binfo->present) {
|
|
|
|
binfo->percentage = 0;
|
|
|
|
binfo->valid = 0;
|
|
|
|
binfo->charge_time = 0;
|
2014-08-18 22:56:13 +00:00
|
|
|
globals->rtime = 0;
|
2011-03-25 18:45:13 +00:00
|
|
|
return;
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
binfo->percentage = calc_remaining_percentage(batt);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
/* set the battery's capacity state, based (at present) on some
|
2014-08-18 22:56:10 +00:00
|
|
|
* guesstimated values: more than 75% == HIGH, 25% to 75% MED, and
|
2014-08-18 22:56:13 +00:00
|
|
|
* less than 25% is LOW. Less than globals->crit_level is CRIT. */
|
2014-08-18 22:56:11 +00:00
|
|
|
if (binfo->percentage == -1)
|
|
|
|
binfo->state = BS_ERR;
|
2014-08-18 22:56:13 +00:00
|
|
|
if (binfo->percentage < globals->crit_level)
|
2014-08-18 22:56:11 +00:00
|
|
|
binfo->state = CRIT;
|
2014-10-05 15:29:59 +00:00
|
|
|
else if (binfo->percentage > 75)
|
2014-08-18 22:56:10 +00:00
|
|
|
binfo->state = HIGH;
|
|
|
|
else if (binfo->percentage > 25)
|
|
|
|
binfo->state = MED;
|
2014-10-05 15:29:59 +00:00
|
|
|
else
|
2014-08-18 22:56:10 +00:00
|
|
|
binfo->state = LOW;
|
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
/* we need to /know/ that we've got a valid state for the
|
2014-08-18 22:56:13 +00:00
|
|
|
* globals->power value . . . .*/
|
2014-08-18 22:56:21 +00:00
|
|
|
ap->power = get_power_status(globals);
|
2014-08-18 22:56:10 +00:00
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
binfo->charge_time = calc_charge_time(globals, batt);
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* and finally, we tell anyone who wants to use this information
|
|
|
|
* that it's now valid . . .*/
|
|
|
|
binfo->valid = 1;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
void acquire_all_batt_info(global_t *globals)
|
2014-08-18 22:56:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:19 +00:00
|
|
|
for(i = 0; i < globals->battery_count; i++)
|
2014-08-18 22:56:21 +00:00
|
|
|
acquire_batt_info(globals, i);
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
/*
|
|
|
|
* One of the feature requests I've had is for some way to deal with
|
|
|
|
* batteries that are too dumb or too b0rken to report a present rate
|
|
|
|
* value. The way to do this, obviously, is to record the time that
|
|
|
|
* samples were taken and use that information to calculate the rate
|
|
|
|
* at which the battery is draining/charging. This still won't help
|
|
|
|
* systems where the battery doesn't even report the remaining
|
|
|
|
* capacity, but without the present rate or the remaining capacity, I
|
|
|
|
* don't think there's /anything/ we can do to work around it.
|
|
|
|
*
|
|
|
|
* So, what we need to do is provide a way to use a different method
|
|
|
|
* to calculate the time remaining. What seems most sensible is to
|
|
|
|
* split out the code to calculate it into a seperate function, and
|
2014-10-05 15:29:59 +00:00
|
|
|
* then provide multiple implementations . . .
|
2014-08-18 22:56:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the default implementation - if present rate and remaining capacity
|
|
|
|
* are both reported correctly, we use them.
|
|
|
|
*/
|
|
|
|
int calc_time_remaining_rate(global_t *globals)
|
2014-08-18 22:56:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rtime;
|
|
|
|
float rcap = 0;
|
|
|
|
float rate = 0;
|
2014-08-18 22:56:11 +00:00
|
|
|
battery_t *binfo;
|
2014-08-18 22:56:10 +00:00
|
|
|
static float rate_samples[SAMPLES];
|
|
|
|
static int sample_count = 0;
|
2014-08-18 22:56:21 +00:00
|
|
|
static int j = 0;
|
2014-08-18 22:56:10 +00:00
|
|
|
static int n = 0;
|
|
|
|
|
2014-10-05 15:29:59 +00:00
|
|
|
/* calculate the time remaining, using the battery's remaining
|
|
|
|
* capacity and the reported burn rate (3.9.3).
|
|
|
|
* For added accuracy, we average the value over the last
|
2014-08-18 22:56:10 +00:00
|
|
|
* SAMPLES number of calls, or for anything less than this we
|
|
|
|
* simply report the raw number. */
|
2014-10-05 15:29:59 +00:00
|
|
|
/* XXX: this needs to correctly handle the case where
|
2014-08-18 22:56:11 +00:00
|
|
|
* any of the values used is unknown (which we flag using
|
|
|
|
* -1). */
|
2014-08-18 22:56:19 +00:00
|
|
|
for (i = 0; i < globals->battery_count; i++) {
|
2014-08-18 22:56:11 +00:00
|
|
|
binfo = &batteries[i];
|
|
|
|
if (binfo->present && binfo->valid) {
|
|
|
|
rcap += (float)binfo->remaining_cap;
|
|
|
|
rate += (float)binfo->present_rate;
|
2011-03-25 18:45:13 +00:00
|
|
|
}
|
2014-08-18 22:56:11 +00:00
|
|
|
}
|
|
|
|
rate_samples[j] = rate;
|
|
|
|
j++, sample_count++;
|
2014-08-18 22:56:21 +00:00
|
|
|
if (j >= SAMPLES)
|
|
|
|
j = 0;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
/* for the first SAMPLES number of calls we calculate the
|
|
|
|
* average based on sample_count, then we use SAMPLES to
|
|
|
|
* calculate the rolling average. */
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
/* when this fails, n should be equal to SAMPLES. */
|
|
|
|
if (sample_count < SAMPLES)
|
|
|
|
n++;
|
|
|
|
for (i = 0, rate = 0; i < n; i++) {
|
2014-10-05 15:29:59 +00:00
|
|
|
/* if any of our samples are invalid, we drop
|
2014-08-18 22:56:11 +00:00
|
|
|
* straight out, and flag our unknown values. */
|
|
|
|
if (rate_samples[i] < 0) {
|
|
|
|
rate = -1;
|
|
|
|
rtime = -1;
|
2014-08-18 22:56:10 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-08-18 22:56:11 +00:00
|
|
|
rate += rate_samples[i];
|
|
|
|
}
|
|
|
|
rate = rate/(float)n;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-08-18 22:56:11 +00:00
|
|
|
if ((rcap < 1) || (rate < 1)) {
|
|
|
|
rtime = 0;
|
|
|
|
goto out;
|
2011-03-25 18:45:13 +00:00
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
if (rate <= 0)
|
2014-08-18 22:56:11 +00:00
|
|
|
rate = 1;
|
|
|
|
/* time remaining in minutes */
|
|
|
|
rtime = (int)((rcap/rate) * 60.0);
|
2014-10-05 15:29:59 +00:00
|
|
|
if(rtime <= 0)
|
2014-08-18 22:56:11 +00:00
|
|
|
rtime = 0;
|
|
|
|
out:
|
2014-08-18 22:56:22 +00:00
|
|
|
pdebug("discharge time rem: %d\n", rtime);
|
2014-08-18 22:56:21 +00:00
|
|
|
return rtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the alternative implementation - record the time at which each
|
|
|
|
* sample was taken, and then use the difference between the latest
|
|
|
|
* sample and the one SAMPLES ago to calculate the difference over
|
|
|
|
* that time, and from there the rate of change of capacity.
|
|
|
|
*
|
|
|
|
* XXX: this code sucks, but largely because batteries aren't exactly
|
|
|
|
* precision instruments - mine only report with about 70mAH
|
|
|
|
* resolution, so they don't report any changes until the difference
|
|
|
|
* is 70mAH. This means that calculating the current rate from the
|
2014-10-05 15:29:59 +00:00
|
|
|
* remaining capacity is very choppy . . .
|
2014-08-18 22:56:21 +00:00
|
|
|
*
|
|
|
|
* To fix this, we should calculate an average over some number of
|
|
|
|
* samples at the old end of the set - this would smooth out the
|
2014-10-05 15:29:59 +00:00
|
|
|
* transitions.
|
2014-08-18 22:56:21 +00:00
|
|
|
*/
|
|
|
|
int calc_time_remaining_cap(global_t *globals)
|
|
|
|
{
|
2014-08-18 22:56:22 +00:00
|
|
|
static float cap_samples[CAP_SAMPLES];
|
|
|
|
static int time_samples[CAP_SAMPLES];
|
2014-08-18 22:56:21 +00:00
|
|
|
static int sample_count = 0;
|
|
|
|
static int current = 0;
|
|
|
|
static int old = 1;
|
|
|
|
battery_t *binfo;
|
|
|
|
int i;
|
|
|
|
int rtime;
|
|
|
|
int tdiff;
|
|
|
|
float cdiff;
|
|
|
|
float cap = 0;
|
|
|
|
float current_rate;
|
|
|
|
|
|
|
|
for (i = 0; i < globals->battery_count; i++) {
|
|
|
|
binfo = &batteries[i];
|
|
|
|
if (binfo->present && binfo->valid)
|
|
|
|
cap += binfo->remaining_cap;
|
|
|
|
}
|
|
|
|
cap_samples[current] = cap;
|
|
|
|
time_samples[current] = time(NULL);
|
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
if (sample_count == 0) {
|
|
|
|
/* we can't do much if we don't have any data . . . */
|
|
|
|
current_rate = 0;
|
|
|
|
} else if (sample_count < CAP_SAMPLES) {
|
|
|
|
/* if we have less than SAMPLES samples so far, we use the first
|
|
|
|
* sample and the current one */
|
2014-08-18 22:56:21 +00:00
|
|
|
cdiff = cap_samples[0] - cap_samples[current];
|
|
|
|
tdiff = time_samples[current] - time_samples[0];
|
|
|
|
current_rate = cdiff/tdiff;
|
|
|
|
} else {
|
|
|
|
/* if we have more than SAMPLES samples, we use the oldest
|
|
|
|
* current one, which at this point is current + 1. This will
|
|
|
|
* wrap the same way that current will wrap, but one cycle
|
|
|
|
* ahead */
|
|
|
|
cdiff = cap_samples[old] - cap_samples[current];
|
|
|
|
tdiff = time_samples[current] - time_samples[old];
|
|
|
|
current_rate = cdiff/tdiff;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
if (current_rate == 0)
|
2014-08-18 22:56:21 +00:00
|
|
|
rtime = 0;
|
|
|
|
else
|
|
|
|
rtime = (int)(cap_samples[current]/(current_rate * 60.0));
|
|
|
|
|
|
|
|
sample_count++, current++, old++;
|
2014-08-18 22:56:22 +00:00
|
|
|
if (current >= CAP_SAMPLES)
|
2014-08-18 22:56:21 +00:00
|
|
|
current = 0;
|
2014-08-18 22:56:22 +00:00
|
|
|
if (old >= CAP_SAMPLES)
|
2014-08-18 22:56:21 +00:00
|
|
|
old = 0;
|
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
pdebug("cap discharge time rem: %d\n", rtime);
|
2014-08-18 22:56:21 +00:00
|
|
|
return rtime;
|
2014-10-05 15:29:59 +00:00
|
|
|
}
|
2014-08-18 22:56:21 +00:00
|
|
|
|
|
|
|
void acquire_global_info(global_t *globals)
|
|
|
|
{
|
|
|
|
adapter_t *ap = &globals->adapter;
|
|
|
|
|
2014-08-18 22:56:22 +00:00
|
|
|
globals->rt_mode = check_rt_mode(globals);
|
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
switch(globals->rt_mode) {
|
|
|
|
case RT_RATE:
|
|
|
|
globals->rtime = calc_time_remaining_rate(globals);
|
|
|
|
break;
|
|
|
|
case RT_CAP:
|
|
|
|
globals->rtime = calc_time_remaining_cap(globals);
|
|
|
|
break;
|
|
|
|
}
|
2011-03-25 18:45:13 +00:00
|
|
|
|
2014-08-18 22:56:10 +00:00
|
|
|
/* get the power status.
|
|
|
|
* note that this is actually reported seperately from the
|
|
|
|
* battery info, under /proc/acpi/ac_adapter/AC/state */
|
2014-08-18 22:56:21 +00:00
|
|
|
ap->power = get_power_status(globals);
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
void acquire_all_info(global_t *globals)
|
2014-08-18 22:56:10 +00:00
|
|
|
{
|
2014-08-18 22:56:21 +00:00
|
|
|
acquire_all_batt_info(globals);
|
|
|
|
acquire_global_info(globals);
|
2011-03-25 18:45:13 +00:00
|
|
|
}
|