[gpm] bug to fix mAh -> uWh



I'm getting more and more people falling for the mAh to uWh bug in HAL
which manifests itself in many odd ways:

http://bugzilla.gnome.org/show_bug.cgi?id=348234
http://bugzilla.gnome.org/show_bug.cgi?id=348998
http://bugzilla.gnome.org/show_bug.cgi?id=338281

Basically, for those not following all the gory details, acpi sends the
rate normally in mWh or sometimes mAh.

We can convert the mAh reading (battery.reporting.rate) into mWh
(battery.charge_level.rate) by multiplying it by the voltage (in Volts),
but in current HAL cvs we are multiplying it by the voltage in mV, and
so the rate information is a factor of 3 too large. This *really* breaks
calculating the time for multibattery laptops, and doing snazzy things
like that.

Note, we can't always convert into mWh for stuff like UPS (so it's not a
standard unit as far as HAL is concerned) but at the moment we are
advertising that we have converted it into mWh and in fact we have
converted it into uWh.

The attached patch has been tested to fix the various issues by no fewer
than 7 people, either on IRC or in person (just at GUADEC this fixed two
people's laptops that I bumped into).

The patch has also been unconditionally compiled into my utopia RPMs for
the last couple of months and I've had no complaints.

Good to commit?

Richard.

Index: hald/linux2/acpi.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux2/acpi.c,v
retrieving revision 1.50
diff -u -r1.50 acpi.c
--- hald/linux2/acpi.c	27 Apr 2006 21:47:34 -0000	1.50
+++ hald/linux2/acpi.c	8 May 2006 16:44:04 -0000
@@ -152,13 +152,13 @@
 		voltage = hal_device_property_get_int (d, "battery.voltage.current");
 
 		/* Just in case we don't get design voltage information, then
-		 * this will pretend that we have 1mV.  This degrades our
+		 * this will pretend that we have 1V.  This degrades our
 		 * ability to report accurate times on multi-battery systems
 		 * but will always prevent negative charge levels and allow
 		 * accurate reporting on single-battery systems.
 		 */
 		if (design_voltage <= 0)
-			design_voltage = 1;
+			design_voltage = 1000; /* mV */
 
 		/* If the current voltage is unknown or greater than design,
 		 * then use design voltage.
@@ -166,9 +166,9 @@
 		if (voltage <= 0 || voltage > design_voltage)
 			voltage = design_voltage;
 
-		normalised_current = reporting_current * voltage;
-		normalised_lastfull = reporting_lastfull * voltage;
-		normalised_rate = reporting_rate * voltage;
+		normalised_current = (reporting_current * voltage) / 1000;
+		normalised_lastfull = (reporting_lastfull * voltage) / 1000;
+		normalised_rate = (reporting_rate * voltage) / 1000;
 	} else {
 		/*
 		 * handle as if mWh, which is the most common case.
@@ -517,21 +517,21 @@
 	} else if (reporting_unit && strcmp (reporting_unit, "mAh") == 0) {
 		voltage_design = hal_device_property_get_int (d, "battery.voltage.design");
 	
-		/* If design voltage is unknown, use 1mV. */
+		/* If design voltage is unknown, use 1V. */
 		if (voltage_design <= 0)
-			voltage_design = 1;
+			voltage_design = 1000; /* mV */;
 
 		/* scale by factor battery.voltage.design */
 		hal_device_property_set_int (d, "battery.charge_level.design",
-			reporting_design * voltage_design);
+			(reporting_design * voltage_design) / 1000);
 		hal_device_property_set_int (d, "battery.charge_level.warning",
-			reporting_warning * voltage_design);
+			(reporting_warning * voltage_design) / 1000);
 		hal_device_property_set_int (d, "battery.charge_level.low",
-			reporting_low * voltage_design);
+			(reporting_low * voltage_design) / 1000);
 		hal_device_property_set_int (d, "battery.charge_level.granularity_1",
-			reporting_gran1 * voltage_design);
+			(reporting_gran1 * voltage_design) / 1000);
 		hal_device_property_set_int (d, "battery.charge_level.granularity_2",
-			reporting_gran2 * voltage_design);
+			(reporting_gran2 * voltage_design) / 1000);
 
 		/* set unit */
 		hal_device_property_set_string (d, "battery.charge_level.unit",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]