1#!/bin/bash
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#
29
30set -e
31set -x
32
33at_exit()
34{
35    EXIT_CODE=$?
36
37    killall expect || true
38    killall ot-cli-ftd || true
39    killall ot-cli || true
40    killall ot-rcp || true
41
42    exit $EXIT_CODE
43}
44
45build()
46{
47    make -f examples/Makefile-simulation
48    make -f src/posix/Makefile-posix
49}
50
51check()
52{
53    trap at_exit INT TERM EXIT
54
55    rm -rf tmp/
56
57    PANID="0xc001"
58    EXT_PANID="0123456789abcdef"
59    NETWORK_NAME="OT_NCP_TO_RCP"
60    CHANNEL="20"
61    NETWORK_KEY="0123456789abcdef0123456789abcdef"
62
63    echo "Step 1. Start NCP platform and form a PAN..."
64    RADIO_NCP_CMD="$PWD/output/simulation/bin/ot-cli-ftd"
65
66    expect <<EOF
67spawn ${RADIO_NCP_CMD} 1
68set timeout 2
69expect_after {
70    timeout { exit 1 }
71}
72send "panid ${PANID}\r\n"
73expect "Done"
74send "extpanid ${EXT_PANID}\r\n"
75expect "Done"
76send "networkname ${NETWORK_NAME}\r\n"
77expect "Done"
78send "channel ${CHANNEL}\r\n"
79expect "Done"
80send "networkkey ${NETWORK_KEY}\r\n"
81expect "Done"
82send "ifconfig up\r\n"
83expect "Done"
84send "thread start\r\n"
85expect "Done"
86sleep 10
87send "state\r\n"
88expect "leader"
89expect "Done"
90send "exit\r\n"
91expect eof
92EOF
93
94    echo "Step 2. Start retrieving dataset from Radio..."
95    RADIO_NCP_PATH="$PWD/output/simulation/bin/ot-ncp-ftd"
96    "$PWD/output/posix/bin/ot-cli" -n --radio-version "spinel+hdlc+forkpty://${RADIO_NCP_PATH}?forkpty-arg=1&ncp-dataset=1"
97
98    echo "Step 3. Start posix app and check whether PAN dataset is the same..."
99    RADIO_RCP_PATH="$PWD/output/simulation/bin/ot-rcp"
100
101    OT_CLI_CMD="$PWD/output/posix/bin/ot-cli spinel+hdlc+forkpty://${RADIO_RCP_PATH}?forkpty-arg=1"
102
103    expect <<EOF
104spawn ${OT_CLI_CMD}
105set timeout 2
106expect_after {
107    timeout { exit 1 }
108}
109send "panid\r\n"
110expect ${PANID}
111expect "Done"
112send "extpanid\r\n"
113expect ${EXT_PANID}
114expect "Done"
115send "networkname\r\n"
116expect ${NETWORK_NAME}
117expect "Done"
118send "channel\r\n"
119expect ${CHANNEL}
120expect "Done"
121send "networkkey\r\n"
122expect ${NETWORK_KEY}
123expect "Done"
124send "exit\r\n"
125expect eof
126EOF
127
128    echo "Step 4. Start posix app and check whether it can get radio firmware version..."
129    RADIO_VERSION="$("$PWD/output/posix/bin/ot-cli" -n --radio-version "spinel+hdlc+forkpty://${RADIO_RCP_PATH}?forkpty-arg=1&ncp-dataset=1")" || true
130    echo "${RADIO_VERSION}"
131    test -n "{RADIO_VERSION}"
132}
133
134main()
135{
136    case "$1" in
137        build)
138            build
139            ;;
140        check)
141            check
142            ;;
143        *)
144            build
145            check
146            ;;
147    esac
148}
149
150main "$@"
151