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: forming a Thread network 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 = 4 43wpan.Node.set_time_speedup_factor(speedup) 44 45node = wpan.Node() 46 47# ----------------------------------------------------------------------------------------------------------------------- 48# Init all nodes 49 50wpan.Node.init_all_nodes() 51 52# ----------------------------------------------------------------------------------------------------------------------- 53# Test implementation 54 55# default values after reset 56DEFAULT_NAME = '"OpenThread"' 57DEFAULT_PANID = '0xFFFF' 58DEFAULT_XPANID = '0xDEAD00BEEF00CAFE' 59 60verify(node.get(wpan.WPAN_STATE) == wpan.STATE_OFFLINE) 61verify(node.get(wpan.WPAN_NAME) == DEFAULT_NAME) 62verify(node.get(wpan.WPAN_PANID) == DEFAULT_PANID) 63verify(node.get(wpan.WPAN_XPANID) == DEFAULT_XPANID) 64 65# Form a network 66 67node.form('asha') 68verify(node.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED) 69verify(node.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_LEADER) 70verify(node.get(wpan.WPAN_NAME) == '"asha"') 71verify(node.get(wpan.WPAN_PANID) != DEFAULT_PANID) 72verify(node.get(wpan.WPAN_XPANID) != DEFAULT_XPANID) 73 74node.leave() 75verify(node.get(wpan.WPAN_STATE) == wpan.STATE_OFFLINE) 76 77# Form a network on a specific channel. 78 79node.form('ahura', channel=20) 80verify(node.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED) 81verify(node.get(wpan.WPAN_NAME) == '"ahura"') 82verify(node.get(wpan.WPAN_CHANNEL) == '20') 83verify(node.get(wpan.WPAN_PANID) != DEFAULT_PANID) 84verify(node.get(wpan.WPAN_XPANID) != DEFAULT_XPANID) 85 86node.leave() 87verify(node.get(wpan.WPAN_STATE) == wpan.STATE_OFFLINE) 88 89# Form a network with a specific panid, xpanid and key specified separately 90 91node.set(wpan.WPAN_PANID, '0x1977') 92node.set(wpan.WPAN_XPANID, '1020031510006016', binary_data=True) 93node.set(wpan.WPAN_KEY, '0123456789abcdeffecdba9876543210', binary_data=True) 94 95node.form('mazda', channel=12) 96verify(node.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED) 97verify(node.get(wpan.WPAN_NAME) == '"mazda"') 98verify(node.get(wpan.WPAN_CHANNEL) == '12') 99verify(node.get(wpan.WPAN_KEY) == '[0123456789ABCDEFFECDBA9876543210]') 100verify(node.get(wpan.WPAN_PANID) == '0x1977') 101verify(node.get(wpan.WPAN_XPANID) == '0x1020031510006016') 102 103node.leave() 104verify(node.get(wpan.WPAN_STATE) == wpan.STATE_OFFLINE) 105 106# Form a network with all parameters given as part of `form` command itself 107 108node.form( 109 'vahman', 110 channel_mask='15,20-24', 111 panid='0x1977', 112 xpanid='1020031510006016', 113 key='0123456789abcdeffecdba9876543210', 114 key_index='1', 115 mesh_local_prefix='fd00:cafe::', 116) 117 118verify(node.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED) 119verify(node.get(wpan.WPAN_NAME) == '"vahman"') 120channel = int(node.get(wpan.WPAN_CHANNEL), 0) 121verify(channel == 15 or (20 <= channel <= 24)) 122verify(node.get(wpan.WPAN_KEY) == '[0123456789ABCDEFFECDBA9876543210]') 123verify(node.get(wpan.WPAN_KEY_INDEX) == '1') 124verify(node.get(wpan.WPAN_PANID) == '0x1977') 125verify(node.get(wpan.WPAN_XPANID) == '0x1020031510006016') 126verify(node.get(wpan.WPAN_IP6_MESH_LOCAL_PREFIX) == '"fd00:cafe::/64"') 127 128# Verify behavior when commands are issued immediately after a `reset` 129 130node.reset() 131node.leave() 132 133node.reset() 134node.form('net-after-reset') 135 136# ----------------------------------------------------------------------------------------------------------------------- 137# Test finished 138 139wpan.Node.finalize_all_nodes() 140 141print('\'{}\' passed.'.format(test_name)) 142