1# Copyright 2022 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import logging 16 17from mobly import suite_runner, asserts, base_test 18 19from avatar.controllers import pandora_device 20 21from bumble.smp import PairingDelegate, PairingConfig 22 23 24class ClassicConnect(base_test.BaseTestClass): 25 def setup_class(self): 26 self.pandora_devices = self.register_controller(pandora_device) 27 self.dut = self.pandora_devices[0] 28 self.bumble = self.pandora_devices[1] 29 30 def setup_test(self): 31 self.dut.host.Reset() 32 33 def test_io_cap_keyboard_only(self): 34 self._connect_to_dut(PairingDelegate.KEYBOARD_INPUT_ONLY) 35 36 def test_display_yes_no(self): 37 self._connect_to_dut(PairingDelegate.DISPLAY_OUTPUT_AND_YES_NO_INPUT) 38 39 def test_io_cap_display_only(self): 40 self._connect_to_dut(PairingDelegate.DISPLAY_OUTPUT_ONLY) 41 42 def test_io_cap_no_input_no_output(self): 43 self._connect_to_dut(PairingDelegate.NO_OUTPUT_NO_INPUT) 44 45 def _connect_to_dut(self, io_cap): 46 bumble_address = self.bumble.address 47 self.bumble.device.pairing_config_factory = lambda _: PairingConfig( 48 delegate=Delegate(io_cap, self.dut, bumble_address) 49 ) 50 connect_resp = self.bumble.host.Connect( 51 address=self.dut.address, wait_for_ready=True) 52 asserts.assert_true(connect_resp.WhichOneof( 53 "result") == "connection", "Failed to connect") 54 55 56class Delegate(PairingDelegate): 57 58 def __init__(self, io_capability, dut, address): 59 super().__init__(io_capability) 60 logging.info("Delegate init") 61 self._dut = dut 62 self._address = address 63 64 async def get_number(self): 65 logging.info("get_number") 66 passkey = self._dut.host.ReadPasskey(address=self._address).passkey 67 return passkey 68 69 async def compare_numbers(self, number, digits=6): 70 logging.info("compare_number") 71 dut_passkey = self._dut.host.ReadPasskey(address=self._address).passkey 72 return dut_passkey == number 73 74 75if __name__ == '__main__': 76 logging.basicConfig(level=logging.DEBUG) 77 suite_runner.run_suite([ClassicConnect]) 78