1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from telemetry.internal.platform.power_monitor import android_power_monitor_base
6
7
8class FuelGaugePowerMonitor(android_power_monitor_base.AndroidPowerMonitorBase):
9  """PowerMonitor that relies on the fuel gauge chips to monitor the power
10  consumption of a android device.
11  """
12  def __init__(self, battery):
13    """Constructor.
14
15    Args:
16        battery: A BatteryUtil instance.
17        platform_backend: A LinuxBasedPlatformBackend instance.
18    """
19    super(FuelGaugePowerMonitor, self).__init__()
20    self._battery = battery
21    self._starting_fuel_gauge = None
22
23  def CanMonitorPower(self):
24    return self._battery.SupportsFuelGauge()
25
26  def StartMonitoringPower(self, browser):
27    self._CheckStart()
28    self._starting_fuel_gauge = self._battery.GetFuelGaugeChargeCounter()
29
30  def StopMonitoringPower(self):
31    self._CheckStop()
32    # Convert from nAh to mAh.
33    fuel_gauge_delta = (
34        float((self._starting_fuel_gauge) -
35        self._battery.GetFuelGaugeChargeCounter()) / 1000000)
36    voltage = self._ParseVoltage(self._battery.GetBatteryInfo().get('voltage'))
37    return self.ProcessPowerData(voltage, fuel_gauge_delta)
38
39  @staticmethod
40  def ProcessPowerData(voltage, fuel_gauge_delta):
41    return {'identifier': 'fuel_gauge',
42            'fuel_gauge_energy_consumption_mwh': fuel_gauge_delta * voltage}
43