Updated web-sys and wasm-bindgen, added UsbDevice.forget()

This commit is contained in:
G2-Games 2024-02-07 13:21:55 -06:00
parent b98694a551
commit d57cd84338
5 changed files with 71 additions and 15 deletions

View file

@ -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"

View file

@ -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"

View file

@ -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<Vec<u8>, 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<u8>) -> Result<usize, UsbError> {
match self.interface.interrupt_out(endpoint, buf).await.into_result() {
Ok(res) => Ok(res.actual_length()),
Err(_) => Err(UsbError::TransferError),
}
}
*/
}
impl From<ControlIn> for nusb::transfer::ControlIn {

View file

@ -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<Vec<u8>, 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<u8>) -> Result<usize, UsbError> {
todo!()
}
*/
}
impl From<ControlIn> for UsbControlTransferParameters {

View file

@ -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<usize, UsbError>;
/* Interrupt transfers are a work in progress
async fn interrupt_in(&self, _endpoint: u8, _buf: Vec<u8>) {
unimplemented!()
}
/* TODO: Figure out interrupt transfers on Web USB
async fn interrupt_out(&self, _endpoint: u8, _buf: Vec<u8>) {
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<Vec<u8>, 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<u8>) -> Result<usize, UsbError>;
*/
}
@ -93,6 +92,9 @@ pub enum UsbError {
#[error("device disconnected")]
Disconnected,
#[error("device no longer valid")]
Invalid,
}
/// The type of USB transfer