Skip to content

Commit 11a5b7b

Browse files
authored
Minor error handling and function naming improvements (#795)
* Remove unnecessary `Result` from return type, don't leak constants from `connection` module * Improve error handling when receiving an invalid command response type * Update `CHANGELOG.md`
1 parent acf2034 commit 11a5b7b

File tree

8 files changed

+39
-25
lines changed

8 files changed

+39
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- `board-info` now prints `Security information`. (#758)
2323
- The `command`, `elf` and `error` modules are no longer public (#772)
2424
- `write-bin` now works for files whose lengths are not divisible by 4 (#780, #788)
25+
- `get_usb_pid` is now `usb_pid` and no longer needlessly returns a `Result` (#795)
2526

2627
### Fixed
2728

cargo-espflash/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
354354
}
355355

356356
if args.flash_args.monitor {
357-
let pid = flasher.get_usb_pid()?;
357+
let pid = flasher.usb_pid();
358358
let mut monitor_args = args.flash_args.monitor_args;
359359

360360
// The 26MHz ESP32-C2's need to be treated as a special case.

espflash/src/bin/espflash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
267267
}
268268

269269
if args.flash_args.monitor {
270-
let pid = flasher.get_usb_pid()?;
270+
let pid = flasher.usb_pid();
271271
let mut monitor_args = args.flash_args.monitor_args;
272272

273273
// The 26MHz ESP32-C2's need to be treated as a special case.

espflash/src/cli/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
557557
/// Open a serial monitor
558558
pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
559559
let mut flasher = connect(&args.connect_args, config, true, true)?;
560-
let pid = flasher.get_usb_pid()?;
560+
let pid = flasher.usb_pid();
561561

562562
let elf = if let Some(elf_path) = args.monitor_args.elf.clone() {
563563
let path = fs::canonicalize(elf_path).into_diagnostic()?;

espflash/src/connection/mod.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) mod reset;
4040

4141
const MAX_CONNECT_ATTEMPTS: usize = 7;
4242
const MAX_SYNC_ATTEMPTS: usize = 5;
43-
pub(crate) const USB_SERIAL_JTAG_PID: u16 = 0x1001;
43+
const USB_SERIAL_JTAG_PID: u16 = 0x1001;
4444

4545
#[cfg(unix)]
4646
pub type Port = serialport::TTYPort;
@@ -60,8 +60,12 @@ impl TryInto<u32> for CommandResponseValue {
6060
fn try_into(self) -> Result<u32, Self::Error> {
6161
match self {
6262
CommandResponseValue::ValueU32(value) => Ok(value),
63-
CommandResponseValue::ValueU128(_) => Err(Error::InternalError),
64-
CommandResponseValue::Vector(_) => Err(Error::InternalError),
63+
CommandResponseValue::ValueU128(_) => Err(Error::InvalidResponse(
64+
"expected `u32` but found `u128`".into(),
65+
)),
66+
CommandResponseValue::Vector(_) => Err(Error::InvalidResponse(
67+
"expected `u32` but found `Vec`".into(),
68+
)),
6569
}
6670
}
6771
}
@@ -71,9 +75,13 @@ impl TryInto<u128> for CommandResponseValue {
7175

7276
fn try_into(self) -> Result<u128, Self::Error> {
7377
match self {
74-
CommandResponseValue::ValueU32(_) => Err(Error::InternalError),
78+
CommandResponseValue::ValueU32(_) => Err(Error::InvalidResponse(
79+
"expected `u128` but found `u32`".into(),
80+
)),
7581
CommandResponseValue::ValueU128(value) => Ok(value),
76-
CommandResponseValue::Vector(_) => Err(Error::InternalError),
82+
CommandResponseValue::Vector(_) => Err(Error::InvalidResponse(
83+
"expected `u128` but found `Vec`".into(),
84+
)),
7785
}
7886
}
7987
}
@@ -83,8 +91,12 @@ impl TryInto<Vec<u8>> for CommandResponseValue {
8391

8492
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
8593
match self {
86-
CommandResponseValue::ValueU32(_) => Err(Error::InternalError),
87-
CommandResponseValue::ValueU128(_) => Err(Error::InternalError),
94+
CommandResponseValue::ValueU32(_) => Err(Error::InvalidResponse(
95+
"expected `Vec` but found `u32`".into(),
96+
)),
97+
CommandResponseValue::ValueU128(_) => Err(Error::InvalidResponse(
98+
"expected `Vec` but found `u128`".into(),
99+
)),
88100
CommandResponseValue::Vector(value) => Ok(value),
89101
}
90102
}
@@ -263,7 +275,7 @@ impl Connection {
263275

264276
// Reset the device taking into account the reset after argument
265277
pub fn reset_after(&mut self, is_stub: bool) -> Result<(), Error> {
266-
let pid = self.get_usb_pid()?;
278+
let pid = self.usb_pid();
267279

268280
match self.after_operation {
269281
ResetAfterOperation::HardReset => hard_reset(&mut self.serial, pid),
@@ -461,10 +473,11 @@ impl Connection {
461473

462474
/// Read a register command with a timeout
463475
pub fn read_reg(&mut self, reg: u32) -> Result<u32, Error> {
464-
self.with_timeout(CommandType::ReadReg.timeout(), |connection| {
476+
let resp = self.with_timeout(CommandType::ReadReg.timeout(), |connection| {
465477
connection.command(Command::ReadReg { address: reg })
466-
})
467-
.map(|v| v.try_into().unwrap())
478+
})?;
479+
480+
resp.try_into()
468481
}
469482

470483
/// Write a register command with a timeout
@@ -502,8 +515,12 @@ impl Connection {
502515
}
503516

504517
/// Get the USB PID of the serial port
505-
pub fn get_usb_pid(&self) -> Result<u16, Error> {
506-
Ok(self.port_info.pid)
518+
pub fn usb_pid(&self) -> u16 {
519+
self.port_info.pid
520+
}
521+
522+
pub(crate) fn is_using_usb_serial_jtag(&self) -> bool {
523+
self.port_info.pid == USB_SERIAL_JTAG_PID
507524
}
508525
}
509526

espflash/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,6 @@ pub enum Error {
205205
#[error(transparent)]
206206
TryFromSlice(#[from] TryFromSliceError),
207207

208-
#[error("Internal Error")]
209-
InternalError,
210-
211208
#[error("Failed to open file: {0}")]
212209
FileOpenError(String, #[source] io::Error),
213210

espflash/src/flasher/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,8 @@ impl Flasher {
11881188
self.connection.into_serial()
11891189
}
11901190

1191-
pub fn get_usb_pid(&self) -> Result<u16, Error> {
1192-
self.connection.get_usb_pid()
1191+
pub fn usb_pid(&self) -> u16 {
1192+
self.connection.usb_pid()
11931193
}
11941194

11951195
pub fn erase_region(&mut self, offset: u32, size: u32) -> Result<(), Error> {
@@ -1248,7 +1248,7 @@ impl Flasher {
12481248
while data.len() < size as usize {
12491249
let response = self.connection.read_response()?;
12501250
let chunk: Vec<u8> = if let Some(response) = response {
1251-
response.value.try_into().unwrap()
1251+
response.value.try_into()?
12521252
} else {
12531253
return Err(Error::IncorrectResponse);
12541254
};
@@ -1268,7 +1268,7 @@ impl Flasher {
12681268

12691269
let response = self.connection.read_response()?;
12701270
let digest: Vec<u8> = if let Some(response) = response {
1271-
response.value.try_into().unwrap()
1271+
response.value.try_into()?
12721272
} else {
12731273
return Err(Error::IncorrectResponse);
12741274
};

espflash/src/targets/flash_target/esp32.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::{
1212
connection::{
1313
command::{Command, CommandType},
1414
Connection,
15-
USB_SERIAL_JTAG_PID,
1615
},
1716
flasher::ProgressCallbacks,
1817
targets::FlashTarget,
@@ -76,7 +75,7 @@ impl FlashTarget for Esp32Target {
7675
//
7776
// TODO: the stub doesn't appear to disable the watchdog on ESP32-S3, so we
7877
// explicitly disable the watchdog here.
79-
if connection.get_usb_pid()? == USB_SERIAL_JTAG_PID {
78+
if connection.is_using_usb_serial_jtag() {
8079
match self.chip {
8180
Chip::Esp32c3 => {
8281
connection.command(Command::WriteReg {

0 commit comments

Comments
 (0)