1#!/usr/bin/env python3 2# 3# Copyright (c) 2022, 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 cli import verify 30from cli import verify_within 31import cli 32 33# ----------------------------------------------------------------------------------------------------------------------- 34# Test description: MAC scan operation 35 36test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 37print('-' * 120) 38print('Starting \'{}\''.format(test_name)) 39 40 41# ----------------------------------------------------------------------------------------------------------------------- 42def verify_scan_result_conatins_nodes(scan_result, nodes): 43 table = cli.Node.parse_table(scan_result) 44 verify(len(table) >= len(nodes)) 45 for node in nodes: 46 ext_addr = node.get_ext_addr() 47 for entry in table: 48 if entry['MAC Address'] == ext_addr: 49 verify(int(entry['PAN'], 16) == int(node.get_panid(), 16)) 50 verify(int(entry['Ch']) == int(node.get_channel())) 51 break 52 else: 53 verify(False) 54 55 56# ----------------------------------------------------------------------------------------------------------------------- 57# Creating `cli.Nodes` instances 58 59speedup = 10 60cli.Node.set_time_speedup_factor(speedup) 61 62node1 = cli.Node() 63node2 = cli.Node() 64node3 = cli.Node() 65node4 = cli.Node() 66node5 = cli.Node() 67scanner = cli.Node() 68 69nodes = [node1, node2, node3, node4, node5] 70 71# ----------------------------------------------------------------------------------------------------------------------- 72# Test implementation 73 74node1.form('net1', panid=0x1111, channel=12) 75node2.form('net2', panid=0x2222, channel=13) 76node3.form('net3', panid=0x3333, channel=14) 77node4.form('net4', panid=0x4444, channel=15) 78node5.form('net5', panid=0x5555, channel=16) 79 80# MAC scan 81 82# Scan on all channels, should see all nodes 83verify_scan_result_conatins_nodes(scanner.cli('scan'), nodes) 84 85# Scan on channel 12 only, should only see node1 86verify_scan_result_conatins_nodes(scanner.cli('scan 12'), [node1]) 87 88# Scan on channel 20 only, should see no result 89verify_scan_result_conatins_nodes(scanner.cli('scan 20'), []) 90 91# MLE Discover scan 92 93scanner.interface_up() 94 95verify_scan_result_conatins_nodes(scanner.cli('discover'), nodes) 96verify_scan_result_conatins_nodes(scanner.cli('scan 12'), [node1]) 97verify_scan_result_conatins_nodes(scanner.cli('scan 20'), []) 98 99scanner.form('scanner') 100verify_scan_result_conatins_nodes(scanner.cli('discover'), nodes) 101 102# ----------------------------------------------------------------------------------------------------------------------- 103# Test finished 104 105cli.Node.finalize_all_nodes() 106 107print('\'{}\' passed.'.format(test_name)) 108