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 logging
6import re
7
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros.bluetooth import bluetooth_semiauto_helper
11
12
13class bluetooth_IDCheck(bluetooth_semiauto_helper.BluetoothSemiAutoHelper):
14    """Checks whether the Bluetooth ID is in the correct format."""
15    version = 1
16
17    # Boards which only support bluetooth version 3 and below
18    _BLUETOOTH_3_BOARDS = ['x86-mario', 'x86-zgb']
19
20    # Boards which are not shipped to customers and don't need an id.
21    _REFERENCE_ONLY = ['rambi', 'nyan']
22
23    def warmup(self):
24        """Overwrite parent warmup; no need to log in."""
25        pass
26
27    def _check_id(self):
28        """Fail if the Bluetooth ID is not in the correct format."""
29        adapter_info = self._get_adapter_info()
30        modalias = adapter_info['Modalias']
31        logging.info('Saw Bluetooth ID of: %s', modalias)
32
33        if self._device in self._BLUETOOTH_3_BOARDS:
34            bt_format = 'bluetooth:v00E0p24..d0300'
35        else:
36            bt_format = 'bluetooth:v00E0p24..d0400'
37
38        if not re.match(bt_format, modalias):
39            raise error.TestError('%s does not match expected format: %s '
40                                 % (modalias, bt_format))
41
42    def run_once(self):
43        """Entry point of this test."""
44        if not self.supports_bluetooth():
45            return
46
47        self._device = utils.get_board()
48        if self._device in self._REFERENCE_ONLY:
49            return
50
51        self.poll_adapter_presence()
52        self._check_id()
53