@onekeyfe/hd-transport-react-native 1.2.0-alpha.15 → 1.2.0-alpha.151
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 +128 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +902 -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 +882 -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 +1182 -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,586 @@ 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.each(['ios', 'android'] as const)(
|
|
428
|
+
'keeps a first expected Protocol V2 probe miss retryable on %s',
|
|
429
|
+
async platform => {
|
|
430
|
+
setPlatformOS(platform);
|
|
431
|
+
const { transport, uuid, device } = createHarness();
|
|
432
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
433
|
+
|
|
434
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
435
|
+
errorCode: HardwareErrorCode.RuntimeError,
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
expect(device.cancelConnection).not.toHaveBeenCalled();
|
|
439
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
440
|
+
}
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
test.each(['ios', 'android'] as const)(
|
|
444
|
+
'keeps a second expected Protocol V2 probe miss retryable on %s',
|
|
445
|
+
async platform => {
|
|
446
|
+
setPlatformOS(platform);
|
|
447
|
+
const { transport, uuid, device } = createHarness();
|
|
448
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
449
|
+
|
|
450
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
451
|
+
errorCode: HardwareErrorCode.RuntimeError,
|
|
452
|
+
});
|
|
453
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
454
|
+
errorCode: HardwareErrorCode.RuntimeError,
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
expect(device.cancelConnection).not.toHaveBeenCalled();
|
|
458
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
459
|
+
}
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
test.each(['ios', 'android'] as const)(
|
|
463
|
+
'reports a stale bond on %s when a previously confirmed Protocol V2 device stops responding',
|
|
464
|
+
async platform => {
|
|
465
|
+
setPlatformOS(platform);
|
|
466
|
+
const { transport, uuid, device } = createHarness();
|
|
467
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
468
|
+
await transport.release(uuid, true);
|
|
469
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
470
|
+
|
|
471
|
+
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
472
|
+
errorCode: HardwareErrorCode.BleDeviceBondError,
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
expect(device.cancelConnection).toHaveBeenCalled();
|
|
476
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
477
|
+
}
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
test('waits for an in-flight release before reacquiring the same device', async () => {
|
|
481
|
+
const { transport, uuid } = createHarness();
|
|
482
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
483
|
+
const releaseGate = createDeferred<void>();
|
|
484
|
+
const releaseStarted = createDeferred<void>();
|
|
485
|
+
const { protocolV2Links } = transport as any;
|
|
486
|
+
const invalidateLink = protocolV2Links.invalidateLink.bind(protocolV2Links);
|
|
487
|
+
jest
|
|
488
|
+
.spyOn(protocolV2Links, 'invalidateLink')
|
|
489
|
+
.mockImplementationOnce(async (...args: unknown[]) => {
|
|
490
|
+
releaseStarted.resolve();
|
|
491
|
+
await releaseGate.promise;
|
|
492
|
+
return invalidateLink(...args);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
const release = transport.release(uuid, true);
|
|
496
|
+
await releaseStarted.promise;
|
|
497
|
+
let reacquired = false;
|
|
498
|
+
const acquire = transport.acquire({ uuid, expectedProtocol: 'V2' }).then(result => {
|
|
499
|
+
reacquired = true;
|
|
500
|
+
return result;
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
await Promise.resolve();
|
|
504
|
+
expect(reacquired).toBe(false);
|
|
505
|
+
|
|
506
|
+
releaseGate.resolve();
|
|
507
|
+
await release;
|
|
508
|
+
await expect(acquire).resolves.toEqual({ uuid, protocolType: 'V2' });
|
|
509
|
+
await expect(transport.call(uuid, 'Ping', { message: 'after-release' })).resolves.toMatchObject(
|
|
510
|
+
{
|
|
511
|
+
type: 'Success',
|
|
512
|
+
message: { message: 'ok' },
|
|
513
|
+
}
|
|
514
|
+
);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test(
|
|
518
|
+
'releases the lifecycle queue when native teardown never settles',
|
|
519
|
+
async () => {
|
|
520
|
+
const { transport, uuid, device, bleManager } = createHarness();
|
|
521
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
522
|
+
const otherUuid = 'rn-pro2-other-id';
|
|
523
|
+
const otherDevice = {
|
|
524
|
+
...device,
|
|
525
|
+
id: otherUuid,
|
|
526
|
+
name: 'OneKey Pro 2 Other',
|
|
527
|
+
localName: 'OneKey Pro 2 Other',
|
|
528
|
+
};
|
|
529
|
+
await (transport as any).installTransportForAcquire(otherUuid, otherDevice);
|
|
530
|
+
(transport as any).deviceProtocol.set(otherUuid, 'V2');
|
|
531
|
+
const disconnectEvents: Array<{ connectId: string }> = [];
|
|
532
|
+
transport.emitter?.on(TRANSPORT_EVENT.DEVICE_DISCONNECT, event => {
|
|
533
|
+
disconnectEvents.push(event);
|
|
534
|
+
});
|
|
535
|
+
const stalledNativeCleanup = createDeferred<void>();
|
|
536
|
+
bleManager.cancelTransaction
|
|
537
|
+
.mockImplementationOnce(() => stalledNativeCleanup.promise)
|
|
538
|
+
.mockResolvedValue(undefined);
|
|
539
|
+
const resetPlxManager = jest.spyOn(transport as any, 'resetPlxManager');
|
|
540
|
+
const nextLifecycleOperation = jest.fn().mockResolvedValue('next-operation');
|
|
541
|
+
|
|
542
|
+
const release = transport.release(uuid, true);
|
|
543
|
+
const nextOperation = (transport as any).runLifecycleOperation(uuid, nextLifecycleOperation);
|
|
544
|
+
|
|
545
|
+
await expect(release).resolves.toBe(true);
|
|
546
|
+
await expect(nextOperation).resolves.toBe('next-operation');
|
|
547
|
+
expect(resetPlxManager).toHaveBeenCalledTimes(1);
|
|
548
|
+
expect(nextLifecycleOperation).toHaveBeenCalledTimes(1);
|
|
549
|
+
expect(disconnectEvents).toContainEqual(expect.objectContaining({ connectId: otherUuid }));
|
|
550
|
+
expect(() => (transport as any).getCachedTransport(otherUuid)).toThrow();
|
|
551
|
+
|
|
552
|
+
await (transport as any).installTransportForAcquire(uuid, device);
|
|
553
|
+
(transport as any).deviceProtocol.set(uuid, 'V2');
|
|
554
|
+
|
|
555
|
+
stalledNativeCleanup.resolve();
|
|
556
|
+
await Promise.resolve();
|
|
557
|
+
await expect(
|
|
558
|
+
transport.call(uuid, 'Ping', { message: 'after-stale-cleanup' })
|
|
559
|
+
).resolves.toMatchObject({
|
|
560
|
+
type: 'Success',
|
|
561
|
+
message: { message: 'ok' },
|
|
562
|
+
});
|
|
563
|
+
},
|
|
564
|
+
BLE_NATIVE_TEARDOWN_TIMEOUT_MS + 5_000
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
test('falls back to the other active probe on iOS when protocol metadata is absent', async () => {
|
|
568
|
+
const { transport, uuid } = createHarness({ deviceName: 'OneKey' });
|
|
569
|
+
const probeProtocolV1 = jest
|
|
570
|
+
.spyOn(transport as any, 'probeProtocolV1')
|
|
571
|
+
.mockResolvedValue(false);
|
|
572
|
+
const probeProtocolV2 = jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(true);
|
|
573
|
+
|
|
574
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
575
|
+
uuid,
|
|
576
|
+
protocolType: 'V2',
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
expect(probeProtocolV1).toHaveBeenCalledTimes(1);
|
|
580
|
+
expect(probeProtocolV2).toHaveBeenCalledTimes(1);
|
|
581
|
+
expect(probeProtocolV1.mock.invocationCallOrder[0]).toBeLessThan(
|
|
582
|
+
probeProtocolV2.mock.invocationCallOrder[0]
|
|
583
|
+
);
|
|
584
|
+
await transport.release(uuid, true);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
test('continues with the current MTU when the connected snapshot refresh fails', async () => {
|
|
588
|
+
const { transport, uuid, device } = createHarness();
|
|
589
|
+
const mtuError = new Error('MTU refresh failed');
|
|
590
|
+
device.requestMTU.mockRejectedValueOnce(mtuError);
|
|
591
|
+
|
|
592
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
593
|
+
uuid,
|
|
594
|
+
protocolType: 'V2',
|
|
595
|
+
});
|
|
596
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(1);
|
|
597
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(247);
|
|
598
|
+
await transport.release(uuid, true);
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
test('refreshes a transient bootloader MTU after notifications are ready', async () => {
|
|
602
|
+
const { transport, uuid, device } = createHarness();
|
|
603
|
+
device.mtu = 23;
|
|
604
|
+
device.requestMTU
|
|
605
|
+
.mockResolvedValueOnce(device)
|
|
606
|
+
.mockResolvedValueOnce(device)
|
|
607
|
+
.mockImplementationOnce(() => {
|
|
608
|
+
device.mtu = 247;
|
|
609
|
+
return Promise.resolve(device);
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
613
|
+
uuid,
|
|
614
|
+
protocolType: 'V2',
|
|
615
|
+
});
|
|
616
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(3);
|
|
617
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(247);
|
|
618
|
+
await transport.release(uuid, true);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test('continues with a low bootloader MTU when the bounded retry fails', async () => {
|
|
622
|
+
const { transport, uuid, device } = createHarness();
|
|
623
|
+
device.mtu = 23;
|
|
624
|
+
device.requestMTU
|
|
625
|
+
.mockResolvedValueOnce(device)
|
|
626
|
+
.mockResolvedValueOnce(device)
|
|
627
|
+
.mockRejectedValueOnce(new Error('bootloader MTU retry failed'));
|
|
628
|
+
|
|
629
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
630
|
+
uuid,
|
|
631
|
+
protocolType: 'V2',
|
|
632
|
+
});
|
|
633
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(3);
|
|
634
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(23);
|
|
635
|
+
await transport.release(uuid, true);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
test('accepts a stable low MTU without the delayed refresh loop', async () => {
|
|
639
|
+
const { transport, uuid, device } = createHarness();
|
|
640
|
+
device.mtu = 185;
|
|
641
|
+
|
|
642
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
643
|
+
uuid,
|
|
644
|
+
protocolType: 'V2',
|
|
645
|
+
});
|
|
646
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(1);
|
|
647
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBe(185);
|
|
648
|
+
await transport.release(uuid, true);
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
test('continues Protocol V2 probing with a conservative packet size when MTU is unavailable', async () => {
|
|
652
|
+
const { transport, uuid, device, writeCharacteristic } = createHarness();
|
|
653
|
+
device.mtu = undefined;
|
|
654
|
+
|
|
655
|
+
await expect(transport.acquire({ uuid })).resolves.toEqual({
|
|
656
|
+
uuid,
|
|
657
|
+
protocolType: 'V2',
|
|
658
|
+
});
|
|
659
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(3);
|
|
660
|
+
expect((transport as any).getCachedTransport(uuid).mtuSize).toBeUndefined();
|
|
661
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalled();
|
|
662
|
+
await transport.release(uuid, true);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
test('refreshes an unavailable MTU before a Protocol V2 high-volume write', async () => {
|
|
666
|
+
const { transport, uuid, device, writeCharacteristic } = createHarness();
|
|
667
|
+
device.mtu = undefined;
|
|
668
|
+
|
|
669
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
670
|
+
device.requestMTU.mockImplementationOnce(() => {
|
|
671
|
+
device.mtu = 247;
|
|
672
|
+
return Promise.resolve(device);
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
await expect(transport.call(uuid, 'FileWrite', {})).resolves.toBeDefined();
|
|
676
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(4);
|
|
677
|
+
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
678
|
+
await transport.release(uuid, true);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
test('rejects a Protocol V2 high-volume write when MTU remains unavailable', async () => {
|
|
682
|
+
const { transport, uuid, device, writeCharacteristic } = createHarness();
|
|
683
|
+
device.mtu = undefined;
|
|
684
|
+
|
|
685
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
686
|
+
|
|
687
|
+
await expect(transport.call(uuid, 'FileWrite', {})).rejects.toMatchObject({
|
|
688
|
+
errorCode: HardwareErrorCode.BleConnectedError,
|
|
689
|
+
});
|
|
690
|
+
expect(device.requestMTU).toHaveBeenCalledTimes(4);
|
|
691
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
692
|
+
await transport.release(uuid, true);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
test('reconnects before falling back to Protocol V1 after a fatal V2 probe failure', async () => {
|
|
696
|
+
setPlatformOS('android');
|
|
697
|
+
const { transport, uuid, device, notifySubscriptionRemovers, disconnectSubscriptionRemovers } =
|
|
698
|
+
createV1Harness();
|
|
699
|
+
const probeProtocolV2 = jest
|
|
700
|
+
.spyOn(transport as any, 'probeProtocolV2')
|
|
701
|
+
.mockImplementation(async () => {
|
|
702
|
+
await (transport as any).releaseNative(uuid, true);
|
|
703
|
+
return false;
|
|
704
|
+
});
|
|
705
|
+
const resolveCharacteristics = jest.spyOn(transport as any, 'resolveCharacteristics');
|
|
706
|
+
|
|
707
|
+
await expect(transport.acquire({ uuid, protocolHint: 'V2' })).resolves.toEqual({
|
|
708
|
+
uuid,
|
|
709
|
+
protocolType: 'V1',
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
expect(probeProtocolV2).toHaveBeenCalledTimes(1);
|
|
713
|
+
expect(resolveCharacteristics).toHaveBeenCalledTimes(2);
|
|
714
|
+
expect(transport.getProtocolType(uuid)).toBe('V1');
|
|
715
|
+
expect(device.onDisconnected).toHaveBeenCalledTimes(1);
|
|
716
|
+
expect(notifySubscriptionRemovers).toHaveLength(2);
|
|
717
|
+
expect(notifySubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
|
|
718
|
+
|
|
719
|
+
await transport.release(uuid, true);
|
|
720
|
+
|
|
721
|
+
expect(notifySubscriptionRemovers[1]).toHaveBeenCalledTimes(1);
|
|
722
|
+
expect(disconnectSubscriptionRemovers).toHaveLength(1);
|
|
723
|
+
expect(disconnectSubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
test('cleans the rebuilt transport when Protocol V1 fallback also fails', async () => {
|
|
727
|
+
setPlatformOS('android');
|
|
728
|
+
const { transport, uuid, device, bleManager, notifySubscriptionRemovers } = createV1Harness();
|
|
729
|
+
jest.spyOn(transport as any, 'probeProtocolV2').mockImplementation(async () => {
|
|
730
|
+
await (transport as any).releaseNative(uuid, true);
|
|
731
|
+
return false;
|
|
732
|
+
});
|
|
733
|
+
jest.spyOn(transport as any, 'probeProtocolV1').mockResolvedValue(false);
|
|
734
|
+
|
|
735
|
+
await expect(transport.acquire({ uuid, protocolHint: 'V2' })).rejects.toMatchObject({
|
|
736
|
+
errorCode: HardwareErrorCode.BleTimeoutError,
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
expect(device.onDisconnected).not.toHaveBeenCalled();
|
|
740
|
+
expect(notifySubscriptionRemovers).toHaveLength(2);
|
|
741
|
+
expect(notifySubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
|
|
742
|
+
expect(notifySubscriptionRemovers[1]).toHaveBeenCalledTimes(1);
|
|
743
|
+
expect(bleManager.cancelTransaction).toHaveBeenCalled();
|
|
744
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
test('disconnects and invalidates a Protocol V1 link after a response timeout', async () => {
|
|
748
|
+
const { transport, uuid, device } = createV1Harness({
|
|
749
|
+
respondOnWriteCount: Number.POSITIVE_INFINITY,
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
await transport.acquire({ uuid, expectedProtocol: 'V1' });
|
|
753
|
+
await expect(transport.call(uuid, 'Initialize', {}, { timeoutMs: 5 })).rejects.toMatchObject({
|
|
754
|
+
errorCode: HardwareErrorCode.BleTimeoutError,
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
expect(device.cancelConnection).toHaveBeenCalled();
|
|
758
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
afterEach(() => {
|
|
762
|
+
setPlatformOS('ios');
|
|
763
|
+
resetProtocolV2BleTuning();
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
test('uses the first Protocol V2 sequence for the first Core call when protocol is known', async () => {
|
|
170
767
|
const { transport, uuid, sentSeqs } = createHarness();
|
|
171
768
|
|
|
172
|
-
await transport.acquire({ uuid });
|
|
173
|
-
await transport.call(uuid, 'Ping', { message: '
|
|
769
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
770
|
+
await transport.call(uuid, 'Ping', { message: 'first-core-command' });
|
|
174
771
|
|
|
175
772
|
expect(sentSeqs).toEqual([1, 2]);
|
|
176
|
-
expect(bytesToHex(new Uint8Array([sentSeqs[0], sentSeqs[1]]))).toBe('0102');
|
|
177
773
|
await transport.release(uuid, true);
|
|
178
774
|
});
|
|
179
775
|
|
|
180
776
|
test('rejects the active Protocol V2 reader when the current monitor errors', async () => {
|
|
181
777
|
const harness = createHarness();
|
|
182
778
|
const { transport, uuid, sentSeqs } = harness;
|
|
183
|
-
await transport.acquire({ uuid });
|
|
779
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
184
780
|
harness.setShouldRespond(false);
|
|
185
781
|
|
|
186
782
|
const call = transport.call(uuid, 'Ping', { message: 'wait-for-monitor' }, { timeoutMs: 50 });
|
|
187
|
-
while (sentSeqs.length <
|
|
188
|
-
await Promise
|
|
783
|
+
while (sentSeqs.length < 1) {
|
|
784
|
+
await new Promise(resolve => {
|
|
785
|
+
setTimeout(resolve, 0);
|
|
786
|
+
});
|
|
189
787
|
}
|
|
190
788
|
await new Promise(resolve => {
|
|
191
789
|
setTimeout(resolve, 0);
|
|
@@ -200,44 +798,296 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
200
798
|
test('retains the sequence cursor when a new monitor generation is acquired', async () => {
|
|
201
799
|
const { transport, uuid, sentSeqs } = createHarness();
|
|
202
800
|
|
|
203
|
-
await transport.acquire({ uuid });
|
|
801
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
802
|
+
await transport.call(uuid, 'Ping', { message: 'first-generation' });
|
|
204
803
|
await transport.release(uuid, true);
|
|
205
|
-
await transport.acquire({ uuid });
|
|
804
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
805
|
+
await transport.call(uuid, 'Ping', { message: 'second-generation' });
|
|
206
806
|
|
|
207
|
-
expect(sentSeqs).toEqual([1, 2]);
|
|
807
|
+
expect(sentSeqs).toEqual([1, 2, 3, 4]);
|
|
208
808
|
await transport.release(uuid, true);
|
|
209
809
|
});
|
|
210
810
|
|
|
211
|
-
test('uses
|
|
811
|
+
test('uses withResponse for consecutive iOS Protocol V2 control calls without releasing', async () => {
|
|
212
812
|
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
213
813
|
|
|
214
|
-
await transport.acquire({ uuid });
|
|
215
|
-
|
|
216
|
-
expect(writeCharacteristic.
|
|
814
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
815
|
+
const releaseNative = jest.spyOn(transport as any, 'releaseNative');
|
|
816
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
817
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
|
|
818
|
+
|
|
819
|
+
await transport.call(uuid, 'DeviceInfoGet', {});
|
|
820
|
+
await transport.call(uuid, 'ProtocolInfoRequest', {});
|
|
821
|
+
|
|
822
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(3);
|
|
823
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
824
|
+
expect(releaseNative).not.toHaveBeenCalled();
|
|
825
|
+
|
|
826
|
+
await transport.release(uuid, true);
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
test('keeps iOS Protocol V2 high-volume calls on withoutResponse', async () => {
|
|
830
|
+
const { transport, uuid, logger, writeCharacteristic } = createHarness();
|
|
831
|
+
|
|
832
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
217
833
|
|
|
218
|
-
await transport.call(uuid, '
|
|
834
|
+
await transport.call(uuid, 'FileWrite', {});
|
|
835
|
+
await transport.call(uuid, 'FileWrite', {});
|
|
219
836
|
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(2);
|
|
220
|
-
expect(writeCharacteristic.writeWithResponse).
|
|
837
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
|
|
838
|
+
expect(
|
|
839
|
+
logger.debug.mock.calls.filter(
|
|
840
|
+
([message]) =>
|
|
841
|
+
message === '[ReactNativeBleTransport] Protocol V2 high-volume write configured'
|
|
842
|
+
)
|
|
843
|
+
).toHaveLength(1);
|
|
844
|
+
await transport.release(uuid, true);
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
test('uses Android 517 MTU and high connection priority during Protocol V2 high-volume calls', async () => {
|
|
848
|
+
setPlatformOS('android');
|
|
849
|
+
const { transport, uuid, device } = createHarness();
|
|
221
850
|
|
|
851
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
852
|
+
expect(device.requestMTU).toHaveBeenCalledWith(517);
|
|
853
|
+
|
|
854
|
+
await transport.call(uuid, 'FileWrite', {});
|
|
222
855
|
await transport.call(uuid, 'FileWrite', {});
|
|
223
|
-
|
|
856
|
+
|
|
857
|
+
expect(device.requestConnectionPriority).toHaveBeenCalledTimes(1);
|
|
858
|
+
expect(device.requestConnectionPriority).toHaveBeenCalledWith(1);
|
|
859
|
+
|
|
860
|
+
await transport.release(uuid, true);
|
|
861
|
+
expect(device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
test('uses withResponse for an iOS Protocol V2 firmware file write when requested', async () => {
|
|
865
|
+
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
866
|
+
|
|
867
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
868
|
+
|
|
869
|
+
await transport.call(uuid, 'FileWrite', {}, { writeWithResponse: true });
|
|
870
|
+
expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(2);
|
|
871
|
+
expect(writeCharacteristic.writeWithoutResponse).not.toHaveBeenCalled();
|
|
872
|
+
await transport.release(uuid, true);
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
test('falls back to withoutResponse for an iOS Protocol V2 control call when required', async () => {
|
|
876
|
+
const { transport, uuid, writeCharacteristic } = createHarness({
|
|
877
|
+
isWritableWithResponse: false,
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
881
|
+
await transport.call(uuid, 'ProtocolInfoRequest', {});
|
|
882
|
+
|
|
224
883
|
expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
|
|
884
|
+
expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(2);
|
|
225
885
|
await transport.release(uuid, true);
|
|
226
886
|
});
|
|
227
887
|
|
|
888
|
+
test('does not resend a failed iOS Protocol V2 control write without response', async () => {
|
|
889
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
890
|
+
const writeError = new Error('write with response failed');
|
|
891
|
+
const writeWithResponse = jest.fn().mockRejectedValue(writeError);
|
|
892
|
+
const writeWithoutResponse = jest.fn().mockResolvedValue(undefined);
|
|
893
|
+
const context = {
|
|
894
|
+
messageName: 'ProtocolInfoRequest',
|
|
895
|
+
timeoutMs: 1000,
|
|
896
|
+
highThroughput: false,
|
|
897
|
+
generation: 1,
|
|
898
|
+
signal: new AbortController().signal,
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
await expect(
|
|
902
|
+
transport.writeProtocolV2Packet(
|
|
903
|
+
'test-device',
|
|
904
|
+
{
|
|
905
|
+
writeCharacteristic: {
|
|
906
|
+
isWritableWithResponse: true,
|
|
907
|
+
writeWithResponse,
|
|
908
|
+
writeWithoutResponse,
|
|
909
|
+
},
|
|
910
|
+
},
|
|
911
|
+
Buffer.from('control').toString('base64'),
|
|
912
|
+
context,
|
|
913
|
+
jest.fn()
|
|
914
|
+
)
|
|
915
|
+
).rejects.toBe(writeError);
|
|
916
|
+
expect(writeWithResponse).toHaveBeenCalledTimes(1);
|
|
917
|
+
expect(writeWithoutResponse).not.toHaveBeenCalled();
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
test('does not pace a one-packet Protocol V2 control write on iOS', async () => {
|
|
921
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
922
|
+
const writeWithoutResponse = jest.fn().mockResolvedValue(undefined);
|
|
923
|
+
const bleTransport = {
|
|
924
|
+
mtuSize: 23,
|
|
925
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
926
|
+
};
|
|
927
|
+
const context = {
|
|
928
|
+
messageName: 'ProtocolInfoRequest',
|
|
929
|
+
timeoutMs: 1000,
|
|
930
|
+
highThroughput: false,
|
|
931
|
+
generation: 1,
|
|
932
|
+
signal: new AbortController().signal,
|
|
933
|
+
};
|
|
934
|
+
configureProtocolV2BleTuning({ iosPacketLength: 20 });
|
|
935
|
+
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
|
|
936
|
+
|
|
937
|
+
try {
|
|
938
|
+
const call = transport.writeProtocolV2Frame(
|
|
939
|
+
'test-device',
|
|
940
|
+
bleTransport,
|
|
941
|
+
new Uint8Array(10),
|
|
942
|
+
context,
|
|
943
|
+
jest.fn()
|
|
944
|
+
);
|
|
945
|
+
|
|
946
|
+
await call;
|
|
947
|
+
// The only scheduled timer is the per-packet BLE write watchdog: no pacing delay.
|
|
948
|
+
expect(setTimeoutSpy.mock.calls.map(([, timeout]) => timeout)).toEqual([
|
|
949
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
950
|
+
]);
|
|
951
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
952
|
+
} finally {
|
|
953
|
+
setTimeoutSpy.mockRestore();
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
|
|
228
957
|
test('rejects an active Protocol V2 reader when disconnect resets the link', async () => {
|
|
229
958
|
const harness = createHarness();
|
|
230
959
|
const { transport, uuid, sentSeqs } = harness;
|
|
231
|
-
await transport.acquire({ uuid });
|
|
960
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
232
961
|
harness.setShouldRespond(false);
|
|
233
962
|
|
|
234
963
|
const call = transport.call(uuid, 'Ping', { message: 'disconnect' }, { timeoutMs: 50 });
|
|
235
|
-
while (sentSeqs.length <
|
|
236
|
-
await Promise
|
|
964
|
+
while (sentSeqs.length < 1) {
|
|
965
|
+
await new Promise(resolve => {
|
|
966
|
+
setTimeout(resolve, 0);
|
|
967
|
+
});
|
|
237
968
|
}
|
|
238
969
|
|
|
239
970
|
const rejection = expect(call).rejects.toThrow('React Native BLE transport disconnected');
|
|
240
971
|
await transport.disconnect(uuid);
|
|
241
972
|
await rejection;
|
|
242
973
|
});
|
|
974
|
+
|
|
975
|
+
test('emits one disconnect event when the physical callback races manual cleanup', async () => {
|
|
976
|
+
const harness = createHarness();
|
|
977
|
+
const disconnectListener = jest.fn();
|
|
978
|
+
harness.emitter.on(TRANSPORT_EVENT.DEVICE_DISCONNECT, disconnectListener);
|
|
979
|
+
|
|
980
|
+
await harness.transport.acquire({ uuid: harness.uuid, expectedProtocol: 'V2' });
|
|
981
|
+
harness.emitDisconnect();
|
|
982
|
+
await harness.transport.disconnect(harness.uuid);
|
|
983
|
+
|
|
984
|
+
expect(disconnectListener).toHaveBeenCalledTimes(1);
|
|
985
|
+
expect(disconnectListener).toHaveBeenCalledWith({
|
|
986
|
+
name: 'OneKey Pro 2',
|
|
987
|
+
id: harness.uuid,
|
|
988
|
+
connectId: harness.uuid,
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
|
|
992
|
+
test('chunks large frames and retries only transient GATT congestion', async () => {
|
|
993
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
994
|
+
const congested = { status: 143, message: 'GATT_CONGESTED' };
|
|
995
|
+
const writeWithoutResponse = jest
|
|
996
|
+
.fn()
|
|
997
|
+
.mockRejectedValueOnce(congested)
|
|
998
|
+
.mockResolvedValue(undefined);
|
|
999
|
+
const bleTransport = {
|
|
1000
|
+
mtuSize: 23,
|
|
1001
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
1002
|
+
};
|
|
1003
|
+
const context = {
|
|
1004
|
+
messageName: 'Ping',
|
|
1005
|
+
timeoutMs: 1000,
|
|
1006
|
+
highThroughput: false,
|
|
1007
|
+
generation: 1,
|
|
1008
|
+
signal: new AbortController().signal,
|
|
1009
|
+
};
|
|
1010
|
+
const assertCurrentGeneration = jest.fn();
|
|
1011
|
+
configureProtocolV2BleTuning({ iosPacketLength: 20 });
|
|
1012
|
+
|
|
1013
|
+
await transport.writeProtocolV2Frame(
|
|
1014
|
+
'device-uuid',
|
|
1015
|
+
bleTransport,
|
|
1016
|
+
new Uint8Array(30),
|
|
1017
|
+
context,
|
|
1018
|
+
assertCurrentGeneration
|
|
1019
|
+
);
|
|
1020
|
+
|
|
1021
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(3);
|
|
1022
|
+
expect(writeWithoutResponse.mock.calls[0][0]).toBe(writeWithoutResponse.mock.calls[1][0]);
|
|
1023
|
+
expect(Buffer.from(writeWithoutResponse.mock.calls[2][0], 'base64')).toHaveLength(10);
|
|
1024
|
+
expect(assertCurrentGeneration).toHaveBeenCalled();
|
|
1025
|
+
});
|
|
1026
|
+
|
|
1027
|
+
test('does not retry a disconnected Protocol V2 write inside a partial frame', async () => {
|
|
1028
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
1029
|
+
const writeWithoutResponse = jest
|
|
1030
|
+
.fn()
|
|
1031
|
+
.mockRejectedValue({ errorCode: 205, message: 'Device disconnected' });
|
|
1032
|
+
const bleTransport = {
|
|
1033
|
+
mtuSize: 23,
|
|
1034
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
1035
|
+
};
|
|
1036
|
+
const context = {
|
|
1037
|
+
messageName: 'FileWrite',
|
|
1038
|
+
timeoutMs: 1000,
|
|
1039
|
+
highThroughput: true,
|
|
1040
|
+
generation: 1,
|
|
1041
|
+
signal: new AbortController().signal,
|
|
1042
|
+
};
|
|
1043
|
+
configureProtocolV2BleTuning({ iosPacketLength: 20 });
|
|
1044
|
+
|
|
1045
|
+
await expect(
|
|
1046
|
+
transport.writeProtocolV2Frame(
|
|
1047
|
+
'device-uuid',
|
|
1048
|
+
bleTransport,
|
|
1049
|
+
new Uint8Array(30),
|
|
1050
|
+
context,
|
|
1051
|
+
jest.fn()
|
|
1052
|
+
)
|
|
1053
|
+
).rejects.toMatchObject({ errorCode: 205 });
|
|
1054
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(1);
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
test('does not apply fixed burst or flush pauses to high-volume Protocol V2 writes', async () => {
|
|
1058
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 }) as any;
|
|
1059
|
+
const writeWithoutResponse = jest.fn().mockResolvedValue(undefined);
|
|
1060
|
+
const bleTransport = {
|
|
1061
|
+
mtuSize: 247,
|
|
1062
|
+
writeCharacteristic: { writeWithoutResponse },
|
|
1063
|
+
};
|
|
1064
|
+
const context = {
|
|
1065
|
+
messageName: 'FileWrite',
|
|
1066
|
+
timeoutMs: 1000,
|
|
1067
|
+
highThroughput: true,
|
|
1068
|
+
generation: 1,
|
|
1069
|
+
signal: new AbortController().signal,
|
|
1070
|
+
};
|
|
1071
|
+
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
|
|
1072
|
+
|
|
1073
|
+
try {
|
|
1074
|
+
await transport.writeProtocolV2Frame(
|
|
1075
|
+
'test-device',
|
|
1076
|
+
bleTransport,
|
|
1077
|
+
new Uint8Array(600),
|
|
1078
|
+
context,
|
|
1079
|
+
jest.fn()
|
|
1080
|
+
);
|
|
1081
|
+
|
|
1082
|
+
expect(writeWithoutResponse).toHaveBeenCalledTimes(3);
|
|
1083
|
+
// One BLE write watchdog per packet and nothing else: no burst or flush pauses.
|
|
1084
|
+
expect(setTimeoutSpy.mock.calls.map(([, timeout]) => timeout)).toEqual([
|
|
1085
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
1086
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
1087
|
+
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
1088
|
+
]);
|
|
1089
|
+
} finally {
|
|
1090
|
+
setTimeoutSpy.mockRestore();
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
243
1093
|
});
|