1#!/usr/bin/env python3
2#
3#  Copyright (c) 2018, 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: discover scan
34
35test_name = __file__[:-3] if __file__.endswith('.py') else __file__
36print('-' * 120)
37print('Starting \'{}\''.format(test_name))
38
39# -----------------------------------------------------------------------------------------------------------------------
40# Creating `wpan.Nodes` instances
41
42speedup = 2
43wpan.Node.set_time_speedup_factor(speedup)
44
45node1 = wpan.Node()
46node2 = wpan.Node()
47node3 = wpan.Node()
48node4 = wpan.Node()
49node5 = wpan.Node()
50
51scanner = wpan.Node()
52
53# -----------------------------------------------------------------------------------------------------------------------
54# Init all nodes
55
56wpan.Node.init_all_nodes()
57
58# -----------------------------------------------------------------------------------------------------------------------
59# Build network topology
60
61node1.form("net1", channel='11', panid='0x0001')
62node2.form("net2", channel='12', panid='0x0002')
63node3.form("net3", channel='13', panid='0x0002')
64node4.form("net4", channel='13', panid='0x0004')
65node5.form("net5", channel='14', panid='0x0005')
66
67# -----------------------------------------------------------------------------------------------------------------------
68# Test implementation
69
70# Perform active scan and check that all nodes are seen in the scan result.
71
72scan_result = wpan.parse_scan_result(scanner.discover_scan())
73
74verify(len(scan_result) == 5)
75for node in [node1, node2, node3, node4, node5]:
76    verify(node.is_in_scan_result(scan_result))
77
78# Scan from an already associated node.
79
80scan_result = wpan.parse_scan_result(node1.discover_scan())
81
82verify(len(scan_result) == 4)
83for node in [node2, node3, node4, node5]:
84    verify(node.is_in_scan_result(scan_result))
85
86# Scan on specific subset of channels
87
88scan_result = wpan.parse_scan_result(scanner.discover_scan(channel="11-13"))
89
90verify(len(scan_result) == 4)
91for node in [node1, node2, node3, node4]:
92    verify(node.is_in_scan_result(scan_result))
93
94# Filter on specific PAN ID.
95
96scan_result = wpan.parse_scan_result(scanner.discover_scan(panid_filter="0x0002"))
97
98verify(len(scan_result) == 2)
99for node in [node2, node3]:
100    verify(node.is_in_scan_result(scan_result))
101
102# Scan joinable only.
103
104scanner_hw_addr = scanner.get(wpan.WPAN_HW_ADDRESS)[1:-1]  # Remove the `[]`
105
106node1.commissioner_start()
107node1.commissioner_add_joiner(scanner_hw_addr, '123456')
108node2.commissioner_start()
109node2.commissioner_add_joiner('1122334455667788', '123456')
110
111scan_result = wpan.parse_scan_result(scanner.discover_scan(joiner_only=True))
112
113verify(len(scan_result) == 2)
114for node in [node1, node2]:
115    verify(node.is_in_scan_result(scan_result))
116
117# Scan with filter enabled
118
119scan_result = wpan.parse_scan_result(scanner.discover_scan(enable_filtering=True))
120
121verify(len(scan_result) == 1)
122verify(node1.is_in_scan_result(scan_result))
123
124# -----------------------------------------------------------------------------------------------------------------------
125# Test finished
126
127wpan.Node.finalize_all_nodes()
128
129print('\'{}\' passed.'.format(test_name))
130