1# Copyright 2024 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.asserts import assert_equal
18
19from pairing.ble.test_base import BLEPairTestBaseWithGeneralAndDedicatedPairingTests
20
21from pandora.security_pb2 import PairingEventAnswer
22
23
24class BLELegKbdOnlyTestClass(BLEPairTestBaseWithGeneralAndDedicatedPairingTests):
25
26    def _setup_devices(self) -> None:
27        self.ref.config.setdefault('le_enabled', True)
28
29        self.ref.config.setdefault('classic_enabled', False)
30        self.ref.config.setdefault('classic_ssp_enabled', False)
31        self.ref.config.setdefault('classic_sc_enabled', False)
32
33        self.ref.config.setdefault(
34            'server',
35            {
36                # legacy pairing
37                'pairing_sc_enable': False,
38                'pairing_mitm_enable': True,
39                'pairing_bonding_enable': True,
40                # Android IO CAP: Display_KBD
41                # Ref IO CAP:
42                'io_capability': 'keyboard_input_only',
43            },
44        )
45
46    async def accept_pairing(self):
47        notif_expected_pairing_method = 'passkey_entry_notification'
48        req_expected_pairing_method = 'passkey_entry_request'
49
50        responder_ev = await anext(self.responder_pairing_event_stream)
51        logging.debug(f'responder_ev.method_variant()[1]:{responder_ev.method_variant()}')
52
53        if responder_ev.method_variant() == 'just_works':
54            logging.debug('>>> confirming pairing request')
55            ans = PairingEventAnswer(event=responder_ev, confirm=True)
56            self.responder_pairing_event_stream.send_nowait(ans)
57
58            responder_ev = await anext(self.responder_pairing_event_stream)
59            logging.debug(f'responder_ev.method_variant()[2]:{responder_ev.method_variant()}')
60
61        init_ev = await anext(self.initiator_pairing_event_stream)
62        logging.debug(f'init_ev.method_variant():{init_ev.method_variant()}')
63
64        if init_ev.method_variant() == req_expected_pairing_method:
65            notif_ev = responder_ev
66            req_ev = init_ev
67            req_stream = self.initiator_pairing_event_stream
68        else:
69            notif_ev = init_ev
70            req_ev = responder_ev
71            req_stream = self.responder_pairing_event_stream
72
73        assert_equal(notif_ev.method_variant(), notif_expected_pairing_method)
74        assert_equal(req_ev.method_variant(), req_expected_pairing_method)
75
76        notified_passkey = notif_ev.passkey_entry_notification
77
78        ans = PairingEventAnswer(event=req_ev, passkey=notified_passkey)
79        req_stream.send_nowait(ans)
80