@onekeyfe/hd-transport-web-device 1.2.11-alpha.8 → 1.2.11-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/electron-ble-transport.test.ts +63 -579
- package/__tests__/webusb-identity-probe.test.ts +2 -6
- package/__tests__/webusb-protocol-cache.test.ts +31 -0
- package/__tests__/webusb-protocol-v2-timeout.test.ts +3 -3
- package/dist/electron-ble-transport.d.ts +5 -0
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +73 -73
- 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 +74 -84
- package/src/webusb.ts +8 -10
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import transport, { PROTOCOL_V2_CHANNEL_BLE_UART, bytesToHex } from '@onekeyfe/hd-transport';
|
|
2
|
-
import {
|
|
3
|
-
EBleDisconnectReason,
|
|
4
|
-
EOneKeyBleMessageKeys,
|
|
5
|
-
ERRORS,
|
|
6
|
-
HardwareErrorCode,
|
|
7
|
-
createDeferred,
|
|
8
|
-
} from '@onekeyfe/hd-shared';
|
|
2
|
+
import { EBleDisconnectReason, HardwareErrorCode, createDeferred } from '@onekeyfe/hd-shared';
|
|
9
3
|
import EventEmitter from 'events';
|
|
10
4
|
|
|
11
5
|
import ElectronBleTransport from '../src/electron-ble-transport';
|
|
@@ -843,42 +837,6 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
843
837
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
844
838
|
});
|
|
845
839
|
|
|
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
|
-
|
|
882
840
|
test('keeps a first expected Protocol V2 probe miss retryable', async () => {
|
|
883
841
|
const device = { id: 'first-v2-id', name: 'OneKey Pro 2' };
|
|
884
842
|
const nobleBle = createNobleBle(device);
|
|
@@ -1072,221 +1030,6 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1072
1030
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1073
1031
|
});
|
|
1074
1032
|
|
|
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
|
-
|
|
1290
1033
|
test('does not take a Protocol V2 hint from the BLE name', async () => {
|
|
1291
1034
|
const device = { id: 'named-pro2-id', name: 'OneKey Pro 2' };
|
|
1292
1035
|
const nobleBle = createNobleBle(device);
|
|
@@ -1629,340 +1372,81 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1629
1372
|
await bleTransport.release(device.id);
|
|
1630
1373
|
}
|
|
1631
1374
|
});
|
|
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
1375
|
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
const
|
|
1650
|
-
const
|
|
1651
|
-
const
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
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
|
-
};
|
|
1376
|
+
test('stop() unsubscribes host disconnect listener and releases owned links idempotently', async () => {
|
|
1377
|
+
const device = { id: 'stop-cleanup-pro2-id', name: 'OneKey Pro 2' };
|
|
1378
|
+
const nobleBle = createNobleBle(device);
|
|
1379
|
+
const emitter = new EventEmitter();
|
|
1380
|
+
const hostDisconnectCleanup = jest.fn();
|
|
1381
|
+
const notifyCleanup = jest.fn();
|
|
1382
|
+
const mtuCleanup = jest.fn();
|
|
1383
|
+
let disconnectHandler:
|
|
1384
|
+
| ((d: { id: string; name: string; reason?: EBleDisconnectReason }) => void)
|
|
1385
|
+
| undefined;
|
|
1721
1386
|
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
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
|
-
),
|
|
1387
|
+
nobleBle.onDeviceDisconnected.mockImplementation(handler => {
|
|
1388
|
+
disconnectHandler = handler;
|
|
1389
|
+
return () => {
|
|
1390
|
+
hostDisconnectCleanup();
|
|
1391
|
+
disconnectHandler = undefined;
|
|
1392
|
+
};
|
|
1741
1393
|
});
|
|
1742
|
-
|
|
1743
|
-
const
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
noble.emit('discover', peripheral);
|
|
1748
|
-
}),
|
|
1749
|
-
stopScanning: jest.fn(callback => callback?.()),
|
|
1750
|
-
stop: jest.fn(),
|
|
1394
|
+
echoProtocolV2(nobleBle, device.id);
|
|
1395
|
+
const echoOnNotification = nobleBle.onNotification.getMockImplementation();
|
|
1396
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
1397
|
+
echoOnNotification?.(handler);
|
|
1398
|
+
return notifyCleanup;
|
|
1751
1399
|
});
|
|
1400
|
+
nobleBle.onMtuChanged.mockImplementation(() => mtuCleanup);
|
|
1752
1401
|
|
|
1753
|
-
jest.
|
|
1754
|
-
|
|
1755
|
-
|
|
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);
|
|
1402
|
+
const transportDisconnect = jest.fn();
|
|
1403
|
+
emitter.on('transport-device-disconnect', transportDisconnect);
|
|
1404
|
+
const bleTransport = configureTransport(nobleBle, emitter);
|
|
1903
1405
|
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
writeCharacteristic.write.mockClear();
|
|
1907
|
-
peripheral.disconnect.mockClear();
|
|
1406
|
+
await bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
1407
|
+
expect(bleTransport.getProtocolType(device.id)).toBe('V2');
|
|
1908
1408
|
|
|
1909
|
-
|
|
1910
|
-
(bleTransport as any).deviceProtocolHints.delete(peripheral.id);
|
|
1409
|
+
await bleTransport.stop();
|
|
1911
1410
|
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
protocolType: 'V2',
|
|
1919
|
-
});
|
|
1411
|
+
expect(hostDisconnectCleanup).toHaveBeenCalledTimes(1);
|
|
1412
|
+
expect(notifyCleanup).toHaveBeenCalledTimes(1);
|
|
1413
|
+
expect(mtuCleanup).toHaveBeenCalledTimes(1);
|
|
1414
|
+
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1415
|
+
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1416
|
+
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1920
1417
|
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
// After mockClear, only the next cold-setup force-reconnect should run.
|
|
1926
|
-
expect(peripheral.disconnect).toHaveBeenCalledTimes(1);
|
|
1418
|
+
// Calling stop() again is a safe no-op.
|
|
1419
|
+
await bleTransport.stop();
|
|
1420
|
+
expect(hostDisconnectCleanup).toHaveBeenCalledTimes(1);
|
|
1421
|
+
expect(nobleBle.disconnect).toHaveBeenCalledTimes(1);
|
|
1927
1422
|
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
}
|
|
1423
|
+
// Late host disconnect events are no longer delivered to the stopped transport.
|
|
1424
|
+
disconnectHandler?.({ ...device, reason: EBleDisconnectReason.DeviceDisconnected });
|
|
1425
|
+
expect(transportDisconnect).not.toHaveBeenCalled();
|
|
1932
1426
|
});
|
|
1933
1427
|
|
|
1934
|
-
test('
|
|
1935
|
-
const {
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
try {
|
|
1947
|
-
await driveTimersUntilSettled(nobleBle.enumerate());
|
|
1428
|
+
test('stop() during getDevice() does not let retired acquire disconnect a replacement link', async () => {
|
|
1429
|
+
const device = { id: 'lookup-stop-pro2-id', name: 'OneKey Pro 2' };
|
|
1430
|
+
const nobleBle = createNobleBle(device);
|
|
1431
|
+
let resolveGetDevice: ((value: typeof device) => void) | undefined;
|
|
1432
|
+
nobleBle.getDevice.mockImplementation(
|
|
1433
|
+
() =>
|
|
1434
|
+
new Promise(resolve => {
|
|
1435
|
+
resolveGetDevice = resolve;
|
|
1436
|
+
})
|
|
1437
|
+
);
|
|
1438
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1948
1439
|
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
platform: 'win32',
|
|
1956
|
-
stage: 'write',
|
|
1957
|
-
reason: 'pairing_failed',
|
|
1958
|
-
nativeErrorMessage: 'GATT operation failed, status: 3',
|
|
1959
|
-
},
|
|
1960
|
-
});
|
|
1440
|
+
const acquiring = bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
1441
|
+
const stopping = bleTransport.stop();
|
|
1442
|
+
await stopping;
|
|
1443
|
+
// Clear disconnect count recorded during stopResources() so we verify the late
|
|
1444
|
+
// retired acquire does not issue another disconnect against a replacement link.
|
|
1445
|
+
nobleBle.disconnect.mockClear();
|
|
1961
1446
|
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
}
|
|
1447
|
+
resolveGetDevice?.(device);
|
|
1448
|
+
await expect(acquiring).rejects.toThrow(/Electron BLE transport is stopped/);
|
|
1449
|
+
expect(nobleBle.connect).not.toHaveBeenCalled();
|
|
1450
|
+
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
1967
1451
|
});
|
|
1968
1452
|
});
|