@onekeyfe/hd-transport-react-native 1.2.11-alpha.7 → 1.2.11-alpha.8
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/dist/bleIosBond.d.ts +3 -0
- package/dist/bleIosBond.d.ts.map +1 -0
- package/dist/index.d.ts +53 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +464 -272
- package/package.json +5 -5
- package/src/__tests__/bleIosBond.test.ts +35 -0
- package/src/__tests__/connectTimeout.test.ts +44 -0
- package/src/__tests__/iosPresentDeviceConnect.test.ts +228 -0
- package/src/__tests__/nativeFrameWrite.test.ts +1 -0
- package/src/__tests__/protocolV2Link.test.ts +486 -3
- package/src/__tests__/releaseBondConcurrency.test.ts +284 -0
- package/src/bleIosBond.ts +39 -0
- package/src/index.ts +627 -336
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.2.11-alpha.
|
|
3
|
+
"version": "1.2.11-alpha.8",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-core": "1.2.11-alpha.
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
23
|
+
"@onekeyfe/hd-core": "1.2.11-alpha.8",
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.8",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.8",
|
|
26
26
|
"@onekeyfe/react-native-ble-utils": "0.1.9",
|
|
27
27
|
"react-native-ble-plx": "3.5.1"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "f2f0e4af13efbb09f4b421171142f9144124a1c2"
|
|
30
30
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
+
|
|
3
|
+
import { isIosPeerRemovedPairingInformation, toIosStaleBondError } from '../bleIosBond';
|
|
4
|
+
|
|
5
|
+
describe('iOS lost-bond mapping', () => {
|
|
6
|
+
test('recognizes CBErrorPeerRemovedPairingInformation from the native code', () => {
|
|
7
|
+
const error = Object.assign(new Error('Device was disconnected'), {
|
|
8
|
+
errorCode: 201,
|
|
9
|
+
iosErrorCode: 14,
|
|
10
|
+
reason: 'Peer removed pairing information',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
expect(isIosPeerRemovedPairingInformation(error)).toBe(true);
|
|
14
|
+
expect(isIosPeerRemovedPairingInformation({ iosErrorCode: 7 })).toBe(false);
|
|
15
|
+
expect(isIosPeerRemovedPairingInformation(new Error('localized text is ignored'))).toBe(false);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('maps the lost bond to the public re-pair error with safe context', () => {
|
|
19
|
+
const mapped = toIosStaleBondError(
|
|
20
|
+
Object.assign(new Error('Device was disconnected'), {
|
|
21
|
+
iosErrorCode: 14,
|
|
22
|
+
reason: 'Peer removed pairing information',
|
|
23
|
+
})
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(mapped).toMatchObject({
|
|
27
|
+
errorCode: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
28
|
+
params: {
|
|
29
|
+
phase: 'connect',
|
|
30
|
+
reason: 'peer_removed_pairing_information',
|
|
31
|
+
nativeErrorMessage: 'Peer removed pairing information Device was disconnected',
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -528,6 +528,50 @@ describe('BLE connect timeout', () => {
|
|
|
528
528
|
expect(BLE_CONNECT_TIMEOUT_MS).toBeLessThanOrEqual(12000);
|
|
529
529
|
});
|
|
530
530
|
|
|
531
|
+
test.each(
|
|
532
|
+
(['ios', 'android'] as const).flatMap(platform =>
|
|
533
|
+
(['manager', 'device'] as const).flatMap(source =>
|
|
534
|
+
(['V1', 'V2'] as const).map(expectedProtocol => ({ platform, source, expectedProtocol }))
|
|
535
|
+
)
|
|
536
|
+
)
|
|
537
|
+
)(
|
|
538
|
+
'cancel drains a late $platform $source connect before rejecting $expectedProtocol acquire',
|
|
539
|
+
async ({ platform, source, expectedProtocol }) => {
|
|
540
|
+
Object.assign(Platform, { OS: platform });
|
|
541
|
+
const { transport, device, bleManager, connect } = createHarness(() => nativeConnect.promise);
|
|
542
|
+
const nativeConnect = createDeferred<typeof device>();
|
|
543
|
+
const nativeDisconnect = createDeferred<void>();
|
|
544
|
+
if (source === 'manager') bleManager.devices.mockResolvedValue([]);
|
|
545
|
+
device.isConnected.mockResolvedValue(true);
|
|
546
|
+
if (source === 'device') device.isConnected.mockResolvedValueOnce(false);
|
|
547
|
+
bleManager.cancelDeviceConnection.mockImplementation(() => nativeDisconnect.promise);
|
|
548
|
+
let settled = false;
|
|
549
|
+
const acquiring = transport.acquire({ uuid: UUID, expectedProtocol }).catch(error => {
|
|
550
|
+
settled = true;
|
|
551
|
+
return error;
|
|
552
|
+
});
|
|
553
|
+
await flush();
|
|
554
|
+
await flush();
|
|
555
|
+
const pendingConnect = source === 'manager' ? bleManager.connectToDevice : connect;
|
|
556
|
+
expect(pendingConnect).toHaveBeenCalledTimes(1);
|
|
557
|
+
|
|
558
|
+
await transport.cancel(UUID);
|
|
559
|
+
nativeConnect.resolve(device);
|
|
560
|
+
await flush();
|
|
561
|
+
expect(settled).toBe(false);
|
|
562
|
+
expect(bleManager.cancelDeviceConnection).toHaveBeenCalledWith(UUID);
|
|
563
|
+
expect(device.discoverAllServicesAndCharacteristics).not.toHaveBeenCalled();
|
|
564
|
+
|
|
565
|
+
nativeDisconnect.resolve();
|
|
566
|
+
await advanceUntil(() => settled, 1000);
|
|
567
|
+
await expect(acquiring).resolves.toMatchObject({
|
|
568
|
+
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
569
|
+
});
|
|
570
|
+
expect(pendingConnect).toHaveBeenCalledTimes(1);
|
|
571
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
572
|
+
}
|
|
573
|
+
);
|
|
574
|
+
|
|
531
575
|
test.each(
|
|
532
576
|
(['ios', 'android'] as const).flatMap(platform =>
|
|
533
577
|
(['connect', 'mtu', 'gatt'] as const).flatMap(stage =>
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
3
|
+
|
|
4
|
+
import ReactNativeBleTransport, {
|
|
5
|
+
IOS_PRESENT_DEVICE_CONNECT_TIMEOUT_MS,
|
|
6
|
+
IOS_PRESENT_DEVICE_WINDOW_MS,
|
|
7
|
+
} from '../index';
|
|
8
|
+
import protocolV1Schema from './protocolV1SchemaFixture';
|
|
9
|
+
|
|
10
|
+
jest.mock(
|
|
11
|
+
'react-native',
|
|
12
|
+
() => ({
|
|
13
|
+
Platform: { OS: 'ios', select: (spec: Record<string, unknown>) => spec.ios },
|
|
14
|
+
PermissionsAndroid: {
|
|
15
|
+
PERMISSIONS: {},
|
|
16
|
+
RESULTS: {},
|
|
17
|
+
request: jest.fn(),
|
|
18
|
+
requestMultiple: jest.fn(),
|
|
19
|
+
},
|
|
20
|
+
}),
|
|
21
|
+
{ virtual: true }
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
jest.mock('react-native-ble-plx', () => ({
|
|
25
|
+
BleATTErrorCode: { InvalidHandle: 1, UnlikelyError: 14 },
|
|
26
|
+
BleError: Error,
|
|
27
|
+
BleErrorCode: {
|
|
28
|
+
DeviceDisconnected: 201,
|
|
29
|
+
OperationStartFailed: 601,
|
|
30
|
+
DeviceMTUChangeFailed: 401,
|
|
31
|
+
OperationCancelled: 2,
|
|
32
|
+
OperationTimedOut: 3,
|
|
33
|
+
DeviceAlreadyConnected: 203,
|
|
34
|
+
},
|
|
35
|
+
BleManager: jest.fn(),
|
|
36
|
+
ScanMode: { LowLatency: 2 },
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
jest.mock('@onekeyfe/react-native-ble-utils', () => ({
|
|
40
|
+
__esModule: true,
|
|
41
|
+
default: {
|
|
42
|
+
getConnectedPeripherals: jest.fn(() => Promise.resolve([])),
|
|
43
|
+
getBondedPeripherals: jest.fn(() => Promise.resolve([])),
|
|
44
|
+
pairDevice: jest.fn(() => Promise.resolve({ bonded: true, bonding: false })),
|
|
45
|
+
onDeviceBondState: jest.fn(),
|
|
46
|
+
},
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
const UUID = '554820A1-9DE5-8240-DC4F-38D156399D6B';
|
|
50
|
+
const NATIVE_CONNECT_TIMEOUT_MS = 3000;
|
|
51
|
+
/** Measured on an iPhone XR: iOS names the lost bond a little after the 3s native budget. */
|
|
52
|
+
const IOS_LOST_BOND_VERDICT_MS = 3200;
|
|
53
|
+
|
|
54
|
+
type ConnectOptions = { timeout?: number };
|
|
55
|
+
|
|
56
|
+
const flush = () =>
|
|
57
|
+
new Promise(resolve => {
|
|
58
|
+
setImmediate(resolve);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
function createHarness() {
|
|
62
|
+
const connectOptions: ConnectOptions[] = [];
|
|
63
|
+
const connectToLostBond = (options: ConnectOptions = {}) => {
|
|
64
|
+
connectOptions.push(options);
|
|
65
|
+
return new Promise((_, reject) => {
|
|
66
|
+
const budget = options.timeout ?? Number.POSITIVE_INFINITY;
|
|
67
|
+
if (budget < IOS_LOST_BOND_VERDICT_MS) {
|
|
68
|
+
setTimeout(
|
|
69
|
+
() => reject(Object.assign(new Error('Operation timed out'), { errorCode: 3 })),
|
|
70
|
+
budget
|
|
71
|
+
);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
setTimeout(
|
|
75
|
+
() =>
|
|
76
|
+
reject(
|
|
77
|
+
Object.assign(new Error(`Device ${UUID} connection failed`), {
|
|
78
|
+
errorCode: 200,
|
|
79
|
+
iosErrorCode: 14,
|
|
80
|
+
reason: 'Peer removed pairing information',
|
|
81
|
+
})
|
|
82
|
+
),
|
|
83
|
+
IOS_LOST_BOND_VERDICT_MS
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
const device = {
|
|
88
|
+
id: UUID,
|
|
89
|
+
name: 'Neo 5284',
|
|
90
|
+
localName: 'Neo 5284',
|
|
91
|
+
serviceUUIDs: ['00000001-0000-1000-8000-00805f9b34fb'],
|
|
92
|
+
isConnected: jest.fn(() => Promise.resolve(false)),
|
|
93
|
+
cancelConnection: jest.fn(() => Promise.resolve()),
|
|
94
|
+
connect: jest.fn(connectToLostBond),
|
|
95
|
+
discoverAllServicesAndCharacteristics: jest.fn(() => Promise.resolve()),
|
|
96
|
+
onDisconnected: jest.fn(() => ({ remove: jest.fn() })),
|
|
97
|
+
};
|
|
98
|
+
const bleManager = {
|
|
99
|
+
devices: jest.fn(() => Promise.resolve([device])),
|
|
100
|
+
connectedDevices: jest.fn(() => Promise.resolve([])),
|
|
101
|
+
connectToDevice: jest.fn((_id: string, options?: ConnectOptions) => connectToLostBond(options)),
|
|
102
|
+
cancelTransaction: jest.fn(() => Promise.resolve()),
|
|
103
|
+
cancelDeviceConnection: jest.fn(() => Promise.resolve()),
|
|
104
|
+
destroy: jest.fn(() => Promise.resolve()),
|
|
105
|
+
onStateChange: jest.fn((listener: (state: string) => void) => {
|
|
106
|
+
setImmediate(() => listener('PoweredOn'));
|
|
107
|
+
return { remove: jest.fn() };
|
|
108
|
+
}),
|
|
109
|
+
state: jest.fn(() => Promise.resolve('PoweredOn')),
|
|
110
|
+
startDeviceScan: jest.fn(),
|
|
111
|
+
stopDeviceScan: jest.fn(),
|
|
112
|
+
};
|
|
113
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
|
|
114
|
+
(transport as any).blePlxManager = bleManager;
|
|
115
|
+
transport.init(
|
|
116
|
+
{ debug: jest.fn(), error: jest.fn(), warn: jest.fn() } as any,
|
|
117
|
+
new EventEmitter()
|
|
118
|
+
);
|
|
119
|
+
transport.configure(protocolV1Schema);
|
|
120
|
+
|
|
121
|
+
return { transport, connectOptions, device };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Runs one acquire to its end, moving the fake clock past every wait on the way. */
|
|
125
|
+
async function acquire(transport: ReactNativeBleTransport, input: Record<string, unknown> = {}) {
|
|
126
|
+
let outcome: { code?: unknown; message?: string } | undefined;
|
|
127
|
+
transport.acquire({ uuid: UUID, ...input } as never).then(
|
|
128
|
+
() => {
|
|
129
|
+
outcome = {};
|
|
130
|
+
},
|
|
131
|
+
error => {
|
|
132
|
+
const { code, error: message } = ERRORS.serializeError({ error });
|
|
133
|
+
outcome = { code, message };
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
for (let i = 0; i < 600 && !outcome; i += 1) {
|
|
137
|
+
// eslint-disable-next-line no-await-in-loop
|
|
138
|
+
await flush();
|
|
139
|
+
if (!outcome) jest.advanceTimersByTime(100);
|
|
140
|
+
}
|
|
141
|
+
if (!outcome) throw new Error('acquire did not settle');
|
|
142
|
+
return outcome;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
describe('iOS connect to a device that was just seen advertising', () => {
|
|
146
|
+
beforeEach(() => {
|
|
147
|
+
jest.useFakeTimers({ doNotFake: ['setImmediate', 'performance', 'nextTick'] });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
afterEach(() => {
|
|
151
|
+
jest.clearAllTimers();
|
|
152
|
+
jest.useRealTimers();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('waits long enough for iOS to name the lost bond', async () => {
|
|
156
|
+
const { transport, connectOptions } = createHarness();
|
|
157
|
+
(transport as any).iosLastAdvertisedAt.set(UUID, Date.now());
|
|
158
|
+
|
|
159
|
+
await expect(acquire(transport)).resolves.toMatchObject({
|
|
160
|
+
code: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
161
|
+
});
|
|
162
|
+
expect(connectOptions).toEqual([
|
|
163
|
+
expect.objectContaining({ timeout: IOS_PRESENT_DEVICE_CONNECT_TIMEOUT_MS }),
|
|
164
|
+
]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('without a sighting the short budget ends as a setup timeout, not a lost bond', async () => {
|
|
168
|
+
const { transport, connectOptions } = createHarness();
|
|
169
|
+
|
|
170
|
+
const first = await acquire(transport);
|
|
171
|
+
const second = await acquire(transport);
|
|
172
|
+
|
|
173
|
+
expect(connectOptions.map(options => options.timeout)).toEqual([
|
|
174
|
+
NATIVE_CONNECT_TIMEOUT_MS,
|
|
175
|
+
NATIVE_CONNECT_TIMEOUT_MS,
|
|
176
|
+
]);
|
|
177
|
+
expect(first.code).not.toBe(HardwareErrorCode.BlePeerRemovedPairingInformation);
|
|
178
|
+
expect(second).toMatchObject({ code: HardwareErrorCode.PollingTimeout });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('spends the sighting once, so the next attempt is short again', async () => {
|
|
182
|
+
const { transport, connectOptions } = createHarness();
|
|
183
|
+
(transport as any).iosLastAdvertisedAt.set(UUID, Date.now());
|
|
184
|
+
|
|
185
|
+
await acquire(transport);
|
|
186
|
+
await acquire(transport);
|
|
187
|
+
|
|
188
|
+
expect(connectOptions.map(options => options.timeout)).toEqual([
|
|
189
|
+
IOS_PRESENT_DEVICE_CONNECT_TIMEOUT_MS,
|
|
190
|
+
NATIVE_CONNECT_TIMEOUT_MS,
|
|
191
|
+
]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('ignores a sighting that is no longer fresh', async () => {
|
|
195
|
+
const { transport, connectOptions } = createHarness();
|
|
196
|
+
(transport as any).iosLastAdvertisedAt.set(UUID, Date.now());
|
|
197
|
+
jest.setSystemTime(Date.now() + IOS_PRESENT_DEVICE_WINDOW_MS + 1);
|
|
198
|
+
|
|
199
|
+
await acquire(transport);
|
|
200
|
+
|
|
201
|
+
expect(connectOptions[0]?.timeout).toBe(NATIVE_CONNECT_TIMEOUT_MS);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('keeps the short budget for a firmware-install reconnect', async () => {
|
|
205
|
+
const { transport, connectOptions } = createHarness();
|
|
206
|
+
(transport as any).iosLastAdvertisedAt.set(UUID, Date.now());
|
|
207
|
+
|
|
208
|
+
await acquire(transport, { expectedProtocol: 'V2', skipProtocolProbe: true });
|
|
209
|
+
|
|
210
|
+
expect(connectOptions[0]?.timeout).toBe(NATIVE_CONNECT_TIMEOUT_MS);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('reports a lost bond named by the first GATT operation', async () => {
|
|
214
|
+
const { transport, device } = createHarness();
|
|
215
|
+
device.connect.mockResolvedValueOnce(device);
|
|
216
|
+
device.discoverAllServicesAndCharacteristics.mockRejectedValueOnce(
|
|
217
|
+
Object.assign(new Error('Device was disconnected'), {
|
|
218
|
+
errorCode: 201,
|
|
219
|
+
iosErrorCode: 14,
|
|
220
|
+
reason: 'Peer removed pairing information',
|
|
221
|
+
})
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
await expect(acquire(transport)).resolves.toMatchObject({
|
|
225
|
+
code: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
|
@@ -42,6 +42,7 @@ jest.mock('react-native-ble-plx', () => ({
|
|
|
42
42
|
}));
|
|
43
43
|
|
|
44
44
|
jest.mock('../BleManager', () => ({
|
|
45
|
+
getBondedDevices: jest.fn(() => Promise.resolve([])),
|
|
45
46
|
getConnectedDeviceIds: jest.fn(() => Promise.resolve([])),
|
|
46
47
|
onDeviceBondState: jest.fn(() => Promise.resolve()),
|
|
47
48
|
pairDevice: jest.fn(() => Promise.resolve({ bonded: true, bonding: false })),
|