• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 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"""Tests for the servo `power_state` control.
6
7## Purpose
8This test exists to ensure that servo's `power_state` control provides a
9consistent, hardware-independent implementation across all models of
10Chrome hardware.
11
12    ==== NOTE NOTE NOTE ====
13    YOU ARE NOT ALLOWED TO CHANGE THIS TEST TO TREAT DIFFERENT HARDWARE
14    DIFFERENTLY.
15
16In particular, importing from `faft.config` in order to customize this
17test for different hardware is never allowed.  This has been tried twice
18before; anyone making a third attempt is liable to be taken out and
19shot.  Or at least, be given a good talking to.
20
21This test is intended to enforce a contract between hardware-independent
22parts of Autotest (especially the repair code) and hardware-dependent
23implementations of servo in `hdctools`.  The contract imposes requirements
24and limitations on both sides.
25
26## Requirements on the servo implementation in `hdctools`
27### Setting `power_state:off`
28Setting `power_state:off` must turn the DUT "off" unconditionally.  The
29operation must have the same effect regardless of the state of the DUT
30prior to the operation.  The operation is not allowed to fail.
31
32The meaning of "off" does not require any specific component to be
33actually powered off, provided that the DUT does not respond on the
34network, and does not respond to USB devices being plugged or unplugged.
35Some examples of "off" that are acceptable:
36  * A system in ACPI power state S5.
37  * A system held in reset indefinitely by asserting `cold_reset:on`.
38  * In general, any system where power is not supplied to the AP.
39
40While a DUT is turned off, it is allowed to respond to the power button,
41the lid, and to the reset signals in ways that are hardware dependent.
42The signals may be ignored, or they may have any other effect that's
43appropriate to the hardware, such as turning the DUT on.
44
45### Setting `power_state:on` and `power_state:rec`
46After applying `power_state:off` to turn a DUT off, setting
47`power_state:on` or `power_state:rec` will turn the DUT on.
48  * Setting "on" turns the DUT on in normal mode.  The DUT will
49    boot normally as for a cold start.
50  * Setting "rec" turns the DUT on in recovery mode.  The DUT
51    will go to the recovery screen and wait for a USB stick to be
52    inserted.
53
54If a DUT is not off because of setting `power_state:off`, the response
55to "on" and "rec" may be hardware dependent.  The settings may turn the
56device on as specified, they may have no effect, or they may provide any
57other response that's appropriate to the hardware.
58
59### Setting `power_state:reset`
60Applying `power_state:reset` has the same visible effects as setting
61`power_state:off` followed by `power_state:on`.  The operation must have
62the same effect regardless of the state of the DUT prior to the
63operation.  The operation is not allowed to fail.
64
65Additionally, applying reset will assert and deassert `cold_reset` to
66ensure a full hardware reset.
67
68### Timing Constraints
69The servo implementation for `power_state` cannot impose timing
70constraints on Autotest.  If the hardware imposes constraints, the servo
71implemention must provide the necessary time delays by sleeping.
72
73In particular:
74  * After `power_state:off`, it is allowed to immediately apply either
75    `on` or `rec`.
76  * After `rec`, USB stick insertion must be recognized immediately.
77  * Setting `power_state:reset` twice in a row must work reliably.
78
79### Requirements on Autotest
80### Setting `power_state:off`
81Once a DUT has been turned "off", changing signals such as the power
82button, the lid, or reset signals isn't allowed.  The only allowed
83state changes are these:
84  * USB muxes may be switched at will.
85  * The `power_state` control can be set to any valid setting.
86
87Any other operation may cause the DUT to change state unpredictably
88(e.g. by powering the DUT on).
89
90## Setting `power_state:on` and `power_state:rec`
91Autotest can only apply `power_state:on` or `power_state:rec` if the DUT
92was previously turned off with `power_state:off`.
93
94"""
95
96import logging
97import time
98
99# STOP!  You are not allowed to import anything from FAFT.  Read the
100# comments above.
101from autotest_lib.client.common_lib import error
102from autotest_lib.server import test
103
104
105class platform_ServoPowerStateController(test.test):
106    """Test servo can power on and off DUT in recovery and non-recovery mode."""
107    version = 1
108
109
110    def initialize(self, host):
111        """Initialize DUT for testing."""
112        pass
113
114    # This is used as detection of b/159938441 occurrence
115    def check_vbus(self):
116        """Check if Vbus is supplied"""
117        # Check the issue occurs - VBUS is not supplied.
118        mv = self.host.servo.get_vbus_voltage()
119        if mv is not None and mv < self.host.servo.VBUS_THRESHOLD:
120            return False
121        # If vbus voltage monitoring isn't supported, does not signal the issue
122        return True
123
124    def cleanup(self):
125        """Clean up DUT after servo actions."""
126        # Ensure DUTs with type_c servo are in src mode.
127        self.host.servo.set_servo_v4_role('src')
128        if not self.host.is_up():
129            # Power off, then power on DUT from internal storage.
130            self.host.power_off_via_servo()
131            self.host.servo.switch_usbkey('off')
132            self.host.power_on_via_servo(self.controller.REC_OFF)
133            self.host.wait_up(timeout=300)
134
135
136    def assert_dut_on(self, rec_on=False):
137        """Confirm DUT is powered on, claim test failure if DUT is off.
138
139        @param rec_on: True if DUT should boot from external USB stick as in
140                       recovery mode.
141
142        @raise TestFail: If DUT is off or DUT boot from wrong source.
143        """
144        if not self.host.wait_up(timeout=300):
145            raise error.TestFail('power_state:%s did not turn DUT on.' %
146                                 ('rec' if rec_on else 'on'))
147
148        # Check boot source. Raise TestFail if DUT boot from wrong source.
149        boot_from_usb = self.host.is_boot_from_usb()
150        if boot_from_usb != rec_on:
151            boot_source = ('USB' if boot_from_usb else
152                           'non-removable storage')
153            raise error.TestFail('power_state:%s booted from %s.' %
154                                 ('rec' if rec_on else 'on', boot_source))
155
156
157    def assert_dut_off(self, error_message):
158        """Confirm DUT is off and does not turn back on after 30 seconds.
159
160        @param error_message: Error message to raise if DUT stays on.
161        @raise TestFail: If DUT stays on.
162        """
163        if not self.host.ping_wait_down(timeout=10):
164            raise error.TestFail(error_message)
165
166        if self.host.ping_wait_up(timeout=30):
167            raise error.TestFail('%s. %s' % (error_message, 'DUT turns back on'
168                                             ' after it is turned off.'))
169
170
171    def test_with_usb_plugged_in(self):
172        """Run test when USB stick is plugged in to servo."""
173
174        # Servo V4 needs to be in snk role to allow booting from USB in
175        # recovery mode (b/161464597).
176        # TODO(waihong): Add a check to see if the battery level is too
177        # low and sleep for a while for charging.
178        if self.host.get_board().split(':')[1] == 'grunt':
179            # Skip on Grunt to avoid breaking CCD (b/170167166).
180            # TODO: Remove this once Grunt FW is fixed.
181            logging.info('Grunt: Do not put servo_v4 into snk role')
182        else:
183            logging.info('Put servo_v4 into snk role')
184            self.host.servo.set_servo_v4_role('snk')
185
186        logging.info('Power off DUT')
187        self.host.power_off_via_servo()
188        self.assert_dut_off('power_state:off did not turn DUT off.')
189
190        logging.info('Power DUT on in recovery mode, DUT shall boot from USB.')
191        self.host.servo.switch_usbkey('off')
192        self.host.power_on_via_servo(self.controller.REC_ON)
193        self.assert_dut_off('power_state:rec didn\'t stay at recovery screen.')
194
195        self.host.servo.switch_usbkey('dut')
196        time.sleep(30)
197        if self.check_vbus():
198            self.assert_dut_on(rec_on=True)
199            logging.info('Power off DUT which is up in recovery mode.')
200        else:
201            logging.error('EC/RO bug(b:159938441) present.'
202                          'The check for power_state:rec with USB plugged'
203                          'omitted for this board.')
204            logging.info('Power off DUT at recovery screen.')
205
206        self.host.power_off_via_servo()
207        self.assert_dut_off('power_state:off failed after boot from external '
208                            'USB stick.')
209
210        logging.info('Power DUT off in recovery mode without booting.')
211        self.host.servo.switch_usbkey('off')
212        self.host.power_on_via_servo(self.controller.REC_ON)
213        self.host.power_off_via_servo()
214        self.assert_dut_off('power_state:off failed at recovery screen ')
215
216        # Power DUT on in non-recovery mode with USB stick plugged in.
217        # DUT shall boot from internal storage.
218        logging.info('Power on DUT in non-recovery mode.')
219        self.host.servo.switch_usbkey('dut')
220        self.host.power_on_via_servo(self.controller.REC_OFF)
221        self.assert_dut_on()
222        self.host.servo.switch_usbkey('off')
223
224
225    def test_with_usb_unplugged(self):
226        """Run test when USB stick is not plugged in servo."""
227        # Power off DUT regardless its current status.
228        logging.info('Power off DUT.')
229        self.host.power_off_via_servo()
230        self.assert_dut_off('power_state:off did not turn DUT off.')
231
232        # Try to power off the DUT again, make sure the DUT stays off.
233        logging.info('Power off DUT which is already off.')
234        self.host.power_off_via_servo()
235        self.assert_dut_off('power_state:off turned DUT on.')
236
237        # USB stick should be unplugged before the test.
238        self.host.servo.switch_usbkey('off')
239
240        logging.info('Power on in non-recovery mode.')
241        self.host.power_on_via_servo(self.controller.REC_OFF)
242        self.assert_dut_on(rec_on=False)
243
244        logging.info('Power DUT off and on without delay. DUT should be '
245                     'on after power_on is completed.')
246        self.host.power_off_via_servo()
247        self.host.power_on_via_servo(self.controller.REC_OFF)
248        self.assert_dut_on(rec_on=False)
249
250        logging.info('Power off DUT which is up in non-recovery mode.')
251        self.host.power_off_via_servo()
252        self.assert_dut_off('power_state:off failed after boot from '
253                            'internal storage.')
254
255        logging.info('Power DUT off and reset. DUT should be on after '
256                     'reset is completed.')
257        self.host.reset_via_servo()
258        self.assert_dut_on(rec_on=False)
259
260        logging.info('Reset DUT when it\'s on. DUT should be on after '
261                     'reset is completed.')
262        boot_id = self.host.get_boot_id()
263        self.host.reset_via_servo()
264        self.assert_dut_on(rec_on=False)
265        new_boot_id = self.host.get_boot_id()
266        if not new_boot_id or boot_id == new_boot_id:
267            raise error.TestFail('power_state:reset failed to reboot DUT.')
268
269
270    def run_once(self, host, usb_available=True):
271        """Run the test.
272
273        @param host: host object of tested DUT.
274        @param usb_plugged_in: True if USB stick is plugged in servo.
275        """
276        self.host = host
277        self.controller = host.servo.get_power_state_controller()
278
279        self.test_with_usb_unplugged()
280        if usb_available:
281            self.test_with_usb_plugged_in()
282