Skip to content

Commit

Permalink
feat: adapt lowlevel transport
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Aug 11, 2023
1 parent 820e2d0 commit 58f79ec
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 30 deletions.
14 changes: 9 additions & 5 deletions packages/core/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import semver from 'semver';
import EventEmitter from 'events';
import { Features, OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
import { Features, LowlevelTransportSharedPlugin, OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
import {
createDeferred,
Deferred,
Expand Down Expand Up @@ -765,15 +765,19 @@ export const initConnector = () => {
return _connector;
};

const initTransport = (Transport: any) => {
TransportManager.setTransport(Transport);
const initTransport = (Transport: any, plugin?: LowlevelTransportSharedPlugin) => {
TransportManager.setTransport(Transport, plugin);
};

export const init = async (settings: ConnectSettings, Transport: any) => {
export const init = async (
settings: ConnectSettings,
Transport: any,
plugin?: LowlevelTransportSharedPlugin
) => {
try {
try {
await DataManager.load(settings);
initTransport(Transport);
initTransport(Transport, plugin);
} catch {
Log.error('DataManager.load error');
}
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/data-manager/TransportManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transport } from '@onekeyfe/hd-transport';
import { LowlevelTransportSharedPlugin, Transport } from '@onekeyfe/hd-transport';
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
import DataManager, { MessageVersion } from './DataManager';
import { getLogger, LoggerNames } from '../utils';
Expand All @@ -10,6 +10,7 @@ import { Features } from '../types';
const Log = getLogger(LoggerNames.Transport);
const BleLogger = getLogger(LoggerNames.HdBleTransport);
const HttpLogger = getLogger(LoggerNames.HdTransportHttp);
const LowLevelLogger = getLogger(LoggerNames.HdTransportLowLevel);

/**
* transport 在同一个环境中只会存在一个
Expand All @@ -27,6 +28,8 @@ export default class TransportManager {

static messageVersion: MessageVersion = 'latest';

static plugin: LowlevelTransportSharedPlugin | null = null;

static load() {
Log.debug('transport manager load');
this.defaultMessages = DataManager.getProtobufMessages();
Expand All @@ -45,6 +48,14 @@ export default class TransportManager {
} else {
Log.debug('React Native Do Not Initializing transports');
}
} else if (env === 'lowlevel') {
if (!this.plugin) {
throw ERRORS.TypedError(
HardwareErrorCode.TransportNotConfigured,
'Lowlevel transport mast have plugin'
);
}
await this.transport.init(LowLevelLogger, DevicePool.emitter, this.plugin);
} else {
await this.transport.init(HttpLogger);
}
Expand Down Expand Up @@ -81,7 +92,7 @@ export default class TransportManager {
}
}

static setTransport(TransportConstructor: any) {
static setTransport(TransportConstructor: any, plugin?: LowlevelTransportSharedPlugin) {
const env = DataManager.getSettings('env');
if (env === 'react-native') {
/** Actually initializes the ReactNativeTransport */
Expand All @@ -90,6 +101,10 @@ export default class TransportManager {
/** Actually initializes the HttpTransport */
this.transport = new TransportConstructor() as unknown as Transport;
}
if (plugin) {
this.plugin = plugin;
Log.debug('set transport plugin: ', this.plugin);
}
Log.debug('set transport: ', this.transport);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types/api/init.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
import { LowLevelCoreApi } from '../../lowLevelInject';
import type { ConnectSettings } from '../settings';

export declare function init(
settings: Partial<ConnectSettings>,
lowLevelApi?: LowLevelCoreApi
lowLevelApi?: LowLevelCoreApi,
pulgin?: LowlevelTransportSharedPlugin
): Promise<boolean>;

export declare function updateSettings(settings: Partial<ConnectSettings>): Promise<boolean>;
2 changes: 1 addition & 1 deletion packages/core/src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type ConnectSettings = {
priority: number;
trustedHost: boolean;
supportedBrowser?: boolean;
env: 'node' | 'web' | 'webextension' | 'electron' | 'react-native' | 'webusb';
env: 'node' | 'web' | 'webextension' | 'electron' | 'react-native' | 'webusb' | 'lowlevel';
timestamp: number;
isFrame?: boolean;
preRelease?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export enum LoggerNames {
HdCommonConnectSdk = '@onekey/common-connect-sdk',
HdBleSdk = '@onekey/hd-ble-sdk',
HdTransportHttp = '@onekey/hd-transport-http',
HdTransportLowLevel = '@onekey/hd-transport-lowlevel',
HdBleTransport = '@onekey/hd-ble-transport',
Connect = '@onekey/connect',
Iframe = 'IFrame',
Expand All @@ -173,6 +174,7 @@ export const LoggerMap = {
[LoggerNames.HdBleSdk]: initLog(LoggerNames.HdBleSdk),
[LoggerNames.HdTransportHttp]: initLog(LoggerNames.HdTransportHttp),
[LoggerNames.HdBleTransport]: initLog(LoggerNames.HdBleTransport),
[LoggerNames.HdTransportLowLevel]: initLog(LoggerNames.HdTransportLowLevel),
[LoggerNames.Connect]: initLog(LoggerNames.Connect),
[LoggerNames.Iframe]: initLog(LoggerNames.Iframe),
[LoggerNames.SendMessage]: initLog(LoggerNames.SendMessage),
Expand Down
3 changes: 2 additions & 1 deletion packages/hd-common-connect-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@onekeyfe/hd-core": "^0.3.22",
"@onekeyfe/hd-shared": "^0.3.22",
"@onekeyfe/hd-transport-http": "^0.3.22",
"@onekeyfe/hd-transport-webusb": "^0.3.22"
"@onekeyfe/hd-transport-webusb": "^0.3.22",
"@onekeyfe/hd-transport-lowlevel": "^0.3.22"
}
}
24 changes: 21 additions & 3 deletions packages/hd-common-connect-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import HardwareSdk, {
FIRMWARE_EVENT,
DEVICE_EVENT,
DEVICE,
LowLevelCoreApi,
} from '@onekeyfe/hd-core';
import { ERRORS, createDeferred, Deferred, HardwareErrorCode } from '@onekeyfe/hd-shared';
import type { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
import HttpTransport from '@onekeyfe/hd-transport-http';
import WebusbTransport from '@onekeyfe/hd-transport-webusb';
import LowlevelTransport from '@onekeyfe/hd-transport-lowlevel';

const eventEmitter = new EventEmitter();
const Log = getLogger(LoggerNames.HdCommonConnectSdk);
Expand Down Expand Up @@ -100,7 +103,11 @@ async function postMessage(message: CoreMessage, usePromise = true) {
_core.handleMessage(message);
}

const init = async (settings: Partial<ConnectSettings>) => {
const init = async (
settings: Partial<ConnectSettings>,
_?: LowLevelCoreApi,
plugin?: LowlevelTransportSharedPlugin
) => {
_settings = { ..._settings, ...settings, env: settings.env ?? 'node' };

enableLog(!!settings.debug);
Expand All @@ -109,8 +116,19 @@ const init = async (settings: Partial<ConnectSettings>) => {

try {
console.log(_settings.env);
const Transport = _settings.env === 'webusb' ? WebusbTransport : HttpTransport;
_core = await initCore(_settings, Transport);
// const Transport = _settings.env === 'webusb' ? WebusbTransport : HttpTransport;
let Transport: any;
switch (_settings.env) {
case 'webusb':
Transport = WebusbTransport;
break;
case 'lowlevel':
Transport = LowlevelTransport;
break;
default:
Transport = HttpTransport;
}
_core = await initCore(_settings, Transport, plugin);
_core?.on(CORE_EVENT, handleMessage);
setLoggerPostMessage(handleMessage);

Expand Down
2 changes: 1 addition & 1 deletion packages/hd-transport-lowlevel/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "hd-transport-lowlevel",
"name": "@onekeyfe/hd-transport-lowlevel",
"version": "0.3.22",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "MIT",
Expand Down
11 changes: 8 additions & 3 deletions packages/hd-transport-lowlevel/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
import transport from '@onekeyfe/hd-transport';
import type { LowlevelTransportSharedPlugin, LowLevelAcquireInput } from './types';
import type EventEmitter from 'events';
import type { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
import type { LowLevelAcquireInput } from './types';

const { check, buildBuffers, receiveOne, parseConfigure } = transport;

Expand All @@ -11,10 +13,13 @@ export default class LowlevelTransport {

Log?: any;

emitter?: EventEmitter;

plugin: LowlevelTransportSharedPlugin = {} as LowlevelTransportSharedPlugin;

init(logger: any, plugin: LowlevelTransportSharedPlugin) {
init(logger: any, emitter: EventEmitter, plugin: LowlevelTransportSharedPlugin) {
this.Log = logger;
this.emitter = emitter;
this.plugin = plugin;
this.plugin.init();
}
Expand All @@ -29,7 +34,7 @@ export default class LowlevelTransport {
// empty
}

async enumerate() {
enumerate() {
return this.plugin.enumerate();
}

Expand Down
12 changes: 0 additions & 12 deletions packages/hd-transport-lowlevel/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
export type Device = { id: string; name: string };
export type LowlevelTransportSharedPlugin = {
enumerate: () => Promise<Device[]>;
send: (uuid: string, data: string) => Promise<void>;
receive: () => Promise<string>;
connect: (uuid: string) => Promise<void>;
disconnect: (uuid: string) => Promise<void>;

init: () => Promise<void>;
version: string;
};

export type LowLevelAcquireInput = {
uuid: string;
};
2 changes: 2 additions & 0 deletions packages/hd-transport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type {
OneKeyMobileDeviceInfo,
OneKeyDeviceInfoWithSession,
MessageFromOneKey,
LowlevelTransportSharedPlugin,
LowLevelDevice,
} from './types';

export { Messages } from './types';
Expand Down
18 changes: 17 additions & 1 deletion packages/hd-transport/src/types/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export type AcquireInput = {

export type MessageFromOneKey = { type: string; message: Record<string, any> };

type ITransportInitFn = (logger?: any, emitter?: EventEmitter) => Promise<string>;
type ITransportInitFn = (
logger?: any,
emitter?: EventEmitter,
plugin?: LowlevelTransportSharedPlugin
) => Promise<string>;

export type Transport = {
enumerate(): Promise<Array<OneKeyDeviceInfo>>;
Expand Down Expand Up @@ -55,3 +59,15 @@ export type Transport = {

isOutdated: boolean;
};

export type LowLevelDevice = { id: string; name: string };
export type LowlevelTransportSharedPlugin = {
enumerate: () => Promise<LowLevelDevice[]>;
send: (uuid: string, data: string) => Promise<void>;
receive: () => Promise<string>;
connect: (uuid: string) => Promise<void>;
disconnect: (uuid: string) => Promise<void>;

init: () => Promise<void>;
version: string;
};

0 comments on commit 58f79ec

Please sign in to comment.