@onekeyfe/hd-transport-web-device 1.2.11-alpha.2 → 1.2.11-alpha.20
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__/electron-ble-transport.test.ts +120 -0
- package/__tests__/webusb-protocol-cache.test.ts +31 -0
- package/dist/electron-ble-transport.d.ts +7 -2
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +9 -4
- package/dist/index.js +61 -12
- package/dist/webusb.d.ts +2 -2
- package/dist/webusb.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/electron-ble-transport.ts +70 -9
- package/src/webusb.ts +6 -7
|
@@ -343,6 +343,49 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
343
343
|
}
|
|
344
344
|
);
|
|
345
345
|
|
|
346
|
+
test.each(['Pro 2', 'Neo'])(
|
|
347
|
+
'retains %s V2 subscription while forwarding keepSession=false for idle disconnect',
|
|
348
|
+
async name => {
|
|
349
|
+
const device = { id: 'retained-idle-v2-id', name };
|
|
350
|
+
const release = jest.fn().mockResolvedValue(undefined);
|
|
351
|
+
const nobleBle = { ...createNobleBle(device), release };
|
|
352
|
+
echoProtocolV2(nobleBle, device.id);
|
|
353
|
+
const bleTransport = configureTransport(nobleBle);
|
|
354
|
+
await bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
355
|
+
await bleTransport.call(device.id, 'Ping', { message: 'before-release' });
|
|
356
|
+
const notifyCleanup = nobleBle.onNotification.mock.results[0].value;
|
|
357
|
+
|
|
358
|
+
try {
|
|
359
|
+
await bleTransport.release(device.id, false, false, { retainLink: true });
|
|
360
|
+
expect(release).toHaveBeenCalledWith(device.id, false);
|
|
361
|
+
expect(bleTransport.getProtocolType(device.id)).toBe('V2');
|
|
362
|
+
expect(notifyCleanup).not.toHaveBeenCalled();
|
|
363
|
+
|
|
364
|
+
await expect(
|
|
365
|
+
bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' })
|
|
366
|
+
).resolves.toMatchObject({ uuid: device.id, protocolType: 'V2' });
|
|
367
|
+
await expect(
|
|
368
|
+
bleTransport.call(device.id, 'Ping', { message: 'within-idle-window' })
|
|
369
|
+
).resolves.toMatchObject({ type: 'Success' });
|
|
370
|
+
|
|
371
|
+
expect(nobleBle.connect).toHaveBeenCalledTimes(1);
|
|
372
|
+
expect(nobleBle.subscribe).toHaveBeenCalledTimes(1);
|
|
373
|
+
expect(
|
|
374
|
+
nobleBle.write.mock.calls.map(([, hex]) => Number.parseInt(hex.slice(12, 14), 16))
|
|
375
|
+
).toEqual([1, 2, 3]);
|
|
376
|
+
|
|
377
|
+
// Once the main-process 20s idle timer drops the link, retained state is cleared.
|
|
378
|
+
await bleTransport.release(device.id, false, false, { retainLink: true });
|
|
379
|
+
const disconnectHandler = nobleBle.onDeviceDisconnected.mock.calls[0][0];
|
|
380
|
+
disconnectHandler({ ...device, reason: EBleDisconnectReason.IdleKeepAlive });
|
|
381
|
+
expect(notifyCleanup).toHaveBeenCalledTimes(1);
|
|
382
|
+
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
383
|
+
} finally {
|
|
384
|
+
await bleTransport.disconnect(device.id);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
);
|
|
388
|
+
|
|
346
389
|
test.each(['ordinary release', 'onclose', 'forced probe'])(
|
|
347
390
|
'does not retain renderer state for %s',
|
|
348
391
|
async mode => {
|
|
@@ -1372,4 +1415,81 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1372
1415
|
await bleTransport.release(device.id);
|
|
1373
1416
|
}
|
|
1374
1417
|
});
|
|
1418
|
+
|
|
1419
|
+
test('stop() unsubscribes host disconnect listener and releases owned links idempotently', async () => {
|
|
1420
|
+
const device = { id: 'stop-cleanup-pro2-id', name: 'OneKey Pro 2' };
|
|
1421
|
+
const nobleBle = createNobleBle(device);
|
|
1422
|
+
const emitter = new EventEmitter();
|
|
1423
|
+
const hostDisconnectCleanup = jest.fn();
|
|
1424
|
+
const notifyCleanup = jest.fn();
|
|
1425
|
+
const mtuCleanup = jest.fn();
|
|
1426
|
+
let disconnectHandler:
|
|
1427
|
+
| ((d: { id: string; name: string; reason?: EBleDisconnectReason }) => void)
|
|
1428
|
+
| undefined;
|
|
1429
|
+
|
|
1430
|
+
nobleBle.onDeviceDisconnected.mockImplementation(handler => {
|
|
1431
|
+
disconnectHandler = handler;
|
|
1432
|
+
return () => {
|
|
1433
|
+
hostDisconnectCleanup();
|
|
1434
|
+
disconnectHandler = undefined;
|
|
1435
|
+
};
|
|
1436
|
+
});
|
|
1437
|
+
echoProtocolV2(nobleBle, device.id);
|
|
1438
|
+
const echoOnNotification = nobleBle.onNotification.getMockImplementation();
|
|
1439
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
1440
|
+
echoOnNotification?.(handler);
|
|
1441
|
+
return notifyCleanup;
|
|
1442
|
+
});
|
|
1443
|
+
nobleBle.onMtuChanged.mockImplementation(() => mtuCleanup);
|
|
1444
|
+
|
|
1445
|
+
const transportDisconnect = jest.fn();
|
|
1446
|
+
emitter.on('transport-device-disconnect', transportDisconnect);
|
|
1447
|
+
const bleTransport = configureTransport(nobleBle, emitter);
|
|
1448
|
+
|
|
1449
|
+
await bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
1450
|
+
expect(bleTransport.getProtocolType(device.id)).toBe('V2');
|
|
1451
|
+
|
|
1452
|
+
await bleTransport.stop();
|
|
1453
|
+
|
|
1454
|
+
expect(hostDisconnectCleanup).toHaveBeenCalledTimes(1);
|
|
1455
|
+
expect(notifyCleanup).toHaveBeenCalledTimes(1);
|
|
1456
|
+
expect(mtuCleanup).toHaveBeenCalledTimes(1);
|
|
1457
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1458
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1459
|
+
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1460
|
+
|
|
1461
|
+
// Calling stop() again is a safe no-op.
|
|
1462
|
+
await bleTransport.stop();
|
|
1463
|
+
expect(hostDisconnectCleanup).toHaveBeenCalledTimes(1);
|
|
1464
|
+
expect(nobleBle.disconnect).toHaveBeenCalledTimes(1);
|
|
1465
|
+
|
|
1466
|
+
// Late host disconnect events are no longer delivered to the stopped transport.
|
|
1467
|
+
disconnectHandler?.({ ...device, reason: EBleDisconnectReason.DeviceDisconnected });
|
|
1468
|
+
expect(transportDisconnect).not.toHaveBeenCalled();
|
|
1469
|
+
});
|
|
1470
|
+
|
|
1471
|
+
test('stop() during getDevice() does not let retired acquire disconnect a replacement link', async () => {
|
|
1472
|
+
const device = { id: 'lookup-stop-pro2-id', name: 'OneKey Pro 2' };
|
|
1473
|
+
const nobleBle = createNobleBle(device);
|
|
1474
|
+
let resolveGetDevice: ((value: typeof device) => void) | undefined;
|
|
1475
|
+
nobleBle.getDevice.mockImplementation(
|
|
1476
|
+
() =>
|
|
1477
|
+
new Promise(resolve => {
|
|
1478
|
+
resolveGetDevice = resolve;
|
|
1479
|
+
})
|
|
1480
|
+
);
|
|
1481
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1482
|
+
|
|
1483
|
+
const acquiring = bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
1484
|
+
const stopping = bleTransport.stop();
|
|
1485
|
+
await stopping;
|
|
1486
|
+
// Clear disconnect count recorded during stopResources() so we verify the late
|
|
1487
|
+
// retired acquire does not issue another disconnect against a replacement link.
|
|
1488
|
+
nobleBle.disconnect.mockClear();
|
|
1489
|
+
|
|
1490
|
+
resolveGetDevice?.(device);
|
|
1491
|
+
await expect(acquiring).rejects.toThrow(/Electron BLE transport is stopped/);
|
|
1492
|
+
expect(nobleBle.connect).not.toHaveBeenCalled();
|
|
1493
|
+
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
1494
|
+
});
|
|
1375
1495
|
});
|
|
@@ -16,6 +16,37 @@ const schema = {
|
|
|
16
16
|
},
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
test('reads a multi-packet V1 response without a global Buffer', async () => {
|
|
20
|
+
const webusb = new WebUsbTransport();
|
|
21
|
+
const messages = transport.parseConfigure(schema);
|
|
22
|
+
const response = { message: 'v1 response '.repeat(9) };
|
|
23
|
+
const expected = transport.ProtocolV1.encodeEnvelope(messages, 'Success', response).toString(
|
|
24
|
+
'hex'
|
|
25
|
+
);
|
|
26
|
+
const packets = transport.ProtocolV1.encodeTransportPackets(messages, 'Success', response);
|
|
27
|
+
expect(packets.length).toBeGreaterThan(1);
|
|
28
|
+
const reader = webusb as unknown as {
|
|
29
|
+
transferInWithRetry: () => Promise<Uint8Array>;
|
|
30
|
+
};
|
|
31
|
+
const transferIn = jest.spyOn(reader, 'transferInWithRetry');
|
|
32
|
+
packets.forEach(packet => {
|
|
33
|
+
transferIn.mockResolvedValueOnce(new Uint8Array(packet.toBuffer()));
|
|
34
|
+
});
|
|
35
|
+
const buffer = globalThis.Buffer;
|
|
36
|
+
try {
|
|
37
|
+
Object.defineProperty(globalThis, 'Buffer', { value: undefined });
|
|
38
|
+
const received = await webusb.receiveData('synthetic-usb');
|
|
39
|
+
expect(received).toBe(expected);
|
|
40
|
+
expect(transport.ProtocolV1.decodeMessage(messages, received)).toMatchObject({
|
|
41
|
+
type: 'Success',
|
|
42
|
+
message: response,
|
|
43
|
+
});
|
|
44
|
+
} finally {
|
|
45
|
+
Object.defineProperty(globalThis, 'Buffer', { value: buffer });
|
|
46
|
+
transferIn.mockRestore();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
19
50
|
function buildAcquirableTransport(path = 'pro-webusb') {
|
|
20
51
|
const webusb = new WebUsbTransport() as any;
|
|
21
52
|
webusb.Log = { debug: jest.fn() };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { Deferred } from '@onekeyfe/hd-shared';
|
|
3
3
|
import type { DesktopAPI } from '@onekeyfe/hd-transport-electron';
|
|
4
|
-
import type { OneKeyDeviceInfo, ProtocolType, TransportCallOptions } from '@onekeyfe/hd-transport';
|
|
4
|
+
import type { OneKeyDeviceInfo, ProtocolType, TransportCallOptions, TransportReleaseOptions } from '@onekeyfe/hd-transport';
|
|
5
5
|
import type EventEmitter from 'events';
|
|
6
6
|
declare global {
|
|
7
7
|
interface Window {
|
|
@@ -28,6 +28,8 @@ export default class ElectronBleTransport {
|
|
|
28
28
|
private retainedDevices;
|
|
29
29
|
private protocolV2CallCounts;
|
|
30
30
|
private acquiringDevices;
|
|
31
|
+
private stopped;
|
|
32
|
+
private stopPromise?;
|
|
31
33
|
private deviceProtocol;
|
|
32
34
|
private deviceProtocolHints;
|
|
33
35
|
private deviceMtus;
|
|
@@ -47,6 +49,9 @@ export default class ElectronBleTransport {
|
|
|
47
49
|
private handleBluetoothError;
|
|
48
50
|
private cleanupDeviceState;
|
|
49
51
|
init(logger: any, emitter?: EventEmitter): void;
|
|
52
|
+
private assertRunning;
|
|
53
|
+
stop(): Promise<void>;
|
|
54
|
+
private stopResources;
|
|
50
55
|
private subscribeHostDisconnects;
|
|
51
56
|
configure(signedData: any): void;
|
|
52
57
|
configureProtocolV2(signedData: any): void;
|
|
@@ -64,7 +69,7 @@ export default class ElectronBleTransport {
|
|
|
64
69
|
bleColorCode?: string | undefined;
|
|
65
70
|
protocolType?: ProtocolType | undefined;
|
|
66
71
|
}>;
|
|
67
|
-
release(id: string, onclose?: boolean, keepSession?: boolean): Promise<void>;
|
|
72
|
+
release(id: string, onclose?: boolean, keepSession?: boolean, options?: TransportReleaseOptions): Promise<void>;
|
|
68
73
|
disconnect(id: string): Promise<void>;
|
|
69
74
|
private releaseNative;
|
|
70
75
|
private releaseLogical;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";AAyBA,OAAO,KAAK,EAAE,QAAQ,EA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M;YAgBb,aAAa;YAqBb,cAAc;IA4B5B,OAAO,CAAC,2BAA2B;IAMnC,OAAO,CAAC,4BAA4B;IAIpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;IA0D5B,OAAO,CAAC,8BAA8B;YAgBxB,iCAAiC;YAwBjC,eAAe;YAqBf,eAAe;YAyBf,SAAS;YAeT,wBAAwB;IAMtC,OAAO,CAAC,uBAAuB;IAc/B,OAAO,CAAC,qBAAqB;IAU7B,OAAO,CAAC,oBAAoB;IAqB5B,OAAO,CAAC,kBAAkB;IA+B1B,OAAO,CAAC,iCAAiC;IAazC,OAAO,CAAC,4BAA4B;IAkBpC,OAAO,CAAC,uBAAuB;IAS/B,OAAO,CAAC,sBAAsB;IAU9B,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,sBAAsB;YAShB,mBAAmB;IAiBjC,OAAO,CAAC,4BAA4B;IAqB9B,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;IAuB1B,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;YAatD,cAAc;YAqFd,cAAc;IA+B5B,OAAO,CAAC,uBAAuB;IAyC/B,OAAO,CAAC,6BAA6B;IA4CrC,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _onekeyfe_hd_transport from '@onekeyfe/hd-transport';
|
|
2
|
-
import _onekeyfe_hd_transport__default, { ProtocolV2UsbTransportBase, AcquireInput, TransportCallOptions, ProtocolV2Schemas, ProtocolV2CallContext, ProtocolType, OneKeyDeviceInfoBase, OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
|
|
2
|
+
import _onekeyfe_hd_transport__default, { ProtocolV2UsbTransportBase, AcquireInput, TransportCallOptions, ProtocolV2Schemas, ProtocolV2CallContext, ProtocolType, OneKeyDeviceInfoBase, OneKeyDeviceInfo, TransportReleaseOptions } from '@onekeyfe/hd-transport';
|
|
3
3
|
import EventEmitter from 'events';
|
|
4
4
|
import { Deferred } from '@onekeyfe/hd-shared';
|
|
5
5
|
import { DesktopAPI } from '@onekeyfe/hd-transport-electron';
|
|
@@ -22,9 +22,9 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
|
|
|
22
22
|
private handleOperationSequence;
|
|
23
23
|
private deviceLogSequence;
|
|
24
24
|
private deviceLogIds;
|
|
25
|
-
messages: ReturnType<typeof _onekeyfe_hd_transport__default.
|
|
25
|
+
messages: ReturnType<typeof _onekeyfe_hd_transport__default.parseTransportConfigure> | undefined;
|
|
26
26
|
/** Protobuf schema for Protocol V2 transports. */
|
|
27
|
-
messagesV2: ReturnType<typeof _onekeyfe_hd_transport__default.
|
|
27
|
+
messagesV2: ReturnType<typeof _onekeyfe_hd_transport__default.parseTransportConfigure> | undefined;
|
|
28
28
|
private protocolV2SchemaSource;
|
|
29
29
|
/** Per-path protocol type detected by active wire-level probe. */
|
|
30
30
|
private deviceProtocol;
|
|
@@ -265,6 +265,8 @@ declare class ElectronBleTransport {
|
|
|
265
265
|
* the link; `releaseNative()` needs it to cancel that pending connect.
|
|
266
266
|
*/
|
|
267
267
|
private acquiringDevices;
|
|
268
|
+
private stopped;
|
|
269
|
+
private stopPromise?;
|
|
268
270
|
private deviceProtocol;
|
|
269
271
|
private deviceProtocolHints;
|
|
270
272
|
private deviceMtus;
|
|
@@ -293,6 +295,9 @@ declare class ElectronBleTransport {
|
|
|
293
295
|
private handleBluetoothError;
|
|
294
296
|
private cleanupDeviceState;
|
|
295
297
|
init(logger: any, emitter?: EventEmitter): void;
|
|
298
|
+
private assertRunning;
|
|
299
|
+
stop(): Promise<void>;
|
|
300
|
+
private stopResources;
|
|
296
301
|
/**
|
|
297
302
|
* One host subscription for the whole transport lifetime. init() can run
|
|
298
303
|
* again after an SDK reset, so drop the previous listener first rather than
|
|
@@ -315,7 +320,7 @@ declare class ElectronBleTransport {
|
|
|
315
320
|
bleColorCode?: string | undefined;
|
|
316
321
|
protocolType?: ProtocolType | undefined;
|
|
317
322
|
}>;
|
|
318
|
-
release(id: string, onclose?: boolean, keepSession?: boolean): Promise<void>;
|
|
323
|
+
release(id: string, onclose?: boolean, keepSession?: boolean, options?: TransportReleaseOptions): Promise<void>;
|
|
319
324
|
disconnect(id: string): Promise<void>;
|
|
320
325
|
private releaseNative;
|
|
321
326
|
private releaseLogical;
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
41
41
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const {
|
|
44
|
+
const { parseTransportConfigure: parseTransportConfigure$1, check: check$1, ProtocolV1: ProtocolV1$1 } = transport__default["default"];
|
|
45
45
|
const CONFIGURATION_ID = 1;
|
|
46
46
|
const INTERFACE_ID = 0;
|
|
47
47
|
const ENDPOINT_ID = 1;
|
|
@@ -320,7 +320,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
320
320
|
this.probedDeviceObjects.delete(path);
|
|
321
321
|
}
|
|
322
322
|
configure(signedData) {
|
|
323
|
-
const messages =
|
|
323
|
+
const messages = parseTransportConfigure$1(signedData);
|
|
324
324
|
this.configured = true;
|
|
325
325
|
this.messages = messages;
|
|
326
326
|
}
|
|
@@ -332,7 +332,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
332
332
|
if (schemaSource === this.protocolV2SchemaSource)
|
|
333
333
|
return;
|
|
334
334
|
const hadProtocolV2Schema = this.protocolV2SchemaSource !== undefined;
|
|
335
|
-
this.messagesV2 =
|
|
335
|
+
this.messagesV2 = parseTransportConfigure$1(signedData);
|
|
336
336
|
this.protocolV2SchemaSource = schemaSource;
|
|
337
337
|
if (!hadProtocolV2Schema)
|
|
338
338
|
return;
|
|
@@ -1428,8 +1428,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
1428
1428
|
}
|
|
1429
1429
|
}
|
|
1430
1430
|
decoded.reset();
|
|
1431
|
-
|
|
1432
|
-
return Buffer.from(result).toString('hex');
|
|
1431
|
+
return decoded.toString('hex');
|
|
1433
1432
|
});
|
|
1434
1433
|
}
|
|
1435
1434
|
release(path) {
|
|
@@ -1505,7 +1504,7 @@ function resolveBlePacketCapacity(mtu, maximumPacketCapacity, fallbackPacketCapa
|
|
|
1505
1504
|
return Math.min(maximumPacketCapacity, Math.floor(mtu) - BLE_ATT_HEADER_BYTES);
|
|
1506
1505
|
}
|
|
1507
1506
|
|
|
1508
|
-
const {
|
|
1507
|
+
const { parseTransportConfigure, ProtocolV1, check } = transport__default["default"];
|
|
1509
1508
|
const toBleDescriptor = (device, protocolType) => (Object.assign(Object.assign({ id: device.id, name: device.name, path: device.id, debug: false, commType: 'electron-ble' }, (device.bleColorCode ? { bleColorCode: device.bleColorCode } : {})), (protocolType ? { protocolType } : {})));
|
|
1510
1509
|
const invokeNobleBle = (request) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1511
1510
|
const response = yield request;
|
|
@@ -1535,6 +1534,7 @@ class ElectronBleTransport {
|
|
|
1535
1534
|
this.retainedDevices = new Set();
|
|
1536
1535
|
this.protocolV2CallCounts = new Map();
|
|
1537
1536
|
this.acquiringDevices = new Set();
|
|
1537
|
+
this.stopped = false;
|
|
1538
1538
|
this.deviceProtocol = new Map();
|
|
1539
1539
|
this.deviceProtocolHints = new Map();
|
|
1540
1540
|
this.deviceMtus = new Map();
|
|
@@ -1559,7 +1559,7 @@ class ElectronBleTransport {
|
|
|
1559
1559
|
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
1560
1560
|
this.rejectProtocolV2Frames(uuid, new Error(reason));
|
|
1561
1561
|
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug('[Electron BLE] Protocol V2 link invalidated:', uuid, reason);
|
|
1562
|
-
if (reason.startsWith('Protocol V2 link-fatal error:')) {
|
|
1562
|
+
if (!this.stopped && reason.startsWith('Protocol V2 link-fatal error:')) {
|
|
1563
1563
|
yield this.releaseNative(uuid, { forceNative: true });
|
|
1564
1564
|
}
|
|
1565
1565
|
}),
|
|
@@ -1639,6 +1639,8 @@ class ElectronBleTransport {
|
|
|
1639
1639
|
}
|
|
1640
1640
|
init(logger, emitter) {
|
|
1641
1641
|
var _a, _b;
|
|
1642
|
+
this.stopped = false;
|
|
1643
|
+
this.stopPromise = undefined;
|
|
1642
1644
|
this.Log = logger;
|
|
1643
1645
|
this.emitter = emitter;
|
|
1644
1646
|
if (!((_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle)) {
|
|
@@ -1647,6 +1649,41 @@ class ElectronBleTransport {
|
|
|
1647
1649
|
this.subscribeHostDisconnects();
|
|
1648
1650
|
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug('[Electron BLE] Transport initialized');
|
|
1649
1651
|
}
|
|
1652
|
+
assertRunning() {
|
|
1653
|
+
if (this.stopped) {
|
|
1654
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Electron BLE transport is stopped');
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
stop() {
|
|
1658
|
+
var _a;
|
|
1659
|
+
this.stopped = true;
|
|
1660
|
+
(_a = this.hostDisconnectCleanup) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
1661
|
+
this.hostDisconnectCleanup = undefined;
|
|
1662
|
+
if (!this.stopPromise) {
|
|
1663
|
+
this.stopPromise = this.stopResources();
|
|
1664
|
+
}
|
|
1665
|
+
return this.stopPromise;
|
|
1666
|
+
}
|
|
1667
|
+
stopResources() {
|
|
1668
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1669
|
+
const trackedDeviceIds = new Set([
|
|
1670
|
+
...this.connectedDevices,
|
|
1671
|
+
...this.retainedDevices,
|
|
1672
|
+
...this.acquiringDevices,
|
|
1673
|
+
...this.notificationCleanups.keys(),
|
|
1674
|
+
...this.mtuCleanups.keys(),
|
|
1675
|
+
...this.deviceProtocol.keys(),
|
|
1676
|
+
...this.v1Buffers.keys(),
|
|
1677
|
+
...this.v2Assemblers.keys(),
|
|
1678
|
+
]);
|
|
1679
|
+
yield Promise.all(Array.from(trackedDeviceIds, deviceId => this.releaseNative(deviceId)));
|
|
1680
|
+
this.acquiringDevices.clear();
|
|
1681
|
+
this.protocolV2CallCounts.clear();
|
|
1682
|
+
yield this.protocolV2Links
|
|
1683
|
+
.dispose('Electron BLE transport stopped')
|
|
1684
|
+
.catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] link dispose failed:', error); });
|
|
1685
|
+
});
|
|
1686
|
+
}
|
|
1650
1687
|
subscribeHostDisconnects() {
|
|
1651
1688
|
var _a, _b, _c;
|
|
1652
1689
|
(_a = this.hostDisconnectCleanup) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
@@ -1665,7 +1702,7 @@ class ElectronBleTransport {
|
|
|
1665
1702
|
});
|
|
1666
1703
|
}
|
|
1667
1704
|
configure(signedData) {
|
|
1668
|
-
this._messages =
|
|
1705
|
+
this._messages = parseTransportConfigure(signedData);
|
|
1669
1706
|
this.configured = true;
|
|
1670
1707
|
}
|
|
1671
1708
|
configureProtocolV2(signedData) {
|
|
@@ -1674,7 +1711,7 @@ class ElectronBleTransport {
|
|
|
1674
1711
|
return;
|
|
1675
1712
|
}
|
|
1676
1713
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
1677
|
-
this._messagesV2 =
|
|
1714
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
1678
1715
|
this.protocolV2SchemaConfiguration = configuration;
|
|
1679
1716
|
if (isReconfiguration) {
|
|
1680
1717
|
this.protocolV2Links
|
|
@@ -1730,12 +1767,15 @@ class ElectronBleTransport {
|
|
|
1730
1767
|
this.runPromise = null;
|
|
1731
1768
|
this.runPromiseDeviceId = null;
|
|
1732
1769
|
}
|
|
1770
|
+
this.assertRunning();
|
|
1733
1771
|
this.acquiringDevices.add(uuid);
|
|
1772
|
+
let connectAttempted = false;
|
|
1734
1773
|
try {
|
|
1735
1774
|
if (!((_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle)) {
|
|
1736
1775
|
throw new Error('Noble BLE API not available');
|
|
1737
1776
|
}
|
|
1738
1777
|
const device = yield invokeNobleBle(window.desktopApi.nobleBle.getDevice(uuid));
|
|
1778
|
+
this.assertRunning();
|
|
1739
1779
|
if (!device) {
|
|
1740
1780
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotFound, `Device ${uuid} not found`);
|
|
1741
1781
|
}
|
|
@@ -1752,12 +1792,14 @@ class ElectronBleTransport {
|
|
|
1752
1792
|
this.deviceProtocolHints.set(uuid, protocolHint);
|
|
1753
1793
|
}
|
|
1754
1794
|
try {
|
|
1795
|
+
connectAttempted = true;
|
|
1755
1796
|
yield invokeNobleBle(window.desktopApi.nobleBle.connect(uuid));
|
|
1756
1797
|
this.connectedDevices.add(uuid);
|
|
1757
1798
|
}
|
|
1758
1799
|
catch (error) {
|
|
1759
1800
|
this.handleBluetoothError(error);
|
|
1760
1801
|
}
|
|
1802
|
+
this.assertRunning();
|
|
1761
1803
|
const mtuCleanup = this.createMtuSubscription(uuid);
|
|
1762
1804
|
if (mtuCleanup) {
|
|
1763
1805
|
this.mtuCleanups.set(uuid, mtuCleanup);
|
|
@@ -1771,9 +1813,11 @@ class ElectronBleTransport {
|
|
|
1771
1813
|
this.handleBluetoothError(error);
|
|
1772
1814
|
}
|
|
1773
1815
|
yield this.refreshBlePacketCapacity(uuid);
|
|
1816
|
+
this.assertRunning();
|
|
1774
1817
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
1775
1818
|
this.notificationCleanups.set(uuid, cleanup);
|
|
1776
1819
|
const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint);
|
|
1820
|
+
this.assertRunning();
|
|
1777
1821
|
if (protocolType === 'V2') {
|
|
1778
1822
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug('[Electron BLE] Protocol V2 write configured', {
|
|
1779
1823
|
writeMode: 'withoutResponse',
|
|
@@ -1787,7 +1831,7 @@ class ElectronBleTransport {
|
|
|
1787
1831
|
(_e = this.Log) === null || _e === void 0 ? void 0 : _e.error('[Electron BLE] acquire failed:', error);
|
|
1788
1832
|
try {
|
|
1789
1833
|
const nobleBle = (_f = window.desktopApi) === null || _f === void 0 ? void 0 : _f.nobleBle;
|
|
1790
|
-
if (nobleBle) {
|
|
1834
|
+
if (nobleBle && !this.stopped && (connectAttempted || this.connectedDevices.has(uuid))) {
|
|
1791
1835
|
if (this.connectedDevices.has(uuid)) {
|
|
1792
1836
|
yield invokeNobleBle(nobleBle.unsubscribe(uuid)).catch(cleanupError => {
|
|
1793
1837
|
var _a;
|
|
@@ -1808,11 +1852,11 @@ class ElectronBleTransport {
|
|
|
1808
1852
|
}
|
|
1809
1853
|
});
|
|
1810
1854
|
}
|
|
1811
|
-
release(id, onclose, keepSession) {
|
|
1855
|
+
release(id, onclose, keepSession, options) {
|
|
1812
1856
|
var _a;
|
|
1813
1857
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1814
1858
|
try {
|
|
1815
|
-
const retainLink = keepSession === true &&
|
|
1859
|
+
const retainLink = (keepSession === true || (options === null || options === void 0 ? void 0 : options.retainLink) === true) &&
|
|
1816
1860
|
!onclose &&
|
|
1817
1861
|
this.connectedDevices.has(id) &&
|
|
1818
1862
|
this.deviceProtocol.get(id) === 'V2' &&
|
|
@@ -1917,11 +1961,14 @@ class ElectronBleTransport {
|
|
|
1917
1961
|
}
|
|
1918
1962
|
const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
1919
1963
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
1964
|
+
this.assertRunning();
|
|
1920
1965
|
const protocol = probeOrder[i];
|
|
1921
1966
|
if (i > 0) {
|
|
1922
1967
|
yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
1968
|
+
this.assertRunning();
|
|
1923
1969
|
}
|
|
1924
1970
|
const detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
|
|
1971
|
+
this.assertRunning();
|
|
1925
1972
|
if (detected) {
|
|
1926
1973
|
this.deviceProtocol.set(uuid, protocol);
|
|
1927
1974
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -2015,12 +2062,14 @@ class ElectronBleTransport {
|
|
|
2015
2062
|
writeOnce(uuid, hexData) {
|
|
2016
2063
|
var _a;
|
|
2017
2064
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2065
|
+
this.assertRunning();
|
|
2018
2066
|
const nobleBle = (_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle;
|
|
2019
2067
|
if (!nobleBle) {
|
|
2020
2068
|
throw new Error('Noble BLE API not available');
|
|
2021
2069
|
}
|
|
2022
2070
|
try {
|
|
2023
2071
|
yield invokeNobleBle(nobleBle.write(uuid, hexData, { pacingDelayMs: 0 }));
|
|
2072
|
+
this.assertRunning();
|
|
2024
2073
|
}
|
|
2025
2074
|
catch (error) {
|
|
2026
2075
|
this.handleBluetoothError(error);
|
package/dist/webusb.d.ts
CHANGED
|
@@ -18,8 +18,8 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
18
18
|
private handleOperationSequence;
|
|
19
19
|
private deviceLogSequence;
|
|
20
20
|
private deviceLogIds;
|
|
21
|
-
messages: ReturnType<typeof transport.
|
|
22
|
-
messagesV2: ReturnType<typeof transport.
|
|
21
|
+
messages: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
22
|
+
messagesV2: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
23
23
|
private protocolV2SchemaSource;
|
|
24
24
|
private deviceProtocol;
|
|
25
25
|
private confirmedDeviceProtocols;
|
package/dist/webusb.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webusb.d.ts","sourceRoot":"","sources":["../src/webusb.ts"],"names":[],"mappings":";;AACA,OAAO,SAAS,EAAE,EAQhB,0BAA0B,EAG3B,MAAM,wBAAwB,CAAC;AAahC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,YAAY,EACZ,oBAAoB,EACpB,YAAY,EACZ,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AA0BhC,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AA2BD,UAAU,oBAAoB;IAC5B,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAmCD,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,0BAA0B,CAAC,MAAM,CAAC;IA+EjE,OAAO,CAAC,QAAQ,CAAC,aAAa;IA9E1C,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAmC;IAEvE,OAAO,CAAC,uBAAuB,CAAK;IAEpC,OAAO,CAAC,iBAAiB,CAAK;IAE9B,OAAO,CAAC,YAAY,CAAoC;IAExD,QAAQ,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,
|
|
1
|
+
{"version":3,"file":"webusb.d.ts","sourceRoot":"","sources":["../src/webusb.ts"],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AI,iBAAiB;IAUtD,SAAS,CAAC,sBAAsB;cAIhB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,IAAI,CAAC;cAIA,uBAAuB,CACrC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,UAAU,CAAC;cASN,4BAA4B,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1F,SAAS,CAAC,8BAA8B,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAKrE,SAAS,CAAC,+BAA+B,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,KAAK;IAWjF,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-web-device",
|
|
3
|
-
"version": "1.2.11-alpha.
|
|
3
|
+
"version": "1.2.11-alpha.20",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
"lint:fix": "eslint . --fix"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.20",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.20",
|
|
26
|
+
"bytebuffer": "^5.0.1"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.
|
|
29
|
+
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.20",
|
|
29
30
|
"@types/w3c-web-usb": "^1.0.6",
|
|
30
31
|
"@types/web-bluetooth": "^0.0.17"
|
|
31
32
|
},
|
|
32
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "32db23ffce1a306da3292732503b6afc4b5ac332"
|
|
33
34
|
}
|
|
@@ -31,10 +31,11 @@ import type {
|
|
|
31
31
|
ProtocolType,
|
|
32
32
|
ProtocolV2CallContext,
|
|
33
33
|
TransportCallOptions,
|
|
34
|
+
TransportReleaseOptions,
|
|
34
35
|
} from '@onekeyfe/hd-transport';
|
|
35
36
|
import type EventEmitter from 'events';
|
|
36
37
|
|
|
37
|
-
const {
|
|
38
|
+
const { parseTransportConfigure, ProtocolV1, check } = transport;
|
|
38
39
|
|
|
39
40
|
declare global {
|
|
40
41
|
interface Window {
|
|
@@ -98,9 +99,9 @@ const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
|
98
99
|
* after a Protocol V1 GetFeatures timeout by probing Protocol V2 Ping.
|
|
99
100
|
*/
|
|
100
101
|
export default class ElectronBleTransport {
|
|
101
|
-
private _messages: ReturnType<typeof transport.
|
|
102
|
+
private _messages: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
102
103
|
|
|
103
|
-
private _messagesV2: ReturnType<typeof transport.
|
|
104
|
+
private _messagesV2: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
104
105
|
|
|
105
106
|
private protocolV2SchemaConfiguration: string | undefined;
|
|
106
107
|
|
|
@@ -131,6 +132,10 @@ export default class ElectronBleTransport {
|
|
|
131
132
|
*/
|
|
132
133
|
private acquiringDevices: Set<string> = new Set();
|
|
133
134
|
|
|
135
|
+
private stopped = false;
|
|
136
|
+
|
|
137
|
+
private stopPromise?: Promise<void>;
|
|
138
|
+
|
|
134
139
|
private deviceProtocol: Map<string, ProtocolType> = new Map();
|
|
135
140
|
|
|
136
141
|
private deviceProtocolHints: Map<string, ProtocolType> = new Map();
|
|
@@ -162,7 +167,7 @@ export default class ElectronBleTransport {
|
|
|
162
167
|
this.v2Assemblers.get(uuid)?.reset();
|
|
163
168
|
this.rejectProtocolV2Frames(uuid, new Error(reason));
|
|
164
169
|
this.Log?.debug('[Electron BLE] Protocol V2 link invalidated:', uuid, reason);
|
|
165
|
-
if (reason.startsWith('Protocol V2 link-fatal error:')) {
|
|
170
|
+
if (!this.stopped && reason.startsWith('Protocol V2 link-fatal error:')) {
|
|
166
171
|
await this.releaseNative(uuid, { forceNative: true });
|
|
167
172
|
}
|
|
168
173
|
},
|
|
@@ -269,6 +274,8 @@ export default class ElectronBleTransport {
|
|
|
269
274
|
}
|
|
270
275
|
|
|
271
276
|
init(logger: any, emitter?: EventEmitter) {
|
|
277
|
+
this.stopped = false;
|
|
278
|
+
this.stopPromise = undefined;
|
|
272
279
|
this.Log = logger;
|
|
273
280
|
this.emitter = emitter;
|
|
274
281
|
|
|
@@ -284,6 +291,43 @@ export default class ElectronBleTransport {
|
|
|
284
291
|
this.Log?.debug('[Electron BLE] Transport initialized');
|
|
285
292
|
}
|
|
286
293
|
|
|
294
|
+
private assertRunning() {
|
|
295
|
+
if (this.stopped) {
|
|
296
|
+
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Electron BLE transport is stopped');
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
stop(): Promise<void> {
|
|
301
|
+
this.stopped = true;
|
|
302
|
+
this.hostDisconnectCleanup?.();
|
|
303
|
+
this.hostDisconnectCleanup = undefined;
|
|
304
|
+
|
|
305
|
+
if (!this.stopPromise) {
|
|
306
|
+
this.stopPromise = this.stopResources();
|
|
307
|
+
}
|
|
308
|
+
return this.stopPromise;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private async stopResources(): Promise<void> {
|
|
312
|
+
const trackedDeviceIds = new Set<string>([
|
|
313
|
+
...this.connectedDevices,
|
|
314
|
+
...this.retainedDevices,
|
|
315
|
+
...this.acquiringDevices,
|
|
316
|
+
...this.notificationCleanups.keys(),
|
|
317
|
+
...this.mtuCleanups.keys(),
|
|
318
|
+
...this.deviceProtocol.keys(),
|
|
319
|
+
...this.v1Buffers.keys(),
|
|
320
|
+
...this.v2Assemblers.keys(),
|
|
321
|
+
]);
|
|
322
|
+
|
|
323
|
+
await Promise.all(Array.from(trackedDeviceIds, deviceId => this.releaseNative(deviceId)));
|
|
324
|
+
this.acquiringDevices.clear();
|
|
325
|
+
this.protocolV2CallCounts.clear();
|
|
326
|
+
await this.protocolV2Links
|
|
327
|
+
.dispose('Electron BLE transport stopped')
|
|
328
|
+
.catch(error => this.Log?.debug('[Electron BLE] link dispose failed:', error));
|
|
329
|
+
}
|
|
330
|
+
|
|
287
331
|
/**
|
|
288
332
|
* One host subscription for the whole transport lifetime. init() can run
|
|
289
333
|
* again after an SDK reset, so drop the previous listener first rather than
|
|
@@ -322,7 +366,7 @@ export default class ElectronBleTransport {
|
|
|
322
366
|
}
|
|
323
367
|
|
|
324
368
|
configure(signedData: any) {
|
|
325
|
-
this._messages =
|
|
369
|
+
this._messages = parseTransportConfigure(signedData);
|
|
326
370
|
this.configured = true;
|
|
327
371
|
}
|
|
328
372
|
|
|
@@ -333,7 +377,7 @@ export default class ElectronBleTransport {
|
|
|
333
377
|
}
|
|
334
378
|
|
|
335
379
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
336
|
-
this._messagesV2 =
|
|
380
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
337
381
|
this.protocolV2SchemaConfiguration = configuration;
|
|
338
382
|
if (isReconfiguration) {
|
|
339
383
|
this.protocolV2Links
|
|
@@ -390,13 +434,16 @@ export default class ElectronBleTransport {
|
|
|
390
434
|
this.runPromiseDeviceId = null;
|
|
391
435
|
}
|
|
392
436
|
|
|
437
|
+
this.assertRunning();
|
|
393
438
|
this.acquiringDevices.add(uuid);
|
|
439
|
+
let connectAttempted = false;
|
|
394
440
|
try {
|
|
395
441
|
if (!window.desktopApi?.nobleBle) {
|
|
396
442
|
throw new Error('Noble BLE API not available');
|
|
397
443
|
}
|
|
398
444
|
|
|
399
445
|
const device = await invokeNobleBle(window.desktopApi.nobleBle.getDevice(uuid));
|
|
446
|
+
this.assertRunning();
|
|
400
447
|
if (!device) {
|
|
401
448
|
throw ERRORS.TypedError(HardwareErrorCode.DeviceNotFound, `Device ${uuid} not found`);
|
|
402
449
|
}
|
|
@@ -420,11 +467,13 @@ export default class ElectronBleTransport {
|
|
|
420
467
|
}
|
|
421
468
|
|
|
422
469
|
try {
|
|
470
|
+
connectAttempted = true;
|
|
423
471
|
await invokeNobleBle(window.desktopApi.nobleBle.connect(uuid));
|
|
424
472
|
this.connectedDevices.add(uuid);
|
|
425
473
|
} catch (error) {
|
|
426
474
|
this.handleBluetoothError(error);
|
|
427
475
|
}
|
|
476
|
+
this.assertRunning();
|
|
428
477
|
|
|
429
478
|
const mtuCleanup = this.createMtuSubscription(uuid);
|
|
430
479
|
if (mtuCleanup) {
|
|
@@ -440,11 +489,13 @@ export default class ElectronBleTransport {
|
|
|
440
489
|
this.handleBluetoothError(error);
|
|
441
490
|
}
|
|
442
491
|
await this.refreshBlePacketCapacity(uuid);
|
|
492
|
+
this.assertRunning();
|
|
443
493
|
|
|
444
494
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
445
495
|
this.notificationCleanups.set(uuid, cleanup);
|
|
446
496
|
|
|
447
497
|
const protocolType = await this.detectProtocol(uuid, expectedProtocol, protocolHint);
|
|
498
|
+
this.assertRunning();
|
|
448
499
|
if (protocolType === 'V2') {
|
|
449
500
|
this.Log?.debug('[Electron BLE] Protocol V2 write configured', {
|
|
450
501
|
writeMode: 'withoutResponse',
|
|
@@ -461,7 +512,7 @@ export default class ElectronBleTransport {
|
|
|
461
512
|
this.Log?.error('[Electron BLE] acquire failed:', error);
|
|
462
513
|
try {
|
|
463
514
|
const nobleBle = window.desktopApi?.nobleBle;
|
|
464
|
-
if (nobleBle) {
|
|
515
|
+
if (nobleBle && !this.stopped && (connectAttempted || this.connectedDevices.has(uuid))) {
|
|
465
516
|
if (this.connectedDevices.has(uuid)) {
|
|
466
517
|
await invokeNobleBle(nobleBle.unsubscribe(uuid)).catch(cleanupError => {
|
|
467
518
|
this.Log?.debug('[Electron BLE] acquire unsubscribe failed:', cleanupError);
|
|
@@ -479,10 +530,15 @@ export default class ElectronBleTransport {
|
|
|
479
530
|
}
|
|
480
531
|
}
|
|
481
532
|
|
|
482
|
-
async release(
|
|
533
|
+
async release(
|
|
534
|
+
id: string,
|
|
535
|
+
onclose?: boolean,
|
|
536
|
+
keepSession?: boolean,
|
|
537
|
+
options?: TransportReleaseOptions
|
|
538
|
+
) {
|
|
483
539
|
try {
|
|
484
540
|
const retainLink =
|
|
485
|
-
keepSession === true &&
|
|
541
|
+
(keepSession === true || options?.retainLink === true) &&
|
|
486
542
|
!onclose &&
|
|
487
543
|
this.connectedDevices.has(id) &&
|
|
488
544
|
this.deviceProtocol.get(id) === 'V2' &&
|
|
@@ -616,14 +672,17 @@ export default class ElectronBleTransport {
|
|
|
616
672
|
protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
617
673
|
|
|
618
674
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
675
|
+
this.assertRunning();
|
|
619
676
|
const protocol = probeOrder[i];
|
|
620
677
|
if (i > 0) {
|
|
621
678
|
// Keep the physical BLE link while switching probes. Reconnecting here can
|
|
622
679
|
// summon a second OS pairing prompt for a device that is still onboarding.
|
|
623
680
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
681
|
+
this.assertRunning();
|
|
624
682
|
}
|
|
625
683
|
const detected =
|
|
626
684
|
protocol === 'V1' ? await this.probeProtocolV1(uuid) : await this.probeProtocolV2(uuid);
|
|
685
|
+
this.assertRunning();
|
|
627
686
|
if (detected) {
|
|
628
687
|
this.deviceProtocol.set(uuid, protocol);
|
|
629
688
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -722,6 +781,7 @@ export default class ElectronBleTransport {
|
|
|
722
781
|
}
|
|
723
782
|
|
|
724
783
|
private async writeOnce(uuid: string, hexData: string): Promise<void> {
|
|
784
|
+
this.assertRunning();
|
|
725
785
|
const nobleBle = window.desktopApi?.nobleBle;
|
|
726
786
|
if (!nobleBle) {
|
|
727
787
|
throw new Error('Noble BLE API not available');
|
|
@@ -729,6 +789,7 @@ export default class ElectronBleTransport {
|
|
|
729
789
|
|
|
730
790
|
try {
|
|
731
791
|
await invokeNobleBle(nobleBle.write(uuid, hexData, { pacingDelayMs: 0 }));
|
|
792
|
+
this.assertRunning();
|
|
732
793
|
} catch (error) {
|
|
733
794
|
this.handleBluetoothError(error);
|
|
734
795
|
}
|
package/src/webusb.ts
CHANGED
|
@@ -33,7 +33,7 @@ import type {
|
|
|
33
33
|
TransportCallOptions,
|
|
34
34
|
} from '@onekeyfe/hd-transport';
|
|
35
35
|
|
|
36
|
-
const {
|
|
36
|
+
const { parseTransportConfigure, check, ProtocolV1 } = transport;
|
|
37
37
|
|
|
38
38
|
const CONFIGURATION_ID = 1;
|
|
39
39
|
const INTERFACE_ID = 0;
|
|
@@ -135,10 +135,10 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
135
135
|
|
|
136
136
|
private deviceLogIds = new WeakMap<USBDevice, number>();
|
|
137
137
|
|
|
138
|
-
messages: ReturnType<typeof transport.
|
|
138
|
+
messages: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
139
139
|
|
|
140
140
|
/** Protobuf schema for Protocol V2 transports. */
|
|
141
|
-
messagesV2: ReturnType<typeof transport.
|
|
141
|
+
messagesV2: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
142
142
|
|
|
143
143
|
private protocolV2SchemaSource: string | undefined;
|
|
144
144
|
|
|
@@ -505,7 +505,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
505
505
|
* Configure Protocol V1 protobuf schema (legacy chunked 0x3F framing).
|
|
506
506
|
*/
|
|
507
507
|
configure(signedData: any) {
|
|
508
|
-
const messages =
|
|
508
|
+
const messages = parseTransportConfigure(signedData);
|
|
509
509
|
this.configured = true;
|
|
510
510
|
this.messages = messages;
|
|
511
511
|
}
|
|
@@ -521,7 +521,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
521
521
|
if (schemaSource === this.protocolV2SchemaSource) return;
|
|
522
522
|
|
|
523
523
|
const hadProtocolV2Schema = this.protocolV2SchemaSource !== undefined;
|
|
524
|
-
this.messagesV2 =
|
|
524
|
+
this.messagesV2 = parseTransportConfigure(signedData);
|
|
525
525
|
this.protocolV2SchemaSource = schemaSource;
|
|
526
526
|
if (!hadProtocolV2Schema) return;
|
|
527
527
|
|
|
@@ -1828,8 +1828,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
1828
1828
|
}
|
|
1829
1829
|
}
|
|
1830
1830
|
decoded.reset();
|
|
1831
|
-
|
|
1832
|
-
return Buffer.from(result as unknown as ArrayBuffer).toString('hex');
|
|
1831
|
+
return decoded.toString('hex');
|
|
1833
1832
|
}
|
|
1834
1833
|
|
|
1835
1834
|
/**
|