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 | |
David Hendricks | 548f08e | 2014-05-09 15:05:49 -0700 | [diff] [blame] | 5 | import ast, logging, re, time |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 6 | |
| 7 | from autotest_lib.client.common_lib import error |
Victor Hsieh | b5210ef | 2016-04-22 15:20:02 -0700 | [diff] [blame^] | 8 | from autotest_lib.client.cros import ec |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 9 | |
Tom Wai-Hong Tam | 665281c | 2012-10-30 11:55:10 +0800 | [diff] [blame] | 10 | # Hostevent codes, copied from: |
| 11 | # ec/include/ec_commands.h |
| 12 | HOSTEVENT_LID_CLOSED = 0x00000001 |
| 13 | HOSTEVENT_LID_OPEN = 0x00000002 |
| 14 | HOSTEVENT_POWER_BUTTON = 0x00000004 |
| 15 | HOSTEVENT_AC_CONNECTED = 0x00000008 |
| 16 | HOSTEVENT_AC_DISCONNECTED = 0x00000010 |
| 17 | HOSTEVENT_BATTERY_LOW = 0x00000020 |
| 18 | HOSTEVENT_BATTERY_CRITICAL = 0x00000040 |
| 19 | HOSTEVENT_BATTERY = 0x00000080 |
| 20 | HOSTEVENT_THERMAL_THRESHOLD = 0x00000100 |
| 21 | HOSTEVENT_THERMAL_OVERLOAD = 0x00000200 |
| 22 | HOSTEVENT_THERMAL = 0x00000400 |
| 23 | HOSTEVENT_USB_CHARGER = 0x00000800 |
| 24 | HOSTEVENT_KEY_PRESSED = 0x00001000 |
| 25 | HOSTEVENT_INTERFACE_READY = 0x00002000 |
| 26 | # Keyboard recovery combo has been pressed |
| 27 | HOSTEVENT_KEYBOARD_RECOVERY = 0x00004000 |
| 28 | # Shutdown due to thermal overload |
| 29 | HOSTEVENT_THERMAL_SHUTDOWN = 0x00008000 |
| 30 | # Shutdown due to battery level too low |
| 31 | HOSTEVENT_BATTERY_SHUTDOWN = 0x00010000 |
| 32 | HOSTEVENT_INVALID = 0x80000000 |
| 33 | |
| 34 | |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 35 | class ChromeEC(object): |
| 36 | """Manages control of a Chrome EC. |
| 37 | |
| 38 | We control the Chrome EC via the UART of a Servo board. Chrome EC |
| 39 | provides many interfaces to set and get its behavior via console commands. |
| 40 | This class is to abstract these interfaces. |
| 41 | """ |
| 42 | |
| 43 | def __init__(self, servo): |
| 44 | """Initialize and keep the servo object. |
| 45 | |
| 46 | Args: |
| 47 | servo: A Servo object. |
| 48 | """ |
| 49 | self._servo = servo |
Vic Yang | 0ef0c09 | 2012-11-02 15:28:54 +0800 | [diff] [blame] | 50 | self._cached_uart_regexp = None |
| 51 | |
| 52 | |
| 53 | def set_uart_regexp(self, regexp): |
| 54 | if self._cached_uart_regexp == regexp: |
| 55 | return |
| 56 | self._cached_uart_regexp = regexp |
| 57 | self._servo.set('ec_uart_regexp', regexp) |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 58 | |
| 59 | |
Tom Wai-Hong Tam | f65ca35 | 2013-02-27 16:29:22 +0800 | [diff] [blame] | 60 | def send_command(self, commands): |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 61 | """Send command through UART. |
| 62 | |
| 63 | This function opens UART pty when called, and then command is sent |
| 64 | through UART. |
| 65 | |
| 66 | Args: |
Tom Wai-Hong Tam | f65ca35 | 2013-02-27 16:29:22 +0800 | [diff] [blame] | 67 | commands: The commands to send, either a list or a string. |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 68 | """ |
Vic Yang | 0ef0c09 | 2012-11-02 15:28:54 +0800 | [diff] [blame] | 69 | self.set_uart_regexp('None') |
Tom Wai-Hong Tam | f65ca35 | 2013-02-27 16:29:22 +0800 | [diff] [blame] | 70 | if isinstance(commands, list): |
| 71 | try: |
| 72 | self._servo.set_nocheck('ec_uart_multicmd', ';'.join(commands)) |
| 73 | except error.TestFail as e: |
| 74 | if 'No control named' in str(e): |
| 75 | logging.warning( |
| 76 | 'The servod is too old that ec_uart_multicmd ' |
| 77 | 'not supported. Use ec_uart_cmd instead.') |
| 78 | for command in commands: |
| 79 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 80 | else: |
| 81 | raise |
| 82 | else: |
| 83 | self._servo.set_nocheck('ec_uart_cmd', commands) |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 84 | |
| 85 | |
Vadim Bendebury | 5c8f867 | 2013-01-17 17:47:15 -0800 | [diff] [blame] | 86 | def send_command_get_output(self, command, regexp_list): |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 87 | """Send command through UART and wait for response. |
| 88 | |
| 89 | This function waits for response message matching regular expressions. |
| 90 | |
| 91 | Args: |
| 92 | command: The command sent. |
| 93 | regexp_list: List of regular expressions used to match response |
| 94 | message. Note, list must be ordered. |
| 95 | |
| 96 | Returns: |
| 97 | List of tuples, each of which contains the entire matched string and |
| 98 | all the subgroups of the match. None if not matched. |
| 99 | For example: |
| 100 | response of the given command: |
| 101 | High temp: 37.2 |
| 102 | Low temp: 36.4 |
| 103 | regexp_list: |
| 104 | ['High temp: (\d+)\.(\d+)', 'Low temp: (\d+)\.(\d+)'] |
| 105 | returns: |
| 106 | [('High temp: 37.2', '37', '2'), ('Low temp: 36.4', '36', '4')] |
| 107 | |
| 108 | Raises: |
| 109 | error.TestError: An error when the given regexp_list is not valid. |
| 110 | """ |
| 111 | if not isinstance(regexp_list, list): |
| 112 | raise error.TestError('Arugment regexp_list is not a list: %s' % |
| 113 | str(regexp_list)) |
| 114 | |
Vic Yang | 0ef0c09 | 2012-11-02 15:28:54 +0800 | [diff] [blame] | 115 | self.set_uart_regexp(str(regexp_list)) |
Tom Wai-Hong Tam | 6019a1a | 2012-10-12 14:03:34 +0800 | [diff] [blame] | 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' % |
Victor Hsieh | b5210ef | 2016-04-22 15:20:02 -0700 | [diff] [blame^] | 127 | (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0])) |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 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' % |
Victor Hsieh | b5210ef | 2016-04-22 15:20:02 -0700 | [diff] [blame^] | 137 | (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0])) |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 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 | """ |
Tom Wai-Hong Tam | f65ca35 | 2013-02-27 16:29:22 +0800 | [diff] [blame] | 146 | self.send_command([ |
| 147 | 'kbpress %d %d 1' % |
Victor Hsieh | b5210ef | 2016-04-22 15:20:02 -0700 | [diff] [blame^] | 148 | (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0]), |
Tom Wai-Hong Tam | f65ca35 | 2013-02-27 16:29:22 +0800 | [diff] [blame] | 149 | 'kbpress %d %d 0' % |
Victor Hsieh | b5210ef | 2016-04-22 15:20:02 -0700 | [diff] [blame^] | 150 | (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0]), |
Tom Wai-Hong Tam | f65ca35 | 2013-02-27 16:29:22 +0800 | [diff] [blame] | 151 | ]) |
Tom Wai-Hong Tam | 8326bff | 2012-10-12 15:34:38 +0800 | [diff] [blame] | 152 | |
| 153 | |
| 154 | def send_key_string_raw(self, string): |
| 155 | """Send key strokes consisting of only characters. |
| 156 | |
| 157 | Args: |
| 158 | string: Raw string. |
| 159 | """ |
| 160 | for c in string: |
| 161 | self.key_press(c) |
| 162 | |
| 163 | |
| 164 | def send_key_string(self, string): |
| 165 | """Send key strokes including special keys. |
| 166 | |
| 167 | Args: |
| 168 | string: Character string including special keys. An example |
| 169 | is "this is an<tab>example<enter>". |
| 170 | """ |
| 171 | for m in re.finditer("(<[^>]+>)|([^<>]+)", string): |
| 172 | sp, raw = m.groups() |
| 173 | if raw is not None: |
| 174 | self.send_key_string_raw(raw) |
| 175 | else: |
| 176 | self.key_press(sp) |
Tom Wai-Hong Tam | bb92e6c | 2012-10-30 11:06:09 +0800 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def reboot(self, flags=''): |
| 180 | """Reboot EC with given flags. |
| 181 | |
| 182 | Args: |
| 183 | flags: Optional, a space-separated string of flags passed to the |
| 184 | reboot command, including: |
| 185 | default: EC soft reboot; |
| 186 | 'hard': EC hard/cold reboot; |
| 187 | 'ap-off': Leave AP off after EC reboot (by default, EC turns |
| 188 | AP on after reboot if lid is open). |
| 189 | |
| 190 | Raises: |
| 191 | error.TestError: If the string of flags is invalid. |
| 192 | """ |
| 193 | for flag in flags.split(): |
| 194 | if flag not in ('hard', 'ap-off'): |
| 195 | raise error.TestError( |
| 196 | 'The flag %s of EC reboot command is invalid.' % flag) |
| 197 | self.send_command("reboot %s" % flags) |
Tom Wai-Hong Tam | 05574ee | 2012-10-30 11:34:21 +0800 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def set_flash_write_protect(self, enable): |
| 201 | """Set the software write protect of EC flash. |
| 202 | |
| 203 | Args: |
| 204 | enable: True to enable write protect, False to disable. |
| 205 | """ |
| 206 | if enable: |
| 207 | self.send_command("flashwp enable") |
| 208 | else: |
| 209 | self.send_command("flashwp disable") |
Tom Wai-Hong Tam | 665281c | 2012-10-30 11:55:10 +0800 | [diff] [blame] | 210 | |
| 211 | |
| 212 | def set_hostevent(self, codes): |
| 213 | """Set the EC hostevent codes. |
| 214 | |
| 215 | Args: |
| 216 | codes: Hostevent codes, HOSTEVENT_* |
| 217 | """ |
| 218 | self.send_command("hostevent set %#x" % codes) |
David Hendricks | 548f08e | 2014-05-09 15:05:49 -0700 | [diff] [blame] | 219 | # Allow enough time for EC to process input and set flag. |
| 220 | # See chromium:371631 for details. |
| 221 | # FIXME: Stop importing time module if this hack becomes obsolete. |
| 222 | time.sleep(1) |