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

skip attempts to set baud rate of 0 via ioctl (macos) #58

Merged
merged 1 commit into from
Jul 7, 2022
Merged
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
8 changes: 7 additions & 1 deletion src/posix/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ pub(crate) fn get_termios(fd: RawFd) -> Result<Termios> {
pub(crate) fn set_termios(fd: RawFd, termios: &libc::termios, baud_rate: u32) -> Result<()> {
let res = unsafe { libc::tcsetattr(fd, libc::TCSANOW, termios) };
nix::errno::Errno::result(res)?;
crate::posix::ioctl::iossiospeed(fd, &(baud_rate as libc::speed_t))?;

// Note: attempting to set the baud rate on a pseudo terminal via this ioctl call will faill
// with the `ENOTTY` error.
if baud_rate > 0 {
crate::posix::ioctl::iossiospeed(fd, &(baud_rate as libc::speed_t))?;
}

Ok(())
}

Expand Down
15 changes: 15 additions & 0 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ fn close(fd: RawFd) {
/// should not be instantiated directly by using `TTYPort::open()`, instead use
/// the cross-platform `serialport::open()` or
/// `serialport::open_with_settings()`.
///
/// Note: on macOS, when connecting to a pseudo-terminal (`pty` opened via
/// `posix_openpt`), the `baud_rate` should be set to 0; this will be used to
/// explicitly _skip_ an attempt to set the baud rate of the file descriptor
/// that would otherwise happen via an `ioctl` command.
///
/// ```
/// use serialport::{TTYPort, SerialPort};
///
/// let (mut master, slave) = TTYPort::pair().expect("Unable to create ptty pair");
///
/// // ... elsewhere
///
/// let mut port = TTYPort::open(&serialport::new(slave.name().unwrap(), 0)).expect("unable to open");
/// ```
#[derive(Debug)]
pub struct TTYPort {
fd: RawFd,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ fn test_ttyport_timeout() {
}
}

#[test]
#[cfg(any(target_os = "ios", target_os = "macos"))]
fn test_osx_pty_pair() {
#![allow(unused_variables)]
let (mut master, slave) = TTYPort::pair().expect("Unable to create ptty pair");
let (output_sink, output_stream) = std::sync::mpsc::sync_channel(1);
let name = slave.name().unwrap();

master.write("12".as_bytes()).expect("");

let reader_thread = std::thread::spawn(move || {
let mut port = TTYPort::open(&serialport::new(&name, 0)).expect("unable to open");
let mut buffer = [0u8; 2];
let amount = port.read_exact(&mut buffer);
output_sink
.send(String::from_utf8(buffer.to_vec()).expect("buffer not read as valid utf-8"))
.expect("unable to send from thread");
});

reader_thread.join().expect("unable to join sink thread");
assert_eq!(output_stream.recv().unwrap(), "12");
}

// On Mac this should work (in fact used to in b77768a) but now fails. It's not functionality that
// should be required, and the ptys work otherwise. So going to just diable this test instead.
#[test]
Expand Down