Skip to content

Commit

Permalink
Try to set DTR on best effort on POSIX
Browse files Browse the repository at this point in the history
Deciding on whether the actual device supports setting DTR based on the
baud rate does not play out for Linux pseudo terminals. Let's try on
best effort then and ignore ENOTTY/ErrorKind::Unsupported.
  • Loading branch information
sirhcel committed Jan 22, 2025
1 parent ec256f0 commit 9b56161
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/posix/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl From<nix::Error> for Error {
E::EINTR => K::Io(IO::Interrupted),
E::EACCES => K::Io(IO::PermissionDenied),
E::ENOENT => K::Io(IO::NotFound),
E::ENOTTY => K::Io(IO::Unsupported),
_ => K::Unknown,
};
Error::new(kind, e.desc())
Expand Down
13 changes: 8 additions & 5 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ impl TTYPort {
baud_rate: builder.baud_rate,
};

// Ignore setting DTR for pseudo terminals (indicated by baud_rate == 0).
if builder.baud_rate > 0 {
if let Some(dtr) = builder.dtr_on_open {
port.write_data_terminal_ready(dtr)?;
}
// Try to set DTR on best effort. This operation fails for certain devices like Linux
// pseudo terminals but we have currently no strategy for reliably detecting whether this
// is supposed to work or not.
if let Some(dtr) = builder.dtr_on_open {
port.write_data_terminal_ready(dtr).or_else(|e| match e {
e if e.kind() == ErrorKind::Io(io::ErrorKind::Unsupported) => Ok(()),
e => Err(e),
})?;
}

Ok(port)
Expand Down

0 comments on commit 9b56161

Please sign in to comment.