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