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