Skip to content

Commit

Permalink
[opentitanlib] make uart_read return at most a line at a time
Browse files Browse the repository at this point in the history
This is a hack to provide temporary solution to address some of our
flaky tests, where `UartConsole::wait_for` over-consumes data.

Signed-off-by: Gary Guo <[email protected]>
  • Loading branch information
nbdd0121 committed Jan 11, 2024
1 parent b6df4b1 commit bd4c7ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sw/host/opentitanlib/src/uart/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ impl UartConsole {
return Ok(ExitStatus::ExitSuccess);
}

// HACK(nbdd0121): do a nonblocking read because the UART buffer may still have data in it.
// If we wait for mio event now, we might be blocking forever.
while self.uart_read(device, Duration::from_millis(0), &mut stdout)? {
if self
.exit_success
.as_ref()
.map(|rx| rx.is_match(&self.buffer))
== Some(true)
{
return Ok(ExitStatus::ExitSuccess);
}
if self
.exit_failure
.as_ref()
.map(|rx| rx.is_match(&self.buffer))
== Some(true)
{
return Ok(ExitStatus::ExitFailure);
}
}

let mut poll = Poll::new()?;
let transport_help_token = Self::get_next_token();
let nonblocking_help = device.nonblocking_help()?;
Expand Down Expand Up @@ -176,7 +197,7 @@ impl UartConsole {
where
T: ConsoleDevice + ?Sized,
{
let mut buf = [0u8; 256];
let mut buf = [0u8; 1];
let len = device.console_read(&mut buf, timeout)?;
if len == 0 {
return Ok(false);
Expand Down
1 change: 1 addition & 0 deletions sw/host/tests/chip/rv_dm/src/ndm_reset_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn test_ndm_reset_req(opts: &Opts, transport: &TransportWrapper) -> Result<()> {
// Enable console and wait for the message.
let uart = transport.uart("console")?;
uart.set_flow_control(true)?;
let _ = UartConsole::wait_for(&*uart, r"Running [^\r\n]*", opts.timeout)?;

log::info!("Waiting for \"wait for ndm reset\" message");
let _ = UartConsole::wait_for(&*uart, "wait for ndm reset", opts.timeout)?;
Expand Down

0 comments on commit bd4c7ef

Please sign in to comment.