1#  Copyright (C) 2024 The Android Open Source Project
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#       http://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.
14import random
15import sys
16import threading
17import time
18from typing import List, Optional
19
20from uwb import uwb_ranging_params
21from lib import ranging_base_test
22from lib import generic_ranging_decorator
23from mobly import asserts
24from mobly import config_parser
25from mobly import signals
26from mobly import suite_runner
27from test_utils import uwb_test_utils
28
29RESPONDER_STOP_CALLBACK_TIMEOUT = 60
30
31_TEST_CASES = (
32    "test_one_to_one_ranging",
33)
34
35
36class RangingTest(ranging_base_test.RangingBaseTest):
37    """Tests for UWB Ranging APIs.
38
39    Attributes:
40    android_devices: list of android device objects.
41    """
42
43    def __init__(self, configs: config_parser.TestRunConfig):
44        """Init method for the test class.
45
46        Args:
47        configs: A config_parser.TestRunConfig object.
48        """
49        super().__init__(configs)
50        self.tests = _TEST_CASES
51
52    def setup_class(self):
53        super().setup_class()
54        self.uwb_devices = [
55            generic_ranging_decorator.GenericRangingDecorator(ad)
56            for ad in self.android_devices
57        ]
58        self.initiator, self.responder = self.uwb_devices
59        self.device_addresses = self.user_params.get("device_addresses",
60                                                     [[1, 2], [3, 4]])
61        self.initiator_addr, self.responder_addr = self.device_addresses
62        self.new_responder_addr = [4, 5]
63        # self.p_sts_sub_session_id = 11
64        # self.p_sts_sub_session_key = [
65        #     8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8]
66        # self.block_stride_length = random.randint(1, 10)
67
68    def setup_test(self):
69        super().setup_test()
70        # for uwb_device in self.uwb_devices:
71        #     try:
72        #         uwb_device.close_ranging()
73        #     except TimeoutError:
74        #         uwb_device.log.warn("Failed to cleanup ranging sessions")
75        # for uwb_device in self.uwb_devices:
76        #     uwb_test_utils.set_airplane_mode(uwb_device.ad, False)
77        #     self._reset_snippet_fg_bg_state(uwb_device)
78
79    def teardown_test(self):
80        super().teardown_test()
81
82    ### Test Cases ###
83
84    def test_one_to_one_ranging(self):
85        initiator_params = uwb_ranging_params.UwbRangingParams(
86            config_id=1,
87            session_id=5,
88            sub_session_id=1,
89            device_role=uwb_ranging_params.FiraParamEnums.DEVICE_ROLE_INITIATOR,
90            device_type=uwb_ranging_params.FiraParamEnums.DEVICE_TYPE_CONTROLLER,
91            device_address=self.initiator_addr,
92            destination_addresses=[self.responder_addr],
93        )
94        responder_params = uwb_ranging_params.UwbRangingParams(
95            config_id=1,
96            session_id=5,
97            sub_session_id=1,
98            device_role=uwb_ranging_params.FiraParamEnums.DEVICE_ROLE_RESPONDER,
99            device_type=uwb_ranging_params.FiraParamEnums.DEVICE_TYPE_CONTROLEE,
100            device_address=self.responder_addr,
101            destination_addresses=[self.initiator_addr],
102        )
103        self.initiator.start_uwb_ranging(initiator_params)
104        self.responder.start_uwb_ranging(responder_params)
105
106        time.sleep(20)
107        self.initiator.stop_uwb_ranging(initiator_params)
108        self.responder.stop_uwb_ranging(responder_params)
109
110
111if __name__ == "__main__":
112    if "--" in sys.argv:
113        index = sys.argv.index("--")
114        sys.argv = sys.argv[:1] + sys.argv[index + 1:]
115    suite_runner.run_suite([RangingTest])
116