diff --git a/tests/expectnmcu/net.tcl b/tests/expectnmcu/net.tcl new file mode 100644 index 00000000..7dc0b1a4 --- /dev/null +++ b/tests/expectnmcu/net.tcl @@ -0,0 +1,55 @@ +namespace eval expectnmcu::net { +} + +package require expectnmcu::core + +# Wait for `wifi.sta.getip()` to return something that looks like an address, +# indicating that the device under test is probably online. +proc ::expectnmcu::net::waitwifista { dev {timeout 10} } { + for {set i 0} {${i} < ${timeout}} {incr i} { + send -i ${dev} "=wifi.sta.getip()\n" + expect { + -i ${dev} -re "\n(\[^\n\t\]+)\t\[^\t\]+\t\[^\t\]+\[\r\n\]+> " { + # That looks like an address report to me + return ${expect_out(1,string)} + } + -i ${dev} -ex "nil\[\r\n\]+> " { + # must not be connected yet + sleep 1 + } + -i ${dev} -re ${::expectnmcu::core::panicre} { return -code error "Panic!" } + timeout { } + } + } + + return -code error "WIFI STA: no IP address" +} + +proc ::expectnmcu::net::guessmyip { victimip } { + # Guess our IP address by using the victim's + spawn "ip" "route" "get" ${victimip} + expect { + -re "src (\[0-9.\]+) " { + close + return ${expect_out(1,string)} + } + } + close + return -code error "Cannot find source IP" +} + +# Open a socat stream with some verbosity and wait for it to become +# ready before returning. +proc ::expectnmcu::net::mksocat { socat_remote } { + spawn -noecho "socat" "-dd" "STDIO,cfmakeraw" ${socat_remote} + + set timeout 10 + expect { + -i ${spawn_id} -ex "listening on" { return ${spawn_id} } + -i ${spawn_id} -ex "starting data transfer loop" { return ${spawn_id} } + timeout { return -code error "Timeout opening local socat" } + } +} + + +package provide expectnmcu::net 1.0 diff --git a/tests/expectnmcu/pkgIndex.tcl b/tests/expectnmcu/pkgIndex.tcl index 64dbff69..04a5dffa 100644 --- a/tests/expectnmcu/pkgIndex.tcl +++ b/tests/expectnmcu/pkgIndex.tcl @@ -9,4 +9,5 @@ # full path name of this file's directory. package ifneeded expectnmcu::core 1.0 [list source [file join $dir core.tcl]] +package ifneeded expectnmcu::net 1.0 [list source [file join $dir net.tcl]] package ifneeded expectnmcu::xfer 1.0 [list source [file join $dir xfer.tcl]]