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

Detect max packet size, and emit zlp if a multiple of that #84

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
71 changes: 69 additions & 2 deletions source/postcard-rpc/src/host_client/raw_nusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,34 @@ where
.claim_interface(interface_id as u8)
.map_err(|e| format!("Failed claiming interface: {e:?}"))?;

let mut mps: Option<usize> = None;
if let Ok(config) = dev.active_configuration() {
for ias in config.interface_alt_settings() {
for ep in ias.endpoints() {
if ep.address() == BULK_OUT_EP {
mps = Some(match mps.take() {
Some(old) => old.min(ep.max_packet_size()),
None => ep.max_packet_size(),
});
}
}
}
}

if let Some(max_packet_size) = &mps {
tracing::debug!(max_packet_size, "Detected max packet size");
} else {
tracing::warn!("Unable to detect Max Packet Size!");
};

let boq = interface.bulk_out_queue(BULK_OUT_EP);
let biq = interface.bulk_in_queue(BULK_IN_EP);

Ok(HostClient::new_with_wire(
NusbWireTx { boq },
NusbWireTx {
boq,
max_packet_size: mps,
},
NusbWireRx {
biq,
consecutive_errs: 0,
Expand Down Expand Up @@ -197,11 +220,34 @@ where
.claim_interface(interface_id as u8)
.map_err(|e| format!("Failed claiming interface: {e:?}"))?;

let mut mps: Option<usize> = None;
if let Ok(config) = dev.active_configuration() {
for ias in config.interface_alt_settings() {
for ep in ias.endpoints() {
if ep.address() == BULK_OUT_EP {
mps = Some(match mps.take() {
Some(old) => old.min(ep.max_packet_size()),
None => ep.max_packet_size(),
});
}
}
}
}

if let Some(max_packet_size) = &mps {
tracing::debug!(max_packet_size, "Detected max packet size");
} else {
tracing::warn!("Unable to detect Max Packet Size!");
};

let boq = interface.bulk_out_queue(BULK_OUT_EP);
let biq = interface.bulk_in_queue(BULK_IN_EP);

Ok(HostClient::new_with_wire(
NusbWireTx { boq },
NusbWireTx {
boq,
max_packet_size: mps,
},
NusbWireRx {
biq,
consecutive_errs: 0,
Expand Down Expand Up @@ -276,6 +322,7 @@ impl WireSpawn for NusbSpawn {
/// NUSB Wire Transmit Interface Implementor
struct NusbWireTx {
boq: Queue<Vec<u8>>,
max_packet_size: Option<usize>,
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -295,13 +342,33 @@ impl WireTx for NusbWireTx {

impl NusbWireTx {
async fn send_inner(&mut self, data: Vec<u8>) -> Result<(), NusbWireTxError> {
let needs_zlp = if let Some(mps) = self.max_packet_size {
(data.len() % mps) == 0
} else {
true
};

self.boq.submit(data);

// Append ZLP if we are a multiple of max packet
if needs_zlp {
self.boq.submit(vec![]);
}

let send_res = self.boq.next_complete().await;
if let Err(e) = send_res.status {
tracing::error!("Output Queue Error: {e:?}");
return Err(e.into());
}

if needs_zlp {
let send_res = self.boq.next_complete().await;
if let Err(e) = send_res.status {
tracing::error!("Output Queue Error: {e:?}");
return Err(e.into());
}
}

Ok(())
}
}
Expand Down
6 changes: 5 additions & 1 deletion source/postcard-rpc/src/server/impls/embassy_usb_v0_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ static STINDX: AtomicU8 = AtomicU8::new(0xFF);
static HDLR: ConstStaticCell<PoststationHandler> = ConstStaticCell::new(PoststationHandler {});

impl embassy_usb_0_3::Handler for PoststationHandler {
fn get_string(&mut self, index: embassy_usb_0_3::types::StringIndex, lang_id: u16) -> Option<&str> {
fn get_string(
&mut self,
index: embassy_usb_0_3::types::StringIndex,
lang_id: u16,
) -> Option<&str> {
use embassy_usb_0_3::descriptor::lang_id;

let stindx = STINDX.load(Ordering::Relaxed);
Expand Down
6 changes: 5 additions & 1 deletion source/postcard-rpc/src/server/impls/embassy_usb_v0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ static STINDX: AtomicU8 = AtomicU8::new(0xFF);
static HDLR: ConstStaticCell<PoststationHandler> = ConstStaticCell::new(PoststationHandler {});

impl embassy_usb_0_4::Handler for PoststationHandler {
fn get_string(&mut self, index: embassy_usb_0_4::types::StringIndex, lang_id: u16) -> Option<&str> {
fn get_string(
&mut self,
index: embassy_usb_0_4::types::StringIndex,
lang_id: u16,
) -> Option<&str> {
use embassy_usb_0_4::descriptor::lang_id;

let stindx = STINDX.load(Ordering::Relaxed);
Expand Down