use std::mem::MaybeUninit;
use std::os::unix::prelude::*;
use std::path::Path;
use std::time::Duration;
use std::{io, mem};
use nix::fcntl::{fcntl, OFlag};
use nix::{self, libc, unistd};
use crate::posix::ioctl::{self, SerialLines};
use crate::posix::termios;
use crate::{
ClearBuffer, DataBits, Error, ErrorKind, FlowControl, Parity, Result, SerialPort,
SerialPortBuilder, StopBits,
};
fn close(fd: RawFd) {
let _ = ioctl::tiocnxcl(fd);
let _ = unistd::close(fd);
}
#[derive(Debug)]
pub struct TTYPort {
fd: RawFd,
timeout: Duration,
exclusive: bool,
port_name: Option<String>,
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate: u32,
}
#[derive(Clone, Copy, Debug)]
pub enum BreakDuration {
Short,
Arbitrary(std::num::NonZeroI32),
}
struct OwnedFd(RawFd);
impl Drop for OwnedFd {
fn drop(&mut self) {
close(self.0);
}
}
impl OwnedFd {
fn into_raw(self) -> RawFd {
let fd = self.0;
mem::forget(self);
fd
}
}
impl TTYPort {
pub fn open(builder: &SerialPortBuilder) -> Result<TTYPort> {
use nix::fcntl::FcntlArg::F_SETFL;
use nix::libc::{cfmakeraw, tcgetattr, tcsetattr};
let path = Path::new(&builder.path);
let fd = OwnedFd(nix::fcntl::open(
path,
OFlag::O_RDWR | OFlag::O_NOCTTY | OFlag::O_NONBLOCK | OFlag::O_CLOEXEC,
nix::sys::stat::Mode::empty(),
)?);
ioctl::tiocexcl(fd.0)?;
let mut termios = MaybeUninit::uninit();
nix::errno::Errno::result(unsafe { tcgetattr(fd.0, termios.as_mut_ptr()) })?;
let mut termios = unsafe { termios.assume_init() };
termios.c_cflag |= libc::CREAD | libc::CLOCAL;
unsafe { cfmakeraw(&mut termios) };
unsafe { tcsetattr(fd.0, libc::TCSANOW, &termios) };
let mut actual_termios = MaybeUninit::uninit();
unsafe { tcgetattr(fd.0, actual_termios.as_mut_ptr()) };
let actual_termios = unsafe { actual_termios.assume_init() };
if actual_termios.c_iflag != termios.c_iflag
|| actual_termios.c_oflag != termios.c_oflag
|| actual_termios.c_lflag != termios.c_lflag
|| actual_termios.c_cflag != termios.c_cflag
{
return Err(Error::new(
ErrorKind::Unknown,
"Settings did not apply correctly",
));
};
#[cfg(any(target_os = "ios", target_os = "macos"))]
if builder.baud_rate > 0 {
unsafe { libc::tcflush(fd.0, libc::TCIOFLUSH) };
}
fcntl(fd.0, F_SETFL(nix::fcntl::OFlag::empty()))?;
let mut termios = termios::get_termios(fd.0)?;
termios::set_parity(&mut termios, builder.parity);
termios::set_flow_control(&mut termios, builder.flow_control);
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);
#[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")))]
termios::set_termios(fd.0, &termios)?;
Ok(TTYPort {
fd: fd.into_raw(),
timeout: builder.timeout,
exclusive: true,
port_name: Some(builder.path.clone()),
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate: builder.baud_rate,
})
}
pub fn exclusive(&self) -> bool {
self.exclusive
}
pub fn set_exclusive(&mut self, exclusive: bool) -> Result<()> {
let setting_result = if exclusive {
ioctl::tiocexcl(self.fd)
} else {
ioctl::tiocnxcl(self.fd)
};
setting_result?;
self.exclusive = exclusive;
Ok(())
}
fn set_pin(&mut self, pin: ioctl::SerialLines, level: bool) -> Result<()> {
if level {
ioctl::tiocmbis(self.fd, pin)
} else {
ioctl::tiocmbic(self.fd, pin)
}
}
fn read_pin(&mut self, pin: ioctl::SerialLines) -> Result<bool> {
ioctl::tiocmget(self.fd).map(|pins| pins.contains(pin))
}
pub fn pair() -> Result<(Self, Self)> {
let next_pty_fd = nix::pty::posix_openpt(nix::fcntl::OFlag::O_RDWR)?;
nix::pty::grantpt(&next_pty_fd)?;
nix::pty::unlockpt(&next_pty_fd)?;
#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia"
)))]
let ptty_name = unsafe { nix::pty::ptsname(&next_pty_fd)? };
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia"
))]
let ptty_name = nix::pty::ptsname_r(&next_pty_fd)?;
#[cfg(any(target_os = "ios", target_os = "macos"))]
let baud_rate = 9600;
let fd = nix::fcntl::open(
Path::new(&ptty_name),
OFlag::O_RDWR | OFlag::O_NOCTTY | OFlag::O_NONBLOCK,
nix::sys::stat::Mode::empty(),
)?;
let mut termios = MaybeUninit::uninit();
let res = unsafe { crate::posix::tty::libc::tcgetattr(fd, termios.as_mut_ptr()) };
if let Err(e) = nix::errno::Errno::result(res) {
close(fd);
return Err(e.into());
}
let mut termios = unsafe { termios.assume_init() };
unsafe { crate::posix::tty::libc::cfmakeraw(&mut termios) };
unsafe { crate::posix::tty::libc::tcsetattr(fd, libc::TCSANOW, &termios) };
fcntl(
fd,
nix::fcntl::FcntlArg::F_SETFL(nix::fcntl::OFlag::empty()),
)?;
let slave_tty = TTYPort {
fd,
timeout: Duration::from_millis(100),
exclusive: true,
port_name: Some(ptty_name),
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate,
};
let master_tty = TTYPort {
fd: next_pty_fd.into_raw_fd(),
timeout: Duration::from_millis(100),
exclusive: true,
port_name: None,
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate,
};
Ok((master_tty, slave_tty))
}
pub fn send_break(&self, duration: BreakDuration) -> Result<()> {
match duration {
BreakDuration::Short => nix::sys::termios::tcsendbreak(self.fd, 0),
BreakDuration::Arbitrary(n) => nix::sys::termios::tcsendbreak(self.fd, n.get()),
}
.map_err(|e| e.into())
}
pub fn try_clone_native(&self) -> Result<TTYPort> {
let fd_cloned: i32 = fcntl(self.fd, nix::fcntl::F_DUPFD_CLOEXEC(self.fd))?;
Ok(TTYPort {
fd: fd_cloned,
exclusive: self.exclusive,
port_name: self.port_name.clone(),
timeout: self.timeout,
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate: self.baud_rate,
})
}
}
impl Drop for TTYPort {
fn drop(&mut self) {
close(self.fd);
}
}
impl AsRawFd for TTYPort {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
impl IntoRawFd for TTYPort {
fn into_raw_fd(self) -> RawFd {
let TTYPort { fd, .. } = self;
mem::forget(self);
fd
}
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
fn get_termios_speed(fd: RawFd) -> u32 {
let mut termios = MaybeUninit::uninit();
let res = unsafe { libc::tcgetattr(fd, termios.as_mut_ptr()) };
nix::errno::Errno::result(res).expect("Failed to get termios data");
let termios = unsafe { termios.assume_init() };
assert_eq!(termios.c_ospeed, termios.c_ispeed);
termios.c_ospeed as u32
}
impl FromRawFd for TTYPort {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
TTYPort {
fd,
timeout: Duration::from_millis(100),
exclusive: ioctl::tiocexcl(fd).is_ok(),
port_name: None,
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate: get_termios_speed(fd),
}
}
}
impl io::Read for TTYPort {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if let Err(e) = super::poll::wait_read_fd(self.fd, self.timeout) {
return Err(io::Error::from(Error::from(e)));
}
nix::unistd::read(self.fd, buf).map_err(|e| io::Error::from(Error::from(e)))
}
}
impl io::Write for TTYPort {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if let Err(e) = super::poll::wait_write_fd(self.fd, self.timeout) {
return Err(io::Error::from(Error::from(e)));
}
nix::unistd::write(self.fd, buf).map_err(|e| io::Error::from(Error::from(e)))
}
fn flush(&mut self) -> io::Result<()> {
nix::sys::termios::tcdrain(self.fd)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "flush failed"))
}
}
impl SerialPort for TTYPort {
fn name(&self) -> Option<String> {
self.port_name.clone()
}
#[cfg(any(
target_os = "android",
all(
target_os = "linux",
not(any(
target_env = "musl",
target_arch = "powerpc",
target_arch = "powerpc64"
))
)
))]
fn baud_rate(&self) -> Result<u32> {
let termios2 = ioctl::tcgets2(self.fd)?;
assert!(termios2.c_ospeed == termios2.c_ispeed);
Ok(termios2.c_ospeed)
}
#[cfg(any(
target_os = "dragonflybsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
fn baud_rate(&self) -> Result<u32> {
let termios = termios::get_termios(self.fd)?;
let ospeed = unsafe { libc::cfgetospeed(&termios) };
let ispeed = unsafe { libc::cfgetispeed(&termios) };
assert!(ospeed == ispeed);
Ok(ospeed as u32)
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
fn baud_rate(&self) -> Result<u32> {
Ok(self.baud_rate)
}
#[cfg(all(
target_os = "linux",
any(
target_env = "musl",
target_arch = "powerpc",
target_arch = "powerpc64"
)
))]
fn baud_rate(&self) -> Result<u32> {
use self::libc::{
B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000,
B460800, B500000, B576000, B921600,
};
use self::libc::{
B110, B115200, B1200, B134, B150, B1800, B19200, B200, B230400, B2400, B300, B38400,
B4800, B50, B57600, B600, B75, B9600,
};
let termios = termios::get_termios(self.fd)?;
let ospeed = unsafe { libc::cfgetospeed(&termios) };
let ispeed = unsafe { libc::cfgetispeed(&termios) };
assert!(ospeed == ispeed);
let res: u32 = match ospeed {
B50 => 50,
B75 => 75,
B110 => 110,
B134 => 134,
B150 => 150,
B200 => 200,
B300 => 300,
B600 => 600,
B1200 => 1200,
B1800 => 1800,
B2400 => 2400,
B4800 => 4800,
B9600 => 9600,
B19200 => 19_200,
B38400 => 38_400,
B57600 => 57_600,
B115200 => 115_200,
B230400 => 230_400,
B460800 => 460_800,
B500000 => 500_000,
B576000 => 576_000,
B921600 => 921_600,
B1000000 => 1_000_000,
B1152000 => 1_152_000,
B1500000 => 1_500_000,
B2000000 => 2_000_000,
B2500000 => 2_500_000,
B3000000 => 3_000_000,
B3500000 => 3_500_000,
B4000000 => 4_000_000,
_ => unreachable!(),
};
Ok(res)
}
fn data_bits(&self) -> Result<DataBits> {
let termios = termios::get_termios(self.fd)?;
match termios.c_cflag & libc::CSIZE {
libc::CS8 => Ok(DataBits::Eight),
libc::CS7 => Ok(DataBits::Seven),
libc::CS6 => Ok(DataBits::Six),
libc::CS5 => Ok(DataBits::Five),
_ => Err(Error::new(
ErrorKind::Unknown,
"Invalid data bits setting encountered",
)),
}
}
fn flow_control(&self) -> Result<FlowControl> {
let termios = termios::get_termios(self.fd)?;
if termios.c_cflag & libc::CRTSCTS == libc::CRTSCTS {
Ok(FlowControl::Hardware)
} else if termios.c_iflag & (libc::IXON | libc::IXOFF) == (libc::IXON | libc::IXOFF) {
Ok(FlowControl::Software)
} else {
Ok(FlowControl::None)
}
}
fn parity(&self) -> Result<Parity> {
let termios = termios::get_termios(self.fd)?;
if termios.c_cflag & libc::PARENB == libc::PARENB {
if termios.c_cflag & libc::PARODD == libc::PARODD {
Ok(Parity::Odd)
} else {
Ok(Parity::Even)
}
} else {
Ok(Parity::None)
}
}
fn stop_bits(&self) -> Result<StopBits> {
let termios = termios::get_termios(self.fd)?;
if termios.c_cflag & libc::CSTOPB == libc::CSTOPB {
Ok(StopBits::Two)
} else {
Ok(StopBits::One)
}
}
fn timeout(&self) -> Duration {
self.timeout
}
#[cfg(any(
target_os = "android",
target_os = "dragonflybsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux"
))]
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_termios(self.fd, &termios)
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
ioctl::iossiospeed(self.fd, &(baud_rate as libc::speed_t))?;
self.baud_rate = baud_rate;
Ok(())
}
fn set_flow_control(&mut self, flow_control: FlowControl) -> Result<()> {
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);
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
return termios::set_termios(self.fd, &termios);
}
fn set_parity(&mut self, parity: Parity) -> Result<()> {
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);
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
return termios::set_termios(self.fd, &termios);
}
fn set_data_bits(&mut self, data_bits: DataBits) -> Result<()> {
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);
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
return termios::set_termios(self.fd, &termios);
}
fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<()> {
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);
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
return termios::set_termios(self.fd, &termios);
}
fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
self.timeout = timeout;
Ok(())
}
fn write_request_to_send(&mut self, level: bool) -> Result<()> {
self.set_pin(SerialLines::REQUEST_TO_SEND, level)
}
fn write_data_terminal_ready(&mut self, level: bool) -> Result<()> {
self.set_pin(SerialLines::DATA_TERMINAL_READY, level)
}
fn read_clear_to_send(&mut self) -> Result<bool> {
self.read_pin(SerialLines::CLEAR_TO_SEND)
}
fn read_data_set_ready(&mut self) -> Result<bool> {
self.read_pin(SerialLines::DATA_SET_READY)
}
fn read_ring_indicator(&mut self) -> Result<bool> {
self.read_pin(SerialLines::RING)
}
fn read_carrier_detect(&mut self) -> Result<bool> {
self.read_pin(SerialLines::DATA_CARRIER_DETECT)
}
fn bytes_to_read(&self) -> Result<u32> {
ioctl::fionread(self.fd)
}
fn bytes_to_write(&self) -> Result<u32> {
ioctl::tiocoutq(self.fd)
}
fn clear(&self, buffer_to_clear: ClearBuffer) -> Result<()> {
let buffer_id = match buffer_to_clear {
ClearBuffer::Input => libc::TCIFLUSH,
ClearBuffer::Output => libc::TCOFLUSH,
ClearBuffer::All => libc::TCIOFLUSH,
};
let res = unsafe { nix::libc::tcflush(self.fd, buffer_id) };
nix::errno::Errno::result(res)
.map(|_| ())
.map_err(|e| e.into())
}
fn try_clone(&self) -> Result<Box<dyn SerialPort>> {
match self.try_clone_native() {
Ok(p) => Ok(Box::new(p)),
Err(e) => Err(e),
}
}
fn set_break(&self) -> Result<()> {
ioctl::tiocsbrk(self.fd)
}
fn clear_break(&self) -> Result<()> {
ioctl::tioccbrk(self.fd)
}
}
#[test]
fn test_ttyport_into_raw_fd() {
#![allow(unused_variables)]
let (master, slave) = TTYPort::pair().expect("Unable to create ptty pair");
let master_fd = master.into_raw_fd();
let mut termios = MaybeUninit::uninit();
let res = unsafe { nix::libc::tcgetattr(master_fd, termios.as_mut_ptr()) };
if res != 0 {
close(master_fd);
panic!("tcgetattr on the master port failed");
}
let slave_fd = slave.into_raw_fd();
let res = unsafe { nix::libc::tcgetattr(slave_fd, termios.as_mut_ptr()) };
if res != 0 {
close(slave_fd);
panic!("tcgetattr on the master port failed");
}
close(master_fd);
close(slave_fd);
}