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: Active scan and permit-join
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
42NUM_NODES = 5
43
44nodes = []
45for i in range(NUM_NODES):
46    nodes.append(wpan.Node())
47
48scanner = wpan.Node()
49
50# -----------------------------------------------------------------------------------------------------------------------
51# Init all nodes
52
53wpan.Node.init_all_nodes()
54
55# -----------------------------------------------------------------------------------------------------------------------
56# Build network topology
57
58for node in nodes:
59    node.form(node.interface_name)
60
61# -----------------------------------------------------------------------------------------------------------------------
62# Test implementation
63
64# Perform active scan and check that all nodes are seen in the scan result.
65
66scan_result = wpan.parse_scan_result(scanner.active_scan())
67
68for node in nodes:
69    verify(node.is_in_scan_result(scan_result))
70
71# Make every other network joinable, scan and check the result.
72
73make_joinable = False
74for node in nodes:
75    make_joinable = not make_joinable
76    if make_joinable:
77        node.permit_join()
78
79scan_result = wpan.parse_scan_result(scanner.active_scan())
80
81for node in nodes:
82    verify(node.is_in_scan_result(scan_result))
83
84# Scan from an already associated node.
85
86scan_result = wpan.parse_scan_result(nodes[0].active_scan())
87
88for node in nodes[1:]:
89    verify(node.is_in_scan_result(scan_result))
90
91# Scan on a specific channel
92
93channel = nodes[0].get(wpan.WPAN_CHANNEL)
94scan_result = wpan.parse_scan_result(scanner.active_scan(channel=channel))
95verify(nodes[0].is_in_scan_result(scan_result))
96
97# -----------------------------------------------------------------------------------------------------------------------
98# Test finished
99
100wpan.Node.finalize_all_nodes()
101
102print('\'{}\' passed.'.format(test_name))
103