Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: build/android/pylib/device/battery_utils.py

Issue 1067853003: [Android] Move power implementation from device_utils to battery_utils (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | build/android/pylib/device/battery_utils_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2015 The Chromium Authors. All rights reserved. 1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Provides a variety of device interactions with power. 5 """Provides a variety of device interactions with power.
6 """ 6 """
7 # pylint: disable=unused-argument 7 # pylint: disable=unused-argument
8 8
9 import collections 9 import collections
10 import contextlib 10 import contextlib
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 returns: 140 returns:
141 Dict of UID and power data. 141 Dict of UID and power data.
142 { 142 {
143 'uid': uid, 143 'uid': uid,
144 'data': [1,2,3] 144 'data': [1,2,3]
145 } 145 }
146 None if the package is not found in the power data. 146 None if the package is not found in the power data.
147 """ 147 """
148 return self.GetPowerData().get(package) 148 return self.GetPowerData().get(package)
149 149
150 # TODO(rnephew): Move implementation from device_utils when this is used.
151 def GetBatteryInfo(self, timeout=None, retries=None): 150 def GetBatteryInfo(self, timeout=None, retries=None):
152 """Gets battery info for the device. 151 """Gets battery info for the device.
153 152
154 Args: 153 Args:
155 timeout: timeout in seconds 154 timeout: timeout in seconds
156 retries: number of retries 155 retries: number of retries
157 Returns: 156 Returns:
158 A dict containing various battery information as reported by dumpsys 157 A dict containing various battery information as reported by dumpsys
159 battery. 158 battery.
160 """ 159 """
161 return self._device.GetBatteryInfo(timeout=None, retries=None) 160 result = {}
161 # Skip the first line, which is just a header.
162 for line in self._device.RunShellCommand(
163 ['dumpsys', 'battery'], check_return=True)[1:]:
164 # If usb charging has been disabled, an extra line of header exists.
165 if 'UPDATES STOPPED' in line:
166 logging.warning('Dumpsys battery not receiving updates. '
167 'Run dumpsys battery reset if this is in error.')
168 elif ':' not in line:
169 logging.warning('Unknown line found in dumpsys battery.')
jbudorick 2015/04/07 16:56:33 nit: combine these two logging lines logging.wa
rnephew (Reviews Here) 2015/04/07 17:06:14 Done.
170 logging.warning(line)
171 else:
172 k, v = line.split(': ', 1)
jbudorick 2015/04/07 16:56:33 Why line.split(': ') when you're checking for ':'
rnephew (Reviews Here) 2015/04/07 17:06:14 Done.
173 result[k.strip()] = v.strip()
174 return result
162 175
163 # TODO(rnephew): Move implementation from device_utils when this is used.
164 def GetCharging(self, timeout=None, retries=None): 176 def GetCharging(self, timeout=None, retries=None):
165 """Gets the charging state of the device. 177 """Gets the charging state of the device.
166 178
167 Args: 179 Args:
168 timeout: timeout in seconds 180 timeout: timeout in seconds
169 retries: number of retries 181 retries: number of retries
170 Returns: 182 Returns:
171 True if the device is charging, false otherwise. 183 True if the device is charging, false otherwise.
172 """ 184 """
173 return self._device.GetCharging(timeout=None, retries=None) 185 battery_info = self.GetBatteryInfo()
186 for k in ('AC powered', 'USB powered', 'Wireless powered'):
187 if (k in battery_info and
188 battery_info[k].lower() in ('true', '1', 'yes')):
189 return True
190 return False
174 191
175 # TODO(rnephew): Move implementation from device_utils when this is used.
176 def SetCharging(self, enabled, timeout=None, retries=None): 192 def SetCharging(self, enabled, timeout=None, retries=None):
177 """Enables or disables charging on the device. 193 """Enables or disables charging on the device.
178 194
179 Args: 195 Args:
180 enabled: A boolean indicating whether charging should be enabled or 196 enabled: A boolean indicating whether charging should be enabled or
181 disabled. 197 disabled.
182 timeout: timeout in seconds 198 timeout: timeout in seconds
183 retries: number of retries 199 retries: number of retries
184 200
185 Raises: 201 Raises:
186 device_errors.CommandFailedError: If method of disabling charging cannot 202 device_errors.CommandFailedError: If method of disabling charging cannot
187 be determined. 203 be determined.
188 """ 204 """
189 self._device.SetCharging(enabled, timeout=None, retries=None) 205 if 'charging_config' not in self._cache:
206 for c in _CONTROL_CHARGING_COMMANDS:
207 if self._device.FileExists(c['witness_file']):
208 self._cache['charging_config'] = c
209 break
210 else:
211 raise device_errors.CommandFailedError(
212 'Unable to find charging commands.')
190 213
191 # TODO(rnephew): Move implementation from device_utils when this is used. 214 if enabled:
215 command = self._cache['charging_config']['enable_command']
216 else:
217 command = self._cache['charging_config']['disable_command']
218
219 def set_and_verify_charging():
220 self._device.RunShellCommand(command, check_return=True)
221 return self.GetCharging() == enabled
222
223 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1)
224
192 # TODO(rnephew): Make private when all use cases can use the context manager. 225 # TODO(rnephew): Make private when all use cases can use the context manager.
193 def DisableBatteryUpdates(self, timeout=None, retries=None): 226 def DisableBatteryUpdates(self, timeout=None, retries=None):
194 """ Resets battery data and makes device appear like it is not 227 """ Resets battery data and makes device appear like it is not
195 charging so that it will collect power data since last charge. 228 charging so that it will collect power data since last charge.
196 229
197 Args: 230 Args:
198 timeout: timeout in seconds 231 timeout: timeout in seconds
199 retries: number of retries 232 retries: number of retries
200 233
201 Raises: 234 Raises:
202 device_errors.CommandFailedError: When resetting batterystats fails to 235 device_errors.CommandFailedError: When resetting batterystats fails to
203 reset power values. 236 reset power values.
204 """ 237 """
205 self._device.DisableBatteryUpdates(timeout=None, retries=None) 238 def battery_updates_disabled():
239 return self.GetCharging() is False
206 240
207 # TODO(rnephew): Move implementation from device_utils when this is used. 241 self._device.RunShellCommand(
242 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True)
243 self._device.RunShellCommand(
244 ['dumpsys', 'batterystats', '--reset'], check_return=True)
245 battery_data = self._device.RunShellCommand(
246 ['dumpsys', 'batterystats', '--charged', '--checkin'],
247 check_return=True)
248 ROW_TYPE_INDEX = 3
249 PWI_POWER_INDEX = 5
250 for line in battery_data:
251 l = line.split(',')
252 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi'
253 and l[PWI_POWER_INDEX] != 0):
254 raise device_errors.CommandFailedError(
255 'Non-zero pmi value found after reset.')
256 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'],
257 check_return=True)
258 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1)
259
208 # TODO(rnephew): Make private when all use cases can use the context manager. 260 # TODO(rnephew): Make private when all use cases can use the context manager.
209 def EnableBatteryUpdates(self, timeout=None, retries=None): 261 def EnableBatteryUpdates(self, timeout=None, retries=None):
210 """ Restarts device charging so that dumpsys no longer collects power data. 262 """ Restarts device charging so that dumpsys no longer collects power data.
211 263
212 Args: 264 Args:
213 timeout: timeout in seconds 265 timeout: timeout in seconds
214 retries: number of retries 266 retries: number of retries
215 """ 267 """
216 self._device.EnableBatteryUpdates(timeout=None, retries=None) 268 def battery_updates_enabled():
269 return self.GetCharging() is True
270
271 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '1'],
272 check_return=True)
273 self._device.RunShellCommand(['dumpsys', 'battery', 'reset'],
274 check_return=True)
275 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1)
217 276
218 @contextlib.contextmanager 277 @contextlib.contextmanager
219 def BatteryMeasurement(self, timeout=None, retries=None): 278 def BatteryMeasurement(self, timeout=None, retries=None):
220 """Context manager that enables battery data collection. It makes 279 """Context manager that enables battery data collection. It makes
221 the device appear to stop charging so that dumpsys will start collecting 280 the device appear to stop charging so that dumpsys will start collecting
222 power data since last charge. Once the with block is exited, charging is 281 power data since last charge. Once the with block is exited, charging is
223 resumed and power data since last charge is no longer collected. 282 resumed and power data since last charge is no longer collected.
224 283
225 Only for devices L and higher. 284 Only for devices L and higher.
226 285
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 battery_level = self.GetBatteryInfo().get('level') 319 battery_level = self.GetBatteryInfo().get('level')
261 if battery_level is None: 320 if battery_level is None:
262 logging.warning('Unable to find current battery level.') 321 logging.warning('Unable to find current battery level.')
263 battery_level = 100 322 battery_level = 100
264 else: 323 else:
265 logging.info('current battery level: %s', battery_level) 324 logging.info('current battery level: %s', battery_level)
266 battery_level = int(battery_level) 325 battery_level = int(battery_level)
267 return battery_level >= level 326 return battery_level >= level
268 327
269 timeout_retry.WaitFor(device_charged, wait_period=wait_period) 328 timeout_retry.WaitFor(device_charged, wait_period=wait_period)
OLDNEW
« no previous file with comments | « no previous file | build/android/pylib/device/battery_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698