core.tcl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. namespace eval expectnmcu::core {
  2. set panicre "powered by Lua \[0-9.\]+ on SDK \[0-9.\]+"
  3. set promptstr "\n> "
  4. namespace export reboot waitboot connect
  5. namespace export send_exp_prompt send_exp_res_prompt send_exp_prompt_c
  6. }
  7. package require cmdline
  8. # Use DTR/RTS signaling to reboot the device
  9. ## I'm not sure why we have to keep resetting the mode, but so it goes.
  10. proc ::expectnmcu::core::reboot { dev } {
  11. set victimfd [open ${dev} ]
  12. set mode [fconfigure ${victimfd} -mode ]
  13. fconfigure ${victimfd} -mode ${mode} -ttycontrol {DTR 0 RTS 1}
  14. sleep 0.1
  15. fconfigure ${victimfd} -mode ${mode} -ttycontrol {DTR 0 RTS 0}
  16. close ${victimfd}
  17. }
  18. proc ::expectnmcu::core::waitboot { victim } {
  19. expect {
  20. -i ${victim} "Formatting file system" {
  21. set timeout 120
  22. exp_continue
  23. }
  24. -i ${victim} "powered by Lua" { }
  25. timeout { return -code error "Timeout" }
  26. }
  27. # Catch nwf's system bootup, in case we're testing an existing system,
  28. # rather than a blank firmware.
  29. expect {
  30. -i ${victim} -re "Reset delay!.*${::expectnmcu::core::promptstr}" {
  31. send -i ${victim} "stop(true)\n"
  32. expect -i ${victim} -ex ${::expectnmcu::core::promptstr}
  33. }
  34. -i ${victim} -ex ${::expectnmcu::core::promptstr} { }
  35. timeout { return -code error "Timeout" }
  36. }
  37. # Do a little more active synchronization with the DUT: send it a command
  38. # and wait for the side-effect of that command to happen, thereby ensuring
  39. # that the next prompt we see is after this point in the input.
  40. send -i ${victim} "print(\"a\",\"z\")\n"
  41. expect {
  42. -i ${victim} -ex "a\tz" { }
  43. }
  44. expect {
  45. -i ${victim} -ex ${::expectnmcu::core::promptstr} { }
  46. timeout { return -code error "Timeout" }
  47. }
  48. }
  49. # Establish a serial connection to the device via socat. Takes
  50. # -baud=N, -reboot=0/1/dontwait, -waitboot=0/1 optional parameters
  51. proc ::expectnmcu::core::connect { dev args } {
  52. set opts {
  53. { baud.arg 115200 }
  54. { reboot.arg 1 }
  55. }
  56. array set arg [::cmdline::getoptions args $opts]
  57. spawn "socat" "STDIO" "${dev},b${arg(baud)},raw,crnl"
  58. close -onexec 1 -i ${spawn_id}
  59. set victim ${spawn_id}
  60. # XXX?
  61. set victimfd [open ${dev} ]
  62. set mode [fconfigure ${victimfd} -mode ${arg(baud)},n,8,1 ]
  63. if { ${arg(reboot)} != 0 } {
  64. ::expectnmcu::core::reboot ${dev}
  65. if { ${arg(reboot)} != "dontwait" } {
  66. ::expectnmcu::core::waitboot ${victim}
  67. }
  68. }
  69. close ${victimfd}
  70. return ${victim}
  71. }
  72. # This one is somewhat "for experts only" -- it expects that you have either
  73. # consumed whatever command you flung at the node or that you have some reason
  74. # to not be concerned with its echo (and return)
  75. proc ::expectnmcu::core::exp_prompt { sid } {
  76. expect {
  77. -i ${sid} -ex ${::expectnmcu::core::promptstr} { }
  78. -i ${sid} -re ${::expectnmcu::core::panicre} { return -code error "Panic!" }
  79. timeout { return -code error "Timeout" }
  80. }
  81. }
  82. proc ::expectnmcu::core::send_exp_prompt { sid cmd } {
  83. send -i ${sid} -- "${cmd}\n"
  84. expect {
  85. -i ${sid} -ex "${cmd}" { }
  86. timeout { return -code error "Timeout" }
  87. }
  88. expect {
  89. -i ${sid} -ex ${::expectnmcu::core::promptstr} { }
  90. -i ${sid} -re ${::expectnmcu::core::panicre} { return -code error "Panic!" }
  91. timeout { return -code error "Timeout" }
  92. }
  93. }
  94. proc ::expectnmcu::core::send_exp_res_prompt { sid cmd res } {
  95. send -i ${sid} -- "${cmd}\n"
  96. expect {
  97. -i ${sid} -ex "${cmd}" { }
  98. timeout { return -code error "Timeout" }
  99. }
  100. expect {
  101. -i ${sid} -re "${res}.*${::expectnmcu::core::promptstr}" { }
  102. -i ${sid} -re ${::expectnmcu::core::panicre} { return -code error "Panic!" }
  103. -i ${sid} -ex ${::expectnmcu::core::promptstr} { return -code error "Prompt before expected response" }
  104. timeout { return -code error "Timeout" }
  105. }
  106. }
  107. proc ::expectnmcu::core::send_exp_prompt_c { sid cmd } {
  108. send -i ${sid} -- "${cmd}\n"
  109. expect {
  110. -i ${sid} -ex "${cmd}" { }
  111. timeout { return -code error "Timeout" }
  112. }
  113. expect {
  114. -i ${sid} -ex "\n>> " { }
  115. -i ${sid} -ex ${::expectnmcu::core::promptstr} { return -code error "Non-continuation prompt" }
  116. -i ${sid} -re ${::expectnmcu::core::panicre} { return -code error "Panic!" }
  117. timeout { return -code error "Timeout" }
  118. }
  119. }
  120. package provide expectnmcu::core 1.0