xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-017-parent-reset-child-recovery.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: Parent restoring children after reset
34*cfb92d14SAndroid Build Coastguard Worker#
35*cfb92d14SAndroid Build Coastguard Worker# This test covers the following:
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker# - A parent node with a given number sleepy and rx-on children.
38*cfb92d14SAndroid Build Coastguard Worker# - Parent node is reset
39*cfb92d14SAndroid Build Coastguard Worker# - It is verified that all children are recovered and are present in the parent's child table
40*cfb92d14SAndroid Build Coastguard Worker# - It is also verified that all children are restored on parent through "Child Update" exchange process and none of
41*cfb92d14SAndroid Build Coastguard Worker#   them got detached and needed to attach back.
42*cfb92d14SAndroid Build Coastguard Worker#
43*cfb92d14SAndroid Build Coastguard Worker
44*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
45*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
46*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
47*cfb92d14SAndroid Build Coastguard Worker
48*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
49*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
50*cfb92d14SAndroid Build Coastguard Worker
51*cfb92d14SAndroid Build Coastguard Workerspeedup = 2
52*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard WorkerNUM_SLEEPY_CHILDREN = 3
55*cfb92d14SAndroid Build Coastguard WorkerNUM_RX_ON_CHILDREN = 2
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard WorkerNUM_CHILDREN = NUM_SLEEPY_CHILDREN + NUM_RX_ON_CHILDREN
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Workerparent = wpan.Node()
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Workersleepy_children = []
62*cfb92d14SAndroid Build Coastguard Workerfor num in range(NUM_SLEEPY_CHILDREN):
63*cfb92d14SAndroid Build Coastguard Worker    sleepy_children.append(wpan.Node())
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Workerrx_on_children = []
66*cfb92d14SAndroid Build Coastguard Workerfor num in range(NUM_RX_ON_CHILDREN):
67*cfb92d14SAndroid Build Coastguard Worker    rx_on_children.append(wpan.Node())
68*cfb92d14SAndroid Build Coastguard Worker
69*cfb92d14SAndroid Build Coastguard Workerall_children = sleepy_children + rx_on_children
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
72*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
75*cfb92d14SAndroid Build Coastguard Worker
76*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
77*cfb92d14SAndroid Build Coastguard Worker# Build network topology
78*cfb92d14SAndroid Build Coastguard Worker#
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Workerparent.form("recovery")
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Workerfor child in sleepy_children:
83*cfb92d14SAndroid Build Coastguard Worker    child.join_node(parent, wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
84*cfb92d14SAndroid Build Coastguard Worker    child.set(wpan.WPAN_POLL_INTERVAL, '10000')
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Workerfor child in rx_on_children:
87*cfb92d14SAndroid Build Coastguard Worker    child.join_node(parent, wpan.JOIN_TYPE_END_DEVICE)
88*cfb92d14SAndroid Build Coastguard Worker
89*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
90*cfb92d14SAndroid Build Coastguard Worker# Test implementation
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Worker# The test verifies that all children are recovered after the parent is reset.
93*cfb92d14SAndroid Build Coastguard Worker#
94*cfb92d14SAndroid Build Coastguard Worker# The `wpantund` statistics collector is used to determine if the children are
95*cfb92d14SAndroid Build Coastguard Worker# recovered through "Child Update Request/Response" exchange triggered by parent
96*cfb92d14SAndroid Build Coastguard Worker# after it was reset (child restoration process) or if they got detached and
97*cfb92d14SAndroid Build Coastguard Worker# went through full attach process again. The wpantund stat-collector "stat:ncp"
98*cfb92d14SAndroid Build Coastguard Worker# property keeps track of all NCP state changes on a device:
99*cfb92d14SAndroid Build Coastguard Worker#
100*cfb92d14SAndroid Build Coastguard Worker# Node.wpanctl('get -v stat:ncp'):
101*cfb92d14SAndroid Build Coastguard Worker#    [
102*cfb92d14SAndroid Build Coastguard Worker#       "NCP State History"
103*cfb92d14SAndroid Build Coastguard Worker#       "-------------------------"
104*cfb92d14SAndroid Build Coastguard Worker#       "00:00:03.220 ago -> associated"
105*cfb92d14SAndroid Build Coastguard Worker#       "00:00:03.604 ago -> associating"
106*cfb92d14SAndroid Build Coastguard Worker#       "00:00:06.027 ago -> offline"
107*cfb92d14SAndroid Build Coastguard Worker#    ]
108*cfb92d14SAndroid Build Coastguard Worker#
109*cfb92d14SAndroid Build Coastguard Worker#
110*cfb92d14SAndroid Build Coastguard Worker# In the first case (child being restored through "Child Update" exchange) the
111*cfb92d14SAndroid Build Coastguard Worker# child should never be detached and its "stat:ncp" history should remain the
112*cfb92d14SAndroid Build Coastguard Worker# same. In the second case the child would get detached and we should
113*cfb92d14SAndroid Build Coastguard Worker# observe a wpantund state change "associated:no-parent" before it gets attached
114*cfb92d14SAndroid Build Coastguard Worker# again:
115*cfb92d14SAndroid Build Coastguard Worker#
116*cfb92d14SAndroid Build Coastguard Worker# Node.wpanctl('get -v stat:ncp'):
117*cfb92d14SAndroid Build Coastguard Worker#    [
118*cfb92d14SAndroid Build Coastguard Worker#       "NCP State History"
119*cfb92d14SAndroid Build Coastguard Worker#       "-------------------------"
120*cfb92d14SAndroid Build Coastguard Worker#       "00:00:01.160 ago -> associated"
121*cfb92d14SAndroid Build Coastguard Worker#       "00:00:01.814 ago -> associated:no-parent"
122*cfb92d14SAndroid Build Coastguard Worker#       "00:00:04.911 ago -> associated"
123*cfb92d14SAndroid Build Coastguard Worker#       "00:00:05.298 ago -> associating"
124*cfb92d14SAndroid Build Coastguard Worker#       "00:00:06.475 ago -> offline"
125*cfb92d14SAndroid Build Coastguard Worker#    ]
126*cfb92d14SAndroid Build Coastguard Worker#
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Workerdef check_child_table():
130*cfb92d14SAndroid Build Coastguard Worker    # Checks the child table includes the expected number of children.
131*cfb92d14SAndroid Build Coastguard Worker    child_table = wpan.parse_list(parent.get(wpan.WPAN_THREAD_CHILD_TABLE))
132*cfb92d14SAndroid Build Coastguard Worker    verify(len(child_table) == NUM_CHILDREN)
133*cfb92d14SAndroid Build Coastguard Worker
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Worker# Verify that all children are present in the child table
136*cfb92d14SAndroid Build Coastguard Workercheck_child_table()
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Worker# Remember number of NCP state changes (using "stat:ncp" property) per child
139*cfb92d14SAndroid Build Coastguard Workerchild_num_state_changes = []
140*cfb92d14SAndroid Build Coastguard Workerfor child in all_children:
141*cfb92d14SAndroid Build Coastguard Worker    child_num_state_changes.append(len(wpan.parse_list(child.get("stat:ncp"))))
142*cfb92d14SAndroid Build Coastguard Worker
143*cfb92d14SAndroid Build Coastguard Worker# Reset the parent
144*cfb92d14SAndroid Build Coastguard Workerparent.reset()
145*cfb92d14SAndroid Build Coastguard Worker
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Workerdef check_parent_is_associated():
148*cfb92d14SAndroid Build Coastguard Worker    verify(parent.is_associated())
149*cfb92d14SAndroid Build Coastguard Worker
150*cfb92d14SAndroid Build Coastguard Worker
151*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_parent_is_associated, 40)
152*cfb92d14SAndroid Build Coastguard Worker
153*cfb92d14SAndroid Build Coastguard Worker# Verify that all the children are recovered and present in the parent's
154*cfb92d14SAndroid Build Coastguard Worker# child table again.
155*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_child_table, 15)
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker# Verify that number of state changes on at least one child stays as before
158*cfb92d14SAndroid Build Coastguard Worker# (indicating they did not get detached).
159*cfb92d14SAndroid Build Coastguard Workerfor i in range(len(all_children)):
160*cfb92d14SAndroid Build Coastguard Worker    if child_num_state_changes[i] == len(wpan.parse_list(all_children[i].get("stat:ncp"))):
161*cfb92d14SAndroid Build Coastguard Worker        break
162*cfb92d14SAndroid Build Coastguard Workerelse:
163*cfb92d14SAndroid Build Coastguard Worker    verify(False)
164*cfb92d14SAndroid Build Coastguard Worker
165*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
166*cfb92d14SAndroid Build Coastguard Worker# Test finished
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
171