1 From 22da1a0bc5943b683189418d8b0f766e91b2bdbe Mon Sep 17 00:00:00 2001
2 From: Martin Pitt <martinpitt@gnome.org>
3 Date: Tue, 22 Oct 2013 08:02:51 +0000
4 Subject: linux: Clamp percentage for overfull batteries
6 Some batteries report energy > energy_full and a percentage ("capacity"
7 attribute) > 100%. Clamp these within 0 and 100% for both plausibility as well
8 as to avoid setting an out-of-range property which would then become 0%.
10 https://launchpad.net/bugs/1240673
12 diff --git a/src/linux/integration-test b/src/linux/integration-test
13 index 6adc5c4..b7f11a9 100755
14 --- a/src/linux/integration-test
15 +++ b/src/linux/integration-test
16 @@ -458,6 +458,39 @@ class Tests(unittest.TestCase):
17 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)
20 + def test_battery_overfull(self):
21 + '''battery which reports a > 100% percentage for a full battery'''
23 + self.testbed.add_device('power_supply', 'BAT0', None,
27 + 'current_now', '1000',
28 + 'charge_now', '11000000',
29 + 'charge_full', '10000000',
30 + 'charge_full_design', '11000000',
32 + 'voltage_now', '12000000'], [])
35 + devs = self.proxy.EnumerateDevices()
36 + self.assertEqual(len(devs), 1)
39 + # should clamp percentage
40 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 100.0)
41 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)
42 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'),
43 + UP_DEVICE_STATE_FULLY_CHARGED)
44 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 132.0)
45 + # should adjust EnergyFull to reality, not what the battery claims
46 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 132.0)
47 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 132.0)
48 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)
49 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'PowerSupply'), True)
50 + self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Type'), 2)
53 def test_battery_temperature(self):
54 '''battery which reports temperature'''
56 diff --git a/src/linux/up-device-supply.c b/src/linux/up-device-supply.c
57 index fd509c3..977d1b0 100644
58 --- a/src/linux/up-device-supply.c
59 +++ b/src/linux/up-device-supply.c
60 @@ -677,6 +677,10 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply)
61 /* get a precise percentage */
62 if (sysfs_file_exists (native_path, "capacity")) {
63 percentage = sysfs_get_double (native_path, "capacity");
64 + if (percentage < 0.0f)
66 + if (percentage > 100.0f)
67 + percentage = 100.0f;
68 /* for devices which provide capacity, but not {energy,charge}_now */
69 if (energy < 0.1f && energy_full > 0.0f)
70 energy = energy_full * percentage / 100;