diff --git a/.cargo/config.toml b/.cargo/config.toml index db7e33d..4865338 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,7 @@ [target.'cfg(target_family = "wasm")'] rustflags = ["--cfg=web_sys_unstable_apis"] + +[build] +#just comment in the "current" target +#target = "x86_64-unknown-linux-gnu" +target = "wasm32-unknown-unknown" diff --git a/Cargo.toml b/Cargo.toml index 645348f..e893bb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,9 @@ tokio-test = "0.4.3" # Wasm deps [target.'cfg(target_family = "wasm")'.dependencies] -wasm-bindgen = "0.2.84" -wasm-bindgen-futures = "0.4.39" -js-sys = "0.3.67" +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" +js-sys = "0.3" [target.'cfg(target_family = "wasm")'.dependencies.web-sys] version = "0.3" diff --git a/src/backend/native.rs b/src/backend/native.rs index 5d25e82..9d25274 100644 --- a/src/backend/native.rs +++ b/src/backend/native.rs @@ -110,6 +110,10 @@ impl Device for UsbDevice { } } + async fn forget(&self) -> Result<(), UsbError> { + self.reset().await + } + async fn vendor_id(&self) -> u16 { self.device_info.vendor_id() } @@ -177,6 +181,25 @@ impl<'a> Interface<'a> for UsbInterface { Err(_) => Err(UsbError::TransferError), } } + + /* + async fn interrupt_in(&self, endpoint: u8, length: usize) -> Result, UsbError> { + let buf = Vec::new(); + let buffer = nusb::transfer::RequestBuffer::reuse(buf, length); + + match self.interface.interrupt_in(endpoint, buffer).await.into_result() { + Ok(res) => Ok(res), + Err(_) => Err(UsbError::TransferError), + } + } + + async fn interrupt_out(&self, endpoint: u8, buf: Vec) -> Result { + match self.interface.interrupt_out(endpoint, buf).await.into_result() { + Ok(res) => Ok(res.actual_length()), + Err(_) => Err(UsbError::TransferError), + } + } + */ } impl From for nusb::transfer::ControlIn { diff --git a/src/backend/wasm.rs b/src/backend/wasm.rs index 0ea3ed3..818a276 100644 --- a/src/backend/wasm.rs +++ b/src/backend/wasm.rs @@ -190,7 +190,6 @@ impl Device for UsbDevice { } } - /* async fn forget(&self) -> Result<(), UsbError> { let result = JsFuture::from(Promise::resolve(&self.device.forget())).await; @@ -199,7 +198,6 @@ impl Device for UsbDevice { Err(_) => Err(UsbError::CommunicationError), } } - */ async fn vendor_id(&self) -> u16 { self.device.vendor_id() @@ -307,6 +305,34 @@ impl<'a> Interface<'a> for UsbInterface { Ok(transfer_result.bytes_written() as usize) } + + /* + async fn interrupt_in(&self, endpoint: u8, length: usize) -> Result, UsbError> { + let promise = Promise::resolve(&self.device.transfer_in(endpoint, length as u32)); + + let result = JsFuture::from(promise).await; + + let transfer_result: UsbInTransferResult = match result { + Ok(res) => res.into(), + Err(_) => return Err(UsbError::TransferError), + }; + + if transfer_result. + + let data = match transfer_result.data() { + Some(res) => res.buffer(), + None => return Err(UsbError::TransferError), + }; + + let array = Uint8Array::new(&data); + + Ok(array.to_vec()) + } + + async fn interrupt_out(&self, endpoint: u8, buf: Vec) -> Result { + todo!() + } + */ } impl From for UsbControlTransferParameters { diff --git a/src/usb.rs b/src/usb.rs index 0014abd..6b2d63e 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -20,13 +20,11 @@ pub trait Device { /// request a new device with [crate::get_device] or [crate::get_device_filter] async fn reset(&self) -> Result<(), UsbError>; - /* /// Remove the device from the paired devices list, causing it to no longer be usable. /// You must request to reconnect using [crate::get_device] or [crate::get_device_filter] /// - /// **Note: Only does anything on WASM, on Native it simply resets the device** + /// **Note: on Native with `nusb` this simply resets the device** async fn forget(&self) -> Result<(), UsbError>; - */ /// 16 bit device Product ID async fn product_id(&self) -> u16; @@ -68,14 +66,15 @@ pub trait Interface<'a> { /// a slice, and returns a [Result] containing the number of bytes transferred async fn bulk_out(&self, endpoint: u8, data: &[u8]) -> Result; - /* Interrupt transfers are a work in progress - async fn interrupt_in(&self, _endpoint: u8, _buf: Vec) { - unimplemented!() - } + /* TODO: Figure out interrupt transfers on Web USB - async fn interrupt_out(&self, _endpoint: u8, _buf: Vec) { - unimplemented!() - } + /// A USB interrupt in transfer (device to host). + /// Takes in an endpoint and a buffer to fill + async fn interrupt_in(&self, endpoint: u8, length: usize) -> Result, UsbError>; + + /// A USB interrupt out transfer (host to device). + /// Takes in an endpoint and a buffer to send + async fn interrupt_out(&self, endpoint: u8, buf: Vec) -> Result; */ } @@ -93,6 +92,9 @@ pub enum UsbError { #[error("device disconnected")] Disconnected, + + #[error("device no longer valid")] + Invalid, } /// The type of USB transfer