xref: /aosp_15_r20/external/ot-br-posix/tests/scripts/expect/_common.exp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1#!/usr/bin/expect -f
2#
3#  Copyright (c) 2024, 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 wait_for {command success {failure {[\r\n]FAILURE_NOT_EXPECTED[\r\n]}}} {
31    set timeout 1
32    for {set i 0} {$i < 40} {incr i} {
33        if {$command != ""} {
34            send "$command\n"
35        }
36
37        expect {
38            -re $success {
39                return 0
40            }
41            -re $failure {
42                fail "Failed due to '$failure' found"
43            }
44            timeout {
45                # Do nothing
46            }
47        }
48    }
49    fail "Failed due to '$success' not found"
50}
51
52proc expect_line {line} {
53    set timeout 10
54    expect -re "\[\r\n \]($line)(?=\[\r\n>\])"
55    return $expect_out(1,string)
56}
57
58# type: The type of the node.
59#   Possible values:
60#   1. cli: The cli app. ot-cli-ftd or ot-cli-mtd
61#   2. otbr: The otbr-agent.
62#
63# sim_app: The path of the simulation app to start the node.
64#   If type is 'cli', sim_app is the path of the cli app.
65#   If type is 'otbr', sim_app is the path of the coprocessor. It could be 'ot-rcp', 'ot-ncp-ftd'
66#     or 'ot-ncp-mtd'.
67proc spawn_node {id type sim_app} {
68    global spawn_id
69    global spawn_ids
70    global argv0
71
72    send_user "\n# ${id} ${type} ${sim_app}\n"
73
74    switch -regexp ${type} {
75        cli {
76            spawn $sim_app $id
77            send "factoryreset\n"
78            wait_for "state" "disabled"
79            expect_line "Done"
80            send "routerselectionjitter 1\n"
81            expect_line "Done"
82
83            expect_after {
84                timeout { fail "Timed out" }
85            }
86        }
87        otbr {
88            spawn $::env(EXP_OTBR_AGENT_PATH) -I $::env(EXP_TUN_NAME) -d7 "spinel+hdlc+forkpty://${sim_app}?forkpty-arg=${id}"
89        }
90    }
91
92    set spawn_ids($id) $spawn_id
93
94    return $spawn_id
95}
96
97proc switch_node {id} {
98    global spawn_ids
99    global spawn_id
100
101    send_user "\n# ${id}\n"
102    set spawn_id $spawn_ids($id)
103}
104