1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-26 07:11:25 +00:00

refactor(rust/trezor-client): nicer way to specify timeouts

fixes test failure introduced in #4459
This commit is contained in:
matejcik 2025-01-08 13:45:30 +01:00 committed by matejcik
parent d06382e298
commit 6b76378d34
2 changed files with 8 additions and 12 deletions

View File

@ -19,8 +19,8 @@ use constants::{DEFAULT_DEBUG_PORT, DEFAULT_HOST, DEFAULT_PORT, LOCAL_LISTENER};
/// The chunk size for the serial protocol. /// The chunk size for the serial protocol.
const CHUNK_SIZE: usize = 64; const CHUNK_SIZE: usize = 64;
const READ_TIMEOUT_MS: u64 = 0; const READ_TIMEOUT: Option<Duration> = None;
const WRITE_TIMEOUT_MS: u64 = 1000; const WRITE_TIMEOUT: Option<Duration> = Some(Duration::from_secs(1));
/// An available transport for connecting with a device. /// An available transport for connecting with a device.
#[derive(Debug)] #[derive(Debug)]
@ -45,8 +45,7 @@ struct UdpLink {
impl Link for UdpLink { impl Link for UdpLink {
fn write_chunk(&mut self, chunk: Vec<u8>) -> Result<(), Error> { fn write_chunk(&mut self, chunk: Vec<u8>) -> Result<(), Error> {
debug_assert_eq!(CHUNK_SIZE, chunk.len()); debug_assert_eq!(CHUNK_SIZE, chunk.len());
let timeout = Duration::from_millis(WRITE_TIMEOUT_MS); self.socket.set_write_timeout(WRITE_TIMEOUT)?;
self.socket.set_write_timeout(Some(timeout))?;
if let Err(e) = self.socket.send(&chunk) { if let Err(e) = self.socket.send(&chunk) {
return Err(e.into()); return Err(e.into());
} }
@ -55,8 +54,7 @@ impl Link for UdpLink {
fn read_chunk(&mut self) -> Result<Vec<u8>, Error> { fn read_chunk(&mut self) -> Result<Vec<u8>, Error> {
let mut chunk = vec![0; CHUNK_SIZE]; let mut chunk = vec![0; CHUNK_SIZE];
let timeout = Duration::from_millis(READ_TIMEOUT_MS); self.socket.set_read_timeout(READ_TIMEOUT)?;
self.socket.set_read_timeout(Some(timeout))?;
let n = self.socket.recv(&mut chunk)?; let n = self.socket.recv(&mut chunk)?;
if n == CHUNK_SIZE { if n == CHUNK_SIZE {

View File

@ -26,8 +26,8 @@ mod constants {
/// The chunk size for the serial protocol. /// The chunk size for the serial protocol.
const CHUNK_SIZE: usize = 64; const CHUNK_SIZE: usize = 64;
const READ_TIMEOUT_MS: u64 = 0; const READ_TIMEOUT: Duration = Duration::from_secs(0);
const WRITE_TIMEOUT_MS: u64 = 1000; const WRITE_TIMEOUT: Duration = Duration::from_secs(1);
/// An available transport for connecting with a device. /// An available transport for connecting with a device.
#[derive(Debug)] #[derive(Debug)]
@ -51,8 +51,7 @@ pub struct WebUsbLink {
impl Link for WebUsbLink { impl Link for WebUsbLink {
fn write_chunk(&mut self, chunk: Vec<u8>) -> Result<(), Error> { fn write_chunk(&mut self, chunk: Vec<u8>) -> Result<(), Error> {
debug_assert_eq!(CHUNK_SIZE, chunk.len()); debug_assert_eq!(CHUNK_SIZE, chunk.len());
let timeout = Duration::from_millis(WRITE_TIMEOUT_MS); if let Err(e) = self.handle.write_interrupt(self.endpoint, &chunk, WRITE_TIMEOUT) {
if let Err(e) = self.handle.write_interrupt(self.endpoint, &chunk, timeout) {
return Err(e.into()); return Err(e.into());
} }
Ok(()) Ok(())
@ -61,9 +60,8 @@ impl Link for WebUsbLink {
fn read_chunk(&mut self) -> Result<Vec<u8>, Error> { fn read_chunk(&mut self) -> Result<Vec<u8>, Error> {
let mut chunk = vec![0; CHUNK_SIZE]; let mut chunk = vec![0; CHUNK_SIZE];
let endpoint = constants::READ_ENDPOINT_MASK | self.endpoint; let endpoint = constants::READ_ENDPOINT_MASK | self.endpoint;
let timeout = Duration::from_millis(READ_TIMEOUT_MS);
let n = self.handle.read_interrupt(endpoint, &mut chunk, timeout)?; let n = self.handle.read_interrupt(endpoint, &mut chunk, READ_TIMEOUT)?;
if n == CHUNK_SIZE { if n == CHUNK_SIZE {
Ok(chunk) Ok(chunk)
} else { } else {