xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-005-discover-scan.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2018, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Workerfrom wpan import verify
30*cfb92d14SAndroid Build Coastguard Workerimport wpan
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
33*cfb92d14SAndroid Build Coastguard Worker# Test description: discover scan
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
36*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
37*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
38*cfb92d14SAndroid Build Coastguard Worker
39*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
40*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Workerspeedup = 2
43*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Workernode1 = wpan.Node()
46*cfb92d14SAndroid Build Coastguard Workernode2 = wpan.Node()
47*cfb92d14SAndroid Build Coastguard Workernode3 = wpan.Node()
48*cfb92d14SAndroid Build Coastguard Workernode4 = wpan.Node()
49*cfb92d14SAndroid Build Coastguard Workernode5 = wpan.Node()
50*cfb92d14SAndroid Build Coastguard Worker
51*cfb92d14SAndroid Build Coastguard Workerscanner = wpan.Node()
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
54*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
55*cfb92d14SAndroid Build Coastguard Worker
56*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
59*cfb92d14SAndroid Build Coastguard Worker# Build network topology
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Workernode1.form("net1", channel='11', panid='0x0001')
62*cfb92d14SAndroid Build Coastguard Workernode2.form("net2", channel='12', panid='0x0002')
63*cfb92d14SAndroid Build Coastguard Workernode3.form("net3", channel='13', panid='0x0002')
64*cfb92d14SAndroid Build Coastguard Workernode4.form("net4", channel='13', panid='0x0004')
65*cfb92d14SAndroid Build Coastguard Workernode5.form("net5", channel='14', panid='0x0005')
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
68*cfb92d14SAndroid Build Coastguard Worker# Test implementation
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Worker# Perform active scan and check that all nodes are seen in the scan result.
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Workerscan_result = wpan.parse_scan_result(scanner.discover_scan())
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerverify(len(scan_result) == 5)
75*cfb92d14SAndroid Build Coastguard Workerfor node in [node1, node2, node3, node4, node5]:
76*cfb92d14SAndroid Build Coastguard Worker    verify(node.is_in_scan_result(scan_result))
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Worker# Scan from an already associated node.
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Workerscan_result = wpan.parse_scan_result(node1.discover_scan())
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Workerverify(len(scan_result) == 4)
83*cfb92d14SAndroid Build Coastguard Workerfor node in [node2, node3, node4, node5]:
84*cfb92d14SAndroid Build Coastguard Worker    verify(node.is_in_scan_result(scan_result))
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Worker# Scan on specific subset of channels
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Workerscan_result = wpan.parse_scan_result(scanner.discover_scan(channel="11-13"))
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Workerverify(len(scan_result) == 4)
91*cfb92d14SAndroid Build Coastguard Workerfor node in [node1, node2, node3, node4]:
92*cfb92d14SAndroid Build Coastguard Worker    verify(node.is_in_scan_result(scan_result))
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker# Filter on specific PAN ID.
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Workerscan_result = wpan.parse_scan_result(scanner.discover_scan(panid_filter="0x0002"))
97*cfb92d14SAndroid Build Coastguard Worker
98*cfb92d14SAndroid Build Coastguard Workerverify(len(scan_result) == 2)
99*cfb92d14SAndroid Build Coastguard Workerfor node in [node2, node3]:
100*cfb92d14SAndroid Build Coastguard Worker    verify(node.is_in_scan_result(scan_result))
101*cfb92d14SAndroid Build Coastguard Worker
102*cfb92d14SAndroid Build Coastguard Worker# Scan joinable only.
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Workerscanner_hw_addr = scanner.get(wpan.WPAN_HW_ADDRESS)[1:-1]  # Remove the `[]`
105*cfb92d14SAndroid Build Coastguard Worker
106*cfb92d14SAndroid Build Coastguard Workernode1.commissioner_start()
107*cfb92d14SAndroid Build Coastguard Workernode1.commissioner_add_joiner(scanner_hw_addr, '123456')
108*cfb92d14SAndroid Build Coastguard Workernode2.commissioner_start()
109*cfb92d14SAndroid Build Coastguard Workernode2.commissioner_add_joiner('1122334455667788', '123456')
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Workerscan_result = wpan.parse_scan_result(scanner.discover_scan(joiner_only=True))
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Workerverify(len(scan_result) == 2)
114*cfb92d14SAndroid Build Coastguard Workerfor node in [node1, node2]:
115*cfb92d14SAndroid Build Coastguard Worker    verify(node.is_in_scan_result(scan_result))
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Worker# Scan with filter enabled
118*cfb92d14SAndroid Build Coastguard Worker
119*cfb92d14SAndroid Build Coastguard Workerscan_result = wpan.parse_scan_result(scanner.discover_scan(enable_filtering=True))
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Workerverify(len(scan_result) == 1)
122*cfb92d14SAndroid Build Coastguard Workerverify(node1.is_in_scan_result(scan_result))
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
125*cfb92d14SAndroid Build Coastguard Worker# Test finished
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
130