1#!/usr/bin/env python3 2# 3# Copyright (c) 2019, 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 time 30import wpan 31from wpan import verify 32 33# ----------------------------------------------------------------------------------------------------------------------- 34# Test description: Verify transmission of data polls and poll interval change. 35 36test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 37print('-' * 120) 38print('Starting \'{}\''.format(test_name)) 39 40# ----------------------------------------------------------------------------------------------------------------------- 41# Creating `wpan.Nodes` instances 42 43speedup = 4 44wpan.Node.set_time_speedup_factor(speedup) 45 46parent = wpan.Node() 47child = wpan.Node() 48 49# ----------------------------------------------------------------------------------------------------------------------- 50# Init all nodes 51 52wpan.Node.init_all_nodes() 53 54# ----------------------------------------------------------------------------------------------------------------------- 55# Build network topology 56# 57 58parent.form("poll-interval") 59 60child.join_node(parent, wpan.JOIN_TYPE_SLEEPY_END_DEVICE) 61 62# ----------------------------------------------------------------------------------------------------------------------- 63# Test implementation 64 65# Verify the default poll interval is smaller than child_timeout 66 67child_timeout = int(child.get(wpan.WPAN_THREAD_CHILD_TIMEOUT), 0) * 1000 68default_poll_interval = int(child.get(wpan.WPAN_POLL_INTERVAL), 0) 69verify(0 < default_poll_interval <= child_timeout) 70 71WAIT_TIME = 0.36 # in seconds 72 73# Check number of data polls with different poll intervals 74 75for poll_interval in [100, 200, 500, 50]: # in milliseconds 76 77 poll_count_before = int(child.get(wpan.WPAN_NCP_COUNTER_TX_PKT_DATA_POLL), 0) 78 79 child.set(wpan.WPAN_POLL_INTERVAL, str(poll_interval)) 80 verify(int(child.get(wpan.WPAN_POLL_INTERVAL), 0) == poll_interval) 81 82 time.sleep(WAIT_TIME) 83 poll_count_after = int(child.get(wpan.WPAN_NCP_COUNTER_TX_PKT_DATA_POLL), 0) 84 actual_polls = poll_count_after - poll_count_before 85 86 expected_polls = WAIT_TIME * 1000 * speedup / poll_interval 87 88 print("poll interval {} ms, polls -> actual {}, expected {}".format(poll_interval, actual_polls, expected_polls)) 89 90 verify(actual_polls >= int(expected_polls)) 91 92# Verify behavior when poll interval is switched from long to short. 93# 94# - Poll interval set to 5 seconds 95# - Wait for 200 ms 96# - Change poll interval to 200 ms 97# - This should immediately trigger a poll tx since 100 ms is 98# already passed since last poll transmission. 99 100child.set(wpan.WPAN_POLL_INTERVAL, '5000') 101WIAT_TIME = 0.2 # in seconds 102time.sleep(WAIT_TIME / speedup) 103poll_count_before = int(child.get(wpan.WPAN_NCP_COUNTER_TX_PKT_DATA_POLL), 0) 104child.set(wpan.WPAN_POLL_INTERVAL, str(WAIT_TIME * 1000)) 105time.sleep(0.01) 106poll_count_after = int(child.get(wpan.WPAN_NCP_COUNTER_TX_PKT_DATA_POLL), 0) 107verify(poll_count_after > poll_count_before) 108 109# Verify behavior when poll interval is set to zero. 110# Poll interval should use default interval again (based on child timeout). 111 112child.set(wpan.WPAN_POLL_INTERVAL, '0') 113verify(int(child.get(wpan.WPAN_POLL_INTERVAL), 0) == default_poll_interval) 114 115# Change "child timeout" and verify that default interval does change 116 117child.set(wpan.WPAN_THREAD_CHILD_TIMEOUT, str(child_timeout / 1000 * 2)) 118new_default_interval = int(child.get(wpan.WPAN_POLL_INTERVAL), 0) 119verify(default_poll_interval < new_default_interval <= child_timeout * 2) 120 121child.set(wpan.WPAN_THREAD_CHILD_TIMEOUT, str(child_timeout / 1000)) 122verify(int(child.get(wpan.WPAN_POLL_INTERVAL), 0) == default_poll_interval) 123 124# ----------------------------------------------------------------------------------------------------------------------- 125# Test finished 126 127wpan.Node.finalize_all_nodes() 128 129print('\'{}\' passed.'.format(test_name)) 130