From c963f3774a32e75fb1e0aea414e1a687b34b425b Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 24 Jul 2022 19:10:29 +0530 Subject: [PATCH] 0.5.0 --- Cargo.toml | 6 +- README.md | 2 +- bindings/bindings.ts | 318 +++++++++++++++++++++++-------------------- 3 files changed, 175 insertions(+), 151 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a467e25..449eb12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "webusb" description = "WebUSB API implementation in Rust" -version = "0.4.0" +version = "0.5.0" authors = ["Divy Srivastava "] edition = "2018" repository = "https://github.com/littledivy/webusb" @@ -27,9 +27,9 @@ serde_derive = ["serde"] rusb = { version = "0.8.1", optional = true } libusb1-sys = { version = "0.5.0" , optional = true } serde = { version = "1", features = ["derive"], optional = true } -deno_bindgen = { version = "0.5.0", optional = true } +deno_bindgen = { version = "0.6.0", optional = true } once_cell = { version = "1.9.0", optional = true } [dev-dependencies] # For a loose connection :-) -flaky_test = "0.1.0" \ No newline at end of file +flaky_test = "0.1.0" diff --git a/README.md b/README.md index 5ec94a7..a204ed8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Rust. ```toml [dependencies] -webusb = "0.4.0" +webusb = "0.5.0" ``` [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/X8X4Y6IZ) diff --git a/bindings/bindings.ts b/bindings/bindings.ts index dde1f48..9e8daa7 100644 --- a/bindings/bindings.ts +++ b/bindings/bindings.ts @@ -1,29 +1,53 @@ // Auto-generated with deno_bindgen -import { CachePolicy, prepare } from "https://deno.land/x/plug@0.5.1/plug.ts"; +import { CachePolicy, prepare } from "https://deno.land/x/plug@0.5.2/plug.ts" + function encode(v: string | Uint8Array): Uint8Array { - if (typeof v !== "string") return v; - return new TextEncoder().encode(v); + if (typeof v !== "string") return v + return new TextEncoder().encode(v) } + function decode(v: Uint8Array): string { - return new TextDecoder().decode(v); + return new TextDecoder().decode(v) } + function readPointer(v: any): Uint8Array { - const ptr = new Deno.UnsafePointerView(v as Deno.UnsafePointer); - const lengthBe = new Uint8Array(4); - const view = new DataView(lengthBe.buffer); - ptr.copyInto(lengthBe, 0); - const buf = new Uint8Array(view.getUint32(0)); - ptr.copyInto(buf, 4); - return buf; + const ptr = new Deno.UnsafePointerView(v as bigint) + const lengthBe = new Uint8Array(4) + const view = new DataView(lengthBe.buffer) + ptr.copyInto(lengthBe, 0) + const buf = new Uint8Array(view.getUint32(0)) + ptr.copyInto(buf, 4) + return buf } + +const url = new URL( + "https://github.com/littledivy/webusb/releases/download/0.5.0", + import.meta.url, +) +let uri = url.toString() +if (!uri.endsWith("/")) uri += "/" + +let darwin: string | { aarch64: string; x86_64: string } = uri + + "libwebusb.dylib" + +if (url.protocol !== "file:") { + // Assume that remote assets follow naming scheme + // for each macOS artifact. + darwin = { + aarch64: uri + "libwebusb_arm64.dylib", + x86_64: uri + "libwebusb.dylib", + } +} + const opts = { name: "webusb", - url: (new URL( - "https://github.com/littledivy/webusb/releases/download/0.4.0", - import.meta.url, - )).toString(), + urls: { + darwin, + windows: uri + "webusb.dll", + linux: uri + "libwebusb.so", + }, policy: undefined, -}; +} const _lib = await prepare(opts, { claim_interface: { parameters: ["pointer", "usize", "u8"], @@ -86,32 +110,18 @@ const _lib = await prepare(opts, { result: "void", nonblocking: false, }, -}); +}) +export type UsbInterface = { + interfaceNumber: number + alternate: UsbAlternateInterface + alternates: Array + claimed: boolean +} export type UsbEndpointType = | "bulk" | "interrupt" | "isochronous" - | "control"; -export type Devices = { - devices: Array; -}; -export type UsbAlternateInterface = { - alternateSetting: number; - interfaceClass: number; - interfaceSubclass: number; - interfaceProtocol: number; - interfaceName: string | undefined | null; - endpoints: Array; -}; -export type Direction = - | "in" - | "out"; -export type UsbEndpoint = { - endpointNumber: number; - direction: Direction; - type: UsbEndpointType; - packetSize: number; -}; + | "control" /** * Represents a UsbDevice. * Only way you can obtain one is through `Context::devices` @@ -124,181 +134,195 @@ export type UsbDevice = { * `configurations.len()` SHALL be equal to the * bNumConfigurations field of the device descriptor. */ - configurations: Array; + configurations: Array /** * Represents the currently selected configuration. * One of the elements of `self.configurations`. * None, if the device is not configured. */ - configuration: UsbConfiguration | undefined | null; + configuration: UsbConfiguration | undefined | null /** * bDeviceClass value of the device descriptor. */ - deviceClass: number; + deviceClass: number /** * bDeviceSubClass value of the device descriptor. */ - deviceSubclass: number; + deviceSubclass: number /** * bDeviceProtocol value of the device descriptor. */ - deviceProtocol: number; + deviceProtocol: number /** * The major version declared by bcdDevice field * such that bcdDevice 0xJJMN represents major version JJ. */ - deviceVersionMajor: number; + deviceVersionMajor: number /** * The minor version declared by bcdDevice field * such that bcdDevice 0xJJMN represents minor version M. */ - deviceVersionMinor: number; + deviceVersionMinor: number /** * The subminor version declared by bcdDevice field * such that bcdDevice 0xJJMN represents subminor version N. */ - deviceVersionSubminor: number; + deviceVersionSubminor: number /** * Optional property of the string descriptor. * Indexed by the iManufacturer field of device descriptor. */ - manufacturerName: string | undefined | null; + manufacturerName: string | undefined | null /** * idProduct field of the device descriptor. */ - productId: number; + productId: number /** * Optional property of the string descriptor. * Indexed by the iProduct field of device descriptor. */ - productName: string | undefined | null; + productName: string | undefined | null /** * Optional property of the string descriptor. * None, if the iSerialNumber field of device descriptor * is 0. */ - serialNumber: string | undefined | null; + serialNumber: string | undefined | null /** * The major version declared by bcdUSB field * such that bcdUSB 0xJJMN represents major version JJ. */ - usbVersionMajor: number; + usbVersionMajor: number /** * The minor version declared by bcdUSB field * such that bcdUSB 0xJJMN represents minor version M. */ - usbVersionMinor: number; + usbVersionMinor: number /** * The subminor version declared by bcdUSB field * such that bcdUSB 0xJJMN represents subminor version N. */ - usbVersionSubminor: number; + usbVersionSubminor: number /** * idVendor field of the device descriptor. * https://wicg.github.io/webusb/#vendor-id */ - vendorId: number; + vendorId: number /** * If true, the underlying device handle is owned by this object. */ - opened: boolean; + opened: boolean /** * WEBUSB_URL value of the WebUSB Platform Capability Descriptor. */ - url: string | undefined | null; + url: string | undefined | null /** * Resource ID associated with this Device instance. */ - rid: number; - device: Device; - deviceHandle: DeviceHandle | undefined | null; -}; -export type UsbRequestType = - | "standard" - | "class" - | "vendor"; -export type UsbInterface = { - interfaceNumber: number; - alternate: UsbAlternateInterface; - alternates: Array; - claimed: boolean; -}; + rid: number + device: Device + deviceHandle: DeviceHandle | undefined | null +} +export type UsbControlTransferParameters = { + requestType: UsbRequestType + recipient: UsbRecipient + request: number + value: number + index: number +} export type FfiDirection = { - inner: Direction; -}; + inner: Direction +} export type UsbRecipient = | "device" | "interface" | "endpoint" - | "other"; -export type UsbConfiguration = { - configurationName: string | undefined | null; - configurationValue: number; - interfaces: Array; -}; + | "other" +export type Direction = + | "in" + | "out" export type Device = { - device: UsbDevice; -}; + device: UsbDevice +} +export type UsbConfiguration = { + configurationName: string | undefined | null + configurationValue: number + interfaces: Array +} +export type Devices = { + devices: Array +} +export type UsbEndpoint = { + endpointNumber: number + direction: Direction + type: UsbEndpointType + packetSize: number +} +export type UsbAlternateInterface = { + alternateSetting: number + interfaceClass: number + interfaceSubclass: number + interfaceProtocol: number + interfaceName: string | undefined | null + endpoints: Array +} +export type UsbRequestType = + | "standard" + | "class" + | "vendor" export type FfiUsbControlTransferParameters = { - inner: UsbControlTransferParameters; -}; -export type UsbControlTransferParameters = { - requestType: UsbRequestType; - recipient: UsbRecipient; - request: number; - value: number; - index: number; -}; + inner: UsbControlTransferParameters +} export function claim_interface(a0: Device, a1: number) { - const a0_buf = encode(JSON.stringify(a0)); - let rawResult = _lib.symbols.claim_interface(a0_buf, a0_buf.byteLength, a1); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + const a0_buf = encode(JSON.stringify(a0)) + let rawResult = _lib.symbols.claim_interface(a0_buf, a0_buf.byteLength, a1) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function clear_halt(a0: Device, a1: FfiDirection, a2: number) { - const a0_buf = encode(JSON.stringify(a0)); - const a1_buf = encode(JSON.stringify(a1)); + const a0_buf = encode(JSON.stringify(a0)) + const a1_buf = encode(JSON.stringify(a1)) let rawResult = _lib.symbols.clear_halt( a0_buf, a0_buf.byteLength, a1_buf, a1_buf.byteLength, a2, - ); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + ) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function close(a0: Device) { - const a0_buf = encode(JSON.stringify(a0)); - let rawResult = _lib.symbols.close(a0_buf, a0_buf.byteLength); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + const a0_buf = encode(JSON.stringify(a0)) + let rawResult = _lib.symbols.close(a0_buf, a0_buf.byteLength) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function control_transfer_in( a0: Device, a1: FfiUsbControlTransferParameters, - a2: number, + a2: bigint, ) { - const a0_buf = encode(JSON.stringify(a0)); - const a1_buf = encode(JSON.stringify(a1)); + const a0_buf = encode(JSON.stringify(a0)) + const a1_buf = encode(JSON.stringify(a1)) let rawResult = _lib.symbols.control_transfer_in( a0_buf, a0_buf.byteLength, a1_buf, a1_buf.byteLength, a2, - ); - const result = readPointer(rawResult); - return result; + ) + const result = readPointer(rawResult) + return result } export function control_transfer_out( a0: Device, a1: FfiUsbControlTransferParameters, a2: Uint8Array, ) { - const a0_buf = encode(JSON.stringify(a0)); - const a1_buf = encode(JSON.stringify(a1)); - const a2_buf = encode(a2); + const a0_buf = encode(JSON.stringify(a0)) + const a1_buf = encode(JSON.stringify(a1)) + const a2_buf = encode(a2) let rawResult = _lib.symbols.control_transfer_out( a0_buf, a0_buf.byteLength, @@ -306,70 +330,70 @@ export function control_transfer_out( a1_buf.byteLength, a2_buf, a2_buf.byteLength, - ); - const result = rawResult; - return result; + ) + const result = rawResult + return result } export function get_devices() { - let rawResult = _lib.symbols.get_devices(); - const result = rawResult.then(readPointer); - return result.then((r) => JSON.parse(decode(r))) as Promise; + let rawResult = _lib.symbols.get_devices() + const result = rawResult.then(readPointer) + return result.then(r => JSON.parse(decode(r))) as Promise } export function open(a0: Device) { - const a0_buf = encode(JSON.stringify(a0)); - let rawResult = _lib.symbols.open(a0_buf, a0_buf.byteLength); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + const a0_buf = encode(JSON.stringify(a0)) + let rawResult = _lib.symbols.open(a0_buf, a0_buf.byteLength) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function release_interface(a0: Device, a1: number) { - const a0_buf = encode(JSON.stringify(a0)); - let rawResult = _lib.symbols.release_interface(a0_buf, a0_buf.byteLength, a1); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + const a0_buf = encode(JSON.stringify(a0)) + let rawResult = _lib.symbols.release_interface(a0_buf, a0_buf.byteLength, a1) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function reset(a0: Device) { - const a0_buf = encode(JSON.stringify(a0)); - let rawResult = _lib.symbols.reset(a0_buf, a0_buf.byteLength); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + const a0_buf = encode(JSON.stringify(a0)) + let rawResult = _lib.symbols.reset(a0_buf, a0_buf.byteLength) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function select_alternate_interface(a0: Device, a1: number, a2: number) { - const a0_buf = encode(JSON.stringify(a0)); + const a0_buf = encode(JSON.stringify(a0)) let rawResult = _lib.symbols.select_alternate_interface( a0_buf, a0_buf.byteLength, a1, a2, - ); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + ) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } export function select_configuration(a0: Device, a1: number) { - const a0_buf = encode(JSON.stringify(a0)); + const a0_buf = encode(JSON.stringify(a0)) let rawResult = _lib.symbols.select_configuration( a0_buf, a0_buf.byteLength, a1, - ); - const result = readPointer(rawResult); - return JSON.parse(decode(result)) as Device; + ) + const result = readPointer(rawResult) + return JSON.parse(decode(result)) as Device } -export function transfer_in(a0: Device, a1: number, a2: number) { - const a0_buf = encode(JSON.stringify(a0)); - let rawResult = _lib.symbols.transfer_in(a0_buf, a0_buf.byteLength, a1, a2); - const result = readPointer(rawResult); - return result; +export function transfer_in(a0: Device, a1: number, a2: bigint) { + const a0_buf = encode(JSON.stringify(a0)) + let rawResult = _lib.symbols.transfer_in(a0_buf, a0_buf.byteLength, a1, a2) + const result = readPointer(rawResult) + return result } export function transfer_out(a0: Device, a1: number, a2: Uint8Array) { - const a0_buf = encode(JSON.stringify(a0)); - const a2_buf = encode(a2); + const a0_buf = encode(JSON.stringify(a0)) + const a2_buf = encode(a2) let rawResult = _lib.symbols.transfer_out( a0_buf, a0_buf.byteLength, a1, a2_buf, a2_buf.byteLength, - ); - const result = rawResult; - return result; + ) + const result = rawResult + return result }