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

Wrap device in Arc<Mutex<_>> #125

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions ledger/src/transports/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct HidApiWrapper {
/// Instantiate with [`new`][TransportNativeHID::new].
pub struct TransportNativeHID {
api_mutex: Arc<Mutex<hidapi_rusb::HidApi>>,
device: HidDevice,
device: Arc<Mutex<HidDevice>>,
guard: Mutex<i32>,
}

Expand Down Expand Up @@ -172,7 +172,7 @@ impl TransportNativeHID {
};

let ledger = TransportNativeHID {
device,
device: Arc::new(Mutex::new(device)),
guard: Mutex::new(0),
api_mutex: api_mutex.clone(),
};
Expand Down Expand Up @@ -200,7 +200,8 @@ impl TransportNativeHID {
buffer[4] = (sequence_idx & 0xFF) as u8; // sequence_idx big endian
buffer[5..5 + chunk.len()].copy_from_slice(chunk);

let result = self.device.write(&buffer);
let device = self.device.lock().unwrap();
let result = device.write(&buffer);

match result {
Ok(size) => {
Expand Down Expand Up @@ -236,9 +237,8 @@ impl TransportNativeHID {
let mut answer_buf = vec![];

loop {
let res = self
.device
.read_timeout(&mut response_buffer, LEDGER_TIMEOUT)?;
let device = self.device.lock().unwrap();
let res = device.read_timeout(&mut response_buffer, LEDGER_TIMEOUT)?;

if (sequence_idx == 0 && res < 7) || res < 5 {
return Err(NativeTransportError::Comm("Read error. Incomplete header"));
Expand Down
4 changes: 2 additions & 2 deletions ledger/src/transports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cfg_if::cfg_if! {
/// transport library.
pub struct Ledger(DefaultTransport);

#[async_trait(?Send)]
#[async_trait]
/// An asynchronous interface to the Ledger device. It is critical that the device have only one
/// connection active, so the `init` function acquires a lock on the device.
pub trait LedgerAsync: Sized {
Expand All @@ -48,7 +48,7 @@ pub trait LedgerAsync: Sized {
fn close(self) {}
}

#[async_trait(?Send)]
#[async_trait]
impl LedgerAsync for Ledger {
#[cfg(not(target_arch = "wasm32"))]
async fn init() -> Result<Self, LedgerError> {
Expand Down