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 | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 30 | class ChromeEC(object): |
| 31 | """Manages control of a Chrome EC. |
| 32 | |
| 33 | We control the Chrome EC via the UART of a Servo board. Chrome EC |
| 34 | provides many interfaces to set and get its behavior via console commands. |
| 35 | This class is to abstract these interfaces. |
| 36 | """ |
| 37 | |
| 38 | def __init__(self, servo): |
| 39 | """Initialize and keep the servo object. |
| 40 | |
| 41 | Args: |
| 42 | servo: A Servo object. |
| 43 | """ |
| 44 | self._servo = servo |
| 45 | |
| 46 | |
| 47 | def send_command(self, command): |
| 48 | """Send command through UART. |
| 49 | |
| 50 | This function opens UART pty when called, and then command is sent |
| 51 | through UART. |
| 52 | |
| 53 | Args: |
| 54 | command: The command string to send. |
| 55 | """ |
| 56 | self._servo.set('ec_uart_regexp', 'None') |
| 57 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 58 | |
| 59 | |
| 60 | def send_command_get_output(self, command, regexp_list, timeout=1): |
| 61 | """Send command through UART and wait for response. |
| 62 | |
| 63 | This function waits for response message matching regular expressions. |
| 64 | |
| 65 | Args: |
| 66 | command: The command sent. |
| 67 | regexp_list: List of regular expressions used to match response |
| 68 | message. Note, list must be ordered. |
| 69 | |
| 70 | Returns: |
| 71 | List of tuples, each of which contains the entire matched string and |
| 72 | all the subgroups of the match. None if not matched. |
| 73 | For example: |
| 74 | response of the given command: |
| 75 | High temp: 37.2 |
| 76 | Low temp: 36.4 |
| 77 | regexp_list: |
| 78 | ['High temp: (\d+)\.(\d+)', 'Low temp: (\d+)\.(\d+)'] |
| 79 | returns: |
| 80 | [('High temp: 37.2', '37', '2'), ('Low temp: 36.4', '36', '4')] |
| 81 | |
| 82 | Raises: |
| 83 | error.TestError: An error when the given regexp_list is not valid. |
| 84 | """ |
| 85 | if not isinstance(regexp_list, list): |
| 86 | raise error.TestError('Arugment regexp_list is not a list: %s' % |
| 87 | str(regexp_list)) |
| 88 | |
| 89 | self._servo.set('ec_uart_timeout', str(float(timeout))) |
| 90 | self._servo.set('ec_uart_regexp', str(regexp_list)) |
| 91 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 92 | return ast.literal_eval(self._servo.get('ec_uart_cmd')) |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def key_down(self, keyname): |
| 96 | """Simulate pressing a key. |
| 97 | |
| 98 | Args: |
| 99 | keyname: Key name, one of the keys of KEYMATRIX. |
| 100 | """ |
| 101 | self.send_command('kbpress %d %d 1' % |
| 102 | (KEYMATRIX[keyname][1], KEYMATRIX[keyname][0])) |
| 103 | |
| 104 | |
| 105 | def key_up(self, keyname): |
| 106 | """Simulate releasing a key. |
| 107 | |
| 108 | Args: |
| 109 | keyname: Key name, one of the keys of KEYMATRIX. |
| 110 | """ |
| 111 | self.send_command('kbpress %d %d 0' % |
| 112 | (KEYMATRIX[keyname][1], KEYMATRIX[keyname][0])) |
| 113 | |
| 114 | |
| 115 | def key_press(self, keyname): |
| 116 | """Press and then release a key. |
| 117 | |
| 118 | Args: |
| 119 | keyname: Key name, one of the keys of KEYMATRIX. |
| 120 | """ |
| 121 | self.key_down(keyname) |
| 122 | self.key_up(keyname) |
| 123 | |
| 124 | |
| 125 | def send_key_string_raw(self, string): |
| 126 | """Send key strokes consisting of only characters. |
| 127 | |
| 128 | Args: |
| 129 | string: Raw string. |
| 130 | """ |
| 131 | for c in string: |
| 132 | self.key_press(c) |
| 133 | |
| 134 | |
| 135 | def send_key_string(self, string): |
| 136 | """Send key strokes including special keys. |
| 137 | |
| 138 | Args: |
| 139 | string: Character string including special keys. An example |
| 140 | is "this is an<tab>example<enter>". |
| 141 | """ |
| 142 | for m in re.finditer("(<[^>]+>)|([^<>]+)", string): |
| 143 | sp, raw = m.groups() |
| 144 | if raw is not None: |
| 145 | self.send_key_string_raw(raw) |
| 146 | else: |
| 147 | self.key_press(sp) |
Tom Wai-Hong Tam | bb92e6c | 2012-10-30 11:06:09 +0800 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def reboot(self, flags=''): |
| 151 | """Reboot EC with given flags. |
| 152 | |
| 153 | Args: |
| 154 | flags: Optional, a space-separated string of flags passed to the |
| 155 | reboot command, including: |
| 156 | default: EC soft reboot; |
| 157 | 'hard': EC hard/cold reboot; |
| 158 | 'ap-off': Leave AP off after EC reboot (by default, EC turns |
| 159 | AP on after reboot if lid is open). |
| 160 | |
| 161 | Raises: |
| 162 | error.TestError: If the string of flags is invalid. |
| 163 | """ |
| 164 | for flag in flags.split(): |
| 165 | if flag not in ('hard', 'ap-off'): |
| 166 | raise error.TestError( |
| 167 | 'The flag %s of EC reboot command is invalid.' % flag) |
| 168 | self.send_command("reboot %s" % flags) |
Tom Wai-Hong Tam | 05574ee | 2012-10-30 11:34:21 +0800 | [diff] [blame^] | 169 | |
| 170 | |
| 171 | def set_flash_write_protect(self, enable): |
| 172 | """Set the software write protect of EC flash. |
| 173 | |
| 174 | Args: |
| 175 | enable: True to enable write protect, False to disable. |
| 176 | """ |
| 177 | if enable: |
| 178 | self.send_command("flashwp enable") |
| 179 | else: |
| 180 | self.send_command("flashwp disable") |