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#                             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)
58c3 = wpan.Node(wpan.NODE_15_4)
59
60# -----------------------------------------------------------------------------------------------------------------------
61# Init all nodes
62
63wpan.Node.init_all_nodes()
64
65# -----------------------------------------------------------------------------------------------------------------------
66# Build network topology
67#
68
69r1.allowlist_node(r2)
70r2.allowlist_node(r1)
71
72r2.allowlist_node(r3)
73r3.allowlist_node(r2)
74
75r3.allowlist_node(c3)
76c3.allowlist_node(r3)
77
78r1.form("mesh-header")
79
80r2.join_node(r1, wpan.JOIN_TYPE_ROUTER)
81r3.join_node(r2, wpan.JOIN_TYPE_ROUTER)
82c3.join_node(r3, wpan.JOIN_TYPE_END_DEVICE)
83
84# -----------------------------------------------------------------------------------------------------------------------
85# Test implementation
86
87WAIT_TIME = 5
88INVALID_ROUTER_ID = 63
89MSG_LEN = 1000
90MSG_COUNT = 7
91
92# Verify that each node supports the correct radio links.
93
94r1_radios = wpan.parse_list(r1.get(wpan.WPAN_OT_SUPPORTED_RADIO_LINKS))
95verify(len(r1_radios) == 1 and (wpan.RADIO_LINK_TREL_UDP6 in r1_radios))
96
97r2_radios = wpan.parse_list(r2.get(wpan.WPAN_OT_SUPPORTED_RADIO_LINKS))
98verify(
99    len(r2_radios) == 2 and (wpan.RADIO_LINK_IEEE_802_15_4 in r2_radios) and (wpan.RADIO_LINK_TREL_UDP6 in r2_radios))
100
101r3_radios = wpan.parse_list(r3.get(wpan.WPAN_OT_SUPPORTED_RADIO_LINKS))
102verify(len(r3_radios) == 1 and (wpan.RADIO_LINK_IEEE_802_15_4 in r3_radios))
103
104# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105# Wait for all router entries are updated
106
107r1_rloc = int(r1.get(wpan.WPAN_THREAD_RLOC16), 16)
108
109
110def check_r1_router_table():
111    router_table = wpan.parse_router_table_result(r1.get(wpan.WPAN_THREAD_ROUTER_TABLE))
112    verify(len(router_table) == 3)
113    for entry in router_table:
114        verify(entry.rloc16 == r1_rloc or entry.is_link_established() or entry.next_hop != INVALID_ROUTER_ID)
115
116
117wpan.verify_within(check_r1_router_table, WAIT_TIME)
118
119# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120# Send large message from r1 to r3. Such a messag would be sent as a
121# Mesh Header message. Note that the origin r1 is a trel-only device
122# whereas the final destination r3 is a 15.4-only device. The originator
123# should use smaller MTU size (which then fragment the message into
124# multiple frames) otherwise the 15.4 link won't be able to handle it.
125
126r1_ml_address = r1.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
127r3_ml_address = r3.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
128
129sender = r1.prepare_tx(r1_ml_address, r3_ml_address, MSG_LEN, MSG_COUNT)
130recver = r3.prepare_rx(sender)
131
132wpan.Node.perform_async_tx_rx()
133
134verify(sender.was_successful)
135verify(recver.was_successful)
136
137# -----------------------------------------------------------------------------------------------------------------------
138# Test finished
139
140wpan.Node.finalize_all_nodes()
141
142print('\'{}\' passed.'.format(test_name))
143