Skip to content

Commit

Permalink
Don't ingnore errors when setting baud rate
Browse files Browse the repository at this point in the history
"Don't you think we should tell them that this baud rate is not
supported? Nah. Just smile and wave boys smile, smile and wave." While
this makes great movies, this does not make setting a knowingly
unsupported baud rate a great experience. At least when in comes to
debugging.

Play nice and report this and other errors related to baud rates back.
  • Loading branch information
sirhcel authored and eldruin committed Aug 31, 2024
1 parent 18f5c2a commit 02406d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/posix/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@ pub(crate) fn set_stop_bits(termios: &mut Termios, stop_bits: StopBits) {
))
)
))]
pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) {
pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) -> Result<()> {
termios.c_cflag &= !nix::libc::CBAUD;
termios.c_cflag |= nix::libc::BOTHER;
termios.c_ispeed = baud_rate;
termios.c_ospeed = baud_rate;
Ok(())
}

// BSDs use the baud rate as the constant value so there's no translation necessary
Expand All @@ -227,9 +228,10 @@ pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) {
target_os = "netbsd",
target_os = "openbsd"
))]
pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) {
// Ignore the return value because this should never fail
unsafe { libc::cfsetspeed(termios, baud_rate.into()) };
pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) -> Result<()> {
let res = unsafe { libc::cfsetspeed(termios, baud_rate.into()) };
nix::errno::Errno::result(res)?;
Ok(())
}

#[cfg(all(
Expand All @@ -240,7 +242,9 @@ pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) {
target_arch = "powerpc64"
)
))]
pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) {
pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) -> Result<()> {
use crate::{Error, ErrorKind};

use self::libc::{
B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000, B460800,
B500000, B576000, B921600,
Expand Down Expand Up @@ -281,8 +285,9 @@ pub(crate) fn set_baud_rate(termios: &mut Termios, baud_rate: u32) {
3_000_000 => B3000000,
3_500_000 => B3500000,
4_000_000 => B4000000,
_ => return,
_ => return Err(Error::new(ErrorKind::InvalidInput, "Unsupported baud rate")),
};
let res = unsafe { libc::cfsetspeed(termios, baud_rate) };
nix::errno::Errno::result(res).expect("cfsetspeed failed");
nix::errno::Errno::result(res)?;
Ok(())
}
4 changes: 2 additions & 2 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl TTYPort {
termios::set_data_bits(&mut termios, builder.data_bits);
termios::set_stop_bits(&mut termios, builder.stop_bits);
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
termios::set_baud_rate(&mut termios, builder.baud_rate);
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)?;
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
Expand Down Expand Up @@ -630,7 +630,7 @@ impl SerialPort for TTYPort {
))]
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
let mut termios = termios::get_termios(self.fd)?;
termios::set_baud_rate(&mut termios, baud_rate);
termios::set_baud_rate(&mut termios, baud_rate)?;
termios::set_termios(self.fd, &termios)
}

Expand Down

0 comments on commit 02406d7

Please sign in to comment.