Code clean up for reading battery info

This commit is contained in:
aninsen 2021-10-06 16:17:59 +05:30 committed by BenBE
parent 3f9f52fd29
commit 3e70de64b3
1 changed files with 86 additions and 152 deletions

View File

@ -611,133 +611,85 @@ bool Platform_getNetworkIO(NetworkIOData* data) {
// Linux battery reading by Ian P. Hands (iphands@gmail.com, ihands@redhat.com). // Linux battery reading by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
#define MAX_BATTERIES 64
#define PROC_BATTERY_DIR PROCDIR "/acpi/battery" #define PROC_BATTERY_DIR PROCDIR "/acpi/battery"
#define PROC_POWERSUPPLY_DIR PROCDIR "/acpi/ac_adapter" #define PROC_POWERSUPPLY_DIR PROCDIR "/acpi/ac_adapter"
#define PROC_POWERSUPPLY_ACSTATE_FILE PROC_POWERSUPPLY_DIR "/AC/state"
#define SYS_POWERSUPPLY_DIR "/sys/class/power_supply" #define SYS_POWERSUPPLY_DIR "/sys/class/power_supply"
// ---------------------------------------- // ----------------------------------------
// READ FROM /proc // READ FROM /proc
// ---------------------------------------- // ----------------------------------------
static unsigned long int parseBatInfo(const char* fileName, const unsigned short int lineNum, const unsigned short int wordNum) { static double Platform_Battery_getProcBatInfo(void) {
const char batteryPath[] = PROC_BATTERY_DIR; DIR* batteryDir = opendir(PROC_BATTERY_DIR);
DIR* batteryDir = opendir(batteryPath);
if (!batteryDir) if (!batteryDir)
return 0; return NAN;
char* batteries[MAX_BATTERIES]; uint64_t totalFull = 0;
unsigned int nBatteries = 0; uint64_t totalRemain = 0;
memset(batteries, 0, MAX_BATTERIES * sizeof(char*));
while (nBatteries < MAX_BATTERIES) {
const struct dirent* dirEntry = readdir(batteryDir);
if (!dirEntry)
break;
struct dirent* dirEntry = NULL;
while ((dirEntry = readdir(batteryDir))) {
const char* entryName = dirEntry->d_name; const char* entryName = dirEntry->d_name;
if (!String_startsWith(entryName, "BAT")) if (!String_startsWith(entryName, "BAT"))
continue; continue;
batteries[nBatteries] = xStrdup(entryName); char filePath[256];
nBatteries++; char bufInfo[1024] = {0};
} xSnprintf(filePath, sizeof(filePath), "%s/%s/info", PROC_BATTERY_DIR, entryName);
closedir(batteryDir); ssize_t r = xReadfile(filePath, bufInfo, sizeof(bufInfo));
if (r < 0)
continue;
unsigned long int total = 0; char bufState[1024] = {0};
for (unsigned int i = 0; i < nBatteries; i++) { xSnprintf(filePath, sizeof(filePath), "%s/%s/state", PROC_BATTERY_DIR, entryName);
char infoPath[30]; r = xReadfile(filePath, bufState, sizeof(bufState));
xSnprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName); if (r < 0)
continue;
FILE* file = fopen(infoPath, "r"); const char* line;
if (!file)
break;
char* line = NULL; //Getting total charge for all batteries
for (unsigned short int j = 0; j < lineNum; j++) { char* buf = bufInfo;
free(line); while ((line = strsep(&buf, "\n")) != NULL) {
line = String_readLine(file); char field[100] = {0};
if (!line) int val = 0;
if (2 != sscanf(line, "%99[^:]:%d", field, &val))
continue;
if (String_eq(field, "last full capacity")) {
totalFull += val;
break; break;
}
} }
fclose(file); //Getting remaining charge for all batteries
buf = bufState;
while ((line = strsep(&buf, "\n")) != NULL) {
char field[100] = {0};
int val = 0;
if (2 != sscanf(line, "%99[^:]:%d", field, &val))
continue;
if (!line) if (String_eq(field, "remaining capacity")) {
break; totalRemain += val;
break;
char* foundNumStr = String_getToken(line, wordNum); }
const unsigned long int foundNum = atoi(foundNumStr); }
free(foundNumStr);
free(line);
total += foundNum;
} }
for (unsigned int i = 0; i < nBatteries; i++) closedir(batteryDir);
free(batteries[i]);
return total; return totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN;
} }
static ACPresence procAcpiCheck(void) { static ACPresence procAcpiCheck(void) {
ACPresence isOn = AC_ERROR; char buffer[1024] = {0};
const char* power_supplyPath = PROC_POWERSUPPLY_DIR; ssize_t r = xReadfile(PROC_POWERSUPPLY_ACSTATE_FILE, buffer, sizeof(buffer));
DIR* dir = opendir(power_supplyPath); if (r < 1)
if (!dir)
return AC_ERROR; return AC_ERROR;
for (;;) { return String_eq(buffer, "on-line") ? AC_PRESENT : AC_ABSENT;
const struct dirent* dirEntry = readdir(dir);
if (!dirEntry)
break;
const char* entryName = dirEntry->d_name;
if (entryName[0] != 'A')
continue;
char statePath[256];
xSnprintf(statePath, sizeof(statePath), "%s/%s/state", power_supplyPath, entryName);
FILE* file = fopen(statePath, "r");
if (!file) {
isOn = AC_ERROR;
continue;
}
char* line = String_readLine(file);
fclose(file);
if (!line)
continue;
char* isOnline = String_getToken(line, 2);
free(line);
if (String_eq(isOnline, "on-line"))
isOn = AC_PRESENT;
else
isOn = AC_ABSENT;
free(isOnline);
if (isOn == AC_PRESENT)
break;
}
closedir(dir);
return isOn;
}
static double Platform_Battery_getProcBatInfo(void) {
const unsigned long int totalFull = parseBatInfo("info", 3, 4);
if (totalFull == 0)
return NAN;
const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
if (totalRemain == 0)
return NAN;
return totalRemain * 100.0 / (double) totalFull;
} }
static void Platform_Battery_getProcData(double* percent, ACPresence* isOnAC) { static void Platform_Battery_getProcData(double* percent, ACPresence* isOnAC) {
@ -750,7 +702,6 @@ static void Platform_Battery_getProcData(double* percent, ACPresence* isOnAC) {
// ---------------------------------------- // ----------------------------------------
static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) { static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
*percent = NAN; *percent = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
@ -758,68 +709,52 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
if (!dir) if (!dir)
return; return;
unsigned long int totalFull = 0; uint64_t totalFull = 0;
unsigned long int totalRemain = 0; uint64_t totalRemain = 0;
for (;;) {
const struct dirent* dirEntry = readdir(dir);
if (!dirEntry)
break;
struct dirent* dirEntry = NULL;
while ((dirEntry = readdir(dir))) {
const char* entryName = dirEntry->d_name; const char* entryName = dirEntry->d_name;
char filePath[256];
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/type", entryName); if (String_startsWith(entryName, "BAT")) {
char buffer[1024] = {0};
char type[8]; char filePath[256];
ssize_t r = xReadfile(filePath, type, sizeof(type));
if (r < 3)
continue;
if (type[0] == 'B' && type[1] == 'a' && type[2] == 't') {
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName); xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
char buffer[1024]; ssize_t r = xReadfile(filePath, buffer, sizeof(buffer));
r = xReadfile(filePath, buffer, sizeof(buffer)); if (r < 0)
if (r < 0) { continue;
closedir(dir);
return;
}
char* buf = buffer;
const char* line;
bool full = false; bool full = false;
bool now = false; bool now = false;
int fullSize = 0;
double fullCharge = 0;
double capacityLevel = NAN; double capacityLevel = NAN;
const char* line;
#define match(str,prefix) \ char* buf = buffer;
(String_startsWith(str,prefix) ? (str) + strlen(prefix) : NULL)
while ((line = strsep(&buf, "\n")) != NULL) { while ((line = strsep(&buf, "\n")) != NULL) {
const char* ps = match(line, "POWER_SUPPLY_"); char field[100] = {0};
if (!ps) int val = 0;
if (2 != sscanf(line, "POWER_SUPPLY_%99[^=]=%d", field, &val))
continue; continue;
const char* capacity = match(ps, "CAPACITY=");
if (capacity) if (String_eq(field, "CAPACITY")) {
capacityLevel = atoi(capacity) / 100.0; capacityLevel = val / 100.0;
const char* energy = match(ps, "ENERGY_");
if (!energy)
energy = match(ps, "CHARGE_");
if (!energy)
continue; continue;
const char* value = (!full) ? match(energy, "FULL=") : NULL; }
if (value) {
fullSize = atoi(value); if (String_eq(field, "ENERGY_FULL") || String_eq(field, "CHARGE_FULL")) {
totalFull += fullSize; fullCharge = val;
totalFull += fullCharge;
full = true; full = true;
if (now) if (now)
break; break;
continue; continue;
} }
value = (!now) ? match(energy, "NOW=") : NULL;
if (value) { if (String_eq(field, "ENERGY_NOW") || String_eq(field, "CHARGE_NOW")) {
totalRemain += atoi(value); totalRemain += val;
now = true; now = true;
if (full) if (full)
break; break;
@ -827,23 +762,21 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
} }
} }
#undef match
if (!now && full && !isnan(capacityLevel)) if (!now && full && !isnan(capacityLevel))
totalRemain += (capacityLevel * fullSize); totalRemain += capacityLevel * fullCharge;
} else if (entryName[0] == 'A') { } else if (String_eq(entryName, "AC")) {
char buffer[2] = {0};
if (*isOnAC != AC_ERROR) if (*isOnAC != AC_ERROR)
continue; continue;
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName); char filePath[256];
xSnprintf(filePath, sizeof(filePath), SYS_POWERSUPPLY_DIR "/%s/online", entryName);
char buffer[2]; ssize_t r = xReadfile(filePath, buffer, sizeof(buffer));
r = xReadfile(filePath, buffer, sizeof(buffer));
if (r < 1) { if (r < 1) {
closedir(dir); *isOnAC = AC_ERROR;
return; continue;
} }
if (buffer[0] == '0') if (buffer[0] == '0')
@ -852,6 +785,7 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
*isOnAC = AC_PRESENT; *isOnAC = AC_PRESENT;
} }
} }
closedir(dir); closedir(dir);
*percent = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN; *percent = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN;