jacdac-ts
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -7,3 +7,3 @@ import { Packet } from "./packet"; | ||
sendPacketAsync?: (p: Packet) => Promise<void>; | ||
connectAsync?: () => Promise<void>; | ||
connectAsync?: (userRequest?: boolean) => Promise<void>; | ||
disconnectAsync?: () => Promise<void>; | ||
@@ -42,3 +42,3 @@ } | ||
get id(): string; | ||
node(id: string): import("./service").Service | import("./register").Register | Device | this; | ||
node(id: string): this | Device | import("./service").Service | import("./register").Register; | ||
private resetTime; | ||
@@ -54,3 +54,3 @@ get timestamp(): number; | ||
errorHandler(context: string, exception: any): void; | ||
connectAsync(): Promise<void>; | ||
connectAsync(userRequest?: boolean): Promise<void>; | ||
disconnectAsync(): Promise<void>; | ||
@@ -70,2 +70,3 @@ /** | ||
private gcDevices; | ||
private disconnectDevice; | ||
/** | ||
@@ -72,0 +73,0 @@ * Ingests and process a packet received from the bus. |
/// <reference types="w3c-web-usb" /> | ||
import * as U from "./utils"; | ||
import { USBOptions } from "./usb"; | ||
export declare const HF2_DEVICE_MAJOR = 42; | ||
@@ -31,3 +32,3 @@ export declare const HF2_CMD_BININFO = 1; | ||
export declare class Transport { | ||
private requestDevice; | ||
private usb; | ||
dev: USBDevice; | ||
@@ -40,3 +41,3 @@ iface: USBInterface; | ||
ready: boolean; | ||
constructor(requestDevice: (options: USBDeviceRequestOptions) => Promise<USBDevice>); | ||
constructor(usb: USBOptions); | ||
onData: (v: Uint8Array) => void; | ||
@@ -51,3 +52,7 @@ onError: (e: Error) => void; | ||
sendPacketAsync(pkt: Uint8Array): Promise<void>; | ||
init(): Promise<void>; | ||
private checkDevice; | ||
private tryReconnectAsync; | ||
private requestDeviceAsync; | ||
connectAsync(userInteraction: boolean): Promise<void>; | ||
private openDeviceAsync; | ||
} | ||
@@ -69,4 +74,4 @@ export declare class Proto { | ||
onSerial(data: Uint8Array, iserr: boolean): void; | ||
init(): Promise<void>; | ||
connectAsync(userRequest?: boolean): Promise<void>; | ||
disconnectAsync(): Promise<void>; | ||
} |
/// <reference types="w3c-web-usb" /> | ||
import { Bus } from "./bus"; | ||
export interface UsbBusOptions { | ||
requestDevice?: (options: USBDeviceRequestOptions) => Promise<USBDevice>; | ||
export interface USBOptions { | ||
getDevices(): Promise<USBDevice[]>; | ||
requestDevice(options: USBDeviceRequestOptions): Promise<USBDevice>; | ||
addEventListener(type: "connect" | "disconnect", listener: (this: this, ev: USBConnectionEvent) => any, useCapture?: boolean): void; | ||
removeEventListener(type: "connect" | "disconnect", callback: (this: this, ev: USBConnectionEvent) => any, useCapture?: boolean): void; | ||
} | ||
export declare function createUSBBus(options?: UsbBusOptions): Bus; | ||
export declare function isWebUSBSupported(): boolean; | ||
export declare function createUSBBus(options?: USBOptions): Bus; |
/// <reference types="react" /> | ||
import { Bus } from "../dom/bus"; | ||
import { Bus, BusState } from "../dom/bus"; | ||
export interface JacDacContextProps { | ||
bus: Bus; | ||
connected: boolean; | ||
connecting: boolean; | ||
connectionState: BusState; | ||
connectAsync: () => Promise<void>; | ||
@@ -8,0 +7,0 @@ disconnectAsync: () => Promise<void>; |
export * from './Context'; | ||
export * from './useQuery'; |
{ | ||
"name": "jacdac-ts", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -29,3 +29,3 @@ import { Packet } from "./packet"; | ||
sendPacketAsync?: (p: Packet) => Promise<void>; | ||
connectAsync?: () => Promise<void>; | ||
connectAsync?: (userRequest?: boolean) => Promise<void>; | ||
disconnectAsync?: () => Promise<void>; | ||
@@ -66,2 +66,3 @@ } | ||
super(); | ||
console.log(`new bus...`) | ||
this.options = this.options || {}; | ||
@@ -154,3 +155,3 @@ this.resetTime(); | ||
connectAsync(): Promise<void> { | ||
connectAsync(userRequest?: boolean): Promise<void> { | ||
// already connected | ||
@@ -169,3 +170,3 @@ if (this.connectionState == BusState.Connected) | ||
this.setConnectionState(BusState.Connecting) | ||
const connectAsyncPromise = this.options?.connectAsync() || Promise.resolve(); | ||
const connectAsyncPromise = this.options?.connectAsync(userRequest) || Promise.resolve(); | ||
this._connectPromise = connectAsyncPromise | ||
@@ -207,2 +208,4 @@ .then(() => { | ||
this._disconnectPromise = undefined; | ||
this._devices.forEach(device => this.disconnectDevice(device)) | ||
this._devices = [] | ||
this.setConnectionState(BusState.Disconnected); | ||
@@ -225,3 +228,2 @@ }); | ||
let r = this._devices.slice(); | ||
@@ -256,4 +258,3 @@ if (sc > -1) r = r.filter(s => s.hasService(sc)) | ||
i-- | ||
dev.disconnect(); | ||
this.emit(DEVICE_DISCONNECT, dev); | ||
this.disconnectDevice(dev) | ||
} | ||
@@ -263,2 +264,7 @@ } | ||
private disconnectDevice(dev: Device) { | ||
dev.disconnect(); | ||
this.emit(DEVICE_DISCONNECT, dev); | ||
} | ||
/** | ||
@@ -265,0 +271,0 @@ * Ingests and process a packet received from the bus. |
import * as U from "./utils" | ||
import { Bus } from "./bus"; | ||
import { Packet } from "./packet"; | ||
import { USBOptions } from "./usb"; | ||
@@ -108,3 +107,3 @@ const controlTransferGetReport = 0x01; | ||
constructor(private requestDevice: (options: USBDeviceRequestOptions) => Promise<USBDevice>) { | ||
constructor(private usb: USBOptions) { | ||
@@ -233,14 +232,8 @@ } | ||
async init() { | ||
this.dev = await this.requestDevice({ | ||
filters: [{ | ||
classCode: 255, | ||
subclassCode: HF2_DEVICE_MAJOR, | ||
}] | ||
}) | ||
private checkDevice() { | ||
this.iface = undefined; | ||
this.altIface = undefined; | ||
if (!this.dev) | ||
return false; | ||
this.log("connect device: " + this.dev.manufacturerName + " " + this.dev.productName) | ||
// resolve interfaces | ||
@@ -255,8 +248,44 @@ for (const iface of this.dev.configuration.interfaces) { | ||
} | ||
return !!this.iface; | ||
} | ||
if (!this.iface) throw new Error("HF2 interface not found") | ||
private async tryReconnectAsync() { | ||
try { | ||
const devices = await this.usb.getDevices(); | ||
this.dev = devices[0]; | ||
} catch (e) { | ||
console.log(e) | ||
this.dev = undefined; | ||
} | ||
} | ||
private async requestDeviceAsync() { | ||
try { | ||
this.dev = await this.usb.requestDevice({ | ||
filters: [{ | ||
classCode: 255, | ||
subclassCode: HF2_DEVICE_MAJOR, | ||
}] | ||
}) | ||
} catch (e) { | ||
console.log(e) | ||
this.dev = undefined; | ||
} | ||
} | ||
async connectAsync(userInteraction: boolean) { | ||
await this.tryReconnectAsync(); | ||
if (!this.dev && userInteraction) | ||
await this.requestDeviceAsync(); | ||
await this.openDeviceAsync(); | ||
} | ||
private async openDeviceAsync() { | ||
if (!this.dev) | ||
throw new Error("device not found") | ||
if (!this.checkDevice()) | ||
throw new Error("device does not support HF2") | ||
await this.dev.open() | ||
await this.dev.selectConfiguration(1) | ||
if (this.altIface.endpoints.length) { | ||
@@ -420,4 +449,4 @@ this.epIn = this.altIface.endpoints.filter(e => e.direction == "in")[0] | ||
async init() { | ||
await this.io.init() | ||
async connectAsync(userRequest?: boolean) { | ||
await this.io.connectAsync(userRequest) | ||
const buf = await this.talkAsync(HF2_CMD_INFO) | ||
@@ -424,0 +453,0 @@ this.io.log("Connected to: " + U.bufferToString(buf)) |
import { Transport, Proto } from "./hf2"; | ||
import { Bus } from "./bus"; | ||
import { Packet } from "./packet"; | ||
import { assert } from "./utils"; | ||
export interface UsbBusOptions { | ||
requestDevice?: (options: USBDeviceRequestOptions) => Promise<USBDevice> | ||
export interface USBOptions { | ||
getDevices(): Promise<USBDevice[]>; | ||
requestDevice(options: USBDeviceRequestOptions): Promise<USBDevice> | ||
addEventListener(type: "connect" | "disconnect", listener: (this: this, ev: USBConnectionEvent) => any, useCapture?: boolean): void; | ||
removeEventListener(type: "connect" | "disconnect", callback: (this: this, ev: USBConnectionEvent) => any, useCapture?: boolean): void; | ||
} | ||
export function createUSBBus(options?: UsbBusOptions): Bus { | ||
let requestDevice = options?.requestDevice; | ||
if (!requestDevice && typeof navigator !== "undefined" && navigator.usb && navigator.usb.requestDevice) { | ||
requestDevice = options => navigator.usb.requestDevice(options); | ||
export function isWebUSBSupported() { | ||
return typeof navigator !== "undefined" && navigator.usb | ||
&& !!navigator.usb.requestDevice | ||
&& !!navigator.usb.getDevices; | ||
} | ||
export function createUSBBus(options?: USBOptions): Bus { | ||
if (!options) { | ||
if (isWebUSBSupported()) | ||
options = navigator.usb | ||
} | ||
assert(!!options) | ||
let hf2: Proto; | ||
const bus = new Bus({ | ||
connectAsync: () => { | ||
connectAsync: (userRequest) => { | ||
if (hf2) return Promise.resolve(); | ||
const transport = new Transport(requestDevice); | ||
const transport = new Transport(options); | ||
hf2 = new Proto(transport); | ||
@@ -26,3 +37,4 @@ const onJDMessage = (buf: Uint8Array) => { | ||
} | ||
return hf2.init().then(() => hf2.onJDMessage(onJDMessage)) | ||
return hf2.connectAsync(userRequest) | ||
.then(() => hf2.onJDMessage(onJDMessage)) | ||
}, | ||
@@ -29,0 +41,0 @@ sendPacketAsync: p => { |
export * from './Context' | ||
export * from './useQuery' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
0
0
4072532
72
38023