diff --git a/Cargo.toml b/Cargo.toml index ea6a5a0..1a3283e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,12 @@ edition = "2021" [lib] crate-type = ["cdylib", "rlib"] +# Wasm deps [target.'cfg(target_family = "wasm")'.dependencies] wasm-bindgen = "0.2.84" wasm-bindgen-futures = "0.4.39" js-sys = "0.3.67" +gloo = "0.11.0" [target.'cfg(target_family = "wasm")'.dependencies.web-sys] version = "0.3" @@ -29,14 +31,12 @@ features = [ "Storage" ] +# Non-wasm deps [target.'cfg(not(target_family = "wasm"))'.dependencies] nusb = "0.1.4" [profile.release] opt-level = "s" -[dependencies] -thiserror = "1.0.56" - [package.metadata.wasm-pack.profile.dev.wasm-bindgen] dwarf-debug-info = true diff --git a/src/backend/wasm.rs b/src/backend/wasm.rs index f9baaaa..3e623a5 100644 --- a/src/backend/wasm.rs +++ b/src/backend/wasm.rs @@ -1,7 +1,7 @@ #![cfg_attr(debug_assertions, allow(dead_code, unused_imports))] use std::error::Error; use wasm_bindgen::prelude::*; -use js_sys::JSON; +use gloo::console::log; use web_sys::{ console, @@ -14,7 +14,7 @@ use web_sys::{ UsbRequestType, UsbDeviceRequestOptions, }; -use js_sys::{Array, Uint8Array, Promise}; +use js_sys::{Array, Uint8Array, Promise, Object}; use wasm_bindgen_futures::JsFuture; // Crate stuff @@ -89,7 +89,6 @@ impl Device for UsbDevice { return Err(format!("{:?}", err).into()) }, }; - //let interface: WasmUsbInterface = dev_promise.await.unwrap().into(); Ok(UsbInterface { device: self.device.clone() @@ -121,9 +120,9 @@ impl<'a> Interface<'a> for UsbInterface { let length = data.length; let params: UsbControlTransferParameters = data.into(); - let promise = Promise::resolve(&self.device.control_transfer_in(¶ms, length)); - - let result = JsFuture::from(promise).await; + let result = JsFuture::from(Promise::resolve( + &self.device.control_transfer_in(¶ms, length) + )).await; let transfer_result: UsbInTransferResult = match result { Ok(res) => res.into(), @@ -141,13 +140,15 @@ impl<'a> Interface<'a> for UsbInterface { } async fn control_out(&self, data: crate::usb::ControlOut<'a>) -> Result<(), Box> { - let params = data.into(); - let promise = Promise::resolve(&self.device.control_transfer_out(¶ms)); + let array = Uint8Array::from(data.data); + let array_obj = Object::try_from(&array).unwrap(); + let params: UsbControlTransferParameters = data.into(); + let promise = Promise::resolve(&self.device.control_transfer_out_with_buffer_source(¶ms, &array_obj)); let result = JsFuture::from(promise).await; match result { - Ok(_) => Ok(()), + Ok(res) => Ok(()), Err(err) => Err(format!("{:?}", err).into()), } } diff --git a/src/usb.rs b/src/usb.rs index e7f4a75..5dba38e 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -1,11 +1,6 @@ #![cfg_attr(debug_assertions, allow(async_fn_in_trait))] use std::error::Error; use crate::context::UsbInterface; -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum UsbError { -} /// A unique USB device pub trait Device {