Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify log command to work under MacOS & Linux #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ prettytable-rs = "0.8"
regex = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serialport = "3.3"
signal-hook = "0.1"
structopt = "0.3"
tempfile = "3"
Expand Down
7 changes: 2 additions & 5 deletions src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ pub use self::output::{Output, OutputMap, OutputStream};

use anyhow::Result;
use std::{
fs::File,
io::prelude::*,
io::Read,
ops::{Generator, GeneratorState},
path::PathBuf,
pin::Pin,
thread,
};

type ParserFn = fn(&[Output]) -> Pin<Box<dyn Generator<u8, Yield = (), Return = Result<!>> + '_>>;

/// Runs log capture thread.
pub fn capture(input: PathBuf, outputs: Vec<Output>, parser: ParserFn) {
pub fn capture(input: Box<dyn Read + Send>, outputs: Vec<Output>, parser: ParserFn) {
thread::spawn(move || {
(|| -> Result<()> {
let input = File::open(input)?;
let mut parser = Box::pin(parser(&outputs));
for byte in input.bytes() {
let byte = byte?;
Expand Down
6 changes: 3 additions & 3 deletions src/probe/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ pub fn log_swo_serial(
let mut gdb = spawn_command(gdb_script_command(&config, None, script.path()))?;

let (pipe, packet) = gdb_script_wait(&signals, pipe)?;
setup_serial_endpoint(&signals, serial_endpoint, config_log_swo.baud_rate)?;
exhaust_fifo(serial_endpoint)?;
log::capture(serial_endpoint.into(), log::Output::open_all(&outputs)?, log::swo::parser);
let port = setup_serial_endpoint(serial_endpoint, config_log_swo.baud_rate)?;
exhaust_fifo(&port)?;
log::capture(port, log::Output::open_all(&outputs)?, log::swo::parser);
begin_log_output(color);
gdb_script_continue(&signals, pipe, packet)?;

Expand Down
10 changes: 3 additions & 7 deletions src/probe/jlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,9 @@ pub fn log_dso_serial(
let mut gdb = spawn_command(gdb_script_command(&config, None, script.path()))?;

let (pipe, packet) = gdb_script_wait(&signals, pipe)?;
setup_serial_endpoint(&signals, &config_log_dso.serial_endpoint, config_log_dso.baud_rate)?;
exhaust_fifo(&config_log_dso.serial_endpoint)?;
log::capture(
config_log_dso.serial_endpoint.clone().into(),
log::Output::open_all(&outputs)?,
log::dso::parser,
);
let port = setup_serial_endpoint(&config_log_dso.serial_endpoint, config_log_dso.baud_rate)?;
exhaust_fifo(&port)?;
log::capture(port, log::Output::open_all(&outputs)?, log::dso::parser);
begin_log_output(color);
gdb_script_continue(&signals, pipe, packet)?;

Expand Down
16 changes: 8 additions & 8 deletions src/probe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ansi_term::Color::Cyan;
use anyhow::{anyhow, bail, Error, Result};
use drone_config as config;
use serde::{Deserialize, Serialize};
use serialport::{posix::TTYPort, SerialPortSettings};
use signal_hook::iterator::Signals;
use std::{
convert::TryFrom,
Expand All @@ -23,6 +24,7 @@ use std::{
path::{Path, PathBuf},
process::{Command, Stdio},
thread,
time::Duration,
};

/// An `enum` of all supported debug probes.
Expand Down Expand Up @@ -136,14 +138,12 @@ pub fn log(probe: Probe, log: Log) -> Option<LogFn> {
}
}

/// Configures the endpoint with `stty` command.
pub fn setup_serial_endpoint(signals: &Signals, endpoint: &str, baud_rate: u32) -> Result<()> {
let mut stty = Command::new("stty");
stty.arg(format!("--file={}", endpoint));
stty.arg("speed");
stty.arg(format!("{}", baud_rate));
stty.arg("raw");
block_with_signals(signals, true, || run_command(stty))
/// Opens and configures the serial port endpoint.
pub fn setup_serial_endpoint(endpoint: &str, baud_rate: u32) -> Result<Box<TTYPort>> {
let mut settings: SerialPortSettings = Default::default();
settings.baud_rate = baud_rate;
settings.timeout = Duration::new(10, 0);
Ok(Box::new(TTYPort::open(Path::new(endpoint), &settings)?))
}

/// Runs a GDB server.
Expand Down
15 changes: 8 additions & 7 deletions src/probe/openocd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
use anyhow::Result;
use drone_config as config;
use signal_hook::iterator::Signals;
use std::process::Command;
use std::{fs::File, io::Read, process::Command};
use tempfile::tempdir_in;

/// Runs `drone reset` command.
Expand Down Expand Up @@ -97,16 +97,17 @@ pub fn log_swo(
let dir = tempdir_in(temp_dir())?;
let pipe = make_fifo(&dir, "pipe")?;
let ports = outputs.iter().flat_map(|output| output.ports.iter().copied()).collect();
let input;
let input: Box<dyn Read + Send>;
let script;
if let Some(serial_endpoint) = &config_log_swo.serial_endpoint {
setup_serial_endpoint(&signals, serial_endpoint, config_log_swo.baud_rate)?;
exhaust_fifo(serial_endpoint)?;
input = serial_endpoint.into();
let port = setup_serial_endpoint(serial_endpoint, config_log_swo.baud_rate)?;
exhaust_fifo(&port)?;
input = port;
script = registry.openocd_swo(&config, &ports, reset, &pipe, None)?;
} else {
input = make_fifo(&dir, "input")?;
script = registry.openocd_swo(&config, &ports, reset, &pipe, Some(&input))?;
let fifo_name = make_fifo(&dir, "input")?;
script = registry.openocd_swo(&config, &ports, reset, &pipe, Some(&fifo_name))?;
input = Box::new(File::open(fifo_name)?);
}
log::capture(input, log::Output::open_all(&outputs)?, log::swo::parser);
let mut gdb = spawn_command(gdb_script_command(&config, None, script.path()))?;
Expand Down
19 changes: 14 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ use crate::color::Color;
use ansi_term::Color::Red;
use anyhow::{bail, Result};
use serde::{de, ser};
use serialport::posix::TTYPort;
use signal_hook::{iterator::Signals, SIGINT, SIGQUIT, SIGTERM};
use std::{
env,
ffi::CString,
fs::OpenOptions,
fs::File,
io::{prelude::*, ErrorKind},
os::unix::{ffi::OsStrExt, io::AsRawFd, process::CommandExt},
os::unix::{
ffi::OsStrExt,
io::{AsRawFd, FromRawFd},
process::CommandExt,
},
path::PathBuf,
process::{exit, Child, Command},
sync::mpsc::{channel, RecvTimeoutError},
Expand Down Expand Up @@ -120,9 +125,13 @@ pub fn make_fifo(dir: &TempDir, name: &str) -> Result<PathBuf> {
}

/// Consumes all remaining data in the fifo.
pub fn exhaust_fifo(path: &str) -> Result<()> {
let mut fifo = OpenOptions::new().read(true).open(path)?;
unsafe { libc::fcntl(fifo.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK) };
pub fn exhaust_fifo(port: &TTYPort) -> Result<()> {
let mut fifo;
unsafe {
let fifo_fd = libc::dup(port.as_raw_fd());
libc::fcntl(fifo_fd, libc::F_SETFL, libc::O_NONBLOCK);
fifo = File::from_raw_fd(fifo_fd);
}
let mut bytes = [0_u8; 1024];
loop {
match fifo.read(&mut bytes) {
Expand Down