Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 8 | |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 9 | import logging |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 10 | import time |
| 11 | import xmlrpclib |
| 12 | import subprocess |
| 13 | |
| 14 | |
| 15 | class Servo: |
| 16 | """Manages control of a Servo board. |
| 17 | |
| 18 | Servo is a board developed by hardware group to aide in the debug and |
| 19 | control of various partner devices. Servo's features include the simulation |
| 20 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 21 | class manages setting up and communicating with a servo demon (servod) |
| 22 | process. It provides both high-level functions for common servo tasks and |
| 23 | low-level functions for directly setting and reading gpios. |
| 24 | """ |
| 25 | |
| 26 | _server = None |
| 27 | _servod = None |
| 28 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 29 | # Power button press delays in seconds. |
| 30 | LONG_DELAY = 8 |
| 31 | SHORT_DELAY = 0.1 |
| 32 | NORMAL_TRANSITION_DELAY = 1.2 |
| 33 | |
| 34 | # Delays to deal with computer transitions. |
| 35 | SLEEP_DELAY = 6 |
| 36 | BOOT_DELAY = 10 |
| 37 | |
| 38 | # Servo-specific delays. |
| 39 | MAX_SERVO_STARTUP_DELAY = 10 |
| 40 | SERVO_SEND_SIGNAL_DELAY = 0.5 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 41 | |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 42 | def __init__(self, servo_port, xml_config='servo.xml', servo_vid=None, |
| 43 | servo_pid=None, servo_serial=None, cold_reset=False): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 44 | """Sets up the servo communication infrastructure. |
| 45 | |
| 46 | Args: |
| 47 | servo_port: Port the servod process should listen on. |
| 48 | xml_config: Configuration XML file for servod. |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 49 | servo_vid: USB vendor id of servo. |
| 50 | servo_pid: USB product id of servo. |
| 51 | servo_serial: USB serial id in device descriptor to host to |
| 52 | distinguish and control multiple servos. Note servo's EEPROM must |
| 53 | be programmed to use this feature. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 54 | cold_reset: If True, cold reset device and boot during init, |
| 55 | otherwise perform init with device running. |
| 56 | """ |
| 57 | # launch servod |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 58 | self._launch_servod(servo_port, xml_config, servo_vid, servo_pid, |
| 59 | servo_serial) |
| 60 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 61 | |
| 62 | # connect to servod |
| 63 | assert servo_port |
| 64 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 65 | self._do_cold_reset = cold_reset |
| 66 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 67 | self._connect_servod(servo_port) |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 68 | |
| 69 | |
| 70 | def initialize_dut(self): |
| 71 | """Initializes a dut for testing purposes.""" |
| 72 | if self._do_cold_reset: |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 73 | self._init_seq_cold_reset_devmode() |
| 74 | else: |
| 75 | self._init_seq() |
| 76 | |
| 77 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 78 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 79 | """Simulate a long power button press.""" |
| 80 | self.power_key(Servo.LONG_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 84 | """Simulate a normal power button press.""" |
| 85 | self.power_key() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 89 | """Simulate a short power button press.""" |
| 90 | self.power_key(Servo.SHORT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 91 | |
| 92 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 93 | def power_key(self, secs=NORMAL_TRANSITION_DELAY): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 94 | """Simulate a power button press. |
| 95 | |
| 96 | Args: |
| 97 | secs: Time in seconds to simulate the keypress. |
| 98 | """ |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 99 | self.set_nocheck('pwr_button', 'press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 100 | time.sleep(secs) |
| 101 | self.set('pwr_button', 'release') |
| 102 | |
| 103 | |
| 104 | def lid_open(self): |
| 105 | """Simulate opening the lid.""" |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 106 | self.set_nocheck('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def lid_close(self): |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 110 | """Simulate closing the lid. |
| 111 | |
| 112 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 113 | """ |
| 114 | self.set_nocheck('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 115 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 116 | |
| 117 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 118 | def ctrl_d(self): |
| 119 | """Simulate Ctrl-d simultaneous button presses.""" |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 120 | self.set_nocheck('kbd_en', 'on') |
| 121 | self.set_nocheck('kbd_m1', 'r2_c2') |
| 122 | self.set_nocheck('kbd_m2', 'r1_c1') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 123 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 124 | self.set_nocheck('kbd_en', 'off') |
| 125 | |
| 126 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 127 | def enter_key(self): |
| 128 | """Simulate Enter key button press.""" |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 129 | self.set_nocheck('kbd_en', 'on') |
| 130 | self.set_nocheck('kbd_m1', 'r3_c2') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 131 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 132 | self.set_nocheck('kbd_en', 'off') |
| 133 | |
| 134 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 135 | def refresh_key(self): |
| 136 | """Simulate Refresh key (F3) button press.""" |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 137 | self.set_nocheck('kbd_en', 'on') |
| 138 | self.set_nocheck('kbd_m2', 'r2_c1') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 139 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 140 | self.set_nocheck('kbd_en', 'off') |
| 141 | |
| 142 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 143 | def imaginary_key(self): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 144 | """Simulate imaginary key button press. |
| 145 | |
| 146 | Maps to a key that doesn't physically exist. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 147 | """ |
| 148 | self.set_nocheck('kbd_en', 'on') |
| 149 | self.set_nocheck('kbd_m2', 'r3_c1') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 150 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 151 | self.set_nocheck('kbd_en', 'off') |
| 152 | |
| 153 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 154 | def enable_recovery_mode(self): |
| 155 | """Enable recovery mode on device.""" |
| 156 | self.set('rec_mode', 'on') |
| 157 | |
| 158 | |
| 159 | def disable_recovery_mode(self): |
| 160 | """Disable recovery mode on device.""" |
| 161 | self.set('rec_mode', 'off') |
| 162 | |
| 163 | |
| 164 | def enable_development_mode(self): |
| 165 | """Enable development mode on device.""" |
| 166 | self.set('dev_mode', 'on') |
| 167 | |
| 168 | |
| 169 | def disable_development_mode(self): |
| 170 | """Disable development mode on device.""" |
| 171 | self.set('dev_mode', 'off') |
| 172 | |
| 173 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 174 | def boot_devmode(self): |
| 175 | """Boot a dev-mode device that is powered off.""" |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 176 | self.power_normal_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 177 | self.pass_devmode() |
| 178 | |
| 179 | |
| 180 | def pass_devmode(self): |
| 181 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 182 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 183 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 184 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 185 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 186 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 187 | def cold_reset(self): |
| 188 | """Perform a cold reset of the EC. |
| 189 | |
| 190 | Has the side effect of shutting off the device. |
| 191 | """ |
| 192 | self.set('cold_reset', 'on') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 193 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 194 | self.set('cold_reset', 'off') |
| 195 | |
| 196 | |
| 197 | def warm_reset(self): |
| 198 | """Perform a warm reset of the device. |
| 199 | |
| 200 | Has the side effect of restarting the device. |
| 201 | """ |
| 202 | self.set('warm_reset', 'on') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 203 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 204 | self.set('warm_reset', 'off') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def get(self, gpio_name): |
| 208 | """Get the value of a gpio from Servod.""" |
| 209 | assert gpio_name |
| 210 | return self._server.get(gpio_name) |
| 211 | |
| 212 | |
| 213 | def set(self, gpio_name, gpio_value): |
| 214 | """Set and check the value of a gpio using Servod.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 215 | self.set_nocheck(gpio_name, gpio_value) |
| 216 | assert gpio_value == self.get(gpio_name), \ |
| 217 | 'Servo failed to set %s to %s' % (gpio_name, gpio_value) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 218 | |
| 219 | |
| 220 | def set_nocheck(self, gpio_name, gpio_value): |
| 221 | """Set the value of a gpio using Servod.""" |
| 222 | assert gpio_name and gpio_value |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 223 | logging.info('Setting %s to %s' % (gpio_name, gpio_value)) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 224 | self._server.set(gpio_name, gpio_value) |
| 225 | |
| 226 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 227 | def _init_seq_cold_reset_devmode(self): |
| 228 | """Cold reset, init device, and boot in dev-mode.""" |
| 229 | self.cold_reset() |
| 230 | self._init_seq() |
| 231 | self.set('dev_mode', 'on') |
| 232 | self.boot_devmode() |
| 233 | |
| 234 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 235 | def __del__(self): |
| 236 | """Kill the Servod process.""" |
| 237 | assert self._servod |
| 238 | # kill servod one way or another |
| 239 | try: |
| 240 | # won't work without superuser privileges |
| 241 | self._servod.terminate() |
| 242 | except: |
| 243 | # should work without superuser privileges |
| 244 | assert subprocess.call(['sudo', 'kill', str(self._servod.pid)]) |
| 245 | |
| 246 | |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 247 | def _launch_servod(self, servo_port, xml_config, servo_vid, servo_pid, |
| 248 | servo_serial): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 249 | """Launch the servod process. |
| 250 | |
| 251 | Args: |
| 252 | servo_port: Port to start servod listening on. |
| 253 | xml_config: XML configuration file for servod. |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 254 | servo_vid: USB vendor id of servo. |
| 255 | servo_pid: USB product id of servo. |
| 256 | servo_serial: USB serial id in device descriptor to host to |
| 257 | distinguish and control multiple servos. Note servo's EEPROM must |
| 258 | be programmed to use this feature. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 259 | """ |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 260 | cmdlist = ['sudo', 'servod', '-c', str(xml_config), '--host=localhost', |
| 261 | '--port=' + str(servo_port)] |
| 262 | if servo_vid is not None: |
| 263 | cmdlist.append('--vendor=%s' % str(servo_vid)) |
| 264 | if servo_pid is not None: |
| 265 | cmdlist.append('--product=%s' % str(servo_pid)) |
| 266 | if servo_serial is not None: |
| 267 | cmdlist.append('--serialname=%s' % str(servo_serial)) |
| 268 | logging.info('starting servod w/ cmd :: %s' % ' '.join(cmdlist)) |
| 269 | self._servod = subprocess.Popen(cmdlist, 0, None, None, None, |
| 270 | subprocess.PIPE) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 271 | # wait for servod to initialize |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame^] | 272 | timeout = Servo.MAX_SERVO_STARTUP_DELAY |
| 273 | while ('Listening' not in self._servod.stderr.readline() and |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 274 | self._servod.returncode is None and timeout > 0): |
| 275 | time.sleep(1) |
| 276 | timeout -= 1 |
| 277 | assert self._servod.returncode is None and timeout |
| 278 | |
| 279 | |
| 280 | def _init_seq(self): |
| 281 | """Initiate starting state for servo.""" |
| 282 | self.set('tx_dir', 'input') |
| 283 | self.set('servo_dut_tx', 'off') |
| 284 | self.set('lid_open', 'yes') |
| 285 | self.set('rec_mode', 'off') |
| 286 | |
| 287 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 288 | def _connect_servod(self, servo_port=''): |
| 289 | """Connect to the Servod process with XMLRPC. |
| 290 | |
| 291 | Args: |
| 292 | servo_port: Port the Servod process is listening on. |
| 293 | """ |
| 294 | remote = 'http://localhost:%s' % servo_port |
| 295 | self._server = xmlrpclib.ServerProxy(remote) |