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 | |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 8 | import os |
| 9 | |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 10 | import logging, re, time, xmlrpclib |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 11 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import error |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 13 | from autotest_lib.server.cros.servo import firmware_programmer |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 14 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 15 | # Time to wait when probing for a usb device, it takes on avg 17 seconds |
| 16 | # to do a full probe. |
| 17 | _USB_PROBE_TIMEOUT = 40 |
| 18 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 19 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 20 | class _PowerStateController(object): |
| 21 | |
| 22 | """Class to provide board-specific power operations. |
| 23 | |
| 24 | This class is responsible for "power on" and "power off" |
| 25 | operations that can operate without making assumptions in |
| 26 | advance about board state. It offers an interface that |
| 27 | abstracts out the different sequences required for different |
| 28 | board types. |
| 29 | |
| 30 | """ |
| 31 | |
| 32 | # Constants acceptable to be passed for the `rec_mode` parameter |
| 33 | # to power_on(). |
| 34 | # |
| 35 | # REC_ON: Boot the DUT in recovery mode, i.e. boot from USB or |
| 36 | # SD card. |
| 37 | # REC_OFF: Boot in normal mode, i.e. boot from internal storage. |
| 38 | |
| 39 | REC_ON = 'rec' |
| 40 | REC_OFF = 'on' |
| 41 | |
| 42 | # Delay in seconds needed between asserting and de-asserting |
| 43 | # warm reset. |
| 44 | _RESET_HOLD_TIME = 0.5 |
| 45 | |
| 46 | def __init__(self, servo): |
| 47 | """Initialize the power state control. |
| 48 | |
| 49 | @param servo Servo object providing the underlying `set` and `get` |
| 50 | methods for the target controls. |
| 51 | |
| 52 | """ |
| 53 | self._servo = servo |
| 54 | |
| 55 | def reset(self): |
| 56 | """Force the DUT to reset. |
| 57 | |
| 58 | The DUT is guaranteed to be on at the end of this call, |
| 59 | regardless of its previous state, provided that there is |
| 60 | working OS software. This also guarantees that the EC has |
| 61 | been restarted. |
| 62 | |
| 63 | """ |
| 64 | self._servo.set_nocheck('power_state', 'reset') |
| 65 | |
| 66 | def warm_reset(self): |
| 67 | """Apply warm reset to the DUT. |
| 68 | |
| 69 | This asserts, then de-asserts the 'warm_reset' signal. |
| 70 | Generally, this causes the board to restart. |
| 71 | |
| 72 | """ |
| 73 | self._servo.set_get_all(['warm_reset:on', |
| 74 | 'sleep:%.4f' % self._RESET_HOLD_TIME, |
| 75 | 'warm_reset:off']) |
| 76 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 77 | def power_off(self): |
| 78 | """Force the DUT to power off. |
| 79 | |
| 80 | The DUT is guaranteed to be off at the end of this call, |
| 81 | regardless of its previous state, provided that there is |
| 82 | working EC and boot firmware. There is no requirement for |
| 83 | working OS software. |
| 84 | |
| 85 | """ |
| 86 | self._servo.set_nocheck('power_state', 'off') |
| 87 | |
| 88 | def power_on(self, rec_mode=REC_OFF): |
| 89 | """Force the DUT to power on. |
| 90 | |
| 91 | Prior to calling this function, the DUT must be powered off, |
| 92 | e.g. with a call to `power_off()`. |
| 93 | |
| 94 | At power on, recovery mode is set as specified by the |
| 95 | corresponding argument. When booting with recovery mode on, it |
| 96 | is the caller's responsibility to unplug/plug in a bootable |
| 97 | external storage device. |
| 98 | |
| 99 | If the DUT requires a delay after powering on but before |
| 100 | processing inputs such as USB stick insertion, the delay is |
| 101 | handled by this method; the caller is not responsible for such |
| 102 | delays. |
| 103 | |
| 104 | @param rec_mode Setting of recovery mode to be applied at |
| 105 | power on. default: REC_OFF aka 'off' |
| 106 | |
| 107 | """ |
| 108 | self._servo.set_nocheck('power_state', rec_mode) |
| 109 | |
| 110 | |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 111 | class Servo(object): |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 112 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 113 | """Manages control of a Servo board. |
| 114 | |
| 115 | Servo is a board developed by hardware group to aide in the debug and |
| 116 | control of various partner devices. Servo's features include the simulation |
| 117 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 118 | class manages setting up and communicating with a servo demon (servod) |
| 119 | process. It provides both high-level functions for common servo tasks and |
| 120 | low-level functions for directly setting and reading gpios. |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 121 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 122 | """ |
| 123 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 124 | # Power button press delays in seconds. |
J. Richard Barnette | d2e4cbd | 2012-06-29 12:18:40 -0700 | [diff] [blame] | 125 | # |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 126 | # The EC specification says that 8.0 seconds should be enough |
| 127 | # for the long power press. However, some platforms need a bit |
| 128 | # more time. Empirical testing has found these requirements: |
| 129 | # Alex: 8.2 seconds |
| 130 | # ZGB: 8.5 seconds |
| 131 | # The actual value is set to the largest known necessary value. |
| 132 | # |
| 133 | # TODO(jrbarnette) Being generous is the right thing to do for |
| 134 | # existing platforms, but if this code is to be used for |
| 135 | # qualification of new hardware, we should be less generous. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 136 | SHORT_DELAY = 0.1 |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 137 | |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 138 | # Maximum number of times to re-read power button on release. |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 139 | GET_RETRY_MAX = 10 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 140 | |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 141 | # Delays to deal with DUT state transitions. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 142 | SLEEP_DELAY = 6 |
| 143 | BOOT_DELAY = 10 |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 144 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 145 | # Default minimum time interval between 'press' and 'release' |
| 146 | # keyboard events. |
Vic Yang | 0aca1c2 | 2012-11-19 18:33:56 -0800 | [diff] [blame] | 147 | SERVO_KEY_PRESS_DELAY = 0.1 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 148 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 149 | # Time to toggle recovery switch on and off. |
| 150 | REC_TOGGLE_DELAY = 0.1 |
| 151 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 152 | # Time to toggle development switch on and off. |
| 153 | DEV_TOGGLE_DELAY = 0.1 |
| 154 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 155 | # Time between an usb disk plugged-in and detected in the system. |
| 156 | USB_DETECTION_DELAY = 10 |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 157 | # Time to keep USB power off before and after USB mux direction is changed |
| 158 | USB_POWEROFF_DELAY = 2 |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 159 | |
Simran Basi | b7850bb | 2013-07-24 12:33:42 -0700 | [diff] [blame] | 160 | # Time to wait before timing out on servo initialization. |
| 161 | INIT_TIMEOUT_SECS = 10 |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame] | 162 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 163 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 164 | def __init__(self, servo_host, servo_serial=None): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 165 | """Sets up the servo communication infrastructure. |
| 166 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 167 | @param servo_host: A ServoHost object representing |
| 168 | the host running servod. |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 169 | @param servo_serial: Serial number of the servo board. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 170 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 171 | # TODO(fdeng): crbug.com/298379 |
| 172 | # We should move servo_host object out of servo object |
| 173 | # to minimize the dependencies on the rest of Autotest. |
| 174 | self._servo_host = servo_host |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 175 | self._servo_serial = servo_serial |
Richard Barnette | 180efe6 | 2016-12-02 23:20:44 +0000 | [diff] [blame] | 176 | self._server = servo_host.get_servod_server_proxy() |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 177 | self._power_state = _PowerStateController(self) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 178 | self._usb_state = None |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 179 | self._programmer = None |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 180 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 181 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 182 | @property |
| 183 | def servo_serial(self): |
| 184 | """Returns the serial number of the servo board.""" |
| 185 | return self._servo_serial |
| 186 | |
| 187 | |
Tom Wai-Hong Tam | 22ee0e5 | 2013-04-03 13:36:39 +0800 | [diff] [blame] | 188 | def get_power_state_controller(self): |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 189 | """Return the power state controller for this Servo. |
| 190 | |
| 191 | The power state controller provides board-independent |
| 192 | interfaces for reset, power-on, power-off operations. |
| 193 | |
| 194 | """ |
| 195 | return self._power_state |
| 196 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 197 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 198 | def initialize_dut(self, cold_reset=False): |
| 199 | """Initializes a dut for testing purposes. |
| 200 | |
| 201 | This sets various servo signals back to default values |
| 202 | appropriate for the target board. By default, if the DUT |
| 203 | is already on, it stays on. If the DUT is powered off |
| 204 | before initialization, its state afterward is unspecified. |
| 205 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 206 | Rationale: Basic initialization of servo sets the lid open, |
| 207 | when there is a lid. This operation won't affect powered on |
| 208 | units; however, setting the lid open may power on a unit |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 209 | that's off, depending on the board type and previous state |
| 210 | of the device. |
| 211 | |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 212 | If `cold_reset` is a true value, the DUT and its EC will be |
| 213 | reset, and the DUT rebooted in normal mode. |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 214 | |
| 215 | @param cold_reset If True, cold reset the device after |
| 216 | initialization. |
| 217 | """ |
| 218 | self._server.hwinit() |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 219 | self.set('usb_mux_oe1', 'on') |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 220 | self._usb_state = None |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 221 | self.switch_usbkey('off') |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 222 | if cold_reset: |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 223 | self._power_state.reset() |
J. Richard Barnette | 1777f5d | 2014-09-03 15:18:19 -0700 | [diff] [blame] | 224 | logging.debug('Servo initialized, version is %s', |
| 225 | self._server.get_version()) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 226 | |
| 227 | |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 228 | def is_localhost(self): |
| 229 | """Is the servod hosted locally? |
| 230 | |
| 231 | Returns: |
| 232 | True if local hosted; otherwise, False. |
| 233 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 234 | return self._servo_host.is_localhost() |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 235 | |
| 236 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 237 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 238 | """Simulate a long power button press.""" |
J. Richard Barnette | f12bff2 | 2012-07-18 14:41:06 -0700 | [diff] [blame] | 239 | # After a long power press, the EC may ignore the next power |
| 240 | # button press (at least on Alex). To guarantee that this |
| 241 | # won't happen, we need to allow the EC one second to |
| 242 | # collect itself. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 243 | self._server.power_long_press() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 244 | |
| 245 | |
| 246 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 247 | """Simulate a normal power button press.""" |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 248 | self._server.power_normal_press() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 249 | |
| 250 | |
| 251 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 252 | """Simulate a short power button press.""" |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 253 | self._server.power_short_press() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 254 | |
| 255 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 256 | def power_key(self, press_secs=''): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 257 | """Simulate a power button press. |
| 258 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 259 | @param press_secs : Str. Time to press key. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 260 | """ |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 261 | self._server.power_key(press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 262 | |
| 263 | |
| 264 | def lid_open(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 265 | """Simulate opening the lid and raise exception if all attempts fail""" |
| 266 | self.set('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 267 | |
| 268 | |
| 269 | def lid_close(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 270 | """Simulate closing the lid and raise exception if all attempts fail |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 271 | |
| 272 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 273 | """ |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 274 | self.set('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 275 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 276 | |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 277 | def volume_up(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 278 | """Simulate pushing the volume down button. |
| 279 | |
| 280 | @param timeout: Timeout for setting the volume. |
| 281 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 282 | self.set_get_all(['volume_up:yes', |
| 283 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 284 | 'volume_up:no']) |
| 285 | # we need to wait for commands to take effect before moving on |
| 286 | time_left = float(timeout) |
| 287 | while time_left > 0.0: |
| 288 | value = self.get('volume_up') |
| 289 | if value == 'no': |
| 290 | return |
| 291 | time.sleep(self.SHORT_DELAY) |
| 292 | time_left = time_left - self.SHORT_DELAY |
| 293 | raise error.TestFail("Failed setting volume_up to no") |
| 294 | |
| 295 | def volume_down(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 296 | """Simulate pushing the volume down button. |
| 297 | |
| 298 | @param timeout: Timeout for setting the volume. |
| 299 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 300 | self.set_get_all(['volume_down:yes', |
| 301 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 302 | 'volume_down:no']) |
| 303 | # we need to wait for commands to take effect before moving on |
| 304 | time_left = float(timeout) |
| 305 | while time_left > 0.0: |
| 306 | value = self.get('volume_down') |
| 307 | if value == 'no': |
| 308 | return |
| 309 | time.sleep(self.SHORT_DELAY) |
| 310 | time_left = time_left - self.SHORT_DELAY |
| 311 | raise error.TestFail("Failed setting volume_down to no") |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 312 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 313 | def ctrl_d(self, press_secs=''): |
| 314 | """Simulate Ctrl-d simultaneous button presses. |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 315 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 316 | @param press_secs : Str. Time to press key. |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 317 | """ |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 318 | self._server.ctrl_d(press_secs) |
Gediminas Ramanauskas | 9da80f2 | 2012-11-14 12:59:43 -0800 | [diff] [blame] | 319 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 320 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 321 | def ctrl_u(self): |
| 322 | """Simulate Ctrl-u simultaneous button presses. |
| 323 | |
| 324 | @param press_secs : Str. Time to press key. |
| 325 | """ |
| 326 | self._server.ctrl_u() |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 327 | |
| 328 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 329 | def ctrl_enter(self, press_secs=''): |
| 330 | """Simulate Ctrl-enter simultaneous button presses. |
| 331 | |
| 332 | @param press_secs : Str. Time to press key. |
| 333 | """ |
| 334 | self._server.ctrl_enter(press_secs) |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 335 | |
| 336 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 337 | def d_key(self, press_secs=''): |
| 338 | """Simulate Enter key button press. |
| 339 | |
| 340 | @param press_secs : Str. Time to press key. |
| 341 | """ |
| 342 | self._server.d_key(press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 343 | |
| 344 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 345 | def ctrl_key(self, press_secs=''): |
| 346 | """Simulate Enter key button press. |
| 347 | |
| 348 | @param press_secs : Str. Time to press key. |
| 349 | """ |
| 350 | self._server.ctrl_key(press_secs) |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 351 | |
| 352 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 353 | def enter_key(self, press_secs=''): |
| 354 | """Simulate Enter key button press. |
| 355 | |
| 356 | @param press_secs : Str. Time to press key. |
| 357 | """ |
| 358 | self._server.enter_key(press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 359 | |
| 360 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 361 | def refresh_key(self, press_secs=''): |
| 362 | """Simulate Refresh key (F3) button press. |
| 363 | |
| 364 | @param press_secs : Str. Time to press key. |
| 365 | """ |
| 366 | self._server.refresh_key(press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 367 | |
| 368 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 369 | def ctrl_refresh_key(self, press_secs=''): |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 370 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 371 | |
| 372 | This key combination is an alternative of Space key. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 373 | |
| 374 | @param press_secs : Str. Time to press key. |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 375 | """ |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 376 | self._server.ctrl_refresh_key(press_secs) |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 377 | |
| 378 | |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 379 | def imaginary_key(self, press_secs=''): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 380 | """Simulate imaginary key button press. |
| 381 | |
| 382 | Maps to a key that doesn't physically exist. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 383 | |
| 384 | @param press_secs : Str. Time to press key. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 385 | """ |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 386 | self._server.imaginary_key(press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 387 | |
| 388 | |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 389 | def sysrq_x(self, press_secs=''): |
| 390 | """Simulate Alt VolumeUp X simulataneous press. |
| 391 | |
| 392 | This key combination is the kernel system request (sysrq) X. |
| 393 | |
| 394 | @param press_secs : Str. Time to press key. |
| 395 | """ |
| 396 | self._server.sysrq_x(press_secs) |
| 397 | |
| 398 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 399 | def toggle_recovery_switch(self): |
| 400 | """Toggle recovery switch on and off.""" |
| 401 | self.enable_recovery_mode() |
| 402 | time.sleep(self.REC_TOGGLE_DELAY) |
| 403 | self.disable_recovery_mode() |
| 404 | |
| 405 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 406 | def enable_recovery_mode(self): |
| 407 | """Enable recovery mode on device.""" |
| 408 | self.set('rec_mode', 'on') |
| 409 | |
| 410 | |
| 411 | def disable_recovery_mode(self): |
| 412 | """Disable recovery mode on device.""" |
| 413 | self.set('rec_mode', 'off') |
| 414 | |
| 415 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 416 | def toggle_development_switch(self): |
| 417 | """Toggle development switch on and off.""" |
| 418 | self.enable_development_mode() |
| 419 | time.sleep(self.DEV_TOGGLE_DELAY) |
| 420 | self.disable_development_mode() |
| 421 | |
| 422 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 423 | def enable_development_mode(self): |
| 424 | """Enable development mode on device.""" |
| 425 | self.set('dev_mode', 'on') |
| 426 | |
| 427 | |
| 428 | def disable_development_mode(self): |
| 429 | """Disable development mode on device.""" |
| 430 | self.set('dev_mode', 'off') |
| 431 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 432 | def boot_devmode(self): |
| 433 | """Boot a dev-mode device that is powered off.""" |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 434 | self.power_short_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 435 | self.pass_devmode() |
| 436 | |
| 437 | |
| 438 | def pass_devmode(self): |
| 439 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 440 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 441 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 442 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 443 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 444 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 445 | def get_board(self): |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 446 | """Get the board connected to servod.""" |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 447 | return self._server.get_board() |
| 448 | |
| 449 | |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 450 | def get_base_board(self): |
| 451 | """Get the board of the base connected to servod.""" |
Wai-Hong Tam | 7f6169e | 2017-09-29 09:23:48 -0700 | [diff] [blame^] | 452 | try: |
| 453 | return self._server.get_base_board() |
| 454 | except xmlrpclib.Fault as e: |
| 455 | # TODO(waihong): Remove the following compatibility check when |
| 456 | # the new versions of hdctools are deployed. |
| 457 | if 'not supported' in str(e): |
| 458 | logging.warning('The servod is too old that get_base_board ' |
| 459 | 'not supported.') |
| 460 | return '' |
| 461 | raise |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 462 | |
| 463 | |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 464 | def _get_xmlrpclib_exception(self, xmlexc): |
| 465 | """Get meaningful exception string from xmlrpc. |
| 466 | |
| 467 | Args: |
| 468 | xmlexc: xmlrpclib.Fault object |
| 469 | |
| 470 | xmlrpclib.Fault.faultString has the following format: |
| 471 | |
| 472 | <type 'exception type'>:'actual error message' |
| 473 | |
| 474 | Parse and return the real exception from the servod side instead of the |
| 475 | less meaningful one like, |
| 476 | <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no |
| 477 | attribute 'hw_driver'"> |
| 478 | |
| 479 | Returns: |
| 480 | string of underlying exception raised in servod. |
| 481 | """ |
| 482 | return re.sub('^.*>:', '', xmlexc.faultString) |
| 483 | |
| 484 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 485 | def get(self, gpio_name): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 486 | """Get the value of a gpio from Servod. |
| 487 | |
| 488 | @param gpio_name Name of the gpio. |
| 489 | """ |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 490 | assert gpio_name |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 491 | try: |
Simran Basi | 1cbc5f2 | 2013-07-17 14:23:58 -0700 | [diff] [blame] | 492 | return self._server.get(gpio_name) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 493 | except xmlrpclib.Fault as e: |
| 494 | err_msg = "Getting '%s' :: %s" % \ |
| 495 | (gpio_name, self._get_xmlrpclib_exception(e)) |
| 496 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 497 | |
| 498 | |
| 499 | def set(self, gpio_name, gpio_value): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 500 | """Set and check the value of a gpio using Servod. |
| 501 | |
| 502 | @param gpio_name Name of the gpio. |
| 503 | @param gpio_value New setting for the gpio. |
| 504 | """ |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 505 | self.set_nocheck(gpio_name, gpio_value) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 506 | retry_count = Servo.GET_RETRY_MAX |
| 507 | while gpio_value != self.get(gpio_name) and retry_count: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 508 | logging.warning("%s != %s, retry %d", gpio_name, gpio_value, |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 509 | retry_count) |
| 510 | retry_count -= 1 |
| 511 | time.sleep(Servo.SHORT_DELAY) |
| 512 | if not retry_count: |
| 513 | assert gpio_value == self.get(gpio_name), \ |
| 514 | 'Servo failed to set %s to %s' % (gpio_name, gpio_value) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 515 | |
| 516 | |
| 517 | def set_nocheck(self, gpio_name, gpio_value): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 518 | """Set the value of a gpio using Servod. |
| 519 | |
| 520 | @param gpio_name Name of the gpio. |
| 521 | @param gpio_value New setting for the gpio. |
| 522 | """ |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 523 | assert gpio_name and gpio_value |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 524 | logging.info('Setting %s to %s', gpio_name, gpio_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 525 | try: |
Simran Basi | 1cbc5f2 | 2013-07-17 14:23:58 -0700 | [diff] [blame] | 526 | self._server.set(gpio_name, gpio_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 527 | except xmlrpclib.Fault as e: |
| 528 | err_msg = "Setting '%s' to '%s' :: %s" % \ |
| 529 | (gpio_name, gpio_value, self._get_xmlrpclib_exception(e)) |
| 530 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 531 | |
| 532 | |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 533 | def set_get_all(self, controls): |
| 534 | """Set &| get one or more control values. |
| 535 | |
| 536 | @param controls: list of strings, controls to set &| get. |
| 537 | |
| 538 | @raise: error.TestError in case error occurs setting/getting values. |
| 539 | """ |
| 540 | rv = [] |
| 541 | try: |
Vic Yang | cad9acb | 2013-05-21 14:02:05 +0800 | [diff] [blame] | 542 | logging.info('Set/get all: %s', str(controls)) |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 543 | rv = self._server.set_get_all(controls) |
| 544 | except xmlrpclib.Fault as e: |
| 545 | # TODO(waihong): Remove the following backward compatibility when |
| 546 | # the new versions of hdctools are deployed. |
| 547 | if 'not supported' in str(e): |
| 548 | logging.warning('The servod is too old that set_get_all ' |
| 549 | 'not supported. Use set and get instead.') |
| 550 | for control in controls: |
| 551 | if ':' in control: |
| 552 | (name, value) = control.split(':') |
| 553 | if name == 'sleep': |
| 554 | time.sleep(float(value)) |
| 555 | else: |
| 556 | self.set_nocheck(name, value) |
| 557 | rv.append(True) |
| 558 | else: |
| 559 | rv.append(self.get(name)) |
| 560 | else: |
| 561 | err_msg = "Problem with '%s' :: %s" % \ |
| 562 | (controls, self._get_xmlrpclib_exception(e)) |
| 563 | raise error.TestFail(err_msg) |
| 564 | return rv |
| 565 | |
| 566 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 567 | # TODO(waihong) It may fail if multiple servo's are connected to the same |
| 568 | # host. Should look for a better way, like the USB serial name, to identify |
| 569 | # the USB device. |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 570 | # TODO(sbasi) Remove this code from autoserv once firmware tests have been |
| 571 | # updated. |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 572 | def probe_host_usb_dev(self, timeout=_USB_PROBE_TIMEOUT): |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 573 | """Probe the USB disk device plugged-in the servo from the host side. |
| 574 | |
Kevin Cheng | eb06fe7 | 2016-08-22 15:26:32 -0700 | [diff] [blame] | 575 | 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] | 576 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 577 | @param timeout The timeout period when probing for the usb host device. |
| 578 | |
| 579 | @return: String of USB disk path (e.g. '/dev/sdb') or None. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 580 | """ |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 581 | return self._server.probe_host_usb_dev(timeout) or None |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 582 | |
| 583 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 584 | def image_to_servo_usb(self, image_path=None, |
| 585 | make_image_noninteractive=False): |
| 586 | """Install an image to the USB key plugged into the servo. |
| 587 | |
| 588 | This method may copy any image to the servo USB key including a |
| 589 | recovery image or a test image. These images are frequently used |
| 590 | for test purposes such as restoring a corrupted image or conducting |
| 591 | an upgrade of ec/fw/kernel as part of a test of a specific image part. |
| 592 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 593 | @param image_path Path on the host to the recovery image. |
| 594 | @param make_image_noninteractive Make the recovery image |
| 595 | noninteractive, therefore the DUT |
| 596 | will reboot automatically after |
| 597 | installation. |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 598 | """ |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 599 | # We're about to start plugging/unplugging the USB key. We |
| 600 | # don't know the state of the DUT, or what it might choose |
| 601 | # to do to the device after hotplug. To avoid surprises, |
| 602 | # force the DUT to be off. |
| 603 | self._server.hwinit() |
| 604 | self._power_state.power_off() |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 605 | |
| 606 | # Set up Servo's usb mux. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 607 | self.switch_usbkey('host') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 608 | if image_path: |
Tom Wai-Hong Tam | 42f136d | 2012-10-26 11:11:23 +0800 | [diff] [blame] | 609 | logging.info('Searching for usb device and copying image to it. ' |
| 610 | 'Please wait a few minutes...') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 611 | if not self._server.download_image_to_usb(image_path): |
| 612 | logging.error('Failed to transfer requested image to USB. ' |
| 613 | 'Please take a look at Servo Logs.') |
| 614 | raise error.AutotestError('Download image to usb failed.') |
| 615 | if make_image_noninteractive: |
| 616 | logging.info('Making image noninteractive') |
| 617 | if not self._server.make_image_noninteractive(): |
| 618 | logging.error('Failed to make image noninteractive. ' |
| 619 | 'Please take a look at Servo Logs.') |
| 620 | |
| 621 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 622 | def install_recovery_image(self, image_path=None, |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 623 | make_image_noninteractive=False): |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 624 | """Install the recovery image specified by the path onto the DUT. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 625 | |
| 626 | This method uses google recovery mode to install a recovery image |
| 627 | 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] | 628 | board specified by the usb_dev. If no image path is specified |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 629 | we use the recovery image already on the usb image. |
| 630 | |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 631 | @param image_path: Path on the host to the recovery image. |
| 632 | @param make_image_noninteractive: Make the recovery image |
| 633 | noninteractive, therefore the DUT will reboot automatically |
| 634 | after installation. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 635 | """ |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 636 | self.image_to_servo_usb(image_path, make_image_noninteractive) |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 637 | self._power_state.power_on(rec_mode=self._power_state.REC_ON) |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 638 | self.switch_usbkey('dut') |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 639 | |
| 640 | |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 641 | def _scp_image(self, image_path): |
| 642 | """Copy image to the servo host. |
| 643 | |
| 644 | When programming a firmware image on the DUT, the image must be |
| 645 | located on the host to which the servo device is connected. Sometimes |
| 646 | servo is controlled by a remote host, in this case the image needs to |
| 647 | be transferred to the remote host. |
| 648 | |
| 649 | @param image_path: a string, name of the firmware image file to be |
| 650 | transferred. |
| 651 | @return: a string, full path name of the copied file on the remote. |
| 652 | """ |
| 653 | |
| 654 | dest_path = os.path.join('/tmp', os.path.basename(image_path)) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 655 | self._servo_host.send_file(image_path, dest_path) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 656 | return dest_path |
| 657 | |
| 658 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 659 | def system(self, command, timeout=3600): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 660 | """Execute the passed in command on the servod host. |
| 661 | |
| 662 | @param command Command to be executed. |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 663 | @param timeout Maximum number of seconds of runtime allowed. Default to |
| 664 | 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 665 | """ |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 666 | logging.info('Will execute on servo host: %s', command) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 667 | self._servo_host.run(command, timeout=timeout) |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 668 | |
| 669 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 670 | def system_output(self, command, timeout=3600, |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 671 | ignore_status=False, args=()): |
| 672 | """Execute the passed in command on the servod host, return stdout. |
| 673 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 674 | @param command a string, the command to execute |
| 675 | @param timeout an int, max number of seconds to wait til command |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 676 | execution completes. Default to 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 677 | @param ignore_status a Boolean, if true - ignore command's nonzero exit |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 678 | status, otherwise an exception will be thrown |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 679 | @param args a tuple of strings, each becoming a separate command line |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 680 | parameter for the command |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 681 | @return command's stdout as a string. |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 682 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 683 | return self._servo_host.run(command, timeout=timeout, |
| 684 | ignore_status=ignore_status, |
| 685 | args=args).stdout.strip() |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 686 | |
| 687 | |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 688 | def get_servo_version(self): |
| 689 | """Get the version of the servo, e.g., servo_v2 or servo_v3. |
| 690 | |
| 691 | @return: The version of the servo. |
| 692 | |
| 693 | """ |
| 694 | return self._server.get_version() |
| 695 | |
| 696 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 697 | def _initialize_programmer(self, rw_only=False): |
| 698 | """Initialize the firmware programmer. |
| 699 | |
| 700 | @param rw_only: True to initialize a programmer which only |
| 701 | programs the RW portions. |
| 702 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 703 | if self._programmer: |
| 704 | return |
| 705 | # Initialize firmware programmer |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 706 | servo_version = self.get_servo_version() |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 707 | if servo_version.startswith('servo_v2'): |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 708 | self._programmer = firmware_programmer.ProgrammerV2(self) |
Wai-Hong Tam | c36c4d2 | 2016-09-09 10:39:45 -0700 | [diff] [blame] | 709 | self._programmer_rw = firmware_programmer.ProgrammerV2RwOnly(self) |
Kevin Cheng | df2e29f | 2016-09-09 02:31:22 -0700 | [diff] [blame] | 710 | # Both servo v3 and v4 use the same programming methods so just leverage |
| 711 | # ProgrammerV3 for servo v4 as well. |
| 712 | elif (servo_version.startswith('servo_v3') or |
| 713 | servo_version.startswith('servo_v4')): |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 714 | self._programmer = firmware_programmer.ProgrammerV3(self) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 715 | self._programmer_rw = firmware_programmer.ProgrammerV3RwOnly(self) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 716 | else: |
| 717 | raise error.TestError( |
| 718 | 'No firmware programmer for servo version: %s' % |
| 719 | servo_version) |
| 720 | |
| 721 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 722 | def program_bios(self, image, rw_only=False): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 723 | """Program bios on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 724 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 725 | @param image: a string, file name of the BIOS image to program |
| 726 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 727 | @param rw_only: True to only program the RW portion of BIOS. |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 728 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 729 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 730 | self._initialize_programmer() |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 731 | if not self.is_localhost(): |
| 732 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 733 | if rw_only: |
| 734 | self._programmer_rw.program_bios(image) |
| 735 | else: |
| 736 | self._programmer.program_bios(image) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 737 | |
| 738 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 739 | def program_ec(self, image, rw_only=False): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 740 | """Program ec on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 741 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 742 | @param image: a string, file name of the EC image to program |
| 743 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 744 | @param rw_only: True to only program the RW portion of EC. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 745 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 746 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 747 | self._initialize_programmer() |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 748 | if not self.is_localhost(): |
| 749 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 750 | if rw_only: |
| 751 | self._programmer_rw.program_ec(image) |
| 752 | else: |
| 753 | self._programmer.program_ec(image) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 754 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 755 | |
| 756 | def _switch_usbkey_power(self, power_state, detection_delay=False): |
| 757 | """Switch usbkey power. |
| 758 | |
| 759 | This function switches usbkey power by setting the value of |
| 760 | 'prtctl4_pwren'. If the power is already in the |
| 761 | requested state, this function simply returns. |
| 762 | |
| 763 | @param power_state: A string, 'on' or 'off'. |
| 764 | @param detection_delay: A boolean value, if True, sleep |
| 765 | for |USB_DETECTION_DELAY| after switching |
| 766 | the power on. |
| 767 | """ |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 768 | # TODO(kevcheng): Forgive me for this terrible hack. This is just to |
| 769 | # handle beaglebones that haven't yet updated and have the |
| 770 | # safe_switch_usbkey_power RPC. I'll remove this once all beaglebones |
| 771 | # have been updated and also think about a better way to handle |
| 772 | # situations like this. |
| 773 | try: |
| 774 | self._server.safe_switch_usbkey_power(power_state) |
| 775 | except Exception: |
| 776 | self.set('prtctl4_pwren', power_state) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 777 | if power_state == 'off': |
| 778 | time.sleep(self.USB_POWEROFF_DELAY) |
| 779 | elif detection_delay: |
| 780 | time.sleep(self.USB_DETECTION_DELAY) |
| 781 | |
| 782 | |
| 783 | def switch_usbkey(self, usb_state): |
| 784 | """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] | 785 | |
| 786 | This function switches the servo multiplexer to provide electrical |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 787 | connection between the USB port J3 and either host or DUT side. It |
| 788 | can also be used to turn the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 789 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 790 | Switching to 'dut' or 'host' is accompanied by powercycling |
| 791 | of the USB stick, because it sometimes gets wedged if the mux |
| 792 | is switched while the stick power is on. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 793 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 794 | @param usb_state: A string, one of 'dut', 'host', or 'off'. |
| 795 | 'dut' and 'host' indicate which side the |
| 796 | USB flash device is required to be connected to. |
| 797 | 'off' indicates turning the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 798 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 799 | @raise: error.TestError in case the parameter is not 'dut' |
| 800 | 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 801 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 802 | if self.get_usbkey_direction() == usb_state: |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 803 | return |
| 804 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 805 | if usb_state == 'off': |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 806 | self._switch_usbkey_power('off') |
| 807 | self._usb_state = usb_state |
| 808 | return |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 809 | elif usb_state == 'host': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 810 | mux_direction = 'servo_sees_usbkey' |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 811 | elif usb_state == 'dut': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 812 | mux_direction = 'dut_sees_usbkey' |
| 813 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 814 | raise error.TestError('Unknown USB state request: %s' % usb_state) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 815 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 816 | self._switch_usbkey_power('off') |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 817 | # TODO(kevcheng): Forgive me for this terrible hack. This is just to |
| 818 | # handle beaglebones that haven't yet updated and have the |
| 819 | # safe_switch_usbkey RPC. I'll remove this once all beaglebones have |
| 820 | # been updated and also think about a better way to handle situations |
| 821 | # like this. |
| 822 | try: |
| 823 | self._server.safe_switch_usbkey(mux_direction) |
| 824 | except Exception: |
| 825 | self.set('usb_mux_sel1', mux_direction) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 826 | time.sleep(self.USB_POWEROFF_DELAY) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 827 | self._switch_usbkey_power('on', usb_state == 'host') |
| 828 | self._usb_state = usb_state |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 829 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 830 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 831 | def get_usbkey_direction(self): |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 832 | """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] | 833 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 834 | @return: A string, one of 'dut', 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 835 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 836 | if not self._usb_state: |
| 837 | if self.get('prtctl4_pwren') == 'off': |
| 838 | self._usb_state = 'off' |
| 839 | elif self.get('usb_mux_sel1').startswith('dut'): |
| 840 | self._usb_state = 'dut' |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 841 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 842 | self._usb_state = 'host' |
| 843 | return self._usb_state |