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