• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/expect -f
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
30proc skip_on_macos {} {
31    set OSTYPE [lindex $::tcl_platform(os) 0]
32
33    if { $OSTYPE == "Darwin" } {
34        exit 77
35    }
36}
37
38proc wait_for {command success {failure {[\r\n]FAILURE_NOT_EXPECTED[\r\n]}}} {
39    set timeout 1
40    for {set i 0} {$i < 40} {incr i} {
41        if {$command != ""} {
42            send "$command\n"
43        }
44
45        expect {
46            -re $success {
47                return 0
48            }
49            -re $failure {
50                fail "Failed due to '$failure' found"
51            }
52            timeout {
53                # Do nothing
54            }
55        }
56    }
57    fail "Failed due to '$success' not found"
58}
59
60proc expect_line {line} {
61    set timeout 10
62    expect -re "\[\r\n \]($line)(?=\[\r\n>\])"
63    return $expect_out(1,string)
64}
65
66proc spawn_node {id {type ""} {radio_url ""}} {
67    global spawn_id
68    global spawn_ids
69    global argv0
70
71    if {${type} == ""} {
72        set type $::env(OT_NODE_TYPE)
73    }
74
75    if {${radio_url} == ""} {
76        set radio_url "spinel+hdlc+uart://$::env(OT_SIMULATION_APPS)/ncp/ot-rcp?forkpty-arg=$id"
77    }
78
79    send_user "\n# ${id} ${type}\n"
80
81    if {[info exists ::env(CC)] && $::env(CC) == "clang"} {
82        set gcov_prefix ""
83    } else {
84        set gcov_prefix "ot-run/$argv0/ot-gcda.$id"
85    }
86
87    if {[info exists ::env(OT_SIMULATION_LOCAL_HOST)]} {
88        set ot_simulation_local_host $::env(OT_SIMULATION_LOCAL_HOST)
89        set radio_url "$radio_url&forkpty-arg=-L$ot_simulation_local_host"
90    } else {
91        set ot_simulation_local_host "127.0.0.1"
92    }
93
94    switch -regexp ${type} {
95        {rcp|rcp-cli} {
96            # Sleep 0.2 seconds to ensure that the ot-rcp in the previous test has exited to
97            # avoid the error: "bind(sTxFd): Address already in use"
98            sleep 0.2
99            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_POSIX_APPS)/ot-cli $radio_url
100            send "factoryreset\n"
101            wait_for "state" "disabled"
102            expect_line "Done"
103            send "routerselectionjitter 1\n"
104            expect_line "Done"
105        }
106        cli {
107            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_SIMULATION_APPS)/cli/ot-cli-ftd \
108                -L$ot_simulation_local_host $id
109            send "factoryreset\n"
110            wait_for "state" "disabled"
111            expect_line "Done"
112            send "routerselectionjitter 1\n"
113            expect_line "Done"
114        }
115        mtd {
116            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_SIMULATION_APPS)/cli/ot-cli-mtd \
117                -L$ot_simulation_local_host $id
118            send "factoryreset\n"
119            wait_for "state" "disabled"
120            expect_line "Done"
121        }
122    }
123
124    expect_after {
125        timeout { fail "Timed out" }
126    }
127
128    set spawn_ids($id) $spawn_id
129
130    return $spawn_id
131}
132
133proc switch_node {id} {
134    global spawn_ids
135    global spawn_id
136
137    send_user "\n# ${id}\n"
138    set spawn_id $spawn_ids($id)
139}
140
141proc attach {{role "leader"}} {
142    send "ifconfig up\n"
143    expect_line "Done"
144    send "thread start\n"
145    expect_line "Done"
146    wait_for "state" $role
147    expect_line "Done"
148}
149
150proc setup_leader {} {
151    send "dataset init new\n"
152    expect_line "Done"
153    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
154    expect_line "Done"
155    send "dataset commit active\n"
156    expect_line "Done"
157    attach
158}
159
160proc dispose_node {id} {
161    switch_node $id
162    send "\x04"
163    expect eof
164}
165
166proc dispose_all {} {
167    global spawn_ids
168    set max_node [array size spawn_ids]
169    for {set i 1} {$i <= $max_node} {incr i} {
170        dispose_node $i
171    }
172    array unset spawn_ids
173}
174
175proc get_ipaddr {type} {
176    send "ipaddr $type\n"
177    expect "ipaddr $type"
178    set rval [expect_line {([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}}]
179    expect_line "Done"
180
181    return $rval
182}
183
184proc get_extaddr {} {
185    send "extaddr\n"
186    set rval [expect_line {[0-9a-fA-F]{16}}]
187    expect_line "Done"
188
189    return $rval
190}
191
192proc get_extpanid {} {
193    send "extpanid\n"
194    set rval [expect_line {[0-9a-fA-F]{16}}]
195    expect_line "Done"
196
197    return $rval
198}
199
200proc get_panid {} {
201    send "panid\n"
202    expect -re {0x([0-9a-fA-F]{4})}
203    set rval $expect_out(1,string)
204    expect_line "Done"
205
206    return $rval
207}
208
209proc get_meshlocal_prefix {} {
210    send "prefix meshlocal\n"
211    expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])}
212    set rval $expect_out(1,string)
213    expect_line "Done"
214
215    return $rval
216}
217
218proc get_rloc16 {} {
219    send "rloc16\n"
220    expect "rloc16"
221    set rval [expect_line {[0-9a-fA-F]{4}}]
222    expect_line "Done"
223
224    return $rval
225}
226
227proc setup_default_network {} {
228    send "dataset init new\n"
229    expect_line "Done"
230    send "dataset channel 11\n"
231    expect_line "Done"
232    send "dataset extpanid 000db80000000000\n"
233    expect_line "Done"
234    send "dataset meshlocalprefix fd00:db8::\n"
235    expect_line "Done"
236    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
237    expect_line "Done"
238    send "dataset networkname OpenThread-face\n"
239    expect_line "Done"
240    send "dataset panid 0xface\n"
241    expect_line "Done"
242    send "dataset pskc c23a76e98f1a6483639b1ac1271e2e27\n"
243    expect_line "Done"
244    send "dataset commit active\n"
245    expect_line "Done"
246}
247
248proc fail {message} {
249    dispose_all
250    error $message
251}
252
253set timeout 10
254