@onekeyfe/hd-transport-web-device 1.2.0-alpha.4 → 1.2.0-alpha.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/electron-ble-transport.test.ts +349 -12
- package/__tests__/webusb-protocol-v2-timeout.test.ts +385 -0
- package/dist/electron-ble-transport.d.ts +8 -5
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +101 -14
- package/dist/index.js +317 -320
- package/dist/transportLog.d.ts +2 -0
- package/dist/transportLog.d.ts.map +1 -0
- package/dist/webusb.d.ts +15 -10
- package/dist/webusb.d.ts.map +1 -1
- package/jest.config.js +5 -0
- package/package.json +6 -5
- package/src/electron-ble-transport.ts +213 -174
- package/src/transportLog.ts +1 -0
- package/src/webusb.ts +184 -217
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import transport, {
|
|
2
|
+
PROTOCOL_V2_CHANNEL_USB,
|
|
3
|
+
ProtocolV2,
|
|
4
|
+
ProtocolV2LinkError,
|
|
5
|
+
} from '@onekeyfe/hd-transport';
|
|
6
|
+
import { HardwareErrorCode, ONEKEY_WEBUSB_FILTER } from '@onekeyfe/hd-shared';
|
|
7
|
+
|
|
8
|
+
import WebUsbTransport from '../src/webusb';
|
|
9
|
+
|
|
10
|
+
const schema = {
|
|
11
|
+
nested: {
|
|
12
|
+
Ping: { fields: { message: { type: 'string', id: 1 } } },
|
|
13
|
+
Success: { fields: { message: { type: 'string', id: 1 } } },
|
|
14
|
+
MessageType: {
|
|
15
|
+
values: {
|
|
16
|
+
MessageType_Ping: 60206,
|
|
17
|
+
MessageType_Success: 60207,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('WebUsbTransport Protocol V2 timeout recovery', () => {
|
|
24
|
+
test('only enumerates devices with real USB serial numbers', async () => {
|
|
25
|
+
const filter = ONEKEY_WEBUSB_FILTER[0];
|
|
26
|
+
const deviceWithSerial = {
|
|
27
|
+
...filter,
|
|
28
|
+
manufacturerName: 'OneKey',
|
|
29
|
+
productName: 'OneKey Pro 2',
|
|
30
|
+
serialNumber: 'PRO2-SERIAL',
|
|
31
|
+
} as USBDevice;
|
|
32
|
+
const deviceWithoutSerial = {
|
|
33
|
+
...filter,
|
|
34
|
+
manufacturerName: 'OneKey',
|
|
35
|
+
productName: 'OneKey Pro 2',
|
|
36
|
+
serialNumber: null,
|
|
37
|
+
} as USBDevice;
|
|
38
|
+
const webusb = new WebUsbTransport();
|
|
39
|
+
webusb.usb = {
|
|
40
|
+
getDevices: jest.fn().mockResolvedValue([deviceWithSerial, deviceWithoutSerial]),
|
|
41
|
+
} as unknown as USB;
|
|
42
|
+
|
|
43
|
+
await expect(webusb.getConnectedDevices()).resolves.toEqual([
|
|
44
|
+
{
|
|
45
|
+
path: 'PRO2-SERIAL',
|
|
46
|
+
device: deviceWithSerial,
|
|
47
|
+
commType: 'webusb',
|
|
48
|
+
},
|
|
49
|
+
]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('keeps active links when the Protocol V2 schema is configured repeatedly', () => {
|
|
53
|
+
const webusb = new WebUsbTransport() as any;
|
|
54
|
+
webusb.invalidateAllProtocolV2UsbLinks = jest.fn().mockResolvedValue(undefined);
|
|
55
|
+
const schemaSource = JSON.stringify(schema);
|
|
56
|
+
|
|
57
|
+
webusb.configureProtocolV2(schemaSource);
|
|
58
|
+
webusb.configureProtocolV2(schemaSource);
|
|
59
|
+
|
|
60
|
+
expect(webusb.invalidateAllProtocolV2UsbLinks).not.toHaveBeenCalled();
|
|
61
|
+
|
|
62
|
+
webusb.configureProtocolV2(
|
|
63
|
+
JSON.stringify({
|
|
64
|
+
...schema,
|
|
65
|
+
nested: {
|
|
66
|
+
...schema.nested,
|
|
67
|
+
Failure: { fields: { message: { type: 'string', id: 1 } } },
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
expect(webusb.invalidateAllProtocolV2UsbLinks).toHaveBeenCalledWith(
|
|
72
|
+
'Protocol V2 schema reconfigured'
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('resets the connection between a failed V1 probe and the V2 probe', async () => {
|
|
77
|
+
const webusb = new WebUsbTransport() as any;
|
|
78
|
+
const path = 'pro2-webusb';
|
|
79
|
+
const events: string[] = [];
|
|
80
|
+
webusb.probeProtocolV1 = jest.fn().mockImplementation(() => {
|
|
81
|
+
events.push('probe-v1');
|
|
82
|
+
return Promise.resolve(false);
|
|
83
|
+
});
|
|
84
|
+
webusb.resetConnectionAfterProbe = jest.fn().mockImplementation(() => {
|
|
85
|
+
events.push('reset');
|
|
86
|
+
return Promise.resolve();
|
|
87
|
+
});
|
|
88
|
+
webusb.probeProtocolV2 = jest.fn().mockImplementation(() => {
|
|
89
|
+
events.push('probe-v2');
|
|
90
|
+
return Promise.resolve(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
await expect(webusb.detectProtocol(path)).resolves.toBe('V2');
|
|
94
|
+
|
|
95
|
+
expect(events).toEqual(['probe-v1', 'reset', 'probe-v2']);
|
|
96
|
+
expect(webusb.deviceProtocol.get(path)).toBe('V2');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('reports DeviceNotFound when automatic protocol detection exhausts both probes', async () => {
|
|
100
|
+
const webusb = new WebUsbTransport() as any;
|
|
101
|
+
const path = 'unresponsive-webusb';
|
|
102
|
+
webusb.probeProtocolV1 = jest.fn().mockResolvedValue(false);
|
|
103
|
+
webusb.probeProtocolV2 = jest.fn().mockResolvedValue(false);
|
|
104
|
+
webusb.resetConnectionAfterProbe = jest.fn().mockResolvedValue(undefined);
|
|
105
|
+
webusb.closeConnectionAfterProbe = jest.fn().mockResolvedValue(undefined);
|
|
106
|
+
|
|
107
|
+
await expect(webusb.detectProtocol(path)).rejects.toMatchObject({
|
|
108
|
+
errorCode: HardwareErrorCode.DeviceNotFound,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(webusb.probeProtocolV1).toHaveBeenCalledTimes(1);
|
|
112
|
+
expect(webusb.probeProtocolV2).toHaveBeenCalledTimes(1);
|
|
113
|
+
expect(webusb.resetConnectionAfterProbe).toHaveBeenCalledTimes(1);
|
|
114
|
+
expect(webusb.closeConnectionAfterProbe).toHaveBeenCalledTimes(1);
|
|
115
|
+
expect(webusb.deviceProtocol.has(path)).toBe(false);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('allows legacy WebUSB Initialize up to the Node USB probe timeout', async () => {
|
|
119
|
+
const webusb = new WebUsbTransport() as any;
|
|
120
|
+
const path = 'pro-webusb';
|
|
121
|
+
webusb.messages = {};
|
|
122
|
+
webusb.callProtocolV1 = jest.fn().mockResolvedValue({});
|
|
123
|
+
|
|
124
|
+
await expect(webusb.probeProtocolV1(path)).resolves.toBe(true);
|
|
125
|
+
|
|
126
|
+
expect(webusb.callProtocolV1).toHaveBeenCalledWith(
|
|
127
|
+
path,
|
|
128
|
+
'Initialize',
|
|
129
|
+
{},
|
|
130
|
+
{
|
|
131
|
+
timeoutMs: 5000,
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('retries an expected Protocol V2 probe once after resetting the connection', async () => {
|
|
137
|
+
const webusb = new WebUsbTransport() as any;
|
|
138
|
+
const path = 'pro2-webusb';
|
|
139
|
+
webusb.probeProtocolV1 = jest.fn();
|
|
140
|
+
webusb.probeProtocolV2 = jest.fn().mockResolvedValueOnce(false).mockResolvedValueOnce(true);
|
|
141
|
+
webusb.resetConnectionAfterProbe = jest.fn().mockResolvedValue(undefined);
|
|
142
|
+
|
|
143
|
+
await expect(webusb.detectProtocol(path, 'V2')).resolves.toBe('V2');
|
|
144
|
+
|
|
145
|
+
expect(webusb.probeProtocolV2).toHaveBeenCalledTimes(2);
|
|
146
|
+
expect(webusb.probeProtocolV1).not.toHaveBeenCalled();
|
|
147
|
+
expect(webusb.resetConnectionAfterProbe).toHaveBeenCalledTimes(1);
|
|
148
|
+
expect(webusb.deviceProtocol.get(path)).toBe('V2');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('reports a Protocol V2 probe timeout only after the bounded retry is exhausted', async () => {
|
|
152
|
+
const webusb = new WebUsbTransport() as any;
|
|
153
|
+
const path = 'pro2-webusb';
|
|
154
|
+
webusb.probeProtocolV1 = jest.fn();
|
|
155
|
+
webusb.probeProtocolV2 = jest.fn().mockResolvedValue(false);
|
|
156
|
+
webusb.resetConnectionAfterProbe = jest.fn().mockResolvedValue(undefined);
|
|
157
|
+
webusb.closeConnectionAfterProbe = jest.fn().mockResolvedValue(undefined);
|
|
158
|
+
|
|
159
|
+
await expect(webusb.detectProtocol(path, 'V2')).rejects.toThrow(
|
|
160
|
+
'Protocol V2 probe timeout after 2 attempts'
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
expect(webusb.probeProtocolV2).toHaveBeenCalledTimes(2);
|
|
164
|
+
expect(webusb.probeProtocolV1).not.toHaveBeenCalled();
|
|
165
|
+
expect(webusb.resetConnectionAfterProbe).toHaveBeenCalledTimes(1);
|
|
166
|
+
expect(webusb.closeConnectionAfterProbe).toHaveBeenCalledTimes(1);
|
|
167
|
+
expect(webusb.deviceProtocol.has(path)).toBe(false);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('closes the reopened device when acquire exhausts the expected Protocol V2 probe', async () => {
|
|
171
|
+
const webusb = new WebUsbTransport() as any;
|
|
172
|
+
const path = 'pro2-webusb';
|
|
173
|
+
const device = {
|
|
174
|
+
opened: false,
|
|
175
|
+
releaseInterface: jest.fn().mockResolvedValue(undefined),
|
|
176
|
+
close: jest.fn().mockImplementation(() => {
|
|
177
|
+
device.opened = false;
|
|
178
|
+
return Promise.resolve();
|
|
179
|
+
}),
|
|
180
|
+
};
|
|
181
|
+
webusb.deviceList = [{ path, device }];
|
|
182
|
+
webusb.Log = { debug: jest.fn() };
|
|
183
|
+
webusb.rotateProtocolV2UsbGeneration = jest.fn().mockResolvedValue(undefined);
|
|
184
|
+
webusb.connect = jest.fn().mockImplementation(() => {
|
|
185
|
+
device.opened = true;
|
|
186
|
+
return Promise.resolve();
|
|
187
|
+
});
|
|
188
|
+
webusb.detectProtocol = jest.fn().mockRejectedValue(new Error('terminal probe failure'));
|
|
189
|
+
|
|
190
|
+
await expect(webusb.acquire({ path, expectedProtocol: 'V2' })).rejects.toThrow(
|
|
191
|
+
'terminal probe failure'
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
expect(device.releaseInterface).toHaveBeenCalledTimes(1);
|
|
195
|
+
expect(device.close).toHaveBeenCalledTimes(1);
|
|
196
|
+
expect(device.opened).toBe(false);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('invalidates and resets the cached connection before another call can start', async () => {
|
|
200
|
+
const webusb = new WebUsbTransport() as any;
|
|
201
|
+
const path = 'pro2-webusb';
|
|
202
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
203
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
204
|
+
webusb.writeProtocolV2UsbPacket = jest.fn().mockResolvedValue(undefined);
|
|
205
|
+
webusb.readProtocolV2UsbPacket = jest.fn(() => new Promise<void>(() => {}));
|
|
206
|
+
webusb.resetProtocolV2UsbNativeLink = jest.fn().mockResolvedValue(undefined);
|
|
207
|
+
webusb.resetConnectionAfterProbe = jest.fn();
|
|
208
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test connection');
|
|
209
|
+
|
|
210
|
+
await expect(
|
|
211
|
+
webusb.callProtocolV2(path, 'Ping', { message: 'timeout' }, { timeoutMs: 10 })
|
|
212
|
+
).rejects.toThrow('timeout');
|
|
213
|
+
|
|
214
|
+
expect(webusb.resetProtocolV2UsbNativeLink).toHaveBeenCalledWith(
|
|
215
|
+
path,
|
|
216
|
+
expect.stringContaining('timeout')
|
|
217
|
+
);
|
|
218
|
+
expect(webusb.resetConnectionAfterProbe).not.toHaveBeenCalled();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('does not reconnect inside a Protocol V2 frame read after a USB I/O failure', async () => {
|
|
222
|
+
const webusb = new WebUsbTransport() as any;
|
|
223
|
+
const path = 'pro2-webusb';
|
|
224
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
225
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
226
|
+
webusb.writeProtocolV2UsbPacket = jest.fn().mockResolvedValue(undefined);
|
|
227
|
+
webusb.readProtocolV2UsbPacket = jest
|
|
228
|
+
.fn()
|
|
229
|
+
.mockRejectedValue(new Error('NetworkError: transferIn device disconnected'));
|
|
230
|
+
webusb.resetProtocolV2UsbNativeLink = jest.fn().mockResolvedValue(undefined);
|
|
231
|
+
webusb.resetConnectionAfterProbe = jest.fn();
|
|
232
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test connection');
|
|
233
|
+
|
|
234
|
+
await expect(webusb.callProtocolV2(path, 'Ping', { message: 'read-error' })).rejects.toThrow(
|
|
235
|
+
'NetworkError'
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
expect(webusb.readProtocolV2UsbPacket).toHaveBeenCalledTimes(1);
|
|
239
|
+
expect(webusb.resetProtocolV2UsbNativeLink).toHaveBeenCalledWith(
|
|
240
|
+
path,
|
|
241
|
+
expect.stringContaining('NetworkError')
|
|
242
|
+
);
|
|
243
|
+
expect(webusb.resetConnectionAfterProbe).not.toHaveBeenCalled();
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test('rejects an active Protocol V2 read without reconnecting after release', async () => {
|
|
247
|
+
const webusb = new WebUsbTransport() as any;
|
|
248
|
+
const path = 'pro2-webusb';
|
|
249
|
+
let markReadStarted: () => void = () => undefined;
|
|
250
|
+
const readStarted = new Promise<void>(resolve => {
|
|
251
|
+
markReadStarted = resolve;
|
|
252
|
+
});
|
|
253
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
254
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
255
|
+
webusb.writeProtocolV2UsbPacket = jest.fn().mockResolvedValue(undefined);
|
|
256
|
+
webusb.readProtocolV2UsbPacket = jest.fn().mockImplementation(() => {
|
|
257
|
+
markReadStarted();
|
|
258
|
+
return new Promise<void>(() => {});
|
|
259
|
+
});
|
|
260
|
+
webusb.closeOpenDevice = jest.fn().mockResolvedValue(undefined);
|
|
261
|
+
webusb.connect = jest.fn();
|
|
262
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test connection');
|
|
263
|
+
|
|
264
|
+
const call = webusb.callProtocolV2(path, 'Ping', { message: 'release' });
|
|
265
|
+
await readStarted;
|
|
266
|
+
await webusb.release(path);
|
|
267
|
+
|
|
268
|
+
await expect(call).rejects.toThrow('WebUSB transport released');
|
|
269
|
+
expect(webusb.connect).not.toHaveBeenCalled();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test.each(['router', 'packet-source', 'ack-sequence', 'response-sequence', 'frame'] as const)(
|
|
273
|
+
'invalidates cached state for typed Protocol V2 %s errors',
|
|
274
|
+
async code => {
|
|
275
|
+
const webusb = new WebUsbTransport() as any;
|
|
276
|
+
const path = 'pro2-webusb';
|
|
277
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
278
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
279
|
+
webusb.writeProtocolV2UsbPacket = jest.fn().mockResolvedValue(undefined);
|
|
280
|
+
const recoveredResponse = ProtocolV2.encodeFrame(
|
|
281
|
+
{ protocolV1: webusb.messages, protocolV2: webusb.messagesV2 },
|
|
282
|
+
'Success',
|
|
283
|
+
{ message: 'recovered' },
|
|
284
|
+
{ seq: 1 }
|
|
285
|
+
);
|
|
286
|
+
webusb.readProtocolV2UsbPacket = jest
|
|
287
|
+
.fn()
|
|
288
|
+
.mockRejectedValueOnce(
|
|
289
|
+
new ProtocolV2LinkError(code, `Protocol V2 ${code} validation failed`)
|
|
290
|
+
)
|
|
291
|
+
.mockResolvedValue(recoveredResponse);
|
|
292
|
+
webusb.resetProtocolV2UsbNativeLink = jest.fn().mockResolvedValue(undefined);
|
|
293
|
+
webusb.resetConnectionAfterProbe = jest.fn();
|
|
294
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test connection');
|
|
295
|
+
|
|
296
|
+
await expect(webusb.callProtocolV2(path, 'Ping', { message: 'mismatch' })).rejects.toThrow(
|
|
297
|
+
`${code} validation failed`
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
expect(webusb.resetProtocolV2UsbNativeLink).toHaveBeenCalledWith(
|
|
301
|
+
path,
|
|
302
|
+
expect.stringContaining(`${code} validation failed`)
|
|
303
|
+
);
|
|
304
|
+
expect(webusb.resetConnectionAfterProbe).not.toHaveBeenCalled();
|
|
305
|
+
|
|
306
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test reconnect');
|
|
307
|
+
await expect(
|
|
308
|
+
webusb.callProtocolV2(path, 'Ping', { message: 'after-reset' })
|
|
309
|
+
).resolves.toMatchObject({
|
|
310
|
+
type: 'Success',
|
|
311
|
+
message: { message: 'recovered' },
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
test('does not discard buffered Protocol V2 frames before each call', async () => {
|
|
317
|
+
const webusb = new WebUsbTransport() as any;
|
|
318
|
+
const path = 'pro2-webusb';
|
|
319
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
320
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
321
|
+
webusb.writeProtocolV2UsbPacket = jest.fn().mockResolvedValue(undefined);
|
|
322
|
+
const firstResponse = ProtocolV2.encodeFrame(
|
|
323
|
+
{ protocolV1: webusb.messages, protocolV2: webusb.messagesV2 },
|
|
324
|
+
'Success',
|
|
325
|
+
{ message: 'first' },
|
|
326
|
+
{ router: PROTOCOL_V2_CHANNEL_USB, seq: 1 }
|
|
327
|
+
);
|
|
328
|
+
const secondResponse = ProtocolV2.encodeFrame(
|
|
329
|
+
{ protocolV1: webusb.messages, protocolV2: webusb.messagesV2 },
|
|
330
|
+
'Success',
|
|
331
|
+
{ message: 'second' },
|
|
332
|
+
{ router: PROTOCOL_V2_CHANNEL_USB, seq: 2 }
|
|
333
|
+
);
|
|
334
|
+
const coalescedResponses = new Uint8Array(firstResponse.length + secondResponse.length);
|
|
335
|
+
coalescedResponses.set(firstResponse);
|
|
336
|
+
coalescedResponses.set(secondResponse, firstResponse.length);
|
|
337
|
+
webusb.readProtocolV2UsbPacket = jest.fn().mockResolvedValue(coalescedResponses);
|
|
338
|
+
webusb.resetProtocolV2UsbNativeLink = jest.fn().mockResolvedValue(undefined);
|
|
339
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test connection');
|
|
340
|
+
|
|
341
|
+
await expect(webusb.callProtocolV2(path, 'Ping', { message: 'first' })).resolves.toMatchObject({
|
|
342
|
+
type: 'Success',
|
|
343
|
+
message: { message: 'first' },
|
|
344
|
+
});
|
|
345
|
+
await expect(webusb.callProtocolV2(path, 'Ping', { message: 'second' })).resolves.toMatchObject(
|
|
346
|
+
{
|
|
347
|
+
type: 'Success',
|
|
348
|
+
message: { message: 'second' },
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
expect(webusb.readProtocolV2UsbPacket).toHaveBeenCalledTimes(1);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test('keeps queued Protocol V2 read timeouts scoped to each call', async () => {
|
|
356
|
+
const webusb = new WebUsbTransport() as any;
|
|
357
|
+
const path = 'pro2-webusb';
|
|
358
|
+
let responseSequence = 0;
|
|
359
|
+
const readTimeouts: number[] = [];
|
|
360
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
361
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
362
|
+
webusb.writeProtocolV2UsbPacket = jest.fn().mockResolvedValue(undefined);
|
|
363
|
+
webusb.readProtocolV2UsbPacket = jest.fn().mockImplementation((_path, context) => {
|
|
364
|
+
responseSequence += 1;
|
|
365
|
+
readTimeouts.push(context.timeoutMs);
|
|
366
|
+
return Promise.resolve(
|
|
367
|
+
ProtocolV2.encodeFrame(
|
|
368
|
+
{ protocolV1: webusb.messages, protocolV2: webusb.messagesV2 },
|
|
369
|
+
'Success',
|
|
370
|
+
{ message: 'ok' },
|
|
371
|
+
{ seq: responseSequence }
|
|
372
|
+
)
|
|
373
|
+
);
|
|
374
|
+
});
|
|
375
|
+
webusb.resetProtocolV2UsbNativeLink = jest.fn().mockResolvedValue(undefined);
|
|
376
|
+
await webusb.rotateProtocolV2UsbGeneration(path, 'test connection');
|
|
377
|
+
|
|
378
|
+
await Promise.all([
|
|
379
|
+
webusb.callProtocolV2(path, 'Ping', { message: 'long' }, { timeoutMs: 1_000 }),
|
|
380
|
+
webusb.callProtocolV2(path, 'Ping', { message: 'short' }, { timeoutMs: 25 }),
|
|
381
|
+
]);
|
|
382
|
+
|
|
383
|
+
expect(readTimeouts).toEqual([1_000, 25]);
|
|
384
|
+
});
|
|
385
|
+
});
|
|
@@ -12,13 +12,16 @@ export type BleAcquireInput = {
|
|
|
12
12
|
uuid: string;
|
|
13
13
|
forceCleanRunPromise?: boolean;
|
|
14
14
|
expectedProtocol?: ProtocolType;
|
|
15
|
+
protocolHint?: ProtocolType;
|
|
15
16
|
};
|
|
16
17
|
export default class ElectronBleTransport {
|
|
17
18
|
private _messages;
|
|
18
19
|
private _messagesV2;
|
|
20
|
+
private protocolV2SchemaConfiguration;
|
|
19
21
|
name: string;
|
|
20
22
|
configured: boolean;
|
|
21
23
|
runPromise: Deferred<Uint8Array | string> | null;
|
|
24
|
+
private runPromiseDeviceId;
|
|
22
25
|
Log?: any;
|
|
23
26
|
emitter?: EventEmitter;
|
|
24
27
|
private connectedDevices;
|
|
@@ -28,8 +31,7 @@ export default class ElectronBleTransport {
|
|
|
28
31
|
private v2Assemblers;
|
|
29
32
|
private v2FrameQueues;
|
|
30
33
|
private v2FramePromises;
|
|
31
|
-
private
|
|
32
|
-
private nextProtocolV2CallToken;
|
|
34
|
+
private protocolV2Links;
|
|
33
35
|
private notificationCleanups;
|
|
34
36
|
private disconnectCleanups;
|
|
35
37
|
private notificationTokens;
|
|
@@ -53,6 +55,7 @@ export default class ElectronBleTransport {
|
|
|
53
55
|
protocolType?: ProtocolType | undefined;
|
|
54
56
|
}>;
|
|
55
57
|
release(id: string): Promise<void>;
|
|
58
|
+
private releaseNative;
|
|
56
59
|
private createProtocolMismatchError;
|
|
57
60
|
private createProtocolDetectionError;
|
|
58
61
|
private clearProbeProtocol;
|
|
@@ -61,20 +64,20 @@ export default class ElectronBleTransport {
|
|
|
61
64
|
private resetProbeStateAfterProtocolProbe;
|
|
62
65
|
private probeProtocolV1;
|
|
63
66
|
private probeProtocolV2;
|
|
64
|
-
private writeWithChunking;
|
|
65
67
|
private writeOnce;
|
|
68
|
+
private writeProtocolV2Frame;
|
|
66
69
|
private handleNotification;
|
|
67
70
|
private handleProtocolV2Notification;
|
|
68
71
|
private getProtocolV2FrameQueue;
|
|
69
72
|
private resolveProtocolV2Frame;
|
|
70
|
-
private rejectAllProtocolV2Frames;
|
|
71
73
|
private resetProtocolV2Frames;
|
|
72
|
-
private
|
|
74
|
+
private rejectProtocolV2Frames;
|
|
73
75
|
private readProtocolV2Frame;
|
|
74
76
|
private handleProtocolV1Notification;
|
|
75
77
|
call(uuid: string, name: string, data: Record<string, unknown>, options?: TransportCallOptions): Promise<import("@onekeyfe/hd-transport").MessageFromOneKey>;
|
|
76
78
|
private callProtocolV1;
|
|
77
79
|
private callProtocolV2;
|
|
80
|
+
private createProtocolV2Adapter;
|
|
78
81
|
private processProtocolV1Notification;
|
|
79
82
|
getProtocolType(path: string): ProtocolType | undefined;
|
|
80
83
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble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import * as _onekeyfe_hd_transport from '@onekeyfe/hd-transport';
|
|
2
|
-
import _onekeyfe_hd_transport__default, { AcquireInput, TransportCallOptions, ProtocolType, OneKeyDeviceInfoBase, OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
|
|
2
|
+
import _onekeyfe_hd_transport__default, { ProtocolV2UsbTransportBase, AcquireInput, TransportCallOptions, ProtocolV2Schemas, ProtocolV2CallContext, ProtocolType, OneKeyDeviceInfoBase, OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
|
|
3
3
|
import { Deferred } from '@onekeyfe/hd-shared';
|
|
4
4
|
import { DesktopAPI } from '@onekeyfe/hd-transport-electron';
|
|
5
5
|
import EventEmitter from 'events';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Device information with path and WebUSB device instance
|
|
9
|
+
*/
|
|
7
10
|
interface DeviceInfo extends OneKeyDeviceInfoBase {
|
|
8
11
|
path: string;
|
|
9
12
|
device: USBDevice;
|
|
10
13
|
protocolType?: ProtocolType;
|
|
11
14
|
}
|
|
12
|
-
declare class WebUsbTransport {
|
|
15
|
+
declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
|
|
13
16
|
messages: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
|
|
17
|
+
/** Protobuf schema for Protocol V2 transports. */
|
|
14
18
|
messagesV2: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
|
|
19
|
+
private protocolV2SchemaSource;
|
|
20
|
+
/** Per-path protocol type detected by active wire-level probe. */
|
|
15
21
|
private deviceProtocol;
|
|
16
22
|
private deviceProtocolHints;
|
|
17
|
-
|
|
18
|
-
private protocolV2Sessions;
|
|
19
|
-
private protocolV2ReadTimeouts;
|
|
23
|
+
/** Per-path USB endpoint / interface numbers (discovered from USB descriptors) */
|
|
20
24
|
private deviceEndpoints;
|
|
21
|
-
private mockSerialPaths;
|
|
22
|
-
private mockSerialCounter;
|
|
23
25
|
name: string;
|
|
24
26
|
stopped: boolean;
|
|
25
27
|
configured: boolean;
|
|
26
28
|
Log?: any;
|
|
27
29
|
usb?: USB;
|
|
30
|
+
/**
|
|
31
|
+
* Cached list of connected devices
|
|
32
|
+
* This is essential for maintaining device references between operations
|
|
33
|
+
*/
|
|
28
34
|
deviceList: Array<DeviceInfo>;
|
|
29
35
|
configurationId: number;
|
|
30
36
|
endpointId: number;
|
|
31
37
|
interfaceId: number;
|
|
38
|
+
constructor();
|
|
39
|
+
/**
|
|
40
|
+
* Initialize WebUSB transport
|
|
41
|
+
*/
|
|
32
42
|
init(logger: any): void;
|
|
43
|
+
/**
|
|
44
|
+
* Configure Protocol V1 protobuf schema (legacy chunked 0x3F framing).
|
|
45
|
+
*/
|
|
33
46
|
configure(signedData: any): void;
|
|
47
|
+
/**
|
|
48
|
+
* Cache the Protocol V2 protobuf schema.
|
|
49
|
+
*/
|
|
34
50
|
configureProtocolV2(signedData: any): void;
|
|
51
|
+
/**
|
|
52
|
+
* Request user to select a device
|
|
53
|
+
* This method must be called in response to a user action
|
|
54
|
+
* to comply with WebUSB security requirements
|
|
55
|
+
*/
|
|
35
56
|
promptDeviceAccess(): Promise<USBDevice | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Enumerate already connected devices
|
|
59
|
+
* This method only returns devices that are already authorized by the browser
|
|
60
|
+
* It does NOT prompt the user to select a device
|
|
61
|
+
*/
|
|
36
62
|
enumerate(): Promise<DeviceInfo[]>;
|
|
37
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Get list of connected devices
|
|
65
|
+
*/
|
|
38
66
|
getConnectedDevices(): Promise<DeviceInfo[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Acquire device control
|
|
69
|
+
*/
|
|
39
70
|
acquire(input: AcquireInput): Promise<string | undefined>;
|
|
71
|
+
/**
|
|
72
|
+
* Determine protocol type after connect.
|
|
73
|
+
* Probe Protocol V1 first with Initialize. If it does not answer in time,
|
|
74
|
+
* fall back to a Protocol V2 Ping probe.
|
|
75
|
+
*/
|
|
40
76
|
private createProtocolMismatchError;
|
|
77
|
+
private createProtocolProbeTimeoutError;
|
|
41
78
|
private createProtocolDetectionError;
|
|
42
79
|
private detectProtocol;
|
|
80
|
+
/**
|
|
81
|
+
* Find device by path
|
|
82
|
+
*/
|
|
43
83
|
findDevice(path: string): Promise<USBDevice>;
|
|
84
|
+
/**
|
|
85
|
+
* Connect to device with retry mechanism
|
|
86
|
+
*/
|
|
44
87
|
connect(path: string, first: boolean): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Discover vendor-class (0xFF) interface and its IN/OUT endpoint numbers from USB descriptors.
|
|
90
|
+
* Falls back to legacy hardcoded values if no vendor interface is found.
|
|
91
|
+
*/
|
|
45
92
|
private discoverEndpoints;
|
|
93
|
+
/**
|
|
94
|
+
* Connect to specific device.
|
|
95
|
+
* Discovers interface/endpoint numbers from USB descriptors on first connection.
|
|
96
|
+
*/
|
|
46
97
|
connectToDevice(path: string, first: boolean): Promise<void>;
|
|
47
98
|
private closeOpenDevice;
|
|
48
99
|
private clearEndpointHalt;
|
|
@@ -55,16 +106,43 @@ declare class WebUsbTransport {
|
|
|
55
106
|
private transferOutWithRetry;
|
|
56
107
|
private transferOutOnce;
|
|
57
108
|
private transferInWithRetry;
|
|
109
|
+
private transferInOnce;
|
|
110
|
+
private closeConnectionAfterProbe;
|
|
58
111
|
private resetConnectionAfterProbe;
|
|
59
112
|
private withProtocolReadTimeout;
|
|
60
113
|
private probeProtocolV1;
|
|
61
114
|
private probeProtocolV2;
|
|
115
|
+
/**
|
|
116
|
+
* Call device method — branches to Protocol V1 or Protocol V2 based on active probe.
|
|
117
|
+
*/
|
|
62
118
|
call(path: string, name: string, data: Record<string, unknown>, options?: TransportCallOptions): Promise<_onekeyfe_hd_transport.MessageFromOneKey>;
|
|
63
119
|
private callProtocolV1;
|
|
120
|
+
/**
|
|
121
|
+
* Send/receive a single call over Protocol V2 (0x5A framing).
|
|
122
|
+
*
|
|
123
|
+
* Encoding: protobuf message → 2-byte LE messageTypeId + pb bytes → Protocol V2 frame
|
|
124
|
+
* Decoding: Protocol V2 frame → messageTypeId + pb bytes → protobuf message
|
|
125
|
+
*/
|
|
64
126
|
private callProtocolV2;
|
|
65
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Receive data from device
|
|
129
|
+
*/
|
|
66
130
|
receiveData(path: string, timeoutMs?: number): Promise<string>;
|
|
131
|
+
/**
|
|
132
|
+
* Release device
|
|
133
|
+
*/
|
|
67
134
|
release(path: string): Promise<void>;
|
|
135
|
+
protected getProtocolV2UsbSchemas(): ProtocolV2Schemas;
|
|
136
|
+
protected getProtocolV2UsbLogger(): any;
|
|
137
|
+
protected writeProtocolV2UsbPacket(path: string, frame: Uint8Array, _context: ProtocolV2CallContext): Promise<void>;
|
|
138
|
+
protected readProtocolV2UsbPacket(path: string, _context: ProtocolV2CallContext): Promise<Uint8Array>;
|
|
139
|
+
protected resetProtocolV2UsbNativeLink(path: string, _reason: string): Promise<void>;
|
|
140
|
+
protected onProtocolV2UsbLinkInvalidated(path: string, reason: string): void;
|
|
141
|
+
protected createProtocolV2UsbTimeoutError(name: string, timeoutMs: number): Error;
|
|
142
|
+
/**
|
|
143
|
+
* Expose the detected protocol type for a given device path.
|
|
144
|
+
* Used by upper layers (e.g. TransportManager) to select the correct schema.
|
|
145
|
+
*/
|
|
68
146
|
getProtocolType(path: string): ProtocolType | undefined;
|
|
69
147
|
}
|
|
70
148
|
|
|
@@ -77,13 +155,22 @@ type BleAcquireInput = {
|
|
|
77
155
|
uuid: string;
|
|
78
156
|
forceCleanRunPromise?: boolean;
|
|
79
157
|
expectedProtocol?: ProtocolType;
|
|
158
|
+
protocolHint?: ProtocolType;
|
|
80
159
|
};
|
|
160
|
+
/**
|
|
161
|
+
* Desktop Electron BLE transport with automatic Protocol V1/V2 detection.
|
|
162
|
+
*
|
|
163
|
+
* Protocol V1 devices continue using chunked packets. Protocol V2 is detected
|
|
164
|
+
* after a Protocol V1 GetFeatures timeout by probing Protocol V2 Ping.
|
|
165
|
+
*/
|
|
81
166
|
declare class ElectronBleTransport {
|
|
82
167
|
private _messages;
|
|
83
168
|
private _messagesV2;
|
|
169
|
+
private protocolV2SchemaConfiguration;
|
|
84
170
|
name: string;
|
|
85
171
|
configured: boolean;
|
|
86
172
|
runPromise: Deferred<Uint8Array | string> | null;
|
|
173
|
+
private runPromiseDeviceId;
|
|
87
174
|
Log?: any;
|
|
88
175
|
emitter?: EventEmitter;
|
|
89
176
|
private connectedDevices;
|
|
@@ -93,8 +180,7 @@ declare class ElectronBleTransport {
|
|
|
93
180
|
private v2Assemblers;
|
|
94
181
|
private v2FrameQueues;
|
|
95
182
|
private v2FramePromises;
|
|
96
|
-
private
|
|
97
|
-
private nextProtocolV2CallToken;
|
|
183
|
+
private protocolV2Links;
|
|
98
184
|
private notificationCleanups;
|
|
99
185
|
private disconnectCleanups;
|
|
100
186
|
private notificationTokens;
|
|
@@ -118,6 +204,7 @@ declare class ElectronBleTransport {
|
|
|
118
204
|
protocolType?: ProtocolType | undefined;
|
|
119
205
|
}>;
|
|
120
206
|
release(id: string): Promise<void>;
|
|
207
|
+
private releaseNative;
|
|
121
208
|
private createProtocolMismatchError;
|
|
122
209
|
private createProtocolDetectionError;
|
|
123
210
|
private clearProbeProtocol;
|
|
@@ -126,20 +213,20 @@ declare class ElectronBleTransport {
|
|
|
126
213
|
private resetProbeStateAfterProtocolProbe;
|
|
127
214
|
private probeProtocolV1;
|
|
128
215
|
private probeProtocolV2;
|
|
129
|
-
private writeWithChunking;
|
|
130
216
|
private writeOnce;
|
|
217
|
+
private writeProtocolV2Frame;
|
|
131
218
|
private handleNotification;
|
|
132
219
|
private handleProtocolV2Notification;
|
|
133
220
|
private getProtocolV2FrameQueue;
|
|
134
221
|
private resolveProtocolV2Frame;
|
|
135
|
-
private rejectAllProtocolV2Frames;
|
|
136
222
|
private resetProtocolV2Frames;
|
|
137
|
-
private
|
|
223
|
+
private rejectProtocolV2Frames;
|
|
138
224
|
private readProtocolV2Frame;
|
|
139
225
|
private handleProtocolV1Notification;
|
|
140
226
|
call(uuid: string, name: string, data: Record<string, unknown>, options?: TransportCallOptions): Promise<_onekeyfe_hd_transport.MessageFromOneKey>;
|
|
141
227
|
private callProtocolV1;
|
|
142
228
|
private callProtocolV2;
|
|
229
|
+
private createProtocolV2Adapter;
|
|
143
230
|
private processProtocolV1Notification;
|
|
144
231
|
getProtocolType(path: string): ProtocolType | undefined;
|
|
145
232
|
}
|