@onekeyfe/hd-transport-web-device 1.2.2-alpha.100 → 1.2.2-alpha.101
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 +150 -11
- package/__tests__/webusb-protocol-cache.test.ts +32 -0
- package/__tests__/webusb-protocol-v2-timeout.test.ts +8 -1
- package/dist/electron-ble-transport.d.ts +2 -0
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +83 -28
- package/dist/webusb.d.ts +1 -0
- package/dist/webusb.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/electron-ble-transport.ts +72 -18
- package/src/webusb.ts +37 -5
|
@@ -73,8 +73,26 @@ const protocolV2Schema = {
|
|
|
73
73
|
},
|
|
74
74
|
},
|
|
75
75
|
},
|
|
76
|
+
Failure: {
|
|
77
|
+
fields: {
|
|
78
|
+
code: {
|
|
79
|
+
type: 'FailureType',
|
|
80
|
+
id: 1,
|
|
81
|
+
},
|
|
82
|
+
message: {
|
|
83
|
+
type: 'string',
|
|
84
|
+
id: 2,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
FailureType: {
|
|
89
|
+
values: {
|
|
90
|
+
Failure_ProcessError: 5,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
76
93
|
MessageType: {
|
|
77
94
|
values: {
|
|
95
|
+
MessageType_Failure: 3,
|
|
78
96
|
MessageType_ProtocolInfoRequest: 60200,
|
|
79
97
|
MessageType_ProtocolInfo: 60201,
|
|
80
98
|
MessageType_Ping: 60206,
|
|
@@ -327,7 +345,7 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
327
345
|
}
|
|
328
346
|
});
|
|
329
347
|
|
|
330
|
-
test('reconnects Protocol V1
|
|
348
|
+
test('reconnects a declared Protocol V1 device without probing again', async () => {
|
|
331
349
|
const device = { id: 'classic-id', name: 'OneKey Classic' };
|
|
332
350
|
const nobleBle = createNobleBle(device);
|
|
333
351
|
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
@@ -360,7 +378,10 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
360
378
|
uuid: device.id,
|
|
361
379
|
})
|
|
362
380
|
);
|
|
363
|
-
|
|
381
|
+
// The first acquire probes because the protocol is unknown; the second
|
|
382
|
+
// declares V1 and must send nothing at all, leaving the first frame on
|
|
383
|
+
// the link to Core — which is what carries the wallet session.
|
|
384
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
364
385
|
expect(nobleBle.write.mock.calls.every(([, hex]) => /^3f23230037/.test(hex))).toBe(true);
|
|
365
386
|
expect(protocolV2Writer).not.toHaveBeenCalled();
|
|
366
387
|
} finally {
|
|
@@ -373,19 +394,13 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
373
394
|
const nobleBle = createNobleBle(device);
|
|
374
395
|
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
375
396
|
const v1ResponseHex = '3f23230002000000040a026f6b';
|
|
376
|
-
let writeCount = 0;
|
|
377
|
-
|
|
378
397
|
nobleBle.onNotification.mockImplementation(handler => {
|
|
379
398
|
notificationHandler = handler;
|
|
380
399
|
return jest.fn();
|
|
381
400
|
});
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
setTimeout(() => notificationHandler?.(device.id, v1ResponseHex), 0);
|
|
386
|
-
}
|
|
387
|
-
return Promise.resolve();
|
|
388
|
-
});
|
|
401
|
+
// A declared V1 acquire writes nothing, so every write here belongs to the
|
|
402
|
+
// call under test and none of them is answered.
|
|
403
|
+
nobleBle.write.mockImplementation(() => Promise.resolve());
|
|
389
404
|
const bleTransport = configureTransport(nobleBle);
|
|
390
405
|
|
|
391
406
|
await bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V1' });
|
|
@@ -464,6 +479,41 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
464
479
|
expect(transport.getProtocolType(device.id)).toBeUndefined();
|
|
465
480
|
});
|
|
466
481
|
|
|
482
|
+
test('surfaces Protocol V2 link disabled while the initial V1 probe is active', async () => {
|
|
483
|
+
const device = { id: 'usb-priority-pro2-id', name: 'OneKey Pro 2' };
|
|
484
|
+
const nobleBle = createNobleBle(device);
|
|
485
|
+
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
486
|
+
|
|
487
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
488
|
+
notificationHandler = handler;
|
|
489
|
+
return jest.fn();
|
|
490
|
+
});
|
|
491
|
+
nobleBle.write.mockImplementation(() => {
|
|
492
|
+
const response = ProtocolV2.encodeFrame(
|
|
493
|
+
schemas,
|
|
494
|
+
'Failure',
|
|
495
|
+
{ code: 5, message: 'link disabled' },
|
|
496
|
+
{ router: PROTOCOL_V2_CHANNEL_BLE_UART }
|
|
497
|
+
);
|
|
498
|
+
const splitAt = 4;
|
|
499
|
+
setTimeout(() => {
|
|
500
|
+
notificationHandler?.(device.id, bytesToHex(response.subarray(0, splitAt)));
|
|
501
|
+
notificationHandler?.(device.id, bytesToHex(response.subarray(splitAt)));
|
|
502
|
+
}, 0);
|
|
503
|
+
return Promise.resolve();
|
|
504
|
+
});
|
|
505
|
+
const transport = configureTransport(nobleBle);
|
|
506
|
+
|
|
507
|
+
await expect(transport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
508
|
+
name: 'ProtocolV2LinkDisabledError',
|
|
509
|
+
failureCode: 'Failure_ProcessError',
|
|
510
|
+
firmwareMessage: 'link disabled',
|
|
511
|
+
});
|
|
512
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
513
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
514
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
515
|
+
});
|
|
516
|
+
|
|
467
517
|
test('keeps a first expected Protocol V2 probe miss retryable', async () => {
|
|
468
518
|
const device = { id: 'first-v2-id', name: 'OneKey Pro 2' };
|
|
469
519
|
const nobleBle = createNobleBle(device);
|
|
@@ -500,6 +550,95 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
500
550
|
expect(transport.getProtocolType(device.id)).toBeUndefined();
|
|
501
551
|
});
|
|
502
552
|
|
|
553
|
+
test('fails Protocol V2 acquire immediately when subscribe reports insufficient encryption', async () => {
|
|
554
|
+
const device = { id: 'stale-bond-pro2-id', name: 'OneKey Pro 2' };
|
|
555
|
+
const nobleBle = createNobleBle(device);
|
|
556
|
+
nobleBle.subscribe.mockRejectedValue({
|
|
557
|
+
name: 'HardwareError',
|
|
558
|
+
message: 'Bluetooth pairing information is no longer valid',
|
|
559
|
+
errorCode: HardwareErrorCode.BleBondInvalid,
|
|
560
|
+
params: { nativeErrorMessage: 'Encryption is insufficient' },
|
|
561
|
+
});
|
|
562
|
+
const transport = configureTransport(nobleBle);
|
|
563
|
+
const probe = jest.spyOn(transport as any, 'probeProtocolV2');
|
|
564
|
+
|
|
565
|
+
await expect(
|
|
566
|
+
transport.acquire({ uuid: device.id, expectedProtocol: 'V2' })
|
|
567
|
+
).rejects.toMatchObject({
|
|
568
|
+
errorCode: HardwareErrorCode.BleBondInvalid,
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
expect(probe).not.toHaveBeenCalled();
|
|
572
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
573
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
test('rehydrates a structured stale-bond error before the protocol is known', async () => {
|
|
577
|
+
const device = { id: 'reset-unknown-protocol-id', name: 'OneKey Pro 2' };
|
|
578
|
+
const nobleBle = createNobleBle(device);
|
|
579
|
+
nobleBle.connect.mockRejectedValue({
|
|
580
|
+
name: 'HardwareError',
|
|
581
|
+
message: 'Bluetooth pairing information is no longer valid',
|
|
582
|
+
errorCode: HardwareErrorCode.BleBondInvalid,
|
|
583
|
+
params: { nativeErrorMessage: 'CBErrorDomain:14 native message' },
|
|
584
|
+
});
|
|
585
|
+
const transport = configureTransport(nobleBle);
|
|
586
|
+
|
|
587
|
+
await expect(transport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
588
|
+
name: 'HardwareError',
|
|
589
|
+
errorCode: HardwareErrorCode.BleBondInvalid,
|
|
590
|
+
params: { nativeErrorMessage: 'CBErrorDomain:14 native message' },
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
test('does not infer hardware errors from native error text in the renderer', async () => {
|
|
595
|
+
const device = { id: 'reset-pro2-localized-id', name: 'OneKey Pro 2' };
|
|
596
|
+
const nobleBle = createNobleBle(device);
|
|
597
|
+
nobleBle.connect.mockRejectedValue(new Error('CBATTErrorDomain:14 localized native message'));
|
|
598
|
+
const transport = configureTransport(nobleBle);
|
|
599
|
+
|
|
600
|
+
try {
|
|
601
|
+
await transport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
602
|
+
throw new Error('Expected acquire to fail');
|
|
603
|
+
} catch (error) {
|
|
604
|
+
expect(error).toBeInstanceOf(Error);
|
|
605
|
+
expect((error as Error).message).toBe('CBATTErrorDomain:14 localized native message');
|
|
606
|
+
expect((error as { errorCode?: unknown }).errorCode).toBeUndefined();
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
test('does not classify a generic macOS connection failure as a stale bond', async () => {
|
|
611
|
+
const device = { id: 'offline-pro2-macos-id', name: 'OneKey Pro 2' };
|
|
612
|
+
const nobleBle = createNobleBle(device);
|
|
613
|
+
nobleBle.connect.mockRejectedValue(new Error('connection failed'));
|
|
614
|
+
const transport = configureTransport(nobleBle);
|
|
615
|
+
|
|
616
|
+
try {
|
|
617
|
+
await transport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
618
|
+
throw new Error('Expected acquire to fail');
|
|
619
|
+
} catch (error) {
|
|
620
|
+
expect(error).toBeInstanceOf(Error);
|
|
621
|
+
expect((error as Error).message).toBe('connection failed');
|
|
622
|
+
expect((error as { errorCode?: unknown }).errorCode).toBeUndefined();
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
test('keeps stale-bond subscribe mapping out of Protocol V1 acquire', async () => {
|
|
627
|
+
const device = { id: 'classic-v1-id', name: 'OneKey Classic' };
|
|
628
|
+
const nobleBle = createNobleBle(device);
|
|
629
|
+
nobleBle.subscribe.mockRejectedValue(new Error('Encryption is insufficient'));
|
|
630
|
+
const transport = configureTransport(nobleBle);
|
|
631
|
+
|
|
632
|
+
try {
|
|
633
|
+
await transport.acquire({ uuid: device.id, expectedProtocol: 'V1' });
|
|
634
|
+
throw new Error('Expected Protocol V1 acquire to fail');
|
|
635
|
+
} catch (error) {
|
|
636
|
+
expect(error).toBeInstanceOf(Error);
|
|
637
|
+
expect((error as Error).message).toBe('Encryption is insufficient');
|
|
638
|
+
expect((error as { errorCode?: unknown }).errorCode).toBeUndefined();
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
|
|
503
642
|
test('reports a stale bond when a previously confirmed Protocol V2 device stops responding', async () => {
|
|
504
643
|
const device = { id: 'reset-pro2-id', name: 'OneKey Pro 2' };
|
|
505
644
|
const nobleBle = createNobleBle(device);
|
|
@@ -69,6 +69,38 @@ describe('WebUsbTransport protocol probe cache', () => {
|
|
|
69
69
|
expect(webusb.detectProtocol).toHaveBeenCalledWith(path, 'V1', undefined);
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
+
test('acquire reuses a previously confirmed protocol during explicit no-probe recovery', async () => {
|
|
73
|
+
const webusb = buildAcquirableTransport();
|
|
74
|
+
const path = 'pro-webusb';
|
|
75
|
+
webusb.deviceProtocol.set(path, 'V1');
|
|
76
|
+
webusb.confirmedDeviceProtocols.set(path, 'V2');
|
|
77
|
+
webusb.markProtocolStale(path);
|
|
78
|
+
webusb.detectProtocol = jest.fn();
|
|
79
|
+
|
|
80
|
+
await expect(
|
|
81
|
+
webusb.acquire({ path, expectedProtocol: 'V2', skipProtocolProbe: true })
|
|
82
|
+
).resolves.toBe(path);
|
|
83
|
+
|
|
84
|
+
expect(webusb.detectProtocol).not.toHaveBeenCalled();
|
|
85
|
+
expect(webusb.deviceProtocol.get(path)).toBe('V2');
|
|
86
|
+
expect(webusb.staleProtocolPaths.has(path)).toBe(false);
|
|
87
|
+
expect(webusb.acquiredPaths.has(path)).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('acquire rejects no-probe recovery without a previously confirmed protocol', async () => {
|
|
91
|
+
const webusb = buildAcquirableTransport();
|
|
92
|
+
const path = 'pro-webusb';
|
|
93
|
+
webusb.detectProtocol = jest.fn();
|
|
94
|
+
|
|
95
|
+
await expect(
|
|
96
|
+
webusb.acquire({ path, expectedProtocol: 'V2', skipProtocolProbe: true })
|
|
97
|
+
).rejects.toThrow('previously confirmed protocol');
|
|
98
|
+
|
|
99
|
+
expect(webusb.detectProtocol).not.toHaveBeenCalled();
|
|
100
|
+
expect(webusb.deviceProtocol.has(path)).toBe(false);
|
|
101
|
+
expect(webusb.acquiredPaths.has(path)).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
72
104
|
test('forced protocol detection bypasses a valid cache and probes on the wire', async () => {
|
|
73
105
|
const webusb = buildAcquirableTransport();
|
|
74
106
|
const path = 'pro-webusb';
|
|
@@ -387,6 +387,13 @@ describe('WebUsbTransport Protocol V2 timeout recovery', () => {
|
|
|
387
387
|
webusb.callProtocolV2(path, 'Ping', { message: 'short' }, { timeoutMs: 25 }),
|
|
388
388
|
]);
|
|
389
389
|
|
|
390
|
-
|
|
390
|
+
// Each call keeps its own budget instead of inheriting the other's. The
|
|
391
|
+
// queued one is a deadline, so it arrives with whatever the first call left
|
|
392
|
+
// of its 25ms — asserting the exact remainder makes this fail on a loaded
|
|
393
|
+
// machine, which is timing, not behaviour.
|
|
394
|
+
expect(readTimeouts).toHaveLength(2);
|
|
395
|
+
expect(readTimeouts[0]).toBe(1_000);
|
|
396
|
+
expect(readTimeouts[1]).toBeGreaterThan(0);
|
|
397
|
+
expect(readTimeouts[1]).toBeLessThanOrEqual(25);
|
|
391
398
|
});
|
|
392
399
|
});
|
|
@@ -41,6 +41,7 @@ export default class ElectronBleTransport {
|
|
|
41
41
|
private hostDisconnectCleanup?;
|
|
42
42
|
private notificationTokens;
|
|
43
43
|
private nextNotificationToken;
|
|
44
|
+
private normalizeBluetoothError;
|
|
44
45
|
private handleBluetoothError;
|
|
45
46
|
private cleanupDeviceState;
|
|
46
47
|
init(logger: any, emitter?: EventEmitter): void;
|
|
@@ -78,6 +79,7 @@ export default class ElectronBleTransport {
|
|
|
78
79
|
private createMtuSubscription;
|
|
79
80
|
private writeProtocolV2Frame;
|
|
80
81
|
private handleNotification;
|
|
82
|
+
private readProtocolV2LinkDisabledFailure;
|
|
81
83
|
private handleProtocolV2Notification;
|
|
82
84
|
private getProtocolV2FrameQueue;
|
|
83
85
|
private resolveProtocolV2Frame;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";AA0BA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EAEZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AAIvC,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,UAAU,CAAC,EAAE,UAAU,CAAC;KACzB;CACF;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO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E,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
|
|
|
19
19
|
private protocolV2SchemaSource;
|
|
20
20
|
/** Per-path protocol type detected by active wire-level probe. */
|
|
21
21
|
private deviceProtocol;
|
|
22
|
+
/** Protocols previously confirmed by an active response for this transport instance. */
|
|
23
|
+
private confirmedDeviceProtocols;
|
|
22
24
|
private deviceProtocolHints;
|
|
23
25
|
/**
|
|
24
26
|
* Paths whose cached protocol must be re-probed on the next acquire (a USB
|
|
@@ -240,6 +242,7 @@ declare class ElectronBleTransport {
|
|
|
240
242
|
private hostDisconnectCleanup?;
|
|
241
243
|
private notificationTokens;
|
|
242
244
|
private nextNotificationToken;
|
|
245
|
+
private normalizeBluetoothError;
|
|
243
246
|
private handleBluetoothError;
|
|
244
247
|
private cleanupDeviceState;
|
|
245
248
|
init(logger: any, emitter?: EventEmitter): void;
|
|
@@ -282,6 +285,7 @@ declare class ElectronBleTransport {
|
|
|
282
285
|
private createMtuSubscription;
|
|
283
286
|
private writeProtocolV2Frame;
|
|
284
287
|
private handleNotification;
|
|
288
|
+
private readProtocolV2LinkDisabledFailure;
|
|
285
289
|
private handleProtocolV2Notification;
|
|
286
290
|
private getProtocolV2FrameQueue;
|
|
287
291
|
private resolveProtocolV2Frame;
|
package/dist/index.js
CHANGED
|
@@ -79,6 +79,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
79
79
|
logPrefix: 'ProtocolV2 WebUSB',
|
|
80
80
|
});
|
|
81
81
|
this.deviceProtocol = new Map();
|
|
82
|
+
this.confirmedDeviceProtocols = new Map();
|
|
82
83
|
this.deviceProtocolHints = new Map();
|
|
83
84
|
this.staleProtocolPaths = new Set();
|
|
84
85
|
this.acquiredPaths = new Set();
|
|
@@ -182,7 +183,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
182
183
|
});
|
|
183
184
|
}
|
|
184
185
|
acquire(input) {
|
|
185
|
-
var _a, _b, _c, _d, _e, _f;
|
|
186
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
186
187
|
return __awaiter(this, void 0, void 0, function* () {
|
|
187
188
|
if (!input.path)
|
|
188
189
|
return;
|
|
@@ -190,35 +191,53 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
190
191
|
yield this.rotateProtocolV2UsbGeneration(input.path, 'WebUSB transport acquired');
|
|
191
192
|
yield this.closeOpenDevice(input.path);
|
|
192
193
|
yield this.connect((_a = input.path) !== null && _a !== void 0 ? _a : '', true);
|
|
193
|
-
if (input.
|
|
194
|
+
if (input.skipProtocolProbe) {
|
|
195
|
+
if (!input.expectedProtocol) {
|
|
196
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'skipProtocolProbe requires an expected protocol');
|
|
197
|
+
}
|
|
198
|
+
if (this.confirmedDeviceProtocols.get(input.path) !== input.expectedProtocol) {
|
|
199
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'skipProtocolProbe requires a previously confirmed protocol for this WebUSB endpoint');
|
|
200
|
+
}
|
|
201
|
+
this.staleProtocolPaths.delete(input.path);
|
|
202
|
+
this.deviceProtocol.set(input.path, input.expectedProtocol);
|
|
203
|
+
const currentDevice = (_b = this.deviceList.find(d => d.path === input.path)) === null || _b === void 0 ? void 0 : _b.device;
|
|
204
|
+
if (currentDevice) {
|
|
205
|
+
this.probedDeviceObjects.set(input.path, currentDevice);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (input.forceProtocolDetection) {
|
|
194
209
|
this.staleProtocolPaths.delete(input.path);
|
|
195
210
|
this.deviceProtocol.delete(input.path);
|
|
196
211
|
this.deviceProtocolHints.delete(input.path);
|
|
197
212
|
}
|
|
198
|
-
if (this.staleProtocolPaths.has(input.path)) {
|
|
213
|
+
if (!input.skipProtocolProbe && this.staleProtocolPaths.has(input.path)) {
|
|
199
214
|
this.staleProtocolPaths.delete(input.path);
|
|
200
215
|
this.deviceProtocol.delete(input.path);
|
|
201
216
|
}
|
|
202
|
-
if (this.deviceProtocol.has(input.path)) {
|
|
203
|
-
const currentDevice = (
|
|
217
|
+
if (!input.skipProtocolProbe && this.deviceProtocol.has(input.path)) {
|
|
218
|
+
const currentDevice = (_c = this.deviceList.find(d => d.path === input.path)) === null || _c === void 0 ? void 0 : _c.device;
|
|
204
219
|
if (!currentDevice || currentDevice !== this.probedDeviceObjects.get(input.path)) {
|
|
205
220
|
this.deviceProtocol.delete(input.path);
|
|
206
221
|
}
|
|
207
222
|
}
|
|
208
223
|
const cachedProtocol = this.deviceProtocol.get(input.path);
|
|
209
|
-
if (
|
|
224
|
+
if (!input.skipProtocolProbe &&
|
|
225
|
+
cachedProtocol &&
|
|
226
|
+
input.expectedProtocol &&
|
|
227
|
+
cachedProtocol !== input.expectedProtocol) {
|
|
210
228
|
this.deviceProtocol.delete(input.path);
|
|
211
229
|
}
|
|
212
230
|
if (!this.deviceProtocol.has(input.path)) {
|
|
213
|
-
const usbDevice = (
|
|
231
|
+
const usbDevice = (_d = this.deviceList.find(device => device.path === input.path)) === null || _d === void 0 ? void 0 : _d.device;
|
|
214
232
|
const protocolHint = input.expectedProtocol
|
|
215
233
|
? undefined
|
|
216
|
-
: (
|
|
234
|
+
: (_f = (_e = input.protocolHint) !== null && _e !== void 0 ? _e : this.deviceProtocolHints.get(input.path)) !== null && _f !== void 0 ? _f : hdShared.inferProtocolHintFromUsbId(usbDevice === null || usbDevice === void 0 ? void 0 : usbDevice.vendorId, usbDevice === null || usbDevice === void 0 ? void 0 : usbDevice.productId);
|
|
217
235
|
if (protocolHint) {
|
|
218
236
|
this.deviceProtocolHints.set(input.path, protocolHint);
|
|
219
237
|
}
|
|
220
|
-
yield this.detectProtocol(input.path, input.expectedProtocol, protocolHint);
|
|
221
|
-
|
|
238
|
+
const detectedProtocol = yield this.detectProtocol(input.path, input.expectedProtocol, protocolHint);
|
|
239
|
+
this.confirmedDeviceProtocols.set(input.path, detectedProtocol);
|
|
240
|
+
const probedDevice = (_g = this.deviceList.find(d => d.path === input.path)) === null || _g === void 0 ? void 0 : _g.device;
|
|
222
241
|
if (probedDevice) {
|
|
223
242
|
this.probedDeviceObjects.set(input.path, probedDevice);
|
|
224
243
|
}
|
|
@@ -852,17 +871,20 @@ class ElectronBleTransport {
|
|
|
852
871
|
this.notificationTokens = new Map();
|
|
853
872
|
this.nextNotificationToken = 1;
|
|
854
873
|
}
|
|
855
|
-
|
|
874
|
+
normalizeBluetoothError(error) {
|
|
856
875
|
if (error && typeof error === 'object') {
|
|
876
|
+
if (typeof error.errorCode === 'number') {
|
|
877
|
+
return hdShared.ERRORS.TypedError(error.errorCode, typeof error.message === 'string' ? error.message : undefined, error.params);
|
|
878
|
+
}
|
|
857
879
|
if ('code' in error) {
|
|
858
880
|
if (error.code === hdShared.HardwareErrorCode.BlePoweredOff) {
|
|
859
|
-
|
|
881
|
+
return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
|
|
860
882
|
}
|
|
861
883
|
if (error.code === hdShared.HardwareErrorCode.BleUnsupported) {
|
|
862
|
-
|
|
884
|
+
return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
|
|
863
885
|
}
|
|
864
886
|
if (error.code === hdShared.HardwareErrorCode.BlePermissionError) {
|
|
865
|
-
|
|
887
|
+
return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
|
|
866
888
|
}
|
|
867
889
|
}
|
|
868
890
|
const errorMessage = error.message || String(error);
|
|
@@ -870,16 +892,19 @@ class ElectronBleTransport {
|
|
|
870
892
|
const unsupportedMessage = hdShared.HardwareErrorCodeMessage[hdShared.HardwareErrorCode.BleUnsupported];
|
|
871
893
|
const permissionMessage = hdShared.HardwareErrorCodeMessage[hdShared.HardwareErrorCode.BlePermissionError];
|
|
872
894
|
if (errorMessage.includes(poweredOffMessage) || errorMessage.includes('poweredOff')) {
|
|
873
|
-
|
|
895
|
+
return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
|
|
874
896
|
}
|
|
875
897
|
if (errorMessage.includes(unsupportedMessage) || errorMessage.includes('unsupported')) {
|
|
876
|
-
|
|
898
|
+
return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
|
|
877
899
|
}
|
|
878
900
|
if (errorMessage.includes(permissionMessage) || errorMessage.includes('unauthorized')) {
|
|
879
|
-
|
|
901
|
+
return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
|
|
880
902
|
}
|
|
881
903
|
}
|
|
882
|
-
|
|
904
|
+
return error;
|
|
905
|
+
}
|
|
906
|
+
handleBluetoothError(error) {
|
|
907
|
+
throw this.normalizeBluetoothError(error);
|
|
883
908
|
}
|
|
884
909
|
cleanupDeviceState(deviceId) {
|
|
885
910
|
this.protocolV2Links
|
|
@@ -1022,7 +1047,12 @@ class ElectronBleTransport {
|
|
|
1022
1047
|
}
|
|
1023
1048
|
this.v1Buffers.set(uuid, { buffer: [], bufferLength: 0 });
|
|
1024
1049
|
this.v2Assemblers.set(uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
|
|
1025
|
-
|
|
1050
|
+
try {
|
|
1051
|
+
yield window.desktopApi.nobleBle.subscribe(uuid);
|
|
1052
|
+
}
|
|
1053
|
+
catch (error) {
|
|
1054
|
+
this.handleBluetoothError(error);
|
|
1055
|
+
}
|
|
1026
1056
|
yield this.refreshBlePacketCapacity(uuid);
|
|
1027
1057
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
1028
1058
|
this.notificationCleanups.set(uuid, cleanup);
|
|
@@ -1048,7 +1078,7 @@ class ElectronBleTransport {
|
|
|
1048
1078
|
(_g = this.Log) === null || _g === void 0 ? void 0 : _g.debug('[Electron BLE] acquire cleanup failed:', cleanupError);
|
|
1049
1079
|
}
|
|
1050
1080
|
this.cleanupDeviceState(uuid);
|
|
1051
|
-
|
|
1081
|
+
this.handleBluetoothError(error);
|
|
1052
1082
|
}
|
|
1053
1083
|
});
|
|
1054
1084
|
}
|
|
@@ -1128,12 +1158,9 @@ class ElectronBleTransport {
|
|
|
1128
1158
|
var _a, _b, _c;
|
|
1129
1159
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1130
1160
|
if (expectedProtocol === 'V1') {
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
return 'V1';
|
|
1135
|
-
}
|
|
1136
|
-
throw this.createProtocolMismatchError(expectedProtocol, uuid);
|
|
1161
|
+
this.deviceProtocol.set(uuid, 'V1');
|
|
1162
|
+
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> V1 (expected, no probe)`);
|
|
1163
|
+
return 'V1';
|
|
1137
1164
|
}
|
|
1138
1165
|
if (expectedProtocol === 'V2') {
|
|
1139
1166
|
if (yield this.probeProtocolV2(uuid)) {
|
|
@@ -1211,6 +1238,9 @@ class ElectronBleTransport {
|
|
|
1211
1238
|
catch (error) {
|
|
1212
1239
|
this.clearProbeProtocol(uuid, 'V1');
|
|
1213
1240
|
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
1241
|
+
if (transport.isProtocolV2LinkDisabledError(error)) {
|
|
1242
|
+
throw error;
|
|
1243
|
+
}
|
|
1214
1244
|
return false;
|
|
1215
1245
|
}
|
|
1216
1246
|
});
|
|
@@ -1233,6 +1263,7 @@ class ElectronBleTransport {
|
|
|
1233
1263
|
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
1234
1264
|
this.resetProtocolV2Frames(uuid);
|
|
1235
1265
|
},
|
|
1266
|
+
shouldRethrow: error => hdShared.isBleStaleBondHardwareError(error) || transport.isProtocolV2LinkDisabledError(error),
|
|
1236
1267
|
});
|
|
1237
1268
|
if (!detected) {
|
|
1238
1269
|
this.clearProbeProtocol(uuid, 'V2');
|
|
@@ -1247,7 +1278,12 @@ class ElectronBleTransport {
|
|
|
1247
1278
|
if (!nobleBle) {
|
|
1248
1279
|
throw new Error('Noble BLE API not available');
|
|
1249
1280
|
}
|
|
1250
|
-
|
|
1281
|
+
try {
|
|
1282
|
+
yield nobleBle.write(uuid, hexData, { pacingDelayMs: 0 });
|
|
1283
|
+
}
|
|
1284
|
+
catch (error) {
|
|
1285
|
+
this.handleBluetoothError(error);
|
|
1286
|
+
}
|
|
1251
1287
|
});
|
|
1252
1288
|
}
|
|
1253
1289
|
refreshBlePacketCapacity(uuid) {
|
|
@@ -1316,8 +1352,27 @@ class ElectronBleTransport {
|
|
|
1316
1352
|
this.handleProtocolV2Notification(deviceId, hexData);
|
|
1317
1353
|
return;
|
|
1318
1354
|
}
|
|
1355
|
+
const linkDisabledError = this.readProtocolV2LinkDisabledFailure(deviceId, hexData);
|
|
1356
|
+
if (linkDisabledError) {
|
|
1357
|
+
if (this.runPromise && this.runPromiseDeviceId === deviceId) {
|
|
1358
|
+
this.runPromise.reject(linkDisabledError);
|
|
1359
|
+
}
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1319
1362
|
this.handleProtocolV1Notification(deviceId, hexData);
|
|
1320
1363
|
}
|
|
1364
|
+
readProtocolV2LinkDisabledFailure(deviceId, hexData) {
|
|
1365
|
+
if (!this._messages || !this._messagesV2)
|
|
1366
|
+
return undefined;
|
|
1367
|
+
const assembler = this.v2Assemblers.get(deviceId);
|
|
1368
|
+
if (!assembler)
|
|
1369
|
+
return undefined;
|
|
1370
|
+
return transport.detectProtocolV2LinkDisabledError({
|
|
1371
|
+
schemas: { protocolV1: this._messages, protocolV2: this._messagesV2 },
|
|
1372
|
+
assembler,
|
|
1373
|
+
bytes: transport.hexToBytes(hexData),
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1321
1376
|
handleProtocolV2Notification(deviceId, hexData) {
|
|
1322
1377
|
var _a;
|
|
1323
1378
|
try {
|
|
@@ -1490,7 +1545,7 @@ class ElectronBleTransport {
|
|
|
1490
1545
|
yield this.releaseNative(uuid);
|
|
1491
1546
|
}
|
|
1492
1547
|
}
|
|
1493
|
-
throw e;
|
|
1548
|
+
throw this.normalizeBluetoothError(e);
|
|
1494
1549
|
}
|
|
1495
1550
|
finally {
|
|
1496
1551
|
if (timeout)
|
package/dist/webusb.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
13
13
|
messagesV2: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
14
14
|
private protocolV2SchemaSource;
|
|
15
15
|
private deviceProtocol;
|
|
16
|
+
private confirmedDeviceProtocols;
|
|
16
17
|
private deviceProtocolHints;
|
|
17
18
|
private staleProtocolPaths;
|
|
18
19
|
private acquiredPaths;
|
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;AAYhC,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;AAuBhC,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAuCD,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,0BAA0B,CAAC,MAAM,CAAC;IAC7E,QAAQ,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,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AgBvB,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YA2BlB,cAAc;YAkCd,cAAc;IAYtB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;IAgD5C,OAAO,CAAC,IAAI,EAAE,MAAM;IAY1B,SAAS,CAAC,uBAAuB,IAAI,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.2-alpha.
|
|
3
|
+
"version": "1.2.2-alpha.101",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"lint:fix": "eslint . --fix"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.2-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.2-alpha.
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.2-alpha.101",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.2-alpha.101"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@onekeyfe/hd-transport-electron": "1.2.2-alpha.
|
|
28
|
+
"@onekeyfe/hd-transport-electron": "1.2.2-alpha.101",
|
|
29
29
|
"@types/w3c-web-usb": "^1.0.6",
|
|
30
30
|
"@types/web-bluetooth": "^0.0.17"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "b9a5ca0c73e5e2cc979c91c007ace476e442718c"
|
|
33
33
|
}
|
|
@@ -6,7 +6,9 @@ import transport, {
|
|
|
6
6
|
ProtocolV2LinkManager,
|
|
7
7
|
TRANSPORT_EVENT,
|
|
8
8
|
bytesToHex,
|
|
9
|
+
detectProtocolV2LinkDisabledError,
|
|
9
10
|
hexToBytes,
|
|
11
|
+
isProtocolV2LinkDisabledError,
|
|
10
12
|
probeProtocolV2 as probeProtocolV2Helper,
|
|
11
13
|
writeProtocolV2BleFrame,
|
|
12
14
|
} from '@onekeyfe/hd-transport';
|
|
@@ -16,6 +18,7 @@ import {
|
|
|
16
18
|
HardwareErrorCode,
|
|
17
19
|
HardwareErrorCodeMessage,
|
|
18
20
|
createDeferred,
|
|
21
|
+
isBleStaleBondHardwareError,
|
|
19
22
|
isHeaderChunk,
|
|
20
23
|
} from '@onekeyfe/hd-shared';
|
|
21
24
|
|
|
@@ -159,17 +162,24 @@ export default class ElectronBleTransport {
|
|
|
159
162
|
|
|
160
163
|
private nextNotificationToken = 1;
|
|
161
164
|
|
|
162
|
-
private
|
|
165
|
+
private normalizeBluetoothError(error: any): any {
|
|
163
166
|
if (error && typeof error === 'object') {
|
|
167
|
+
if (typeof error.errorCode === 'number') {
|
|
168
|
+
return ERRORS.TypedError(
|
|
169
|
+
error.errorCode,
|
|
170
|
+
typeof error.message === 'string' ? error.message : undefined,
|
|
171
|
+
error.params
|
|
172
|
+
);
|
|
173
|
+
}
|
|
164
174
|
if ('code' in error) {
|
|
165
175
|
if (error.code === HardwareErrorCode.BlePoweredOff) {
|
|
166
|
-
|
|
176
|
+
return ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
|
|
167
177
|
}
|
|
168
178
|
if (error.code === HardwareErrorCode.BleUnsupported) {
|
|
169
|
-
|
|
179
|
+
return ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
|
|
170
180
|
}
|
|
171
181
|
if (error.code === HardwareErrorCode.BlePermissionError) {
|
|
172
|
-
|
|
182
|
+
return ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
|
|
173
183
|
}
|
|
174
184
|
}
|
|
175
185
|
const errorMessage = error.message || String(error);
|
|
@@ -178,16 +188,20 @@ export default class ElectronBleTransport {
|
|
|
178
188
|
const permissionMessage = HardwareErrorCodeMessage[HardwareErrorCode.BlePermissionError];
|
|
179
189
|
|
|
180
190
|
if (errorMessage.includes(poweredOffMessage) || errorMessage.includes('poweredOff')) {
|
|
181
|
-
|
|
191
|
+
return ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
|
|
182
192
|
}
|
|
183
193
|
if (errorMessage.includes(unsupportedMessage) || errorMessage.includes('unsupported')) {
|
|
184
|
-
|
|
194
|
+
return ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
|
|
185
195
|
}
|
|
186
196
|
if (errorMessage.includes(permissionMessage) || errorMessage.includes('unauthorized')) {
|
|
187
|
-
|
|
197
|
+
return ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
|
|
188
198
|
}
|
|
189
199
|
}
|
|
190
|
-
|
|
200
|
+
return error;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private handleBluetoothError(error: any): never {
|
|
204
|
+
throw this.normalizeBluetoothError(error);
|
|
191
205
|
}
|
|
192
206
|
|
|
193
207
|
private cleanupDeviceState(deviceId: string): void {
|
|
@@ -367,7 +381,11 @@ export default class ElectronBleTransport {
|
|
|
367
381
|
this.v1Buffers.set(uuid, { buffer: [], bufferLength: 0 });
|
|
368
382
|
this.v2Assemblers.set(uuid, new ProtocolV2FrameAssembler(PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
|
|
369
383
|
|
|
370
|
-
|
|
384
|
+
try {
|
|
385
|
+
await window.desktopApi.nobleBle.subscribe(uuid);
|
|
386
|
+
} catch (error) {
|
|
387
|
+
this.handleBluetoothError(error);
|
|
388
|
+
}
|
|
371
389
|
await this.refreshBlePacketCapacity(uuid);
|
|
372
390
|
|
|
373
391
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
@@ -397,7 +415,7 @@ export default class ElectronBleTransport {
|
|
|
397
415
|
this.Log?.debug('[Electron BLE] acquire cleanup failed:', cleanupError);
|
|
398
416
|
}
|
|
399
417
|
this.cleanupDeviceState(uuid);
|
|
400
|
-
|
|
418
|
+
this.handleBluetoothError(error);
|
|
401
419
|
}
|
|
402
420
|
}
|
|
403
421
|
|
|
@@ -487,13 +505,20 @@ export default class ElectronBleTransport {
|
|
|
487
505
|
expectedProtocol?: ProtocolType,
|
|
488
506
|
protocolHint?: ProtocolType
|
|
489
507
|
): Promise<ProtocolType> {
|
|
508
|
+
// A declared V1 is taken at face value, as the React Native transport
|
|
509
|
+
// already does on iOS: the caller reads the protocol off its own device
|
|
510
|
+
// record, so probing re-asks a question that is already answered and adds a
|
|
511
|
+
// round trip to every cold connect. It also fails in a way that costs the
|
|
512
|
+
// session: a device whose protocol session has stalled ignores the probe
|
|
513
|
+
// frame, and the timeout became a protocol-mismatch error that stopped Core
|
|
514
|
+
// from ever sending Initialize — the one frame such a device still answers,
|
|
515
|
+
// and how it gets revived. A declared V2 keeps probing, matching iOS, so a
|
|
516
|
+
// USB-priority "link disabled" surfaces here rather than as an unmapped
|
|
517
|
+
// error later. An undeclared protocol still goes through full detection.
|
|
490
518
|
if (expectedProtocol === 'V1') {
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
return 'V1';
|
|
495
|
-
}
|
|
496
|
-
throw this.createProtocolMismatchError(expectedProtocol, uuid);
|
|
519
|
+
this.deviceProtocol.set(uuid, 'V1');
|
|
520
|
+
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> V1 (expected, no probe)`);
|
|
521
|
+
return 'V1';
|
|
497
522
|
}
|
|
498
523
|
|
|
499
524
|
if (expectedProtocol === 'V2') {
|
|
@@ -588,6 +613,9 @@ export default class ElectronBleTransport {
|
|
|
588
613
|
} catch (error) {
|
|
589
614
|
this.clearProbeProtocol(uuid, 'V1');
|
|
590
615
|
this.Log?.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
616
|
+
if (isProtocolV2LinkDisabledError(error)) {
|
|
617
|
+
throw error;
|
|
618
|
+
}
|
|
591
619
|
return false;
|
|
592
620
|
}
|
|
593
621
|
}
|
|
@@ -608,6 +636,8 @@ export default class ElectronBleTransport {
|
|
|
608
636
|
this.v2Assemblers.get(uuid)?.reset();
|
|
609
637
|
this.resetProtocolV2Frames(uuid);
|
|
610
638
|
},
|
|
639
|
+
shouldRethrow: error =>
|
|
640
|
+
isBleStaleBondHardwareError(error) || isProtocolV2LinkDisabledError(error),
|
|
611
641
|
});
|
|
612
642
|
if (!detected) {
|
|
613
643
|
this.clearProbeProtocol(uuid, 'V2');
|
|
@@ -621,7 +651,11 @@ export default class ElectronBleTransport {
|
|
|
621
651
|
throw new Error('Noble BLE API not available');
|
|
622
652
|
}
|
|
623
653
|
|
|
624
|
-
|
|
654
|
+
try {
|
|
655
|
+
await nobleBle.write(uuid, hexData, { pacingDelayMs: 0 });
|
|
656
|
+
} catch (error) {
|
|
657
|
+
this.handleBluetoothError(error);
|
|
658
|
+
}
|
|
625
659
|
}
|
|
626
660
|
|
|
627
661
|
private async refreshBlePacketCapacity(uuid: string): Promise<void> {
|
|
@@ -695,9 +729,29 @@ export default class ElectronBleTransport {
|
|
|
695
729
|
this.handleProtocolV2Notification(deviceId, hexData);
|
|
696
730
|
return;
|
|
697
731
|
}
|
|
732
|
+
const linkDisabledError = this.readProtocolV2LinkDisabledFailure(deviceId, hexData);
|
|
733
|
+
if (linkDisabledError) {
|
|
734
|
+
if (this.runPromise && this.runPromiseDeviceId === deviceId) {
|
|
735
|
+
this.runPromise.reject(linkDisabledError);
|
|
736
|
+
}
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
698
739
|
this.handleProtocolV1Notification(deviceId, hexData);
|
|
699
740
|
}
|
|
700
741
|
|
|
742
|
+
private readProtocolV2LinkDisabledFailure(deviceId: string, hexData: string) {
|
|
743
|
+
if (!this._messages || !this._messagesV2) return undefined;
|
|
744
|
+
|
|
745
|
+
const assembler = this.v2Assemblers.get(deviceId);
|
|
746
|
+
if (!assembler) return undefined;
|
|
747
|
+
|
|
748
|
+
return detectProtocolV2LinkDisabledError({
|
|
749
|
+
schemas: { protocolV1: this._messages, protocolV2: this._messagesV2 },
|
|
750
|
+
assembler,
|
|
751
|
+
bytes: hexToBytes(hexData),
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
|
|
701
755
|
private handleProtocolV2Notification(deviceId: string, hexData: string): void {
|
|
702
756
|
try {
|
|
703
757
|
const bytes = hexToBytes(hexData);
|
|
@@ -901,7 +955,7 @@ export default class ElectronBleTransport {
|
|
|
901
955
|
await this.releaseNative(uuid);
|
|
902
956
|
}
|
|
903
957
|
}
|
|
904
|
-
throw e;
|
|
958
|
+
throw this.normalizeBluetoothError(e);
|
|
905
959
|
} finally {
|
|
906
960
|
if (timeout) clearTimeout(timeout);
|
|
907
961
|
if (this.runPromise === runPromise) {
|
package/src/webusb.ts
CHANGED
|
@@ -107,6 +107,9 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
107
107
|
/** Per-path protocol type detected by active wire-level probe. */
|
|
108
108
|
private deviceProtocol: Map<string, ProtocolType> = new Map();
|
|
109
109
|
|
|
110
|
+
/** Protocols previously confirmed by an active response for this transport instance. */
|
|
111
|
+
private confirmedDeviceProtocols: Map<string, ProtocolType> = new Map();
|
|
112
|
+
|
|
110
113
|
private deviceProtocolHints: Map<string, ProtocolType> = new Map();
|
|
111
114
|
|
|
112
115
|
/**
|
|
@@ -309,7 +312,26 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
309
312
|
await this.rotateProtocolV2UsbGeneration(input.path, 'WebUSB transport acquired');
|
|
310
313
|
await this.closeOpenDevice(input.path);
|
|
311
314
|
await this.connect(input.path ?? '', true);
|
|
312
|
-
if (input.
|
|
315
|
+
if (input.skipProtocolProbe) {
|
|
316
|
+
if (!input.expectedProtocol) {
|
|
317
|
+
throw ERRORS.TypedError(
|
|
318
|
+
HardwareErrorCode.RuntimeError,
|
|
319
|
+
'skipProtocolProbe requires an expected protocol'
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
if (this.confirmedDeviceProtocols.get(input.path) !== input.expectedProtocol) {
|
|
323
|
+
throw ERRORS.TypedError(
|
|
324
|
+
HardwareErrorCode.RuntimeError,
|
|
325
|
+
'skipProtocolProbe requires a previously confirmed protocol for this WebUSB endpoint'
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
this.staleProtocolPaths.delete(input.path);
|
|
329
|
+
this.deviceProtocol.set(input.path, input.expectedProtocol);
|
|
330
|
+
const currentDevice = this.deviceList.find(d => d.path === input.path)?.device;
|
|
331
|
+
if (currentDevice) {
|
|
332
|
+
this.probedDeviceObjects.set(input.path, currentDevice);
|
|
333
|
+
}
|
|
334
|
+
} else if (input.forceProtocolDetection) {
|
|
313
335
|
// Explicit recovery/discovery (e.g. detectDeviceConnectProtocol) must
|
|
314
336
|
// probe on the wire regardless of any cached result — the cached
|
|
315
337
|
// binding may be exactly what the caller is trying to recover from.
|
|
@@ -317,13 +339,13 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
317
339
|
this.deviceProtocol.delete(input.path);
|
|
318
340
|
this.deviceProtocolHints.delete(input.path);
|
|
319
341
|
}
|
|
320
|
-
if (this.staleProtocolPaths.has(input.path)) {
|
|
342
|
+
if (!input.skipProtocolProbe && this.staleProtocolPaths.has(input.path)) {
|
|
321
343
|
// The device disconnected (possibly rebooting into another mode) since
|
|
322
344
|
// the protocol was probed — drop the cache so it is re-probed below.
|
|
323
345
|
this.staleProtocolPaths.delete(input.path);
|
|
324
346
|
this.deviceProtocol.delete(input.path);
|
|
325
347
|
}
|
|
326
|
-
if (this.deviceProtocol.has(input.path)) {
|
|
348
|
+
if (!input.skipProtocolProbe && this.deviceProtocol.has(input.path)) {
|
|
327
349
|
const currentDevice = this.deviceList.find(d => d.path === input.path)?.device;
|
|
328
350
|
if (!currentDevice || currentDevice !== this.probedDeviceObjects.get(input.path)) {
|
|
329
351
|
// The OS re-enumerated the device since the probe (a replug/reboot we
|
|
@@ -333,7 +355,12 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
333
355
|
}
|
|
334
356
|
}
|
|
335
357
|
const cachedProtocol = this.deviceProtocol.get(input.path);
|
|
336
|
-
if (
|
|
358
|
+
if (
|
|
359
|
+
!input.skipProtocolProbe &&
|
|
360
|
+
cachedProtocol &&
|
|
361
|
+
input.expectedProtocol &&
|
|
362
|
+
cachedProtocol !== input.expectedProtocol
|
|
363
|
+
) {
|
|
337
364
|
// The caller expects a different protocol than the cached probe result;
|
|
338
365
|
// the cache is stale — drop it and re-probe on the wire below.
|
|
339
366
|
this.deviceProtocol.delete(input.path);
|
|
@@ -348,7 +375,12 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
348
375
|
if (protocolHint) {
|
|
349
376
|
this.deviceProtocolHints.set(input.path, protocolHint);
|
|
350
377
|
}
|
|
351
|
-
await this.detectProtocol(
|
|
378
|
+
const detectedProtocol = await this.detectProtocol(
|
|
379
|
+
input.path,
|
|
380
|
+
input.expectedProtocol,
|
|
381
|
+
protocolHint
|
|
382
|
+
);
|
|
383
|
+
this.confirmedDeviceProtocols.set(input.path, detectedProtocol);
|
|
352
384
|
const probedDevice = this.deviceList.find(d => d.path === input.path)?.device;
|
|
353
385
|
if (probedDevice) {
|
|
354
386
|
this.probedDeviceObjects.set(input.path, probedDevice);
|