Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
make try_connect_polling() a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Mar 1, 2018
1 parent 2e2ddd8 commit 23bb027
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
21 changes: 10 additions & 11 deletions hw/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,17 @@ impl Manager {
Err(Error::InvalidDevice)
}
}
}

// Try to connect to the device using polling at most 1 second
// Failure will be logged via the debug log
fn try_connect_polling(ledger: Arc<Manager>, duration: Duration) -> bool {
let start_time = Instant::now();
while start_time.elapsed() <= duration {
if let Ok(_) = ledger.update_devices() {
return true
}
// Try to connect to the device using polling in at most the time specified by the `timeout`
fn try_connect_polling(ledger: Arc<Manager>, timeout: Duration) -> bool {
let start_time = Instant::now();
while start_time.elapsed() <= timeout {
if let Ok(_) = ledger.update_devices() {
return true
}
false
}
false
}

/// Ledger event handler
Expand All @@ -386,7 +385,7 @@ impl libusb::Hotplug for EventHandler {
fn device_arrived(&mut self, device: libusb::Device) {
debug!(target: "hw", "Ledger arrived");
if let (Some(ledger), Ok(_)) = (self.ledger.upgrade(), Manager::is_valid_ledger(&device)) {
if Manager::try_connect_polling(ledger, Duration::from_millis(500)) != true {
if try_connect_polling(ledger, Duration::from_millis(500)) != true {
debug!(target: "hw", "Ledger connect timeout");
}
}
Expand All @@ -395,7 +394,7 @@ impl libusb::Hotplug for EventHandler {
fn device_left(&mut self, device: libusb::Device) {
debug!(target: "hw", "Ledger left");
if let (Some(ledger), Ok(_)) = (self.ledger.upgrade(), Manager::is_valid_ledger(&device)) {
if Manager::try_connect_polling(ledger, Duration::from_millis(500)) != true {
if try_connect_polling(ledger, Duration::from_millis(500)) != true {
debug!(target: "hw", "Ledger disconnect timeout");
}
}
Expand Down
21 changes: 10 additions & 11 deletions hw/src/trezor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,18 +398,17 @@ impl Manager {
}
Ok((msg_type, data[..msg_size as usize].to_vec()))
}
}

// Try to connect to the device using polling at most 1 second
// Failure will be logged via the debug log
fn try_connect_polling(trezor: Arc<Manager>, duration: Duration) -> bool {
let start_time = Instant::now();
while start_time.elapsed() <= duration {
if let Ok(_) = trezor.update_devices() {
return true
}
// Try to connect to the device using polling in at most the time specified by the `timeout`
fn try_connect_polling(trezor: Arc<Manager>, duration: Duration) -> bool {
let start_time = Instant::now();
while start_time.elapsed() <= duration {
if let Ok(_) = trezor.update_devices() {
return true
}
false
}
false
}

/// Trezor event handler
Expand All @@ -432,7 +431,7 @@ impl libusb::Hotplug for EventHandler {
fn device_arrived(&mut self, _device: libusb::Device) {
debug!(target: "hw", "Trezor V1 arrived");
if let Some(trezor) = self.trezor.upgrade() {
if Manager::try_connect_polling(trezor, Duration::from_millis(500)) != true {
if try_connect_polling(trezor, Duration::from_millis(500)) != true {
debug!(target: "hw", "Ledger connect timeout");
}
}
Expand All @@ -441,7 +440,7 @@ impl libusb::Hotplug for EventHandler {
fn device_left(&mut self, _device: libusb::Device) {
debug!(target: "hw", "Trezor V1 left");
if let Some(trezor) = self.trezor.upgrade() {
if Manager::try_connect_polling(trezor, Duration::from_millis(500)) != true {
if try_connect_polling(trezor, Duration::from_millis(500)) != true {
debug!(target: "hw", "Ledger disconnect timeout");
}
}
Expand Down

0 comments on commit 23bb027

Please sign in to comment.