summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenny Baumann <BenBE@geshi.org>2020-09-07 11:52:42 +0200
committercgzones <cgzones@googlemail.com>2020-09-24 18:06:36 +0200
commit47e2cefe02dffe00369e5630eb1e3f54174f20af (patch)
treef517c3f21b226cda4520ed27686af7130f2d0531
parentf80509358954a39234b136466c4c6a77187744e9 (diff)
Update battery API to use NAN on error
-rw-r--r--BatteryMeter.c5
-rw-r--r--darwin/Battery.c4
-rw-r--r--dragonflybsd/Battery.c3
-rw-r--r--freebsd/Battery.c3
-rw-r--r--linux/Battery.c25
-rw-r--r--openbsd/Battery.c3
-rw-r--r--solaris/Battery.c3
-rw-r--r--unsupported/Battery.c3
8 files changed, 29 insertions, 20 deletions
diff --git a/BatteryMeter.c b/BatteryMeter.c
index 859df9d7..0ae2834c 100644
--- a/BatteryMeter.c
+++ b/BatteryMeter.c
@@ -15,6 +15,7 @@ This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
#include "StringUtils.h"
#include "Platform.h"
+#include <math.h>
#include <string.h>
#include <stdlib.h>
@@ -29,8 +30,8 @@ static void BatteryMeter_updateValues(Meter * this, char *buffer, int len) {
Battery_getData(&percent, &isOnAC);
- if (percent == -1) {
- this->values[0] = 0;
+ if (isnan(percent)) {
+ this->values[0] = NAN;
xSnprintf(buffer, len, "n/a");
return;
}
diff --git a/darwin/Battery.c b/darwin/Battery.c
index d52a5954..51d49c6d 100644
--- a/darwin/Battery.c
+++ b/darwin/Battery.c
@@ -1,6 +1,8 @@
#include "BatteryMeter.h"
+#include <math.h>
+
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFString.h>
#include <IOKit/ps/IOPowerSources.h>
@@ -9,7 +11,7 @@
void Battery_getData(double* level, ACPresence* isOnAC) {
CFTypeRef power_sources = IOPSCopyPowerSourcesInfo();
- *level = -1;
+ *level = NAN;
*isOnAC = AC_ERROR;
if(NULL == power_sources) {
diff --git a/dragonflybsd/Battery.c b/dragonflybsd/Battery.c
index f17efb5d..58960429 100644
--- a/dragonflybsd/Battery.c
+++ b/dragonflybsd/Battery.c
@@ -7,13 +7,14 @@ in the source distribution for its full text.
*/
#include "BatteryMeter.h"
+#include <math.h>
#include <sys/sysctl.h>
void Battery_getData(double* level, ACPresence* isOnAC) {
int life;
size_t life_len = sizeof(life);
if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
- *level = -1;
+ *level = NAN;
else
*level = life;
diff --git a/freebsd/Battery.c b/freebsd/Battery.c
index b8c5e312..009f7abe 100644
--- a/freebsd/Battery.c
+++ b/freebsd/Battery.c
@@ -6,13 +6,14 @@ in the source distribution for its full text.
*/
#include "BatteryMeter.h"
+#include <math.h>
#include <sys/sysctl.h>
void Battery_getData(double* level, ACPresence* isOnAC) {
int life;
size_t life_len = sizeof(life);
if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
- *level = -1;
+ *level = NAN;
else
*level = life;
diff --git a/linux/Battery.c b/linux/Battery.c
index 1930880d..f92a3176 100644
--- a/linux/Battery.c
+++ b/linux/Battery.c
@@ -17,6 +17,7 @@ Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat
#include <string.h>
#include <fcntl.h>
#include <time.h>
+#include <math.h>
#include "BatteryMeter.h"
#include "StringUtils.h"
@@ -140,18 +141,18 @@ static ACPresence procAcpiCheck(void) {
static double Battery_getProcBatData(void) {
const unsigned long int totalFull = parseBatInfo("info", 3, 4);
if (totalFull == 0)
- return 0;
+ return NAN;
const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
if (totalRemain == 0)
- return 0;
+ return NAN;
return totalRemain * 100.0 / (double) totalFull;
}
static void Battery_getProcData(double* level, ACPresence* isOnAC) {
- *level = Battery_getProcBatData();
*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) {
- *level = 0;
+ *level = NAN;
*isOnAC = AC_ERROR;
DIR *dir = opendir(SYS_POWERSUPPLY_DIR);
@@ -279,13 +280,14 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
}
}
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 time_t Battery_cacheTime = 0;
-static double Battery_cacheLevel = 0;
+static double Battery_cacheLevel = NAN;
static ACPresence Battery_cacheIsOnAC = 0;
void Battery_getData(double* level, ACPresence* isOnAC) {
@@ -299,22 +301,21 @@ void Battery_getData(double* level, ACPresence* isOnAC) {
if (Battery_method == BAT_PROC) {
Battery_getProcData(level, isOnAC);
- if (*level == 0) {
+ if (isnan(*level)) {
Battery_method = BAT_SYS;
}
}
if (Battery_method == BAT_SYS) {
Battery_getSysData(level, isOnAC);
- if (*level == 0) {
+ if (isnan(*level)) {
Battery_method = BAT_ERR;
}
}
if (Battery_method == BAT_ERR) {
- *level = -1;
+ *level = NAN;
*isOnAC = AC_ERROR;
- }
- if (*level > 100.0) {
- *level = 100.0;
+ } else {
+ CLAMP(*level, 0.0, 100.0);
}
Battery_cacheLevel = *level;
Battery_cacheIsOnAC = *isOnAC;
diff --git a/openbsd/Battery.c b/openbsd/Battery.c
index c215e418..66e4b631 100644
--- a/openbsd/Battery.c
+++ b/openbsd/Battery.c
@@ -10,6 +10,7 @@ in the source distribution for its full text.
#include <sys/sysctl.h>
#include <sys/sensors.h>
#include <errno.h>
+#include <math.h>
#include <string.h>
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);
- *level = -1;
+ *level = NAN;
if (found) {
/* last full capacity */
mib[3] = 7;
diff --git a/solaris/Battery.c b/solaris/Battery.c
index 080cf540..121d2e49 100644
--- a/solaris/Battery.c
+++ b/solaris/Battery.c
@@ -1,7 +1,8 @@
+#include <math.h>
#include "BatteryMeter.h"
void Battery_getData(double* level, ACPresence* isOnAC) {
- *level = -1;
+ *level = NAN;
*isOnAC = AC_ERROR;
}
diff --git a/unsupported/Battery.c b/unsupported/Battery.c
index 080cf540..121d2e49 100644
--- a/unsupported/Battery.c
+++ b/unsupported/Battery.c
@@ -1,7 +1,8 @@
+#include <math.h>
#include "BatteryMeter.h"
void Battery_getData(double* level, ACPresence* isOnAC) {
- *level = -1;
+ *level = NAN;
*isOnAC = AC_ERROR;
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy