diff --git a/packages/core/src/internal/usb/model/AccessibleUsbHidDevice.ts b/packages/core/src/internal/usb/model/DiscoveredDevice.ts similarity index 54% rename from packages/core/src/internal/usb/model/AccessibleUsbHidDevice.ts rename to packages/core/src/internal/usb/model/DiscoveredDevice.ts index f8a58f679..9bcbaa2d3 100644 --- a/packages/core/src/internal/usb/model/AccessibleUsbHidDevice.ts +++ b/packages/core/src/internal/usb/model/DiscoveredDevice.ts @@ -1,13 +1,10 @@ import { DeviceId, DeviceModel } from "../../device/model/DeviceModel"; -// TODO: export type ScannedDevice; -// DiscoveredDevice -export type AccessibleUsbHidDevice = { +/** + * Represents a discovered/scanned (not yet connected to) device. + */ +export type DiscoveredDevice = { // type: "web-hid", // "node-hid" in the future -> no need as we will only have 1 USB transport implementation running id: DeviceId, // UUID to map with the associated transport device deviceModel: DeviceModel, - - // TODO: actually not necessary - // Informs if the device was selected (in the case of WebHID) during the discovery - // isSelected: boolean, }; diff --git a/packages/core/src/internal/usb/service/DefaultUsbService.ts b/packages/core/src/internal/usb/service/DefaultUsbService.ts index c0cc0131b..868966abb 100644 --- a/packages/core/src/internal/usb/service/DefaultUsbService.ts +++ b/packages/core/src/internal/usb/service/DefaultUsbService.ts @@ -1,7 +1,7 @@ import { Observable } from "rxjs"; import { inject, injectable } from "inversify"; import { types } from "@internal/usb/di/usbTypes"; -import { AccessibleUsbHidDevice } from "@internal/usb/model/AccessibleUsbHidDevice"; +import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice"; import type { UsbHidTransport } from "@internal/usb/transport/UsbHidTransport"; import { UsbService } from "./UsbService"; @@ -10,12 +10,12 @@ export class DefaultUsbService implements UsbService { constructor(@inject(types.UsbHidTransport) private usbHidTransport: UsbHidTransport) {} - // async promptDeviceAccess(): Promise> { + // async promptDeviceAccess(): Promise> { // // [ASK] or should we re-map the errors ? // return this.usbHidTransport.promptDeviceAccess(); // } - startDiscovering(): Observable { + startDiscovering(): Observable { return this.usbHidTransport.startDiscovering(); } diff --git a/packages/core/src/internal/usb/service/UsbService.ts b/packages/core/src/internal/usb/service/UsbService.ts index 7ea2f1485..61d22d221 100644 --- a/packages/core/src/internal/usb/service/UsbService.ts +++ b/packages/core/src/internal/usb/service/UsbService.ts @@ -1,17 +1,17 @@ -import { AccessibleUsbHidDevice } from "@internal/usb/model/AccessibleUsbHidDevice"; +import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice"; import { Observable } from "rxjs"; // [ASK] Are we exposing transport interface internal errors that should not be exposed ? // export type PromptDeviceAccessError = UsbHidTransportPromptDeviceAccessError; -export type StartDiscoveringReturn = Observable; +export type StartDiscoveringReturn = Observable; export interface UsbService { // [ASK] But should not return errors to our user too ? So purity's Either ? // And inside the use-case we can handle the error and return either a union type with error, or throw ? - // promptDeviceAccess(): Promise; - // promptDeviceAccess(): Promise>; + // promptDeviceAccess(): Promise; + // promptDeviceAccess(): Promise>; /** * Starts discovering USB devices. diff --git a/packages/core/src/internal/usb/transport/UsbHidTransport.ts b/packages/core/src/internal/usb/transport/UsbHidTransport.ts index 23127b71a..8cb1c998e 100644 --- a/packages/core/src/internal/usb/transport/UsbHidTransport.ts +++ b/packages/core/src/internal/usb/transport/UsbHidTransport.ts @@ -1,4 +1,4 @@ -import { AccessibleUsbHidDevice } from "@internal/usb/model/AccessibleUsbHidDevice"; +import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice"; import { Observable } from "rxjs"; export class UsbHidTransportNotSupported { @@ -29,14 +29,14 @@ export type GetAccessibleDevicesError = UsbHidTransportNotSupported; export interface UsbHidTransport { isSupported(): boolean; - // getAccessibleDevices(): Promise>; + // getAccessibleDevices(): Promise>; - // promptDeviceAccess(): Promise>; + // promptDeviceAccess(): Promise>; - startDiscovering(): Observable; + startDiscovering(): Observable; stopDiscovering(): void; // TODO: should return a Either using Purify - connect(params: { device: AccessibleUsbHidDevice }): Promise; + connect(params: { device: DiscoveredDevice }): Promise; } diff --git a/packages/core/src/internal/usb/transport/WebUsbHidTransport.ts b/packages/core/src/internal/usb/transport/WebUsbHidTransport.ts index 9cc7df92a..5c98a284e 100644 --- a/packages/core/src/internal/usb/transport/WebUsbHidTransport.ts +++ b/packages/core/src/internal/usb/transport/WebUsbHidTransport.ts @@ -2,20 +2,20 @@ import { Either, EitherAsync, Left, Right } from "purify-ts"; import { injectable } from "inversify"; import { v4 as uuid } from "uuid"; import { ledgerVendorId } from '@internal/usb/data/UsbHidConfig'; -import { AccessibleUsbHidDevice } from '@internal/usb/model/AccessibleUsbHidDevice'; +import { DiscoveredDevice } from '@internal/usb/model/DiscoveredDevice'; import { NoAccessibleDevice, UsbHidTransportNotSupported, type GetAccessibleDevicesError, type PromptDeviceAccessError, type UsbHidTransport } from "./UsbHidTransport"; import { Observable, from, switchMap } from "rxjs"; @injectable() export class WebUsbHidTransport implements UsbHidTransport { - // Maps uncoupled AccessibleUsbHidDevice and WebHID's HIDDevice WebHID + // Maps uncoupled DiscoveredDevice and WebHID's HIDDevice WebHID // TODO: ScannedDevice - private accessibleDevicesToHidDevices: Map; + private discoveredDevicesToHidDevices: Map; private connectionListenersAbortController: AbortController; constructor() { - this.accessibleDevicesToHidDevices = new Map() + this.discoveredDevicesToHidDevices = new Map() this.connectionListenersAbortController = new AbortController(); } @@ -47,7 +47,7 @@ export class WebUsbHidTransport implements UsbHidTransport { * * @returns */ - getAccessibleDevices(): Promise> { + getAccessibleDevices(): Promise> { return EitherAsync.liftEither(this.hidApi()).map(async (hidApi) => { let hidDevices: HIDDevice[] = []; @@ -59,29 +59,29 @@ export class WebUsbHidTransport implements UsbHidTransport { console.log('hidDevices', hidDevices); - const accessibleDevices: AccessibleUsbHidDevice[] = []; + const discoveredDevices: DiscoveredDevice[] = []; // TODO: each time we get the list ? Or we only update if we don't have the device already ? // I don't think we should create a uuid each time we get the list // But if there is no unique identifier for the device from the USB/HID connection, we can't do much - this.accessibleDevicesToHidDevices.clear(); + this.discoveredDevicesToHidDevices.clear(); for (const hidDevice of hidDevices) { const deviceId = uuid(); - const accessibleUsbHidDevice: AccessibleUsbHidDevice = { + const discoveredDevice: DiscoveredDevice = { id: deviceId, // TODO deviceModel: "nanoX", }; - this.accessibleDevicesToHidDevices.set(accessibleUsbHidDevice, hidDevice); - accessibleDevices.push(accessibleUsbHidDevice); + this.discoveredDevicesToHidDevices.set(discoveredDevice, hidDevice); + discoveredDevices.push(discoveredDevice); } - return accessibleDevices; + return discoveredDevices; }).run(); } - async promptDeviceAccess(): Promise> { + async promptDeviceAccess(): Promise> { return EitherAsync.liftEither(this.hidApi()).map(async (hidApi) => { // `requestDevice` returns an array. but normally the user can select only one device at a time. @@ -102,27 +102,27 @@ export class WebUsbHidTransport implements UsbHidTransport { throw new NoAccessibleDevice(new Error("No selected device")); } - const accessibleDevices: AccessibleUsbHidDevice[] = []; + const discoveredDevices: DiscoveredDevice[] = []; // There is no unique identifier for the device from the USB/HID connection, // so the previously known accessible devices list cannot be trusted. - this.accessibleDevicesToHidDevices.clear(); + this.discoveredDevicesToHidDevices.clear(); for (const hidDevice of hidDevices) { const id = uuid(); - const accessibleUsbHidDevice: AccessibleUsbHidDevice = { + const discoveredDevice: DiscoveredDevice = { id, // TODO deviceModel: "nanoX", }; - this.accessibleDevicesToHidDevices.set(accessibleUsbHidDevice, hidDevice); - accessibleDevices.push(accessibleUsbHidDevice); + this.discoveredDevicesToHidDevices.set(discoveredDevice, hidDevice); + discoveredDevices.push(discoveredDevice); console.log("promptDeviceAccess: selected device", hidDevice); - console.log("promptDeviceAccess: AccessibleUsbHidDevice", id); + console.log("promptDeviceAccess: discoveredDevice", id); } - return accessibleDevices; + return discoveredDevices; }).run(); } @@ -142,10 +142,10 @@ export class WebUsbHidTransport implements UsbHidTransport { * * [ASK] Should we also subscribe to hid events `connect` and `disconnect` ? * - * [ASK] For the 2nd option: the AccessibleUsbHidDevice could have a `isSelected` property ? + * [ASK] For the 2nd option: the DiscoveredDevice could have a `isSelected` property ? * So the consumer can directly select this device. */ - startDiscovering(): Observable { + startDiscovering(): Observable { // Logs the connection and disconnection events this.startListeningToConnectionEvents(); @@ -187,7 +187,7 @@ export class WebUsbHidTransport implements UsbHidTransport { } // TODO: should return a Result/Success using Purify - connect(_params: { device: AccessibleUsbHidDevice }): Promise { + connect(_params: { device: DiscoveredDevice }): Promise { return Promise.resolve(); } }