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

Don't silently fail if defmt data isn't available/correct #524

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,9 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
args.flash_args.monitor_baud.unwrap_or(default_baud),
args.flash_args.log_format,
)
.into_diagnostic()?;
} else {
Ok(())
}

Ok(())
}

fn build(
Expand Down
5 changes: 2 additions & 3 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,9 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
args.flash_args.monitor_baud.unwrap_or(default_baud),
args.flash_args.log_format,
)
.into_diagnostic()?;
} else {
Ok(())
}

Ok(())
}

fn save_image(args: SaveImageArgs) -> Result<()> {
Expand Down
3 changes: 0 additions & 3 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
args.connect_args.baud.unwrap_or(default_baud),
args.log_format,
)
.into_diagnostic()?;

Ok(())
}

/// Convert the provided firmware image from ELF to binary
Expand Down
27 changes: 17 additions & 10 deletions espflash/src/cli/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ pub fn monitor(
pid: u16,
baud: u32,
log_format: LogFormat,
) -> serialport::Result<()> {
) -> miette::Result<()> {
println!("Commands:");
println!(" CTRL+R Reset chip");
println!(" CTRL+C Exit");
println!();

// Explicitly set the baud rate when starting the serial monitor, to allow using
// different rates for flashing.
serial.serial_port_mut().set_baud_rate(baud)?;
serial
.serial_port_mut()
.set_timeout(Duration::from_millis(5))?;
.set_baud_rate(baud)
.into_diagnostic()?;
serial
.serial_port_mut()
.set_timeout(Duration::from_millis(5))
.into_diagnostic()?;

// We are in raw mode until `_raw_mode` is dropped (ie. this function returns).
let _raw_mode = RawModeGuard::new();
Expand All @@ -90,7 +94,7 @@ pub fn monitor(
let mut stdout = ResolvingPrinter::new(elf, stdout.lock());

let mut parser: Box<dyn InputParser> = match log_format {
LogFormat::Defmt => Box::new(parser::esp_defmt::EspDefmt::new(elf)),
LogFormat::Defmt => Box::new(parser::esp_defmt::EspDefmt::new(elf)?),
LogFormat::Serial => Box::new(parser::serial::Serial),
};

Expand All @@ -100,30 +104,33 @@ pub fn monitor(
Ok(count) => Ok(count),
Err(e) if e.kind() == ErrorKind::TimedOut => Ok(0),
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
err => err,
err => err.into_diagnostic(),
}?;

parser.feed(&buff[0..read_count], &mut stdout);

// Don't forget to flush the writer!
stdout.flush().ok();

if poll(Duration::from_secs(0))? {
if let Event::Key(key) = read()? {
if poll(Duration::from_secs(0)).into_diagnostic()? {
if let Event::Key(key) = read().into_diagnostic()? {
if key.modifiers.contains(KeyModifiers::CONTROL) {
match key.code {
KeyCode::Char('c') => break,
KeyCode::Char('r') => {
reset_after_flash(&mut serial, pid)?;
reset_after_flash(&mut serial, pid).into_diagnostic()?;
continue;
}
_ => {}
}
}

if let Some(bytes) = handle_key_event(key) {
serial.serial_port_mut().write_all(&bytes)?;
serial.serial_port_mut().flush()?;
serial
.serial_port_mut()
.write_all(&bytes)
.into_diagnostic()?;
serial.serial_port_mut().flush().into_diagnostic()?;
}
}
}
Expand Down
52 changes: 27 additions & 25 deletions espflash/src/cli/monitor/parser/esp_defmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crossterm::{
QueueableCommand,
};
use defmt_decoder::{Frame, Table};
use miette::{bail, miette};

use crate::cli::monitor::parser::InputParser;

Expand Down Expand Up @@ -79,31 +80,37 @@ impl FrameDelimiter {

pub struct EspDefmt {
delimiter: FrameDelimiter,
table: Option<Table>,
table: Table,
}

impl EspDefmt {
fn load_table(elf: Option<&[u8]>) -> Option<Table> {
// Load symbols from the ELF file (if provided) and initialize the context.
Table::parse(elf?).ok().flatten().and_then(|table| {
let encoding = table.encoding();

// We only support rzcobs encoding because it is the only way to multiplex
// a defmt stream and an ASCII log stream over the same serial port.
if encoding == defmt_decoder::Encoding::Rzcobs {
Some(table)
} else {
log::warn!("Unsupported defmt encoding: {:?}", encoding);
None
}
})
/// Loads symbols from the ELF file (if provided) and initializes the context.
fn load_table(elf: Option<&[u8]>) -> miette::Result<Table> {
bugadani marked this conversation as resolved.
Show resolved Hide resolved
let Some(elf) = elf else {
bail!("The defmt log format can not be used without an .elf file");
bugadani marked this conversation as resolved.
Show resolved Hide resolved
};

let table = Table::parse(elf).map_err(|e| miette!(e.to_string()))?;
let Some(table) = table else {
bail!("No defmt data was found in the .elf file");
};

let encoding = table.encoding();

// We only support rzcobs encoding because it is the only way to multiplex
// a defmt stream and an ASCII log stream over the same serial port.
if encoding == defmt_decoder::Encoding::Rzcobs {
Ok(table)
} else {
bail!("Unsupported defmt encoding: {:?}", encoding)
}
}

pub fn new(elf: Option<&[u8]>) -> Self {
Self {
pub fn new(elf: Option<&[u8]>) -> miette::Result<Self> {
Self::load_table(elf).map(|table| Self {
delimiter: FrameDelimiter::new(),
table: Self::load_table(elf),
}
table,
})
}

fn handle_raw(bytes: &[u8], out: &mut dyn Write) {
Expand Down Expand Up @@ -143,12 +150,7 @@ impl EspDefmt {

impl InputParser for EspDefmt {
fn feed(&mut self, bytes: &[u8], out: &mut dyn Write) {
let Some(table) = self.table.as_mut() else {
Self::handle_raw(bytes, out);
return;
};

let mut decoder = table.new_stream_decoder();
let mut decoder = self.table.new_stream_decoder();

self.delimiter.feed(bytes, |frame| match frame {
FrameKind::Defmt(frame) => {
Expand Down