xref: /aosp_15_r20/external/ot-br-posix/tests/scripts/expect/_common.exp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker#!/usr/bin/expect -f
2*4a64e381SAndroid Build Coastguard Worker#
3*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2024, The OpenThread Authors.
4*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
5*4a64e381SAndroid Build Coastguard Worker#
6*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*4a64e381SAndroid Build Coastguard Worker#
17*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*4a64e381SAndroid Build Coastguard Worker#
29*4a64e381SAndroid Build Coastguard Worker
30*4a64e381SAndroid Build Coastguard Workerproc wait_for {command success {failure {[\r\n]FAILURE_NOT_EXPECTED[\r\n]}}} {
31*4a64e381SAndroid Build Coastguard Worker    set timeout 1
32*4a64e381SAndroid Build Coastguard Worker    for {set i 0} {$i < 40} {incr i} {
33*4a64e381SAndroid Build Coastguard Worker        if {$command != ""} {
34*4a64e381SAndroid Build Coastguard Worker            send "$command\n"
35*4a64e381SAndroid Build Coastguard Worker        }
36*4a64e381SAndroid Build Coastguard Worker
37*4a64e381SAndroid Build Coastguard Worker        expect {
38*4a64e381SAndroid Build Coastguard Worker            -re $success {
39*4a64e381SAndroid Build Coastguard Worker                return 0
40*4a64e381SAndroid Build Coastguard Worker            }
41*4a64e381SAndroid Build Coastguard Worker            -re $failure {
42*4a64e381SAndroid Build Coastguard Worker                fail "Failed due to '$failure' found"
43*4a64e381SAndroid Build Coastguard Worker            }
44*4a64e381SAndroid Build Coastguard Worker            timeout {
45*4a64e381SAndroid Build Coastguard Worker                # Do nothing
46*4a64e381SAndroid Build Coastguard Worker            }
47*4a64e381SAndroid Build Coastguard Worker        }
48*4a64e381SAndroid Build Coastguard Worker    }
49*4a64e381SAndroid Build Coastguard Worker    fail "Failed due to '$success' not found"
50*4a64e381SAndroid Build Coastguard Worker}
51*4a64e381SAndroid Build Coastguard Worker
52*4a64e381SAndroid Build Coastguard Workerproc expect_line {line} {
53*4a64e381SAndroid Build Coastguard Worker    set timeout 10
54*4a64e381SAndroid Build Coastguard Worker    expect -re "\[\r\n \]($line)(?=\[\r\n>\])"
55*4a64e381SAndroid Build Coastguard Worker    return $expect_out(1,string)
56*4a64e381SAndroid Build Coastguard Worker}
57*4a64e381SAndroid Build Coastguard Worker
58*4a64e381SAndroid Build Coastguard Worker# type: The type of the node.
59*4a64e381SAndroid Build Coastguard Worker#   Possible values:
60*4a64e381SAndroid Build Coastguard Worker#   1. cli: The cli app. ot-cli-ftd or ot-cli-mtd
61*4a64e381SAndroid Build Coastguard Worker#   2. otbr: The otbr-agent.
62*4a64e381SAndroid Build Coastguard Worker#
63*4a64e381SAndroid Build Coastguard Worker# sim_app: The path of the simulation app to start the node.
64*4a64e381SAndroid Build Coastguard Worker#   If type is 'cli', sim_app is the path of the cli app.
65*4a64e381SAndroid Build Coastguard Worker#   If type is 'otbr', sim_app is the path of the coprocessor. It could be 'ot-rcp', 'ot-ncp-ftd'
66*4a64e381SAndroid Build Coastguard Worker#     or 'ot-ncp-mtd'.
67*4a64e381SAndroid Build Coastguard Workerproc spawn_node {id type sim_app} {
68*4a64e381SAndroid Build Coastguard Worker    global spawn_id
69*4a64e381SAndroid Build Coastguard Worker    global spawn_ids
70*4a64e381SAndroid Build Coastguard Worker    global argv0
71*4a64e381SAndroid Build Coastguard Worker
72*4a64e381SAndroid Build Coastguard Worker    send_user "\n# ${id} ${type} ${sim_app}\n"
73*4a64e381SAndroid Build Coastguard Worker
74*4a64e381SAndroid Build Coastguard Worker    switch -regexp ${type} {
75*4a64e381SAndroid Build Coastguard Worker        cli {
76*4a64e381SAndroid Build Coastguard Worker            spawn $sim_app $id
77*4a64e381SAndroid Build Coastguard Worker            send "factoryreset\n"
78*4a64e381SAndroid Build Coastguard Worker            wait_for "state" "disabled"
79*4a64e381SAndroid Build Coastguard Worker            expect_line "Done"
80*4a64e381SAndroid Build Coastguard Worker            send "routerselectionjitter 1\n"
81*4a64e381SAndroid Build Coastguard Worker            expect_line "Done"
82*4a64e381SAndroid Build Coastguard Worker
83*4a64e381SAndroid Build Coastguard Worker            expect_after {
84*4a64e381SAndroid Build Coastguard Worker                timeout { fail "Timed out" }
85*4a64e381SAndroid Build Coastguard Worker            }
86*4a64e381SAndroid Build Coastguard Worker        }
87*4a64e381SAndroid Build Coastguard Worker        otbr {
88*4a64e381SAndroid Build Coastguard Worker            spawn $::env(EXP_OTBR_AGENT_PATH) -I $::env(EXP_TUN_NAME) -d7 "spinel+hdlc+forkpty://${sim_app}?forkpty-arg=${id}"
89*4a64e381SAndroid Build Coastguard Worker        }
90*4a64e381SAndroid Build Coastguard Worker    }
91*4a64e381SAndroid Build Coastguard Worker
92*4a64e381SAndroid Build Coastguard Worker    set spawn_ids($id) $spawn_id
93*4a64e381SAndroid Build Coastguard Worker
94*4a64e381SAndroid Build Coastguard Worker    return $spawn_id
95*4a64e381SAndroid Build Coastguard Worker}
96*4a64e381SAndroid Build Coastguard Worker
97*4a64e381SAndroid Build Coastguard Workerproc switch_node {id} {
98*4a64e381SAndroid Build Coastguard Worker    global spawn_ids
99*4a64e381SAndroid Build Coastguard Worker    global spawn_id
100*4a64e381SAndroid Build Coastguard Worker
101*4a64e381SAndroid Build Coastguard Worker    send_user "\n# ${id}\n"
102*4a64e381SAndroid Build Coastguard Worker    set spawn_id $spawn_ids($id)
103*4a64e381SAndroid Build Coastguard Worker}
104