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 | |
| 5 | import ast |
| 6 | |
| 7 | from autotest_lib.client.common_lib import error |
| 8 | |
| 9 | class ChromeEC(object): |
| 10 | """Manages control of a Chrome EC. |
| 11 | |
| 12 | We control the Chrome EC via the UART of a Servo board. Chrome EC |
| 13 | provides many interfaces to set and get its behavior via console commands. |
| 14 | This class is to abstract these interfaces. |
| 15 | """ |
| 16 | |
| 17 | def __init__(self, servo): |
| 18 | """Initialize and keep the servo object. |
| 19 | |
| 20 | Args: |
| 21 | servo: A Servo object. |
| 22 | """ |
| 23 | self._servo = servo |
| 24 | |
| 25 | |
| 26 | def send_command(self, command): |
| 27 | """Send command through UART. |
| 28 | |
| 29 | This function opens UART pty when called, and then command is sent |
| 30 | through UART. |
| 31 | |
| 32 | Args: |
| 33 | command: The command string to send. |
| 34 | """ |
| 35 | self._servo.set('ec_uart_regexp', 'None') |
| 36 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 37 | |
| 38 | |
| 39 | def send_command_get_output(self, command, regexp_list, timeout=1): |
| 40 | """Send command through UART and wait for response. |
| 41 | |
| 42 | This function waits for response message matching regular expressions. |
| 43 | |
| 44 | Args: |
| 45 | command: The command sent. |
| 46 | regexp_list: List of regular expressions used to match response |
| 47 | message. Note, list must be ordered. |
| 48 | |
| 49 | Returns: |
| 50 | List of tuples, each of which contains the entire matched string and |
| 51 | all the subgroups of the match. None if not matched. |
| 52 | For example: |
| 53 | response of the given command: |
| 54 | High temp: 37.2 |
| 55 | Low temp: 36.4 |
| 56 | regexp_list: |
| 57 | ['High temp: (\d+)\.(\d+)', 'Low temp: (\d+)\.(\d+)'] |
| 58 | returns: |
| 59 | [('High temp: 37.2', '37', '2'), ('Low temp: 36.4', '36', '4')] |
| 60 | |
| 61 | Raises: |
| 62 | error.TestError: An error when the given regexp_list is not valid. |
| 63 | """ |
| 64 | if not isinstance(regexp_list, list): |
| 65 | raise error.TestError('Arugment regexp_list is not a list: %s' % |
| 66 | str(regexp_list)) |
| 67 | |
| 68 | self._servo.set('ec_uart_timeout', str(float(timeout))) |
| 69 | self._servo.set('ec_uart_regexp', str(regexp_list)) |
| 70 | self._servo.set_nocheck('ec_uart_cmd', command) |
| 71 | return ast.literal_eval(self._servo.get('ec_uart_cmd')) |