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
5import logging
6
7from autotest_lib.client.bin import utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.bluetooth import bluetooth_semiauto_helper
10
11
12class bluetooth_AdapterSanity(
13        bluetooth_semiauto_helper.BluetoothSemiAutoHelper):
14    """Checks whether the Bluetooth adapter is present and working."""
15    version = 1
16
17    def _find_kernel_errors(self):
18        """Fail test for any suspicious log entries from kernel.
19
20        Ignore some known errors in order to find new ones.
21
22        """
23        fail_terms = ['[^a-z]err[^a-z]']
24        ignore_terms = ['RFKILL control', '"Service Changed" characteristic']
25
26        log_cmd = 'grep -i bluetooth /var/log/messages'
27        for term in ignore_terms:
28            log_cmd += ' | grep -v \'%s\'' % term
29
30        for term in fail_terms:
31            search_cmd = '%s | grep -i \'%s\'' % (log_cmd, term)
32            log_entries = utils.run(search_cmd, ignore_status=True).stdout
33            if len(log_entries) > 0:
34                log_entries.split('\n')
35                logging.info(log_entries)
36                self.collect_logs('Bluetooth kernel error')
37                raise error.TestFail('Bluetooth kernel error found!')
38
39    def warmup(self):
40        """Overwrite parent warmup; no need to log in."""
41        pass
42
43    def run_once(self):
44        """Entry point of this test."""
45        if not self.supports_bluetooth():
46            return
47
48        # Start btmon running.
49        self.start_dump()
50
51        self.poll_adapter_presence()
52
53        # Enable then disable adapter.
54        self.set_adapter_power(True)
55        self.set_adapter_power(False)
56
57        # Check for errors in logs.
58        self._find_kernel_errors()
59