Skip to content

Commit

Permalink
sources/tty: apply user-supplied baud rate
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tmplt committed Jan 11, 2022
1 parent 20d4b1c commit dfdbc74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion cargo-rtic-scope/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ async fn trace(

let trace_source: Box<dyn sources::Source> = 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 {
Expand Down
14 changes: 11 additions & 3 deletions cargo-rtic-scope/src/sources/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ mod ioctl {
///
/// Effectively mirrors the behavior of
/// ```
/// $ screen /dev/ttyUSB3 115200
/// $ screen /dev/ttyUSB3 <baud rate>
/// ```
/// 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<fs::File, SourceError> {
pub fn configure(device: &str, baud_rate: u32) -> Result<fs::File, SourceError> {
// 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)
Expand Down Expand Up @@ -143,7 +151,7 @@ pub fn configure(device: &str) -> Result<fs::File, SourceError> {
| 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
Expand Down

0 comments on commit dfdbc74

Please sign in to comment.