• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import logging
6import os
7
8from autotest_lib.client.common_lib import autotemp
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.common_lib import utils as client_utils
11from autotest_lib.server import autotest
12from autotest_lib.server import hosts
13from autotest_lib.server import test
14from autotest_lib.server import utils
15
16bored_now = """
17[  _____             ____  _               _  ]
18[ |_   _|__   ___   / ___|| | _____      _| | ]
19[  | |/ _ \ / _ \  \___ \| |/ _ \ \ /\ / /| | ]
20[  | | (_) | (_) |  ___) | | (_) \ V  V / |_| ]
21[  |_|\___/ \___/  |____/|_|\___/ \_/\_/  (_) ]
22[                                             ]
23[ The device didn't wake up - either the HID device isn't working or  ]
24[ the chromebook just didn't wake up: Either way, wake the chromebook ]
25[ so we can finish the test, or we'll be sitting here for a while...  ]
26"""
27
28press_button_banner = """
29[     _   _   _             _   _              ]
30[    / \ | |_| |_ ___ _ __ | |_(_) ___  _ __   ]
31[   / _ \| __| __/ _ \ '_ \| __| |/ _ \| '_ \  ]
32[  / ___ \ |_| ||  __/ | | | |_| | (_) | | | | ]
33[ /_/   \_\__|\__\___|_| |_|\__|_|\___/|_| |_| ]
34[                                              ]
35[ Press the power, sleep or other suitable button on your USB HID Device ]
36[ NOTE: NOT on the Chromebook itself - on the USB Keyboard/Remote/etc    ]
37[ Then press Return or Enter here so we can proceed with the test        ]
38"""
39
40
41class platform_USBHIDWake(test.test):
42    version = 1
43
44    def suspend(self):
45        self._client.run("(echo mem > /sys/power/state &)")
46
47
48    def check_dependencies(self):
49        if not utils.system('which openvt', ignore_status=True) == 0:
50            raise error.TestError('openvt missing (see control file)')
51        if not utils.system('sudo true', ignore_status=True) == 0:
52            raise error.TestError('Insufficient privileges: cannot sudo')
53
54
55    def prompt(self, banner=">>>>>>>>>>> Achtung! <<<<<<<<<<<"):
56        """prompt the user with the supplied banner,
57        then wait for them to press enter
58
59        @param banner: A [possibly multi-line] banner prompt to display
60        """
61        temp = autotemp.tempfile(unique_id='vtprompt', text=True)
62        os.write(temp.fd, banner)
63        pcmd = ("sudo openvt -s -w -- " +
64                "sh -c 'clear && cat %s && read -p \"READY> \" REPLY &&" +
65                " echo $REPLY'") % temp.name
66        utils.system(pcmd)
67        temp.clean()
68
69
70    def wait_for_host(self, host=None, timeout=30):
71        '''Wait for the DUT to come back up, with a timeout
72
73        @param host: ip address or hostname of DUT
74        @param timeout: maximum time in seconds to wait
75
76        Returns True if the host comes up in time, False otherwise'''
77        return client_utils.ping(host, deadline=timeout) == 0
78
79
80    def have_hid_device(self):
81        """Return True is a USB HID device is present, False otherwise"""
82        cmd = 'grep "^03$" /sys/bus/usb/devices/[0-9]*/[0-9]*/bInterfaceClass'
83        rval = self._client.run(cmd, ignore_status=True)
84        return rval.exit_status == 0
85
86
87    def run_once(self, client_ip):
88        """Check to see if a DUT at the given address wakes from suspend
89        on USB HID events
90
91        @param client_ip: ip address (string) at which the DUT may be found"""
92        self.check_dependencies()
93        if not client_ip:
94            raise error.TestError('Must have test client IP address')
95        self._client = hosts.create_host(client_ip)
96        if not self.have_hid_device():
97            raise error.TestError('No HID devices found, please attach one')
98        self.suspend()
99        self.prompt(banner=press_button_banner)
100        if not self.wait_for_host(host=client_ip, timeout=10):
101            self.prompt(banner=bored_now)
102            raise error.TestFail('DUT did not wake up on HID event')
103