blob: 690716c9f56286ad8529e314ee3e55b8336d3aa6 [file] [log] [blame]
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +08001# 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 Hendricks548f08e2014-05-09 15:05:49 -07005import ast, logging, re, time
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +08006
7from autotest_lib.client.common_lib import error
Victor Hsiehb5210ef2016-04-22 15:20:02 -07008from autotest_lib.client.cros import ec
Tom Wai-Hong Tam8326bff2012-10-12 15:34:38 +08009
Tom Wai-Hong Tam665281c2012-10-30 11:55:10 +080010# Hostevent codes, copied from:
11# ec/include/ec_commands.h
12HOSTEVENT_LID_CLOSED = 0x00000001
13HOSTEVENT_LID_OPEN = 0x00000002
14HOSTEVENT_POWER_BUTTON = 0x00000004
15HOSTEVENT_AC_CONNECTED = 0x00000008
16HOSTEVENT_AC_DISCONNECTED = 0x00000010
17HOSTEVENT_BATTERY_LOW = 0x00000020
18HOSTEVENT_BATTERY_CRITICAL = 0x00000040
19HOSTEVENT_BATTERY = 0x00000080
20HOSTEVENT_THERMAL_THRESHOLD = 0x00000100
21HOSTEVENT_THERMAL_OVERLOAD = 0x00000200
22HOSTEVENT_THERMAL = 0x00000400
23HOSTEVENT_USB_CHARGER = 0x00000800
24HOSTEVENT_KEY_PRESSED = 0x00001000
25HOSTEVENT_INTERFACE_READY = 0x00002000
26# Keyboard recovery combo has been pressed
27HOSTEVENT_KEYBOARD_RECOVERY = 0x00004000
28# Shutdown due to thermal overload
29HOSTEVENT_THERMAL_SHUTDOWN = 0x00008000
30# Shutdown due to battery level too low
31HOSTEVENT_BATTERY_SHUTDOWN = 0x00010000
32HOSTEVENT_INVALID = 0x80000000
33
34
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +080035class 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 Yang0ef0c092012-11-02 15:28:54 +080050 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 Tam6019a1a2012-10-12 14:03:34 +080058
59
Tom Wai-Hong Tamf65ca352013-02-27 16:29:22 +080060 def send_command(self, commands):
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +080061 """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 Tamf65ca352013-02-27 16:29:22 +080067 commands: The commands to send, either a list or a string.
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +080068 """
Vic Yang0ef0c092012-11-02 15:28:54 +080069 self.set_uart_regexp('None')
Tom Wai-Hong Tamf65ca352013-02-27 16:29:22 +080070 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 Tam6019a1a2012-10-12 14:03:34 +080084
85
Vadim Bendebury5c8f8672013-01-17 17:47:15 -080086 def send_command_get_output(self, command, regexp_list):
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +080087 """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 Yang0ef0c092012-11-02 15:28:54 +0800115 self.set_uart_regexp(str(regexp_list))
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800116 self._servo.set_nocheck('ec_uart_cmd', command)
117 return ast.literal_eval(self._servo.get('ec_uart_cmd'))
Tom Wai-Hong Tam8326bff2012-10-12 15:34:38 +0800118
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 Hsiehb5210ef2016-04-22 15:20:02 -0700127 (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0]))
Tom Wai-Hong Tam8326bff2012-10-12 15:34:38 +0800128
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 Hsiehb5210ef2016-04-22 15:20:02 -0700137 (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0]))
Tom Wai-Hong Tam8326bff2012-10-12 15:34:38 +0800138
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 Tamf65ca352013-02-27 16:29:22 +0800146 self.send_command([
147 'kbpress %d %d 1' %
Victor Hsiehb5210ef2016-04-22 15:20:02 -0700148 (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0]),
Tom Wai-Hong Tamf65ca352013-02-27 16:29:22 +0800149 'kbpress %d %d 0' %
Victor Hsiehb5210ef2016-04-22 15:20:02 -0700150 (ec.KEYMATRIX[keyname][1], ec.KEYMATRIX[keyname][0]),
Tom Wai-Hong Tamf65ca352013-02-27 16:29:22 +0800151 ])
Tom Wai-Hong Tam8326bff2012-10-12 15:34:38 +0800152
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 Tambb92e6c2012-10-30 11:06:09 +0800177
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 Tam05574ee2012-10-30 11:34:21 +0800198
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 Tam665281c2012-10-30 11:55:10 +0800210
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 Hendricks548f08e2014-05-09 15:05:49 -0700219 # 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)
Scott8f77ae82016-05-25 16:40:55 -0700223
224 def enable_console_channel(self, channel):
225 """Find console channel mask and enable that channel only
226
227 @param channel: console channel name
228 """
229 # The 'chan' command returns a list of console channels,
230 # their channel masks and channel numbers
231 regexp = r'(\d+)\s+([\w]+)\s+\*?\s+{0}'.format(channel)
232 l = self.send_command_get_output('chan', [regexp])
233 # Use channel mask and append the 0x for proper hex input value
234 cmd = 'chan 0x' + l[0][2]
235 # Set console to only output the desired channel
236 self.send_command(cmd)