Update battery API to use NAN on error

This commit is contained in:
Benny Baumann 2020-09-07 11:52:42 +02:00 committed by cgzones
parent f805093589
commit 47e2cefe02
8 changed files with 29 additions and 20 deletions

View File

@ -15,6 +15,7 @@ This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
#include "StringUtils.h" #include "StringUtils.h"
#include "Platform.h" #include "Platform.h"
#include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -29,8 +30,8 @@ static void BatteryMeter_updateValues(Meter * this, char *buffer, int len) {
Battery_getData(&percent, &isOnAC); Battery_getData(&percent, &isOnAC);
if (percent == -1) { if (isnan(percent)) {
this->values[0] = 0; this->values[0] = NAN;
xSnprintf(buffer, len, "n/a"); xSnprintf(buffer, len, "n/a");
return; return;
} }

View File

@ -1,6 +1,8 @@
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include <math.h>
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFString.h> #include <CoreFoundation/CFString.h>
#include <IOKit/ps/IOPowerSources.h> #include <IOKit/ps/IOPowerSources.h>
@ -9,7 +11,7 @@
void Battery_getData(double* level, ACPresence* isOnAC) { void Battery_getData(double* level, ACPresence* isOnAC) {
CFTypeRef power_sources = IOPSCopyPowerSourcesInfo(); CFTypeRef power_sources = IOPSCopyPowerSourcesInfo();
*level = -1; *level = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
if(NULL == power_sources) { if(NULL == power_sources) {

View File

@ -7,13 +7,14 @@ in the source distribution for its full text.
*/ */
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include <math.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
void Battery_getData(double* level, ACPresence* isOnAC) { void Battery_getData(double* level, ACPresence* isOnAC) {
int life; int life;
size_t life_len = sizeof(life); size_t life_len = sizeof(life);
if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1) if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
*level = -1; *level = NAN;
else else
*level = life; *level = life;

View File

@ -6,13 +6,14 @@ in the source distribution for its full text.
*/ */
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include <math.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
void Battery_getData(double* level, ACPresence* isOnAC) { void Battery_getData(double* level, ACPresence* isOnAC) {
int life; int life;
size_t life_len = sizeof(life); size_t life_len = sizeof(life);
if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1) if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
*level = -1; *level = NAN;
else else
*level = life; *level = life;

View File

@ -17,6 +17,7 @@ Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#include <math.h>
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "StringUtils.h" #include "StringUtils.h"
@ -140,18 +141,18 @@ static ACPresence procAcpiCheck(void) {
static double Battery_getProcBatData(void) { static double Battery_getProcBatData(void) {
const unsigned long int totalFull = parseBatInfo("info", 3, 4); const unsigned long int totalFull = parseBatInfo("info", 3, 4);
if (totalFull == 0) if (totalFull == 0)
return 0; return NAN;
const unsigned long int totalRemain = parseBatInfo("state", 5, 3); const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
if (totalRemain == 0) if (totalRemain == 0)
return 0; return NAN;
return totalRemain * 100.0 / (double) totalFull; return totalRemain * 100.0 / (double) totalFull;
} }
static void Battery_getProcData(double* level, ACPresence* isOnAC) { static void Battery_getProcData(double* level, ACPresence* isOnAC) {
*level = Battery_getProcBatData();
*isOnAC = procAcpiCheck(); *isOnAC = procAcpiCheck();
*level = AC_ERROR != *isOnAC ? Battery_getProcBatData() : NAN;
} }
// ---------------------------------------- // ----------------------------------------
@ -176,7 +177,7 @@ static inline ssize_t xread(int fd, void *buf, size_t count) {
static void Battery_getSysData(double* level, ACPresence* isOnAC) { static void Battery_getSysData(double* level, ACPresence* isOnAC) {
*level = 0; *level = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
DIR *dir = opendir(SYS_POWERSUPPLY_DIR); DIR *dir = opendir(SYS_POWERSUPPLY_DIR);
@ -279,13 +280,14 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
} }
} }
closedir(dir); closedir(dir);
*level = totalFull > 0 ? ((double) totalRemain * 100) / (double) totalFull : 0;
*level = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN;
} }
static enum { BAT_PROC, BAT_SYS, BAT_ERR } Battery_method = BAT_PROC; static enum { BAT_PROC, BAT_SYS, BAT_ERR } Battery_method = BAT_PROC;
static time_t Battery_cacheTime = 0; static time_t Battery_cacheTime = 0;
static double Battery_cacheLevel = 0; static double Battery_cacheLevel = NAN;
static ACPresence Battery_cacheIsOnAC = 0; static ACPresence Battery_cacheIsOnAC = 0;
void Battery_getData(double* level, ACPresence* isOnAC) { void Battery_getData(double* level, ACPresence* isOnAC) {
@ -299,22 +301,21 @@ void Battery_getData(double* level, ACPresence* isOnAC) {
if (Battery_method == BAT_PROC) { if (Battery_method == BAT_PROC) {
Battery_getProcData(level, isOnAC); Battery_getProcData(level, isOnAC);
if (*level == 0) { if (isnan(*level)) {
Battery_method = BAT_SYS; Battery_method = BAT_SYS;
} }
} }
if (Battery_method == BAT_SYS) { if (Battery_method == BAT_SYS) {
Battery_getSysData(level, isOnAC); Battery_getSysData(level, isOnAC);
if (*level == 0) { if (isnan(*level)) {
Battery_method = BAT_ERR; Battery_method = BAT_ERR;
} }
} }
if (Battery_method == BAT_ERR) { if (Battery_method == BAT_ERR) {
*level = -1; *level = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
} } else {
if (*level > 100.0) { CLAMP(*level, 0.0, 100.0);
*level = 100.0;
} }
Battery_cacheLevel = *level; Battery_cacheLevel = *level;
Battery_cacheIsOnAC = *isOnAC; Battery_cacheIsOnAC = *isOnAC;

View File

@ -10,6 +10,7 @@ in the source distribution for its full text.
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/sensors.h> #include <sys/sensors.h>
#include <errno.h> #include <errno.h>
#include <math.h>
#include <string.h> #include <string.h>
static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, size_t* sdlen) { static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, size_t* sdlen) {
@ -36,7 +37,7 @@ void Battery_getData(double* level, ACPresence* isOnAC) {
bool found = findDevice("acpibat0", mib, &snsrdev, &sdlen); bool found = findDevice("acpibat0", mib, &snsrdev, &sdlen);
*level = -1; *level = NAN;
if (found) { if (found) {
/* last full capacity */ /* last full capacity */
mib[3] = 7; mib[3] = 7;

View File

@ -1,7 +1,8 @@
#include <math.h>
#include "BatteryMeter.h" #include "BatteryMeter.h"
void Battery_getData(double* level, ACPresence* isOnAC) { void Battery_getData(double* level, ACPresence* isOnAC) {
*level = -1; *level = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
} }

View File

@ -1,7 +1,8 @@
#include <math.h>
#include "BatteryMeter.h" #include "BatteryMeter.h"
void Battery_getData(double* level, ACPresence* isOnAC) { void Battery_getData(double* level, ACPresence* isOnAC) {
*level = -1; *level = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
} }