Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 1 | # Copyright (c) 2012 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 | |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 5 | import ast, re |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 6 | |
| 7 | from autotest_lib.client.common_lib import error |
| 8 | |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 9 | # en-US key matrix (from "kb membrane pin matrix.pdf") |
| 10 | KEYMATRIX = {'`': (3, 1), '1': (6, 1), '2': (6, 4), '3': (6, 2), '4': (6, 3), |
| 11 | '5': (3, 3), '6': (3, 6), '7': (6, 6), '8': (6, 5), '9': (6, 9), |
| 12 | '0': (6, 8), '-': (3, 8), '=': (0, 8), 'q': (7, 1), 'w': (7, 4), |
| 13 | 'e': (7, 2), 'r': (7, 3), 't': (2, 3), 'y': (2, 6), 'u': (7, 6), |
| 14 | 'i': (7, 5), 'o': (7, 9), 'p': (7, 8), '[': (2, 8), ']': (2, 5), |
| 15 | '\\': (3, 11), 'a': (4, 1), 's': (4, 4), 'd': (4, 2), 'f': (4, 3), |
| 16 | 'g': (1, 3), 'h': (1, 6), 'j': (4, 6), 'k': (4, 5), 'l': (4, 9), |
| 17 | ';': (4, 8), '\'': (1, 8), 'z': (5, 1), 'x': (5, 4), 'c': (5, 2), |
| 18 | 'v': (5, 3), 'b': (0, 3), 'n': (0, 6), 'm': (5, 6), ',': (5, 5), |
| 19 | '.': (5, 9), '/': (5, 8), ' ': (5, 11), '<right>': (6, 12), |
| 20 | '<alt_r>': (0, 10), '<down>': (6, 11), '<tab>': (2, 1), |
| 21 | '<f10>': (0, 4), '<shift_r>': (7, 7), '<ctrl_r>': (4, 0), |
| 22 | '<esc>': (1, 1), '<backspace>': (1, 11), '<f2>': (3, 2), |
| 23 | '<alt_l>': (6, 10), '<ctrl_l>': (2, 0), '<f1>': (0, 2), |
| 24 | '<search>': (0, 1), '<f3>': (2, 2), '<f4>': (1, 2), '<f5>': (3, 4), |
| 25 | '<f6>': (2, 4), '<f7>': (1, 4), '<f8>': (2, 9), '<f9>': (1, 9), |
| 26 | '<up>': (7, 11), '<shift_l>': (5, 7), '<enter>': (4, 11), |
| 27 | '<left>': (7, 12)} |
| 28 | |
| 29 | |
Tom Wai-Hong Tam | 665281c | 2012-10-30 11:55:10 +0800 | [diff] [blame^] | 30 | # Hostevent codes, copied from: |
| 31 | # ec/include/ec_commands.h |
| 32 | HOSTEVENT_LID_CLOSED = 0x00000001 |
| 33 | HOSTEVENT_LID_OPEN = 0x00000002 |
| 34 | HOSTEVENT_POWER_BUTTON = 0x00000004 |
| 35 | HOSTEVENT_AC_CONNECTED = 0x00000008 |
| 36 | HOSTEVENT_AC_DISCONNECTED = 0x00000010 |
| 37 | HOSTEVENT_BATTERY_LOW = 0x00000020 |
| 38 | HOSTEVENT_BATTERY_CRITICAL = 0x00000040 |
| 39 | HOSTEVENT_BATTERY = 0x00000080 |
| 40 | HOSTEVENT_THERMAL_THRESHOLD = 0x00000100 |
| 41 | HOSTEVENT_THERMAL_OVERLOAD = 0x00000200 |
| 42 | HOSTEVENT_THERMAL = 0x00000400 |
| 43 | HOSTEVENT_USB_CHARGER = 0x00000800 |
| 44 | HOSTEVENT_KEY_PRESSED = 0x00001000 |
| 45 | HOSTEVENT_INTERFACE_READY = 0x00002000 |
| 46 | # Keyboard recovery combo has been pressed |
| 47 | HOSTEVENT_KEYBOARD_RECOVERY = 0x00004000 |
| 48 | # Shutdown due to thermal overload |
| 49 | HOSTEVENT_THERMAL_SHUTDOWN = 0x00008000 |
| 50 | # Shutdown due to battery level too low |
| 51 | HOSTEVENT_BATTERY_SHUTDOWN = 0x00010000 |
| 52 | HOSTEVENT_INVALID = 0x80000000 |
| 53 | |
| 54 | |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 55 | class ChromeEC(object): |
| 56 | """Manages control of a Chrome EC. |
| 57 | |
| 58 | We control the Chrome EC via the UART of a Servo board. Chrome EC |
| 59 | provides many interfaces to set and get its behavior via console commands. |
| 60 | This class is to abstract these interfaces. |
| 61 | """ |
| 62 | |
| 63 | def __init__(self, servo): |
| 64 | """Initialize and keep the servo object. |
| 65 | |
| 66 | Args: |
| 67 | servo: A Servo object. |
| 68 | """ |
| 69 | self._servo = servo |
| 70 | |
| 71 | |
| 72 | def send_command(self, command): |
| 73 | """Send command through UART. |
| 74 | |
| 75 | This function opens UART pty when called, and then command is sent |
| 76 | through UART. |
| 77 | |
| 78 | Args: |
| 79 | command: The command string to send. |
| 80 | """ |
| 81 | self._servo.set('ec_uart_regexp', 'None') |
| 82 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 83 | |
| 84 | |
| 85 | def send_command_get_output(self, command, regexp_list, timeout=1): |
| 86 | """Send command through UART and wait for response. |
| 87 | |
| 88 | This function waits for response message matching regular expressions. |
| 89 | |
| 90 | Args: |
| 91 | command: The command sent. |
| 92 | regexp_list: List of regular expressions used to match response |
| 93 | message. Note, list must be ordered. |
| 94 | |
| 95 | Returns: |
| 96 | List of tuples, each of which contains the entire matched string and |
| 97 | all the subgroups of the match. None if not matched. |
| 98 | For example: |
| 99 | response of the given command: |
| 100 | High temp: 37.2 |
| 101 | Low temp: 36.4 |
| 102 | regexp_list: |
| 103 | ['High temp: (\d+)\.(\d+)', 'Low temp: (\d+)\.(\d+)'] |
| 104 | returns: |
| 105 | [('High temp: 37.2', '37', '2'), ('Low temp: 36.4', '36', '4')] |
| 106 | |
| 107 | Raises: |
| 108 | error.TestError: An error when the given regexp_list is not valid. |
| 109 | """ |
| 110 | if not isinstance(regexp_list, list): |
| 111 | raise error.TestError('Arugment regexp_list is not a list: %s' % |
| 112 | str(regexp_list)) |
| 113 | |
| 114 | self._servo.set('ec_uart_timeout', str(float(timeout))) |
| 115 | self._servo.set('ec_uart_regexp', str(regexp_list)) |
| 116 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 117 | return ast.literal_eval(self._servo.get('ec_uart_cmd')) |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def key_down(self, keyname): |
| 121 | """Simulate pressing a key. |
| 122 | |
| 123 | Args: |
| 124 | keyname: Key name, one of the keys of KEYMATRIX. |
| 125 | """ |
| 126 | self.send_command('kbpress %d %d 1' % |
| 127 | (KEYMATRIX[keyname][1], KEYMATRIX[keyname][0])) |
| 128 | |
| 129 | |
| 130 | def key_up(self, keyname): |
| 131 | """Simulate releasing a key. |
| 132 | |
| 133 | Args: |
| 134 | keyname: Key name, one of the keys of KEYMATRIX. |
| 135 | """ |
| 136 | self.send_command('kbpress %d %d 0' % |
| 137 | (KEYMATRIX[keyname][1], KEYMATRIX[keyname][0])) |
| 138 | |
| 139 | |
| 140 | def key_press(self, keyname): |
| 141 | """Press and then release a key. |
| 142 | |
| 143 | Args: |
| 144 | keyname: Key name, one of the keys of KEYMATRIX. |
| 145 | """ |
| 146 | self.key_down(keyname) |
| 147 | self.key_up(keyname) |
| 148 | |
| 149 | |
| 150 | def send_key_string_raw(self, string): |
| 151 | """Send key strokes consisting of only characters. |
| 152 | |
| 153 | Args: |
| 154 | string: Raw string. |
| 155 | """ |
| 156 | for c in string: |
| 157 | self.key_press(c) |
| 158 | |
| 159 | |
| 160 | def send_key_string(self, string): |
| 161 | """Send key strokes including special keys. |
| 162 | |
| 163 | Args: |
| 164 | string: Character string including special keys. An example |
| 165 | is "this is an<tab>example<enter>". |
| 166 | """ |
| 167 | for m in re.finditer("(<[^>]+>)|([^<>]+)", string): |
| 168 | sp, raw = m.groups() |
| 169 | if raw is not None: |
| 170 | self.send_key_string_raw(raw) |
| 171 | else: |
| 172 | self.key_press(sp) |
Tom Wai-Hong Tam | bb92e6c | 2012-10-30 11:06:09 +0800 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def reboot(self, flags=''): |
| 176 | """Reboot EC with given flags. |
| 177 | |
| 178 | Args: |
| 179 | flags: Optional, a space-separated string of flags passed to the |
| 180 | reboot command, including: |
| 181 | default: EC soft reboot; |
| 182 | 'hard': EC hard/cold reboot; |
| 183 | 'ap-off': Leave AP off after EC reboot (by default, EC turns |
| 184 | AP on after reboot if lid is open). |
| 185 | |
| 186 | Raises: |
| 187 | error.TestError: If the string of flags is invalid. |
| 188 | """ |
| 189 | for flag in flags.split(): |
| 190 | if flag not in ('hard', 'ap-off'): |
| 191 | raise error.TestError( |
| 192 | 'The flag %s of EC reboot command is invalid.' % flag) |
| 193 | self.send_command("reboot %s" % flags) |
Tom Wai-Hong Tam | 05574ee | 2012-10-30 11:34:21 +0800 | [diff] [blame] | 194 | |
| 195 | |
| 196 | def set_flash_write_protect(self, enable): |
| 197 | """Set the software write protect of EC flash. |
| 198 | |
| 199 | Args: |
| 200 | enable: True to enable write protect, False to disable. |
| 201 | """ |
| 202 | if enable: |
| 203 | self.send_command("flashwp enable") |
| 204 | else: |
| 205 | self.send_command("flashwp disable") |
Tom Wai-Hong Tam | 665281c | 2012-10-30 11:55:10 +0800 | [diff] [blame^] | 206 | |
| 207 | |
| 208 | def set_hostevent(self, codes): |
| 209 | """Set the EC hostevent codes. |
| 210 | |
| 211 | Args: |
| 212 | codes: Hostevent codes, HOSTEVENT_* |
| 213 | """ |
| 214 | self.send_command("hostevent set %#x" % codes) |