1#!/usr/bin/env python3
2#
3#  Copyright (c) 2018, 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 wpan
30from wpan import verify
31
32# -----------------------------------------------------------------------------------------------------------------------
33# Test description: Traffic between router and end-device (link-local and
34# mesh-local IPv6 addresses)
35
36test_name = __file__[:-3] if __file__.endswith('.py') else __file__
37print('-' * 120)
38print('Starting \'{}\''.format(test_name))
39
40# -----------------------------------------------------------------------------------------------------------------------
41# Creating `wpan.Nodes` instances
42
43node1 = wpan.Node()
44node2 = wpan.Node()
45
46# -----------------------------------------------------------------------------------------------------------------------
47# Init all nodes
48
49wpan.Node.init_all_nodes()
50
51# -----------------------------------------------------------------------------------------------------------------------
52# Build network topology
53
54# Two-node network (node1 leader/router, node2 end-device)
55
56node1.form('test-PAN')
57node2.join_node(node1, node_type=wpan.JOIN_TYPE_END_DEVICE)
58
59verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
60verify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME))
61verify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID))
62verify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID))
63
64# -----------------------------------------------------------------------------------------------------------------------
65# Test implementation
66
67# Get the link local addresses
68ll1 = node1.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1]
69ll2 = node2.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1]
70
71# Get the mesh-local addresses
72ml1 = node1.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
73ml2 = node2.get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
74
75NUM_MSGS = 3
76MSG_LENS = [40, 100, 400, 800, 1000]
77PORT = 1234
78
79# all src and dst configuration (link-local and mesh-local)
80for src, dst in [(ll1, ll2), (ll1, ml2), (ml1, ll2), (ml1, ml2)]:
81
82    for msg_length in MSG_LENS:
83        sender = node1.prepare_tx(src, dst, msg_length, NUM_MSGS)
84        recver = node2.prepare_rx(sender)
85
86        wpan.Node.perform_async_tx_rx()
87
88        verify(sender.was_successful)
89        verify(recver.was_successful)
90
91    # Send and receive at the same time on same port number
92
93    s1 = node1.prepare_tx((src, PORT), (dst, PORT), 'Hi there!', NUM_MSGS)
94    r1 = node2.prepare_rx(s1)
95    s2 = node2.prepare_tx((dst, PORT), (src, PORT), 'Hello back to you!', NUM_MSGS)
96    r2 = node1.prepare_rx(s2)
97
98    wpan.Node.perform_async_tx_rx()
99
100    verify(s1.was_successful and r1.was_successful)
101    verify(s2.was_successful and r2.was_successful)
102
103# -----------------------------------------------------------------------------------------------------------------------
104# Test finished
105
106wpan.Node.finalize_all_nodes()
107
108print('\'{}\' passed.'.format(test_name))
109