use crate::error::Result;
#[cfg(feature = "nusb")]
pub mod usb;
#[cfg(feature = "nusb")]
pub use usb::UsbTransport;
#[cfg(all(feature = "web", target_arch = "wasm32"))]
pub mod web;
#[cfg(all(feature = "web", target_arch = "wasm32"))]
pub use web::WebUsbTransport;
#[cfg(feature = "nusb")]
pub mod record;
#[cfg(feature = "nusb")]
pub use record::Recorder;
#[cfg(feature = "replay")]
pub mod replay;
#[cfg(feature = "replay")]
pub use replay::{
Direction, ErrKind, Expect, Header, ReplayTransport, Script, Section, Source, Step,
};
pub const VENDOR_ID: u16 = 0x0ffc;
pub const PRODUCT_ID_ELECTRO5: u16 = 0x0027;
pub const CLASS_VENDOR_SPECIFIC: u8 = 0xff;
pub const EP_IN: u8 = 0x82;
pub const EP_OUT: u8 = 0x03;
pub const READ_BUFFER: usize = 49152;
#[cfg(any(feature = "nusb", all(feature = "web", target_arch = "wasm32"), test))]
pub(crate) fn needs_terminator(written: usize, packet: usize) -> bool {
written != 0 && written.is_multiple_of(packet)
}
#[allow(async_fn_in_trait)]
pub trait Transport {
async fn write(&mut self, buf: &[u8]) -> Result<()>;
async fn read(&mut self, max: usize) -> Result<Vec<u8>>;
async fn read_timeout(
&mut self,
max: usize,
_limit: std::time::Duration,
) -> Result<Option<Vec<u8>>> {
self.read(max).await.map(Some)
}
async fn write_timeout(&mut self, buf: &[u8], _limit: std::time::Duration) -> Result<bool> {
self.write(buf).await.map(|()| true)
}
}
#[cfg(test)]
mod tests {
use super::needs_terminator;
#[test]
fn a_frame_that_fills_whole_packets_needs_terminating() {
const FULL_SPEED: usize = 64;
for answered in [1, 33, 63, 65, 127, 129] {
assert!(!needs_terminator(answered, FULL_SPEED), "{answered}");
}
for stranded in [64, 128, 192, 32_768] {
assert!(needs_terminator(stranded, FULL_SPEED), "{stranded}");
}
}
#[test]
fn the_boundary_follows_the_endpoints_packet_size() {
assert!(needs_terminator(512, 512));
assert!(!needs_terminator(64, 512));
assert!(!needs_terminator(576, 512));
}
#[test]
fn an_empty_write_is_its_own_terminator() {
assert!(!needs_terminator(0, 64));
assert!(!needs_terminator(0, 512));
}
}