Skip to content

Commit

Permalink
Add a function to probe the backlog
Browse files Browse the repository at this point in the history
  • Loading branch information
aMarcireau committed Apr 10, 2024
1 parent ac0d5d8 commit 9fa078e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions drivers/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub trait Usb: Sized {

fn next_with_timeout(&self, timeout: &std::time::Duration) -> Option<usb::BufferView>;

fn backlog(&self) -> usize;

fn serial(&self) -> String;

fn chip_firmware_configuration(&self) -> Self::Configuration;
Expand Down
8 changes: 8 additions & 0 deletions drivers/src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ macro_rules! register {
}
}

pub fn backlog(&self) -> usize {
match self {
$(
Self::[<$module:camel>](device) => device.backlog(),
)+
}
}

pub fn properties(&self) -> Properties {
match self {
$(
Expand Down
4 changes: 4 additions & 0 deletions drivers/src/devices/prophesee_evk3_hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ impl device::Usb for Device {
self.ring.next_with_timeout(timeout)
}

fn backlog(&self) -> usize {
self.ring.backlog()
}

fn serial(&self) -> String {
self.serial.clone()
}
Expand Down
4 changes: 4 additions & 0 deletions drivers/src/devices/prophesee_evk4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,10 @@ impl device::Usb for Device {
self.ring.next_with_timeout(timeout)
}

fn backlog(&self) -> usize {
self.ring.backlog()
}

fn serial(&self) -> String {
self.serial.clone()
}
Expand Down
23 changes: 16 additions & 7 deletions drivers/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ extern "system" fn usb_transfer_callback(transfer_pointer: *mut libusb1_sys::lib
.ring
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
match shared.transfer_statuses[context.transfer_index] {
TransferStatus::Active => match transfer.status {
libusb1_sys::constants::LIBUSB_TRANSFER_COMPLETED
Expand Down Expand Up @@ -495,7 +495,7 @@ impl Ring {
let shared = context
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
for index in 0..configuration.transfer_queue_size {
// unsafe: libusb1_sys wrapper
let mut transfer = match std::ptr::NonNull::new(unsafe {
Expand Down Expand Up @@ -623,7 +623,7 @@ impl Ring {
.context
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
for rest_index in index..result.transfers.len() {
// dropping 'result' cancels transfers
// mark unscheduled transfers as complete to prevent un-needed cancelling
Expand Down Expand Up @@ -686,6 +686,15 @@ impl Drop for BufferView<'_> {
}

impl Ring {
pub fn backlog(&self) -> usize {
let shared = self
.context
.shared
.lock()
.expect("ring context's lock is not poisoned");
(shared.write_range.0 + shared.buffers.len() - 1 - shared.read) % shared.buffers.len()
}

pub fn next_with_timeout(&self, duration: &std::time::Duration) -> Option<BufferView> {
if self
.active_buffer_view
Expand All @@ -699,7 +708,7 @@ impl Ring {
.context
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
loop {
shared.read = (shared.read + 1) % shared.buffers.len();
while (shared.write_range.1 + shared.buffers.len() - 1 - shared.read)
Expand Down Expand Up @@ -759,7 +768,7 @@ impl Drop for Ring {
.context
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
// unsafe: transfer is allocated
let _ = unsafe { libusb1_sys::libusb_cancel_transfer(self.transfers[0].as_ptr()) };
for index in 0..self.transfers.len() {
Expand All @@ -773,7 +782,7 @@ impl Drop for Ring {
.context
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
for index in 0..self.transfers.len() {
match shared.transfer_statuses[index] {
TransferStatus::Active => {
Expand Down Expand Up @@ -825,7 +834,7 @@ impl Drop for Ring {
.context
.shared
.lock()
.expect("ring context's lock is poisonned");
.expect("ring context's lock is not poisoned");
for buffer in shared.buffers.iter() {
if buffer.dma {
// unsafe: buffer was allocated by libusb with 'capacity' bytes
Expand Down

0 comments on commit 9fa078e

Please sign in to comment.