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 29import wpan 30from wpan import verify 31 32# ----------------------------------------------------------------------------------------------------------------------- 33# Test description: Verify Thread mode change on children and recovery after parent reset. 34# 35 36test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 37print('-' * 120) 38print('Starting \'{}\''.format(test_name)) 39 40# ----------------------------------------------------------------------------------------------------------------------- 41# Utility functions 42 43 44def verify_child_table(parent, children): 45 """ 46 This function verifies that child table on `parent` node contains all the entries in `children` list and the child 47 table entry's mode value matches the children Thread mode. 48 """ 49 child_table = wpan.parse_child_table_result(parent.get(wpan.WPAN_THREAD_CHILD_TABLE)) 50 verify(len(child_table) == len(children)) 51 for child in children: 52 ext_addr = child.get(wpan.WPAN_EXT_ADDRESS)[1:-1] 53 for entry in child_table: 54 if entry.ext_address == ext_addr: 55 break 56 else: 57 raise wpan.VerifyError('Failed to find a child entry for extended address {} in table'.format(ext_addr)) 58 exit(1) 59 60 verify(int(entry.rloc16, 16) == int(child.get(wpan.WPAN_THREAD_RLOC16), 16)) 61 mode = int(child.get(wpan.WPAN_THREAD_DEVICE_MODE), 0) 62 verify(entry.is_rx_on_when_idle() == (mode & wpan.THREAD_MODE_FLAG_RX_ON_WHEN_IDLE != 0)) 63 verify(entry.is_ftd() == (mode & wpan.THREAD_MODE_FLAG_FULL_THREAD_DEV != 0)) 64 verify(entry.is_full_net_data() == (mode & wpan.THREAD_MODE_FLAG_FULL_NETWORK_DATA != 0)) 65 66 67# ----------------------------------------------------------------------------------------------------------------------- 68# Creating `wpan.Nodes` instances 69 70speedup = 4 71wpan.Node.set_time_speedup_factor(speedup) 72 73parent = wpan.Node() 74child1 = wpan.Node() 75child2 = wpan.Node() 76 77# ----------------------------------------------------------------------------------------------------------------------- 78# Init all nodes 79 80wpan.Node.init_all_nodes() 81 82# ----------------------------------------------------------------------------------------------------------------------- 83# Build network topology 84# 85 86parent.form("ModeChangeReset") 87 88child1.join_node(parent, wpan.JOIN_TYPE_END_DEVICE) 89child2.join_node(parent, wpan.JOIN_TYPE_SLEEPY_END_DEVICE) 90 91child1.set(wpan.WPAN_POLL_INTERVAL, '8000') 92child2.set(wpan.WPAN_POLL_INTERVAL, '8000') 93 94children = [child1, child2] 95 96# ----------------------------------------------------------------------------------------------------------------------- 97# Test implementation 98 99WAIT_INTERVAL = 6 100LEADER_RESET_DELAY = 38 101 102# Thread Mode for end-device and sleepy end-device 103DEVICE_MODE_SLEEPY_END_DEVICE = (wpan.THREAD_MODE_FLAG_FULL_NETWORK_DATA) 104DEVICE_MODE_END_DEVICE = (wpan.THREAD_MODE_FLAG_FULL_NETWORK_DATA | wpan.THREAD_MODE_FLAG_FULL_THREAD_DEV | 105 wpan.THREAD_MODE_FLAG_RX_ON_WHEN_IDLE) 106 107# Disable child supervision on all devices 108parent.set(wpan.WPAN_CHILD_SUPERVISION_INTERVAL, '0') 109child1.set(wpan.WPAN_CHILD_SUPERVISION_CHECK_TIMEOUT, '0') 110child2.set(wpan.WPAN_CHILD_SUPERVISION_CHECK_TIMEOUT, '0') 111 112# Verify Thread Device Mode on both children 113verify(int(child1.get(wpan.WPAN_THREAD_DEVICE_MODE), 0) == DEVICE_MODE_END_DEVICE) 114verify(int(child2.get(wpan.WPAN_THREAD_DEVICE_MODE), 0) == DEVICE_MODE_SLEEPY_END_DEVICE) 115 116 117def check_child_table(): 118 verify_child_table(parent, children) 119 120 121wpan.verify_within(check_child_table, WAIT_INTERVAL) 122 123# Prepare a set of IP message tx to both children 124NUM_MSGS = 6 125 126parent_ml_address = parent.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1] 127child2_ml_address = child2.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1] 128 129sender = parent.prepare_tx(parent_ml_address, child2_ml_address, 800, NUM_MSGS) 130 131wpan.Node.perform_async_tx_rx() 132 133verify(sender.was_successful) 134 135# Change mode on both children (make child1 sleepy, and child2 non-sleepy) 136 137child1.set(wpan.WPAN_THREAD_DEVICE_MODE, str(DEVICE_MODE_SLEEPY_END_DEVICE)) 138verify(int(child1.get(wpan.WPAN_THREAD_DEVICE_MODE), 0) == DEVICE_MODE_SLEEPY_END_DEVICE) 139 140child2.set(wpan.WPAN_THREAD_DEVICE_MODE, str(DEVICE_MODE_END_DEVICE)) 141verify(int(child2.get(wpan.WPAN_THREAD_DEVICE_MODE), 0) == DEVICE_MODE_END_DEVICE) 142 143# Verify that the child table on parent is also updated 144wpan.verify_within(check_child_table, WAIT_INTERVAL) 145 146# Reset parent and verify all children are recovered 147parent.reset() 148wpan.verify_within(check_child_table, WAIT_INTERVAL + LEADER_RESET_DELAY) 149 150# ----------------------------------------------------------------------------------------------------------------------- 151# Test finished 152 153wpan.Node.finalize_all_nodes() 154 155print('\'{}\' passed.'.format(test_name)) 156