• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 glob
6import logging
7import pprint
8from threading import Timer
9
10from autotest_lib.client.bin.input.input_device import *
11
12
13class firmwareCheckKeys(object):
14    """An abstraction to deal with checking firmware keys."""
15    # pylint: disable=undefined-variable
16    version = 1
17    actual_output = []
18    device = None
19    ev = None
20
21    def __init__(self):
22        for evdev in glob.glob("/dev/input/event*"):
23            device = InputDevice(evdev)
24            if device.is_keyboard():
25                print 'keyboard device %s' % evdev
26                self.device = device
27
28    def _keyboard_input(self):
29        """Read key presses."""
30        index = 0
31        while True:
32            self.ev.read(self.device.f)
33            if self.ev.code != KEY_RESERVED:
34                print "EventCode is %d value is %d" % (self.ev.code,
35                                                       self.ev.value)
36                if self.ev.type == 0 or self.ev.type == 1:
37                    self.actual_output.append(self.ev.code)
38                    index = index + 1
39
40    def check_keys(self, expected_sequence):
41        """Wait for key press for 10 seconds.
42
43        @return number of input keys captured, -1 for error.
44        """
45        if not self.device:
46            logging.error("Could not find a keyboard device")
47            return -1
48
49        self.ev = InputEvent()
50        Timer(0, self._keyboard_input).start()
51
52        time.sleep(10)
53
54        # Keypresses will have a tendency to repeat as there is delay between
55        # the down and up events.  We're not interested in precisely how many
56        # repeats of the key there is, just what is the sequence of keys,
57        # so, we will make the list unique.
58        uniq_actual_output = sorted(list(set(self.actual_output)))
59        if uniq_actual_output != expected_sequence:
60            print 'Keys mismatched %s' % pprint.pformat(uniq_actual_output)
61            return -1
62        print 'Key match expected: %s' % pprint.pformat(uniq_actual_output)
63        return len(uniq_actual_output)
64