J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | # |
| 5 | # Expects to be run in an environment with sudo and no interactive password |
| 6 | # prompt, such as within the Chromium OS development chroot. |
| 7 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 8 | import ast |
| 9 | import logging |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 10 | import os |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 11 | import re |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame] | 12 | import socket |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 13 | import time |
| 14 | import xmlrpclib |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 15 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import error |
Mary Ruthven | ecf1271 | 2019-06-26 17:36:21 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import lsbrelease_utils |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 18 | from autotest_lib.server import utils as server_utils |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 19 | from autotest_lib.server.cros.servo import firmware_programmer |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 20 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 21 | # Time to wait when probing for a usb device, it takes on avg 17 seconds |
| 22 | # to do a full probe. |
| 23 | _USB_PROBE_TIMEOUT = 40 |
| 24 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 25 | |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 26 | # Regex to match XMLRPC errors due to a servod control not existing. |
| 27 | NO_CONTROL_RE = re.compile(r'No control named (\w*\.?\w*)') |
| 28 | |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 29 | |
| 30 | # The minimum voltage on the charger port on servo v4 that is expected. This is |
| 31 | # to query whether a charger is plugged into servo v4 and thus pd control |
| 32 | # capabilities can be used. |
| 33 | V4_CHG_ATTACHED_MIN_VOLTAGE_MV = 4400 |
| 34 | |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 35 | class ControlUnavailableError(error.TestFail): |
| 36 | """Custom error class to indicate a control is unavailable on servod.""" |
| 37 | pass |
| 38 | |
| 39 | |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 40 | def _extract_image_from_tarball(tarball, dest_dir, image_candidates): |
| 41 | """Try extracting the image_candidates from the tarball. |
| 42 | |
| 43 | @param tarball: The path of the tarball. |
| 44 | @param dest_path: The path of the destination. |
| 45 | @param image_candidates: A tuple of the paths of image candidates. |
| 46 | |
| 47 | @return: The first path from the image candidates, which succeeds, or None |
| 48 | if all the image candidates fail. |
| 49 | """ |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 50 | |
| 51 | # Create the firmware_name subdirectory if it doesn't exist |
| 52 | if not os.path.exists(dest_dir): |
| 53 | os.mkdir(dest_dir) |
| 54 | |
| 55 | # Try to extract image candidates from tarball |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 56 | for image in image_candidates: |
| 57 | status = server_utils.system( |
| 58 | ('tar xf %s -C %s %s' % (tarball, dest_dir, image)), |
| 59 | timeout=60, ignore_status=True) |
| 60 | if status == 0: |
| 61 | return image |
| 62 | return None |
| 63 | |
| 64 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 65 | class _PowerStateController(object): |
| 66 | |
| 67 | """Class to provide board-specific power operations. |
| 68 | |
| 69 | This class is responsible for "power on" and "power off" |
| 70 | operations that can operate without making assumptions in |
| 71 | advance about board state. It offers an interface that |
| 72 | abstracts out the different sequences required for different |
| 73 | board types. |
| 74 | |
| 75 | """ |
| 76 | |
| 77 | # Constants acceptable to be passed for the `rec_mode` parameter |
| 78 | # to power_on(). |
| 79 | # |
| 80 | # REC_ON: Boot the DUT in recovery mode, i.e. boot from USB or |
| 81 | # SD card. |
| 82 | # REC_OFF: Boot in normal mode, i.e. boot from internal storage. |
| 83 | |
| 84 | REC_ON = 'rec' |
| 85 | REC_OFF = 'on' |
Shelley Chen | 6593862 | 2018-05-16 07:45:54 -0700 | [diff] [blame] | 86 | REC_ON_FORCE_MRC = 'rec_force_mrc' |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 87 | |
| 88 | # Delay in seconds needed between asserting and de-asserting |
| 89 | # warm reset. |
| 90 | _RESET_HOLD_TIME = 0.5 |
| 91 | |
| 92 | def __init__(self, servo): |
| 93 | """Initialize the power state control. |
| 94 | |
| 95 | @param servo Servo object providing the underlying `set` and `get` |
| 96 | methods for the target controls. |
| 97 | |
| 98 | """ |
| 99 | self._servo = servo |
| 100 | |
| 101 | def reset(self): |
| 102 | """Force the DUT to reset. |
| 103 | |
| 104 | The DUT is guaranteed to be on at the end of this call, |
| 105 | regardless of its previous state, provided that there is |
| 106 | working OS software. This also guarantees that the EC has |
| 107 | been restarted. |
| 108 | |
| 109 | """ |
| 110 | self._servo.set_nocheck('power_state', 'reset') |
| 111 | |
| 112 | def warm_reset(self): |
| 113 | """Apply warm reset to the DUT. |
| 114 | |
| 115 | This asserts, then de-asserts the 'warm_reset' signal. |
| 116 | Generally, this causes the board to restart. |
| 117 | |
| 118 | """ |
Ravutappa | 951ca43 | 2019-05-15 09:52:52 -0700 | [diff] [blame] | 119 | # TODO: warm_reset support has added to power_state.py. Once it |
| 120 | # available to labstation remove fallback method. |
| 121 | try: |
| 122 | self._servo.set_nocheck('power_state', 'warm_reset') |
| 123 | except error.TestFail as err: |
| 124 | logging.info("Fallback to warm_reset control method") |
| 125 | self._servo.set_get_all(['warm_reset:on', |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 126 | 'sleep:%.4f' % self._RESET_HOLD_TIME, |
| 127 | 'warm_reset:off']) |
| 128 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 129 | def power_off(self): |
| 130 | """Force the DUT to power off. |
| 131 | |
| 132 | The DUT is guaranteed to be off at the end of this call, |
| 133 | regardless of its previous state, provided that there is |
| 134 | working EC and boot firmware. There is no requirement for |
| 135 | working OS software. |
| 136 | |
| 137 | """ |
| 138 | self._servo.set_nocheck('power_state', 'off') |
| 139 | |
| 140 | def power_on(self, rec_mode=REC_OFF): |
| 141 | """Force the DUT to power on. |
| 142 | |
| 143 | Prior to calling this function, the DUT must be powered off, |
| 144 | e.g. with a call to `power_off()`. |
| 145 | |
| 146 | At power on, recovery mode is set as specified by the |
| 147 | corresponding argument. When booting with recovery mode on, it |
| 148 | is the caller's responsibility to unplug/plug in a bootable |
| 149 | external storage device. |
| 150 | |
| 151 | If the DUT requires a delay after powering on but before |
| 152 | processing inputs such as USB stick insertion, the delay is |
| 153 | handled by this method; the caller is not responsible for such |
| 154 | delays. |
| 155 | |
| 156 | @param rec_mode Setting of recovery mode to be applied at |
| 157 | power on. default: REC_OFF aka 'off' |
| 158 | |
| 159 | """ |
| 160 | self._servo.set_nocheck('power_state', rec_mode) |
| 161 | |
| 162 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 163 | class _Uart(object): |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 164 | """Class to capture UART streams of CPU, EC, Cr50, etc.""" |
| 165 | _UartToCapture = ('cpu', 'ec', 'cr50', 'servo_v4', 'servo_micro', 'usbpd') |
| 166 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 167 | def __init__(self, servo): |
| 168 | self._servo = servo |
| 169 | self._streams = [] |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 170 | self.logs_dir = None |
Congbin Guo | fe4d4e8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 171 | |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 172 | def _start_stop_capture(self, uart, start): |
| 173 | """Helper function to start/stop capturing on specified UART. |
| 174 | |
| 175 | @param uart: The UART name to start/stop capturing. |
| 176 | @param start: True to start capturing, otherwise stop. |
| 177 | |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 178 | @returns True if the operation completes successfully. |
| 179 | False if the UART capturing is not supported or failed due to |
| 180 | an error. |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 181 | """ |
| 182 | logging.debug('%s capturing %s UART.', 'Start' if start else 'Stop', |
| 183 | uart) |
Ruben Rodriguez Buchillon | b0e2a9f | 2019-08-12 12:40:44 -0700 | [diff] [blame] | 184 | uart_cmd = '%s_uart_capture' % uart |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 185 | target_level = 'on' if start else 'off' |
| 186 | level = None |
Ruben Rodriguez Buchillon | b0e2a9f | 2019-08-12 12:40:44 -0700 | [diff] [blame] | 187 | if self._servo.has_control(uart_cmd): |
Ruben Rodriguez Buchillon | 9f48564 | 2019-08-21 14:32:22 -0700 | [diff] [blame] | 188 | # Do our own implementation of set() here as not_applicable |
| 189 | # should also count as a valid control. |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 190 | logging.debug('Trying to set %s to %s.', uart_cmd, target_level) |
| 191 | try: |
| 192 | self._servo.set_nocheck(uart_cmd, target_level) |
| 193 | level = self._servo.get(uart_cmd) |
| 194 | except error.TestFail as e: |
| 195 | # Any sort of test failure here should not stop the test. This |
| 196 | # is just to capture more output. Log and move on. |
| 197 | logging.warning('Failed to set %s to %s. %s. Ignoring.', |
| 198 | uart_cmd, target_level, str(e)) |
| 199 | if level == target_level: |
| 200 | logging.debug('Managed to set %s to %s.', uart_cmd, level) |
| 201 | else: |
| 202 | logging.debug('Failed to set %s to %s. Got %s.', uart_cmd, |
| 203 | target_level, level) |
| 204 | return level == target_level |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 205 | |
| 206 | def start_capture(self): |
| 207 | """Start capturing UART streams.""" |
| 208 | for uart in self._UartToCapture: |
| 209 | if self._start_stop_capture(uart, True): |
| 210 | self._streams.append(('%s_uart_stream' % uart, '%s_uart.log' % |
| 211 | uart)) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 212 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 213 | def dump(self): |
| 214 | """Dump UART streams to log files accordingly.""" |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 215 | if not self.logs_dir: |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 216 | return |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 217 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 218 | for stream, logfile in self._streams: |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 219 | logfile_fullname = os.path.join(self.logs_dir, logfile) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 220 | try: |
| 221 | content = self._servo.get(stream) |
| 222 | except Exception as err: |
| 223 | logging.warn('Failed to get UART log for %s: %s', stream, err) |
| 224 | continue |
| 225 | |
Kuang-che Wu | 12192fc | 2019-08-31 09:55:00 +0800 | [diff] [blame] | 226 | if content == 'not_applicable': |
| 227 | logging.warn('%s is not applicable', stream) |
| 228 | continue |
| 229 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 230 | # The UART stream may contain non-printable characters, and servo |
| 231 | # returns it in string representation. We use `ast.leteral_eval` |
| 232 | # to revert it back. |
| 233 | with open(logfile_fullname, 'a') as fd: |
Kuang-che Wu | 12192fc | 2019-08-31 09:55:00 +0800 | [diff] [blame] | 234 | try: |
| 235 | fd.write(ast.literal_eval(content)) |
| 236 | except ValueError: |
| 237 | logging.exception('Invalid value for %s: %r', stream, |
| 238 | content) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 239 | |
| 240 | def stop_capture(self): |
| 241 | """Stop capturing UART streams.""" |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 242 | for uart in self._UartToCapture: |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 243 | try: |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 244 | self._start_stop_capture(uart, False) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 245 | except Exception as err: |
| 246 | logging.warn('Failed to stop UART logging for %s: %s', uart, |
| 247 | err) |
| 248 | |
| 249 | |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 250 | class Servo(object): |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 251 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 252 | """Manages control of a Servo board. |
| 253 | |
| 254 | Servo is a board developed by hardware group to aide in the debug and |
| 255 | control of various partner devices. Servo's features include the simulation |
| 256 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 257 | class manages setting up and communicating with a servo demon (servod) |
| 258 | process. It provides both high-level functions for common servo tasks and |
| 259 | low-level functions for directly setting and reading gpios. |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 260 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 261 | """ |
| 262 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 263 | # Power button press delays in seconds. |
J. Richard Barnette | d2e4cbd | 2012-06-29 12:18:40 -0700 | [diff] [blame] | 264 | # |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 265 | # The EC specification says that 8.0 seconds should be enough |
| 266 | # for the long power press. However, some platforms need a bit |
| 267 | # more time. Empirical testing has found these requirements: |
| 268 | # Alex: 8.2 seconds |
| 269 | # ZGB: 8.5 seconds |
| 270 | # The actual value is set to the largest known necessary value. |
| 271 | # |
| 272 | # TODO(jrbarnette) Being generous is the right thing to do for |
| 273 | # existing platforms, but if this code is to be used for |
| 274 | # qualification of new hardware, we should be less generous. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 275 | SHORT_DELAY = 0.1 |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 276 | |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 277 | # Maximum number of times to re-read power button on release. |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 278 | GET_RETRY_MAX = 10 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 279 | |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 280 | # Delays to deal with DUT state transitions. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 281 | SLEEP_DELAY = 6 |
| 282 | BOOT_DELAY = 10 |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 283 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 284 | # Default minimum time interval between 'press' and 'release' |
| 285 | # keyboard events. |
Vic Yang | 0aca1c2 | 2012-11-19 18:33:56 -0800 | [diff] [blame] | 286 | SERVO_KEY_PRESS_DELAY = 0.1 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 287 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 288 | # Time to toggle recovery switch on and off. |
| 289 | REC_TOGGLE_DELAY = 0.1 |
| 290 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 291 | # Time to toggle development switch on and off. |
| 292 | DEV_TOGGLE_DELAY = 0.1 |
| 293 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 294 | # Time between an usb disk plugged-in and detected in the system. |
| 295 | USB_DETECTION_DELAY = 10 |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 296 | # Time to keep USB power off before and after USB mux direction is changed |
| 297 | USB_POWEROFF_DELAY = 2 |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 298 | |
Simran Basi | b7850bb | 2013-07-24 12:33:42 -0700 | [diff] [blame] | 299 | # Time to wait before timing out on servo initialization. |
| 300 | INIT_TIMEOUT_SECS = 10 |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame] | 301 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 302 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 303 | def __init__(self, servo_host, servo_serial=None): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 304 | """Sets up the servo communication infrastructure. |
| 305 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 306 | @param servo_host: A ServoHost object representing |
| 307 | the host running servod. |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame] | 308 | @type servo_host: autotest_lib.server.hosts.servo_host.ServoHost |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 309 | @param servo_serial: Serial number of the servo board. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 310 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 311 | # TODO(fdeng): crbug.com/298379 |
| 312 | # We should move servo_host object out of servo object |
| 313 | # to minimize the dependencies on the rest of Autotest. |
| 314 | self._servo_host = servo_host |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 315 | self._servo_serial = servo_serial |
Richard Barnette | 180efe6 | 2016-12-02 23:20:44 +0000 | [diff] [blame] | 316 | self._server = servo_host.get_servod_server_proxy() |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 317 | self._servo_type = self.get_servo_version() |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 318 | self._power_state = _PowerStateController(self) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 319 | self._uart = _Uart(self) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 320 | self._usb_state = None |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 321 | self._programmer = None |
Dana Goyette | 4dc0adc | 2019-05-06 14:51:53 -0700 | [diff] [blame] | 322 | self._prev_log_inode = None |
| 323 | self._prev_log_size = 0 |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 324 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 325 | @property |
| 326 | def servo_serial(self): |
| 327 | """Returns the serial number of the servo board.""" |
| 328 | return self._servo_serial |
| 329 | |
Dana Goyette | 0b6e640 | 2019-10-04 11:09:24 -0700 | [diff] [blame] | 330 | def rotate_servod_logs(self, filename=None, directory=None): |
| 331 | """Save the latest servod log into a local directory, then rotate logs. |
Dana Goyette | 4dc0adc | 2019-05-06 14:51:53 -0700 | [diff] [blame] | 332 | |
Dana Goyette | 0b6e640 | 2019-10-04 11:09:24 -0700 | [diff] [blame] | 333 | The files will be <filename>.DEBUG, <filename>.INFO, <filename>.WARNING, |
| 334 | or just <filename>.log if not using split level logging. |
| 335 | |
| 336 | @param filename: local filename prefix (no file extension) to use. |
| 337 | If None, rotate log but don't save it. |
| 338 | @param directory: local directory to save logs into (if unset, use cwd) |
Dana Goyette | 4dc0adc | 2019-05-06 14:51:53 -0700 | [diff] [blame] | 339 | """ |
Dana Goyette | 0b6e640 | 2019-10-04 11:09:24 -0700 | [diff] [blame] | 340 | if self.is_localhost(): |
| 341 | # Local servod usually runs without log-dir, so can't be collected. |
| 342 | # TODO(crbug.com/1011516): allow localhost when rotation is enabled |
| 343 | return |
| 344 | |
| 345 | log_dir = '/var/log/servod_%s' % self._servo_host.servo_port |
| 346 | |
| 347 | if filename: |
Dana Goyette | c861334 | 2019-11-13 15:03:28 -0800 | [diff] [blame] | 348 | logging.info("Saving servod logs: %s/%s.*", directory or '.', |
| 349 | filename) |
Dana Goyette | 0b6e640 | 2019-10-04 11:09:24 -0700 | [diff] [blame] | 350 | # TODO(crrev.com/c/1793030): remove no-level case once CL is pushed |
| 351 | for level_name in ('', 'DEBUG', 'INFO', 'WARNING'): |
| 352 | |
| 353 | remote_path = os.path.join(log_dir, 'latest') |
| 354 | if level_name: |
| 355 | remote_path += '.%s' % level_name |
| 356 | |
| 357 | local_path = '%s.%s' % (filename, level_name or 'log') |
| 358 | if directory: |
| 359 | local_path = os.path.join(directory, local_path) |
| 360 | |
| 361 | try: |
| 362 | self._servo_host.get_file( |
| 363 | remote_path, local_path, try_rsync=False) |
| 364 | |
| 365 | except error.AutoservRunError as e: |
| 366 | result = e.result_obj |
| 367 | if result.exit_status != 0: |
| 368 | stderr = result.stderr.strip() |
| 369 | |
| 370 | # File not existing is okay, but warn for anything else. |
| 371 | if 'no such' not in stderr.lower(): |
| 372 | logging.warn( |
| 373 | "Couldn't retrieve servod log: %s", |
| 374 | stderr or '\n%s' % result) |
| 375 | |
| 376 | try: |
| 377 | if os.stat(local_path).st_size == 0: |
| 378 | os.unlink(local_path) |
| 379 | except EnvironmentError: |
| 380 | pass |
| 381 | |
| 382 | else: |
| 383 | # No filename given, so caller wants to discard the log lines. |
| 384 | # Remove the symlinks to prevent old log-dir links from being |
| 385 | # picked up multiple times when using servod without log-dir. |
| 386 | remote_path = os.path.join(log_dir, 'latest*') |
| 387 | self._servo_host.run( |
| 388 | "rm %s" % remote_path, |
| 389 | stderr_tee=None, ignore_status=True) |
| 390 | |
| 391 | # Servod log rotation renames current log, then creates a new file with |
| 392 | # the old name: log.<date> -> log.<date>.1.tbz2 -> log.<date>.2.tbz2 |
| 393 | |
| 394 | # Must rotate after copying, or the copy would be the new, empty file. |
| 395 | try: |
| 396 | self.set_nocheck('rotate_servod_logs', 'yes') |
| 397 | except ControlUnavailableError as e: |
Dana Goyette | c861334 | 2019-11-13 15:03:28 -0800 | [diff] [blame] | 398 | # Missing control (possibly old servod) |
Dana Goyette | 0b6e640 | 2019-10-04 11:09:24 -0700 | [diff] [blame] | 399 | logging.warn("Couldn't rotate servod logs: %s", str(e)) |
Dana Goyette | c861334 | 2019-11-13 15:03:28 -0800 | [diff] [blame] | 400 | except error.TestFail: |
| 401 | # Control exists but gave an error; don't let it fail the test. |
| 402 | # The error is already logged in set_nocheck(). |
| 403 | pass |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 404 | |
Tom Wai-Hong Tam | 22ee0e5 | 2013-04-03 13:36:39 +0800 | [diff] [blame] | 405 | def get_power_state_controller(self): |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 406 | """Return the power state controller for this Servo. |
| 407 | |
| 408 | The power state controller provides board-independent |
| 409 | interfaces for reset, power-on, power-off operations. |
| 410 | |
| 411 | """ |
| 412 | return self._power_state |
| 413 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 414 | |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 415 | def initialize_dut(self, cold_reset=False, enable_main=True): |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 416 | """Initializes a dut for testing purposes. |
| 417 | |
| 418 | This sets various servo signals back to default values |
| 419 | appropriate for the target board. By default, if the DUT |
| 420 | is already on, it stays on. If the DUT is powered off |
| 421 | before initialization, its state afterward is unspecified. |
| 422 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 423 | Rationale: Basic initialization of servo sets the lid open, |
| 424 | when there is a lid. This operation won't affect powered on |
| 425 | units; however, setting the lid open may power on a unit |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 426 | that's off, depending on the board type and previous state |
| 427 | of the device. |
| 428 | |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 429 | If `cold_reset` is a true value, the DUT and its EC will be |
| 430 | reset, and the DUT rebooted in normal mode. |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 431 | |
| 432 | @param cold_reset If True, cold reset the device after |
| 433 | initialization. |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 434 | @param enable_main If True, make sure the main servo device has |
| 435 | control of the dut. |
| 436 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 437 | """ |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 438 | if enable_main: |
| 439 | self.enable_main_servo_device() |
| 440 | |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame] | 441 | try: |
| 442 | self._server.hwinit() |
| 443 | except socket.error as e: |
| 444 | e.filename = '%s:%s' % (self._servo_host.hostname, |
| 445 | self._servo_host.servo_port) |
| 446 | raise |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 447 | self._usb_state = None |
Mengqi Guo | 51d5bea | 2020-02-03 18:34:45 -0800 | [diff] [blame^] | 448 | if self.has_control('usb_mux_oe1'): |
| 449 | self.set('usb_mux_oe1', 'on') |
| 450 | self.switch_usbkey('off') |
| 451 | else: |
| 452 | logging.warning('Servod command \'usb_mux_oe1\' is not available. ' |
| 453 | 'Any USB drive related servo routines will fail.') |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 454 | self._uart.start_capture() |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 455 | if cold_reset: |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 456 | self._power_state.reset() |
J. Richard Barnette | 1777f5d | 2014-09-03 15:18:19 -0700 | [diff] [blame] | 457 | logging.debug('Servo initialized, version is %s', |
| 458 | self._server.get_version()) |
Ruben Rodriguez Buchillon | b0e2a9f | 2019-08-12 12:40:44 -0700 | [diff] [blame] | 459 | if self.has_control('init_keyboard'): |
| 460 | # This indicates the servod version does not |
| 461 | # have explicit keyboard initialization yet. |
| 462 | # Ignore this. |
Ruben Rodriguez Buchillon | 0529824 | 2019-05-20 11:29:06 -0700 | [diff] [blame] | 463 | # TODO(coconutruben): change this back to set() about a month |
| 464 | # after crrev.com/c/1586239 has been merged (or whenever that |
| 465 | # logic is in the labstation images). |
| 466 | self.set_nocheck('init_keyboard','on') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 467 | |
| 468 | |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 469 | def is_localhost(self): |
| 470 | """Is the servod hosted locally? |
| 471 | |
| 472 | Returns: |
| 473 | True if local hosted; otherwise, False. |
| 474 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 475 | return self._servo_host.is_localhost() |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 476 | |
| 477 | |
Mary Ruthven | ecf1271 | 2019-06-26 17:36:21 -0700 | [diff] [blame] | 478 | def get_os_version(self): |
| 479 | """Returns the chromeos release version.""" |
| 480 | lsb_release_content = self.system_output('cat /etc/lsb-release', |
| 481 | ignore_status=True) |
| 482 | return lsbrelease_utils.get_chromeos_release_builder_path( |
| 483 | lsb_release_content=lsb_release_content) |
| 484 | |
| 485 | |
Mary Ruthven | 83bb595 | 2019-06-27 12:34:05 -0700 | [diff] [blame] | 486 | def get_servod_version(self): |
| 487 | """Returns the servod version.""" |
| 488 | result = self._servo_host.run('servod --version') |
| 489 | # TODO: use system_output once servod --version prints to stdout |
| 490 | stdout = result.stdout.strip() |
| 491 | return stdout if stdout else result.stderr.strip() |
| 492 | |
| 493 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 494 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 495 | """Simulate a long power button press.""" |
J. Richard Barnette | f12bff2 | 2012-07-18 14:41:06 -0700 | [diff] [blame] | 496 | # After a long power press, the EC may ignore the next power |
| 497 | # button press (at least on Alex). To guarantee that this |
| 498 | # won't happen, we need to allow the EC one second to |
| 499 | # collect itself. |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 500 | # long_press is defined as 8.5s in servod |
| 501 | self.set_nocheck('power_key', 'long_press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 502 | |
| 503 | |
| 504 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 505 | """Simulate a normal power button press.""" |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 506 | # press is defined as 1.2s in servod |
| 507 | self.set_nocheck('power_key', 'press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 508 | |
| 509 | |
| 510 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 511 | """Simulate a short power button press.""" |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 512 | # tab is defined as 0.2s in servod |
| 513 | self.set_nocheck('power_key', 'tab') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 514 | |
| 515 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 516 | def power_key(self, press_secs='tab'): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 517 | """Simulate a power button press. |
| 518 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 519 | @param press_secs: int, float, str; time to press key in seconds or |
| 520 | known shorthand: 'tab' 'press' 'long_press' |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 521 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 522 | self.set_nocheck('power_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 523 | |
| 524 | |
Ruben Zakarian | 47c1d4f | 2019-09-30 14:17:41 -0700 | [diff] [blame] | 525 | def pwr_button(self, action='press'): |
| 526 | """Simulate a power button press. |
| 527 | |
| 528 | @param action: str; could be press or could be release. |
| 529 | """ |
| 530 | self.set_nocheck('pwr_button', action) |
| 531 | |
| 532 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 533 | def lid_open(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 534 | """Simulate opening the lid and raise exception if all attempts fail""" |
| 535 | self.set('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 536 | |
| 537 | |
| 538 | def lid_close(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 539 | """Simulate closing the lid and raise exception if all attempts fail |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 540 | |
| 541 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 542 | """ |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 543 | self.set('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 544 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 545 | |
Ruben Zakarian | 47c1d4f | 2019-09-30 14:17:41 -0700 | [diff] [blame] | 546 | |
| 547 | def vbus_power_get(self): |
| 548 | """Get current vbus_power.""" |
| 549 | return self.get('vbus_power') |
| 550 | |
| 551 | |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 552 | def volume_up(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 553 | """Simulate pushing the volume down button. |
| 554 | |
| 555 | @param timeout: Timeout for setting the volume. |
| 556 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 557 | self.set_get_all(['volume_up:yes', |
| 558 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 559 | 'volume_up:no']) |
| 560 | # we need to wait for commands to take effect before moving on |
| 561 | time_left = float(timeout) |
| 562 | while time_left > 0.0: |
| 563 | value = self.get('volume_up') |
| 564 | if value == 'no': |
| 565 | return |
| 566 | time.sleep(self.SHORT_DELAY) |
| 567 | time_left = time_left - self.SHORT_DELAY |
| 568 | raise error.TestFail("Failed setting volume_up to no") |
| 569 | |
| 570 | def volume_down(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 571 | """Simulate pushing the volume down button. |
| 572 | |
| 573 | @param timeout: Timeout for setting the volume. |
| 574 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 575 | self.set_get_all(['volume_down:yes', |
| 576 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 577 | 'volume_down:no']) |
| 578 | # we need to wait for commands to take effect before moving on |
| 579 | time_left = float(timeout) |
| 580 | while time_left > 0.0: |
| 581 | value = self.get('volume_down') |
| 582 | if value == 'no': |
| 583 | return |
| 584 | time.sleep(self.SHORT_DELAY) |
| 585 | time_left = time_left - self.SHORT_DELAY |
| 586 | raise error.TestFail("Failed setting volume_down to no") |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 587 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 588 | def ctrl_d(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 589 | """Simulate Ctrl-d simultaneous button presses. |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 590 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 591 | @param press_secs: int, float, str; time to press key in seconds or |
| 592 | known shorthand: 'tab' 'press' 'long_press' |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 593 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 594 | self.set_nocheck('ctrl_d', press_secs) |
Gediminas Ramanauskas | 9da80f2 | 2012-11-14 12:59:43 -0800 | [diff] [blame] | 595 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 596 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 597 | def ctrl_u(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 598 | """Simulate Ctrl-u simultaneous button presses. |
| 599 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 600 | @param press_secs: int, float, str; time to press key in seconds or |
| 601 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 602 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 603 | self.set_nocheck('ctrl_u', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 604 | |
| 605 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 606 | def ctrl_enter(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 607 | """Simulate Ctrl-enter simultaneous button presses. |
| 608 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 609 | @param press_secs: int, float, str; time to press key in seconds or |
| 610 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 611 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 612 | self.set_nocheck('ctrl_enter', press_secs) |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 613 | |
| 614 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 615 | def ctrl_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 616 | """Simulate Enter key button press. |
| 617 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 618 | @param press_secs: int, float, str; time to press key in seconds or |
| 619 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 620 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 621 | self.set_nocheck('ctrl_key', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 622 | |
| 623 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 624 | def enter_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 625 | """Simulate Enter key button press. |
| 626 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 627 | @param press_secs: int, float, str; time to press key in seconds or |
| 628 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 629 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 630 | self.set_nocheck('enter_key', press_secs) |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 631 | |
| 632 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 633 | def refresh_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 634 | """Simulate Refresh key (F3) button press. |
| 635 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 636 | @param press_secs: int, float, str; time to press key in seconds or |
| 637 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 638 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 639 | self.set_nocheck('refresh_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 640 | |
| 641 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 642 | def ctrl_refresh_key(self, press_secs='tab'): |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 643 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 644 | |
| 645 | This key combination is an alternative of Space key. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 646 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 647 | @param press_secs: int, float, str; time to press key in seconds or |
| 648 | known shorthand: 'tab' 'press' 'long_press' |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 649 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 650 | self.set_nocheck('ctrl_refresh_key', press_secs) |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 651 | |
| 652 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 653 | def imaginary_key(self, press_secs='tab'): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 654 | """Simulate imaginary key button press. |
| 655 | |
| 656 | Maps to a key that doesn't physically exist. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 657 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 658 | @param press_secs: int, float, str; time to press key in seconds or |
| 659 | known shorthand: 'tab' 'press' 'long_press' |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 660 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 661 | self.set_nocheck('imaginary_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 662 | |
| 663 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 664 | def sysrq_x(self, press_secs='tab'): |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 665 | """Simulate Alt VolumeUp X simulataneous press. |
| 666 | |
| 667 | This key combination is the kernel system request (sysrq) X. |
| 668 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 669 | @param press_secs: int, float, str; time to press key in seconds or |
| 670 | known shorthand: 'tab' 'press' 'long_press' |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 671 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 672 | self.set_nocheck('sysrq_x', press_secs) |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 673 | |
| 674 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 675 | def toggle_recovery_switch(self): |
| 676 | """Toggle recovery switch on and off.""" |
| 677 | self.enable_recovery_mode() |
| 678 | time.sleep(self.REC_TOGGLE_DELAY) |
| 679 | self.disable_recovery_mode() |
| 680 | |
| 681 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 682 | def enable_recovery_mode(self): |
| 683 | """Enable recovery mode on device.""" |
| 684 | self.set('rec_mode', 'on') |
| 685 | |
| 686 | |
| 687 | def disable_recovery_mode(self): |
| 688 | """Disable recovery mode on device.""" |
| 689 | self.set('rec_mode', 'off') |
| 690 | |
| 691 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 692 | def toggle_development_switch(self): |
| 693 | """Toggle development switch on and off.""" |
| 694 | self.enable_development_mode() |
| 695 | time.sleep(self.DEV_TOGGLE_DELAY) |
| 696 | self.disable_development_mode() |
| 697 | |
| 698 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 699 | def enable_development_mode(self): |
| 700 | """Enable development mode on device.""" |
| 701 | self.set('dev_mode', 'on') |
| 702 | |
| 703 | |
| 704 | def disable_development_mode(self): |
| 705 | """Disable development mode on device.""" |
| 706 | self.set('dev_mode', 'off') |
| 707 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 708 | def boot_devmode(self): |
| 709 | """Boot a dev-mode device that is powered off.""" |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 710 | self.power_short_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 711 | self.pass_devmode() |
| 712 | |
| 713 | |
| 714 | def pass_devmode(self): |
| 715 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 716 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 717 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 718 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 719 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 720 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 721 | def get_board(self): |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 722 | """Get the board connected to servod.""" |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 723 | return self._server.get_board() |
| 724 | |
| 725 | |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 726 | def get_base_board(self): |
| 727 | """Get the board of the base connected to servod.""" |
Wai-Hong Tam | 7f6169e | 2017-09-29 09:23:48 -0700 | [diff] [blame] | 728 | try: |
| 729 | return self._server.get_base_board() |
| 730 | except xmlrpclib.Fault as e: |
| 731 | # TODO(waihong): Remove the following compatibility check when |
| 732 | # the new versions of hdctools are deployed. |
| 733 | if 'not supported' in str(e): |
| 734 | logging.warning('The servod is too old that get_base_board ' |
| 735 | 'not supported.') |
| 736 | return '' |
| 737 | raise |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 738 | |
| 739 | |
Wai-Hong Tam | cc399ee | 2017-12-08 12:43:28 -0800 | [diff] [blame] | 740 | def get_ec_active_copy(self): |
| 741 | """Get the active copy of the EC image.""" |
| 742 | return self.get('ec_active_copy') |
| 743 | |
| 744 | |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 745 | def _get_xmlrpclib_exception(self, xmlexc): |
| 746 | """Get meaningful exception string from xmlrpc. |
| 747 | |
| 748 | Args: |
| 749 | xmlexc: xmlrpclib.Fault object |
| 750 | |
| 751 | xmlrpclib.Fault.faultString has the following format: |
| 752 | |
| 753 | <type 'exception type'>:'actual error message' |
| 754 | |
| 755 | Parse and return the real exception from the servod side instead of the |
| 756 | less meaningful one like, |
| 757 | <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no |
| 758 | attribute 'hw_driver'"> |
| 759 | |
| 760 | Returns: |
| 761 | string of underlying exception raised in servod. |
| 762 | """ |
| 763 | return re.sub('^.*>:', '', xmlexc.faultString) |
| 764 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 765 | def has_control(self, ctrl_name, prefix=''): |
| 766 | """Query servod server to determine if |ctrl_name| is a valid control. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 767 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 768 | @param ctrl_name Name of the control. |
| 769 | @param prefix: prefix to route control to correct servo device. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 770 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 771 | @returns: true if |ctrl_name| is a known control, false otherwise. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 772 | """ |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 773 | cltr_name = self._build_ctrl_name(ctrl_name, prefix) |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 774 | try: |
| 775 | # If the control exists, doc() will work. |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 776 | self._server.doc(ctrl_name) |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 777 | return True |
| 778 | except xmlrpclib.Fault as e: |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 779 | if re.search('No control %s' % ctrl_name, |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 780 | self._get_xmlrpclib_exception(e)): |
| 781 | return False |
| 782 | raise e |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 783 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 784 | def _build_ctrl_name(self, ctrl_name, prefix): |
| 785 | """Helper to build the control name if a prefix is used. |
| 786 | |
| 787 | @param ctrl_name Name of the control. |
| 788 | @param prefix: prefix to route control to correct servo device. |
| 789 | |
| 790 | @returns: [|prefix|.]ctrl_name depending on whether prefix is non-empty. |
| 791 | """ |
| 792 | assert ctrl_name |
| 793 | if prefix: |
| 794 | return '%s.%s' % (prefix, ctrl_name) |
| 795 | return ctrl_name |
| 796 | |
| 797 | def get(self, ctrl_name, prefix=''): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 798 | """Get the value of a gpio from Servod. |
| 799 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 800 | @param ctrl_name Name of the control. |
| 801 | @param prefix: prefix to route control to correct servo device. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 802 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 803 | @returns: server response to |ctrl_name| request. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 804 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 805 | @raise ControlUnavailableError: if |ctrl_name| not a known control. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 806 | @raise error.TestFail: for all other failures doing get(). |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 807 | """ |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 808 | cltr_name = self._build_ctrl_name(ctrl_name, prefix) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 809 | try: |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 810 | return self._server.get(ctrl_name) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 811 | except xmlrpclib.Fault as e: |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 812 | err_str = self._get_xmlrpclib_exception(e) |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 813 | err_msg = "Getting '%s' :: %s" % (ctrl_name, err_str) |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 814 | unknown_ctrl = re.findall(NO_CONTROL_RE, err_str) |
| 815 | if unknown_ctrl: |
| 816 | raise ControlUnavailableError('No control named %r' % |
| 817 | unknown_ctrl[0]) |
| 818 | else: |
| 819 | logging.error(err_msg) |
| 820 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 821 | |
| 822 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 823 | def set(self, ctrl_name, ctrl_value, prefix=''): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 824 | """Set and check the value of a gpio using Servod. |
| 825 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 826 | @param ctrl_name: Name of the control. |
| 827 | @param ctrl_value: New setting for the control. |
| 828 | @param prefix: prefix to route control to correct servo device. |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 829 | @raise error.TestFail: if the control value fails to change. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 830 | """ |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 831 | cltr_name = self._build_ctrl_name(ctrl_name, prefix) |
| 832 | self.set_nocheck(ctrl_name, ctrl_value) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 833 | retry_count = Servo.GET_RETRY_MAX |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 834 | actual_value = self.get(ctrl_name) |
| 835 | while ctrl_value != actual_value and retry_count: |
| 836 | logging.warning("%s != %s, retry %d", ctrl_name, ctrl_value, |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 837 | retry_count) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 838 | retry_count -= 1 |
| 839 | time.sleep(Servo.SHORT_DELAY) |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 840 | actual_value = self.get(ctrl_name) |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 841 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 842 | if ctrl_value != actual_value: |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 843 | raise error.TestFail( |
| 844 | 'Servo failed to set %s to %s. Got %s.' |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 845 | % (ctrl_name, ctrl_value, actual_value)) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 846 | |
| 847 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 848 | def set_nocheck(self, ctrl_name, ctrl_value, prefix=''): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 849 | """Set the value of a gpio using Servod. |
| 850 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 851 | @param ctrl_name Name of the control. |
| 852 | @param ctrl_value New setting for the control. |
| 853 | @param prefix: prefix to route control to correct servo device. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 854 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 855 | @raise ControlUnavailableError: if |ctrl_name| not a known control. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 856 | @raise error.TestFail: for all other failures doing set(). |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 857 | """ |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 858 | cltr_name = self._build_ctrl_name(ctrl_name, prefix) |
Ruben Rodriguez Buchillon | 1c0219c | 2019-06-25 12:30:44 -0700 | [diff] [blame] | 859 | # The real danger here is to pass a None value through the xmlrpc. |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 860 | assert ctrl_value is not None |
| 861 | logging.debug('Setting %s to %r', ctrl_name, ctrl_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 862 | try: |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 863 | self._server.set(ctrl_name, ctrl_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 864 | except xmlrpclib.Fault as e: |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 865 | err_str = self._get_xmlrpclib_exception(e) |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 866 | err_msg = "Setting '%s' :: %s" % (ctrl_name, err_str) |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 867 | unknown_ctrl = re.findall(NO_CONTROL_RE, err_str) |
| 868 | if unknown_ctrl: |
| 869 | raise ControlUnavailableError('No control named %r' % |
| 870 | unknown_ctrl[0]) |
| 871 | else: |
| 872 | logging.error(err_msg) |
| 873 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 874 | |
| 875 | |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 876 | def set_get_all(self, controls): |
| 877 | """Set &| get one or more control values. |
| 878 | |
| 879 | @param controls: list of strings, controls to set &| get. |
| 880 | |
| 881 | @raise: error.TestError in case error occurs setting/getting values. |
| 882 | """ |
| 883 | rv = [] |
| 884 | try: |
Mary Ruthven | dfa4534 | 2019-06-25 10:10:17 -0700 | [diff] [blame] | 885 | logging.debug('Set/get all: %s', str(controls)) |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 886 | rv = self._server.set_get_all(controls) |
| 887 | except xmlrpclib.Fault as e: |
| 888 | # TODO(waihong): Remove the following backward compatibility when |
| 889 | # the new versions of hdctools are deployed. |
| 890 | if 'not supported' in str(e): |
| 891 | logging.warning('The servod is too old that set_get_all ' |
| 892 | 'not supported. Use set and get instead.') |
| 893 | for control in controls: |
| 894 | if ':' in control: |
| 895 | (name, value) = control.split(':') |
| 896 | if name == 'sleep': |
| 897 | time.sleep(float(value)) |
| 898 | else: |
| 899 | self.set_nocheck(name, value) |
| 900 | rv.append(True) |
| 901 | else: |
| 902 | rv.append(self.get(name)) |
| 903 | else: |
| 904 | err_msg = "Problem with '%s' :: %s" % \ |
| 905 | (controls, self._get_xmlrpclib_exception(e)) |
| 906 | raise error.TestFail(err_msg) |
| 907 | return rv |
| 908 | |
| 909 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 910 | # TODO(waihong) It may fail if multiple servo's are connected to the same |
| 911 | # host. Should look for a better way, like the USB serial name, to identify |
| 912 | # the USB device. |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 913 | # TODO(sbasi) Remove this code from autoserv once firmware tests have been |
| 914 | # updated. |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 915 | def probe_host_usb_dev(self, timeout=_USB_PROBE_TIMEOUT): |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 916 | """Probe the USB disk device plugged-in the servo from the host side. |
| 917 | |
Kevin Cheng | eb06fe7 | 2016-08-22 15:26:32 -0700 | [diff] [blame] | 918 | It uses servod to discover if there is a usb device attached to it. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 919 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 920 | @param timeout The timeout period when probing for the usb host device. |
| 921 | |
| 922 | @return: String of USB disk path (e.g. '/dev/sdb') or None. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 923 | """ |
Ruben Rodriguez Buchillon | 9a4bfc3 | 2018-10-16 20:46:25 +0800 | [diff] [blame] | 924 | # Set up Servo's usb mux. |
| 925 | self.switch_usbkey('host') |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 926 | return self._server.probe_host_usb_dev(timeout) or None |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 927 | |
| 928 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 929 | def image_to_servo_usb(self, image_path=None, |
| 930 | make_image_noninteractive=False): |
| 931 | """Install an image to the USB key plugged into the servo. |
| 932 | |
| 933 | This method may copy any image to the servo USB key including a |
| 934 | recovery image or a test image. These images are frequently used |
| 935 | for test purposes such as restoring a corrupted image or conducting |
| 936 | an upgrade of ec/fw/kernel as part of a test of a specific image part. |
| 937 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 938 | @param image_path Path on the host to the recovery image. |
| 939 | @param make_image_noninteractive Make the recovery image |
| 940 | noninteractive, therefore the DUT |
| 941 | will reboot automatically after |
| 942 | installation. |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 943 | """ |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 944 | # We're about to start plugging/unplugging the USB key. We |
| 945 | # don't know the state of the DUT, or what it might choose |
| 946 | # to do to the device after hotplug. To avoid surprises, |
| 947 | # force the DUT to be off. |
| 948 | self._server.hwinit() |
Ruben Rodriguez Buchillon | b0e2a9f | 2019-08-12 12:40:44 -0700 | [diff] [blame] | 949 | if self.has_control('init_keyboard'): |
| 950 | # This indicates the servod version does not |
| 951 | # have explicit keyboard initialization yet. |
| 952 | # Ignore this. |
Ruben Rodriguez Buchillon | bd3b74f | 2019-08-08 16:18:27 -0700 | [diff] [blame] | 953 | # TODO(coconutruben): change this back to set() about a month |
| 954 | # after crrev.com/c/1586239 has been merged (or whenever that |
| 955 | # logic is in the labstation images). |
| 956 | self.set_nocheck('init_keyboard','on') |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 957 | self._power_state.power_off() |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 958 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 959 | if image_path: |
Garry Wang | d265681 | 2019-08-07 17:29:16 -0700 | [diff] [blame] | 960 | # Set up Servo's usb mux. |
| 961 | self.switch_usbkey('host') |
Tom Wai-Hong Tam | 42f136d | 2012-10-26 11:11:23 +0800 | [diff] [blame] | 962 | logging.info('Searching for usb device and copying image to it. ' |
| 963 | 'Please wait a few minutes...') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 964 | if not self._server.download_image_to_usb(image_path): |
| 965 | logging.error('Failed to transfer requested image to USB. ' |
| 966 | 'Please take a look at Servo Logs.') |
| 967 | raise error.AutotestError('Download image to usb failed.') |
| 968 | if make_image_noninteractive: |
| 969 | logging.info('Making image noninteractive') |
| 970 | if not self._server.make_image_noninteractive(): |
| 971 | logging.error('Failed to make image noninteractive. ' |
| 972 | 'Please take a look at Servo Logs.') |
| 973 | |
Kalin Stoyanov | b3c11f3 | 2018-05-11 09:02:00 -0700 | [diff] [blame] | 974 | def boot_in_recovery_mode(self): |
| 975 | """Boot host DUT in recovery mode.""" |
| 976 | self._power_state.power_on(rec_mode=self._power_state.REC_ON) |
| 977 | self.switch_usbkey('dut') |
| 978 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 979 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 980 | def install_recovery_image(self, image_path=None, |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 981 | make_image_noninteractive=False): |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 982 | """Install the recovery image specified by the path onto the DUT. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 983 | |
| 984 | This method uses google recovery mode to install a recovery image |
| 985 | onto a DUT through the use of a USB stick that is mounted on a servo |
Tom Wai-Hong Tam | a07115e | 2012-01-09 12:27:01 +0800 | [diff] [blame] | 986 | board specified by the usb_dev. If no image path is specified |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 987 | we use the recovery image already on the usb image. |
| 988 | |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 989 | @param image_path: Path on the host to the recovery image. |
| 990 | @param make_image_noninteractive: Make the recovery image |
| 991 | noninteractive, therefore the DUT will reboot automatically |
| 992 | after installation. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 993 | """ |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 994 | self.image_to_servo_usb(image_path, make_image_noninteractive) |
Garry Wang | d265681 | 2019-08-07 17:29:16 -0700 | [diff] [blame] | 995 | # Give the DUT some time to power_off if we skip |
| 996 | # download image to usb. (crbug.com/982993) |
| 997 | if not image_path: |
| 998 | time.sleep(10) |
Kalin Stoyanov | b3c11f3 | 2018-05-11 09:02:00 -0700 | [diff] [blame] | 999 | self.boot_in_recovery_mode() |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1000 | |
| 1001 | |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1002 | def _scp_image(self, image_path): |
| 1003 | """Copy image to the servo host. |
| 1004 | |
| 1005 | When programming a firmware image on the DUT, the image must be |
| 1006 | located on the host to which the servo device is connected. Sometimes |
| 1007 | servo is controlled by a remote host, in this case the image needs to |
Dana Goyette | d5a9554 | 2019-12-30 11:14:14 -0800 | [diff] [blame] | 1008 | be transferred to the remote host. This adds the servod port number, to |
| 1009 | make sure tests for different DUTs don't trample on each other's files. |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1010 | |
| 1011 | @param image_path: a string, name of the firmware image file to be |
| 1012 | transferred. |
| 1013 | @return: a string, full path name of the copied file on the remote. |
| 1014 | """ |
Mary Ruthven | 401a601 | 2019-12-13 11:39:44 -0800 | [diff] [blame] | 1015 | name = os.path.basename(image_path) |
Dana Goyette | d5a9554 | 2019-12-30 11:14:14 -0800 | [diff] [blame] | 1016 | remote_name = 'dut_%s.%s' % (self._servo_host.servo_port, name) |
Mary Ruthven | 401a601 | 2019-12-13 11:39:44 -0800 | [diff] [blame] | 1017 | dest_path = os.path.join('/tmp', remote_name) |
| 1018 | logging.info('Copying %s to %s', name, dest_path) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1019 | self._servo_host.send_file(image_path, dest_path) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1020 | return dest_path |
| 1021 | |
| 1022 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1023 | def system(self, command, timeout=3600): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1024 | """Execute the passed in command on the servod host. |
| 1025 | |
| 1026 | @param command Command to be executed. |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1027 | @param timeout Maximum number of seconds of runtime allowed. Default to |
| 1028 | 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1029 | """ |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1030 | logging.info('Will execute on servo host: %s', command) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1031 | self._servo_host.run(command, timeout=timeout) |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1032 | |
| 1033 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1034 | def system_output(self, command, timeout=3600, |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1035 | ignore_status=False, args=()): |
| 1036 | """Execute the passed in command on the servod host, return stdout. |
| 1037 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1038 | @param command a string, the command to execute |
| 1039 | @param timeout an int, max number of seconds to wait til command |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1040 | execution completes. Default to 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1041 | @param ignore_status a Boolean, if true - ignore command's nonzero exit |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1042 | status, otherwise an exception will be thrown |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1043 | @param args a tuple of strings, each becoming a separate command line |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1044 | parameter for the command |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1045 | @return command's stdout as a string. |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1046 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1047 | return self._servo_host.run(command, timeout=timeout, |
| 1048 | ignore_status=ignore_status, |
| 1049 | args=args).stdout.strip() |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1050 | |
| 1051 | |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1052 | def get_servo_version(self, active=False): |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1053 | """Get the version of the servo, e.g., servo_v2 or servo_v3. |
| 1054 | |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1055 | @param active: Only return the servo type with the active device. |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1056 | @return: The version of the servo. |
| 1057 | |
| 1058 | """ |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1059 | servo_type = self._server.get_version() |
| 1060 | if '_and_' not in servo_type or not active: |
| 1061 | return servo_type |
| 1062 | |
| 1063 | # If servo v4 is using ccd and servo micro, modify the servo type to |
| 1064 | # reflect the active device. |
| 1065 | active_device = self.get('active_v4_device') |
| 1066 | if active_device in servo_type: |
| 1067 | logging.info('%s is active', active_device) |
| 1068 | return 'servo_v4_with_' + active_device |
| 1069 | |
| 1070 | logging.warn("%s is active even though it's not in servo type", |
| 1071 | active_device) |
| 1072 | return servo_type |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1073 | |
| 1074 | |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1075 | def get_main_servo_device(self): |
| 1076 | """Return the main servo device""" |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1077 | return self._servo_type.split('_with_')[-1].split('_and_')[0] |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1078 | |
| 1079 | |
| 1080 | def enable_main_servo_device(self): |
| 1081 | """Make sure the main device has control of the dut.""" |
Mary Ruthven | 1409d9d | 2019-10-22 20:44:24 -0700 | [diff] [blame] | 1082 | # Cr50 detects servo using the EC uart. It doesn't work well if the |
| 1083 | # board doesn't use EC uart. The lab active_v4_device doesn't handle |
| 1084 | # this correctly. Check ec_uart_pty before trying to change the active |
| 1085 | # device. |
| 1086 | # TODO(crbug.com/1016842): reenable the setting the main device when |
| 1087 | # active device works on labstations. |
| 1088 | return |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1089 | if not self.has_control('active_v4_device'): |
| 1090 | return |
| 1091 | self.set('active_v4_device', self.get_main_servo_device()) |
| 1092 | |
| 1093 | |
Ruben Rodriguez Buchillon | 1823bad | 2019-09-30 17:04:34 -0700 | [diff] [blame] | 1094 | def main_device_is_ccd(self): |
| 1095 | """Whether the main servo device (no prefixes) is a ccd device.""" |
Mary Ruthven | 2724ee6 | 2019-07-16 11:16:59 -0700 | [diff] [blame] | 1096 | servo = self._server.get_version() |
| 1097 | return 'ccd_cr50' in servo and 'servo_micro' not in servo |
| 1098 | |
| 1099 | |
Ruben Rodriguez Buchillon | 1823bad | 2019-09-30 17:04:34 -0700 | [diff] [blame] | 1100 | def main_device_is_flex(self): |
| 1101 | """Whether the main servo device (no prefixes) is a legacy device.""" |
| 1102 | return not self.main_device_is_ccd() |
| 1103 | |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1104 | |
| 1105 | def main_device_is_active(self): |
| 1106 | """Return whether the main device is the active device. |
| 1107 | |
| 1108 | This is only relevant for a dual setup with ccd and legacy on the same |
| 1109 | DUT. The main device is the servo that has no prefix on its controls. |
| 1110 | This helper answers the question whether that device is also the |
| 1111 | active device or not. |
| 1112 | """ |
| 1113 | # TODO(coconutruben): The current implementation of the dual setup only |
| 1114 | # ever has legacy as the main device. Therefore, it suffices to ask |
| 1115 | # whether the active device is ccd. |
| 1116 | if not self.dts_mode_is_valid(): |
| 1117 | # Use dts support as a proxy to whether the servo setup could |
| 1118 | # support a dual role. Only those setups now support legacy and ccd. |
| 1119 | return True |
| 1120 | active_device = self.get('active_v4_device') |
| 1121 | return 'ccd_cr50' not in active_device |
| 1122 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1123 | def _initialize_programmer(self, rw_only=False): |
| 1124 | """Initialize the firmware programmer. |
| 1125 | |
| 1126 | @param rw_only: True to initialize a programmer which only |
| 1127 | programs the RW portions. |
| 1128 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1129 | if self._programmer: |
| 1130 | return |
| 1131 | # Initialize firmware programmer |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1132 | if self._servo_type.startswith('servo_v2'): |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1133 | self._programmer = firmware_programmer.ProgrammerV2(self) |
Wai-Hong Tam | c36c4d2 | 2016-09-09 10:39:45 -0700 | [diff] [blame] | 1134 | self._programmer_rw = firmware_programmer.ProgrammerV2RwOnly(self) |
Kevin Cheng | df2e29f | 2016-09-09 02:31:22 -0700 | [diff] [blame] | 1135 | # Both servo v3 and v4 use the same programming methods so just leverage |
| 1136 | # ProgrammerV3 for servo v4 as well. |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1137 | elif (self._servo_type.startswith('servo_v3') or |
| 1138 | self._servo_type.startswith('servo_v4')): |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1139 | self._programmer = firmware_programmer.ProgrammerV3(self) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1140 | self._programmer_rw = firmware_programmer.ProgrammerV3RwOnly(self) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1141 | else: |
| 1142 | raise error.TestError( |
| 1143 | 'No firmware programmer for servo version: %s' % |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1144 | self._servo_type) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1145 | |
| 1146 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1147 | def program_bios(self, image, rw_only=False): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1148 | """Program bios on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1149 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1150 | @param image: a string, file name of the BIOS image to program |
| 1151 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1152 | @param rw_only: True to only program the RW portion of BIOS. |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1153 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1154 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1155 | self._initialize_programmer() |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 1156 | if not self.is_localhost(): |
| 1157 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1158 | if rw_only: |
| 1159 | self._programmer_rw.program_bios(image) |
| 1160 | else: |
| 1161 | self._programmer.program_bios(image) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1162 | |
| 1163 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1164 | def program_ec(self, image, rw_only=False): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1165 | """Program ec on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1166 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1167 | @param image: a string, file name of the EC image to program |
| 1168 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1169 | @param rw_only: True to only program the RW portion of EC. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1170 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1171 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1172 | self._initialize_programmer() |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 1173 | if not self.is_localhost(): |
| 1174 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1175 | if rw_only: |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1176 | self._programmer_rw.program_ec(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1177 | else: |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1178 | self._programmer.program_ec(image) |
| 1179 | |
| 1180 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1181 | def extract_ec_image(self, board, model, tarball_path): |
| 1182 | """Helper function to extract EC image from downloaded tarball. |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1183 | |
Shelley Chen | ac61d5a | 2019-06-24 15:35:46 -0700 | [diff] [blame] | 1184 | @param board: The DUT board name. |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1185 | @param model: The DUT model name. |
| 1186 | @param tarball_path: The path of the downloaded build tarball. |
Justin TerAvest | 1d0c1bb | 2019-12-13 11:53:25 -0700 | [diff] [blame] | 1187 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1188 | @return: Path to extracted EC image. |
| 1189 | """ |
Justin TerAvest | 1d0c1bb | 2019-12-13 11:53:25 -0700 | [diff] [blame] | 1190 | # Best effort; try to retrieve the EC board from the version as |
| 1191 | # reported by the EC. |
| 1192 | ec_board = None |
| 1193 | try: |
| 1194 | ec_board = self.get('ec_board') |
| 1195 | except Exception as err: |
| 1196 | logging.info('Failed to get ec_board value; ignoring') |
| 1197 | pass |
| 1198 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1199 | # Array of candidates for EC image |
| 1200 | ec_image_candidates = ['ec.bin', |
| 1201 | '%s/ec.bin' % model, |
Justin TerAvest | 1d0c1bb | 2019-12-13 11:53:25 -0700 | [diff] [blame] | 1202 | '%s/ec.bin' % board] |
| 1203 | if ec_board: |
| 1204 | ec_image_candidates.append('%s/ec.bin' % ec_board) |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1205 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1206 | # Extract EC image from tarball |
| 1207 | dest_dir = os.path.join(os.path.dirname(tarball_path), 'EC') |
| 1208 | ec_image = _extract_image_from_tarball(tarball_path, dest_dir, |
| 1209 | ec_image_candidates) |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1210 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1211 | # Return path to EC image |
| 1212 | return os.path.join(dest_dir, ec_image) |
| 1213 | |
| 1214 | |
| 1215 | def extract_bios_image(self, board, model, tarball_path): |
| 1216 | """Helper function to extract BIOS image from downloaded tarball. |
| 1217 | |
| 1218 | @param board: The DUT board name. |
| 1219 | @param model: The DUT model name. |
| 1220 | @param tarball_path: The path of the downloaded build tarball. |
| 1221 | |
| 1222 | @return: Path to extracted BIOS image. |
| 1223 | """ |
| 1224 | |
| 1225 | # Array of candidates for BIOS image |
| 1226 | bios_image_candidates = ['image.bin', |
| 1227 | 'image-%s.bin' % model, |
| 1228 | 'image-%s.bin' % board] |
| 1229 | |
| 1230 | # Extract BIOS image from tarball |
| 1231 | dest_dir = os.path.join(os.path.dirname(tarball_path), 'BIOS') |
| 1232 | bios_image = _extract_image_from_tarball(tarball_path, dest_dir, |
| 1233 | bios_image_candidates) |
| 1234 | |
| 1235 | # Return path to BIOS image |
| 1236 | return os.path.join(dest_dir, bios_image) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1237 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1238 | |
| 1239 | def _switch_usbkey_power(self, power_state, detection_delay=False): |
| 1240 | """Switch usbkey power. |
| 1241 | |
| 1242 | This function switches usbkey power by setting the value of |
| 1243 | 'prtctl4_pwren'. If the power is already in the |
| 1244 | requested state, this function simply returns. |
| 1245 | |
| 1246 | @param power_state: A string, 'on' or 'off'. |
| 1247 | @param detection_delay: A boolean value, if True, sleep |
| 1248 | for |USB_DETECTION_DELAY| after switching |
| 1249 | the power on. |
| 1250 | """ |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 1251 | # TODO(kevcheng): Forgive me for this terrible hack. This is just to |
| 1252 | # handle beaglebones that haven't yet updated and have the |
| 1253 | # safe_switch_usbkey_power RPC. I'll remove this once all beaglebones |
| 1254 | # have been updated and also think about a better way to handle |
| 1255 | # situations like this. |
| 1256 | try: |
| 1257 | self._server.safe_switch_usbkey_power(power_state) |
| 1258 | except Exception: |
| 1259 | self.set('prtctl4_pwren', power_state) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1260 | if power_state == 'off': |
| 1261 | time.sleep(self.USB_POWEROFF_DELAY) |
| 1262 | elif detection_delay: |
| 1263 | time.sleep(self.USB_DETECTION_DELAY) |
| 1264 | |
| 1265 | |
| 1266 | def switch_usbkey(self, usb_state): |
| 1267 | """Connect USB flash stick to either host or DUT, or turn USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1268 | |
| 1269 | This function switches the servo multiplexer to provide electrical |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1270 | connection between the USB port J3 and either host or DUT side. It |
| 1271 | can also be used to turn the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1272 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1273 | Switching to 'dut' or 'host' is accompanied by powercycling |
| 1274 | of the USB stick, because it sometimes gets wedged if the mux |
| 1275 | is switched while the stick power is on. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1276 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1277 | @param usb_state: A string, one of 'dut', 'host', or 'off'. |
| 1278 | 'dut' and 'host' indicate which side the |
| 1279 | USB flash device is required to be connected to. |
| 1280 | 'off' indicates turning the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1281 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1282 | @raise: error.TestError in case the parameter is not 'dut' |
| 1283 | 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1284 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1285 | if self.get_usbkey_direction() == usb_state: |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1286 | return |
| 1287 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1288 | if usb_state == 'off': |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1289 | self._switch_usbkey_power('off') |
| 1290 | self._usb_state = usb_state |
| 1291 | return |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1292 | elif usb_state == 'host': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1293 | mux_direction = 'servo_sees_usbkey' |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1294 | elif usb_state == 'dut': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1295 | mux_direction = 'dut_sees_usbkey' |
| 1296 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1297 | raise error.TestError('Unknown USB state request: %s' % usb_state) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1298 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1299 | self._switch_usbkey_power('off') |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 1300 | # TODO(kevcheng): Forgive me for this terrible hack. This is just to |
| 1301 | # handle beaglebones that haven't yet updated and have the |
| 1302 | # safe_switch_usbkey RPC. I'll remove this once all beaglebones have |
| 1303 | # been updated and also think about a better way to handle situations |
| 1304 | # like this. |
| 1305 | try: |
| 1306 | self._server.safe_switch_usbkey(mux_direction) |
| 1307 | except Exception: |
| 1308 | self.set('usb_mux_sel1', mux_direction) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1309 | time.sleep(self.USB_POWEROFF_DELAY) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1310 | self._switch_usbkey_power('on', usb_state == 'host') |
| 1311 | self._usb_state = usb_state |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1312 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1313 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1314 | def get_usbkey_direction(self): |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1315 | """Get which side USB is connected to or 'off' if usb power is off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1316 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1317 | @return: A string, one of 'dut', 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1318 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1319 | if not self._usb_state: |
| 1320 | if self.get('prtctl4_pwren') == 'off': |
| 1321 | self._usb_state = 'off' |
| 1322 | elif self.get('usb_mux_sel1').startswith('dut'): |
| 1323 | self._usb_state = 'dut' |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1324 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1325 | self._usb_state = 'host' |
| 1326 | return self._usb_state |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1327 | |
| 1328 | |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1329 | def set_servo_v4_role(self, role): |
| 1330 | """Set the power role of servo v4, either 'src' or 'snk'. |
| 1331 | |
| 1332 | It does nothing if not a servo v4. |
| 1333 | |
| 1334 | @param role: Power role for DUT port on servo v4, either 'src' or 'snk'. |
| 1335 | """ |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1336 | if self._servo_type.startswith('servo_v4'): |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1337 | value = self.get('servo_v4_role') |
| 1338 | if value != role: |
| 1339 | self.set_nocheck('servo_v4_role', role) |
| 1340 | else: |
| 1341 | logging.debug('Already in the role: %s.', role) |
| 1342 | else: |
| 1343 | logging.debug('Not a servo v4, unable to set role to %s.', role) |
| 1344 | |
| 1345 | |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1346 | def supports_built_in_pd_control(self): |
| 1347 | """Return whether the servo type supports pd charging and control.""" |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1348 | if 'servo_v4' not in self._servo_type: |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1349 | # Only servo v4 supports this feature. |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1350 | logging.info('%r type does not support pd control.', |
| 1351 | self._servo_type) |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1352 | return False |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1353 | # On servo v4, it still needs to be the type-c version. |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1354 | if not self.get('servo_v4_type') == 'type-c': |
| 1355 | logging.info('PD controls require a type-c servo v4.') |
| 1356 | return False |
| 1357 | # Lastly, one cannot really do anything without a plugged in charger. |
| 1358 | chg_port_mv = self.get('ppchg5_mv') |
| 1359 | if chg_port_mv < V4_CHG_ATTACHED_MIN_VOLTAGE_MV: |
| 1360 | logging.warn('It appears that no charger is plugged into servo v4. ' |
| 1361 | 'Charger port voltage: %dmV', chg_port_mv) |
| 1362 | return False |
| 1363 | logging.info('Charger port voltage: %dmV', chg_port_mv) |
| 1364 | return True |
| 1365 | |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1366 | def dts_mode_is_valid(self): |
| 1367 | """Return whether servo setup supports dts mode control for cr50.""" |
| 1368 | if 'servo_v4' not in self._servo_type: |
| 1369 | # Only servo v4 supports this feature. |
| 1370 | logging.debug('%r type does not support dts mode control.', |
| 1371 | self._servo_type) |
| 1372 | return False |
| 1373 | # On servo v4, it still needs ot be the type-c version. |
| 1374 | if not 'type-c' == self.get('servo_v4_type'): |
| 1375 | logging.info('DTS controls require a type-c servo v4.') |
| 1376 | return False |
| 1377 | return True |
| 1378 | |
| 1379 | def dts_mode_is_safe(self): |
| 1380 | """Return whether servo setup supports dts mode without losing access. |
| 1381 | |
| 1382 | DTS mode control exists but the main device might go through ccd. |
| 1383 | In that case, it's only safe to control dts mode if the main device |
| 1384 | is legacy as otherwise the connection to the main device cuts out. |
| 1385 | """ |
| 1386 | return self.dts_mode_is_valid() and self.main_device_is_flex() |
| 1387 | |
| 1388 | def get_dts_mode(self): |
| 1389 | """Return servo dts mode. |
| 1390 | |
| 1391 | @returns: on/off whether dts is on or off |
| 1392 | """ |
| 1393 | if not self.dts_mode_is_valid(): |
| 1394 | logging.info('Not a valid servo setup. Unable to get dts mode.') |
| 1395 | return |
| 1396 | return self.get('servo_v4_dts_mode') |
| 1397 | |
| 1398 | def set_dts_mode(self, state): |
| 1399 | """Set servo dts mode to off or on. |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1400 | |
| 1401 | It does nothing if not a servo v4. Disable the ccd watchdog if we're |
| 1402 | disabling dts mode. CCD will disconnect. The watchdog only allows CCD |
| 1403 | to disconnect for 10 seconds until it kills servod. Disable the |
| 1404 | watchdog, so CCD can stay disconnected indefinitely. |
| 1405 | |
| 1406 | @param state: Set servo v4 dts mode 'off' or 'on'. |
| 1407 | """ |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1408 | if not self.dts_mode_is_valid(): |
| 1409 | logging.info('Not a valid servo setup. Unable to set dts mode %s.', |
| 1410 | state) |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1411 | return |
| 1412 | |
| 1413 | # TODO(mruthven): remove watchdog check once the labstation has been |
| 1414 | # updated to have support for modifying the watchdog. |
Ruben Rodriguez Buchillon | f3f63b5 | 2020-01-27 17:56:49 -0800 | [diff] [blame] | 1415 | set_watchdog = (self.has_control('watchdog') and |
| 1416 | 'ccd' in self._servo_type) |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1417 | enable_watchdog = state == 'on' |
| 1418 | |
| 1419 | if set_watchdog and not enable_watchdog: |
| 1420 | self.set_nocheck('watchdog_remove', 'ccd') |
| 1421 | |
| 1422 | self.set_nocheck('servo_v4_dts_mode', state) |
| 1423 | |
| 1424 | if set_watchdog and enable_watchdog: |
| 1425 | self.set_nocheck('watchdog_add', 'ccd') |
| 1426 | |
| 1427 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1428 | @property |
| 1429 | def uart_logs_dir(self): |
| 1430 | """Return the directory to save UART logs.""" |
| 1431 | return self._uart.logs_dir if self._uart else "" |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1432 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1433 | |
| 1434 | @uart_logs_dir.setter |
| 1435 | def uart_logs_dir(self, logs_dir): |
| 1436 | """Set directory to save UART logs. |
| 1437 | |
| 1438 | @param logs_dir String of directory name.""" |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1439 | if self._uart: |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1440 | self._uart.logs_dir = logs_dir |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1441 | |
| 1442 | |
| 1443 | def close(self): |
| 1444 | """Close the servo object.""" |
| 1445 | if self._uart: |
| 1446 | self._uart.stop_capture() |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1447 | self._uart.dump() |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1448 | self._uart = None |