Skip to content

Commit

Permalink
Fixed &mut of submit() and added poll_transfers()
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed May 15, 2021
1 parent 37afa68 commit 5fbe93c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
42 changes: 42 additions & 0 deletions examples/read_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use rusb::{AsyncTransfer, CbResult, Context, UsbContext};

use std::str::FromStr;
use std::time::Duration;

fn main() {
let args: Vec<String> = std::env::args().collect();

if args.len() < 4 {
eprintln!("Usage: read_async <vendor-id> <product-id> <endpoint>");
return;
}

let vid: u16 = FromStr::from_str(args[1].as_ref()).unwrap();
let pid: u16 = FromStr::from_str(args[2].as_ref()).unwrap();
let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap();

let ctx = Context::new().expect("Could not initialize libusb");
let device = ctx
.open_device_with_vid_pid(vid, pid)
.expect("Could not find device");

const NUM_TRANSFERS: usize = 32;
const BUF_SIZE: usize = 1024;
let mut main_buffer = Box::new([0u8; BUF_SIZE * NUM_TRANSFERS]);

let mut transfers = Vec::new();
for buf in main_buffer.chunks_exact_mut(BUF_SIZE) {
let mut transfer =
AsyncTransfer::new_bulk(&device, endpoint, buf, callback, Duration::from_secs(10));
transfer.submit().expect("Could not submit transfer");
transfers.push(transfer);
}

loop {
rusb::poll_transfers(&ctx, Duration::from_secs(10));
}
}

fn callback(result: CbResult) {
println!("{:?}", result)
}
21 changes: 19 additions & 2 deletions src/device_handle/async_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use std::convert::{TryFrom, TryInto};
use std::marker::{PhantomData, PhantomPinned};
use std::pin::Pin;
use std::ptr::NonNull;
use std::time::Duration;

type CbResult<'a> = Result<&'a [u8], TransferError>;
pub type CbResult<'a> = Result<&'a [u8], TransferError>;

#[derive(Error, Debug)]
pub enum TransferError {
Expand Down Expand Up @@ -94,7 +95,7 @@ impl<'d, 'b, C: 'd + UsbContext, F: FnMut(CbResult<'b>) + Send> AsyncTransfer<'d
/// returned Err, or the callback has gotten an Err.
// Step 3 of async API
#[allow(unused)]
pub fn submit(&mut self) -> Result<(), TransferError> {
pub fn submit(self: &mut Pin<Box<Self>>) -> Result<(), TransferError> {
let errno = unsafe { ffi::libusb_submit_transfer(self.ptr.as_ptr()) };
use ffi::constants::*;
match errno {
Expand Down Expand Up @@ -189,3 +190,19 @@ impl<C: UsbContext, F> Drop for AsyncTransfer<'_, '_, C, F> {
Self::drop_helper(unsafe { Pin::new_unchecked(self) });
}
}

/// Polls for transfers and executes their callbacks. Will block until the
/// given timeout, or return immediately if timeout is zero.
pub fn poll_transfers(ctx: &impl UsbContext, timeout: Duration) {
let timeval = libc::timeval {
tv_sec: timeout.as_secs() as i64,
tv_usec: timeout.subsec_millis() as i64,
};
unsafe {
ffi::libusb_handle_events_timeout_completed(
ctx.as_raw(),
std::ptr::addr_of!(timeval),
std::ptr::null_mut(),
)
};
}
2 changes: 1 addition & 1 deletion src/device_handle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod async_api;
pub mod async_api;

use std::{mem, ptr::NonNull, time::Duration, u8};

Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use crate::{
context::{Context, GlobalContext, Hotplug, LogLevel, Registration, UsbContext},
device::Device,
device_descriptor::DeviceDescriptor,
device_handle::async_api::{poll_transfers, AsyncTransfer, CbResult},
device_handle::DeviceHandle,
device_list::{DeviceList, Devices},
endpoint_descriptor::EndpointDescriptor,
Expand Down Expand Up @@ -106,6 +107,11 @@ pub fn open_device_with_vid_pid(
if handle.is_null() {
None
} else {
Some(unsafe { DeviceHandle::from_libusb(GlobalContext::default(), std::ptr::NonNull::new_unchecked(handle)) })
Some(unsafe {
DeviceHandle::from_libusb(
GlobalContext::default(),
std::ptr::NonNull::new_unchecked(handle),
)
})
}
}

0 comments on commit 5fbe93c

Please sign in to comment.