Skip to content

Commit

Permalink
chore: rename to DiscoveredDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandremgo committed Feb 2, 2024
1 parent 02478dd commit b4e2019
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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,
};
6 changes: 3 additions & 3 deletions packages/core/src/internal/usb/service/DefaultUsbService.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -10,12 +10,12 @@ export class DefaultUsbService implements UsbService {

constructor(@inject(types.UsbHidTransport) private usbHidTransport: UsbHidTransport) {}

// async promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, AccessibleUsbHidDevice>> {
// async promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, DiscoveredDevice>> {
// // [ASK] or should we re-map the errors ?
// return this.usbHidTransport.promptDeviceAccess();
// }

startDiscovering(): Observable<AccessibleUsbHidDevice> {
startDiscovering(): Observable<DiscoveredDevice> {
return this.usbHidTransport.startDiscovering();
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/internal/usb/service/UsbService.ts
Original file line number Diff line number Diff line change
@@ -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<AccessibleUsbHidDevice>;
export type StartDiscoveringReturn = Observable<DiscoveredDevice>;

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<AccessibleUsbHidDevice | null>;
// promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, AccessibleUsbHidDevice>>;
// promptDeviceAccess(): Promise<DiscoveredDevice | null>;
// promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, DiscoveredDevice>>;

/**
* Starts discovering USB devices.
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/internal/usb/transport/UsbHidTransport.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -29,14 +29,14 @@ export type GetAccessibleDevicesError = UsbHidTransportNotSupported;
export interface UsbHidTransport {
isSupported(): boolean;

// getAccessibleDevices(): Promise<Either<GetAccessibleDevicesError, AccessibleUsbHidDevice[]>>;
// getAccessibleDevices(): Promise<Either<GetAccessibleDevicesError, DiscoveredDevice[]>>;

// promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, AccessibleUsbHidDevice>>;
// promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, DiscoveredDevice>>;

startDiscovering(): Observable<AccessibleUsbHidDevice>;
startDiscovering(): Observable<DiscoveredDevice>;

stopDiscovering(): void;

// TODO: should return a Either using Purify
connect(params: { device: AccessibleUsbHidDevice }): Promise<void>;
connect(params: { device: DiscoveredDevice }): Promise<void>;
}
44 changes: 22 additions & 22 deletions packages/core/src/internal/usb/transport/WebUsbHidTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccessibleUsbHidDevice, HIDDevice>;
private discoveredDevicesToHidDevices: Map<DiscoveredDevice, HIDDevice>;
private connectionListenersAbortController: AbortController;

constructor() {
this.accessibleDevicesToHidDevices = new Map()
this.discoveredDevicesToHidDevices = new Map()
this.connectionListenersAbortController = new AbortController();
}

Expand Down Expand Up @@ -47,7 +47,7 @@ export class WebUsbHidTransport implements UsbHidTransport {
*
* @returns
*/
getAccessibleDevices(): Promise<Either<GetAccessibleDevicesError, AccessibleUsbHidDevice[]>> {
getAccessibleDevices(): Promise<Either<GetAccessibleDevicesError, DiscoveredDevice[]>> {
return EitherAsync.liftEither(this.hidApi()).map(async (hidApi) => {
let hidDevices: HIDDevice[] = [];

Expand All @@ -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<Either<PromptDeviceAccessError, AccessibleUsbHidDevice[]>> {
async promptDeviceAccess(): Promise<Either<PromptDeviceAccessError, DiscoveredDevice[]>> {

return EitherAsync.liftEither(this.hidApi()).map(async (hidApi) => {
// `requestDevice` returns an array. but normally the user can select only one device at a time.
Expand All @@ -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();
}

Expand All @@ -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<AccessibleUsbHidDevice> {
startDiscovering(): Observable<DiscoveredDevice> {
// Logs the connection and disconnection events
this.startListeningToConnectionEvents();

Expand Down Expand Up @@ -187,7 +187,7 @@ export class WebUsbHidTransport implements UsbHidTransport {
}

// TODO: should return a Result/Success using Purify
connect(_params: { device: AccessibleUsbHidDevice }): Promise<void> {
connect(_params: { device: DiscoveredDevice }): Promise<void> {
return Promise.resolve();
}
}

0 comments on commit b4e2019

Please sign in to comment.