1#!/usr/bin/env python3
2#
3#  Copyright (c) 2020, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28
29from wpan import verify
30import wpan
31
32# -----------------------------------------------------------------------------------------------------------------------
33# Test description: Test active scan with nodes supporting different radios
34#
35
36test_name = __file__[:-3] if __file__.endswith('.py') else __file__
37print('-' * 120)
38print('Starting \'{}\''.format(test_name))
39
40# -----------------------------------------------------------------------------------------------------------------------
41# Creating `wpan.Nodes` instances
42
43speedup = 1
44wpan.Node.set_time_speedup_factor(speedup)
45
46n1 = wpan.Node(wpan.NODE_15_4)
47n2 = wpan.Node(wpan.NODE_TREL)
48n3 = wpan.Node(wpan.NODE_15_4_TREL)
49s1 = wpan.Node(wpan.NODE_15_4)
50s2 = wpan.Node(wpan.NODE_TREL)
51s3 = wpan.Node(wpan.NODE_15_4_TREL)
52
53# -----------------------------------------------------------------------------------------------------------------------
54# Init all nodes
55
56wpan.Node.init_all_nodes()
57
58# -----------------------------------------------------------------------------------------------------------------------
59# Build network topology
60
61n1.form("n1", channel='20')
62n2.form("n2", channel='21')
63n3.form("n3", channel='22')
64
65# -----------------------------------------------------------------------------------------------------------------------
66# Test implementation
67
68# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
69# Scan by scanner nodes (no network)
70
71# Scan by s1 (15.4 only), expect to see n1(15.4) and n3(15.4+trel)
72result = wpan.parse_scan_result(s1.active_scan())
73verify(n1.is_in_scan_result(result))
74verify(not n2.is_in_scan_result(result))
75verify(n3.is_in_scan_result(result))
76
77# Scan by s2 (trel only), expect to see n2(trel) and n3(15.4+trel)
78result = wpan.parse_scan_result(s2.active_scan())
79verify(not n1.is_in_scan_result(result))
80verify(n2.is_in_scan_result(result))
81verify(n3.is_in_scan_result(result))
82
83# Scan by s3 (trel+15.4), expect to see all nodes
84result = wpan.parse_scan_result(s3.active_scan())
85verify(n1.is_in_scan_result(result))
86verify(n2.is_in_scan_result(result))
87verify(n3.is_in_scan_result(result))
88
89# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90# Scan by the nodes
91
92# Scan by n1 (15.4 only), expect to see only n3(15.4+trel)
93result = wpan.parse_scan_result(n1.active_scan())
94verify(not n1.is_in_scan_result(result))
95verify(not n2.is_in_scan_result(result))
96verify(n3.is_in_scan_result(result))
97
98# Scan by n2 (trel only), expect to see only n3(15.4+trel)
99result = wpan.parse_scan_result(n2.active_scan())
100verify(not n1.is_in_scan_result(result))
101verify(not n2.is_in_scan_result(result))
102verify(n3.is_in_scan_result(result))
103
104# Scan by n3 (15.4+trel), expect to see n1(15.4) and n2(trel)
105result = wpan.parse_scan_result(n3.active_scan())
106verify(n1.is_in_scan_result(result))
107verify(n2.is_in_scan_result(result))
108verify(not n3.is_in_scan_result(result))
109
110# -----------------------------------------------------------------------------------------------------------------------
111# Test finished
112
113wpan.Node.finalize_all_nodes()
114
115print('\'{}\' passed.'.format(test_name))
116