Skip to content

Commit

Permalink
feat: allow blocking read
Browse files Browse the repository at this point in the history
  • Loading branch information
ARizzo35 committed May 7, 2024
1 parent 83fd7fb commit cf19d23
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub struct SerialPortBuilder {
/// Number of bits to use to signal the end of a character
stop_bits: StopBits,
/// Amount of time to wait to receive data before timing out
timeout: Duration,
timeout: Option<Duration>,
}

impl SerialPortBuilder {
Expand Down Expand Up @@ -375,7 +375,7 @@ impl SerialPortBuilder {
/// Set the amount of time to wait to receive data before timing out
#[must_use]
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self.timeout = Some(timeout);
self
}

Expand Down Expand Up @@ -459,7 +459,7 @@ pub trait SerialPort: Send + io::Read + io::Write {
fn stop_bits(&self) -> Result<StopBits>;

/// Returns the current timeout.
fn timeout(&self) -> Duration;
fn timeout(&self) -> Option<Duration>;

// Port settings setters

Expand All @@ -485,7 +485,7 @@ pub trait SerialPort: Send + io::Read + io::Write {
fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<()>;

/// Sets the timeout for future I/O operations.
fn set_timeout(&mut self, timeout: Duration) -> Result<()>;
fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()>;

// Functions for setting non-data control signal pins

Expand Down Expand Up @@ -647,7 +647,7 @@ impl<T: SerialPort> SerialPort for &mut T {
(**self).stop_bits()
}

fn timeout(&self) -> Duration {
fn timeout(&self) -> Option<Duration> {
(**self).timeout()
}

Expand All @@ -671,7 +671,7 @@ impl<T: SerialPort> SerialPort for &mut T {
(**self).set_stop_bits(stop_bits)
}

fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
(**self).set_timeout(timeout)
}

Expand Down Expand Up @@ -812,7 +812,7 @@ pub fn new<'a>(path: impl Into<std::borrow::Cow<'a, str>>, baud_rate: u32) -> Se
flow_control: FlowControl::None,
parity: Parity::None,
stop_bits: StopBits::One,
timeout: Duration::from_millis(0),
timeout: None,
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/posix/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@ use nix::sys::signal::SigSet;
#[cfg(target_os = "linux")]
use nix::sys::time::{TimeSpec, TimeValLike};

pub fn wait_read_fd(fd: RawFd, timeout: Duration) -> io::Result<()> {
pub fn wait_read_fd(fd: RawFd, timeout: Option<Duration>) -> io::Result<()> {
wait_fd(fd, PollFlags::POLLIN, timeout)
}

pub fn wait_write_fd(fd: RawFd, timeout: Duration) -> io::Result<()> {
pub fn wait_write_fd(fd: RawFd, timeout: Option<Duration>) -> io::Result<()> {
wait_fd(fd, PollFlags::POLLOUT, timeout)
}

fn wait_fd(fd: RawFd, events: PollFlags, timeout: Duration) -> io::Result<()> {
fn wait_fd(fd: RawFd, events: PollFlags, timeout: Option<Duration>) -> io::Result<()> {
use nix::errno::Errno::{EIO, EPIPE};

let mut fd = PollFd::new(fd, events);

let milliseconds =
timeout.as_secs() as i64 * 1000 + i64::from(timeout.subsec_nanos()) / 1_000_000;
let milliseconds = timeout.map(|t| t.as_secs() as i64 * 1000 +
i64::from(t.subsec_nanos()) / 1_000_000);
#[cfg(target_os = "linux")]
let wait_res = {
let timespec = TimeSpec::milliseconds(milliseconds);
let timespec = milliseconds.map(TimeSpec::milliseconds);
nix::poll::ppoll(
slice::from_mut(&mut fd),
Some(timespec),
timespec,
Some(SigSet::empty()),
)
};
#[cfg(not(target_os = "linux"))]
let wait_res = nix::poll::poll(slice::from_mut(&mut fd), milliseconds as nix::libc::c_int);
let wait_res =
nix::poll::poll(slice::from_mut(&mut fd), milliseconds.unwrap_or(-1) as nix::libc::c_int);

let wait = match wait_res {
Ok(r) => r,
Expand Down
12 changes: 6 additions & 6 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn close(fd: RawFd) {
#[derive(Debug)]
pub struct TTYPort {
fd: RawFd,
timeout: Duration,
timeout: Option<Duration>,
exclusive: bool,
port_name: Option<String>,
#[cfg(any(target_os = "ios", target_os = "macos"))]
Expand Down Expand Up @@ -311,7 +311,7 @@ impl TTYPort {

let slave_tty = TTYPort {
fd,
timeout: Duration::from_millis(100),
timeout: Some(Duration::from_millis(100)),
exclusive: true,
port_name: Some(ptty_name),
#[cfg(any(target_os = "ios", target_os = "macos"))]
Expand All @@ -323,7 +323,7 @@ impl TTYPort {
// BSDs when used on the master port.
let master_tty = TTYPort {
fd: next_pty_fd.into_raw_fd(),
timeout: Duration::from_millis(100),
timeout: Some(Duration::from_millis(100)),
exclusive: true,
port_name: None,
#[cfg(any(target_os = "ios", target_os = "macos"))]
Expand Down Expand Up @@ -407,7 +407,7 @@ impl FromRawFd for TTYPort {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
TTYPort {
fd,
timeout: Duration::from_millis(100),
timeout: Some(Duration::from_millis(100)),
exclusive: ioctl::tiocexcl(fd).is_ok(),
// It is not trivial to get the file path corresponding to a file descriptor.
// We'll punt on it and set it to `None` here.
Expand Down Expand Up @@ -616,7 +616,7 @@ impl SerialPort for TTYPort {
}
}

fn timeout(&self) -> Duration {
fn timeout(&self) -> Option<Duration> {
self.timeout
}

Expand Down Expand Up @@ -678,7 +678,7 @@ impl SerialPort for TTYPort {
return termios::set_termios(self.fd, &termios);
}

fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
self.timeout = timeout;
Ok(())
}
Expand Down

0 comments on commit cf19d23

Please sign in to comment.