diff --git a/src/posix/termios.rs b/src/posix/termios.rs index f53af807..8a407bbd 100644 --- a/src/posix/termios.rs +++ b/src/posix/termios.rs @@ -97,15 +97,20 @@ pub(crate) fn get_termios(fd: RawFd) -> Result { } #[cfg(any(target_os = "ios", target_os = "macos",))] -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)?; - - // Note: attempting to set the baud rate on a pseudo terminal via this ioctl call will fail - // with the `ENOTTY` error. +pub(crate) fn set_termios(fd: RawFd, termios: &mut libc::termios, baud_rate: u32) -> Result<()> { + let mut ispeed_res = 0; + let mut ospeed_res = 0; if baud_rate > 0 { - crate::posix::ioctl::iossiospeed(fd, &(baud_rate as libc::speed_t))?; + unsafe { + ispeed_res = libc::cfsetispeed(&mut *termios, baud_rate as libc::speed_t); + ospeed_res = libc::cfsetospeed(&mut *termios, baud_rate as libc::speed_t); + } } + nix::errno::Errno::result(ispeed_res)?; + nix::errno::Errno::result(ospeed_res)?; + + let res = unsafe { libc::tcsetattr(fd, libc::TCSANOW, termios) }; + nix::errno::Errno::result(res)?; Ok(()) } diff --git a/src/posix/tty.rs b/src/posix/tty.rs index b1d68cff..68a4078a 100644 --- a/src/posix/tty.rs +++ b/src/posix/tty.rs @@ -178,7 +178,7 @@ impl TTYPort { #[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"))] - termios::set_termios(fd.0, &termios, builder.baud_rate)?; + termios::set_termios(fd.0, &mut termios, builder.baud_rate)?; #[cfg(not(any(target_os = "ios", target_os = "macos")))] termios::set_termios(fd.0, &termios)?; @@ -646,7 +646,7 @@ impl SerialPort for TTYPort { let mut termios = termios::get_termios(self.fd)?; termios::set_flow_control(&mut termios, flow_control); #[cfg(any(target_os = "ios", target_os = "macos"))] - return termios::set_termios(self.fd, &termios, self.baud_rate); + return termios::set_termios(self.fd, &mut termios, self.baud_rate); #[cfg(not(any(target_os = "ios", target_os = "macos")))] return termios::set_termios(self.fd, &termios); } @@ -655,7 +655,7 @@ impl SerialPort for TTYPort { let mut termios = termios::get_termios(self.fd)?; termios::set_parity(&mut termios, parity); #[cfg(any(target_os = "ios", target_os = "macos"))] - return termios::set_termios(self.fd, &termios, self.baud_rate); + return termios::set_termios(self.fd, &mut termios, self.baud_rate); #[cfg(not(any(target_os = "ios", target_os = "macos")))] return termios::set_termios(self.fd, &termios); } @@ -664,7 +664,7 @@ impl SerialPort for TTYPort { let mut termios = termios::get_termios(self.fd)?; termios::set_data_bits(&mut termios, data_bits); #[cfg(any(target_os = "ios", target_os = "macos"))] - return termios::set_termios(self.fd, &termios, self.baud_rate); + return termios::set_termios(self.fd, &mut termios, self.baud_rate); #[cfg(not(any(target_os = "ios", target_os = "macos")))] return termios::set_termios(self.fd, &termios); } @@ -673,7 +673,7 @@ impl SerialPort for TTYPort { let mut termios = termios::get_termios(self.fd)?; termios::set_stop_bits(&mut termios, stop_bits); #[cfg(any(target_os = "ios", target_os = "macos"))] - return termios::set_termios(self.fd, &termios, self.baud_rate); + return termios::set_termios(self.fd, &mut termios, self.baud_rate); #[cfg(not(any(target_os = "ios", target_os = "macos")))] return termios::set_termios(self.fd, &termios); }