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