Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hang on TTY read #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/posix/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{DataBits, FlowControl, Parity, Result, StopBits};
use nix::libc;

use std::os::unix::prelude::*;
use std::time::Duration;

cfg_if! {
if #[cfg(any(
Expand Down Expand Up @@ -183,6 +184,12 @@ pub(crate) fn set_flow_control(termios: &mut Termios, flow_control: FlowControl)
};
}

pub(crate) fn set_timeout(termios: &mut Termios, timeout: Duration) {
let timeout = u128::min(timeout.as_millis() / 100, u8::MAX as u128) as u8;
termios.c_cc[libc::VMIN as usize] = 0;
termios.c_cc[libc::VTIME as usize] = timeout;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description (and the documentation of termios as well) mention setting VTIME to 0 as well. What's the reason for setting it to timeout here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this utils function to bridge how timeouts are set on termios (by setting VTIME to a value with 100ms steps) with the Duration type on rust.
If you check at

termios::set_timeout(&mut termios, Duration::from_millis(0));
, this function is being called with a duration = 0, which means that VTIME is set to zero.

}

pub(crate) fn set_data_bits(termios: &mut Termios, data_bits: DataBits) {
let size = match data_bits {
DataBits::Five => libc::CS5,
Expand Down
6 changes: 5 additions & 1 deletion src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ impl TTYPort {
nix::errno::Errno::result(unsafe { tcgetattr(fd.0, termios.as_mut_ptr()) })?;
let mut termios = unsafe { termios.assume_init() };

// setup TTY for binary serial port access
// Setup TTY for binary serial port access
// Enable non-canonical mode
termios.c_lflag &= !libc::ICANON;
// Enable reading from the port and ignore all modem control lines
termios.c_cflag |= libc::CREAD | libc::CLOCAL;
// Enable raw mode which disables any implicit processing of the input or output data streams
Expand Down Expand Up @@ -175,6 +177,8 @@ impl TTYPort {
termios::set_flow_control(&mut termios, builder.flow_control);
termios::set_data_bits(&mut termios, builder.data_bits);
termios::set_stop_bits(&mut termios, builder.stop_bits);
// Set termios read to non-blocking, as we handle blocking ourselves (with unix::poll)
termios::set_timeout(&mut termios, Duration::from_millis(0));
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
termios::set_baud_rate(&mut termios, builder.baud_rate);
#[cfg(any(target_os = "ios", target_os = "macos"))]
Expand Down
Loading