xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-016-child-mode-change.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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# Verify device mode change on children.
38*cfb92d14SAndroid Build Coastguard Worker#
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
41*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
42*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
43*cfb92d14SAndroid Build Coastguard Worker
44*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
45*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Node` instances
46*cfb92d14SAndroid Build Coastguard Worker
47*cfb92d14SAndroid Build Coastguard Workerspeedup = 10
48*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup)
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Workerparent = cli.Node()
51*cfb92d14SAndroid Build Coastguard Workerchild1 = cli.Node()
52*cfb92d14SAndroid Build Coastguard Workerchild2 = cli.Node()
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
55*cfb92d14SAndroid Build Coastguard Worker# Form topology
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Workerparent.form('modechange')
58*cfb92d14SAndroid Build Coastguard Workerchild1.join(parent, cli.JOIN_TYPE_END_DEVICE)
59*cfb92d14SAndroid Build Coastguard Workerchild2.join(parent, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Workerverify(parent.get_state() == 'leader')
62*cfb92d14SAndroid Build Coastguard Workerverify(child1.get_state() == 'child')
63*cfb92d14SAndroid Build Coastguard Workerverify(child2.get_state() == 'child')
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
66*cfb92d14SAndroid Build Coastguard Worker# Test Implementation
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Workerchild1_rloc = int(child1.get_rloc16(), 16)
69*cfb92d14SAndroid Build Coastguard Workerchild2_rloc = int(child2.get_rloc16(), 16)
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72*cfb92d14SAndroid Build Coastguard Worker# Check the mode on children and also on parent child table
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerverify(parent.get_mode() == 'rdn')
75*cfb92d14SAndroid Build Coastguard Workerverify(child1.get_mode() == 'rn')
76*cfb92d14SAndroid Build Coastguard Workerverify(child2.get_mode() == '-')
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Workerchild_table = parent.get_child_table()
79*cfb92d14SAndroid Build Coastguard Workerverify(len(child_table) == 2)
80*cfb92d14SAndroid Build Coastguard Workerfor entry in child_table:
81*cfb92d14SAndroid Build Coastguard Worker    if int(entry['RLOC16'], 16) == child1_rloc:
82*cfb92d14SAndroid Build Coastguard Worker        verify(entry['R'] == '1')
83*cfb92d14SAndroid Build Coastguard Worker        verify(entry['D'] == '0')
84*cfb92d14SAndroid Build Coastguard Worker        verify(entry['N'] == '1')
85*cfb92d14SAndroid Build Coastguard Worker    elif int(entry['RLOC16'], 16) == child2_rloc:
86*cfb92d14SAndroid Build Coastguard Worker        verify(entry['R'] == '0')
87*cfb92d14SAndroid Build Coastguard Worker        verify(entry['D'] == '0')
88*cfb92d14SAndroid Build Coastguard Worker        verify(entry['N'] == '0')
89*cfb92d14SAndroid Build Coastguard Worker    else:
90*cfb92d14SAndroid Build Coastguard Worker        verify(False)
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93*cfb92d14SAndroid Build Coastguard Worker# Change the network data flag on child 2 (sleepy) and verify that it
94*cfb92d14SAndroid Build Coastguard Worker# gets changed on parent's child table.
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Workerchild2.set_mode('n')
97*cfb92d14SAndroid Build Coastguard Worker
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Workerdef check_child2_n_flag_change():
100*cfb92d14SAndroid Build Coastguard Worker    verify(child2.get_mode() == 'n')
101*cfb92d14SAndroid Build Coastguard Worker    child_table = parent.get_child_table()
102*cfb92d14SAndroid Build Coastguard Worker    verify(len(child_table) == 2)
103*cfb92d14SAndroid Build Coastguard Worker    for entry in child_table:
104*cfb92d14SAndroid Build Coastguard Worker        if int(entry['RLOC16'], 16) == child1_rloc:
105*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '1')
106*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
107*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
108*cfb92d14SAndroid Build Coastguard Worker        elif int(entry['RLOC16'], 16) == child2_rloc:
109*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '0')
110*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
111*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
112*cfb92d14SAndroid Build Coastguard Worker        else:
113*cfb92d14SAndroid Build Coastguard Worker            verify(False)
114*cfb92d14SAndroid Build Coastguard Worker
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Workerverify_within(check_child2_n_flag_change, 5)
117*cfb92d14SAndroid Build Coastguard Worker
118*cfb92d14SAndroid Build Coastguard Worker# Verify that mode change did not cause child2 to detach and re-attach
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Workerverify(int(cli.Node.parse_list(child2.get_mle_counter())['Role Detached']) == 1)
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123*cfb92d14SAndroid Build Coastguard Worker# Change child1 from rx-on to sleepy and verify the change on
124*cfb92d14SAndroid Build Coastguard Worker# parent's child table. This mode change should require child
125*cfb92d14SAndroid Build Coastguard Worker# to detach and attach again.
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Workerchild1.set_mode('n')
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker
130*cfb92d14SAndroid Build Coastguard Workerdef check_child1_become_sleepy():
131*cfb92d14SAndroid Build Coastguard Worker    verify(child1.get_mode() == 'n')
132*cfb92d14SAndroid Build Coastguard Worker    child_table = parent.get_child_table()
133*cfb92d14SAndroid Build Coastguard Worker    verify(len(child_table) == 2)
134*cfb92d14SAndroid Build Coastguard Worker    for entry in child_table:
135*cfb92d14SAndroid Build Coastguard Worker        if int(entry['RLOC16'], 16) == child1_rloc:
136*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '0')
137*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
138*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
139*cfb92d14SAndroid Build Coastguard Worker        elif int(entry['RLOC16'], 16) == child2_rloc:
140*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '0')
141*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
142*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
143*cfb92d14SAndroid Build Coastguard Worker        else:
144*cfb92d14SAndroid Build Coastguard Worker            verify(False)
145*cfb92d14SAndroid Build Coastguard Worker
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Workerverify_within(check_child1_become_sleepy, 5)
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Workerverify(int(cli.Node.parse_list(child1.get_mle_counter())['Role Detached']) == 2)
150*cfb92d14SAndroid Build Coastguard Worker
151*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152*cfb92d14SAndroid Build Coastguard Worker# Change child2 from sleepy to rx-on and verify the change on
153*cfb92d14SAndroid Build Coastguard Worker# parent's child table. Verify that child2 did not detach and
154*cfb92d14SAndroid Build Coastguard Worker# used MLE "Child Update" exchange.
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Workerchild2.set_mode('rn')
157*cfb92d14SAndroid Build Coastguard Worker
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Workerdef check_child2_become_rx_on():
160*cfb92d14SAndroid Build Coastguard Worker    verify(child2.get_mode() == 'rn')
161*cfb92d14SAndroid Build Coastguard Worker    child_table = parent.get_child_table()
162*cfb92d14SAndroid Build Coastguard Worker    verify(len(child_table) == 2)
163*cfb92d14SAndroid Build Coastguard Worker    for entry in child_table:
164*cfb92d14SAndroid Build Coastguard Worker        if int(entry['RLOC16'], 16) == child1_rloc:
165*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '0')
166*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
167*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
168*cfb92d14SAndroid Build Coastguard Worker        elif int(entry['RLOC16'], 16) == child2_rloc:
169*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '1')
170*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
171*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
172*cfb92d14SAndroid Build Coastguard Worker        else:
173*cfb92d14SAndroid Build Coastguard Worker            verify(False)
174*cfb92d14SAndroid Build Coastguard Worker
175*cfb92d14SAndroid Build Coastguard Worker
176*cfb92d14SAndroid Build Coastguard Workerverify_within(check_child2_become_rx_on, 5)
177*cfb92d14SAndroid Build Coastguard Worker
178*cfb92d14SAndroid Build Coastguard Workerverify(int(cli.Node.parse_list(child2.get_mle_counter())['Role Detached']) == 1)
179*cfb92d14SAndroid Build Coastguard Worker
180*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
181*cfb92d14SAndroid Build Coastguard Worker# Now change child2 to become sleepy again. Since it was originally
182*cfb92d14SAndroid Build Coastguard Worker# attached as sleepy it should not detach and attach again and
183*cfb92d14SAndroid Build Coastguard Worker# can do this using MlE "Child Update" exchange with parent.
184*cfb92d14SAndroid Build Coastguard Worker
185*cfb92d14SAndroid Build Coastguard Workerchild2.set_mode('n')
186*cfb92d14SAndroid Build Coastguard Worker
187*cfb92d14SAndroid Build Coastguard Worker
188*cfb92d14SAndroid Build Coastguard Workerdef check_child2_become_sleepy_again():
189*cfb92d14SAndroid Build Coastguard Worker    verify(child2.get_mode() == 'n')
190*cfb92d14SAndroid Build Coastguard Worker    child_table = parent.get_child_table()
191*cfb92d14SAndroid Build Coastguard Worker    verify(len(child_table) == 2)
192*cfb92d14SAndroid Build Coastguard Worker    for entry in child_table:
193*cfb92d14SAndroid Build Coastguard Worker        if int(entry['RLOC16'], 16) == child1_rloc:
194*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '0')
195*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
196*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
197*cfb92d14SAndroid Build Coastguard Worker        elif int(entry['RLOC16'], 16) == child2_rloc:
198*cfb92d14SAndroid Build Coastguard Worker            verify(entry['R'] == '0')
199*cfb92d14SAndroid Build Coastguard Worker            verify(entry['D'] == '0')
200*cfb92d14SAndroid Build Coastguard Worker            verify(entry['N'] == '1')
201*cfb92d14SAndroid Build Coastguard Worker        else:
202*cfb92d14SAndroid Build Coastguard Worker            verify(False)
203*cfb92d14SAndroid Build Coastguard Worker
204*cfb92d14SAndroid Build Coastguard Worker
205*cfb92d14SAndroid Build Coastguard Workerverify_within(check_child2_become_sleepy_again, 5)
206*cfb92d14SAndroid Build Coastguard Worker
207*cfb92d14SAndroid Build Coastguard Workerverify(int(cli.Node.parse_list(child2.get_mle_counter())['Role Detached']) == 1)
208*cfb92d14SAndroid Build Coastguard Worker
209*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
210*cfb92d14SAndroid Build Coastguard Worker# Test finished
211*cfb92d14SAndroid Build Coastguard Worker
212*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes()
213*cfb92d14SAndroid Build Coastguard Worker
214*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
215