1# Copyright 2015 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 5import ast, logging 6 7from autotest_lib.client.common_lib import error 8from autotest_lib.server.cros.servo import chrome_ec 9 10 11class ChromeUSBPD(chrome_ec.ChromeEC): 12 """Manages control of a Chrome PD console. 13 14 We control the Chrome USBPD via the UART of a Servo board. Chrome USBPD 15 provides many interfaces to set and get its behavior via console commands. 16 This class is to abstract these interfaces. The methods contained here 17 differ from the ChromeEC class in that they spell out the usbpd_uart_ 18 instead of ec_uart_ 19 """ 20 21 def __init__(self, servo): 22 """Initialize and keep the servo object. 23 24 @param servo: A Servo object 25 """ 26 super(ChromeUSBPD, self).__init__(servo) 27 28 29 def set_uart_regexp(self, regexp): 30 """Sets the regural expression 31 32 @param regexp: regular expression 33 """ 34 if self._cached_uart_regexp == regexp: 35 return 36 self._cached_uart_regexp = regexp 37 self._servo.set('usbpd_uart_regexp', regexp) 38 39 40 def send_command(self, commands): 41 """Send command through UART. 42 43 This function opens UART pty when called, and then command is sent 44 through UART. 45 46 @param commands: The commands to send, either a list or a string. 47 """ 48 self.set_uart_regexp('None') 49 if isinstance(commands, list): 50 try: 51 self._servo.set_nocheck('usbpd_uart_multicmd', ';'.join(commands)) 52 except error.TestFail as e: 53 if 'No control named' in str(e): 54 logging.warning( 55 'The servod is too old that ec_uart_multicmd ' 56 'not supported. Use ec_uart_cmd instead.') 57 for command in commands: 58 self._servo.set_nocheck('usbpd_uart_cmd', command) 59 else: 60 raise 61 else: 62 self._servo.set_nocheck('usbpd_uart_cmd', commands) 63 64 65 def send_command_get_output(self, command, regexp_list): 66 """Send command through UART and wait for response. 67 68 This function waits for response message matching regular expressions. 69 70 @param command: The command sent. 71 @param regexp_list: List of regular expressions used to match response 72 message. Note, list must be ordered. 73 74 @returns: List of tuples, each of which contains the entire matched 75 string and all the subgroups of the match. None if not matched. 76 For example: 77 response of the given command: 78 High temp: 37.2 79 Low temp: 36.4 80 regexp_list: 81 ['High temp: (\d+)\.(\d+)', 'Low temp: (\d+)\.(\d+)'] 82 returns: 83 [('High temp: 37.2', '37', '2'), ('Low temp: 36.4', '36', '4')] 84 85 @raises error.TestError: An error when the given regexp_list 86 is not valid. 87 """ 88 if not isinstance(regexp_list, list): 89 raise error.TestError('Arugment regexp_list is not a list: %s' % 90 str(regexp_list)) 91 92 self.set_uart_regexp(str(regexp_list)) 93 self._servo.set_nocheck('usbpd_uart_cmd', command) 94 return ast.literal_eval(self._servo.get('usbpd_uart_cmd')) 95 96 97