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    switch -regexp ${type} {
88        {rcp|rcp-cli} {
89            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_POSIX_APPS)/ot-cli $radio_url
90            send "factoryreset\n"
91            wait_for "state" "disabled"
92            expect_line "Done"
93            send "routerselectionjitter 1\n"
94            expect_line "Done"
95        }
96        cli {
97            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_SIMULATION_APPS)/cli/ot-cli-ftd $id
98            send "factoryreset\n"
99            wait_for "state" "disabled"
100            expect_line "Done"
101            send "routerselectionjitter 1\n"
102            expect_line "Done"
103        }
104        mtd {
105            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_SIMULATION_APPS)/cli/ot-cli-mtd $id
106            send "factoryreset\n"
107            wait_for "state" "disabled"
108            expect_line "Done"
109        }
110    }
111
112    expect_after {
113        timeout { fail "Timed out" }
114    }
115
116    set spawn_ids($id) $spawn_id
117
118    return $spawn_id
119}
120
121proc switch_node {id} {
122    global spawn_ids
123    global spawn_id
124
125    send_user "\n# ${id}\n"
126    set spawn_id $spawn_ids($id)
127}
128
129proc attach {{role "leader"}} {
130    send "ifconfig up\n"
131    expect_line "Done"
132    send "thread start\n"
133    expect_line "Done"
134    wait_for "state" $role
135    expect_line "Done"
136}
137
138proc setup_leader {} {
139    send "dataset init new\n"
140    expect_line "Done"
141    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
142    expect_line "Done"
143    send "dataset commit active\n"
144    expect_line "Done"
145    attach
146}
147
148proc dispose_node {id} {
149    switch_node $id
150    send "\x04"
151    expect eof
152}
153
154proc dispose_all {} {
155    global spawn_ids
156    set max_node [array size spawn_ids]
157    for {set i 1} {$i <= $max_node} {incr i} {
158        dispose_node $i
159    }
160    array unset spawn_ids
161}
162
163proc get_ipaddr {type} {
164    send "ipaddr $type\n"
165    expect "ipaddr $type"
166    set rval [expect_line {([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}}]
167    expect_line "Done"
168
169    return $rval
170}
171
172proc get_extaddr {} {
173    send "extaddr\n"
174    set rval [expect_line {[0-9a-fA-F]{16}}]
175    expect_line "Done"
176
177    return $rval
178}
179
180proc get_extpanid {} {
181    send "extpanid\n"
182    set rval [expect_line {[0-9a-fA-F]{16}}]
183    expect_line "Done"
184
185    return $rval
186}
187
188proc get_panid {} {
189    send "panid\n"
190    expect -re {0x([0-9a-fA-F]{4})}
191    set rval $expect_out(1,string)
192    expect_line "Done"
193
194    return $rval
195}
196
197proc get_meshlocal_prefix {} {
198    send "prefix meshlocal\n"
199    expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])}
200    set rval $expect_out(1,string)
201    expect_line "Done"
202
203    return $rval
204}
205
206proc get_rloc16 {} {
207    send "rloc16\n"
208    expect "rloc16"
209    set rval [expect_line {[0-9a-fA-F]{4}}]
210    expect_line "Done"
211
212    return $rval
213}
214
215proc setup_default_network {} {
216    send "channel 11\n"
217    expect_line "Done"
218    send "panid 0xface\n"
219    expect_line "Done"
220    send "networkkey 00112233445566778899aabbccddeeff\n"
221    expect_line "Done"
222}
223
224proc fail {message} {
225    dispose_all
226    error $message
227}
228
229set timeout 10
230