2014-11-27 23:02:52 +00:00
|
|
|
/*
|
|
|
|
htop - linux/Battery.c
|
|
|
|
(C) 2004-2014 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2014-11-27 23:02:52 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
|
|
|
|
Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
|
|
|
|
*/
|
|
|
|
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2020-09-23 11:43:03 +00:00
|
|
|
#include "Battery.h"
|
|
|
|
|
2014-11-27 23:02:52 +00:00
|
|
|
#include <dirent.h>
|
2015-01-23 05:08:21 +00:00
|
|
|
#include <errno.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2014-11-27 23:02:52 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-01-23 05:08:21 +00:00
|
|
|
#include <time.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <unistd.h>
|
2020-10-14 18:21:09 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "BatteryMeter.h"
|
|
|
|
#include "Macros.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.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*));
|
|
|
|
|
|
|
|
while (nBatteries < MAX_BATTERIES) {
|
2017-07-26 18:35:39 +00:00
|
|
|
struct dirent* dirEntry = readdir(batteryDir);
|
|
|
|
if (!dirEntry)
|
2014-11-27 23:02:52 +00:00
|
|
|
break;
|
|
|
|
char* entryName = dirEntry->d_name;
|
2020-10-03 19:20:43 +00:00
|
|
|
if (String_startsWith(entryName, "BAT"))
|
2014-11-27 23:02:52 +00:00
|
|
|
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];
|
2017-07-27 19:07:50 +00:00
|
|
|
xSnprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName);
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
FILE* file = fopen(infoPath, "r");
|
|
|
|
if (!file) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-19 21:55:35 +00:00
|
|
|
char* line = NULL;
|
2020-08-25 10:01:54 +00:00
|
|
|
for (unsigned short int j = 0; j < lineNum; j++) {
|
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);
|
|
|
|
|
2017-05-05 23:25:20 +00:00
|
|
|
if (!line) break;
|
|
|
|
|
2014-11-27 23:02:52 +00:00
|
|
|
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);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2014-11-27 23:02:52 +00:00
|
|
|
total += foundNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nBatteries; i++) {
|
|
|
|
free(batteries[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:28:34 +00:00
|
|
|
static ACPresence procAcpiCheck(void) {
|
2014-11-27 23:02:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2020-09-28 10:17:52 +00:00
|
|
|
struct dirent* dirEntry = readdir(dir);
|
2017-07-26 18:35:39 +00:00
|
|
|
if (!dirEntry)
|
2014-11-27 23:02:52 +00:00
|
|
|
break;
|
|
|
|
|
2020-09-28 10:17:52 +00:00
|
|
|
const char* entryName = dirEntry->d_name;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
if (entryName[0] != 'A')
|
|
|
|
continue;
|
|
|
|
|
2020-08-28 06:57:21 +00:00
|
|
|
char statePath[256];
|
2017-07-27 19:07:50 +00:00
|
|
|
xSnprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName);
|
2014-11-27 23:02:52 +00:00
|
|
|
FILE* file = fopen(statePath, "r");
|
|
|
|
if (!file) {
|
|
|
|
isOn = AC_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-19 21:55:35 +00:00
|
|
|
char* line = String_readLine(file);
|
2014-11-27 23:02:52 +00:00
|
|
|
fclose(file);
|
2018-02-18 13:21:22 +00:00
|
|
|
if (!line) continue;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2020-09-23 12:15:51 +00:00
|
|
|
char *isOnline = String_getToken(line, 2);
|
2016-06-19 21:55:35 +00:00
|
|
|
free(line);
|
2014-11-27 23:02:52 +00:00
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
if (String_eq(isOnline, "on-line")) {
|
2014-11-27 23:02:52 +00:00
|
|
|
isOn = AC_PRESENT;
|
|
|
|
} else {
|
|
|
|
isOn = AC_ABSENT;
|
|
|
|
}
|
2020-09-23 12:15:51 +00:00
|
|
|
free(isOnline);
|
2014-11-27 23:02:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:28:34 +00:00
|
|
|
static double Battery_getProcBatData(void) {
|
2014-11-27 23:02:52 +00:00
|
|
|
const unsigned long int totalFull = parseBatInfo("info", 3, 4);
|
|
|
|
if (totalFull == 0)
|
2020-09-07 09:52:42 +00:00
|
|
|
return NAN;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
|
|
|
|
if (totalRemain == 0)
|
2020-09-07 09:52:42 +00:00
|
|
|
return NAN;
|
2014-11-27 23:02:52 +00:00
|
|
|
|
|
|
|
return totalRemain * 100.0 / (double) totalFull;
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
static void Battery_getProcData(double* level, ACPresence* isOnAC) {
|
|
|
|
*isOnAC = procAcpiCheck();
|
2020-09-07 09:52:42 +00:00
|
|
|
*level = AC_ERROR != *isOnAC ? Battery_getProcBatData() : NAN;
|
2015-01-23 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Battery_getSysData(double* level, ACPresence* isOnAC) {
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-09-07 09:52:42 +00:00
|
|
|
*level = NAN;
|
2015-01-23 05:08:21 +00:00
|
|
|
*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;
|
|
|
|
|
|
|
|
for (;;) {
|
2020-09-28 10:17:52 +00:00
|
|
|
struct dirent* dirEntry = readdir(dir);
|
2017-07-26 18:35:39 +00:00
|
|
|
if (!dirEntry)
|
2014-11-27 23:02:52 +00:00
|
|
|
break;
|
2020-09-28 10:17:52 +00:00
|
|
|
const char* entryName = dirEntry->d_name;
|
2020-09-23 12:15:51 +00:00
|
|
|
char filePath[256];
|
2015-01-23 05:08:21 +00:00
|
|
|
|
2020-09-23 12:15:51 +00:00
|
|
|
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/type", entryName);
|
2020-08-25 10:01:54 +00:00
|
|
|
int fd1 = open(filePath, O_RDONLY);
|
|
|
|
if (fd1 == -1)
|
2019-09-07 14:21:04 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
char type[8];
|
2020-08-25 10:01:54 +00:00
|
|
|
ssize_t typelen = xread(fd1, type, 7);
|
|
|
|
close(fd1);
|
2019-09-07 14:21:04 +00:00
|
|
|
if (typelen < 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (type[0] == 'B' && type[1] == 'a' && type[2] == 't') {
|
2020-09-23 12:15:51 +00:00
|
|
|
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
|
2020-08-25 10:01:54 +00:00
|
|
|
int fd2 = open(filePath, O_RDONLY);
|
|
|
|
if (fd2 == -1) {
|
2015-01-23 05:08:21 +00:00
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buffer[1024];
|
2020-08-25 10:01:54 +00:00
|
|
|
ssize_t buflen = xread(fd2, buffer, 1023);
|
|
|
|
close(fd2);
|
2015-01-23 05:08:21 +00:00
|
|
|
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) {
|
2016-08-11 02:49:35 +00:00
|
|
|
#define match(str,prefix) \
|
|
|
|
(String_startsWith(str,prefix) ? (str) + strlen(prefix) : NULL)
|
2015-01-23 05:08:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 02:49:35 +00:00
|
|
|
#undef match
|
2015-01-23 05:08:21 +00:00
|
|
|
} else if (entryName[0] == 'A') {
|
|
|
|
if (*isOnAC != AC_ERROR) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-09-23 12:15:51 +00:00
|
|
|
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
|
2020-08-25 10:01:54 +00:00
|
|
|
int fd3 = open(filePath, O_RDONLY);
|
|
|
|
if (fd3 == -1) {
|
2015-01-23 05:08:21 +00:00
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buffer[2] = "";
|
|
|
|
for(;;) {
|
2020-08-25 10:01:54 +00:00
|
|
|
ssize_t res = read(fd3, buffer, 1);
|
2015-01-23 05:08:21 +00:00
|
|
|
if (res == -1 && errno == EINTR) continue;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-25 10:01:54 +00:00
|
|
|
close(fd3);
|
2015-01-23 05:08:21 +00:00
|
|
|
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);
|
2020-09-07 09:52:42 +00:00
|
|
|
|
|
|
|
*level = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN;
|
2015-01-23 05:08:21 +00:00
|
|
|
}
|
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;
|
2020-09-07 09:52:42 +00:00
|
|
|
static double Battery_cacheLevel = NAN;
|
2015-01-23 05:08:21 +00:00
|
|
|
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);
|
2020-09-07 09:52:42 +00:00
|
|
|
if (isnan(*level)) {
|
2015-01-23 05:08:21 +00:00
|
|
|
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);
|
2020-09-07 09:52:42 +00:00
|
|
|
if (isnan(*level)) {
|
2015-01-23 05:08:21 +00:00
|
|
|
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) {
|
2020-09-07 09:52:42 +00:00
|
|
|
*level = NAN;
|
2015-01-23 05:08:21 +00:00
|
|
|
*isOnAC = AC_ERROR;
|
2020-09-07 09:52:42 +00:00
|
|
|
} else {
|
2020-09-21 16:50:15 +00:00
|
|
|
*level = CLAMP(*level, 0.0, 100.0);
|
2017-02-16 00:47:03 +00:00
|
|
|
}
|
2015-01-23 05:08:21 +00:00
|
|
|
Battery_cacheLevel = *level;
|
|
|
|
Battery_cacheIsOnAC = *isOnAC;
|
|
|
|
Battery_cacheTime = now;
|
2014-11-27 23:02:52 +00:00
|
|
|
}
|