diff --git a/header.d.ts b/header.d.ts index d2217be..ae0a5af 100644 --- a/header.d.ts +++ b/header.d.ts @@ -1,35 +1,4 @@ -export type USBSpeed = 'low' | 'full' | 'high' | 'super' | 'superPlus'; +export type USBSpeed = 'low' | 'full' | 'high' | 'super' | 'superPlus' | undefined; -export declare interface UsbDevice extends USBDevice { - /** - * The USB bus the device is connected to (e.g. `1-1`, `2-3.4`, etc.) - */ - bus: string; - - /** - * The USB address the device is connected to - */ - address: number; - - /** - * The USB port numbers the device is connected through (e.g. `[1]`, `[3, 4]`, etc.) - */ - ports: Array - - /** - * The USB speed of the device (e.g. `Low`, `Full`, `High`, `Super`, `SuperPlus` or `undefined` if unknown) - */ - speed?: USBSpeed; - - /** - * Detaches the kernel driver from the specified interface number (Linux only) - * @param interfaceNumber - */ - detachKernelDriver(interfaceNumber: number): Promise; - - /** - * Attaches the kernel driver for the specified interface number (Linux only) - * @param interfaceNumber - */ - attachKernelDriver(interfaceNumber: number): Promise; -} +// This declaration merges with the napi-generated `class UsbDevice` below. +export declare interface UsbDevice extends USBDevice {} diff --git a/package-lock.json b/package-lock.json index 2bb1e6d..881351a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -635,9 +635,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -654,9 +651,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -673,9 +667,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -692,9 +683,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -711,9 +699,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -730,9 +715,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -749,9 +731,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -957,9 +936,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -976,9 +952,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -995,9 +968,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1014,9 +984,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1033,9 +1000,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1052,9 +1016,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1262,9 +1223,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1281,9 +1239,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1300,9 +1255,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1319,9 +1271,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1816,8 +1765,8 @@ } }, "node_modules/glob": { - "version": "13.0.6", - "integrity": "sha512-Wjlyrolmm8uDpm/ogGyXZXb1Z+Ca2B8NbJwqBVg0axK9GbBeoS7yGV6vjXnYdGm6X53iehEuxxbyiKp8QmN4Vw==", + "version": "13.0.7", + "integrity": "sha512-wfsFgXQ8A3SEI9XGf7Nl3Y9l7ffSrGh7SUCT7vU/2CzYPep9kjxAn1S40F9OPKxAMGCVl7HtNlJhrCJUeUq83g==", "dev": true, "license": "BlueOak-1.0.0", "dependencies": { @@ -1960,8 +1909,8 @@ "license": "MIT" }, "node_modules/markdown-it": { - "version": "14.3.2", - "integrity": "sha512-sHHjZ5fJKlgrG4qns2YwVcdNep35h5fERrfkD2YNsb9UFk0UIHarbiTaHKVMlPuWAoiilyK8Fv/jAm11slsY7Q==", + "version": "14.3.3", + "integrity": "sha512-L//tDAkjTjJCU6pBUSBTgs3Kg4InVaZuUZog/b6V12UKhcDQ7S78877ibbCanRExwPuYP1CZD7Gd2dxSy1GXRg==", "dev": true, "funding": [ { diff --git a/src/webusb_device.rs b/src/webusb_device.rs index 3e9ae08..dd70374 100644 --- a/src/webusb_device.rs +++ b/src/webusb_device.rs @@ -597,38 +597,23 @@ pub struct UsbDevice { in_endpoints: SharedEndpointCache>, out_endpoints: SharedEndpointCache>, - #[napi(writable = false)] - pub vendorId: u16, - #[napi(writable = false)] - pub productId: u16, - #[napi(writable = false)] - pub deviceVersionMajor: u8, - #[napi(writable = false)] - pub deviceVersionMinor: u8, - #[napi(writable = false)] - pub deviceVersionSubminor: u8, - #[napi(writable = false)] - pub usbVersionMajor: u8, - #[napi(writable = false)] - pub usbVersionMinor: u8, - #[napi(writable = false)] - pub usbVersionSubminor: u8, - #[napi(writable = false)] - pub deviceClass: u8, - #[napi(writable = false)] - pub deviceSubclass: u8, - #[napi(writable = false)] - pub deviceProtocol: u8, + vendorId: u16, + productId: u16, + deviceVersionMajor: u8, + deviceVersionMinor: u8, + deviceVersionSubminor: u8, + usbVersionMajor: u8, + usbVersionMinor: u8, + usbVersionSubminor: u8, + deviceClass: u8, + deviceSubclass: u8, + deviceProtocol: u8, // Non WebUSB props - #[napi(writable = false)] - pub bus: String, - #[napi(writable = false)] - pub address: u8, - #[napi(writable = false)] - pub ports: Vec, - #[napi(writable = false, ts_type = "USBSpeed")] - pub speed: Option, + bus: String, + address: u8, + ports: Vec, + speed: Option, } #[napi] @@ -691,7 +676,82 @@ impl UsbDevice { } #[napi(getter)] - pub unsafe fn manufacturerName(&mut self) -> Result> { + pub fn vendorId(&self) -> u16 { + self.vendorId + } + + #[napi(getter)] + pub fn productId(&self) -> u16 { + self.productId + } + + #[napi(getter)] + pub fn deviceVersionMajor(&self) -> u8 { + self.deviceVersionMajor + } + + #[napi(getter)] + pub fn deviceVersionMinor(&self) -> u8 { + self.deviceVersionMinor + } + + #[napi(getter)] + pub fn deviceVersionSubminor(&self) -> u8 { + self.deviceVersionSubminor + } + + #[napi(getter)] + pub fn usbVersionMajor(&self) -> u8 { + self.usbVersionMajor + } + + #[napi(getter)] + pub fn usbVersionMinor(&self) -> u8 { + self.usbVersionMinor + } + + #[napi(getter)] + pub fn usbVersionSubminor(&self) -> u8 { + self.usbVersionSubminor + } + + #[napi(getter)] + pub fn deviceClass(&self) -> u8 { + self.deviceClass + } + + #[napi(getter)] + pub fn deviceSubclass(&self) -> u8 { + self.deviceSubclass + } + + #[napi(getter)] + pub fn deviceProtocol(&self) -> u8 { + self.deviceProtocol + } + + #[napi(getter)] + pub fn bus(&self) -> String { + self.bus.clone() + } + + #[napi(getter)] + pub fn address(&self) -> u8 { + self.address + } + + #[napi(getter)] + pub fn ports(&self) -> Vec { + self.ports.clone() + } + + #[napi(getter, ts_return_type = "USBSpeed")] + pub fn speed(&self) -> Option { + self.speed.clone() + } + + #[napi(getter)] + pub fn manufacturerName(&self) -> Result> { self.device_info .get_manufacturer_string() .wait() @@ -699,7 +759,7 @@ impl UsbDevice { } #[napi(getter)] - pub unsafe fn productName(&mut self) -> Result> { + pub fn productName(&self) -> Result> { self.device_info .get_product_string() .wait() @@ -707,7 +767,7 @@ impl UsbDevice { } #[napi(getter)] - pub unsafe fn serialNumber(&mut self) -> Result> { + pub fn serialNumber(&self) -> Result> { self.device_info .get_serial_number_string() .wait() @@ -720,7 +780,7 @@ impl UsbDevice { } #[napi(getter, ts_return_type = "USBConfiguration")] - pub unsafe fn configuration(&mut self) -> Result> { + pub fn configuration(&self) -> Result> { let device = match self.device_guard().as_ref() { Some(device) => device.clone(), None => self._open()?, @@ -734,7 +794,7 @@ impl UsbDevice { } #[napi(getter, ts_return_type = "Array")] - pub unsafe fn configurations(&mut self) -> Result> { + pub fn configurations(&self) -> Result> { let device = match self.device_guard().as_ref() { Some(device) => device.clone(), None => self._open()?, @@ -746,7 +806,7 @@ impl UsbDevice { .collect::>>() } - unsafe fn _open(&self) -> Result { + fn _open(&self) -> Result { self.device_info .open() .wait() @@ -754,7 +814,7 @@ impl UsbDevice { } #[napi] - pub async unsafe fn open(&mut self) -> Result<()> { + pub async fn open(&self) -> Result<()> { self.clear_endpoint_caches(); let device_info = self.device_info.clone(); let device = run_blocking(move || { @@ -769,7 +829,7 @@ impl UsbDevice { } #[napi] - pub async unsafe fn close(&self) -> Result<()> { + pub async fn close(&self) -> Result<()> { self.clear_endpoint_caches(); self.interfaces_guard().fill(None); *self.device_guard() = None; @@ -777,7 +837,7 @@ impl UsbDevice { } #[napi] - pub async unsafe fn forget(&self) -> Result<()> { + pub async fn forget(&self) -> Result<()> { self.close().await } @@ -841,7 +901,7 @@ impl UsbDevice { } #[napi] - pub async unsafe fn claimInterface(&mut self, interfaceNumber: u8) -> Result<()> { + pub async fn claimInterface(&self, interfaceNumber: u8) -> Result<()> { let device = self.device_guard().clone(); match device { Some(device) => { @@ -862,7 +922,7 @@ impl UsbDevice { } #[napi] - pub async unsafe fn releaseInterface(&self, interfaceNumber: u8) -> Result<()> { + pub async fn releaseInterface(&self, interfaceNumber: u8) -> Result<()> { if self.device_guard().is_none() { return Err(napi::Error::from_reason( "releaseInterface error: invalid state", @@ -881,8 +941,8 @@ impl UsbDevice { } #[napi] - pub async unsafe fn selectAlternateInterface( - &mut self, + pub async fn selectAlternateInterface( + &self, interfaceNumber: u8, alternateSetting: u8, ) -> Result<()> { diff --git a/test/webusb.js b/test/webusb.js index f22ac05..d514f0c 100644 --- a/test/webusb.js +++ b/test/webusb.js @@ -607,17 +607,19 @@ describe('Active Transfer Cleanup', () => { it('should close while transfer IN is active', async () => { const device = await openClaimedDevice(); const transfer = device.transferIn(3, 64, 60000); + const transferRejection = assert.rejects(transfer); await assert.doesNotReject(device.close()); - await assert.rejects(transfer); + await transferRejection; }); it('should release interface while transfer IN is active', async () => { const device = await openClaimedDevice(); const transfer = device.transferIn(3, 64, 60000); + const transferRejection = assert.rejects(transfer); await assert.doesNotReject(device.releaseInterface(0)); - await assert.rejects(transfer); + await transferRejection; await device.close(); }); });