Skip to content

Commit 5b1027a

Browse files
Check monitor args (#842)
* feat: Check monitor args * docs: Udpate changelog * style: Fix format
1 parent fd795df commit 5b1027a

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- `espflash` no longer allows flashing a too-big partition table (#830)
2525
- Allow specifying a partition label for `write-bin`, add `--partition-table`. (#828)
2626
- `--mmu-page-size` parameter for `flash` and `save-image` (#835)
27+
- Run some arguments checks for monitoring flags. (#842)
2728

2829
### Changed
2930

cargo-espflash/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use std::{
77
use cargo_metadata::{Message, MetadataCommand};
88
use clap::{Args, CommandFactory, Parser, Subcommand};
99
use espflash::{
10-
cli::{self, config::Config, monitor::monitor, *},
10+
cli::{
11+
self,
12+
config::Config,
13+
monitor::{check_monitor_args, monitor},
14+
*,
15+
},
1116
flasher::FlashSize,
1217
logging::initialize_logger,
1318
targets::{Chip, XtalFrequency},
@@ -313,6 +318,11 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
313318
// Read the ELF data from the build path and load it to the target.
314319
let elf_data = fs::read(build_ctx.artifact_path.clone()).into_diagnostic()?;
315320

321+
let mut monitor_args = args.flash_args.monitor_args;
322+
monitor_args.elf = Some(build_ctx.artifact_path.clone());
323+
324+
check_monitor_args(&args.flash_args.monitor, &monitor_args)?;
325+
316326
print_board_info(&mut flasher)?;
317327
ensure_chip_compatibility(chip, Some(elf_data.as_slice()))?;
318328

@@ -348,7 +358,6 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
348358

349359
if args.flash_args.monitor {
350360
let pid = flasher.usb_pid();
351-
let mut monitor_args = args.flash_args.monitor_args;
352361

353362
// The 26MHz ESP32-C2's need to be treated as a special case.
354363
if chip == Chip::Esp32c2

espflash/src/bin/espflash.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ use std::{fs, path::PathBuf};
22

33
use clap::{Args, CommandFactory, Parser, Subcommand};
44
use espflash::{
5-
cli::{self, config::Config, monitor::monitor, *},
5+
cli::{
6+
self,
7+
config::Config,
8+
monitor::{check_monitor_args, monitor},
9+
*,
10+
},
611
flasher::FlashSize,
712
logging::initialize_logger,
813
targets::{Chip, XtalFrequency},
@@ -203,6 +208,10 @@ pub fn erase_parts(args: ErasePartsArgs, config: &Config) -> Result<()> {
203208
}
204209

205210
fn flash(args: FlashArgs, config: &Config) -> Result<()> {
211+
let mut monitor_args = args.flash_args.monitor_args;
212+
monitor_args.elf = Some(args.image.clone());
213+
check_monitor_args(&args.flash_args.monitor, &monitor_args)?;
214+
206215
let mut flasher = connect(
207216
&args.connect_args,
208217
config,
@@ -255,7 +264,6 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
255264

256265
if args.flash_args.monitor {
257266
let pid = flasher.usb_pid();
258-
let mut monitor_args = args.flash_args.monitor_args;
259267

260268
// The 26MHz ESP32-C2's need to be treated as a special case.
261269
if chip == Chip::Esp32c2

espflash/src/cli/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use serialport::{FlowControl, SerialPortInfo, SerialPortType, UsbPortInfo};
2929

3030
use self::{
3131
config::Config,
32-
monitor::{monitor, LogFormat},
32+
monitor::{check_monitor_args, monitor, LogFormat},
3333
};
3434
use crate::{
3535
connection::reset::{ResetAfterOperation, ResetBeforeOperation},
@@ -1040,6 +1040,9 @@ pub fn make_flash_data(
10401040

10411041
/// Write a binary to the flash memory of a target device
10421042
pub fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> {
1043+
// Check monitor arguments
1044+
check_monitor_args(&args.monitor, &args.monitor_args)?;
1045+
10431046
// Load the file to be flashed
10441047
let mut f = File::open(&args.file).into_diagnostic()?;
10451048

espflash/src/cli/monitor/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,34 @@ fn handle_key_event(key_event: KeyEvent) -> Option<Vec<u8>> {
259259

260260
key_str.map(|slice| slice.into())
261261
}
262+
263+
pub fn check_monitor_args(monitor: &bool, monitor_args: &MonitorConfigArgs) -> Result<()> {
264+
// Check if any monitor args are provided but monitor flag isn't set
265+
if !monitor
266+
&& (monitor_args.elf.is_some()
267+
|| monitor_args.log_format.is_some()
268+
|| monitor_args.output_format.is_some()
269+
|| monitor_args.processors.is_some()
270+
|| monitor_args.non_interactive
271+
|| monitor_args.no_reset
272+
|| monitor_args.monitor_baud != 115_200)
273+
{
274+
warn!("Monitor options were provided, but `--monitor/-M` flag isn't set. These options will be ignored.");
275+
}
276+
277+
// Check if log-format is used with serial but output-format is specified
278+
if let Some(LogFormat::Serial) = monitor_args.log_format {
279+
if monitor_args.output_format.is_some() {
280+
warn!("Output format specified but log format is serial. The output format option will be ignored.");
281+
}
282+
}
283+
284+
// Check if log-format is defmt but no ELF file is provided
285+
if let Some(LogFormat::Defmt) = monitor_args.log_format {
286+
if monitor_args.elf.is_none() {
287+
warn!("Log format `defmt` requires an ELF file. Please provide one with the `--elf` option.");
288+
}
289+
}
290+
291+
Ok(())
292+
}

0 commit comments

Comments
 (0)