From db9605fbb7d5eea00b270c6c0552c81c70dcb5c5 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Tue, 30 Jan 2024 18:06:44 -0600 Subject: [PATCH] Fixed some problems with wasm --- Cargo.toml | 6 +++- src/backend/native.rs | 25 +++++++++++----- src/backend/wasm.rs | 69 +++++++++++++++++++++++++------------------ src/usb.rs | 18 +++++------ 4 files changed, 72 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c112967..ea6a5a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "cross-usb" +name = "cross_usb" version = "0.1.0" authors = ["G2-Games "] edition = "2021" @@ -25,6 +25,7 @@ features = [ "UsbRequestType", "UsbControlTransferParameters", "UsbDeviceRequestOptions", + "UsbInTransferResult", "Storage" ] @@ -34,5 +35,8 @@ 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/native.rs b/src/backend/native.rs index 75c2995..d195b3e 100644 --- a/src/backend/native.rs +++ b/src/backend/native.rs @@ -4,6 +4,7 @@ use nusb; use crate::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient}; pub struct UsbDevice { + device_info: nusb::DeviceInfo, device: nusb::Device, } @@ -30,7 +31,10 @@ pub async fn get_device(vendor_id: u16, product_id: u16) -> Result Err(e.into()) } } + + async fn vendor_id(&self) -> u16 { + self.device_info.vendor_id() + } + + async fn product_id(&self) -> u16 { + self.device_info.product_id() + } } impl<'a> Interface<'a> for UsbInterface { @@ -66,16 +78,15 @@ impl<'a> Interface<'a> for UsbInterface { } } - async fn bulk_in(&self, endpoint: u8, buf: Vec) -> Result, Box> { - let buf_len = buf.len(); - let request_buffer = nusb::transfer::RequestBuffer::reuse(buf, buf_len); + async fn bulk_in(&self, endpoint: u8, length: usize) -> Result, Box> { + let request_buffer = nusb::transfer::RequestBuffer::new(length); Ok(self.interface.bulk_in(endpoint, request_buffer).await.into_result()?) } - async fn bulk_out(&self, endpoint: u8, buf: Vec) -> Result<(), Box> { - match self.interface.bulk_out(endpoint, buf).await.into_result() { - Ok(_) => Ok(()), + async fn bulk_out(&self, endpoint: u8, data: Vec) -> Result> { + match self.interface.bulk_out(endpoint, data).await.into_result() { + Ok(len) => Ok(len.actual_length()), Err(e) => Err(e.into()) } } diff --git a/src/backend/wasm.rs b/src/backend/wasm.rs index 57b623a..f9baaaa 100644 --- a/src/backend/wasm.rs +++ b/src/backend/wasm.rs @@ -9,6 +9,7 @@ use web_sys::{ UsbDevice as WasmUsbDevice, UsbInterface as WasmUsbInterface, UsbControlTransferParameters, + UsbInTransferResult, UsbRecipient, UsbRequestType, UsbDeviceRequestOptions, @@ -59,16 +60,14 @@ pub async fn get_device(vendor_id: u16, product_id: u16) -> Result res.into(), + Ok(dev) => dev.into(), Err(err) => { - console::log_1(&err.clone().into()); + console::log_1(&err.clone()); return Err(err.into()) }, }; - let _open_promise = JsFuture::from(Promise::resolve(&device.open())); - - console::log_1(&"got device".into()); + let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?; Ok(UsbDevice { device @@ -80,10 +79,17 @@ impl Device for UsbDevice { type UsbInterface = UsbInterface; async fn open_interface(&self, number: u8) -> Result> { - let dev_promise = JsFuture::from(Promise::resolve(&self.device.claim_interface(number))); + let dev_promise = JsFuture::from(Promise::resolve(&self.device.claim_interface(number))).await; // Wait for the interface to be claimed - let result = dev_promise; + let device: WasmUsbDevice = match dev_promise { + Ok(dev) => dev.into(), + Err(err) => { + console::log_1(&err.clone()); + return Err(format!("{:?}", err).into()) + }, + }; + //let interface: WasmUsbInterface = dev_promise.await.unwrap().into(); Ok(UsbInterface { device: self.device.clone() @@ -97,55 +103,60 @@ impl Device for UsbDevice { match result { Ok(_) => Ok(()), - Err(_) => { - console::log_1(&"Cancelled".into()); - return Err("cancelled".into()) - }, + Err(_) => Err("cancelled".into()), } } + + async fn vendor_id(&self) -> u16 { + self.device.vendor_id() + } + + async fn product_id(&self) -> u16 { + self.device.product_id() + } } impl<'a> Interface<'a> for UsbInterface { async fn control_in(&self, data: crate::usb::ControlIn) -> Result, Box> { let length = data.length; - let params = data.into(); + let params: UsbControlTransferParameters = data.into(); + let promise = Promise::resolve(&self.device.control_transfer_in(¶ms, length)); - let mut result = JsFuture::from(promise).await; + let result = JsFuture::from(promise).await; - let data = match result { + let transfer_result: UsbInTransferResult = match result { Ok(res) => res.into(), - Err(_) => { - console::log_1(&"Cancelled".into()); - return Err("cancelled".into()) - }, + Err(err) => return Err(format!("Error {:?}", err).into()), }; - let unitarray = Uint8Array::new(&data); + let data = match transfer_result.data() { + Some(res) => res.buffer(), + None => return Err("No data returned".into()), + }; - Ok(unitarray.to_vec()) + let array = Uint8Array::new(&data); + + Ok(array.to_vec()) } 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 mut result = JsFuture::from(promise).await; + let result = JsFuture::from(promise).await; match result { Ok(_) => Ok(()), - Err(err) => { - console::log_1(&"Cancelled".into()); - Err(format!("{:?}", err).into()) - }, + Err(err) => Err(format!("{:?}", err).into()), } } - async fn bulk_in(&self, _endpoint: u8, _buf: Vec) -> Result, Box> { + async fn bulk_in(&self, _endpoint: u8, _length: usize) -> Result, Box> { todo!() } - async fn bulk_out(&self, _endpoint: u8, _buf: Vec) -> Result<(), Box> { + async fn bulk_out(&self, _endpoint: u8, _data: Vec) -> Result> { todo!() } } @@ -155,7 +166,7 @@ impl From for UsbControlTransferParameters { UsbControlTransferParameters::new( value.index, value.recipient.into(), - value.request.into(), + value.request, value.control_type.into(), value.value ) @@ -167,7 +178,7 @@ impl From> for UsbControlTransferParameters { UsbControlTransferParameters::new( value.index, value.recipient.into(), - value.request.into(), + value.request, value.control_type.into(), value.value ) diff --git a/src/usb.rs b/src/usb.rs index 94cba51..e7f4a75 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -1,7 +1,11 @@ #![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 { @@ -10,14 +14,10 @@ pub trait Device { async fn open_interface(&self, number: u8) -> Result>; async fn reset(&self) -> Result<(), Box>; + async fn product_id(&self) -> u16; + async fn vendor_id(&self) -> u16; //TODO: Implement these placeholders - async fn product_id(&self) -> u16 { - 0x00 - } - async fn vendor_id(&self) -> u16 { - 0x00 - } async fn class(&self) -> u16 { 0x00 } @@ -37,8 +37,8 @@ pub trait Interface<'a> { async fn control_in(&self, data: ControlIn) -> Result, Box>; async fn control_out(&self, data: ControlOut<'a>) -> Result<(), Box>; - async fn bulk_in(&self, endpoint: u8, buf: Vec) -> Result, Box>; - async fn bulk_out(&self, endpoint: u8, buf: Vec) -> Result<(), Box>; + async fn bulk_in(&self, endpoint: u8, length: usize) -> Result, Box>; + async fn bulk_out(&self, endpoint: u8, data: Vec) -> Result>; async fn interrupt_in(&self, _endpoint: u8, _buf: Vec) { unimplemented!()