From dfdbc74b4c880adbad90816f3d661702205d98dc Mon Sep 17 00:00:00 2001 From: Viktor Sonesten Date: Tue, 11 Jan 2022 16:54:29 +0100 Subject: [PATCH] sources/tty: apply user-supplied baud rate Before this commit, dev in --serial /path/to/dev would always be configured for 115200 B/s, even if the user supplied another rate in the manifest via tpiu_baud, e.g. 9600 B/s. A baud rate that converts to a nix::sys::termios::BaudRate that isn't B0 must be supplied. Ideally we should CI this, but that requires a CI refactor so that --serial can be specified. This is non-trivial, and we better handle #77 first. --- CHANGELOG.md | 13 +++++++++++++ cargo-rtic-scope/src/main.rs | 3 ++- cargo-rtic-scope/src/sources/tty.rs | 14 +++++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bc4aeb..04f9fac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `/docs/`, a submodule that contains the overarching documentation of RTIC Scope, which is rendered at [the organization profile](https://github.com/rtic-scope). +### Changed +- On `--serial /path/to/dev`, `dev` will no longer unconditionally configure for 115200 B/s; the baud rate specified with `tpiu_baud` in the `[package.metadata.rtic-scope]` block in `Cargo.toml` will instead be applied. + For example, `tpiu_baud = 9600` will configure `dev` for 9600 B/s. + Valid baud rates are listed in [`nix::sys::termios::BaudRate`](https://docs.rs/nix/0.23.1/nix/sys/termios/enum.BaudRate.html), with the exception of `B0`. + +### Deprecated + +### Removed + +### Fixed + +### Security + ## [0.3.0] - 2022-01-05 Initial release tracked by this changelog. diff --git a/cargo-rtic-scope/src/main.rs b/cargo-rtic-scope/src/main.rs index 2444fef..7f841b8 100644 --- a/cargo-rtic-scope/src/main.rs +++ b/cargo-rtic-scope/src/main.rs @@ -708,7 +708,8 @@ async fn trace( let trace_source: Box = if let Some(dev) = &opts.serial { Box::new(sources::TTYSource::new( - sources::tty::configure(dev).with_context(|| format!("Failed to configure {}", dev))?, + sources::tty::configure(dev, manip.tpiu_baud) + .with_context(|| format!("Failed to configure {}", dev))?, &manip, )) } else { diff --git a/cargo-rtic-scope/src/sources/tty.rs b/cargo-rtic-scope/src/sources/tty.rs index c383c5a..5442bec 100644 --- a/cargo-rtic-scope/src/sources/tty.rs +++ b/cargo-rtic-scope/src/sources/tty.rs @@ -34,13 +34,21 @@ mod ioctl { /// /// Effectively mirrors the behavior of /// ``` -/// $ screen /dev/ttyUSB3 115200 +/// $ screen /dev/ttyUSB3 /// ``` /// assuming that `device` is `/dev/ttyUSB3`. /// /// TODO ensure POSIX compliance, see termios(3) /// TODO We are currently using line disciple 0. Is that correct? -pub fn configure(device: &str) -> Result { +pub fn configure(device: &str, baud_rate: u32) -> Result { + // ensure a valid baud rate was requested + let baud_rate: BaudRate = baud_rate + .try_into() + .map_err(|_| SourceError::SetupError(format!("{} is not a valid baud rate", baud_rate)))?; + if baud_rate == BaudRate::B0 { + return Err(SourceError::SetupError("baud rate cannot be 0".to_string())); + } + let file = fs::OpenOptions::new() .read(true) .open(&device) @@ -143,7 +151,7 @@ pub fn configure(device: &str) -> Result { | LocalFlags::PENDIN | LocalFlags::NOFLSH); - termios::cfsetspeed(&mut settings, BaudRate::B115200).map_err(|e| { + termios::cfsetspeed(&mut settings, baud_rate).map_err(|e| { SourceError::SetupError(format!( "Failed to configure {} baud rate: cfsetspeed = {}", device, e