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 < 20} {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 setup_leader {} { 130 send "dataset init new\n" 131 expect_line "Done" 132 send "dataset networkkey 00112233445566778899aabbccddeeff\n" 133 expect_line "Done" 134 send "dataset commit active\n" 135 expect_line "Done" 136 send "ifconfig up\n" 137 expect_line "Done" 138 send "thread start\n" 139 expect_line "Done" 140 wait_for "state" "leader" 141 expect_line "Done" 142} 143 144proc dispose_node {id} { 145 switch_node $id 146 send "\x04" 147 expect eof 148} 149 150proc dispose_all {} { 151 global spawn_ids 152 set max_node [array size spawn_ids] 153 for {set i 1} {$i <= $max_node} {incr i} { 154 dispose_node $i 155 } 156 array unset spawn_ids 157} 158 159proc get_ipaddr {type} { 160 send "ipaddr $type\n" 161 expect "ipaddr $type" 162 set rval [expect_line {([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}}] 163 expect_line "Done" 164 165 return $rval 166} 167 168proc get_extaddr {} { 169 send "extaddr\n" 170 set rval [expect_line {[0-9a-fA-F]{16}}] 171 expect_line "Done" 172 173 return $rval 174} 175 176proc get_meshlocal_prefix {} { 177 send "prefix meshlocal\n" 178 expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])} 179 set rval $expect_out(1,string) 180 expect_line "Done" 181 182 return $rval 183} 184 185proc get_rloc16 {} { 186 send "rloc16\n" 187 expect "rloc16" 188 set rval [expect_line {[0-9a-fA-F]{4}}] 189 expect_line "Done" 190 191 return $rval 192} 193 194proc setup_default_network {} { 195 send "channel 11\n" 196 expect_line "Done" 197 send "panid 0xface\n" 198 expect_line "Done" 199 send "networkkey 00112233445566778899aabbccddeeff\n" 200 expect_line "Done" 201} 202 203proc fail {message} { 204 dispose_all 205 error $message 206} 207 208set timeout 10 209