From b38b5ef32782ee38c343ef6b6e7daf58a3420530 Mon Sep 17 00:00:00 2001 From: Jeevitha Kannan K S Date: Thu, 5 Dec 2024 21:00:04 +0530 Subject: [PATCH] Use serial2 crate instead of serial (#6411) * Use serial2 crate instead of serial * Remove mutex and add back the API methods --- Cargo.lock | 67 +++++++---------------------------------------- mux/Cargo.toml | 2 +- mux/src/domain.rs | 2 +- pty/Cargo.toml | 2 +- pty/src/serial.rs | 53 ++++++++++++++++++------------------- 5 files changed, 38 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 118902fa1ce..1708d059f4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2769,15 +2769,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "ioctl-rs" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7970510895cee30b3e9128319f2cefd4bde883a39f38baa279567ba3a7eb97d" -dependencies = [ - "libc", -] - [[package]] name = "ipnet" version = "2.10.1" @@ -3429,7 +3420,7 @@ dependencies = [ "promise", "rangeset", "serde", - "serial", + "serial2", "shell-words", "smol", "terminfo", @@ -4141,7 +4132,7 @@ dependencies = [ "nix 0.28.0", "serde", "serde_derive", - "serial", + "serial2", "shared_library", "shell-words", "smol", @@ -4946,45 +4937,14 @@ dependencies = [ ] [[package]] -name = "serial" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1237a96570fc377c13baa1b88c7589ab66edced652e43ffb17088f003db3e86" -dependencies = [ - "serial-core", - "serial-unix", - "serial-windows", -] - -[[package]] -name = "serial-core" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f46209b345401737ae2125fe5b19a77acce90cd53e1658cda928e4fe9a64581" -dependencies = [ - "libc", -] - -[[package]] -name = "serial-unix" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f03fbca4c9d866e24a459cbca71283f545a37f8e3e002ad8c70593871453cab7" -dependencies = [ - "ioctl-rs", - "libc", - "serial-core", - "termios 0.2.2", -] - -[[package]] -name = "serial-windows" -version = "0.4.0" +name = "serial2" +version = "0.2.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15c6d3b776267a75d31bbdfd5d36c0ca051251caafc285827052bc53bcdc8162" +checksum = "8cd0c773455b60177d1abe4c739cbfa316c4f2f0ef37465befcb72e8a15cdd02" dependencies = [ + "cfg-if", "libc", - "serial-core", + "winapi", ] [[package]] @@ -5478,15 +5438,6 @@ dependencies = [ "phf_codegen", ] -[[package]] -name = "termios" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a" -dependencies = [ - "libc", -] - [[package]] name = "termios" version = "0.3.3" @@ -5537,7 +5488,7 @@ dependencies = [ "siphasher", "tempfile", "terminfo", - "termios 0.3.3", + "termios", "thiserror", "ucd-trie", "unicode-segmentation", @@ -6335,7 +6286,7 @@ dependencies = [ "smol", "tabout", "tempfile", - "termios 0.3.3", + "termios", "termwiz", "termwiz-funcs", "textwrap", diff --git a/mux/Cargo.toml b/mux/Cargo.toml index f56c114ee74..583c37724b1 100644 --- a/mux/Cargo.toml +++ b/mux/Cargo.toml @@ -36,7 +36,7 @@ procinfo = { path = "../procinfo" } promise = { path = "../promise" } rangeset = { path = "../rangeset" } serde = {version="1.0", features = ["rc", "derive"]} -serial = "0.4" +serial2 = "0.2" shell-words = "1.1" smol = "2.0" terminfo = "0.9" diff --git a/mux/src/domain.rs b/mux/src/domain.rs index 3cb3767d5c3..763f8e6ab02 100644 --- a/mux/src/domain.rs +++ b/mux/src/domain.rs @@ -237,7 +237,7 @@ impl LocalDomain { let port = serial_domain.port.as_ref().unwrap_or(&serial_domain.name); let mut serial = portable_pty::serial::SerialTty::new(&port); if let Some(baud) = serial_domain.baud { - serial.set_baud_rate(serial::BaudRate::from_speed(baud)); + serial.set_baud_rate(baud as u32); } let pty_system = Box::new(serial); Ok(Self::with_pty_system(&serial_domain.name, pty_system)) diff --git a/pty/Cargo.toml b/pty/Cargo.toml index 46e2f36e497..a2911e7376e 100644 --- a/pty/Cargo.toml +++ b/pty/Cargo.toml @@ -18,7 +18,7 @@ nix = {version="0.28", features=["term", "fs"]} shell-words = "1.1" serde_derive = {version="1.0", optional=true} serde = {version="1.0", optional=true} -serial = "0.4" +serial2 = "0.2" [features] default = [] diff --git a/pty/src/serial.rs b/pty/src/serial.rs index 7ff799deaf6..948ba108d37 100644 --- a/pty/src/serial.rs +++ b/pty/src/serial.rs @@ -11,22 +11,20 @@ use crate::{ }; use anyhow::{ensure, Context}; use filedescriptor::FileDescriptor; -use serial::{ - BaudRate, CharSize, FlowControl, Parity, PortSettings, SerialPort, StopBits, SystemPort, -}; +use serial2::{CharSize, FlowControl, Parity, SerialPort, StopBits}; use std::cell::RefCell; use std::ffi::{OsStr, OsString}; use std::io::{Read, Result as IoResult, Write}; #[cfg(unix)] use std::path::PathBuf; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::time::Duration; -type Handle = Arc>; +type Handle = Arc; pub struct SerialTty { port: OsString, - baud: BaudRate, + baud: u32, char_size: CharSize, parity: Parity, stop_bits: StopBits, @@ -37,15 +35,15 @@ impl SerialTty { pub fn new + ?Sized>(port: &T) -> Self { Self { port: port.as_ref().to_owned(), - baud: BaudRate::Baud9600, + baud: 9600, char_size: CharSize::Bits8, - parity: Parity::ParityNone, - stop_bits: StopBits::Stop1, - flow_control: FlowControl::FlowSoftware, + parity: Parity::None, + stop_bits: StopBits::One, + flow_control: FlowControl::XonXoff, } } - pub fn set_baud_rate(&mut self, baud: BaudRate) { + pub fn set_baud_rate(&mut self, baud: u32) { self.baud = baud; } @@ -68,27 +66,28 @@ impl SerialTty { impl PtySystem for SerialTty { fn openpty(&self, _size: PtySize) -> anyhow::Result { - let mut port = serial::open(&self.port) + let mut port = SerialPort::open(&self.port, self.baud) .with_context(|| format!("openpty on serial port {:?}", self.port))?; - let settings = PortSettings { - baud_rate: self.baud, - char_size: self.char_size, - parity: self.parity, - stop_bits: self.stop_bits, - flow_control: self.flow_control, - }; - log::debug!("serial settings: {:#?}", settings); - port.configure(&settings)?; + let mut settings = port.get_configuration()?; + settings.set_raw(); + settings.set_baud_rate(self.baud)?; + settings.set_char_size(self.char_size); + settings.set_flow_control(self.flow_control); + settings.set_parity(self.parity); + settings.set_stop_bits(self.stop_bits); + log::debug!("serial settings: {:#?}", port.get_configuration()); + port.set_configuration(&settings)?; // The timeout needs to be rather short because, at least on Windows, // a read with a long timeout will block a concurrent write from // happening. In wezterm we tend to have a thread looping on read // while writes happen occasionally from the gui thread, and if we // make this timeout too long we can block the gui thread. - port.set_timeout(Duration::from_millis(50))?; + port.set_read_timeout(Duration::from_millis(50))?; + port.set_write_timeout(Duration::from_millis(50))?; - let port: Handle = Arc::new(Mutex::new(port)); + let port: Handle = Arc::new(port); Ok(PtyPair { slave: Box::new(Slave { @@ -149,7 +148,7 @@ impl Child for SerialChild { loop { std::thread::sleep(Duration::from_secs(5)); - let mut port = self.port.lock().unwrap(); + let port = &self.port; if let Err(err) = port.read_cd() { log::error!("Error reading carrier detect: {:#}", err); return Ok(ExitStatus::with_exit_code(1)); @@ -201,11 +200,11 @@ struct MasterWriter { impl Write for MasterWriter { fn write(&mut self, buf: &[u8]) -> Result { - self.port.lock().unwrap().write(buf) + self.port.write(buf) } fn flush(&mut self) -> Result<(), std::io::Error> { - self.port.lock().unwrap().flush() + self.port.flush() } } @@ -224,7 +223,7 @@ impl MasterPty for Master { // We rely on the fact that SystemPort implements the traits // that expose the underlying file descriptor, and that direct // reads from that return the raw data that we want - let fd = FileDescriptor::dup(&*self.port.lock().unwrap())?; + let fd = FileDescriptor::dup(&*self.port)?; Ok(Box::new(Reader { fd })) }