11import EventEmitter from 'events' ;
22import semver from 'semver' ;
3- import { OneKeyDeviceInfo as DeviceDescriptor , Enum_Capability } from '@onekeyfe/hd-transport' ;
3+ import { Enum_Capability } from '@onekeyfe/hd-transport' ;
44import {
5- createDeferred ,
6- Deferred ,
75 EDeviceType ,
6+ ERROR_CODES_REQUIRE_DISCONNECT ,
7+ ERROR_CODES_REQUIRE_RELEASE ,
88 ERRORS ,
99 HardwareError ,
1010 HardwareErrorCode ,
11+ createDeferred ,
1112} from '@onekeyfe/hd-shared' ;
13+
1214import {
15+ LoggerNames ,
1316 getDeviceBLEFirmwareVersion ,
1417 getDeviceBleName ,
1518 getDeviceFirmwareVersion ,
@@ -18,38 +21,39 @@ import {
1821 getDeviceUUID ,
1922 getLogger ,
2023 getMethodVersionRange ,
21- LoggerNames ,
2224} from '../utils' ;
2325import {
2426 fixFeaturesFirmwareVersion ,
2527 getPassphraseStateWithRefreshDeviceInfo ,
2628} from '../utils/deviceFeaturesUtils' ;
2729import { generateInstanceId } from '../utils/tracing' ;
2830
29- import type DeviceConnector from './DeviceConnector' ;
3031// eslint-disable-next-line import/no-cycle
31- import { DeviceCommands , PassphrasePromptResponse } from './DeviceCommands' ;
32-
32+ import { DeviceCommands } from './DeviceCommands' ;
3333import {
3434 type DeviceFirmwareRange ,
35- EOneKeyDeviceMode ,
3635 type Device as DeviceTyped ,
36+ EOneKeyDeviceMode ,
3737 type Features ,
3838 type UnavailableCapabilities ,
3939} from '../types' ;
40- import {
41- DEVICE ,
42- DeviceButtonRequestPayload ,
43- DeviceFeaturesPayload ,
44- PassphraseRequestPayload ,
45- UI_REQUEST ,
46- } from '../events' ;
47- import { PROTO } from '../constants' ;
40+ import { DEVICE , UI_REQUEST } from '../events' ;
4841import { DataManager } from '../data-manager' ;
4942import TransportManager from '../data-manager/TransportManager' ;
5043import { toHardened } from '../api/helpers/pathUtils' ;
5144import { existCapability } from '../utils/capabilitieUtils' ;
5245
46+ import type { PROTO } from '../constants' ;
47+ import type {
48+ DeviceButtonRequestPayload ,
49+ DeviceFeaturesPayload ,
50+ PassphraseRequestPayload ,
51+ } from '../events' ;
52+ import type { PassphrasePromptResponse } from './DeviceCommands' ;
53+ import type { Deferred } from '@onekeyfe/hd-shared' ;
54+ import type { OneKeyDeviceInfo as DeviceDescriptor } from '@onekeyfe/hd-transport' ;
55+ import type DeviceConnector from './DeviceConnector' ;
56+
5357export type InitOptions = {
5458 initSession ?: boolean ;
5559 deviceId ?: string ;
@@ -595,15 +599,11 @@ export class Device extends EventEmitter {
595599
596600 if (
597601 e instanceof HardwareError &&
598- ( e . errorCode === HardwareErrorCode . DeviceInitializeFailed ||
599- e . errorCode === HardwareErrorCode . DeviceInterruptedFromOutside ||
600- e . errorCode === HardwareErrorCode . DeviceInterruptedFromUser ||
601- e . errorCode === HardwareErrorCode . DeviceCheckPassphraseStateError ||
602- e . errorCode === HardwareErrorCode . ResponseUnexpectTypeError ||
603- e . errorCode === HardwareErrorCode . PinInvalid ||
604- e . errorCode === HardwareErrorCode . PinCancelled ||
605- e . errorCode === HardwareErrorCode . UnexpectPassphrase )
602+ ERROR_CODES_REQUIRE_RELEASE . includes ( e . errorCode as any )
606603 ) {
604+ if ( ERROR_CODES_REQUIRE_DISCONNECT . includes ( e . errorCode as any ) ) {
605+ await this . deviceConnector ?. disconnect ( this . mainId ) ;
606+ }
607607 await this . release ( ) ;
608608 Log . debug ( `error code ${ e . errorCode } release device, mainId: ${ this . mainId } ` ) ;
609609 }
0 commit comments