@onekeyfe/hd-transport-react-native 1.2.0-alpha.17 → 1.2.0-alpha.171
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/BleTransport.d.ts +2 -4
- package/dist/BleTransport.d.ts.map +1 -1
- package/dist/bleStrategy.d.ts +7 -0
- package/dist/bleStrategy.d.ts.map +1 -1
- package/dist/constants.d.ts +2 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +137 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +921 -304
- package/dist/transportLog.d.ts +1 -12
- package/dist/transportLog.d.ts.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/jest.config.js +10 -0
- package/package.json +6 -5
- package/src/BleTransport.ts +10 -42
- package/src/__tests__/BleTransport.test.ts +114 -0
- package/src/__tests__/bleStrategy.test.ts +93 -5
- package/src/__tests__/connectTimeout.test.ts +303 -0
- package/src/__tests__/constants.test.ts +20 -0
- package/src/__tests__/enumerate.test.ts +150 -0
- package/src/__tests__/protocolReprobe.test.ts +156 -0
- package/src/__tests__/protocolV1SchemaFixture.ts +39 -0
- package/src/__tests__/protocolV2Link.test.ts +900 -32
- package/src/__tests__/staleCallTimeout.test.ts +210 -0
- package/src/__tests__/writePacketTimeout.test.ts +305 -0
- package/src/bleStrategy.ts +26 -11
- package/src/constants.ts +9 -14
- package/src/index.ts +1214 -280
- package/src/transportLog.ts +1 -18
- package/src/types.ts +1 -0
|
@@ -2,11 +2,17 @@ import { EventEmitter } from 'events';
|
|
|
2
2
|
import transportPackage, {
|
|
3
3
|
PROTOCOL_V2_CHANNEL_BLE_UART,
|
|
4
4
|
ProtocolV2,
|
|
5
|
-
|
|
5
|
+
TRANSPORT_EVENT,
|
|
6
6
|
} from '@onekeyfe/hd-transport';
|
|
7
|
-
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
7
|
+
import { HardwareErrorCode, createDeferred } from '@onekeyfe/hd-shared';
|
|
8
8
|
|
|
9
|
-
import ReactNativeBleTransport
|
|
9
|
+
import ReactNativeBleTransport, {
|
|
10
|
+
BLE_NATIVE_TEARDOWN_TIMEOUT_MS,
|
|
11
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
12
|
+
configureProtocolV2BleTuning,
|
|
13
|
+
getFirmwareUploadWriteRetryType,
|
|
14
|
+
resetProtocolV2BleTuning,
|
|
15
|
+
} from '../index';
|
|
10
16
|
|
|
11
17
|
jest.mock(
|
|
12
18
|
'react-native',
|
|
@@ -28,6 +34,7 @@ jest.mock('react-native-ble-plx', () => ({
|
|
|
28
34
|
CharacteristicNotFound: 404,
|
|
29
35
|
},
|
|
30
36
|
BleManager: jest.fn(),
|
|
37
|
+
ConnectionPriority: { Balanced: 0, High: 1, LowPower: 2 },
|
|
31
38
|
ScanMode: { LowLatency: 2 },
|
|
32
39
|
}));
|
|
33
40
|
|
|
@@ -41,11 +48,17 @@ jest.mock('../subscribeBleOn', () => ({
|
|
|
41
48
|
subscribeBleOn: jest.fn(() => Promise.resolve()),
|
|
42
49
|
}));
|
|
43
50
|
|
|
51
|
+
const setPlatformOS = (os: 'ios' | 'android') => {
|
|
52
|
+
const reactNative: { Platform: { OS: string } } = jest.requireMock('react-native');
|
|
53
|
+
reactNative.Platform.OS = os;
|
|
54
|
+
};
|
|
55
|
+
|
|
44
56
|
const { parseConfigure } = transportPackage;
|
|
45
57
|
|
|
46
58
|
const protocolV1Schema = {
|
|
47
59
|
nested: {
|
|
48
60
|
Initialize: { fields: {} },
|
|
61
|
+
GetFeatures: { fields: {} },
|
|
49
62
|
Success: {
|
|
50
63
|
fields: {
|
|
51
64
|
message: { type: 'string', id: 1 },
|
|
@@ -55,6 +68,7 @@ const protocolV1Schema = {
|
|
|
55
68
|
values: {
|
|
56
69
|
MessageType_Initialize: 1,
|
|
57
70
|
MessageType_Success: 2,
|
|
71
|
+
MessageType_GetFeatures: 55,
|
|
58
72
|
},
|
|
59
73
|
},
|
|
60
74
|
},
|
|
@@ -62,11 +76,13 @@ const protocolV1Schema = {
|
|
|
62
76
|
|
|
63
77
|
const protocolV2Schema = {
|
|
64
78
|
nested: {
|
|
79
|
+
ProtocolInfoRequest: { fields: {} },
|
|
65
80
|
Ping: {
|
|
66
81
|
fields: {
|
|
67
82
|
message: { type: 'string', id: 1 },
|
|
68
83
|
},
|
|
69
84
|
},
|
|
85
|
+
DeviceInfoGet: { fields: {} },
|
|
70
86
|
FileWrite: { fields: {} },
|
|
71
87
|
Success: {
|
|
72
88
|
fields: {
|
|
@@ -75,8 +91,10 @@ const protocolV2Schema = {
|
|
|
75
91
|
},
|
|
76
92
|
MessageType: {
|
|
77
93
|
values: {
|
|
94
|
+
MessageType_ProtocolInfoRequest: 60200,
|
|
78
95
|
MessageType_Ping: 60206,
|
|
79
96
|
MessageType_Success: 60207,
|
|
97
|
+
MessageType_DeviceInfoGet: 60600,
|
|
80
98
|
MessageType_FileWrite: 60805,
|
|
81
99
|
},
|
|
82
100
|
},
|
|
@@ -88,9 +106,16 @@ const schemas = {
|
|
|
88
106
|
protocolV2: parseConfigure(protocolV2Schema),
|
|
89
107
|
};
|
|
90
108
|
|
|
91
|
-
const createHarness = (
|
|
109
|
+
const createHarness = ({
|
|
110
|
+
deviceName = 'OneKey Pro 2',
|
|
111
|
+
isWritableWithResponse = true,
|
|
112
|
+
}: {
|
|
113
|
+
deviceName?: string;
|
|
114
|
+
isWritableWithResponse?: boolean;
|
|
115
|
+
} = {}) => {
|
|
92
116
|
const uuid = 'rn-pro2-id';
|
|
93
117
|
const sentSeqs: number[] = [];
|
|
118
|
+
let responseSeq = 0;
|
|
94
119
|
let shouldRespond = true;
|
|
95
120
|
let notifyCallback:
|
|
96
121
|
| ((
|
|
@@ -98,6 +123,7 @@ const createHarness = () => {
|
|
|
98
123
|
characteristic: { value: string } | null
|
|
99
124
|
) => void)
|
|
100
125
|
| undefined;
|
|
126
|
+
let disconnectCallback: (() => void) | undefined;
|
|
101
127
|
const notifyCharacteristic = {
|
|
102
128
|
uuid: '0003',
|
|
103
129
|
deviceID: uuid,
|
|
@@ -111,11 +137,12 @@ const createHarness = () => {
|
|
|
111
137
|
const frame = Buffer.from(base64, 'base64');
|
|
112
138
|
sentSeqs.push(frame[6]);
|
|
113
139
|
if (shouldRespond) {
|
|
140
|
+
responseSeq += 1;
|
|
114
141
|
const response = ProtocolV2.encodeFrame(
|
|
115
142
|
schemas,
|
|
116
143
|
'Success',
|
|
117
144
|
{ message: 'ok' },
|
|
118
|
-
{ router: PROTOCOL_V2_CHANNEL_BLE_UART }
|
|
145
|
+
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: responseSeq }
|
|
119
146
|
);
|
|
120
147
|
notifyCallback?.(null, { value: Buffer.from(response).toString('base64') });
|
|
121
148
|
}
|
|
@@ -124,36 +151,51 @@ const createHarness = () => {
|
|
|
124
151
|
const writeCharacteristic = {
|
|
125
152
|
uuid: '0002',
|
|
126
153
|
deviceID: uuid,
|
|
127
|
-
isWritableWithResponse
|
|
154
|
+
isWritableWithResponse,
|
|
128
155
|
isWritableWithoutResponse: true,
|
|
129
156
|
writeWithResponse: jest.fn(handleWrite),
|
|
130
157
|
writeWithoutResponse: jest.fn(handleWrite),
|
|
131
158
|
};
|
|
132
159
|
const device = {
|
|
133
160
|
id: uuid,
|
|
134
|
-
name:
|
|
135
|
-
localName:
|
|
136
|
-
|
|
161
|
+
name: deviceName,
|
|
162
|
+
localName: deviceName,
|
|
163
|
+
mtu: 247,
|
|
164
|
+
serviceUUIDs: ['00000001-0000-1000-8000-00805f9b34fb'],
|
|
137
165
|
isConnected: jest.fn(() => Promise.resolve(true)),
|
|
138
|
-
|
|
139
|
-
|
|
166
|
+
cancelConnection: jest.fn(() => Promise.resolve()),
|
|
167
|
+
onDisconnected: jest.fn(callback => {
|
|
168
|
+
disconnectCallback = callback;
|
|
169
|
+
return { remove: jest.fn() };
|
|
170
|
+
}),
|
|
171
|
+
} as any;
|
|
172
|
+
device.requestMTU = jest.fn(() => Promise.resolve(device));
|
|
173
|
+
device.requestConnectionPriority = jest.fn(() => Promise.resolve(device));
|
|
140
174
|
const bleManager = {
|
|
141
175
|
devices: jest.fn(() => Promise.resolve([device])),
|
|
142
176
|
connectedDevices: jest.fn(() => Promise.resolve([])),
|
|
143
177
|
cancelTransaction: jest.fn(() => Promise.resolve()),
|
|
178
|
+
cancelDeviceConnection: jest.fn(() => Promise.resolve()),
|
|
179
|
+
destroy: jest.fn(),
|
|
144
180
|
};
|
|
145
181
|
const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
|
|
182
|
+
const emitter = new EventEmitter();
|
|
183
|
+
const logger = { debug: jest.fn(), error: jest.fn() };
|
|
146
184
|
transport.blePlxManager = bleManager;
|
|
147
185
|
transport.resolveCharacteristics = jest.fn(() =>
|
|
148
186
|
Promise.resolve({ writeCharacteristic, notifyCharacteristic })
|
|
149
187
|
);
|
|
150
|
-
transport.init(
|
|
188
|
+
transport.init(logger, emitter);
|
|
151
189
|
transport.configure(protocolV1Schema);
|
|
152
190
|
transport.configureProtocolV2(protocolV2Schema);
|
|
153
191
|
|
|
154
192
|
return {
|
|
155
193
|
transport,
|
|
194
|
+
emitter,
|
|
195
|
+
logger,
|
|
156
196
|
uuid,
|
|
197
|
+
device,
|
|
198
|
+
bleManager,
|
|
157
199
|
sentSeqs,
|
|
158
200
|
writeCharacteristic,
|
|
159
201
|
setShouldRespond(value: boolean) {
|
|
@@ -162,30 +204,604 @@ const createHarness = () => {
|
|
|
162
204
|
emitMonitorError(error: Error & { reason?: string }) {
|
|
163
205
|
notifyCallback?.(error, null);
|
|
164
206
|
},
|
|
207
|
+
emitDisconnect() {
|
|
208
|
+
disconnectCallback?.();
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const createV1Harness = ({
|
|
214
|
+
respondOnWriteCount = 1,
|
|
215
|
+
isWritableWithResponse = true,
|
|
216
|
+
}: {
|
|
217
|
+
respondOnWriteCount?: number | number[];
|
|
218
|
+
isWritableWithResponse?: boolean;
|
|
219
|
+
} = {}) => {
|
|
220
|
+
const uuid = 'rn-classic-id';
|
|
221
|
+
const notifySubscriptionRemovers: jest.Mock[] = [];
|
|
222
|
+
const disconnectSubscriptionRemovers: jest.Mock[] = [];
|
|
223
|
+
let notifyCallback:
|
|
224
|
+
| ((error: Error | null, characteristic: { value: string } | null) => void)
|
|
225
|
+
| undefined;
|
|
226
|
+
const notifyCharacteristic = {
|
|
227
|
+
uuid: '0003',
|
|
228
|
+
deviceID: uuid,
|
|
229
|
+
isNotifiable: true,
|
|
230
|
+
monitor: jest.fn(callback => {
|
|
231
|
+
notifyCallback = callback;
|
|
232
|
+
const remove = jest.fn();
|
|
233
|
+
notifySubscriptionRemovers.push(remove);
|
|
234
|
+
return { remove };
|
|
235
|
+
}),
|
|
236
|
+
};
|
|
237
|
+
let writeCount = 0;
|
|
238
|
+
const responseWriteCounts = new Set(
|
|
239
|
+
Array.isArray(respondOnWriteCount) ? respondOnWriteCount : [respondOnWriteCount]
|
|
240
|
+
);
|
|
241
|
+
const handleWrite = () => {
|
|
242
|
+
writeCount += 1;
|
|
243
|
+
if (responseWriteCounts.has(writeCount)) {
|
|
244
|
+
notifyCallback?.(null, {
|
|
245
|
+
value: Buffer.from('3f23230002000000040a026f6b', 'hex').toString('base64'),
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return Promise.resolve();
|
|
249
|
+
};
|
|
250
|
+
const writeCharacteristic = {
|
|
251
|
+
uuid: '0002',
|
|
252
|
+
deviceID: uuid,
|
|
253
|
+
isWritableWithResponse,
|
|
254
|
+
isWritableWithoutResponse: true,
|
|
255
|
+
writeWithResponse: jest.fn(handleWrite),
|
|
256
|
+
writeWithoutResponse: jest.fn(handleWrite),
|
|
257
|
+
};
|
|
258
|
+
const device = {
|
|
259
|
+
id: uuid,
|
|
260
|
+
name: 'OneKey Classic',
|
|
261
|
+
localName: 'OneKey Classic',
|
|
262
|
+
mtu: 247,
|
|
263
|
+
serviceUUIDs: ['00000001-0000-1000-8000-00805f9b34fb'],
|
|
264
|
+
isConnected: jest.fn(() => Promise.resolve(true)),
|
|
265
|
+
cancelConnection: jest.fn(() => Promise.resolve()),
|
|
266
|
+
onDisconnected: jest.fn(() => {
|
|
267
|
+
const remove = jest.fn();
|
|
268
|
+
disconnectSubscriptionRemovers.push(remove);
|
|
269
|
+
return { remove };
|
|
270
|
+
}),
|
|
271
|
+
} as any;
|
|
272
|
+
device.requestMTU = jest.fn(() => Promise.resolve(device));
|
|
273
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
|
|
274
|
+
const bleManager = {
|
|
275
|
+
devices: jest.fn(() => Promise.resolve([device])),
|
|
276
|
+
connectedDevices: jest.fn(() => Promise.resolve([])),
|
|
277
|
+
cancelTransaction: jest.fn(() => Promise.resolve()),
|
|
278
|
+
};
|
|
279
|
+
transport.blePlxManager = bleManager as any;
|
|
280
|
+
transport.resolveCharacteristics = jest.fn(() =>
|
|
281
|
+
Promise.resolve({ writeCharacteristic, notifyCharacteristic })
|
|
282
|
+
);
|
|
283
|
+
transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
|
|
284
|
+
transport.configure(protocolV1Schema);
|
|
285
|
+
transport.configureProtocolV2(protocolV2Schema);
|
|
286
|
+
return {
|
|
287
|
+
transport,
|
|
288
|
+
uuid,
|
|
289
|
+
device,
|
|
290
|
+
bleManager,
|
|
291
|
+
writeCharacteristic,
|
|
292
|
+
notifySubscriptionRemovers,
|
|
293
|
+
disconnectSubscriptionRemovers,
|
|
165
294
|
};
|
|
166
295
|
};
|
|
167
296
|
|
|
168
297
|
describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
169
|
-
test('
|
|
298
|
+
test('does not classify disconnects as retryable firmware writes', () => {
|
|
299
|
+
expect(
|
|
300
|
+
getFirmwareUploadWriteRetryType({
|
|
301
|
+
errorCode: 205,
|
|
302
|
+
message: 'Device disconnected after write',
|
|
303
|
+
})
|
|
304
|
+
).toBeNull();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test.each(['status 143', 'status:143', 'status = 143', 'GATT_CONGESTED'])(
|
|
308
|
+
'classifies %s as transient GATT congestion',
|
|
309
|
+
message => {
|
|
310
|
+
expect(getFirmwareUploadWriteRetryType({ message })).toBe('congested');
|
|
311
|
+
}
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
test('handles long uncontrolled status messages without a backtracking regular expression', () => {
|
|
315
|
+
const message = `status${' '.repeat(100_000)}142`;
|
|
316
|
+
|
|
317
|
+
expect(getFirmwareUploadWriteRetryType({ message })).toBeNull();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test('keeps another device reader when releasing a device with an active V1 call', async () => {
|
|
321
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
322
|
+
const activeV1Call = createDeferred<string>();
|
|
323
|
+
const otherDeviceReader = createDeferred<Uint8Array>();
|
|
324
|
+
activeV1Call.promise.catch(() => undefined);
|
|
325
|
+
otherDeviceReader.promise.catch(() => undefined);
|
|
326
|
+
transport.runPromise = activeV1Call;
|
|
327
|
+
transport.runPromiseDeviceId = 'device-a';
|
|
328
|
+
transport.protocolV2FramePromises.set('device-b', otherDeviceReader);
|
|
329
|
+
|
|
330
|
+
await transport.releaseNative('device-a', true);
|
|
331
|
+
|
|
332
|
+
expect(transport.protocolV2FramePromises.get('device-b')).toBe(otherDeviceReader);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('rejects a pending reader when its device frame state resets', async () => {
|
|
336
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
337
|
+
const reader = createDeferred<Uint8Array>();
|
|
338
|
+
transport.protocolV2FramePromises.set('device-a', reader);
|
|
339
|
+
const result = Promise.race([
|
|
340
|
+
reader.promise.then(
|
|
341
|
+
() => 'resolved',
|
|
342
|
+
() => 'rejected'
|
|
343
|
+
),
|
|
344
|
+
new Promise(resolve => {
|
|
345
|
+
setTimeout(() => resolve('pending'), 20);
|
|
346
|
+
}),
|
|
347
|
+
]);
|
|
348
|
+
|
|
349
|
+
transport.resetProtocolV2Frames('device-a');
|
|
350
|
+
|
|
351
|
+
await expect(result).resolves.toBe('rejected');
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test('keeps the legacy default BLE scan timeout', () => {
|
|
355
|
+
expect(new ReactNativeBleTransport({}).scanTimeout).toBe(3000);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test('uses withResponse for consecutive iOS Protocol V1 control commands without releasing', async () => {
|
|
359
|
+
const { transport, uuid, writeCharacteristic } = createV1Harness({
|
|
360
|
+
respondOnWriteCount: [1, 2],
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V1' })).resolves.toEqual({
|
|
364
|
+
uuid,
|
|
365
|
+
protocolType: 'V1',
|
|
366
|
+
});
|
|
367
|
+
const releaseNative = jest.spyOn(transport as any, 'releaseNative');
|
|
368
|
+
expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
|
|
369
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
370
|
+
|
|
371
|
+
await expect(transport.call(uuid, 'Initialize', {}, { timeoutMs: 50 })).resolves.toBeDefined();
|
|
372
|
+
await expect(transport.call(uuid, 'GetFeatures', {}, { timeoutMs: 50 })).resolves.toBeDefined();
|
|
373
|
+
|
|
374
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(2);
|
|
375
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
376
|
+
expect(releaseNative).not.toHaveBeenCalled();
|
|
377
|
+
await transport.release(uuid, true);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
test('falls back to withoutResponse for an iOS Protocol V1 control command when required', async () => {
|
|
381
|
+
const { transport, uuid, writeCharacteristic } = createV1Harness({
|
|
382
|
+
isWritableWithResponse: false,
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
await transport.acquire({ uuid, expectedProtocol: 'V1' });
|
|
386
|
+
await expect(transport.call(uuid, 'Initialize', {}, { timeoutMs: 50 })).resolves.toBeDefined();
|
|
387
|
+
|
|
388
|
+
expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
|
|
389
|
+
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
390
|
+
await transport.release(uuid, true);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test('does not resend a failed iOS Protocol V1 control write without response', async () => {
|
|
394
|
+
const { transport, uuid, writeCharacteristic } = createV1Harness();
|
|
395
|
+
const writeError = new Error('write with response failed');
|
|
396
|
+
|
|
397
|
+
await transport.acquire({ uuid, expectedProtocol: 'V1' });
|
|
398
|
+
writeCharacteristic.writeWithResponse.mockRejectedValueOnce(writeError);
|
|
399
|
+
|
|
400
|
+
await expect(transport.call(uuid, 'Initialize', {}, { timeoutMs: 50 })).rejects.toMatchObject({
|
|
401
|
+
errorCode: HardwareErrorCode.BleWriteCharacteristicError,
|
|
402
|
+
});
|
|
403
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
|
|
404
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
405
|
+
await transport.release(uuid, true);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
test('detects Protocol V2 on iOS without using the BLE name as a protocol hint', async () => {
|
|
409
|
+
const { transport, uuid, device, sentSeqs, writeCharacteristic } = createHarness({
|
|
410
|
+
deviceName: 'Pro2 6E9E',
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
414
|
+
uuid,
|
|
415
|
+
protocolType: 'V2',
|
|
416
|
+
});
|
|
417
|
+
expect(device.requestMTU).toHaveBeenCalledWith(247);
|
|
418
|
+
expect(writeCharacteristic.writeWithResponse.mock.calls.length).toBeGreaterThan(1);
|
|
419
|
+
|
|
420
|
+
await expect(
|
|
421
|
+
transport.call(uuid, 'Ping', { message: 'first-core-command' })
|
|
422
|
+
).resolves.toBeDefined();
|
|
423
|
+
expect(sentSeqs.filter(seq => seq > 0)).toEqual([1, 2]);
|
|
424
|
+
await transport.release(uuid, true);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
test('reacquires a known Protocol V2 firmware install link without sending Ping', async () => {
|
|
428
|
+
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
429
|
+
const probeProtocolV2 = jest.spyOn(transport as any, 'probeProtocolV2');
|
|
430
|
+
|
|
431
|
+
await expect(
|
|
432
|
+
transport.acquire({ uuid, expectedProtocol: 'V2', skipProtocolProbe: true })
|
|
433
|
+
).resolves.toEqual({
|
|
434
|
+
uuid,
|
|
435
|
+
protocolType: 'V2',
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
439
|
+
expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
|
|
440
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
441
|
+
expect(transport.getProtocolType(uuid)).toBe('V2');
|
|
442
|
+
await transport.release(uuid, true);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
test.each(['ios', 'android'] as const)(
|
|
446
|
+
'keeps a first expected Protocol V2 probe miss retryable on %s',
|
|
447
|
+
async platform => {
|
|
448
|
+
setPlatformOS(platform);
|
|
449
|
+
const { transport, uuid, device } = createHarness();
|
|
450
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
451
|
+
|
|
452
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
453
|
+
errorCode: HardwareErrorCode.RuntimeError,
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
expect(device.cancelConnection).not.toHaveBeenCalled();
|
|
457
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
458
|
+
}
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
test.each(['ios', 'android'] as const)(
|
|
462
|
+
'keeps a second expected Protocol V2 probe miss retryable on %s',
|
|
463
|
+
async platform => {
|
|
464
|
+
setPlatformOS(platform);
|
|
465
|
+
const { transport, uuid, device } = createHarness();
|
|
466
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
467
|
+
|
|
468
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
469
|
+
errorCode: HardwareErrorCode.RuntimeError,
|
|
470
|
+
});
|
|
471
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
472
|
+
errorCode: HardwareErrorCode.RuntimeError,
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
expect(device.cancelConnection).not.toHaveBeenCalled();
|
|
476
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
477
|
+
}
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
test.each(['ios', 'android'] as const)(
|
|
481
|
+
'reports a stale bond on %s when a previously confirmed Protocol V2 device stops responding',
|
|
482
|
+
async platform => {
|
|
483
|
+
setPlatformOS(platform);
|
|
484
|
+
const { transport, uuid, device } = createHarness();
|
|
485
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
486
|
+
await transport.release(uuid, true);
|
|
487
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
488
|
+
|
|
489
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
490
|
+
errorCode: HardwareErrorCode.BleDeviceBondError,
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
expect(device.cancelConnection).toHaveBeenCalled();
|
|
494
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
495
|
+
}
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
test('waits for an in-flight release before reacquiring the same device', async () => {
|
|
499
|
+
const { transport, uuid } = createHarness();
|
|
500
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
501
|
+
const releaseGate = createDeferred<void>();
|
|
502
|
+
const releaseStarted = createDeferred<void>();
|
|
503
|
+
const { protocolV2Links } = transport as any;
|
|
504
|
+
const invalidateLink = protocolV2Links.invalidateLink.bind(protocolV2Links);
|
|
505
|
+
jest
|
|
506
|
+
.spyOn(protocolV2Links, 'invalidateLink')
|
|
507
|
+
.mockImplementationOnce(async (...args: unknown[]) => {
|
|
508
|
+
releaseStarted.resolve();
|
|
509
|
+
await releaseGate.promise;
|
|
510
|
+
return invalidateLink(...args);
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
const release = transport.release(uuid, true);
|
|
514
|
+
await releaseStarted.promise;
|
|
515
|
+
let reacquired = false;
|
|
516
|
+
const acquire = transport.acquire({ uuid, expectedProtocol: 'V2' }).then(result => {
|
|
517
|
+
reacquired = true;
|
|
518
|
+
return result;
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
await Promise.resolve();
|
|
522
|
+
expect(reacquired).toBe(false);
|
|
523
|
+
|
|
524
|
+
releaseGate.resolve();
|
|
525
|
+
await release;
|
|
526
|
+
await expect(acquire).resolves.toEqual({ uuid, protocolType: 'V2' });
|
|
527
|
+
await expect(transport.call(uuid, 'Ping', { message: 'after-release' })).resolves.toMatchObject(
|
|
528
|
+
{
|
|
529
|
+
type: 'Success',
|
|
530
|
+
message: { message: 'ok' },
|
|
531
|
+
}
|
|
532
|
+
);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test(
|
|
536
|
+
'releases the lifecycle queue when native teardown never settles',
|
|
537
|
+
async () => {
|
|
538
|
+
const { transport, uuid, device, bleManager } = createHarness();
|
|
539
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
540
|
+
const otherUuid = 'rn-pro2-other-id';
|
|
541
|
+
const otherDevice = {
|
|
542
|
+
...device,
|
|
543
|
+
id: otherUuid,
|
|
544
|
+
name: 'OneKey Pro 2 Other',
|
|
545
|
+
localName: 'OneKey Pro 2 Other',
|
|
546
|
+
};
|
|
547
|
+
await (transport as any).installTransportForAcquire(otherUuid, otherDevice);
|
|
548
|
+
(transport as any).deviceProtocol.set(otherUuid, 'V2');
|
|
549
|
+
const disconnectEvents: Array<{ connectId: string }> = [];
|
|
550
|
+
transport.emitter?.on(TRANSPORT_EVENT.DEVICE_DISCONNECT, event => {
|
|
551
|
+
disconnectEvents.push(event);
|
|
552
|
+
});
|
|
553
|
+
const stalledNativeCleanup = createDeferred<void>();
|
|
554
|
+
bleManager.cancelTransaction
|
|
555
|
+
.mockImplementationOnce(() => stalledNativeCleanup.promise)
|
|
556
|
+
.mockResolvedValue(undefined);
|
|
557
|
+
const resetPlxManager = jest.spyOn(transport as any, 'resetPlxManager');
|
|
558
|
+
const nextLifecycleOperation = jest.fn().mockResolvedValue('next-operation');
|
|
559
|
+
|
|
560
|
+
const release = transport.release(uuid, true);
|
|
561
|
+
const nextOperation = (transport as any).runLifecycleOperation(uuid, nextLifecycleOperation);
|
|
562
|
+
|
|
563
|
+
await expect(release).resolves.toBe(true);
|
|
564
|
+
await expect(nextOperation).resolves.toBe('next-operation');
|
|
565
|
+
expect(resetPlxManager).toHaveBeenCalledTimes(1);
|
|
566
|
+
expect(nextLifecycleOperation).toHaveBeenCalledTimes(1);
|
|
567
|
+
expect(disconnectEvents).toContainEqual(expect.objectContaining({ connectId: otherUuid }));
|
|
568
|
+
expect(() => (transport as any).getCachedTransport(otherUuid)).toThrow();
|
|
569
|
+
|
|
570
|
+
await (transport as any).installTransportForAcquire(uuid, device);
|
|
571
|
+
(transport as any).deviceProtocol.set(uuid, 'V2');
|
|
572
|
+
|
|
573
|
+
stalledNativeCleanup.resolve();
|
|
574
|
+
await Promise.resolve();
|
|
575
|
+
await expect(
|
|
576
|
+
transport.call(uuid, 'Ping', { message: 'after-stale-cleanup' })
|
|
577
|
+
).resolves.toMatchObject({
|
|
578
|
+
type: 'Success',
|
|
579
|
+
message: { message: 'ok' },
|
|
580
|
+
});
|
|
581
|
+
},
|
|
582
|
+
BLE_NATIVE_TEARDOWN_TIMEOUT_MS + 5_000
|
|
583
|
+
);
|
|
584
|
+
|
|
585
|
+
test('falls back to the other active probe on iOS when protocol metadata is absent', async () => {
|
|
586
|
+
const { transport, uuid } = createHarness({ deviceName: 'OneKey' });
|
|
587
|
+
const probeProtocolV1 = jest
|
|
588
|
+
.spyOn(transport as any, 'probeProtocolV1')
|
|
589
|
+
.mockResolvedValue(false);
|
|
590
|
+
const probeProtocolV2 = jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(true);
|
|
591
|
+
|
|
592
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
593
|
+
uuid,
|
|
594
|
+
protocolType: 'V2',
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
expect(probeProtocolV1).toHaveBeenCalledTimes(1);
|
|
598
|
+
expect(probeProtocolV2).toHaveBeenCalledTimes(1);
|
|
599
|
+
expect(probeProtocolV1.mock.invocationCallOrder[0]).toBeLessThan(
|
|
600
|
+
probeProtocolV2.mock.invocationCallOrder[0]
|
|
601
|
+
);
|
|
602
|
+
await transport.release(uuid, true);
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
test('continues with the current MTU when the connected snapshot refresh fails', async () => {
|
|
606
|
+
const { transport, uuid, device } = createHarness();
|
|
607
|
+
const mtuError = new Error('MTU refresh failed');
|
|
608
|
+
device.requestMTU.mockRejectedValueOnce(mtuError);
|
|
609
|
+
|
|
610
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
611
|
+
uuid,
|
|
612
|
+
protocolType: 'V2',
|
|
613
|
+
});
|
|
614
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(1);
|
|
615
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(247);
|
|
616
|
+
await transport.release(uuid, true);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
test('refreshes a transient bootloader MTU after notifications are ready', async () => {
|
|
620
|
+
const { transport, uuid, device } = createHarness();
|
|
621
|
+
device.mtu = 23;
|
|
622
|
+
device.requestMTU
|
|
623
|
+
.mockResolvedValueOnce(device)
|
|
624
|
+
.mockResolvedValueOnce(device)
|
|
625
|
+
.mockImplementationOnce(() => {
|
|
626
|
+
device.mtu = 247;
|
|
627
|
+
return Promise.resolve(device);
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
631
|
+
uuid,
|
|
632
|
+
protocolType: 'V2',
|
|
633
|
+
});
|
|
634
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(3);
|
|
635
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(247);
|
|
636
|
+
await transport.release(uuid, true);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
test('continues with a low bootloader MTU when the bounded retry fails', async () => {
|
|
640
|
+
const { transport, uuid, device } = createHarness();
|
|
641
|
+
device.mtu = 23;
|
|
642
|
+
device.requestMTU
|
|
643
|
+
.mockResolvedValueOnce(device)
|
|
644
|
+
.mockResolvedValueOnce(device)
|
|
645
|
+
.mockRejectedValueOnce(new Error('bootloader MTU retry failed'));
|
|
646
|
+
|
|
647
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
648
|
+
uuid,
|
|
649
|
+
protocolType: 'V2',
|
|
650
|
+
});
|
|
651
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(3);
|
|
652
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(23);
|
|
653
|
+
await transport.release(uuid, true);
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
test('accepts a stable low MTU without the delayed refresh loop', async () => {
|
|
657
|
+
const { transport, uuid, device } = createHarness();
|
|
658
|
+
device.mtu = 185;
|
|
659
|
+
|
|
660
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
661
|
+
uuid,
|
|
662
|
+
protocolType: 'V2',
|
|
663
|
+
});
|
|
664
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(1);
|
|
665
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(185);
|
|
666
|
+
await transport.release(uuid, true);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
test('continues Protocol V2 probing with a conservative packet size when MTU is unavailable', async () => {
|
|
670
|
+
const { transport, uuid, device, writeCharacteristic } = createHarness();
|
|
671
|
+
device.mtu = undefined;
|
|
672
|
+
|
|
673
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
674
|
+
uuid,
|
|
675
|
+
protocolType: 'V2',
|
|
676
|
+
});
|
|
677
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(3);
|
|
678
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBeUndefined();
|
|
679
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalled();
|
|
680
|
+
await transport.release(uuid, true);
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
test('refreshes an unavailable MTU before a Protocol V2 high-volume write', async () => {
|
|
684
|
+
const { transport, uuid, device, writeCharacteristic } = createHarness();
|
|
685
|
+
device.mtu = undefined;
|
|
686
|
+
|
|
687
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
688
|
+
device.requestMTU.mockImplementationOnce(() => {
|
|
689
|
+
device.mtu = 247;
|
|
690
|
+
return Promise.resolve(device);
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
await expect(transport.call(uuid, 'FileWrite', {})).resolves.toBeDefined();
|
|
694
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(4);
|
|
695
|
+
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
696
|
+
await transport.release(uuid, true);
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
test('rejects a Protocol V2 high-volume write when MTU remains unavailable', async () => {
|
|
700
|
+
const { transport, uuid, device, writeCharacteristic } = createHarness();
|
|
701
|
+
device.mtu = undefined;
|
|
702
|
+
|
|
703
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
704
|
+
|
|
705
|
+
await expect(transport.call(uuid, 'FileWrite', {})).rejects.toMatchObject({
|
|
706
|
+
errorCode: HardwareErrorCode.BleConnectedError,
|
|
707
|
+
});
|
|
708
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(4);
|
|
709
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
710
|
+
await transport.release(uuid, true);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
test('reconnects before falling back to Protocol V1 after a fatal V2 probe failure', async () => {
|
|
714
|
+
setPlatformOS('android');
|
|
715
|
+
const { transport, uuid, device, notifySubscriptionRemovers, disconnectSubscriptionRemovers } =
|
|
716
|
+
createV1Harness();
|
|
717
|
+
const probeProtocolV2 = jest
|
|
718
|
+
.spyOn(transport as any, 'probeProtocolV2')
|
|
719
|
+
.mockImplementation(async () => {
|
|
720
|
+
await (transport as any).releaseNative(uuid, true);
|
|
721
|
+
return false;
|
|
722
|
+
});
|
|
723
|
+
const resolveCharacteristics = jest.spyOn(transport as any, 'resolveCharacteristics');
|
|
724
|
+
|
|
725
|
+
await expect(transport.acquire({ uuid, protocolHint: 'V2' })).resolves.toEqual({
|
|
726
|
+
uuid,
|
|
727
|
+
protocolType: 'V1',
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
expect(probeProtocolV2).toHaveBeenCalledTimes(1);
|
|
731
|
+
expect(resolveCharacteristics).toHaveBeenCalledTimes(2);
|
|
732
|
+
expect(transport.getProtocolType(uuid)).toBe('V1');
|
|
733
|
+
expect(device.onDisconnected).toHaveBeenCalledTimes(1);
|
|
734
|
+
expect(notifySubscriptionRemovers).toHaveLength(2);
|
|
735
|
+
expect(notifySubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
|
|
736
|
+
|
|
737
|
+
await transport.release(uuid, true);
|
|
738
|
+
|
|
739
|
+
expect(notifySubscriptionRemovers[1]).toHaveBeenCalledTimes(1);
|
|
740
|
+
expect(disconnectSubscriptionRemovers).toHaveLength(1);
|
|
741
|
+
expect(disconnectSubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
test('cleans the rebuilt transport when Protocol V1 fallback also fails', async () => {
|
|
745
|
+
setPlatformOS('android');
|
|
746
|
+
const { transport, uuid, device, bleManager, notifySubscriptionRemovers } = createV1Harness();
|
|
747
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockImplementation(async () => {
|
|
748
|
+
await (transport as any).releaseNative(uuid, true);
|
|
749
|
+
return false;
|
|
750
|
+
});
|
|
751
|
+
jest.spyOn(transport as any, 'probeProtocolV1').mockResolvedValue(false);
|
|
752
|
+
|
|
753
|
+
await expect(transport.acquire({ uuid, protocolHint: 'V2' })).rejects.toMatchObject({
|
|
754
|
+
errorCode: HardwareErrorCode.BleTimeoutError,
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
expect(device.onDisconnected).not.toHaveBeenCalled();
|
|
758
|
+
expect(notifySubscriptionRemovers).toHaveLength(2);
|
|
759
|
+
expect(notifySubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
|
|
760
|
+
expect(notifySubscriptionRemovers[1]).toHaveBeenCalledTimes(1);
|
|
761
|
+
expect(bleManager.cancelTransaction).toHaveBeenCalled();
|
|
762
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
test('disconnects and invalidates a Protocol V1 link after a response timeout', async () => {
|
|
766
|
+
const { transport, uuid, device } = createV1Harness({
|
|
767
|
+
respondOnWriteCount: Number.POSITIVE_INFINITY,
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
await transport.acquire({ uuid, expectedProtocol: 'V1' });
|
|
771
|
+
await expect(transport.call(uuid, 'Initialize', {}, { timeoutMs: 5 })).rejects.toMatchObject({
|
|
772
|
+
errorCode: HardwareErrorCode.BleTimeoutError,
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
expect(device.cancelConnection).toHaveBeenCalled();
|
|
776
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
afterEach(() => {
|
|
780
|
+
setPlatformOS('ios');
|
|
781
|
+
resetProtocolV2BleTuning();
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
test('uses the first Protocol V2 sequence for the first Core call when protocol is known', async () => {
|
|
170
785
|
const { transport, uuid, sentSeqs } = createHarness();
|
|
171
786
|
|
|
172
|
-
await transport.acquire({ uuid });
|
|
173
|
-
await transport.call(uuid, 'Ping', { message: '
|
|
787
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
788
|
+
await transport.call(uuid, 'Ping', { message: 'first-core-command' });
|
|
174
789
|
|
|
175
790
|
expect(sentSeqs).toEqual([1, 2]);
|
|
176
|
-
expect(bytesToHex(new Uint8Array([sentSeqs[0], sentSeqs[1]]))).toBe('0102');
|
|
177
791
|
await transport.release(uuid, true);
|
|
178
792
|
});
|
|
179
793
|
|
|
180
794
|
test('rejects the active Protocol V2 reader when the current monitor errors', async () => {
|
|
181
795
|
const harness = createHarness();
|
|
182
796
|
const { transport, uuid, sentSeqs } = harness;
|
|
183
|
-
await transport.acquire({ uuid });
|
|
797
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
184
798
|
harness.setShouldRespond(false);
|
|
185
799
|
|
|
186
800
|
const call = transport.call(uuid, 'Ping', { message: 'wait-for-monitor' }, { timeoutMs: 50 });
|
|
187
|
-
while (sentSeqs.length <
|
|
188
|
-
await Promise
|
|
801
|
+
while (sentSeqs.length < 1) {
|
|
802
|
+
await new Promise(resolve => {
|
|
803
|
+
setTimeout(resolve, 0);
|
|
804
|
+
});
|
|
189
805
|
}
|
|
190
806
|
await new Promise(resolve => {
|
|
191
807
|
setTimeout(resolve, 0);
|
|
@@ -200,44 +816,296 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
200
816
|
test('retains the sequence cursor when a new monitor generation is acquired', async () => {
|
|
201
817
|
const { transport, uuid, sentSeqs } = createHarness();
|
|
202
818
|
|
|
203
|
-
await transport.acquire({ uuid });
|
|
819
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
820
|
+
await transport.call(uuid, 'Ping', { message: 'first-generation' });
|
|
204
821
|
await transport.release(uuid, true);
|
|
205
|
-
await transport.acquire({ uuid });
|
|
822
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
823
|
+
await transport.call(uuid, 'Ping', { message: 'second-generation' });
|
|
206
824
|
|
|
207
|
-
expect(sentSeqs).toEqual([1, 2]);
|
|
825
|
+
expect(sentSeqs).toEqual([1, 2, 3, 4]);
|
|
208
826
|
await transport.release(uuid, true);
|
|
209
827
|
});
|
|
210
828
|
|
|
211
|
-
test('uses
|
|
829
|
+
test('uses withResponse for consecutive iOS Protocol V2 control calls without releasing', async () => {
|
|
212
830
|
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
213
831
|
|
|
214
|
-
await transport.acquire({ uuid });
|
|
215
|
-
|
|
216
|
-
expect(writeCharacteristic.
|
|
832
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
833
|
+
const releaseNative = jest.spyOn(transport as any, 'releaseNative');
|
|
834
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
835
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
|
|
836
|
+
|
|
837
|
+
await transport.call(uuid, 'DeviceInfoGet', {});
|
|
838
|
+
await transport.call(uuid, 'ProtocolInfoRequest', {});
|
|
839
|
+
|
|
840
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(3);
|
|
841
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
842
|
+
expect(releaseNative).not.toHaveBeenCalled();
|
|
217
843
|
|
|
218
|
-
await transport.
|
|
844
|
+
await transport.release(uuid, true);
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
test('keeps iOS Protocol V2 high-volume calls on withoutResponse', async () => {
|
|
848
|
+
const { transport, uuid, logger, writeCharacteristic } = createHarness();
|
|
849
|
+
|
|
850
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
851
|
+
|
|
852
|
+
await transport.call(uuid, 'FileWrite', {});
|
|
853
|
+
await transport.call(uuid, 'FileWrite', {});
|
|
219
854
|
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(2);
|
|
220
|
-
expect(writeCharacteristic.writeWithResponse).
|
|
855
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
|
|
856
|
+
expect(
|
|
857
|
+
logger.debug.mock.calls.filter(
|
|
858
|
+
([message]) =>
|
|
859
|
+
message === '[ReactNativeBleTransport] Protocol V2 high-volume write configured'
|
|
860
|
+
)
|
|
861
|
+
).toHaveLength(1);
|
|
862
|
+
await transport.release(uuid, true);
|
|
863
|
+
});
|
|
864
|
+
|
|
865
|
+
test('uses Android 517 MTU and high connection priority during Protocol V2 high-volume calls', async () => {
|
|
866
|
+
setPlatformOS('android');
|
|
867
|
+
const { transport, uuid, device } = createHarness();
|
|
868
|
+
|
|
869
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
870
|
+
expect(device.requestMTU).toHaveBeenCalledWith(517);
|
|
221
871
|
|
|
222
872
|
await transport.call(uuid, 'FileWrite', {});
|
|
223
|
-
|
|
873
|
+
await transport.call(uuid, 'FileWrite', {});
|
|
874
|
+
|
|
875
|
+
expect(device.requestConnectionPriority).toHaveBeenCalledTimes(1);
|
|
876
|
+
expect(device.requestConnectionPriority).toHaveBeenCalledWith(1);
|
|
877
|
+
|
|
878
|
+
await transport.release(uuid, true);
|
|
879
|
+
expect(device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
test('uses withResponse for an iOS Protocol V2 firmware file write when requested', async () => {
|
|
883
|
+
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
884
|
+
|
|
885
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
886
|
+
|
|
887
|
+
await transport.call(uuid, 'FileWrite', {}, { writeWithResponse: true });
|
|
888
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(2);
|
|
889
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
890
|
+
await transport.release(uuid, true);
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
test('falls back to withoutResponse for an iOS Protocol V2 control call when required', async () => {
|
|
894
|
+
const { transport, uuid, writeCharacteristic } = createHarness({
|
|
895
|
+
isWritableWithResponse: false,
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
899
|
+
await transport.call(uuid, 'ProtocolInfoRequest', {});
|
|
900
|
+
|
|
224
901
|
expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
|
|
902
|
+
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(2);
|
|
225
903
|
await transport.release(uuid, true);
|
|
226
904
|
});
|
|
227
905
|
|
|
906
|
+
test('does not resend a failed iOS Protocol V2 control write without response', async () => {
|
|
907
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
908
|
+
const writeError = new Error('write with response failed');
|
|
909
|
+
const writeWithResponse = jest.fn().mockRejectedValue(writeError);
|
|
910
|
+
const writeWithoutResponse = jest.fn().mockResolvedValue(undefined);
|
|
911
|
+
const context = {
|
|
912
|
+
messageName: 'ProtocolInfoRequest',
|
|
913
|
+
timeoutMs: 1000,
|
|
914
|
+
highThroughput: false,
|
|
915
|
+
generation: 1,
|
|
916
|
+
signal: new AbortController().signal,
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
await expect(
|
|
920
|
+
transport.writeProtocolV2Packet(
|
|
921
|
+
'test-device',
|
|
922
|
+
{
|
|
923
|
+
writeCharacteristic: {
|
|
924
|
+
isWritableWithResponse: true,
|
|
925
|
+
writeWithResponse,
|
|
926
|
+
writeWithoutResponse,
|
|
927
|
+
},
|
|
928
|
+
},
|
|
929
|
+
Buffer.from('control').toString('base64'),
|
|
930
|
+
context,
|
|
931
|
+
jest.fn()
|
|
932
|
+
)
|
|
933
|
+
).rejects.toBe(writeError);
|
|
934
|
+
expect(writeWithResponse).toHaveBeenCalledTimes(1);
|
|
935
|
+
expect(writeWithoutResponse).not.toHaveBeenCalled();
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
test('does not pace a one-packet Protocol V2 control write on iOS', async () => {
|
|
939
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
940
|
+
const writeWithoutResponse = jest.fn().mockResolvedValue(undefined);
|
|
941
|
+
const bleTransport = {
|
|
942
|
+
mtuSize: 23,
|
|
943
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
944
|
+
};
|
|
945
|
+
const context = {
|
|
946
|
+
messageName: 'ProtocolInfoRequest',
|
|
947
|
+
timeoutMs: 1000,
|
|
948
|
+
highThroughput: false,
|
|
949
|
+
generation: 1,
|
|
950
|
+
signal: new AbortController().signal,
|
|
951
|
+
};
|
|
952
|
+
configureProtocolV2BleTuning({ iosPacketLength: 20 });
|
|
953
|
+
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
|
|
954
|
+
|
|
955
|
+
try {
|
|
956
|
+
const call = transport.writeProtocolV2Frame(
|
|
957
|
+
'test-device',
|
|
958
|
+
bleTransport,
|
|
959
|
+
new Uint8Array(10),
|
|
960
|
+
context,
|
|
961
|
+
jest.fn()
|
|
962
|
+
);
|
|
963
|
+
|
|
964
|
+
await call;
|
|
965
|
+
// The only scheduled timer is the per-packet BLE write watchdog: no pacing delay.
|
|
966
|
+
expect(setTimeoutSpy.mock.calls.map(([, timeout]) => timeout)).toEqual([
|
|
967
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
968
|
+
]);
|
|
969
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
970
|
+
} finally {
|
|
971
|
+
setTimeoutSpy.mockRestore();
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
|
|
228
975
|
test('rejects an active Protocol V2 reader when disconnect resets the link', async () => {
|
|
229
976
|
const harness = createHarness();
|
|
230
977
|
const { transport, uuid, sentSeqs } = harness;
|
|
231
|
-
await transport.acquire({ uuid });
|
|
978
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
232
979
|
harness.setShouldRespond(false);
|
|
233
980
|
|
|
234
981
|
const call = transport.call(uuid, 'Ping', { message: 'disconnect' }, { timeoutMs: 50 });
|
|
235
|
-
while (sentSeqs.length <
|
|
236
|
-
await Promise
|
|
982
|
+
while (sentSeqs.length < 1) {
|
|
983
|
+
await new Promise(resolve => {
|
|
984
|
+
setTimeout(resolve, 0);
|
|
985
|
+
});
|
|
237
986
|
}
|
|
238
987
|
|
|
239
988
|
const rejection = expect(call).rejects.toThrow('React Native BLE transport disconnected');
|
|
240
989
|
await transport.disconnect(uuid);
|
|
241
990
|
await rejection;
|
|
242
991
|
});
|
|
992
|
+
|
|
993
|
+
test('emits one disconnect event when the physical callback races manual cleanup', async () => {
|
|
994
|
+
const harness = createHarness();
|
|
995
|
+
const disconnectListener = jest.fn();
|
|
996
|
+
harness.emitter.on(TRANSPORT_EVENT.DEVICE_DISCONNECT, disconnectListener);
|
|
997
|
+
|
|
998
|
+
await harness.transport.acquire({ uuid: harness.uuid, expectedProtocol: 'V2' });
|
|
999
|
+
harness.emitDisconnect();
|
|
1000
|
+
await harness.transport.disconnect(harness.uuid);
|
|
1001
|
+
|
|
1002
|
+
expect(disconnectListener).toHaveBeenCalledTimes(1);
|
|
1003
|
+
expect(disconnectListener).toHaveBeenCalledWith({
|
|
1004
|
+
name: 'OneKey Pro 2',
|
|
1005
|
+
id: harness.uuid,
|
|
1006
|
+
connectId: harness.uuid,
|
|
1007
|
+
});
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
test('chunks large frames and retries only transient GATT congestion', async () => {
|
|
1011
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
1012
|
+
const congested = { status: 143, message: 'GATT_CONGESTED' };
|
|
1013
|
+
const writeWithoutResponse = jest
|
|
1014
|
+
.fn()
|
|
1015
|
+
.mockRejectedValueOnce(congested)
|
|
1016
|
+
.mockResolvedValue(undefined);
|
|
1017
|
+
const bleTransport = {
|
|
1018
|
+
mtuSize: 23,
|
|
1019
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
1020
|
+
};
|
|
1021
|
+
const context = {
|
|
1022
|
+
messageName: 'Ping',
|
|
1023
|
+
timeoutMs: 1000,
|
|
1024
|
+
highThroughput: false,
|
|
1025
|
+
generation: 1,
|
|
1026
|
+
signal: new AbortController().signal,
|
|
1027
|
+
};
|
|
1028
|
+
const assertCurrentGeneration = jest.fn();
|
|
1029
|
+
configureProtocolV2BleTuning({ iosPacketLength: 20 });
|
|
1030
|
+
|
|
1031
|
+
await transport.writeProtocolV2Frame(
|
|
1032
|
+
'device-uuid',
|
|
1033
|
+
bleTransport,
|
|
1034
|
+
new Uint8Array(30),
|
|
1035
|
+
context,
|
|
1036
|
+
assertCurrentGeneration
|
|
1037
|
+
);
|
|
1038
|
+
|
|
1039
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(3);
|
|
1040
|
+
expect(writeWithoutResponse.mock.calls[0][0]).toBe(writeWithoutResponse.mock.calls[1][0]);
|
|
1041
|
+
expect(Buffer.from(writeWithoutResponse.mock.calls[2][0], 'base64')).toHaveLength(10);
|
|
1042
|
+
expect(assertCurrentGeneration).toHaveBeenCalled();
|
|
1043
|
+
});
|
|
1044
|
+
|
|
1045
|
+
test('does not retry a disconnected Protocol V2 write inside a partial frame', async () => {
|
|
1046
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
1047
|
+
const writeWithoutResponse = jest
|
|
1048
|
+
.fn()
|
|
1049
|
+
.mockRejectedValue({ errorCode: 205, message: 'Device disconnected' });
|
|
1050
|
+
const bleTransport = {
|
|
1051
|
+
mtuSize: 23,
|
|
1052
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
1053
|
+
};
|
|
1054
|
+
const context = {
|
|
1055
|
+
messageName: 'FileWrite',
|
|
1056
|
+
timeoutMs: 1000,
|
|
1057
|
+
highThroughput: true,
|
|
1058
|
+
generation: 1,
|
|
1059
|
+
signal: new AbortController().signal,
|
|
1060
|
+
};
|
|
1061
|
+
configureProtocolV2BleTuning({ iosPacketLength: 20 });
|
|
1062
|
+
|
|
1063
|
+
await expect(
|
|
1064
|
+
transport.writeProtocolV2Frame(
|
|
1065
|
+
'device-uuid',
|
|
1066
|
+
bleTransport,
|
|
1067
|
+
new Uint8Array(30),
|
|
1068
|
+
context,
|
|
1069
|
+
jest.fn()
|
|
1070
|
+
)
|
|
1071
|
+
).rejects.toMatchObject({ errorCode: 205 });
|
|
1072
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
1073
|
+
});
|
|
1074
|
+
|
|
1075
|
+
test('does not apply fixed burst or flush pauses to high-volume Protocol V2 writes', async () => {
|
|
1076
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
1077
|
+
const writeWithoutResponse = jest.fn().mockResolvedValue(undefined);
|
|
1078
|
+
const bleTransport = {
|
|
1079
|
+
mtuSize: 247,
|
|
1080
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
1081
|
+
};
|
|
1082
|
+
const context = {
|
|
1083
|
+
messageName: 'FileWrite',
|
|
1084
|
+
timeoutMs: 1000,
|
|
1085
|
+
highThroughput: true,
|
|
1086
|
+
generation: 1,
|
|
1087
|
+
signal: new AbortController().signal,
|
|
1088
|
+
};
|
|
1089
|
+
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
|
|
1090
|
+
|
|
1091
|
+
try {
|
|
1092
|
+
await transport.writeProtocolV2Frame(
|
|
1093
|
+
'test-device',
|
|
1094
|
+
bleTransport,
|
|
1095
|
+
new Uint8Array(600),
|
|
1096
|
+
context,
|
|
1097
|
+
jest.fn()
|
|
1098
|
+
);
|
|
1099
|
+
|
|
1100
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(3);
|
|
1101
|
+
// One BLE write watchdog per packet and nothing else: no burst or flush pauses.
|
|
1102
|
+
expect(setTimeoutSpy.mock.calls.map(([, timeout]) => timeout)).toEqual([
|
|
1103
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
1104
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
1105
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
1106
|
+
]);
|
|
1107
|
+
} finally {
|
|
1108
|
+
setTimeoutSpy.mockRestore();
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
243
1111
|
});
|