2014-11-27 23:02:52 +00:00
|
|
|
/*
|
|
|
|
htop - linux/Battery.c
|
|
|
|
(C) 2004-2014 Hisham H. Muhammad
|
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
|
|
|
|
Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
#include <dirent.h>
|
2015-01-23 05:08:21 +00:00
|
|
|
#include <errno.h>
|
2014-11-27 23:02:52 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-01-23 05:08:21 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
2014-11-27 23:02:52 +00:00
|
|
|
#include "BatteryMeter.h"
|
2015-08-19 16:43:20 +00:00
|
|
|
#include "StringUtils.h"
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
#define SYS_POWERSUPPLY_DIR "/sys/class/power_supply"
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// READ FROM /proc
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// This implementation reading from from /proc/acpi is really inefficient,
|
|
|
|
// but I think this is on the way out so I did not rewrite it.
|
|
|
|
// The /sys implementation below does things the right way.
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
static unsigned long int parseBatInfo(const char *fileName, const unsigned short int lineNum, const unsigned short int wordNum) {
|
|
|
|
const char batteryPath[] = PROCDIR "/acpi/battery/";
|
|
|
|
DIR* batteryDir = opendir(batteryPath);
|
|
|
|
if (!batteryDir)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#define MAX_BATTERIES 64
|
|
|
|
char* batteries[MAX_BATTERIES];
|
|
|
|
unsigned int nBatteries = 0;
|
|
|
|
memset(batteries, 0, MAX_BATTERIES * sizeof(char*));
|
|
|
|
|
|
|
|
struct dirent result;
|
|
|
|
struct dirent* dirEntry;
|
|
|
|
while (nBatteries < MAX_BATTERIES) {
|
|
|
|
int err = readdir_r(batteryDir, &result, &dirEntry);
|
|
|
|
if (err || !dirEntry)
|
|
|
|
break;
|
|
|
|
char* entryName = dirEntry->d_name;
|
|
|
|
if (strncmp(entryName, "BAT", 3))
|
|
|
|
continue;
|
2016-02-02 14:53:02 +00:00
|
|
|
batteries[nBatteries] = xStrdup(entryName);
|
2014-11-27 23:02:52 +00:00
|
|
|
nBatteries++;
|
|
|
|
}
|
|
|
|
closedir(batteryDir);
|
|
|
|
|
|
|
|
unsigned long int total = 0;
|
|
|
|
for (unsigned int i = 0; i < nBatteries; i++) {
|
|
|
|
char infoPath[30];
|
|
|
|
snprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName);
|
|
|
|
|
|
|
|
FILE* file = fopen(infoPath, "r");
|
|
|
|
if (!file) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-19 21:55:35 +00:00
|
|
|
char* line = NULL;
|
2014-11-27 23:02:52 +00:00
|
|
|
for (unsigned short int i = 0; i < lineNum; i++) {
|
2016-06-19 21:55:35 +00:00
|
|
|
free(line);
|
|
|
|
line = String_readLine(file);
|
|
|
|
if (!line) break;
|
2014-11-27 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
char *foundNumStr = String_getToken(line, wordNum);
|
|
|
|
const unsigned long int foundNum = atoi(foundNumStr);
|
|
|
|
free(foundNumStr);
|
2016-06-19 21:55:35 +00:00
|
|
|
free(line);
|
|
|
|
|
2014-11-27 23:02:52 +00:00
|
|
|
total += foundNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nBatteries; i++) {
|
|
|
|
free(batteries[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ACPresence procAcpiCheck() {
|
|
|
|
ACPresence isOn = AC_ERROR;
|
|
|
|
const char *power_supplyPath = PROCDIR "/acpi/ac_adapter";
|
2015-01-23 05:08:21 +00:00
|
|
|
DIR *dir = opendir(power_supplyPath);
|
|
|
|
if (!dir) {
|
2014-11-27 23:02:52 +00:00
|
|
|
return AC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent result;
|
|
|
|
struct dirent* dirEntry;
|
|
|
|
for (;;) {
|
2015-01-23 05:08:21 +00:00
|
|
|
int err = readdir_r((DIR *) dir, &result, &dirEntry);
|
2014-11-27 23:02:52 +00:00
|
|
|
if (err || !dirEntry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char* entryName = (char *) dirEntry->d_name;
|
|
|
|
|
|
|
|
if (entryName[0] != 'A')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char statePath[50];
|
|
|
|
snprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName);
|
|
|
|
FILE* file = fopen(statePath, "r");
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
isOn = AC_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-19 21:55:35 +00:00
|
|
|
char* line = String_readLine(file);
|
|
|
|
if (!line) continue;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
const char *isOnline = String_getToken(line, 2);
|
2016-06-19 21:55:35 +00:00
|
|
|
free(line);
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
if (strcmp(isOnline, "on-line") == 0) {
|
|
|
|
isOn = AC_PRESENT;
|
|
|
|
} else {
|
|
|
|
isOn = AC_ABSENT;
|
|
|
|
}
|
|
|
|
free((char *) isOnline);
|
|
|
|
if (isOn == AC_PRESENT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
2014-11-27 23:02:52 +00:00
|
|
|
return isOn;
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
static double Battery_getProcBatData() {
|
2014-11-27 23:02:52 +00:00
|
|
|
const unsigned long int totalFull = parseBatInfo("info", 3, 4);
|
|
|
|
if (totalFull == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
|
|
|
|
if (totalRemain == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return totalRemain * 100.0 / (double) totalFull;
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
static void Battery_getProcData(double* level, ACPresence* isOnAC) {
|
|
|
|
*level = Battery_getProcBatData();
|
|
|
|
*isOnAC = procAcpiCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// READ FROM /sys
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
static inline ssize_t xread(int fd, void *buf, size_t count) {
|
|
|
|
// Read some bytes. Retry on EINTR and when we don't get as many bytes as we requested.
|
|
|
|
size_t alreadyRead = 0;
|
|
|
|
for(;;) {
|
|
|
|
ssize_t res = read(fd, buf, count);
|
|
|
|
if (res == -1 && errno == EINTR) continue;
|
|
|
|
if (res > 0) {
|
|
|
|
buf = ((char*)buf)+res;
|
|
|
|
count -= res;
|
|
|
|
alreadyRead += res;
|
|
|
|
}
|
|
|
|
if (res == -1) return -1;
|
|
|
|
if (count == 0 || res == 0) return alreadyRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pointer to the suffix of `str` if its beginning matches `prefix`.
|
|
|
|
* Returns NULL if the prefix does not match.
|
|
|
|
* Examples:
|
|
|
|
* match("hello world", "hello "); -> "world"
|
|
|
|
* match("hello world", "goodbye "); -> NULL
|
|
|
|
*/
|
|
|
|
static inline const char* match(const char* str, const char* prefix) {
|
|
|
|
for (;;) {
|
|
|
|
if (*prefix == '\0') {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
if (*prefix != *str) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
prefix++; str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Battery_getSysData(double* level, ACPresence* isOnAC) {
|
|
|
|
|
|
|
|
*level = 0;
|
|
|
|
*isOnAC = AC_ERROR;
|
|
|
|
|
|
|
|
DIR *dir = opendir(SYS_POWERSUPPLY_DIR);
|
|
|
|
if (!dir)
|
|
|
|
return;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
unsigned long int totalFull = 0;
|
|
|
|
unsigned long int totalRemain = 0;
|
|
|
|
|
|
|
|
struct dirent result;
|
|
|
|
struct dirent* dirEntry;
|
|
|
|
for (;;) {
|
2015-01-23 05:08:21 +00:00
|
|
|
int err = readdir_r((DIR *) dir, &result, &dirEntry);
|
2014-11-27 23:02:52 +00:00
|
|
|
if (err || !dirEntry)
|
|
|
|
break;
|
|
|
|
char* entryName = (char *) dirEntry->d_name;
|
2015-01-23 05:08:21 +00:00
|
|
|
const char filePath[50];
|
|
|
|
|
|
|
|
if (entryName[0] == 'B' && entryName[1] == 'A' && entryName[2] == 'T') {
|
|
|
|
|
|
|
|
snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
|
|
|
|
int fd = open(filePath, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buffer[1024];
|
|
|
|
ssize_t buflen = xread(fd, buffer, 1023);
|
|
|
|
close(fd);
|
|
|
|
if (buflen < 1) {
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer[buflen] = '\0';
|
|
|
|
char *buf = buffer;
|
|
|
|
char *line = NULL;
|
|
|
|
bool full = false;
|
|
|
|
bool now = false;
|
|
|
|
while ((line = strsep(&buf, "\n")) != NULL) {
|
|
|
|
const char* ps = match(line, "POWER_SUPPLY_");
|
|
|
|
if (!ps) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char* energy = match(ps, "ENERGY_");
|
|
|
|
if (!energy) {
|
|
|
|
energy = match(ps, "CHARGE_");
|
|
|
|
}
|
|
|
|
if (!energy) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char* value = (!full) ? match(energy, "FULL=") : NULL;
|
|
|
|
if (value) {
|
|
|
|
totalFull += atoi(value);
|
|
|
|
full = true;
|
|
|
|
if (now) break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
value = (!now) ? match(energy, "NOW=") : NULL;
|
|
|
|
if (value) {
|
|
|
|
totalRemain += atoi(value);
|
|
|
|
now = true;
|
|
|
|
if (full) break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (entryName[0] == 'A') {
|
|
|
|
if (*isOnAC != AC_ERROR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
|
|
|
|
int fd = open(filePath, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buffer[2] = "";
|
|
|
|
for(;;) {
|
|
|
|
ssize_t res = read(fd, buffer, 1);
|
|
|
|
if (res == -1 && errno == EINTR) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if (buffer[0] == '0') {
|
|
|
|
*isOnAC = AC_ABSENT;
|
|
|
|
} else if (buffer[0] == '1') {
|
|
|
|
*isOnAC = AC_PRESENT;
|
|
|
|
}
|
2014-11-27 23:02:52 +00:00
|
|
|
}
|
2015-01-23 05:08:21 +00:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
*level = totalFull > 0 ? ((double) totalRemain * 100) / (double) totalFull : 0;
|
|
|
|
}
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
static enum { BAT_PROC, BAT_SYS, BAT_ERR } Battery_method = BAT_PROC;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
static time_t Battery_cacheTime = 0;
|
|
|
|
static double Battery_cacheLevel = 0;
|
|
|
|
static ACPresence Battery_cacheIsOnAC = 0;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
void Battery_getData(double* level, ACPresence* isOnAC) {
|
|
|
|
time_t now = time(NULL);
|
|
|
|
// update battery reading is slow. Update it each 10 seconds only.
|
|
|
|
if (now < Battery_cacheTime + 10) {
|
|
|
|
*level = Battery_cacheLevel;
|
|
|
|
*isOnAC = Battery_cacheIsOnAC;
|
|
|
|
return;
|
|
|
|
}
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
if (Battery_method == BAT_PROC) {
|
|
|
|
Battery_getProcData(level, isOnAC);
|
|
|
|
if (*level == 0) {
|
|
|
|
Battery_method = BAT_SYS;
|
2014-11-27 23:02:52 +00:00
|
|
|
}
|
2015-01-23 05:08:21 +00:00
|
|
|
}
|
|
|
|
if (Battery_method == BAT_SYS) {
|
|
|
|
Battery_getSysData(level, isOnAC);
|
|
|
|
if (*level == 0) {
|
|
|
|
Battery_method = BAT_ERR;
|
2014-11-27 23:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 05:08:21 +00:00
|
|
|
if (Battery_method == BAT_ERR) {
|
|
|
|
*level = -1;
|
|
|
|
*isOnAC = AC_ERROR;
|
|
|
|
}
|
|
|
|
Battery_cacheLevel = *level;
|
|
|
|
Battery_cacheIsOnAC = *isOnAC;
|
|
|
|
Battery_cacheTime = now;
|
2014-11-27 23:02:52 +00:00
|
|
|
}
|