1#!/usr/bin/env python3
2#
3#  Copyright (c) 2020, 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: This test covers behavior of MeshHeader messages (message sent over multi-hop) when the
35# underlying devices (in the path) support different radio types with different frame MTU length.
36#
37#   r1  --------- r2 ---------- r3
38# (trel)      (15.4+trel)     (15.4)
39#                  |            |
40#                  |            |
41#               c2 (15.4)     c3 (15.4)
42#
43# c3 is used for quick router promotion of r3.
44#
45test_name = __file__[:-3] if __file__.endswith('.py') else __file__
46print('-' * 120)
47print('Starting \'{}\''.format(test_name))
48
49# -----------------------------------------------------------------------------------------------------------------------
50# Creating `wpan.Nodes` instances
51
52speedup = 4
53wpan.Node.set_time_speedup_factor(speedup)
54
55r1 = wpan.Node(wpan.NODE_TREL)
56r2 = wpan.Node(wpan.NODE_15_4_TREL)
57r3 = wpan.Node(wpan.NODE_15_4)
58c2 = wpan.Node(wpan.NODE_15_4)
59c3 = wpan.Node(wpan.NODE_15_4)
60
61# -----------------------------------------------------------------------------------------------------------------------
62# Init all nodes
63
64wpan.Node.init_all_nodes()
65
66# -----------------------------------------------------------------------------------------------------------------------
67# Build network topology
68#
69
70r1.allowlist_node(r2)
71r2.allowlist_node(r1)
72
73r2.allowlist_node(r3)
74r3.allowlist_node(r2)
75
76r2.allowlist_node(c2)
77c2.allowlist_node(r2)
78
79r3.allowlist_node(c3)
80c3.allowlist_node(r3)
81
82r1.form("mesh-header")
83
84r2.join_node(r1, wpan.JOIN_TYPE_ROUTER)
85r3.join_node(r2, wpan.JOIN_TYPE_ROUTER)
86c2.join_node(r2, wpan.NODE_TYPE_SLEEPY_END_DEVICE)
87
88c3.join_node(r3, wpan.JOIN_TYPE_END_DEVICE)
89
90# -----------------------------------------------------------------------------------------------------------------------
91# Test implementation
92
93WAIT_TIME = 5
94INVALID_ROUTER_ID = 63
95MSG_LEN = 1000
96MSG_COUNT = 2
97POLL_INTERVAL = 400
98
99c2.set(wpan.WPAN_POLL_INTERVAL, str(POLL_INTERVAL))
100
101# Verify that each node supports the correct radio links.
102
103r1_radios = wpan.parse_list(r1.get(wpan.WPAN_OT_SUPPORTED_RADIO_LINKS))
104verify(len(r1_radios) == 1 and (wpan.RADIO_LINK_TREL_UDP6 in r1_radios))
105
106r2_radios = wpan.parse_list(r2.get(wpan.WPAN_OT_SUPPORTED_RADIO_LINKS))
107verify(
108    len(r2_radios) == 2 and (wpan.RADIO_LINK_IEEE_802_15_4 in r2_radios) and (wpan.RADIO_LINK_TREL_UDP6 in r2_radios))
109
110r3_radios = wpan.parse_list(r3.get(wpan.WPAN_OT_SUPPORTED_RADIO_LINKS))
111verify(len(r3_radios) == 1 and (wpan.RADIO_LINK_IEEE_802_15_4 in r3_radios))
112
113# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114# Wait for all router entries are updated
115
116r1_rloc = int(r1.get(wpan.WPAN_THREAD_RLOC16), 16)
117
118
119def check_r1_router_table():
120    router_table = wpan.parse_router_table_result(r1.get(wpan.WPAN_THREAD_ROUTER_TABLE))
121    verify(len(router_table) == 3)
122    for entry in router_table:
123        verify(entry.rloc16 == r1_rloc or entry.is_link_established() or entry.next_hop != INVALID_ROUTER_ID)
124
125
126wpan.verify_within(check_r1_router_table, WAIT_TIME)
127
128# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129# Send large message from r1 to r3. Such a message would be sent as a
130# Mesh Header message. Note that the origin r1 is a trel-only device
131# whereas the final destination r3 is a 15.4-only device. The originator
132# should use smaller MTU size (which then fragment the message into
133# multiple frames) otherwise the 15.4 link won't be able to handle it.
134
135r1_ml_address = r1.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
136r3_ml_address = r3.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
137
138sender = r1.prepare_tx(r1_ml_address, r3_ml_address, MSG_LEN, MSG_COUNT)
139recver = r3.prepare_rx(sender)
140
141wpan.Node.perform_async_tx_rx()
142
143verify(sender.was_successful)
144verify(recver.was_successful)
145
146# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147# Send messages from c2 to r1.
148
149c2_ml_address = c2.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
150
151sender = c2.prepare_tx(c2_ml_address, r1_ml_address, MSG_LEN, MSG_COUNT)
152recver = r1.prepare_rx(sender)
153
154wpan.Node.perform_async_tx_rx()
155
156verify(sender.was_successful)
157verify(recver.was_successful)
158
159# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160# Send messages from r3 to r1.
161
162sender = r3.prepare_tx(r3_ml_address, r1_ml_address, MSG_LEN, MSG_COUNT)
163recver = r1.prepare_rx(sender)
164
165wpan.Node.perform_async_tx_rx()
166
167verify(sender.was_successful)
168verify(recver.was_successful)
169
170# -----------------------------------------------------------------------------------------------------------------------
171# Test finished
172
173wpan.Node.finalize_all_nodes()
174
175print('\'{}\' passed.'.format(test_name))
176