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