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 |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame^] | 13 | from autotest_lib.client.common_lib.cros import retry |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 14 | from autotest_lib.server import utils |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 15 | from autotest_lib.server.cros.servo import power_state_controller |
J. Richard Barnette | 7548757 | 2013-03-08 12:47:50 -0800 | [diff] [blame] | 16 | from autotest_lib.server.cros.servo import programmer |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 17 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 18 | |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 19 | class Servo(object): |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 20 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 21 | """Manages control of a Servo board. |
| 22 | |
| 23 | Servo is a board developed by hardware group to aide in the debug and |
| 24 | control of various partner devices. Servo's features include the simulation |
| 25 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 26 | class manages setting up and communicating with a servo demon (servod) |
| 27 | process. It provides both high-level functions for common servo tasks and |
| 28 | low-level functions for directly setting and reading gpios. |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 29 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 30 | """ |
| 31 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 32 | # Power button press delays in seconds. |
J. Richard Barnette | d2e4cbd | 2012-06-29 12:18:40 -0700 | [diff] [blame] | 33 | # |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 34 | # The EC specification says that 8.0 seconds should be enough |
| 35 | # for the long power press. However, some platforms need a bit |
| 36 | # more time. Empirical testing has found these requirements: |
| 37 | # Alex: 8.2 seconds |
| 38 | # ZGB: 8.5 seconds |
| 39 | # The actual value is set to the largest known necessary value. |
| 40 | # |
| 41 | # TODO(jrbarnette) Being generous is the right thing to do for |
| 42 | # existing platforms, but if this code is to be used for |
| 43 | # qualification of new hardware, we should be less generous. |
| 44 | LONG_DELAY = 8.5 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 45 | SHORT_DELAY = 0.1 |
| 46 | NORMAL_TRANSITION_DELAY = 1.2 |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 47 | |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 48 | # Maximum number of times to re-read power button on release. |
| 49 | RELEASE_RETRY_MAX = 5 |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 50 | GET_RETRY_MAX = 10 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 51 | |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 52 | # Delays to deal with DUT state transitions. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 53 | SLEEP_DELAY = 6 |
| 54 | BOOT_DELAY = 10 |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 55 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 56 | # Default minimum time interval between 'press' and 'release' |
| 57 | # keyboard events. |
Vic Yang | 0aca1c2 | 2012-11-19 18:33:56 -0800 | [diff] [blame] | 58 | SERVO_KEY_PRESS_DELAY = 0.1 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 59 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 60 | # Time between an usb disk plugged-in and detected in the system. |
| 61 | USB_DETECTION_DELAY = 10 |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 62 | # Time to keep USB power off before and after USB mux direction is changed |
| 63 | USB_POWEROFF_DELAY = 2 |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 64 | |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame^] | 65 | # Time to wait before timing out on a dut-control call. |
| 66 | CALL_TIMEOUT_SECS = 60 |
| 67 | |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 68 | KEY_MATRIX_ALT_0 = { |
Gediminas Ramanauskas | 515e701 | 2012-09-18 17:01:10 -0700 | [diff] [blame] | 69 | 'ctrl_refresh': ['0', '0', '0', '1'], |
| 70 | 'ctrl_d': ['0', '1', '0', '0'], |
| 71 | 'd': ['0', '1', '1', '1'], |
| 72 | 'ctrl_enter': ['1', '0', '0', '0'], |
| 73 | 'enter': ['1', '0', '1', '1'], |
| 74 | 'ctrl': ['1', '1', '0', '0'], |
| 75 | 'refresh': ['1', '1', '0', '1'], |
| 76 | 'unused': ['1', '1', '1', '0'], |
| 77 | 'none': ['1', '1', '1', '1']} |
Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 78 | |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 79 | KEY_MATRIX_ALT_1 = { |
Gediminas Ramanauskas | 515e701 | 2012-09-18 17:01:10 -0700 | [diff] [blame] | 80 | 'ctrl_d': ['0', '0', '1', '0'], |
| 81 | 'd': ['0', '0', '1', '1'], |
| 82 | 'ctrl_enter': ['0', '1', '1', '0'], |
| 83 | 'enter': ['0', '1', '1', '1'], |
| 84 | 'ctrl_refresh': ['1', '0', '0', '1'], |
| 85 | 'unused': ['1', '1', '0', '0'], |
| 86 | 'refresh': ['1', '1', '0', '1'], |
| 87 | 'ctrl': ['1', '1', '1', '0'], |
| 88 | 'none': ['1', '1', '1', '1']} |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 89 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 90 | KEY_MATRIX_ALT_2 = { |
| 91 | 'ctrl_d': ['0', '0', '0', '1'], |
| 92 | 'd': ['0', '0', '1', '1'], |
| 93 | 'unused': ['0', '1', '1', '1'], |
| 94 | 'rec_mode': ['1', '0', '0', '0'], |
| 95 | 'ctrl_enter': ['1', '0', '0', '1'], |
| 96 | 'enter': ['1', '0', '1', '1'], |
| 97 | 'ctrl': ['1', '1', '0', '1'], |
| 98 | 'refresh': ['1', '1', '1', '0'], |
| 99 | 'ctrl_refresh': ['1', '1', '1', '1'], |
| 100 | 'none': ['1', '1', '1', '1']} |
| 101 | |
| 102 | KEY_MATRIX = [KEY_MATRIX_ALT_0, KEY_MATRIX_ALT_1, KEY_MATRIX_ALT_2] |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 103 | |
J. Richard Barnette | d5f807a | 2013-02-11 16:51:00 -0800 | [diff] [blame] | 104 | def __init__(self, servo_host='localhost', servo_port=9999): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 105 | """Sets up the servo communication infrastructure. |
| 106 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 107 | @param servo_host Name of the host where the servod process |
| 108 | is running. |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 109 | @param servo_port Port the servod process is listening on. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 110 | """ |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 111 | self._key_matrix = 0 |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 112 | self._server = None |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 113 | self._connect_servod(servo_host, servo_port) |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 114 | self._is_localhost = (servo_host == 'localhost') |
J. Richard Barnette | 69929a5 | 2013-03-15 13:22:11 -0700 | [diff] [blame] | 115 | # TODO(jrbarnette): As of this writing, not all beaglebone |
| 116 | # servo hosts support the get_board() function. For now, we |
| 117 | # treat the problem hosts as an unsupported board. The try |
| 118 | # wrapper should be removed once all the hosts are updated. |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 119 | board = None |
J. Richard Barnette | 69929a5 | 2013-03-15 13:22:11 -0700 | [diff] [blame] | 120 | try: |
| 121 | board = self._server.get_board() |
J. Richard Barnette | 69929a5 | 2013-03-15 13:22:11 -0700 | [diff] [blame] | 122 | except xmlrpclib.Fault as e: |
| 123 | logging.error('Failed to create power state controller; ' |
| 124 | 'check hdctools version on %s.', servo_host) |
| 125 | logging.exception(e) |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 126 | self._power_state = ( |
| 127 | power_state_controller.create_controller(self, board)) |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 128 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 129 | # a string, showing what interface (host or dut) the USB device is |
| 130 | # connected to. |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 131 | self._usb_state = None |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 132 | self.set('dut_hub_pwren', 'on') |
| 133 | self.set('usb_mux_oe1', 'on') |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 134 | self.switch_usbkey('off') |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 135 | |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 136 | # Commands on the servo host must be run by the superuser. Our account |
| 137 | # on Beaglebone is root, but locally we might be running as a |
| 138 | # different user. If so - `sudo ' will have to be added to the |
| 139 | # commands. |
| 140 | if self._is_localhost: |
| 141 | self._sudo_required = utils.system_output('id -u') != '0' |
| 142 | self._ssh_prefix = '' |
| 143 | else: |
J. Richard Barnette | 91e6c6e | 2013-03-12 18:18:22 -0700 | [diff] [blame] | 144 | common_options = ('-o BatchMode=yes ' |
| 145 | '-o StrictHostKeyChecking=no ' |
| 146 | '-o UserKnownHostsFile=/dev/null') |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 147 | self._sudo_required = False |
J. Richard Barnette | 91e6c6e | 2013-03-12 18:18:22 -0700 | [diff] [blame] | 148 | self._ssh_prefix = 'ssh -a -x %s root@%s ' % ( |
| 149 | common_options, servo_host) |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 150 | self._scp_cmd_template = 'scp -r %s ' % common_options |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 151 | self._scp_cmd_template += '%s ' + 'root@' + servo_host + ':%s' |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 152 | |
Tom Wai-Hong Tam | 22ee0e5 | 2013-04-03 13:36:39 +0800 | [diff] [blame] | 153 | def get_power_state_controller(self): |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 154 | """Return the power state controller for this Servo. |
| 155 | |
| 156 | The power state controller provides board-independent |
| 157 | interfaces for reset, power-on, power-off operations. |
| 158 | |
| 159 | """ |
| 160 | return self._power_state |
| 161 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 162 | def initialize_dut(self, cold_reset=False): |
| 163 | """Initializes a dut for testing purposes. |
| 164 | |
| 165 | This sets various servo signals back to default values |
| 166 | appropriate for the target board. By default, if the DUT |
| 167 | is already on, it stays on. If the DUT is powered off |
| 168 | before initialization, its state afterward is unspecified. |
| 169 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 170 | Rationale: Basic initialization of servo sets the lid open, |
| 171 | when there is a lid. This operation won't affect powered on |
| 172 | units; however, setting the lid open may power on a unit |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 173 | that's off, depending on the board type and previous state |
| 174 | of the device. |
| 175 | |
| 176 | If `cold_reset` is a true value, cold reset will be applied |
| 177 | to the DUT. Cold reset will force the DUT to power off; |
| 178 | however, the final state of the DUT depends on the board type. |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 179 | |
| 180 | @param cold_reset If True, cold reset the device after |
| 181 | initialization. |
| 182 | """ |
| 183 | self._server.hwinit() |
| 184 | if cold_reset: |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 185 | self._power_state.cold_reset() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 186 | |
| 187 | |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 188 | def is_localhost(self): |
| 189 | """Is the servod hosted locally? |
| 190 | |
| 191 | Returns: |
| 192 | True if local hosted; otherwise, False. |
| 193 | """ |
| 194 | return self._is_localhost |
| 195 | |
| 196 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 197 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 198 | """Simulate a long power button press.""" |
J. Richard Barnette | f12bff2 | 2012-07-18 14:41:06 -0700 | [diff] [blame] | 199 | # After a long power press, the EC may ignore the next power |
| 200 | # button press (at least on Alex). To guarantee that this |
| 201 | # won't happen, we need to allow the EC one second to |
| 202 | # collect itself. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 203 | self.power_key(Servo.LONG_DELAY) |
J. Richard Barnette | f12bff2 | 2012-07-18 14:41:06 -0700 | [diff] [blame] | 204 | time.sleep(1.0) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 208 | """Simulate a normal power button press.""" |
| 209 | self.power_key() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 210 | |
| 211 | |
| 212 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 213 | """Simulate a short power button press.""" |
| 214 | self.power_key(Servo.SHORT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 215 | |
| 216 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 217 | def power_key(self, secs=NORMAL_TRANSITION_DELAY): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 218 | """Simulate a power button press. |
| 219 | |
| 220 | Args: |
| 221 | secs: Time in seconds to simulate the keypress. |
| 222 | """ |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 223 | self.set_get_all(['pwr_button:press', |
| 224 | 'sleep:%.4f' % secs, |
| 225 | 'pwr_button:release']) |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 226 | # TODO(tbroch) Different systems have different release times on the |
| 227 | # power button that this loop addresses. Longer term we may want to |
| 228 | # make this delay platform specific. |
| 229 | retry = 1 |
| 230 | while True: |
| 231 | value = self.get('pwr_button') |
| 232 | if value == 'release' or retry > Servo.RELEASE_RETRY_MAX: |
| 233 | break |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 234 | logging.info('Waiting for pwr_button to release, retry %d.', retry) |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 235 | retry += 1 |
| 236 | time.sleep(Servo.SHORT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def lid_open(self): |
| 240 | """Simulate opening the lid.""" |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 241 | self.set_nocheck('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 242 | |
| 243 | |
| 244 | def lid_close(self): |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 245 | """Simulate closing the lid. |
| 246 | |
| 247 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 248 | """ |
| 249 | self.set_nocheck('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 250 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 251 | |
| 252 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 253 | def _press_keys(self, key): |
| 254 | """Simulate button presses. |
| 255 | |
| 256 | Note, key presses will remain on indefinitely. See |
| 257 | _press_and_release_keys for release procedure. |
| 258 | """ |
Vic Yang | cad9acb | 2013-05-21 14:02:05 +0800 | [diff] [blame] | 259 | (m1_a1_n, m1_a0_n, m2_a1_n, m2_a0_n) = ( |
| 260 | self.KEY_MATRIX[self._key_matrix]['none']) |
Gediminas Ramanauskas | 9da80f2 | 2012-11-14 12:59:43 -0800 | [diff] [blame] | 261 | (m1_a1, m1_a0, m2_a1, m2_a0) = self.KEY_MATRIX[self._key_matrix][key] |
Vic Yang | cad9acb | 2013-05-21 14:02:05 +0800 | [diff] [blame] | 262 | self.set_get_all(['kbd_m2_a0:%s' % m2_a0_n, |
| 263 | 'kbd_m2_a1:%s' % m2_a1_n, |
| 264 | 'kbd_m1_a0:%s' % m1_a0_n, |
| 265 | 'kbd_m1_a1:%s' % m1_a1_n, |
| 266 | 'kbd_en:on', |
| 267 | 'kbd_m2_a0:%s' % m2_a0, |
| 268 | 'kbd_m2_a1:%s' % m2_a1, |
| 269 | 'kbd_m1_a0:%s' % m1_a0, |
| 270 | 'kbd_m1_a1:%s' % m1_a1]) |
Gediminas Ramanauskas | 9da80f2 | 2012-11-14 12:59:43 -0800 | [diff] [blame] | 271 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 272 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 273 | def _press_and_release_keys(self, key, press_secs=None): |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 274 | """Simulate button presses and release.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 275 | if press_secs is None: |
| 276 | press_secs = self.SERVO_KEY_PRESS_DELAY |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 277 | self._press_keys(key) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 278 | time.sleep(press_secs) |
| 279 | self.set_nocheck('kbd_en', 'off') |
| 280 | |
| 281 | |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 282 | def set_key_matrix(self, matrix=0): |
| 283 | """Set keyboard mapping""" |
| 284 | self._key_matrix = matrix |
| 285 | |
| 286 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 287 | def ctrl_d(self, press_secs=None): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 288 | """Simulate Ctrl-d simultaneous button presses.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 289 | self._press_and_release_keys('ctrl_d', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 290 | |
| 291 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 292 | def ctrl_enter(self, press_secs=None): |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 293 | """Simulate Ctrl-enter simultaneous button presses.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 294 | self._press_and_release_keys('ctrl_enter', press_secs) |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 295 | |
| 296 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 297 | def d_key(self, press_secs=None): |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 298 | """Simulate Enter key button press.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 299 | self._press_and_release_keys('d', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 300 | |
| 301 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 302 | def ctrl_key(self, press_secs=None): |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 303 | """Simulate Enter key button press.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 304 | self._press_and_release_keys('ctrl', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 305 | |
| 306 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 307 | def enter_key(self, press_secs=None): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 308 | """Simulate Enter key button press.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 309 | self._press_and_release_keys('enter', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 310 | |
| 311 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 312 | def refresh_key(self, press_secs=None): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 313 | """Simulate Refresh key (F3) button press.""" |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 314 | self._press_and_release_keys('refresh', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 315 | |
| 316 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 317 | def ctrl_refresh_key(self, press_secs=None): |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 318 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 319 | |
| 320 | This key combination is an alternative of Space key. |
| 321 | """ |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 322 | self._press_and_release_keys('ctrl_refresh', press_secs) |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 323 | |
| 324 | |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 325 | def imaginary_key(self, press_secs=None): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 326 | """Simulate imaginary key button press. |
| 327 | |
| 328 | Maps to a key that doesn't physically exist. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 329 | """ |
Vic Yang | 701a59e | 2013-05-02 05:10:22 +0800 | [diff] [blame] | 330 | self._press_and_release_keys('unused', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 331 | |
| 332 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 333 | def enable_recovery_mode(self): |
| 334 | """Enable recovery mode on device.""" |
| 335 | self.set('rec_mode', 'on') |
| 336 | |
| 337 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 338 | def custom_recovery_mode(self): |
| 339 | """Custom key combination to enter recovery mode.""" |
| 340 | self._press_keys('rec_mode') |
| 341 | self.power_normal_press() |
| 342 | time.sleep(self.SERVO_KEY_PRESS_DELAY) |
| 343 | self.set_nocheck('kbd_en', 'off') |
| 344 | |
| 345 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 346 | def disable_recovery_mode(self): |
| 347 | """Disable recovery mode on device.""" |
| 348 | self.set('rec_mode', 'off') |
| 349 | |
| 350 | |
| 351 | def enable_development_mode(self): |
| 352 | """Enable development mode on device.""" |
| 353 | self.set('dev_mode', 'on') |
| 354 | |
| 355 | |
| 356 | def disable_development_mode(self): |
| 357 | """Disable development mode on device.""" |
| 358 | self.set('dev_mode', 'off') |
| 359 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 360 | def boot_devmode(self): |
| 361 | """Boot a dev-mode device that is powered off.""" |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 362 | self.power_short_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 363 | self.pass_devmode() |
| 364 | |
| 365 | |
| 366 | def pass_devmode(self): |
| 367 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 368 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 369 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 370 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 371 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 372 | |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 373 | def _get_xmlrpclib_exception(self, xmlexc): |
| 374 | """Get meaningful exception string from xmlrpc. |
| 375 | |
| 376 | Args: |
| 377 | xmlexc: xmlrpclib.Fault object |
| 378 | |
| 379 | xmlrpclib.Fault.faultString has the following format: |
| 380 | |
| 381 | <type 'exception type'>:'actual error message' |
| 382 | |
| 383 | Parse and return the real exception from the servod side instead of the |
| 384 | less meaningful one like, |
| 385 | <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no |
| 386 | attribute 'hw_driver'"> |
| 387 | |
| 388 | Returns: |
| 389 | string of underlying exception raised in servod. |
| 390 | """ |
| 391 | return re.sub('^.*>:', '', xmlexc.faultString) |
| 392 | |
| 393 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 394 | def get(self, gpio_name): |
| 395 | """Get the value of a gpio from Servod.""" |
| 396 | assert gpio_name |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 397 | try: |
Simran Basi | 1cbc5f2 | 2013-07-17 14:23:58 -0700 | [diff] [blame] | 398 | return self._server.get(gpio_name) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 399 | except xmlrpclib.Fault as e: |
| 400 | err_msg = "Getting '%s' :: %s" % \ |
| 401 | (gpio_name, self._get_xmlrpclib_exception(e)) |
| 402 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 403 | |
| 404 | |
| 405 | def set(self, gpio_name, gpio_value): |
| 406 | """Set and check the value of a gpio using Servod.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 407 | self.set_nocheck(gpio_name, gpio_value) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 408 | retry_count = Servo.GET_RETRY_MAX |
| 409 | while gpio_value != self.get(gpio_name) and retry_count: |
| 410 | logging.warn("%s != %s, retry %d", gpio_name, gpio_value, |
| 411 | retry_count) |
| 412 | retry_count -= 1 |
| 413 | time.sleep(Servo.SHORT_DELAY) |
| 414 | if not retry_count: |
| 415 | assert gpio_value == self.get(gpio_name), \ |
| 416 | 'Servo failed to set %s to %s' % (gpio_name, gpio_value) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 417 | |
| 418 | |
| 419 | def set_nocheck(self, gpio_name, gpio_value): |
| 420 | """Set the value of a gpio using Servod.""" |
| 421 | assert gpio_name and gpio_value |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 422 | logging.info('Setting %s to %s', gpio_name, gpio_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 423 | try: |
Simran Basi | 1cbc5f2 | 2013-07-17 14:23:58 -0700 | [diff] [blame] | 424 | self._server.set(gpio_name, gpio_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 425 | except xmlrpclib.Fault as e: |
| 426 | err_msg = "Setting '%s' to '%s' :: %s" % \ |
| 427 | (gpio_name, gpio_value, self._get_xmlrpclib_exception(e)) |
| 428 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 429 | |
| 430 | |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 431 | def set_get_all(self, controls): |
| 432 | """Set &| get one or more control values. |
| 433 | |
| 434 | @param controls: list of strings, controls to set &| get. |
| 435 | |
| 436 | @raise: error.TestError in case error occurs setting/getting values. |
| 437 | """ |
| 438 | rv = [] |
| 439 | try: |
Vic Yang | cad9acb | 2013-05-21 14:02:05 +0800 | [diff] [blame] | 440 | logging.info('Set/get all: %s', str(controls)) |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 441 | rv = self._server.set_get_all(controls) |
| 442 | except xmlrpclib.Fault as e: |
| 443 | # TODO(waihong): Remove the following backward compatibility when |
| 444 | # the new versions of hdctools are deployed. |
| 445 | if 'not supported' in str(e): |
| 446 | logging.warning('The servod is too old that set_get_all ' |
| 447 | 'not supported. Use set and get instead.') |
| 448 | for control in controls: |
| 449 | if ':' in control: |
| 450 | (name, value) = control.split(':') |
| 451 | if name == 'sleep': |
| 452 | time.sleep(float(value)) |
| 453 | else: |
| 454 | self.set_nocheck(name, value) |
| 455 | rv.append(True) |
| 456 | else: |
| 457 | rv.append(self.get(name)) |
| 458 | else: |
| 459 | err_msg = "Problem with '%s' :: %s" % \ |
| 460 | (controls, self._get_xmlrpclib_exception(e)) |
| 461 | raise error.TestFail(err_msg) |
| 462 | return rv |
| 463 | |
| 464 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 465 | # TODO(waihong) It may fail if multiple servo's are connected to the same |
| 466 | # host. Should look for a better way, like the USB serial name, to identify |
| 467 | # the USB device. |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 468 | # TODO(sbasi) Remove this code from autoserv once firmware tests have been |
| 469 | # updated. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 470 | def probe_host_usb_dev(self): |
| 471 | """Probe the USB disk device plugged-in the servo from the host side. |
| 472 | |
| 473 | It tries to switch the USB mux to make the host unable to see the |
| 474 | USB disk and compares the result difference. |
| 475 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 476 | Returns: |
| 477 | A string of USB disk path, like '/dev/sdb', or None if not existed. |
| 478 | """ |
| 479 | cmd = 'ls /dev/sd[a-z]' |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 480 | original_value = self.get_usbkey_direction() |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 481 | |
| 482 | # Make the host unable to see the USB disk. |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 483 | self.switch_usbkey('off') |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 484 | no_usb_set = set(self.system_output(cmd, ignore_status=True).split()) |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 485 | |
| 486 | # Make the host able to see the USB disk. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 487 | self.switch_usbkey('host') |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 488 | has_usb_set = set(self.system_output(cmd, ignore_status=True).split()) |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 489 | |
| 490 | # Back to its original value. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 491 | if original_value != self.get_usbkey_direction(): |
| 492 | self.switch_usbkey(original_value) |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 493 | |
| 494 | diff_set = has_usb_set - no_usb_set |
| 495 | if len(diff_set) == 1: |
| 496 | return diff_set.pop() |
| 497 | else: |
| 498 | return None |
| 499 | |
| 500 | |
J. Richard Barnette | 69929a5 | 2013-03-15 13:22:11 -0700 | [diff] [blame] | 501 | def recovery_supported(self): |
| 502 | """Return whether servo-based recovery should work. |
| 503 | |
| 504 | Use of `image_to_servo_usb()` and `install_recovery_image()` |
| 505 | relies on DUT-board specific behaviors, and is not supported |
| 506 | for all types of board. Return whether these two operations |
| 507 | are expected to succeed for the current DUT. |
| 508 | |
| 509 | @return `True` iff the recovery related methods are supported |
| 510 | for this servo and DUT. |
| 511 | |
| 512 | """ |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 513 | return self._power_state.recovery_supported() |
J. Richard Barnette | 69929a5 | 2013-03-15 13:22:11 -0700 | [diff] [blame] | 514 | |
| 515 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 516 | def image_to_servo_usb(self, image_path=None, |
| 517 | make_image_noninteractive=False): |
| 518 | """Install an image to the USB key plugged into the servo. |
| 519 | |
| 520 | This method may copy any image to the servo USB key including a |
| 521 | recovery image or a test image. These images are frequently used |
| 522 | for test purposes such as restoring a corrupted image or conducting |
| 523 | an upgrade of ec/fw/kernel as part of a test of a specific image part. |
| 524 | |
| 525 | Args: |
| 526 | image_path: Path on the host to the recovery image. |
| 527 | make_image_noninteractive: Make the recovery image noninteractive, |
| 528 | therefore the DUT will reboot |
| 529 | automatically after installation. |
| 530 | """ |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 531 | # We're about to start plugging/unplugging the USB key. We |
| 532 | # don't know the state of the DUT, or what it might choose |
| 533 | # to do to the device after hotplug. To avoid surprises, |
| 534 | # force the DUT to be off. |
| 535 | self._server.hwinit() |
| 536 | self._power_state.power_off() |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 537 | |
| 538 | # Set up Servo's usb mux. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 539 | self.switch_usbkey('host') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 540 | if image_path: |
Tom Wai-Hong Tam | 42f136d | 2012-10-26 11:11:23 +0800 | [diff] [blame] | 541 | logging.info('Searching for usb device and copying image to it. ' |
| 542 | 'Please wait a few minutes...') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 543 | if not self._server.download_image_to_usb(image_path): |
| 544 | logging.error('Failed to transfer requested image to USB. ' |
| 545 | 'Please take a look at Servo Logs.') |
| 546 | raise error.AutotestError('Download image to usb failed.') |
| 547 | if make_image_noninteractive: |
| 548 | logging.info('Making image noninteractive') |
| 549 | if not self._server.make_image_noninteractive(): |
| 550 | logging.error('Failed to make image noninteractive. ' |
| 551 | 'Please take a look at Servo Logs.') |
| 552 | |
| 553 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 554 | def install_recovery_image(self, image_path=None, |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 555 | make_image_noninteractive=False): |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 556 | """Install the recovery image specied by the path onto the DUT. |
| 557 | |
| 558 | This method uses google recovery mode to install a recovery image |
| 559 | 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] | 560 | board specified by the usb_dev. If no image path is specified |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 561 | we use the recovery image already on the usb image. |
| 562 | |
| 563 | Args: |
| 564 | image_path: Path on the host to the recovery image. |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 565 | make_image_noninteractive: Make the recovery image noninteractive, |
| 566 | therefore the DUT will reboot |
| 567 | automatically after installation. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 568 | """ |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 569 | self.image_to_servo_usb(image_path, make_image_noninteractive) |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 570 | self._power_state.power_on(rec_mode=power_state_controller.REC_ON) |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 571 | self.switch_usbkey('dut') |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 572 | |
| 573 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 574 | def _connect_servod(self, servo_host, servo_port): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 575 | """Connect to the Servod process with XMLRPC. |
| 576 | |
| 577 | Args: |
| 578 | servo_port: Port the Servod process is listening on. |
| 579 | """ |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 580 | remote = 'http://%s:%s' % (servo_host, servo_port) |
Todd Broch | 96d83aa | 2011-08-29 14:37:38 -0700 | [diff] [blame] | 581 | self._server = xmlrpclib.ServerProxy(remote) |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 582 | try: |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame^] | 583 | # We have noticed incidents where we are able to connect but the |
| 584 | # actual servo calls go on for 20-30 mins. Thus _connect_servod now |
| 585 | # grabs the pwr_button state and if it times out raises an error. |
| 586 | timeout, result = retry.timeout( |
| 587 | self._server.get, args=('pwr_button', ), |
| 588 | timeout_sec=Servo.CALL_TIMEOUT_SECS) |
| 589 | if timeout: |
| 590 | raise error.AutotestError('Timed out getting value for ' |
| 591 | 'pwr_button.') |
| 592 | except Exception as e: |
| 593 | # Make sure to catch any errors that can occur connecting to servod. |
| 594 | error_msg = 'Connection to servod failed. Failure: %s' % e |
| 595 | logging.error(error_msg) |
| 596 | raise error.AutotestError(error_msg) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 597 | |
| 598 | |
| 599 | def _scp_image(self, image_path): |
| 600 | """Copy image to the servo host. |
| 601 | |
| 602 | When programming a firmware image on the DUT, the image must be |
| 603 | located on the host to which the servo device is connected. Sometimes |
| 604 | servo is controlled by a remote host, in this case the image needs to |
| 605 | be transferred to the remote host. |
| 606 | |
| 607 | @param image_path: a string, name of the firmware image file to be |
| 608 | transferred. |
| 609 | @return: a string, full path name of the copied file on the remote. |
| 610 | """ |
| 611 | |
| 612 | dest_path = os.path.join('/tmp', os.path.basename(image_path)) |
| 613 | scp_cmd = self._scp_cmd_template % (image_path, dest_path) |
| 614 | utils.system(scp_cmd) |
| 615 | return dest_path |
| 616 | |
| 617 | |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 618 | def system(self, command, timeout=None): |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 619 | """Execute the passed in command on the servod host.""" |
| 620 | if self._sudo_required: |
| 621 | command = 'sudo -n %s' % command |
| 622 | if self._ssh_prefix: |
| 623 | command = "%s '%s'" % (self._ssh_prefix, command) |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 624 | logging.info('Will execute on servo host: %s', command) |
| 625 | utils.system(command, timeout=timeout) |
| 626 | |
| 627 | |
| 628 | def system_output(self, command, timeout=None, |
| 629 | ignore_status=False, args=()): |
| 630 | """Execute the passed in command on the servod host, return stdout. |
| 631 | |
| 632 | @param command, a string, the command to execute |
| 633 | @param timeout, an int, max number of seconds to wait til command |
| 634 | execution completes |
| 635 | @ignore_status, a Boolean, if true - ignore command's nonzero exit |
| 636 | status, otherwise an exception will be thrown |
| 637 | @param args, a tuple of strings, each becoming a separate command line |
| 638 | parameter for the command |
| 639 | @return: command's stdout as a string. |
| 640 | """ |
| 641 | if self._sudo_required: |
| 642 | command = 'sudo -n %s' % command |
| 643 | if self._ssh_prefix: |
| 644 | command = "%s '%s'" % (self._ssh_prefix, command) |
| 645 | logging.info('Will execute and collect output on servo host: %s %s', |
| 646 | command, ' '.join("'%s'" % x for x in args)) |
| 647 | return utils.system_output(command, timeout=timeout, |
| 648 | ignore_status=ignore_status, args=args) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 649 | |
| 650 | |
| 651 | def program_ec(self, board, image): |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 652 | """Program EC on a given board using given image. |
| 653 | |
| 654 | @param board: a string, type of the DUT board |
| 655 | @param image: a string, file name of the EC image to program on the |
| 656 | DUT |
| 657 | """ |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 658 | if not self.is_localhost(): |
| 659 | image = self._scp_image(image) |
| 660 | programmer.program_ec(board, self, image) |
| 661 | |
| 662 | |
| 663 | def program_bootprom(self, board, image): |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 664 | """Program bootprom on a given board using given image. |
| 665 | |
| 666 | In case servo is controlled by a remote host, the image needs to be |
| 667 | transferred to the host. |
| 668 | |
| 669 | If the device tree subdirectory is present along with the image, the |
| 670 | subdirectory is also copied to the remote host. |
| 671 | |
| 672 | @param board: a string, type of the DUT board |
| 673 | @param image: a string, file name of the firmware image to program on |
| 674 | the DUT. The device tree subdirectory, if present, is on |
| 675 | the same level with the image file. |
| 676 | """ |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 677 | if not self.is_localhost(): |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 678 | dts_path = os.path.join(os.path.dirname(image), 'dts') |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 679 | image = self._scp_image(image) |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 680 | if os.path.isdir(dts_path): |
| 681 | self._scp_image(dts_path) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 682 | programmer.program_bootprom(board, self, image) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 683 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 684 | |
| 685 | def _switch_usbkey_power(self, power_state, detection_delay=False): |
| 686 | """Switch usbkey power. |
| 687 | |
| 688 | This function switches usbkey power by setting the value of |
| 689 | 'prtctl4_pwren'. If the power is already in the |
| 690 | requested state, this function simply returns. |
| 691 | |
| 692 | @param power_state: A string, 'on' or 'off'. |
| 693 | @param detection_delay: A boolean value, if True, sleep |
| 694 | for |USB_DETECTION_DELAY| after switching |
| 695 | the power on. |
| 696 | """ |
| 697 | self.set('prtctl4_pwren', power_state) |
| 698 | if power_state == 'off': |
| 699 | time.sleep(self.USB_POWEROFF_DELAY) |
| 700 | elif detection_delay: |
| 701 | time.sleep(self.USB_DETECTION_DELAY) |
| 702 | |
| 703 | |
| 704 | def switch_usbkey(self, usb_state): |
| 705 | """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] | 706 | |
| 707 | This function switches the servo multiplexer to provide electrical |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 708 | connection between the USB port J3 and either host or DUT side. It |
| 709 | can also be used to turn the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 710 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 711 | Switching to 'dut' or 'host' is accompanied by powercycling |
| 712 | of the USB stick, because it sometimes gets wedged if the mux |
| 713 | is switched while the stick power is on. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 714 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 715 | @param usb_state: A string, one of 'dut', 'host', or 'off'. |
| 716 | 'dut' and 'host' indicate which side the |
| 717 | USB flash device is required to be connected to. |
| 718 | 'off' indicates turning the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 719 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 720 | @raise: error.TestError in case the parameter is not 'dut' |
| 721 | 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 722 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 723 | if self.get_usbkey_direction() == usb_state: |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 724 | return |
| 725 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 726 | if usb_state == 'off': |
| 727 | self._switch_usbkey_power('off') |
| 728 | self._usb_state = usb_state |
| 729 | return |
| 730 | elif usb_state == 'host': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 731 | mux_direction = 'servo_sees_usbkey' |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 732 | elif usb_state == 'dut': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 733 | mux_direction = 'dut_sees_usbkey' |
| 734 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 735 | raise error.TestError('Unknown USB state request: %s' % usb_state) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 736 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 737 | self._switch_usbkey_power('off') |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 738 | self.set('usb_mux_sel1', mux_direction) |
| 739 | time.sleep(self.USB_POWEROFF_DELAY) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 740 | self._switch_usbkey_power('on', usb_state == 'host') |
| 741 | self._usb_state = usb_state |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 742 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 743 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 744 | def get_usbkey_direction(self): |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 745 | """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] | 746 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 747 | @return: A string, one of 'dut', 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 748 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 749 | if not self._usb_state: |
| 750 | if self.get('prtctl4_pwren') == 'off': |
| 751 | self._usb_state = 'off' |
| 752 | elif self.get('usb_mux_sel1').startswith('dut'): |
| 753 | self._usb_state = 'dut' |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 754 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 755 | self._usb_state = 'host' |
| 756 | return self._usb_state |