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