1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*cfb92d14SAndroid Build Coastguard Worker# 3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2022, The OpenThread Authors. 4*cfb92d14SAndroid Build Coastguard Worker# All rights reserved. 5*cfb92d14SAndroid Build Coastguard Worker# 6*cfb92d14SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without 7*cfb92d14SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met: 8*cfb92d14SAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright 9*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer. 10*cfb92d14SAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright 11*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer in the 12*cfb92d14SAndroid Build Coastguard Worker# documentation and/or other materials provided with the distribution. 13*cfb92d14SAndroid Build Coastguard Worker# 3. Neither the name of the copyright holder nor the 14*cfb92d14SAndroid Build Coastguard Worker# names of its contributors may be used to endorse or promote products 15*cfb92d14SAndroid Build Coastguard Worker# derived from this software without specific prior written permission. 16*cfb92d14SAndroid Build Coastguard Worker# 17*cfb92d14SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18*cfb92d14SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*cfb92d14SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*cfb92d14SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21*cfb92d14SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*cfb92d14SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*cfb92d14SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*cfb92d14SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*cfb92d14SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*cfb92d14SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*cfb92d14SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE. 28*cfb92d14SAndroid Build Coastguard Worker 29*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify 30*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify_within 31*cfb92d14SAndroid Build Coastguard Workerimport cli 32*cfb92d14SAndroid Build Coastguard Workerimport time 33*cfb92d14SAndroid Build Coastguard Worker 34*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 35*cfb92d14SAndroid Build Coastguard Worker# Test description: 36*cfb92d14SAndroid Build Coastguard Worker# 37*cfb92d14SAndroid Build Coastguard Worker# Verifies `ChannelManager` channel selection procedure 38*cfb92d14SAndroid Build Coastguard Worker 39*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 40*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 41*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 42*cfb92d14SAndroid Build Coastguard Worker 43*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 44*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Node` instances 45*cfb92d14SAndroid Build Coastguard Worker 46*cfb92d14SAndroid Build Coastguard Worker# Run the test with 10,000 time speedup factor 47*cfb92d14SAndroid Build Coastguard Workerspeedup = 10000 48*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup) 49*cfb92d14SAndroid Build Coastguard Worker 50*cfb92d14SAndroid Build Coastguard Workernode = cli.Node() 51*cfb92d14SAndroid Build Coastguard Worker 52*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 53*cfb92d14SAndroid Build Coastguard Worker# Form topology 54*cfb92d14SAndroid Build Coastguard Worker 55*cfb92d14SAndroid Build Coastguard Workernode.form('chan-sel', channel=24) 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard Workerverify(node.get_state() == 'leader') 58*cfb92d14SAndroid Build Coastguard Worker 59*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 60*cfb92d14SAndroid Build Coastguard Worker# Test Implementation 61*cfb92d14SAndroid Build Coastguard Worker 62*cfb92d14SAndroid Build Coastguard Workerchannel = 24 63*cfb92d14SAndroid Build Coastguard Worker 64*cfb92d14SAndroid Build Coastguard Worker 65*cfb92d14SAndroid Build Coastguard Workerdef check_channel(): 66*cfb92d14SAndroid Build Coastguard Worker verify(int(node.get_channel()) == channel) 67*cfb92d14SAndroid Build Coastguard Worker 68*cfb92d14SAndroid Build Coastguard Worker 69*cfb92d14SAndroid Build Coastguard Workerdelay = int(node.cli('channel manager delay')[0]) 70*cfb92d14SAndroid Build Coastguard Worker# add kRequestStartJitterInterval=10000ms to expected channel manager delay 71*cfb92d14SAndroid Build Coastguard Workerdelay += 10 / speedup 72*cfb92d14SAndroid Build Coastguard Worker 73*cfb92d14SAndroid Build Coastguard Workercheck_channel() 74*cfb92d14SAndroid Build Coastguard Worker 75*cfb92d14SAndroid Build Coastguard Workerall_channels_mask = int('0x7fff800', 0) 76*cfb92d14SAndroid Build Coastguard Workerchan_12_to_15_mask = int('0x000f000', 0) 77*cfb92d14SAndroid Build Coastguard Workerchan_15_to_17_mask = int('0x0038000', 0) 78*cfb92d14SAndroid Build Coastguard Worker 79*cfb92d14SAndroid Build Coastguard Worker# Set supported channel mask to be all channels 80*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager supported', all_channels_mask) 81*cfb92d14SAndroid Build Coastguard Worker 82*cfb92d14SAndroid Build Coastguard Worker# Sleep for 4.5 second with speedup factor of 10,000 this is more than 12 83*cfb92d14SAndroid Build Coastguard Worker# hours. We sleep instead of immediately checking the sample counter in 84*cfb92d14SAndroid Build Coastguard Worker# order to not add more actions/events into simulation (specially since 85*cfb92d14SAndroid Build Coastguard Worker# we are running at very high speedup). 86*cfb92d14SAndroid Build Coastguard Workertime.sleep(4.5) 87*cfb92d14SAndroid Build Coastguard Worker 88*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel monitor')[:5]) 89*cfb92d14SAndroid Build Coastguard Workerverify(result['enabled'] == '1') 90*cfb92d14SAndroid Build Coastguard Workerverify(int(result['count']) > 970) 91*cfb92d14SAndroid Build Coastguard Worker 92*cfb92d14SAndroid Build Coastguard Worker# Issue a channel-select with quality check enabled, and verify that no 93*cfb92d14SAndroid Build Coastguard Worker# action is taken. 94*cfb92d14SAndroid Build Coastguard Worker 95*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager select 0') 96*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 97*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == '0') 98*cfb92d14SAndroid Build Coastguard Worker 99*cfb92d14SAndroid Build Coastguard Worker# Issue a channel-select with quality check disabled, verify that channel 100*cfb92d14SAndroid Build Coastguard Worker# is switched to channel 11. 101*cfb92d14SAndroid Build Coastguard Worker 102*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager select 1') 103*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 104*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == '11') 105*cfb92d14SAndroid Build Coastguard Workerchannel = 11 106*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 107*cfb92d14SAndroid Build Coastguard Worker 108*cfb92d14SAndroid Build Coastguard Worker# Set channels 12-15 as favorable and request a channel select, verify 109*cfb92d14SAndroid Build Coastguard Worker# that channel is switched to 12. 110*cfb92d14SAndroid Build Coastguard Worker# 111*cfb92d14SAndroid Build Coastguard Worker# Even though 11 would be best, quality difference between 11 and 12 112*cfb92d14SAndroid Build Coastguard Worker# is not high enough for selection algorithm to pick an unfavored 113*cfb92d14SAndroid Build Coastguard Worker# channel. 114*cfb92d14SAndroid Build Coastguard Worker 115*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager favored', chan_12_to_15_mask) 116*cfb92d14SAndroid Build Coastguard Worker 117*cfb92d14SAndroid Build Coastguard Workerchannel = 25 118*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager change', channel) 119*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager select 1') 122*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 123*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == '12') 124*cfb92d14SAndroid Build Coastguard Workerchannel = 12 125*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 126*cfb92d14SAndroid Build Coastguard Worker 127*cfb92d14SAndroid Build Coastguard Worker# Set channels 15-17 as favorables and request a channel select, 128*cfb92d14SAndroid Build Coastguard Worker# verify that channel is switched to 11. 129*cfb92d14SAndroid Build Coastguard Worker# 130*cfb92d14SAndroid Build Coastguard Worker# This time the quality difference between 11 and 15 should be high 131*cfb92d14SAndroid Build Coastguard Worker# enough for selection algorithm to pick the best though unfavored 132*cfb92d14SAndroid Build Coastguard Worker# channel (i.e., channel 11). 133*cfb92d14SAndroid Build Coastguard Worker 134*cfb92d14SAndroid Build Coastguard Workerchannel = 25 135*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager change', channel) 136*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 137*cfb92d14SAndroid Build Coastguard Worker 138*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager favored', chan_15_to_17_mask) 139*cfb92d14SAndroid Build Coastguard Worker 140*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager select 1') 141*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 142*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == '11') 143*cfb92d14SAndroid Build Coastguard Workerchannel = 11 144*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Worker# Set channels 12-15 as favorable and request a channel select, verify 147*cfb92d14SAndroid Build Coastguard Worker# that channel is not switched. 148*cfb92d14SAndroid Build Coastguard Worker 149*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager favored', chan_12_to_15_mask) 150*cfb92d14SAndroid Build Coastguard Worker 151*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager select 1') 152*cfb92d14SAndroid Build Coastguard Worker 153*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 154*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == '11') 155*cfb92d14SAndroid Build Coastguard Workerchannel = 11 156*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 157*cfb92d14SAndroid Build Coastguard Worker 158*cfb92d14SAndroid Build Coastguard Worker# Starting from channel 12 and issuing a channel select (which would 159*cfb92d14SAndroid Build Coastguard Worker# pick 11 as best channel). However, since quality difference between 160*cfb92d14SAndroid Build Coastguard Worker# current channel 12 and new best channel 11 is not large enough, no 161*cfb92d14SAndroid Build Coastguard Worker# action should be taken. 162*cfb92d14SAndroid Build Coastguard Worker 163*cfb92d14SAndroid Build Coastguard Workerchannel = 12 164*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager change', channel) 165*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 166*cfb92d14SAndroid Build Coastguard Worker 167*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager favored', all_channels_mask) 168*cfb92d14SAndroid Build Coastguard Worker 169*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager select 1') 170*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 171*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == '12') 172*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 173*cfb92d14SAndroid Build Coastguard Worker 174*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 175*cfb92d14SAndroid Build Coastguard Worker# Auto Select 176*cfb92d14SAndroid Build Coastguard Worker 177*cfb92d14SAndroid Build Coastguard Worker# Set channel manager cca failure rate threshold to 0 178*cfb92d14SAndroid Build Coastguard Worker# as we cannot control cca success in simulation 179*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager threshold 0') 180*cfb92d14SAndroid Build Coastguard Worker 181*cfb92d14SAndroid Build Coastguard Worker# Set short channel selection interval to speedup 182*cfb92d14SAndroid Build Coastguard Workerinterval = 30 183*cfb92d14SAndroid Build Coastguard Workernode.cli(f'channel manager interval {interval}') 184*cfb92d14SAndroid Build Coastguard Worker 185*cfb92d14SAndroid Build Coastguard Worker# Set channels 15-17 as favorable and request a channel select, verify 186*cfb92d14SAndroid Build Coastguard Worker# that channel is switched to 11. 187*cfb92d14SAndroid Build Coastguard Worker 188*cfb92d14SAndroid Build Coastguard Workerchannel = 25 189*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager change', channel) 190*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 191*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager favored', chan_15_to_17_mask) 192*cfb92d14SAndroid Build Coastguard Worker 193*cfb92d14SAndroid Build Coastguard Worker# Active auto channel selection 194*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager auto 1') 195*cfb92d14SAndroid Build Coastguard Worker 196*cfb92d14SAndroid Build Coastguard Workerchannel = 11 197*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 198*cfb92d14SAndroid Build Coastguard Workerverify(result['auto'] == '1') 199*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == str(channel)) 200*cfb92d14SAndroid Build Coastguard Worker 201*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 202*cfb92d14SAndroid Build Coastguard Worker 203*cfb92d14SAndroid Build Coastguard Worker# while channel selection timer is running change to channel 25, 204*cfb92d14SAndroid Build Coastguard Worker# set channels 12-15 as favorable, wait for auto channel selection 205*cfb92d14SAndroid Build Coastguard Worker# and verify that channel is switched to 12. 206*cfb92d14SAndroid Build Coastguard Worker 207*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager favored', chan_12_to_15_mask) 208*cfb92d14SAndroid Build Coastguard Workerchannel = 25 209*cfb92d14SAndroid Build Coastguard Workernode.cli('channel manager change', channel) 210*cfb92d14SAndroid Build Coastguard Worker 211*cfb92d14SAndroid Build Coastguard Worker# wait for timeout of auto selection timer 212*cfb92d14SAndroid Build Coastguard Workertime.sleep(2 * interval / speedup) 213*cfb92d14SAndroid Build Coastguard Worker 214*cfb92d14SAndroid Build Coastguard Workerchannel = 12 215*cfb92d14SAndroid Build Coastguard Workerresult = cli.Node.parse_list(node.cli('channel manager')) 216*cfb92d14SAndroid Build Coastguard Workerverify(result['channel'] == str(channel)) 217*cfb92d14SAndroid Build Coastguard Worker 218*cfb92d14SAndroid Build Coastguard Workerverify_within(check_channel, delay) 219*cfb92d14SAndroid Build Coastguard Worker 220*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 221*cfb92d14SAndroid Build Coastguard Worker# Test finished 222*cfb92d14SAndroid Build Coastguard Worker 223*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes() 224*cfb92d14SAndroid Build Coastguard Worker 225*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 226