[Nut-upsdev] [PATCH/RFC v2 2/3] drivers/main.c: add tests for LB condition

Michal Soltys soltys at ziu.info
Mon Feb 7 13:11:58 UTC 2011


This patch implements the generic test of the following conditions:

battery.charge  < battery.charge.low
OR
battery.runtime < battery.runtime.low

Basing on those, LB can be set in addition to what
driver-specific upsdrv_updateinfo() does.

Signed-off-by: Michal Soltys <soltys at ziu.info>
---
 drivers/main.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/drivers/main.c b/drivers/main.c
index d14f990..8269c3c 100644
--- a/drivers/main.c
+++ b/drivers/main.c
@@ -461,6 +461,74 @@ static void setup_signals(void)
 	sigaction(SIGPIPE, &sa, NULL);
 }
 
+/*
+ *  check battery charge condition (float)
+ */
+static int lb_chk_charge(void)
+{
+	const char *curr, *low;
+	float x, n;
+
+	curr = dstate_getinfo("battery.charge");
+	low = dstate_getinfo("battery.charge.low");
+
+	if (curr && low) {
+		errno = 0;
+		x = strtof(curr, NULL);
+		n = strtof(low, NULL);
+		upsdebugx(4, "lb_chk_charge: %.2f [str: %s] < %.2f [str: %s] ?", x, curr, n, low);
+		if (!errno && x < n)
+			return 1;
+	}
+	return 0;
+}
+
+/*
+ *  check battery runtime condition (integer)
+ */
+static int lb_chk_runtime(void)
+{
+	const char *curr, *low;
+	long int x, n;
+
+	curr = dstate_getinfo("battery.runtime");
+	low = dstate_getinfo("battery.runtime.low");
+
+	if (curr && low) {
+		errno = 0;
+		x = strtol(curr, NULL, 10);
+		n = strtol(low, NULL, 10);
+		upsdebugx(4, "lb_chk_runtime: %ld [str: %s] < %ld [str: %s] ?", x, curr, n, low);
+		if (!errno && x < n)
+			return 1;
+	}
+	return 0;
+}
+
+/*
+ * try to manually assert LB, considering following conditions:
+ *
+ * battery.charge < battery.charge.low
+ * OR
+ * battery.runtime < battery.runtime.low
+ */
+static void test_lb_condition(void)
+{
+	const char *status;
+
+	if (!(lb_chk_charge() || lb_chk_runtime()))
+		return;	/* nothing to do */
+
+	/* get current ups.status */
+	if (!(status = dstate_getinfo("ups.status")))
+		fatalx(EXIT_FAILURE, "test_lb_condition: ups.status not defined ?");
+
+	status_init();
+	status_set(status);
+	status_set_uniq("LB");
+	status_commit();
+}
+
 int main(int argc, char **argv)
 {
 	struct	passwd	*new_uid = NULL;
@@ -627,6 +695,7 @@ int main(int argc, char **argv)
 		timeout.tv_sec += poll_interval;
 
 		upsdrv_updateinfo();
+		test_lb_condition();
 
 		while (!dstate_poll_fds(timeout, extrafd) && !exit_flag) {
 			/* repeat until time is up or extrafd has data */
-- 
1.7.2.1




More information about the Nut-upsdev mailing list