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.
14
15# Lint as: python3
16
17import logging
18import time
19
20from mobly import asserts
21from mobly import base_test
22from mobly import test_runner
23from mobly.controllers import android_device
24
25class ThreadNetworkMultiDeviceTest(base_test.BaseTestClass):
26    def setup_class(self):
27        self.node_a, self.node_b = self.register_controller(
28            android_device, min_number=2)
29        self.node_a.adb.shell([
30            'ot-ctl', 'factoryreset',
31        ])
32        self.node_b.adb.shell([
33            'ot-ctl', 'factoryreset',
34        ])
35        time.sleep(1)
36
37    def ot_ctl(self, node, cmd, expect_done=True):
38        args = cmd.split(' ')
39        args = ['ot-ctl'] + args
40        stdout = node.adb.shell(args).decode('utf-8')
41        if expect_done:
42            asserts.assert_in('Done', stdout)
43        return stdout
44
45    def test_b_should_be_able_to_discover_a(self):
46        self.ot_ctl(self.node_a, 'dataset init new')
47        self.ot_ctl(self.node_a, 'dataset commit active')
48        self.ot_ctl(self.node_a, 'ifconfig up')
49        self.ot_ctl(self.node_a, 'thread start')
50        self.ot_ctl(self.node_a, 'state leader')
51        stdout = self.ot_ctl(self.node_a, 'extaddr')
52        extaddr = stdout.splitlines()[0]
53        logging.info('node a extaddr: %s', extaddr)
54        asserts.assert_equal(len(extaddr), 16)
55
56        stdout = self.ot_ctl(self.node_b, 'scan')
57        asserts.assert_in(extaddr, stdout)
58        logging.info('discovered node a')
59
60
61if __name__ == '__main__':
62    # Take test args
63    if '--' in sys.argv:
64        index = sys.argv.index('--')
65        sys.argv = sys.argv[:1] + sys.argv[index + 1:]
66
67    test_runner.main()
68