hal/hald util.c,1.19,1.20 util.h,1.11,1.12

David Zeuthen david at freedesktop.org
Wed Jul 27 18:37:03 PDT 2005


Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv17086/hald

Modified Files:
	util.c util.h 
Log Message:
2005-07-27  David Zeuthen  <davidz at redhat.com>

        Patch from Richard Hughes  <richard at hughsie.com>.

        * hald/util.c (util_compute_percentage_charge): New function used
        to calculate the percentage charge, initially for ACPI batteries,
        but could be used by all devices as contains lots of error checks.

        * hald/linux2/acpi.c (battery_refresh_poll): Call
        util_compute_percentage_charge so we can populate
        battery.charge_level.percentage



Index: util.c
===================================================================
RCS file: /cvs/hal/hal/hald/util.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- util.c	13 Jul 2005 03:38:01 -0000	1.19
+++ util.c	28 Jul 2005 01:37:01 -0000	1.20
@@ -50,6 +50,43 @@
 #include "hald_dbus.h"
 #include "util.h"
 
+/** Given all the required parameters, this function will return the percentage
+ *  charge remaining. There are lots of checks here as ACPI is often broken.
+ *
+ *  @param  id                  Optional ID given to this battery. Unused at present.
+ *  @param  chargeLevel         The current charge level of the battery (typically mWh)
+ *  @param  chargeLastFull      The last "full" charge of the battery (typically mWh)
+ *  @return                     Percentage
+ */
+int 
+util_compute_percentage_charge (const char *id,
+			     int chargeLevel,
+			     int chargeLastFull)
+{
+	int percentage;
+	/* make sure we have chargelevel */
+	if (chargeLevel <= 0) {
+		HAL_WARNING (("chargeLevel %i, returning 0!", chargeLevel));
+		return 0;
+	}
+	/* make sure not division by zero */
+	if (chargeLastFull > 0)
+		percentage = ((double) chargeLevel / (double) chargeLastFull) * 100;
+	else {
+		HAL_WARNING (("chargeLastFull %i, percentage returning 0!", chargeLastFull));
+		return 0;
+	}
+	/* make sure results are sensible */
+	if (percentage < 0) {
+		HAL_WARNING (("Percentage %i, returning 0!", percentage));
+		return 0;
+	} else if (percentage > 100) {
+		HAL_WARNING (("Percentage %i, returning 100!", percentage));
+		return 100;
+	}
+	return percentage;
+}
+
 /** Given all the required parameters, this function will return the number 
  *  of seconds until the battery is charged (if charging) or the number
  *  of seconds until empty (if discharging)

Index: util.h
===================================================================
RCS file: /cvs/hal/hal/hald/util.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- util.h	13 Jul 2005 03:38:01 -0000	1.11
+++ util.h	28 Jul 2005 01:37:01 -0000	1.12
@@ -31,6 +31,8 @@
 
 int util_compute_time_remaining (const char *id, int chargeRate, int chargeLevel, int chargeLastFull, gboolean isDischarging, gboolean isCharging);
 
+int util_compute_percentage_charge (const char *id, int chargeLevel, int chargeLastFull);
+
 gboolean hal_util_remove_trailing_slash (gchar *path);
 
 const gchar *hal_util_get_last_element (const gchar *s);




More information about the hal-commit mailing list