@onekeyfe/hd-transport-lowlevel 1.2.11-alpha.8 → 1.2.11-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/protocol-v2.test.js +90 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +123 -44
- package/package.json +4 -4
- package/src/index.ts +142 -49
|
@@ -563,4 +563,94 @@ describe('LowlevelTransport protocol framing', () => {
|
|
|
563
563
|
});
|
|
564
564
|
expect(lowlevel.getProtocolType('flaky-pro2-id')).toBeUndefined();
|
|
565
565
|
});
|
|
566
|
+
|
|
567
|
+
test('stop() disconnects connected plugin devices and clears protocol state idempotently', async () => {
|
|
568
|
+
const probeResponse = ProtocolV2.encodeFrame(
|
|
569
|
+
schemas,
|
|
570
|
+
'Success',
|
|
571
|
+
{ message: 'ok' },
|
|
572
|
+
{ router: PROTOCOL_V2_CHANNEL_BLE_UART }
|
|
573
|
+
);
|
|
574
|
+
const plugin = createPlugin({
|
|
575
|
+
devices: [{ id: 'stop-pro2-id', name: 'OneKey Pro 2', commType: 'ble' }],
|
|
576
|
+
responses: [bytesToHex(probeResponse)],
|
|
577
|
+
});
|
|
578
|
+
const lowlevel = configureTransport(plugin);
|
|
579
|
+
|
|
580
|
+
await expect(
|
|
581
|
+
lowlevel.acquire({ uuid: 'stop-pro2-id', expectedProtocol: 'V2' })
|
|
582
|
+
).resolves.toEqual({
|
|
583
|
+
uuid: 'stop-pro2-id',
|
|
584
|
+
protocolType: 'V2',
|
|
585
|
+
});
|
|
586
|
+
expect(lowlevel.getProtocolType('stop-pro2-id')).toBe('V2');
|
|
587
|
+
|
|
588
|
+
await lowlevel.stop();
|
|
589
|
+
|
|
590
|
+
expect(plugin.disconnect).toHaveBeenCalledTimes(1);
|
|
591
|
+
expect(plugin.disconnect).toHaveBeenCalledWith('stop-pro2-id');
|
|
592
|
+
expect(lowlevel.getProtocolType('stop-pro2-id')).toBeUndefined();
|
|
593
|
+
|
|
594
|
+
// Calling stop() again is a safe no-op.
|
|
595
|
+
await lowlevel.stop();
|
|
596
|
+
expect(plugin.disconnect).toHaveBeenCalledTimes(1);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
test('stop() aborts an in-flight acquire() and disconnects without leaking the connection', async () => {
|
|
600
|
+
let resolveConnect;
|
|
601
|
+
const connectPromise = new Promise(resolve => {
|
|
602
|
+
resolveConnect = resolve;
|
|
603
|
+
});
|
|
604
|
+
const plugin = createPlugin({
|
|
605
|
+
devices: [{ id: 'inflight-stop-id', name: 'OneKey Pro 2', commType: 'ble' }],
|
|
606
|
+
responses: [],
|
|
607
|
+
});
|
|
608
|
+
plugin.connect.mockImplementation(() => connectPromise);
|
|
609
|
+
const lowlevel = configureTransport(plugin);
|
|
610
|
+
|
|
611
|
+
const acquiring = lowlevel.acquire({ uuid: 'inflight-stop-id', expectedProtocol: 'V2' });
|
|
612
|
+
const stopping = lowlevel.stop();
|
|
613
|
+
resolveConnect();
|
|
614
|
+
|
|
615
|
+
await expect(acquiring).rejects.toThrow(/Lowlevel transport is stopped/);
|
|
616
|
+
await stopping;
|
|
617
|
+
// Only stopResources() disconnects the UUID; the retired acquire must not disconnect again.
|
|
618
|
+
expect(plugin.disconnect).toHaveBeenCalledTimes(1);
|
|
619
|
+
expect(plugin.disconnect).toHaveBeenCalledWith('inflight-stop-id');
|
|
620
|
+
expect(lowlevel.getProtocolType('inflight-stop-id')).toBeUndefined();
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
test('stop() halts in-flight Protocol V1 multi-chunk sends without writing remaining chunks', async () => {
|
|
624
|
+
let resolveFirstSend;
|
|
625
|
+
let sendCalls = 0;
|
|
626
|
+
const plugin = createPlugin({
|
|
627
|
+
devices: [{ id: 'v1-stop-id', name: 'OneKey Classic', commType: 'ble' }],
|
|
628
|
+
responses: [],
|
|
629
|
+
});
|
|
630
|
+
plugin.send.mockImplementation(() => {
|
|
631
|
+
sendCalls += 1;
|
|
632
|
+
if (sendCalls === 1) {
|
|
633
|
+
return new Promise(resolve => {
|
|
634
|
+
resolveFirstSend = resolve;
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
return Promise.resolve();
|
|
638
|
+
});
|
|
639
|
+
const lowlevel = configureTransport(plugin);
|
|
640
|
+
lowlevel.deviceProtocol.set('v1-stop-id', 'V1');
|
|
641
|
+
lowlevel.connectedDevices.add('v1-stop-id');
|
|
642
|
+
|
|
643
|
+
// Payload large enough to require multiple 64-byte Protocol V1 chunks.
|
|
644
|
+
const calling = lowlevel.call('v1-stop-id', 'Success', { message: 'a'.repeat(200) });
|
|
645
|
+
await Promise.resolve();
|
|
646
|
+
expect(sendCalls).toBe(1);
|
|
647
|
+
|
|
648
|
+
const stopping = lowlevel.stop();
|
|
649
|
+
resolveFirstSend();
|
|
650
|
+
|
|
651
|
+
await expect(calling).rejects.toThrow(/Lowlevel transport is stopped/);
|
|
652
|
+
await stopping;
|
|
653
|
+
expect(sendCalls).toBe(1);
|
|
654
|
+
expect(plugin.receive).not.toHaveBeenCalled();
|
|
655
|
+
});
|
|
566
656
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -19,8 +19,8 @@ declare function getProtocolV1SendOptions(name: string): {
|
|
|
19
19
|
} | undefined;
|
|
20
20
|
declare function resolveLowlevelProtocolV2PacketCapacity(capacity?: number | null): number;
|
|
21
21
|
declare class LowlevelTransport {
|
|
22
|
-
_messages: ReturnType<typeof transport__default.
|
|
23
|
-
_messagesV2: ReturnType<typeof transport__default.
|
|
22
|
+
_messages: ReturnType<typeof transport__default.parseTransportConfigure> | undefined;
|
|
23
|
+
_messagesV2: ReturnType<typeof transport__default.parseTransportConfigure> | undefined;
|
|
24
24
|
configured: boolean;
|
|
25
25
|
Log?: any;
|
|
26
26
|
emitter?: EventEmitter;
|
|
@@ -30,6 +30,9 @@ declare class LowlevelTransport {
|
|
|
30
30
|
private protocolV2Assemblers;
|
|
31
31
|
private protocolV2Generations;
|
|
32
32
|
private connectedDevices;
|
|
33
|
+
private acquiringDevices;
|
|
34
|
+
private stopped;
|
|
35
|
+
private stopPromise?;
|
|
33
36
|
private protocolV2PacketCapacities;
|
|
34
37
|
private protocolV2Links;
|
|
35
38
|
private protocolV2SchemaConfiguration;
|
|
@@ -39,11 +42,14 @@ declare class LowlevelTransport {
|
|
|
39
42
|
configureProtocolV2(signedData: any): void;
|
|
40
43
|
listen(): void;
|
|
41
44
|
enumerate(): Promise<transport.LowLevelDevice[]>;
|
|
45
|
+
private assertRunning;
|
|
42
46
|
acquire(input: LowLevelAcquireInput): Promise<{
|
|
43
47
|
uuid: string;
|
|
44
48
|
protocolType: ProtocolType;
|
|
45
49
|
}>;
|
|
46
50
|
release(uuid: string): Promise<boolean>;
|
|
51
|
+
stop(): Promise<void>;
|
|
52
|
+
private stopResources;
|
|
47
53
|
call(uuid: string, name: string, data: Record<string, unknown>, options?: TransportCallOptions): Promise<transport.MessageFromOneKey>;
|
|
48
54
|
post(uuid: string, name: string, data: Record<string, unknown>): Promise<void>;
|
|
49
55
|
private callProtocolV1;
|
|
@@ -63,7 +69,7 @@ declare class LowlevelTransport {
|
|
|
63
69
|
private callProtocolV2;
|
|
64
70
|
private createProtocolV2Adapter;
|
|
65
71
|
private advanceProtocolV2Generation;
|
|
66
|
-
cancel(
|
|
72
|
+
cancel(): void;
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
export { LowlevelTransport as default, getProtocolV1SendOptions, resolveLowlevelProtocolV2PacketCapacity, shouldLogFirmwareUploadProgress };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAYN,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,6BAA6B,EAC7B,YAAY,EAEZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAWpD,wBAAgB,+BAA+B,CAAC,EAC9C,OAAO,EACP,iBAAiB,EACjB,GAAG,EACH,YAAY,GACb,EAAE;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,MAAM,CAAC;CACtB,WAMA;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM;;cAEpD;AAED,wBAAgB,uCAAuC,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,UAK/E;AAUD,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAYN,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,6BAA6B,EAC7B,YAAY,EAEZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAWpD,wBAAgB,+BAA+B,CAAC,EAC9C,OAAO,EACP,iBAAiB,EACjB,GAAG,EACH,YAAY,GACb,EAAE;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,MAAM,CAAC;CACtB,WAMA;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM;;cAEpD;AAED,wBAAgB,uCAAuC,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,UAK/E;AAUD,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,uBAAuB,CAAC,GAAG,SAAS,CAAC;IAE5E,WAAW,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,uBAAuB,CAAC,GAAG,SAAS,CAAC;IAE9E,UAAU,UAAS;IAEnB,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB,MAAM,EAAE,6BAA6B,CAAuC;IAE5E,OAAO,CAAC,cAAc,CAAwC;IAE9D,OAAO,CAAC,mBAAmB,CAAwC;IAEnE,OAAO,CAAC,oBAAoB,CAAoD;IAEhF,OAAO,CAAC,qBAAqB,CAAkC;IAE/D,OAAO,CAAC,gBAAgB,CAA0B;IAElD,OAAO,CAAC,gBAAgB,CAA0B;IAElD,OAAO,CAAC,OAAO,CAAS;IAExB,OAAO,CAAC,WAAW,CAAC,CAAgB;IAEpC,OAAO,CAAC,0BAA0B,CAAkC;IAEpE,OAAO,CAAC,eAAe,CA8BpB;IAEH,OAAO,CAAC,6BAA6B,CAAqB;IAE1D,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAIvD,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,6BAA6B;IAS9E,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,mBAAmB,CAAC,UAAU,EAAE,GAAG;IAiBnC,MAAM;IAIA,SAAS;IAKf,OAAO,CAAC,aAAa;IAMf,OAAO,CAAC,KAAK,EAAE,oBAAoB;;;;IAwEnC,OAAO,CAAC,IAAI,EAAE,MAAM;IAgB1B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YAQP,aAAa;IAgCrB,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;IAoB1B,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;YAatD,cAAc;IAyF5B,OAAO,CAAC,0BAA0B;IAOlC,OAAO,CAAC,2BAA2B;IAMnC,OAAO,CAAC,4BAA4B;IAIpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;YA2Cd,yBAAyB;YAwCzB,eAAe;YAgBf,eAAe;YA6Bf,UAAU;YAYV,qBAAqB;YAmBrB,mBAAmB;YAqBnB,oBAAoB;YAkBpB,+BAA+B;YAgB/B,cAAc;IAwB5B,OAAO,CAAC,uBAAuB;IAgC/B,OAAO,CAAC,2BAA2B;IAMnC,MAAM;CAGP"}
|
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
39
39
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
const { check, ProtocolV1,
|
|
42
|
+
const { check, ProtocolV1, parseTransportConfigure } = transport__default["default"];
|
|
43
43
|
const PROTOCOL_PROBE_TIMEOUT_MS = 1000;
|
|
44
44
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
45
45
|
const LOWLEVEL_PROTOCOL_V2_PACKET_LENGTH_FALLBACK = 192;
|
|
@@ -75,6 +75,8 @@ class LowlevelTransport {
|
|
|
75
75
|
this.protocolV2Assemblers = new Map();
|
|
76
76
|
this.protocolV2Generations = new Map();
|
|
77
77
|
this.connectedDevices = new Set();
|
|
78
|
+
this.acquiringDevices = new Set();
|
|
79
|
+
this.stopped = false;
|
|
78
80
|
this.protocolV2PacketCapacities = new Map();
|
|
79
81
|
this.protocolV2Links = new transport.ProtocolV2LinkManager({
|
|
80
82
|
getSchemas: () => {
|
|
@@ -112,13 +114,15 @@ class LowlevelTransport {
|
|
|
112
114
|
return this.deviceProtocol.get(path);
|
|
113
115
|
}
|
|
114
116
|
init(logger, emitter, plugin) {
|
|
117
|
+
this.stopped = false;
|
|
118
|
+
this.stopPromise = undefined;
|
|
115
119
|
this.Log = logger;
|
|
116
120
|
this.emitter = emitter;
|
|
117
121
|
this.plugin = plugin;
|
|
118
122
|
this.plugin.init();
|
|
119
123
|
}
|
|
120
124
|
configure(signedData) {
|
|
121
|
-
const messages =
|
|
125
|
+
const messages = parseTransportConfigure(signedData);
|
|
122
126
|
this.configured = true;
|
|
123
127
|
this._messages = messages;
|
|
124
128
|
}
|
|
@@ -128,7 +132,7 @@ class LowlevelTransport {
|
|
|
128
132
|
return;
|
|
129
133
|
}
|
|
130
134
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
131
|
-
this._messagesV2 =
|
|
135
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
132
136
|
this.protocolV2SchemaConfiguration = configuration;
|
|
133
137
|
if (isReconfiguration) {
|
|
134
138
|
this.protocolV2Links
|
|
@@ -144,52 +148,72 @@ class LowlevelTransport {
|
|
|
144
148
|
return devices;
|
|
145
149
|
});
|
|
146
150
|
}
|
|
151
|
+
assertRunning() {
|
|
152
|
+
if (this.stopped) {
|
|
153
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Lowlevel transport is stopped');
|
|
154
|
+
}
|
|
155
|
+
}
|
|
147
156
|
acquire(input) {
|
|
148
|
-
var _a, _b, _c, _d;
|
|
157
|
+
var _a, _b, _c, _d, _e;
|
|
149
158
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
this.assertRunning();
|
|
150
160
|
const alreadyConnected = this.connectedDevices.has(input.uuid);
|
|
161
|
+
this.acquiringDevices.add(input.uuid);
|
|
151
162
|
try {
|
|
152
|
-
yield this.plugin.connect(input.uuid);
|
|
153
|
-
yield this.refreshProtocolV2PacketCapacity(input.uuid);
|
|
154
|
-
if (!alreadyConnected) {
|
|
155
|
-
this.connectedDevices.add(input.uuid);
|
|
156
|
-
this.advanceProtocolV2Generation(input.uuid);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
this.connectedDevices.delete(input.uuid);
|
|
161
|
-
this.Log.debug('lowlelvel transport connect error: ', error);
|
|
162
|
-
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.LowlevelTrasnportConnectError, (_a = error.message) !== null && _a !== void 0 ? _a : error);
|
|
163
|
-
}
|
|
164
|
-
this.protocolV2Assemblers.set(input.uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
|
|
165
|
-
const protocolHint = input.expectedProtocol
|
|
166
|
-
? undefined
|
|
167
|
-
: (_b = input.protocolHint) !== null && _b !== void 0 ? _b : this.deviceProtocolHints.get(input.uuid);
|
|
168
|
-
try {
|
|
169
|
-
const protocolType = yield this.detectProtocol(input.uuid, input.expectedProtocol, protocolHint);
|
|
170
|
-
return { uuid: input.uuid, protocolType };
|
|
171
|
-
}
|
|
172
|
-
catch (error) {
|
|
173
163
|
try {
|
|
174
|
-
yield this.
|
|
164
|
+
yield this.plugin.connect(input.uuid);
|
|
165
|
+
this.assertRunning();
|
|
166
|
+
yield this.refreshProtocolV2PacketCapacity(input.uuid);
|
|
167
|
+
this.assertRunning();
|
|
168
|
+
if (!alreadyConnected) {
|
|
169
|
+
this.connectedDevices.add(input.uuid);
|
|
170
|
+
this.advanceProtocolV2Generation(input.uuid);
|
|
171
|
+
}
|
|
175
172
|
}
|
|
176
|
-
catch (
|
|
177
|
-
|
|
173
|
+
catch (error) {
|
|
174
|
+
this.connectedDevices.delete(input.uuid);
|
|
175
|
+
if (this.stopped) {
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('lowlelvel transport connect error: ', error);
|
|
179
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.LowlevelTrasnportConnectError, (_b = error.message) !== null && _b !== void 0 ? _b : error);
|
|
178
180
|
}
|
|
181
|
+
this.protocolV2Assemblers.set(input.uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
|
|
182
|
+
const protocolHint = input.expectedProtocol
|
|
183
|
+
? undefined
|
|
184
|
+
: (_c = input.protocolHint) !== null && _c !== void 0 ? _c : this.deviceProtocolHints.get(input.uuid);
|
|
179
185
|
try {
|
|
180
|
-
yield this.
|
|
186
|
+
const protocolType = yield this.detectProtocol(input.uuid, input.expectedProtocol, protocolHint);
|
|
187
|
+
this.assertRunning();
|
|
188
|
+
return { uuid: input.uuid, protocolType };
|
|
181
189
|
}
|
|
182
|
-
catch (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
190
|
+
catch (error) {
|
|
191
|
+
try {
|
|
192
|
+
yield this.protocolV2Links.invalidateLink(input.uuid, 'Lowlevel transport acquire failed');
|
|
193
|
+
}
|
|
194
|
+
catch (cleanupError) {
|
|
195
|
+
(_d = this.Log) === null || _d === void 0 ? void 0 : _d.debug('[LowlevelTransport] acquire link cleanup failed:', cleanupError);
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
if (!this.stopped) {
|
|
199
|
+
yield this.plugin.disconnect(input.uuid);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (cleanupError) {
|
|
203
|
+
(_e = this.Log) === null || _e === void 0 ? void 0 : _e.debug('[LowlevelTransport] acquire disconnect failed:', cleanupError);
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
this.connectedDevices.delete(input.uuid);
|
|
207
|
+
this.deviceProtocol.delete(input.uuid);
|
|
208
|
+
this.protocolV2Assemblers.delete(input.uuid);
|
|
209
|
+
this.protocolV2PacketCapacities.delete(input.uuid);
|
|
210
|
+
this.advanceProtocolV2Generation(input.uuid);
|
|
211
|
+
}
|
|
212
|
+
throw error;
|
|
191
213
|
}
|
|
192
|
-
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
this.acquiringDevices.delete(input.uuid);
|
|
193
217
|
}
|
|
194
218
|
});
|
|
195
219
|
}
|
|
@@ -210,6 +234,44 @@ class LowlevelTransport {
|
|
|
210
234
|
}
|
|
211
235
|
});
|
|
212
236
|
}
|
|
237
|
+
stop() {
|
|
238
|
+
this.stopped = true;
|
|
239
|
+
if (!this.stopPromise) {
|
|
240
|
+
this.stopPromise = this.stopResources();
|
|
241
|
+
}
|
|
242
|
+
return this.stopPromise;
|
|
243
|
+
}
|
|
244
|
+
stopResources() {
|
|
245
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
+
const trackedDeviceIds = new Set([
|
|
247
|
+
...this.connectedDevices,
|
|
248
|
+
...this.acquiringDevices,
|
|
249
|
+
...this.deviceProtocol.keys(),
|
|
250
|
+
...this.protocolV2Assemblers.keys(),
|
|
251
|
+
...this.protocolV2PacketCapacities.keys(),
|
|
252
|
+
]);
|
|
253
|
+
yield Promise.all(Array.from(trackedDeviceIds, (uuid) => __awaiter(this, void 0, void 0, function* () {
|
|
254
|
+
var _a, _b;
|
|
255
|
+
const shouldDisconnect = this.connectedDevices.has(uuid) || this.acquiringDevices.has(uuid);
|
|
256
|
+
this.advanceProtocolV2Generation(uuid);
|
|
257
|
+
this.connectedDevices.delete(uuid);
|
|
258
|
+
this.deviceProtocol.delete(uuid);
|
|
259
|
+
this.protocolV2Assemblers.delete(uuid);
|
|
260
|
+
this.protocolV2PacketCapacities.delete(uuid);
|
|
261
|
+
if (shouldDisconnect && typeof ((_a = this.plugin) === null || _a === void 0 ? void 0 : _a.disconnect) === 'function') {
|
|
262
|
+
try {
|
|
263
|
+
yield this.plugin.disconnect(uuid);
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug(`[LowlevelTransport] stop disconnect failed: ${uuid}`, error);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
})));
|
|
270
|
+
yield this.protocolV2Links
|
|
271
|
+
.dispose('Lowlevel transport stopped')
|
|
272
|
+
.catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[LowlevelTransport] link dispose failed:', error); });
|
|
273
|
+
});
|
|
274
|
+
}
|
|
213
275
|
call(uuid, name, data, options) {
|
|
214
276
|
return __awaiter(this, void 0, void 0, function* () {
|
|
215
277
|
if (this._messages === null || !this._messages) {
|
|
@@ -235,8 +297,9 @@ class LowlevelTransport {
|
|
|
235
297
|
});
|
|
236
298
|
}
|
|
237
299
|
callProtocolV1(uuid, name, data, options) {
|
|
238
|
-
var _a;
|
|
300
|
+
var _a, _b, _c;
|
|
239
301
|
return __awaiter(this, void 0, void 0, function* () {
|
|
302
|
+
this.assertRunning();
|
|
240
303
|
if (!this._messages) {
|
|
241
304
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
|
|
242
305
|
}
|
|
@@ -249,9 +312,11 @@ class LowlevelTransport {
|
|
|
249
312
|
let lastLoggedPercent = 0;
|
|
250
313
|
let lastLoggedAt = uploadStartedAt;
|
|
251
314
|
for (const [index, o] of buffers.entries()) {
|
|
315
|
+
this.assertRunning();
|
|
252
316
|
const outData = o.toString('hex');
|
|
253
317
|
try {
|
|
254
318
|
yield this.plugin.send(uuid, outData, getProtocolV1SendOptions(name));
|
|
319
|
+
this.assertRunning();
|
|
255
320
|
sentBytes += o.limit;
|
|
256
321
|
if (isFirmwareUpload) {
|
|
257
322
|
const now = Date.now();
|
|
@@ -273,23 +338,29 @@ class LowlevelTransport {
|
|
|
273
338
|
}
|
|
274
339
|
}
|
|
275
340
|
catch (e) {
|
|
276
|
-
this.
|
|
341
|
+
if (this.stopped) {
|
|
342
|
+
throw e;
|
|
343
|
+
}
|
|
344
|
+
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug('lowlevel transport send error: ', e);
|
|
277
345
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
278
346
|
}
|
|
279
347
|
}
|
|
280
348
|
try {
|
|
349
|
+
this.assertRunning();
|
|
281
350
|
const response = yield this.readProtocolV1Message(uuid, options === null || options === void 0 ? void 0 : options.timeoutMs);
|
|
351
|
+
this.assertRunning();
|
|
282
352
|
const jsonData = ProtocolV1.decodeMessage(messages, response);
|
|
283
353
|
return check.call(jsonData);
|
|
284
354
|
}
|
|
285
355
|
catch (e) {
|
|
286
|
-
if (
|
|
356
|
+
if (!this.stopped &&
|
|
357
|
+
(e === null || e === void 0 ? void 0 : e.errorCode) === hdShared.HardwareErrorCode.BleTimeoutError &&
|
|
287
358
|
(options === null || options === void 0 ? void 0 : options.timeoutMs) !== PROTOCOL_PROBE_TIMEOUT_MS) {
|
|
288
359
|
try {
|
|
289
360
|
yield this.resetConnectionAfterProbe(uuid, 'V1');
|
|
290
361
|
}
|
|
291
362
|
catch (resetError) {
|
|
292
|
-
this.Log.debug('[LowlevelTransport] reset after Protocol V1 timeout failed:', resetError);
|
|
363
|
+
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug('[LowlevelTransport] reset after Protocol V1 timeout failed:', resetError);
|
|
293
364
|
}
|
|
294
365
|
}
|
|
295
366
|
if (name === 'Initialize' && (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS) {
|
|
@@ -365,13 +436,19 @@ class LowlevelTransport {
|
|
|
365
436
|
catch (error) {
|
|
366
437
|
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug(`[LowlevelTransport] disconnect after Protocol ${protocol} probe failed:`, error);
|
|
367
438
|
}
|
|
439
|
+
this.assertRunning();
|
|
368
440
|
try {
|
|
369
441
|
yield this.plugin.connect(uuid);
|
|
442
|
+
this.assertRunning();
|
|
370
443
|
yield this.refreshProtocolV2PacketCapacity(uuid);
|
|
444
|
+
this.assertRunning();
|
|
371
445
|
this.connectedDevices.add(uuid);
|
|
372
446
|
this.advanceProtocolV2Generation(uuid);
|
|
373
447
|
}
|
|
374
448
|
catch (error) {
|
|
449
|
+
if (this.stopped) {
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
375
452
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[LowlevelTransport] reconnect after Protocol ${protocol} probe failed:`, error);
|
|
376
453
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.LowlevelTrasnportConnectError, (_d = error.message) !== null && _d !== void 0 ? _d : error);
|
|
377
454
|
}
|
|
@@ -428,7 +505,9 @@ class LowlevelTransport {
|
|
|
428
505
|
}
|
|
429
506
|
receiveHex(uuid, timeoutMs, commandName) {
|
|
430
507
|
return __awaiter(this, void 0, void 0, function* () {
|
|
508
|
+
this.assertRunning();
|
|
431
509
|
const response = yield transport.withProtocolTimeout(this.plugin.receive(uuid), timeoutMs, () => this.createProtocolTimeoutError(commandName, timeoutMs !== null && timeoutMs !== void 0 ? timeoutMs : 0));
|
|
510
|
+
this.assertRunning();
|
|
432
511
|
if (typeof response !== 'string') {
|
|
433
512
|
throw new Error('Returning data is not string');
|
|
434
513
|
}
|
|
@@ -549,7 +628,7 @@ class LowlevelTransport {
|
|
|
549
628
|
this.protocolV2Generations.set(uuid, nextGeneration);
|
|
550
629
|
return nextGeneration;
|
|
551
630
|
}
|
|
552
|
-
cancel(
|
|
631
|
+
cancel() {
|
|
553
632
|
this.Log.debug('lowlevel-transport', 'cancel');
|
|
554
633
|
}
|
|
555
634
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-lowlevel",
|
|
3
|
-
"version": "1.2.11-alpha.
|
|
3
|
+
"version": "1.2.11-alpha.9",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-shared": "1.2.11-alpha.
|
|
24
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
23
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.9",
|
|
24
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.9"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "42a772503abc1acb973bc31232a4c30f113034d1"
|
|
27
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
} from '@onekeyfe/hd-transport';
|
|
23
23
|
import type { LowLevelAcquireInput } from './types';
|
|
24
24
|
|
|
25
|
-
const { check, ProtocolV1,
|
|
25
|
+
const { check, ProtocolV1, parseTransportConfigure } = transport;
|
|
26
26
|
|
|
27
27
|
const PROTOCOL_PROBE_TIMEOUT_MS = 1000;
|
|
28
28
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
@@ -69,9 +69,9 @@ function readProtocolV1PayloadLength(data: Uint8Array) {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
export default class LowlevelTransport {
|
|
72
|
-
_messages: ReturnType<typeof transport.
|
|
72
|
+
_messages: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
73
73
|
|
|
74
|
-
_messagesV2: ReturnType<typeof transport.
|
|
74
|
+
_messagesV2: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
75
75
|
|
|
76
76
|
configured = false;
|
|
77
77
|
|
|
@@ -91,6 +91,12 @@ export default class LowlevelTransport {
|
|
|
91
91
|
|
|
92
92
|
private connectedDevices: Set<string> = new Set();
|
|
93
93
|
|
|
94
|
+
private acquiringDevices: Set<string> = new Set();
|
|
95
|
+
|
|
96
|
+
private stopped = false;
|
|
97
|
+
|
|
98
|
+
private stopPromise?: Promise<void>;
|
|
99
|
+
|
|
94
100
|
private protocolV2PacketCapacities: Map<string, number> = new Map();
|
|
95
101
|
|
|
96
102
|
private protocolV2Links = new ProtocolV2LinkManager<string>({
|
|
@@ -132,6 +138,8 @@ export default class LowlevelTransport {
|
|
|
132
138
|
}
|
|
133
139
|
|
|
134
140
|
init(logger: any, emitter: EventEmitter, plugin: LowlevelTransportSharedPlugin) {
|
|
141
|
+
this.stopped = false;
|
|
142
|
+
this.stopPromise = undefined;
|
|
135
143
|
this.Log = logger;
|
|
136
144
|
this.emitter = emitter;
|
|
137
145
|
this.plugin = plugin;
|
|
@@ -139,7 +147,7 @@ export default class LowlevelTransport {
|
|
|
139
147
|
}
|
|
140
148
|
|
|
141
149
|
configure(signedData: any) {
|
|
142
|
-
const messages =
|
|
150
|
+
const messages = parseTransportConfigure(signedData);
|
|
143
151
|
this.configured = true;
|
|
144
152
|
this._messages = messages;
|
|
145
153
|
}
|
|
@@ -151,7 +159,7 @@ export default class LowlevelTransport {
|
|
|
151
159
|
}
|
|
152
160
|
|
|
153
161
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
154
|
-
this._messagesV2 =
|
|
162
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
155
163
|
this.protocolV2SchemaConfiguration = configuration;
|
|
156
164
|
|
|
157
165
|
if (isReconfiguration) {
|
|
@@ -170,56 +178,81 @@ export default class LowlevelTransport {
|
|
|
170
178
|
return devices;
|
|
171
179
|
}
|
|
172
180
|
|
|
181
|
+
private assertRunning() {
|
|
182
|
+
if (this.stopped) {
|
|
183
|
+
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Lowlevel transport is stopped');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
173
187
|
async acquire(input: LowLevelAcquireInput) {
|
|
188
|
+
this.assertRunning();
|
|
174
189
|
const alreadyConnected = this.connectedDevices.has(input.uuid);
|
|
190
|
+
this.acquiringDevices.add(input.uuid);
|
|
175
191
|
try {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
this.
|
|
180
|
-
this.
|
|
192
|
+
try {
|
|
193
|
+
await this.plugin.connect(input.uuid);
|
|
194
|
+
this.assertRunning();
|
|
195
|
+
await this.refreshProtocolV2PacketCapacity(input.uuid);
|
|
196
|
+
this.assertRunning();
|
|
197
|
+
if (!alreadyConnected) {
|
|
198
|
+
this.connectedDevices.add(input.uuid);
|
|
199
|
+
this.advanceProtocolV2Generation(input.uuid);
|
|
200
|
+
}
|
|
201
|
+
} catch (error) {
|
|
202
|
+
this.connectedDevices.delete(input.uuid);
|
|
203
|
+
if (this.stopped) {
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
this.Log?.debug('lowlelvel transport connect error: ', error);
|
|
207
|
+
throw ERRORS.TypedError(
|
|
208
|
+
HardwareErrorCode.LowlevelTrasnportConnectError,
|
|
209
|
+
error.message ?? error
|
|
210
|
+
);
|
|
181
211
|
}
|
|
182
|
-
} catch (error) {
|
|
183
|
-
this.connectedDevices.delete(input.uuid);
|
|
184
|
-
this.Log.debug('lowlelvel transport connect error: ', error);
|
|
185
|
-
throw ERRORS.TypedError(
|
|
186
|
-
HardwareErrorCode.LowlevelTrasnportConnectError,
|
|
187
|
-
error.message ?? error
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
212
|
|
|
191
|
-
|
|
192
|
-
input.uuid,
|
|
193
|
-
new ProtocolV2FrameAssembler(PROTOCOL_V2_BLE_FRAME_MAX_BYTES)
|
|
194
|
-
);
|
|
195
|
-
const protocolHint = input.expectedProtocol
|
|
196
|
-
? undefined
|
|
197
|
-
: input.protocolHint ?? this.deviceProtocolHints.get(input.uuid);
|
|
198
|
-
try {
|
|
199
|
-
const protocolType = await this.detectProtocol(
|
|
213
|
+
this.protocolV2Assemblers.set(
|
|
200
214
|
input.uuid,
|
|
201
|
-
|
|
202
|
-
protocolHint
|
|
215
|
+
new ProtocolV2FrameAssembler(PROTOCOL_V2_BLE_FRAME_MAX_BYTES)
|
|
203
216
|
);
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
await this.protocolV2Links.invalidateLink(input.uuid, 'Lowlevel transport acquire failed');
|
|
208
|
-
} catch (cleanupError) {
|
|
209
|
-
this.Log?.debug('[LowlevelTransport] acquire link cleanup failed:', cleanupError);
|
|
210
|
-
}
|
|
217
|
+
const protocolHint = input.expectedProtocol
|
|
218
|
+
? undefined
|
|
219
|
+
: input.protocolHint ?? this.deviceProtocolHints.get(input.uuid);
|
|
211
220
|
try {
|
|
212
|
-
await this.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
this.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
+
const protocolType = await this.detectProtocol(
|
|
222
|
+
input.uuid,
|
|
223
|
+
input.expectedProtocol,
|
|
224
|
+
protocolHint
|
|
225
|
+
);
|
|
226
|
+
this.assertRunning();
|
|
227
|
+
return { uuid: input.uuid, protocolType };
|
|
228
|
+
} catch (error) {
|
|
229
|
+
try {
|
|
230
|
+
await this.protocolV2Links.invalidateLink(
|
|
231
|
+
input.uuid,
|
|
232
|
+
'Lowlevel transport acquire failed'
|
|
233
|
+
);
|
|
234
|
+
} catch (cleanupError) {
|
|
235
|
+
this.Log?.debug('[LowlevelTransport] acquire link cleanup failed:', cleanupError);
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
// Once stop() has started, stopResources() owns disconnecting tracked UUIDs;
|
|
239
|
+
// a late retired acquire must not disconnect a replacement transport's link.
|
|
240
|
+
if (!this.stopped) {
|
|
241
|
+
await this.plugin.disconnect(input.uuid);
|
|
242
|
+
}
|
|
243
|
+
} catch (cleanupError) {
|
|
244
|
+
this.Log?.debug('[LowlevelTransport] acquire disconnect failed:', cleanupError);
|
|
245
|
+
} finally {
|
|
246
|
+
this.connectedDevices.delete(input.uuid);
|
|
247
|
+
this.deviceProtocol.delete(input.uuid);
|
|
248
|
+
this.protocolV2Assemblers.delete(input.uuid);
|
|
249
|
+
this.protocolV2PacketCapacities.delete(input.uuid);
|
|
250
|
+
this.advanceProtocolV2Generation(input.uuid);
|
|
251
|
+
}
|
|
252
|
+
throw error;
|
|
221
253
|
}
|
|
222
|
-
|
|
254
|
+
} finally {
|
|
255
|
+
this.acquiringDevices.delete(input.uuid);
|
|
223
256
|
}
|
|
224
257
|
}
|
|
225
258
|
|
|
@@ -239,6 +272,46 @@ export default class LowlevelTransport {
|
|
|
239
272
|
}
|
|
240
273
|
}
|
|
241
274
|
|
|
275
|
+
stop(): Promise<void> {
|
|
276
|
+
this.stopped = true;
|
|
277
|
+
if (!this.stopPromise) {
|
|
278
|
+
this.stopPromise = this.stopResources();
|
|
279
|
+
}
|
|
280
|
+
return this.stopPromise;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private async stopResources(): Promise<void> {
|
|
284
|
+
const trackedDeviceIds = new Set<string>([
|
|
285
|
+
...this.connectedDevices,
|
|
286
|
+
...this.acquiringDevices,
|
|
287
|
+
...this.deviceProtocol.keys(),
|
|
288
|
+
...this.protocolV2Assemblers.keys(),
|
|
289
|
+
...this.protocolV2PacketCapacities.keys(),
|
|
290
|
+
]);
|
|
291
|
+
|
|
292
|
+
await Promise.all(
|
|
293
|
+
Array.from(trackedDeviceIds, async uuid => {
|
|
294
|
+
const shouldDisconnect = this.connectedDevices.has(uuid) || this.acquiringDevices.has(uuid);
|
|
295
|
+
this.advanceProtocolV2Generation(uuid);
|
|
296
|
+
this.connectedDevices.delete(uuid);
|
|
297
|
+
this.deviceProtocol.delete(uuid);
|
|
298
|
+
this.protocolV2Assemblers.delete(uuid);
|
|
299
|
+
this.protocolV2PacketCapacities.delete(uuid);
|
|
300
|
+
if (shouldDisconnect && typeof this.plugin?.disconnect === 'function') {
|
|
301
|
+
try {
|
|
302
|
+
await this.plugin.disconnect(uuid);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
this.Log?.debug(`[LowlevelTransport] stop disconnect failed: ${uuid}`, error);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
})
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
await this.protocolV2Links
|
|
311
|
+
.dispose('Lowlevel transport stopped')
|
|
312
|
+
.catch(error => this.Log?.debug('[LowlevelTransport] link dispose failed:', error));
|
|
313
|
+
}
|
|
314
|
+
|
|
242
315
|
async call(
|
|
243
316
|
uuid: string,
|
|
244
317
|
name: string,
|
|
@@ -282,6 +355,7 @@ export default class LowlevelTransport {
|
|
|
282
355
|
data: Record<string, unknown>,
|
|
283
356
|
options?: TransportCallOptions
|
|
284
357
|
) {
|
|
358
|
+
this.assertRunning();
|
|
285
359
|
if (!this._messages) {
|
|
286
360
|
throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
|
|
287
361
|
}
|
|
@@ -296,9 +370,11 @@ export default class LowlevelTransport {
|
|
|
296
370
|
let lastLoggedAt = uploadStartedAt;
|
|
297
371
|
|
|
298
372
|
for (const [index, o] of buffers.entries()) {
|
|
373
|
+
this.assertRunning();
|
|
299
374
|
const outData = o.toString('hex');
|
|
300
375
|
try {
|
|
301
376
|
await this.plugin.send(uuid, outData, getProtocolV1SendOptions(name));
|
|
377
|
+
this.assertRunning();
|
|
302
378
|
sentBytes += o.limit;
|
|
303
379
|
|
|
304
380
|
if (isFirmwareUpload) {
|
|
@@ -324,24 +400,33 @@ export default class LowlevelTransport {
|
|
|
324
400
|
}
|
|
325
401
|
}
|
|
326
402
|
} catch (e) {
|
|
327
|
-
this.
|
|
403
|
+
if (this.stopped) {
|
|
404
|
+
throw e;
|
|
405
|
+
}
|
|
406
|
+
this.Log?.debug('lowlevel transport send error: ', e);
|
|
328
407
|
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
329
408
|
}
|
|
330
409
|
}
|
|
331
410
|
|
|
332
411
|
try {
|
|
412
|
+
this.assertRunning();
|
|
333
413
|
const response = await this.readProtocolV1Message(uuid, options?.timeoutMs);
|
|
414
|
+
this.assertRunning();
|
|
334
415
|
const jsonData = ProtocolV1.decodeMessage(messages, response);
|
|
335
416
|
return check.call(jsonData);
|
|
336
417
|
} catch (e) {
|
|
337
418
|
if (
|
|
419
|
+
!this.stopped &&
|
|
338
420
|
e?.errorCode === HardwareErrorCode.BleTimeoutError &&
|
|
339
421
|
options?.timeoutMs !== PROTOCOL_PROBE_TIMEOUT_MS
|
|
340
422
|
) {
|
|
341
423
|
try {
|
|
342
424
|
await this.resetConnectionAfterProbe(uuid, 'V1');
|
|
343
425
|
} catch (resetError) {
|
|
344
|
-
this.Log
|
|
426
|
+
this.Log?.debug(
|
|
427
|
+
'[LowlevelTransport] reset after Protocol V1 timeout failed:',
|
|
428
|
+
resetError
|
|
429
|
+
);
|
|
345
430
|
}
|
|
346
431
|
}
|
|
347
432
|
if (name === 'Initialize' && options?.timeoutMs === PROTOCOL_PROBE_TIMEOUT_MS) {
|
|
@@ -436,12 +521,18 @@ export default class LowlevelTransport {
|
|
|
436
521
|
);
|
|
437
522
|
}
|
|
438
523
|
|
|
524
|
+
this.assertRunning();
|
|
439
525
|
try {
|
|
440
526
|
await this.plugin.connect(uuid);
|
|
527
|
+
this.assertRunning();
|
|
441
528
|
await this.refreshProtocolV2PacketCapacity(uuid);
|
|
529
|
+
this.assertRunning();
|
|
442
530
|
this.connectedDevices.add(uuid);
|
|
443
531
|
this.advanceProtocolV2Generation(uuid);
|
|
444
532
|
} catch (error) {
|
|
533
|
+
if (this.stopped) {
|
|
534
|
+
throw error;
|
|
535
|
+
}
|
|
445
536
|
this.Log?.debug(
|
|
446
537
|
`[LowlevelTransport] reconnect after Protocol ${protocol} probe failed:`,
|
|
447
538
|
error
|
|
@@ -499,9 +590,11 @@ export default class LowlevelTransport {
|
|
|
499
590
|
}
|
|
500
591
|
|
|
501
592
|
private async receiveHex(uuid: string, timeoutMs: number | undefined, commandName: string) {
|
|
593
|
+
this.assertRunning();
|
|
502
594
|
const response = await withProtocolTimeout(this.plugin.receive(uuid), timeoutMs, () =>
|
|
503
595
|
this.createProtocolTimeoutError(commandName, timeoutMs ?? 0)
|
|
504
596
|
);
|
|
597
|
+
this.assertRunning();
|
|
505
598
|
if (typeof response !== 'string') {
|
|
506
599
|
throw new Error('Returning data is not string');
|
|
507
600
|
}
|
|
@@ -644,7 +737,7 @@ export default class LowlevelTransport {
|
|
|
644
737
|
return nextGeneration;
|
|
645
738
|
}
|
|
646
739
|
|
|
647
|
-
cancel(
|
|
740
|
+
cancel() {
|
|
648
741
|
this.Log.debug('lowlevel-transport', 'cancel');
|
|
649
742
|
}
|
|
650
743
|
}
|