Home | History | Annotate | Download | only in bluetooth_IDCheck
      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 
      5 import logging
      6 import re
      7 
      8 from autotest_lib.client.bin import utils
      9 from autotest_lib.client.common_lib import error
     10 from autotest_lib.client.cros.bluetooth import bluetooth_semiauto_helper
     11 
     12 
     13 class bluetooth_IDCheck(bluetooth_semiauto_helper.BluetoothSemiAutoHelper):
     14     """Checks whether the Bluetooth ID and Alias are 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', 'oak', 'reef']
     22 
     23     def warmup(self):
     24         """Overwrite parent warmup; no need to log in."""
     25         pass
     26 
     27     def _check_id(self, adapter_info):
     28         """Fail if the Bluetooth ID is not in the correct format.
     29 
     30         @param adapter_info: a dict of information about this device's adapter
     31         @raises: error.TestFail if incorrect format is found.
     32 
     33         """
     34         modalias = adapter_info['Modalias']
     35         logging.info('Saw Bluetooth ID of: %s', modalias)
     36 
     37         if self._device in self._BLUETOOTH_3_BOARDS:
     38             bt_format = 'bluetooth:v00E0p24..d0300'
     39         else:
     40             bt_format = 'bluetooth:v00E0p24..d0400'
     41 
     42         if not re.match(bt_format, modalias):
     43             raise error.TestFail('%s does not match expected format: %s'
     44                                  % (modalias, bt_format))
     45 
     46     def _check_name(self, adapter_info):
     47         """Fail if the Bluetooth name is not in the correct format.
     48 
     49         @param adapter_info: a dict of information about this device's adapter
     50         @raises: error.TestFail if incorrect format is found.
     51 
     52         """
     53         alias = adapter_info['Alias']
     54         logging.info('Saw Bluetooth Alias of: %s', alias)
     55 
     56         device_type = utils.get_device_type().lower()
     57         alias_format = '%s_[a-z0-9]{4}' % device_type
     58         if not re.match(alias_format, alias.lower()):
     59             raise error.TestFail('%s does not match expected format: %s'
     60                                  % (alias, alias_format))
     61 
     62     def run_once(self):
     63         """Entry point of this test."""
     64         if not self.supports_bluetooth():
     65             return
     66 
     67         self._device = utils.get_board()
     68         if self._device in self._REFERENCE_ONLY:
     69             return
     70 
     71         self.poll_adapter_presence()
     72         adapter_info = self._get_adapter_info()
     73         self._check_id(adapter_info)
     74         self._check_name(adapter_info)
     75