@onekeyfe/hd-transport-web-device 1.2.11-alpha.1 → 1.2.11-alpha.3
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.
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import transport, { PROTOCOL_V2_CHANNEL_BLE_UART, bytesToHex } from '@onekeyfe/hd-transport';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
EBleDisconnectReason,
|
|
4
|
+
EOneKeyBleMessageKeys,
|
|
5
|
+
ERRORS,
|
|
6
|
+
HardwareErrorCode,
|
|
7
|
+
createDeferred,
|
|
8
|
+
} from '@onekeyfe/hd-shared';
|
|
3
9
|
import EventEmitter from 'events';
|
|
4
10
|
|
|
5
11
|
import ElectronBleTransport from '../src/electron-ble-transport';
|
|
@@ -837,6 +843,42 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
837
843
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
838
844
|
});
|
|
839
845
|
|
|
846
|
+
test('detects a V2-only device on first acquire without a protocol hint', async () => {
|
|
847
|
+
const device = { id: 'v2-only-no-hint-id', name: 'OneKey Pro 2' };
|
|
848
|
+
const nobleBle = createNobleBle(device);
|
|
849
|
+
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
850
|
+
let responseSeq = 0;
|
|
851
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
852
|
+
notificationHandler = handler;
|
|
853
|
+
return jest.fn();
|
|
854
|
+
});
|
|
855
|
+
nobleBle.write.mockImplementation(() => {
|
|
856
|
+
const writeCount = nobleBle.write.mock.calls.length;
|
|
857
|
+
if (writeCount === 1) {
|
|
858
|
+
return Promise.resolve();
|
|
859
|
+
}
|
|
860
|
+
responseSeq += 1;
|
|
861
|
+
const response = ProtocolV2.encodeFrame(
|
|
862
|
+
schemas,
|
|
863
|
+
'Success',
|
|
864
|
+
{ message: 'ok' },
|
|
865
|
+
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: responseSeq }
|
|
866
|
+
);
|
|
867
|
+
setTimeout(() => notificationHandler?.(device.id, bytesToHex(response)), 0);
|
|
868
|
+
return Promise.resolve();
|
|
869
|
+
});
|
|
870
|
+
const transport = configureTransport(nobleBle);
|
|
871
|
+
|
|
872
|
+
await expect(transport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
873
|
+
uuid: device.id,
|
|
874
|
+
protocolType: 'V2',
|
|
875
|
+
});
|
|
876
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(2);
|
|
877
|
+
expect(nobleBle.connect).toHaveBeenCalledTimes(1);
|
|
878
|
+
expect(nobleBle.subscribe).toHaveBeenCalledTimes(1);
|
|
879
|
+
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
880
|
+
});
|
|
881
|
+
|
|
840
882
|
test('keeps a first expected Protocol V2 probe miss retryable', async () => {
|
|
841
883
|
const device = { id: 'first-v2-id', name: 'OneKey Pro 2' };
|
|
842
884
|
const nobleBle = createNobleBle(device);
|
|
@@ -1030,6 +1072,221 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1030
1072
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1031
1073
|
});
|
|
1032
1074
|
|
|
1075
|
+
test.each([
|
|
1076
|
+
HardwareErrorCode.BleDeviceBondError,
|
|
1077
|
+
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1078
|
+
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1079
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1080
|
+
])('preserves pairing-recovery error %s during undeclared Protocol V1 probe', async errorCode => {
|
|
1081
|
+
const device = { id: `undeclared-v1-${errorCode}`, name: 'Unknown BLE Device' };
|
|
1082
|
+
const nobleBle = createNobleBle(device);
|
|
1083
|
+
nobleBle.write.mockRejectedValueOnce({
|
|
1084
|
+
name: 'HardwareError',
|
|
1085
|
+
message: `Pairing failure ${errorCode}`,
|
|
1086
|
+
errorCode,
|
|
1087
|
+
});
|
|
1088
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1089
|
+
const probeProtocolV2 = jest.spyOn(bleTransport as any, 'probeProtocolV2');
|
|
1090
|
+
|
|
1091
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({ errorCode });
|
|
1092
|
+
|
|
1093
|
+
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
1094
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1095
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1096
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1097
|
+
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
test.each([
|
|
1101
|
+
[
|
|
1102
|
+
'pairing_no_response',
|
|
1103
|
+
HardwareErrorCode.BleDeviceBondError,
|
|
1104
|
+
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'pairing_no_response' },
|
|
1105
|
+
],
|
|
1106
|
+
[
|
|
1107
|
+
'silence stale_bond',
|
|
1108
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1109
|
+
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'stale_bond' },
|
|
1110
|
+
],
|
|
1111
|
+
] as const)(
|
|
1112
|
+
'falls through to Protocol V2 when undeclared V1 probe hits silent Windows %s',
|
|
1113
|
+
async (_label, errorCode, params) => {
|
|
1114
|
+
const device = { id: `win-silent-${errorCode}`, name: 'Unknown BLE Device' };
|
|
1115
|
+
const nobleBle = createNobleBle(device);
|
|
1116
|
+
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
1117
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
1118
|
+
notificationHandler = handler;
|
|
1119
|
+
return jest.fn();
|
|
1120
|
+
});
|
|
1121
|
+
const probeResponse = ProtocolV2.encodeFrame(
|
|
1122
|
+
schemas,
|
|
1123
|
+
'Success',
|
|
1124
|
+
{ message: 'ok' },
|
|
1125
|
+
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: 1 }
|
|
1126
|
+
);
|
|
1127
|
+
nobleBle.write
|
|
1128
|
+
.mockRejectedValueOnce({
|
|
1129
|
+
name: 'HardwareError',
|
|
1130
|
+
message: 'Silent pairing miss',
|
|
1131
|
+
errorCode,
|
|
1132
|
+
params,
|
|
1133
|
+
})
|
|
1134
|
+
.mockImplementationOnce(() => {
|
|
1135
|
+
setTimeout(() => notificationHandler?.(device.id, bytesToHex(probeResponse)), 0);
|
|
1136
|
+
return Promise.resolve();
|
|
1137
|
+
});
|
|
1138
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1139
|
+
|
|
1140
|
+
try {
|
|
1141
|
+
await expect(bleTransport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
1142
|
+
uuid: device.id,
|
|
1143
|
+
protocolType: 'V2',
|
|
1144
|
+
});
|
|
1145
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(2);
|
|
1146
|
+
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
1147
|
+
} finally {
|
|
1148
|
+
await bleTransport.release(device.id);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
);
|
|
1152
|
+
|
|
1153
|
+
test.each([
|
|
1154
|
+
[
|
|
1155
|
+
'pairing_no_response',
|
|
1156
|
+
HardwareErrorCode.BleDeviceBondError,
|
|
1157
|
+
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'pairing_no_response' },
|
|
1158
|
+
],
|
|
1159
|
+
[
|
|
1160
|
+
'silence stale_bond',
|
|
1161
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1162
|
+
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'stale_bond' },
|
|
1163
|
+
],
|
|
1164
|
+
] as const)(
|
|
1165
|
+
'preserves silent Windows %s when both undeclared probes fail',
|
|
1166
|
+
async (_label, errorCode, params) => {
|
|
1167
|
+
const device = { id: `win-silent-both-${errorCode}`, name: 'Unknown BLE Device' };
|
|
1168
|
+
const nobleBle = createNobleBle(device);
|
|
1169
|
+
const silentError = {
|
|
1170
|
+
name: 'HardwareError',
|
|
1171
|
+
message: 'Silent pairing miss',
|
|
1172
|
+
errorCode,
|
|
1173
|
+
params,
|
|
1174
|
+
};
|
|
1175
|
+
nobleBle.write.mockRejectedValue(silentError);
|
|
1176
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1177
|
+
|
|
1178
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1179
|
+
errorCode,
|
|
1180
|
+
params,
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(2);
|
|
1184
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1185
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1186
|
+
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1187
|
+
}
|
|
1188
|
+
);
|
|
1189
|
+
|
|
1190
|
+
test.each([
|
|
1191
|
+
[
|
|
1192
|
+
HardwareErrorCode.BleDeviceBondError,
|
|
1193
|
+
{
|
|
1194
|
+
phase: 'pairing',
|
|
1195
|
+
platform: 'win32',
|
|
1196
|
+
stage: 'write',
|
|
1197
|
+
reason: 'pairing_failed',
|
|
1198
|
+
nativeErrorMessage: 'Access denied',
|
|
1199
|
+
},
|
|
1200
|
+
],
|
|
1201
|
+
[
|
|
1202
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1203
|
+
{
|
|
1204
|
+
phase: 'pairing',
|
|
1205
|
+
platform: 'win32',
|
|
1206
|
+
stage: 'write',
|
|
1207
|
+
reason: 'stale_bond',
|
|
1208
|
+
nativeErrorMessage: 'status: 3',
|
|
1209
|
+
},
|
|
1210
|
+
],
|
|
1211
|
+
[
|
|
1212
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1213
|
+
{
|
|
1214
|
+
phase: 'pairing',
|
|
1215
|
+
platform: 'win32',
|
|
1216
|
+
stage: 'subscribe',
|
|
1217
|
+
reason: 'stale_bond',
|
|
1218
|
+
},
|
|
1219
|
+
],
|
|
1220
|
+
] as const)(
|
|
1221
|
+
'keeps confirmed Windows pairing failure %s terminal during undeclared V1 probe',
|
|
1222
|
+
async (errorCode, params) => {
|
|
1223
|
+
const device = {
|
|
1224
|
+
id: `win-confirmed-${errorCode}-${params.stage}`,
|
|
1225
|
+
name: 'Unknown BLE Device',
|
|
1226
|
+
};
|
|
1227
|
+
const nobleBle = createNobleBle(device);
|
|
1228
|
+
nobleBle.write.mockRejectedValueOnce({
|
|
1229
|
+
name: 'HardwareError',
|
|
1230
|
+
message: 'Confirmed pairing failure',
|
|
1231
|
+
errorCode,
|
|
1232
|
+
params,
|
|
1233
|
+
});
|
|
1234
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1235
|
+
const probeProtocolV2 = jest.spyOn(bleTransport as any, 'probeProtocolV2');
|
|
1236
|
+
|
|
1237
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1238
|
+
errorCode,
|
|
1239
|
+
params,
|
|
1240
|
+
});
|
|
1241
|
+
|
|
1242
|
+
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
1243
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1244
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1245
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1246
|
+
}
|
|
1247
|
+
);
|
|
1248
|
+
|
|
1249
|
+
test.each([
|
|
1250
|
+
HardwareErrorCode.BleDeviceBondError,
|
|
1251
|
+
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1252
|
+
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1253
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1254
|
+
])(
|
|
1255
|
+
'preserves pairing-recovery error %s during undeclared Protocol V2 probe after V1 miss',
|
|
1256
|
+
async errorCode => {
|
|
1257
|
+
const device = { id: `undeclared-v2-${errorCode}`, name: 'OneKey Pro 2' };
|
|
1258
|
+
const nobleBle = createNobleBle(device);
|
|
1259
|
+
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
1260
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
1261
|
+
notificationHandler = handler;
|
|
1262
|
+
return jest.fn();
|
|
1263
|
+
});
|
|
1264
|
+
if (errorCode === HardwareErrorCode.BleDeviceBondedCanceled) {
|
|
1265
|
+
nobleBle.write.mockImplementationOnce(() => {
|
|
1266
|
+
setTimeout(() => notificationHandler?.(device.id, 'PAIRING_REJECTED'), 0);
|
|
1267
|
+
return Promise.resolve();
|
|
1268
|
+
});
|
|
1269
|
+
} else {
|
|
1270
|
+
nobleBle.write.mockRejectedValueOnce({
|
|
1271
|
+
name: 'HardwareError',
|
|
1272
|
+
message: `Pairing failure ${errorCode}`,
|
|
1273
|
+
errorCode,
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1277
|
+
jest
|
|
1278
|
+
.spyOn(bleTransport as any, 'callProtocolV1')
|
|
1279
|
+
.mockRejectedValueOnce(ERRORS.TypedError(HardwareErrorCode.BleTimeoutError));
|
|
1280
|
+
|
|
1281
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({ errorCode });
|
|
1282
|
+
|
|
1283
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1284
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1285
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1286
|
+
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1287
|
+
}
|
|
1288
|
+
);
|
|
1289
|
+
|
|
1033
1290
|
test('does not take a Protocol V2 hint from the BLE name', async () => {
|
|
1034
1291
|
const device = { id: 'named-pro2-id', name: 'OneKey Pro 2' };
|
|
1035
1292
|
const nobleBle = createNobleBle(device);
|
|
@@ -1373,3 +1630,339 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1373
1630
|
}
|
|
1374
1631
|
});
|
|
1375
1632
|
});
|
|
1633
|
+
|
|
1634
|
+
describe('ElectronBleTransport Windows write-loop protocol detection integration', () => {
|
|
1635
|
+
const originalPlatform = process.platform;
|
|
1636
|
+
|
|
1637
|
+
afterEach(() => {
|
|
1638
|
+
Object.defineProperty(process, 'platform', { value: originalPlatform, configurable: true });
|
|
1639
|
+
delete (global as any).window;
|
|
1640
|
+
jest.useRealTimers();
|
|
1641
|
+
jest.resetModules();
|
|
1642
|
+
jest.clearAllMocks();
|
|
1643
|
+
});
|
|
1644
|
+
|
|
1645
|
+
const setupWindowsIntegration = async (options?: { writeErrorOnV1?: Error }) => {
|
|
1646
|
+
Object.defineProperty(process, 'platform', { value: 'win32', configurable: true });
|
|
1647
|
+
jest.useFakeTimers({ doNotFake: ['performance', 'setImmediate'] });
|
|
1648
|
+
|
|
1649
|
+
const handlers = new Map<string, (...args: unknown[]) => unknown>();
|
|
1650
|
+
const notificationListeners = new Set<(deviceId: string, data: string) => void>();
|
|
1651
|
+
const disconnectListeners = new Set<
|
|
1652
|
+
(device: { id: string; name: string; reason?: EBleDisconnectReason }) => void
|
|
1653
|
+
>();
|
|
1654
|
+
const mtuListeners = new Set<(device: { id: string; mtu: number }) => void>();
|
|
1655
|
+
|
|
1656
|
+
const webContents = {
|
|
1657
|
+
isDestroyed: jest.fn(() => false),
|
|
1658
|
+
on: jest.fn(),
|
|
1659
|
+
send: jest.fn((channel: string, ...args: unknown[]) => {
|
|
1660
|
+
if (channel === EOneKeyBleMessageKeys.NOBLE_BLE_NOTIFICATION) {
|
|
1661
|
+
const [deviceId, data] = args as [string, string];
|
|
1662
|
+
notificationListeners.forEach(listener => listener(deviceId, data));
|
|
1663
|
+
} else if (channel === EOneKeyBleMessageKeys.BLE_DEVICE_DISCONNECTED) {
|
|
1664
|
+
const [payload] = args as [{ id: string; name: string; reason?: EBleDisconnectReason }];
|
|
1665
|
+
disconnectListeners.forEach(listener => listener(payload));
|
|
1666
|
+
} else if (channel === EOneKeyBleMessageKeys.NOBLE_BLE_MTU_CHANGED) {
|
|
1667
|
+
const [payload] = args as [{ id: string; mtu: number }];
|
|
1668
|
+
mtuListeners.forEach(listener => listener(payload));
|
|
1669
|
+
}
|
|
1670
|
+
}),
|
|
1671
|
+
};
|
|
1672
|
+
|
|
1673
|
+
const ipcMain = {
|
|
1674
|
+
handle: jest.fn((channel: string, handler: (...args: unknown[]) => unknown) => {
|
|
1675
|
+
handlers.set(channel, handler);
|
|
1676
|
+
}),
|
|
1677
|
+
removeHandler: jest.fn((channel: string) => {
|
|
1678
|
+
handlers.delete(channel);
|
|
1679
|
+
}),
|
|
1680
|
+
};
|
|
1681
|
+
|
|
1682
|
+
const deviceId = 'win-v2-only-device';
|
|
1683
|
+
let responseSeq = 0;
|
|
1684
|
+
|
|
1685
|
+
const notifyCharacteristic = Object.assign(new EventEmitter(), {
|
|
1686
|
+
uuid: '0003',
|
|
1687
|
+
unsubscribe: jest.fn((callback: (error?: Error) => void) => callback()),
|
|
1688
|
+
subscribe: jest.fn((callback: (error?: Error) => void) => callback()),
|
|
1689
|
+
});
|
|
1690
|
+
|
|
1691
|
+
const writeCharacteristic = Object.assign(new EventEmitter(), {
|
|
1692
|
+
uuid: '0002',
|
|
1693
|
+
write: jest.fn(
|
|
1694
|
+
(buffer: Buffer, _withoutResponse: boolean, callback: (error?: Error) => void) => {
|
|
1695
|
+
if (buffer[0] === 0x5a) {
|
|
1696
|
+
// Emit synchronously so attemptWindowsWriteUntilPaired observes
|
|
1697
|
+
// pairedDevices before it starts another silent retry.
|
|
1698
|
+
notifyCharacteristic.emit('data', Buffer.alloc(0));
|
|
1699
|
+
callback();
|
|
1700
|
+
return;
|
|
1701
|
+
}
|
|
1702
|
+
if (options?.writeErrorOnV1) {
|
|
1703
|
+
callback(options.writeErrorOnV1);
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
// Healthy V2-only device ignores V1 GetFeatures silently without native errors.
|
|
1707
|
+
callback();
|
|
1708
|
+
}
|
|
1709
|
+
),
|
|
1710
|
+
});
|
|
1711
|
+
|
|
1712
|
+
const service = {
|
|
1713
|
+
uuid: '0001',
|
|
1714
|
+
discoverCharacteristics: jest.fn(
|
|
1715
|
+
(
|
|
1716
|
+
_characteristicUuids: string[],
|
|
1717
|
+
callback: (error: Error | null, value: unknown[]) => void
|
|
1718
|
+
) => callback(null, [writeCharacteristic, notifyCharacteristic])
|
|
1719
|
+
),
|
|
1720
|
+
};
|
|
1721
|
+
|
|
1722
|
+
const peripheral = Object.assign(new EventEmitter(), {
|
|
1723
|
+
id: deviceId,
|
|
1724
|
+
state: 'disconnected',
|
|
1725
|
+
advertisement: {
|
|
1726
|
+
localName: 'Unknown BLE Device',
|
|
1727
|
+
serviceUuids: ['0001', 'fffd'],
|
|
1728
|
+
},
|
|
1729
|
+
connect: jest.fn((callback: (error?: Error) => void) => {
|
|
1730
|
+
peripheral.state = 'connected';
|
|
1731
|
+
callback();
|
|
1732
|
+
}),
|
|
1733
|
+
disconnect: jest.fn((callback: () => void) => {
|
|
1734
|
+
peripheral.state = 'disconnected';
|
|
1735
|
+
callback();
|
|
1736
|
+
}),
|
|
1737
|
+
discoverServices: jest.fn(
|
|
1738
|
+
(_serviceUuids: string[], callback: (error: Error | null, value: unknown[]) => void) =>
|
|
1739
|
+
callback(null, [service])
|
|
1740
|
+
),
|
|
1741
|
+
});
|
|
1742
|
+
|
|
1743
|
+
const noble = Object.assign(new EventEmitter(), {
|
|
1744
|
+
state: 'poweredOn',
|
|
1745
|
+
startScanning: jest.fn((_services, _duplicates, callback) => {
|
|
1746
|
+
callback?.();
|
|
1747
|
+
noble.emit('discover', peripheral);
|
|
1748
|
+
}),
|
|
1749
|
+
stopScanning: jest.fn(callback => callback?.()),
|
|
1750
|
+
stop: jest.fn(),
|
|
1751
|
+
});
|
|
1752
|
+
|
|
1753
|
+
jest.resetModules();
|
|
1754
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
1755
|
+
jest.doMock(
|
|
1756
|
+
'electron',
|
|
1757
|
+
() => ({
|
|
1758
|
+
ipcMain,
|
|
1759
|
+
webContents: { getAllWebContents: () => [webContents] },
|
|
1760
|
+
}),
|
|
1761
|
+
{ virtual: true }
|
|
1762
|
+
);
|
|
1763
|
+
jest.doMock('electron-log', () => ({
|
|
1764
|
+
info: jest.fn(),
|
|
1765
|
+
debug: jest.fn(),
|
|
1766
|
+
warn: jest.fn(),
|
|
1767
|
+
error: jest.fn(),
|
|
1768
|
+
}));
|
|
1769
|
+
|
|
1770
|
+
const sdk = await import('@onekeyfe/hd-transport-electron/src/noble-ble-handler');
|
|
1771
|
+
const TransportCtor = (await import('../src/electron-ble-transport')).default;
|
|
1772
|
+
sdk.setupNobleBleHandlers(webContents as any);
|
|
1773
|
+
|
|
1774
|
+
const invoke = (channel: string, ...args: unknown[]) => {
|
|
1775
|
+
const handler = handlers.get(channel);
|
|
1776
|
+
if (!handler) {
|
|
1777
|
+
throw new Error(`Missing IPC handler for ${channel}`);
|
|
1778
|
+
}
|
|
1779
|
+
return Promise.resolve(handler(undefined, ...args));
|
|
1780
|
+
};
|
|
1781
|
+
|
|
1782
|
+
const nobleBle = {
|
|
1783
|
+
enumerate: () => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE),
|
|
1784
|
+
getDevice: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_GET_DEVICE, id),
|
|
1785
|
+
connect: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT, id),
|
|
1786
|
+
disconnect: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_DISCONNECT, id),
|
|
1787
|
+
subscribe: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_SUBSCRIBE, id),
|
|
1788
|
+
unsubscribe: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_UNSUBSCRIBE, id),
|
|
1789
|
+
write: async (id: string, hex: string, writeOptions?: unknown) => {
|
|
1790
|
+
const result = await invoke(EOneKeyBleMessageKeys.NOBLE_BLE_WRITE, id, hex, writeOptions);
|
|
1791
|
+
// The native Windows write loop may emit a notification before the
|
|
1792
|
+
// renderer has started readFrame. Deliver the V2 Success after the
|
|
1793
|
+
// write IPC settles, matching the renderer-level V2-only tests.
|
|
1794
|
+
if (hex.startsWith('5a')) {
|
|
1795
|
+
responseSeq += 1;
|
|
1796
|
+
const response = ProtocolV2.encodeFrame(
|
|
1797
|
+
schemas,
|
|
1798
|
+
'Success',
|
|
1799
|
+
{ message: 'ok' },
|
|
1800
|
+
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: responseSeq }
|
|
1801
|
+
);
|
|
1802
|
+
setTimeout(() => {
|
|
1803
|
+
notificationListeners.forEach(listener => listener(id, bytesToHex(response)));
|
|
1804
|
+
}, 0);
|
|
1805
|
+
}
|
|
1806
|
+
return result;
|
|
1807
|
+
},
|
|
1808
|
+
release: (id: string, keepSession?: boolean) =>
|
|
1809
|
+
invoke(EOneKeyBleMessageKeys.NOBLE_BLE_RELEASE, id, keepSession),
|
|
1810
|
+
onNotification: (listener: (id: string, data: string) => void) => {
|
|
1811
|
+
notificationListeners.add(listener);
|
|
1812
|
+
return () => notificationListeners.delete(listener);
|
|
1813
|
+
},
|
|
1814
|
+
onDeviceDisconnected: (
|
|
1815
|
+
listener: (device: { id: string; name: string; reason?: EBleDisconnectReason }) => void
|
|
1816
|
+
) => {
|
|
1817
|
+
disconnectListeners.add(listener);
|
|
1818
|
+
return () => disconnectListeners.delete(listener);
|
|
1819
|
+
},
|
|
1820
|
+
onMtuChanged: (listener: (device: { id: string; mtu: number }) => void) => {
|
|
1821
|
+
mtuListeners.add(listener);
|
|
1822
|
+
return () => mtuListeners.delete(listener);
|
|
1823
|
+
},
|
|
1824
|
+
checkAvailability: () => invoke(EOneKeyBleMessageKeys.BLE_AVAILABILITY_CHECK),
|
|
1825
|
+
};
|
|
1826
|
+
|
|
1827
|
+
(global as any).window = {
|
|
1828
|
+
desktopApi: {
|
|
1829
|
+
nobleBle,
|
|
1830
|
+
},
|
|
1831
|
+
};
|
|
1832
|
+
|
|
1833
|
+
const logger = createLogger();
|
|
1834
|
+
const bleTransport = new TransportCtor();
|
|
1835
|
+
bleTransport.init(logger);
|
|
1836
|
+
bleTransport.configure(protocolV1Schema);
|
|
1837
|
+
bleTransport.configureProtocolV2(protocolV2Schema);
|
|
1838
|
+
|
|
1839
|
+
const driveTimersUntilSettled = async <T>(promise: Promise<T>): Promise<T> => {
|
|
1840
|
+
let settled = false;
|
|
1841
|
+
promise.then(
|
|
1842
|
+
() => {
|
|
1843
|
+
settled = true;
|
|
1844
|
+
},
|
|
1845
|
+
() => {
|
|
1846
|
+
settled = true;
|
|
1847
|
+
}
|
|
1848
|
+
);
|
|
1849
|
+
for (let step = 0; step < 60 && !settled; step += 1) {
|
|
1850
|
+
for (let flush = 0; flush < 5 && !settled; flush += 1) {
|
|
1851
|
+
// eslint-disable-next-line no-await-in-loop
|
|
1852
|
+
await new Promise<void>(resolve => {
|
|
1853
|
+
setImmediate(resolve);
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
if (settled) break;
|
|
1857
|
+
jest.advanceTimersByTime(2000);
|
|
1858
|
+
}
|
|
1859
|
+
return promise;
|
|
1860
|
+
};
|
|
1861
|
+
|
|
1862
|
+
return {
|
|
1863
|
+
sdk,
|
|
1864
|
+
logger,
|
|
1865
|
+
webContents,
|
|
1866
|
+
bleTransport,
|
|
1867
|
+
nobleBle,
|
|
1868
|
+
peripheral,
|
|
1869
|
+
writeCharacteristic,
|
|
1870
|
+
driveTimersUntilSettled,
|
|
1871
|
+
};
|
|
1872
|
+
};
|
|
1873
|
+
|
|
1874
|
+
test('detects a V2-only device on Windows through real attemptWindowsWriteUntilPaired V1 silence (first acquire and wasBonded reconnect)', async () => {
|
|
1875
|
+
const {
|
|
1876
|
+
sdk,
|
|
1877
|
+
bleTransport,
|
|
1878
|
+
nobleBle,
|
|
1879
|
+
peripheral,
|
|
1880
|
+
writeCharacteristic,
|
|
1881
|
+
driveTimersUntilSettled,
|
|
1882
|
+
} = await setupWindowsIntegration();
|
|
1883
|
+
|
|
1884
|
+
try {
|
|
1885
|
+
await driveTimersUntilSettled(nobleBle.enumerate());
|
|
1886
|
+
|
|
1887
|
+
// First acquire, no protocol hint: V1 write loop hits 15 silent attempts (715
|
|
1888
|
+
// pairing_no_response), keeps GATT link, and falls through to V2 Ping.
|
|
1889
|
+
await expect(
|
|
1890
|
+
driveTimersUntilSettled(bleTransport.acquire({ uuid: peripheral.id }))
|
|
1891
|
+
).resolves.toMatchObject({
|
|
1892
|
+
uuid: peripheral.id,
|
|
1893
|
+
protocolType: 'V2',
|
|
1894
|
+
});
|
|
1895
|
+
|
|
1896
|
+
const firstAcquireFirstBytes = writeCharacteristic.write.mock.calls.map(([buf]) => buf[0]);
|
|
1897
|
+
expect(firstAcquireFirstBytes.slice(0, 15).every(b => b === 0x3f)).toBe(true);
|
|
1898
|
+
expect(firstAcquireFirstBytes[15]).toBe(0x5a);
|
|
1899
|
+
expect(firstAcquireFirstBytes).toHaveLength(16);
|
|
1900
|
+
// Cold setup force-reconnects once. V1 silence must not abort the GATT link
|
|
1901
|
+
// before the V2 probe runs.
|
|
1902
|
+
expect(peripheral.disconnect).toHaveBeenCalledTimes(1);
|
|
1903
|
+
|
|
1904
|
+
// Disconnect so pairedDevices clears while bondedDevices retains peripheral.id.
|
|
1905
|
+
await bleTransport.disconnect(peripheral.id);
|
|
1906
|
+
writeCharacteristic.write.mockClear();
|
|
1907
|
+
peripheral.disconnect.mockClear();
|
|
1908
|
+
|
|
1909
|
+
// Clear renderer hint so the next acquire probes V1 first again with wasBonded=true.
|
|
1910
|
+
(bleTransport as any).deviceProtocolHints.delete(peripheral.id);
|
|
1911
|
+
|
|
1912
|
+
// wasBonded + silence on V1 produces BleBondInvalid (724) with reason: 'stale_bond'
|
|
1913
|
+
// and no nativeErrorMessage; healthy V2 device still falls through to V2.
|
|
1914
|
+
await expect(
|
|
1915
|
+
driveTimersUntilSettled(bleTransport.acquire({ uuid: peripheral.id }))
|
|
1916
|
+
).resolves.toMatchObject({
|
|
1917
|
+
uuid: peripheral.id,
|
|
1918
|
+
protocolType: 'V2',
|
|
1919
|
+
});
|
|
1920
|
+
|
|
1921
|
+
const secondAcquireFirstBytes = writeCharacteristic.write.mock.calls.map(([buf]) => buf[0]);
|
|
1922
|
+
expect(secondAcquireFirstBytes.slice(0, 15).every(b => b === 0x3f)).toBe(true);
|
|
1923
|
+
expect(secondAcquireFirstBytes[15]).toBe(0x5a);
|
|
1924
|
+
expect(secondAcquireFirstBytes).toHaveLength(16);
|
|
1925
|
+
// After mockClear, only the next cold-setup force-reconnect should run.
|
|
1926
|
+
expect(peripheral.disconnect).toHaveBeenCalledTimes(1);
|
|
1927
|
+
|
|
1928
|
+
await bleTransport.disconnect(peripheral.id);
|
|
1929
|
+
} finally {
|
|
1930
|
+
await sdk.disposeNobleBleSupport();
|
|
1931
|
+
}
|
|
1932
|
+
});
|
|
1933
|
+
|
|
1934
|
+
test('keeps confirmed Windows native status: 3 terminal during undeclared V1 probe', async () => {
|
|
1935
|
+
const {
|
|
1936
|
+
sdk,
|
|
1937
|
+
bleTransport,
|
|
1938
|
+
nobleBle,
|
|
1939
|
+
peripheral,
|
|
1940
|
+
writeCharacteristic,
|
|
1941
|
+
driveTimersUntilSettled,
|
|
1942
|
+
} = await setupWindowsIntegration({
|
|
1943
|
+
writeErrorOnV1: new Error('GATT operation failed, status: 3'),
|
|
1944
|
+
});
|
|
1945
|
+
|
|
1946
|
+
try {
|
|
1947
|
+
await driveTimersUntilSettled(nobleBle.enumerate());
|
|
1948
|
+
|
|
1949
|
+
await expect(
|
|
1950
|
+
driveTimersUntilSettled(bleTransport.acquire({ uuid: peripheral.id }))
|
|
1951
|
+
).rejects.toMatchObject({
|
|
1952
|
+
errorCode: HardwareErrorCode.BleDeviceBondError,
|
|
1953
|
+
params: {
|
|
1954
|
+
phase: 'pairing',
|
|
1955
|
+
platform: 'win32',
|
|
1956
|
+
stage: 'write',
|
|
1957
|
+
reason: 'pairing_failed',
|
|
1958
|
+
nativeErrorMessage: 'GATT operation failed, status: 3',
|
|
1959
|
+
},
|
|
1960
|
+
});
|
|
1961
|
+
|
|
1962
|
+
expect(writeCharacteristic.write).toHaveBeenCalledTimes(1);
|
|
1963
|
+
expect(peripheral.disconnect).toHaveBeenCalled();
|
|
1964
|
+
} finally {
|
|
1965
|
+
await sdk.disposeNobleBleSupport();
|
|
1966
|
+
}
|
|
1967
|
+
});
|
|
1968
|
+
});
|
|
@@ -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,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAA4B,MAAM,iCAAiC,CAAC;AAC5F,OAAO,KAAK,EAEV,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,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC;
|
|
1
|
+
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
CHANGED
|
@@ -1525,6 +1525,41 @@ const BLE_PACKET_SIZE_MAXIMUM = 244;
|
|
|
1525
1525
|
const BLE_WRITE_DELAY_MS = 5;
|
|
1526
1526
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
1527
1527
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
1528
|
+
const isSilentWindowsPairingNoResponse = (error) => {
|
|
1529
|
+
if (!error || typeof error !== 'object') {
|
|
1530
|
+
return false;
|
|
1531
|
+
}
|
|
1532
|
+
const candidate = error;
|
|
1533
|
+
const code = candidate.errorCode;
|
|
1534
|
+
if (code !== hdShared.HardwareErrorCode.BleDeviceBondError && code !== hdShared.HardwareErrorCode.BleBondInvalid) {
|
|
1535
|
+
return false;
|
|
1536
|
+
}
|
|
1537
|
+
const { params } = candidate;
|
|
1538
|
+
if (!params || typeof params !== 'object') {
|
|
1539
|
+
return false;
|
|
1540
|
+
}
|
|
1541
|
+
const { phase, platform, stage, reason, nativeErrorMessage } = params;
|
|
1542
|
+
if (phase !== 'pairing' || platform !== 'win32' || stage !== 'write') {
|
|
1543
|
+
return false;
|
|
1544
|
+
}
|
|
1545
|
+
if (typeof nativeErrorMessage === 'string' && nativeErrorMessage.length > 0) {
|
|
1546
|
+
return false;
|
|
1547
|
+
}
|
|
1548
|
+
return reason === 'pairing_no_response' || reason === 'stale_bond';
|
|
1549
|
+
};
|
|
1550
|
+
const shouldRethrowProtocolProbeError = (error, isLastProbe = false) => {
|
|
1551
|
+
if (transport.isProtocolV2LinkDisabledError(error)) {
|
|
1552
|
+
return true;
|
|
1553
|
+
}
|
|
1554
|
+
if (isSilentWindowsPairingNoResponse(error)) {
|
|
1555
|
+
return isLastProbe;
|
|
1556
|
+
}
|
|
1557
|
+
const code = error === null || error === void 0 ? void 0 : error.errorCode;
|
|
1558
|
+
return (code === hdShared.HardwareErrorCode.BleDeviceBondError ||
|
|
1559
|
+
code === hdShared.HardwareErrorCode.BleDeviceBondedCanceled ||
|
|
1560
|
+
code === hdShared.HardwareErrorCode.BlePeerRemovedPairingInformation ||
|
|
1561
|
+
code === hdShared.HardwareErrorCode.BleBondInvalid);
|
|
1562
|
+
};
|
|
1528
1563
|
class ElectronBleTransport {
|
|
1529
1564
|
constructor() {
|
|
1530
1565
|
this.name = 'ElectronBleTransport';
|
|
@@ -1918,10 +1953,13 @@ class ElectronBleTransport {
|
|
|
1918
1953
|
const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
1919
1954
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
1920
1955
|
const protocol = probeOrder[i];
|
|
1956
|
+
const isLastProbe = i === probeOrder.length - 1;
|
|
1921
1957
|
if (i > 0) {
|
|
1922
1958
|
yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
1923
1959
|
}
|
|
1924
|
-
const detected = protocol === 'V1'
|
|
1960
|
+
const detected = protocol === 'V1'
|
|
1961
|
+
? yield this.probeProtocolV1(uuid, isLastProbe)
|
|
1962
|
+
: yield this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
1925
1963
|
if (detected) {
|
|
1926
1964
|
this.deviceProtocol.set(uuid, protocol);
|
|
1927
1965
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -1965,7 +2003,7 @@ class ElectronBleTransport {
|
|
|
1965
2003
|
}
|
|
1966
2004
|
});
|
|
1967
2005
|
}
|
|
1968
|
-
probeProtocolV1(uuid) {
|
|
2006
|
+
probeProtocolV1(uuid, isLastProbe = false) {
|
|
1969
2007
|
var _a;
|
|
1970
2008
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1971
2009
|
if (!this._messages) {
|
|
@@ -1979,14 +2017,14 @@ class ElectronBleTransport {
|
|
|
1979
2017
|
catch (error) {
|
|
1980
2018
|
this.clearProbeProtocol(uuid, 'V1');
|
|
1981
2019
|
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
1982
|
-
if (
|
|
2020
|
+
if (shouldRethrowProtocolProbeError(error, isLastProbe)) {
|
|
1983
2021
|
throw error;
|
|
1984
2022
|
}
|
|
1985
2023
|
return false;
|
|
1986
2024
|
}
|
|
1987
2025
|
});
|
|
1988
2026
|
}
|
|
1989
|
-
probeProtocolV2(uuid, expectedProtocol) {
|
|
2027
|
+
probeProtocolV2(uuid, expectedProtocol, isLastProbe = false) {
|
|
1990
2028
|
var _a;
|
|
1991
2029
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1992
2030
|
if (!this._messages || !this._messagesV2) {
|
|
@@ -1994,22 +2032,28 @@ class ElectronBleTransport {
|
|
|
1994
2032
|
}
|
|
1995
2033
|
this.deviceProtocol.set(uuid, 'V2');
|
|
1996
2034
|
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2035
|
+
try {
|
|
2036
|
+
const detected = yield transport.probeProtocolV2({
|
|
2037
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
2038
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
2039
|
+
logger: this.Log,
|
|
2040
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
2041
|
+
onProbeFailed: () => {
|
|
2042
|
+
var _a;
|
|
2043
|
+
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
2044
|
+
this.resetProtocolV2Frames(uuid);
|
|
2045
|
+
},
|
|
2046
|
+
shouldRethrow: error => expectedProtocol === 'V2' || shouldRethrowProtocolProbeError(error, isLastProbe),
|
|
2047
|
+
});
|
|
2048
|
+
if (!detected) {
|
|
2049
|
+
this.clearProbeProtocol(uuid, 'V2');
|
|
2050
|
+
}
|
|
2051
|
+
return detected;
|
|
2052
|
+
}
|
|
2053
|
+
catch (error) {
|
|
2010
2054
|
this.clearProbeProtocol(uuid, 'V2');
|
|
2055
|
+
throw error;
|
|
2011
2056
|
}
|
|
2012
|
-
return detected;
|
|
2013
2057
|
});
|
|
2014
2058
|
}
|
|
2015
2059
|
writeOnce(uuid, hexData) {
|
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.3",
|
|
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.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.3",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.3"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.
|
|
28
|
+
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.3",
|
|
29
29
|
"@types/w3c-web-usb": "^1.0.6",
|
|
30
30
|
"@types/web-bluetooth": "^0.0.17"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "350063c5fbcbd2461c37c2d8419ce9d0e0e5fa4d"
|
|
33
33
|
}
|
|
@@ -91,6 +91,53 @@ const BLE_WRITE_DELAY_MS = 5;
|
|
|
91
91
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
92
92
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
93
93
|
|
|
94
|
+
const isSilentWindowsPairingNoResponse = (error: unknown): boolean => {
|
|
95
|
+
if (!error || typeof error !== 'object') {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const candidate = error as { errorCode?: unknown; params?: unknown };
|
|
99
|
+
const code = candidate.errorCode;
|
|
100
|
+
if (code !== HardwareErrorCode.BleDeviceBondError && code !== HardwareErrorCode.BleBondInvalid) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const { params } = candidate;
|
|
104
|
+
if (!params || typeof params !== 'object') {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const { phase, platform, stage, reason, nativeErrorMessage } = params as Record<string, unknown>;
|
|
108
|
+
if (phase !== 'pairing' || platform !== 'win32' || stage !== 'write') {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (typeof nativeErrorMessage === 'string' && nativeErrorMessage.length > 0) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return reason === 'pairing_no_response' || reason === 'stale_bond';
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const shouldRethrowProtocolProbeError = (error: unknown, isLastProbe = false): boolean => {
|
|
118
|
+
if (isProtocolV2LinkDisabledError(error)) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
// A Windows write loop that received no notification without any native error
|
|
122
|
+
// happens when a V2-only device ignores the initial V1 probe; allow fallback
|
|
123
|
+
// to the alternate protocol on the same link. Confirmed pairing errors carry
|
|
124
|
+
// native evidence and remain terminal. If this is the last remaining probe,
|
|
125
|
+
// preserve the silent 715/724 so Core can show re-pair instead of retrying
|
|
126
|
+
// TransportProtocolProbeFailed.
|
|
127
|
+
if (isSilentWindowsPairingNoResponse(error)) {
|
|
128
|
+
return isLastProbe;
|
|
129
|
+
}
|
|
130
|
+
const code = (error as { errorCode?: unknown })?.errorCode;
|
|
131
|
+
// Pairing-recovery and cancel failures are not protocol mismatches; preserve
|
|
132
|
+
// them instead of falling through to another probe or ProbeFailed.
|
|
133
|
+
return (
|
|
134
|
+
code === HardwareErrorCode.BleDeviceBondError ||
|
|
135
|
+
code === HardwareErrorCode.BleDeviceBondedCanceled ||
|
|
136
|
+
code === HardwareErrorCode.BlePeerRemovedPairingInformation ||
|
|
137
|
+
code === HardwareErrorCode.BleBondInvalid
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
|
|
94
141
|
/**
|
|
95
142
|
* Desktop Electron BLE transport with automatic Protocol V1/V2 detection.
|
|
96
143
|
*
|
|
@@ -617,13 +664,16 @@ export default class ElectronBleTransport {
|
|
|
617
664
|
|
|
618
665
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
619
666
|
const protocol = probeOrder[i];
|
|
667
|
+
const isLastProbe = i === probeOrder.length - 1;
|
|
620
668
|
if (i > 0) {
|
|
621
669
|
// Keep the physical BLE link while switching probes. Reconnecting here can
|
|
622
670
|
// summon a second OS pairing prompt for a device that is still onboarding.
|
|
623
671
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
624
672
|
}
|
|
625
673
|
const detected =
|
|
626
|
-
protocol === 'V1'
|
|
674
|
+
protocol === 'V1'
|
|
675
|
+
? await this.probeProtocolV1(uuid, isLastProbe)
|
|
676
|
+
: await this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
627
677
|
if (detected) {
|
|
628
678
|
this.deviceProtocol.set(uuid, protocol);
|
|
629
679
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -675,7 +725,7 @@ export default class ElectronBleTransport {
|
|
|
675
725
|
}
|
|
676
726
|
}
|
|
677
727
|
|
|
678
|
-
private async probeProtocolV1(uuid: string) {
|
|
728
|
+
private async probeProtocolV1(uuid: string, isLastProbe = false) {
|
|
679
729
|
if (!this._messages) {
|
|
680
730
|
return false;
|
|
681
731
|
}
|
|
@@ -689,36 +739,46 @@ export default class ElectronBleTransport {
|
|
|
689
739
|
} catch (error) {
|
|
690
740
|
this.clearProbeProtocol(uuid, 'V1');
|
|
691
741
|
this.Log?.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
692
|
-
if (
|
|
742
|
+
if (shouldRethrowProtocolProbeError(error, isLastProbe)) {
|
|
693
743
|
throw error;
|
|
694
744
|
}
|
|
695
745
|
return false;
|
|
696
746
|
}
|
|
697
747
|
}
|
|
698
748
|
|
|
699
|
-
private async probeProtocolV2(
|
|
749
|
+
private async probeProtocolV2(
|
|
750
|
+
uuid: string,
|
|
751
|
+
expectedProtocol?: ProtocolType,
|
|
752
|
+
isLastProbe = false
|
|
753
|
+
) {
|
|
700
754
|
if (!this._messages || !this._messagesV2) {
|
|
701
755
|
return false;
|
|
702
756
|
}
|
|
703
757
|
|
|
704
758
|
this.deviceProtocol.set(uuid, 'V2');
|
|
705
759
|
this.v2Assemblers.get(uuid)?.reset();
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
760
|
+
try {
|
|
761
|
+
const detected = await probeProtocolV2Helper({
|
|
762
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
763
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
764
|
+
logger: this.Log,
|
|
765
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
766
|
+
onProbeFailed: () => {
|
|
767
|
+
this.v2Assemblers.get(uuid)?.reset();
|
|
768
|
+
this.resetProtocolV2Frames(uuid);
|
|
769
|
+
},
|
|
770
|
+
// A declared V2 protocol needs no fallback; preserve the actual link failure.
|
|
771
|
+
shouldRethrow: error =>
|
|
772
|
+
expectedProtocol === 'V2' || shouldRethrowProtocolProbeError(error, isLastProbe),
|
|
773
|
+
});
|
|
774
|
+
if (!detected) {
|
|
775
|
+
this.clearProbeProtocol(uuid, 'V2');
|
|
776
|
+
}
|
|
777
|
+
return detected;
|
|
778
|
+
} catch (error) {
|
|
719
779
|
this.clearProbeProtocol(uuid, 'V2');
|
|
780
|
+
throw error;
|
|
720
781
|
}
|
|
721
|
-
return detected;
|
|
722
782
|
}
|
|
723
783
|
|
|
724
784
|
private async writeOnce(uuid: string, hexData: string): Promise<void> {
|