@onekeyfe/hd-transport-lowlevel 1.1.34-alpha.3 → 1.1.34-alpha.4

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/index.d.ts CHANGED
@@ -1,53 +1,26 @@
1
- import * as transport from '@onekeyfe/hd-transport';
2
- import transport__default, { ProtocolType, LowlevelTransportSharedPlugin, LowLevelDevice, TransportCallOptions } from '@onekeyfe/hd-transport';
1
+ import * as _onekeyfe_hd_transport from '@onekeyfe/hd-transport';
2
+ import _onekeyfe_hd_transport__default, { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
3
3
  import EventEmitter from 'events';
4
4
 
5
5
  type LowLevelAcquireInput = {
6
6
  uuid: string;
7
- expectedProtocol?: ProtocolType;
8
7
  };
9
8
 
10
9
  declare class LowlevelTransport {
11
- _messages: ReturnType<typeof transport__default.parseConfigure> | undefined;
12
- _messagesV2: ReturnType<typeof transport__default.parseConfigure> | undefined;
10
+ _messages: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
13
11
  configured: boolean;
14
12
  Log?: any;
15
13
  emitter?: EventEmitter;
16
14
  plugin: LowlevelTransportSharedPlugin;
17
- private deviceProtocol;
18
- private deviceProtocolHints;
19
- private protocolV2Assemblers;
20
- private protocolV2Generations;
21
- private connectedDevices;
22
- private protocolV2Links;
23
- getProtocolType(path: string): ProtocolType | undefined;
24
15
  init(logger: any, emitter: EventEmitter, plugin: LowlevelTransportSharedPlugin): void;
25
16
  configure(signedData: any): void;
26
- configureProtocolV2(signedData: any): void;
27
17
  listen(): void;
28
- enumerate(): Promise<LowLevelDevice[]>;
18
+ enumerate(): Promise<_onekeyfe_hd_transport.LowLevelDevice[]>;
29
19
  acquire(input: LowLevelAcquireInput): Promise<{
30
20
  uuid: string;
31
- protocolType: ProtocolType;
32
21
  }>;
33
22
  release(uuid: string): Promise<boolean>;
34
- call(uuid: string, name: string, data: Record<string, unknown>, options?: TransportCallOptions): Promise<transport.MessageFromOneKey>;
35
- private callProtocolV1;
36
- private createProtocolTimeoutError;
37
- private createProtocolMismatchError;
38
- private createProtocolDetectionError;
39
- private clearProbeProtocol;
40
- private detectProtocol;
41
- private resetConnectionAfterProbe;
42
- private probeProtocolV1;
43
- private probeProtocolV2;
44
- private receiveHex;
45
- private readProtocolV1Message;
46
- private readProtocolV2Frame;
47
- private writeProtocolV2Frame;
48
- private callProtocolV2;
49
- private createProtocolV2Adapter;
50
- private advanceProtocolV2Generation;
23
+ call(uuid: string, name: string, data: Record<string, unknown>): Promise<_onekeyfe_hd_transport.MessageFromOneKey>;
51
24
  cancel(): void;
52
25
  }
53
26
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAWN,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,6BAA6B,EAC7B,YAAY,EACZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAqBpD,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,WAAW,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAErE,UAAU,UAAS;IAEnB,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB,MAAM,EAAE,6BAA6B,CAAuC;IAE5E,OAAO,CAAC,cAAc,CAAwC;IAE9D,OAAO,CAAC,mBAAmB,CAAwC;IAEnE,OAAO,CAAC,oBAAoB,CAAoD;IAEhF,OAAO,CAAC,qBAAqB,CAAkC;IAE/D,OAAO,CAAC,gBAAgB,CAA0B;IAElD,OAAO,CAAC,eAAe,CA6BpB;IAEH,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAIvD,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,6BAA6B;IAO9E,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,mBAAmB,CAAC,UAAU,EAAE,GAAG;IAOnC,MAAM;IAIA,SAAS;IAWT,OAAO,CAAC,KAAK,EAAE,oBAAoB;;;;IA6BnC,OAAO,CAAC,IAAI,EAAE,MAAM;IAgBpB,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YAsBlB,cAAc;IAoC5B,OAAO,CAAC,0BAA0B;IAOlC,OAAO,CAAC,2BAA2B;IAOnC,OAAO,CAAC,4BAA4B;IAOpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;YAsDd,yBAAyB;YAiCzB,eAAe;YAgBf,eAAe;YA6Bf,UAAU;YAUV,qBAAqB;YAmBrB,mBAAmB;YAqBnB,oBAAoB;YAOpB,cAAc;IA6B5B,OAAO,CAAC,uBAAuB;IAkC/B,OAAO,CAAC,2BAA2B;IAMnC,MAAM;CAGP"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAA8B,MAAM,wBAAwB,CAAC;AAEpE,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAIpD,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,UAAU,UAAS;IAEnB,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB,MAAM,EAAE,6BAA6B,CAAuC;IAE5E,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,6BAA6B;IAO9E,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IAIN,SAAS;IAIH,OAAO,CAAC,KAAK,EAAE,oBAAoB;;;IAanC,OAAO,CAAC,IAAI,EAAE,MAAM;IAUpB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAuCpE,MAAM;CAGP"}
package/dist/index.js CHANGED
@@ -37,62 +37,11 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
37
37
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
38
38
  };
39
39
 
40
- const { check, ProtocolV1, parseConfigure } = transport__default["default"];
41
- const PROTOCOL_PROBE_TIMEOUT_MS = 1000;
42
- const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
43
- const LOWLEVEL_PROTOCOL_TIMEOUT_MS = 30000;
44
- const LOWLEVEL_PROTOCOL_V2_PACKET_LENGTH = 64;
45
- function inferProtocolHintFromDeviceName(name) {
46
- return /\bpro\s*2\b/i.test(name !== null && name !== void 0 ? name : '') ? 'V2' : undefined;
47
- }
48
- function isProtocolV1TransportChunk(data) {
49
- return data.length >= 9 && data[0] === 0x3f && data[1] === 0x23 && data[2] === 0x23;
50
- }
51
- function readProtocolV1PayloadLength(data) {
52
- return data[5] * 0x1000000 + data[6] * 0x10000 + data[7] * 0x100 + data[8];
53
- }
40
+ const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
54
41
  class LowlevelTransport {
55
42
  constructor() {
56
43
  this.configured = false;
57
44
  this.plugin = {};
58
- this.deviceProtocol = new Map();
59
- this.deviceProtocolHints = new Map();
60
- this.protocolV2Assemblers = new Map();
61
- this.protocolV2Generations = new Map();
62
- this.connectedDevices = new Set();
63
- this.protocolV2Links = new transport.ProtocolV2LinkManager({
64
- getSchemas: () => {
65
- if (!this._messages || !this._messagesV2) {
66
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
67
- }
68
- return {
69
- protocolV1: this._messages,
70
- protocolV2: this._messagesV2,
71
- };
72
- },
73
- classifyError: () => 'link-fatal',
74
- onLinkInvalidated: (uuid, reason) => __awaiter(this, void 0, void 0, function* () {
75
- var _a, _b, _c;
76
- (_a = this.protocolV2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
77
- (_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug(`[LowlevelTransport] Protocol V2 link invalidated: ${uuid}`, reason);
78
- if (reason.startsWith('Protocol V2 link-fatal error:')) {
79
- this.deviceProtocol.delete(uuid);
80
- this.advanceProtocolV2Generation(uuid);
81
- try {
82
- yield this.plugin.disconnect(uuid);
83
- }
84
- catch (error) {
85
- (_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[LowlevelTransport] disconnect tainted Protocol V2 link failed: ${uuid}`, error);
86
- }
87
- finally {
88
- this.connectedDevices.delete(uuid);
89
- }
90
- }
91
- }),
92
- });
93
- }
94
- getProtocolType(path) {
95
- return this.deviceProtocol.get(path);
96
45
  }
97
46
  init(logger, emitter, plugin) {
98
47
  this.Log = logger;
@@ -105,58 +54,28 @@ class LowlevelTransport {
105
54
  this.configured = true;
106
55
  this._messages = messages;
107
56
  }
108
- configureProtocolV2(signedData) {
109
- this._messagesV2 = parseConfigure(signedData);
110
- this.protocolV2Links
111
- .invalidateAllLinks('Protocol V2 schema reconfigured')
112
- .catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('Protocol V2 schema link cleanup failed:', error); });
113
- }
114
57
  listen() {
115
58
  }
116
59
  enumerate() {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- const devices = yield this.plugin.enumerate();
119
- return devices.map((device) => {
120
- const protocolHint = inferProtocolHintFromDeviceName(device.name);
121
- if (protocolHint) {
122
- this.deviceProtocolHints.set(device.id, protocolHint);
123
- }
124
- return device;
125
- });
126
- });
60
+ return this.plugin.enumerate();
127
61
  }
128
62
  acquire(input) {
129
63
  var _a;
130
64
  return __awaiter(this, void 0, void 0, function* () {
131
- const alreadyConnected = this.connectedDevices.has(input.uuid);
132
65
  try {
133
66
  yield this.plugin.connect(input.uuid);
134
- if (!alreadyConnected) {
135
- this.connectedDevices.add(input.uuid);
136
- this.advanceProtocolV2Generation(input.uuid);
137
- }
67
+ return { uuid: input.uuid };
138
68
  }
139
69
  catch (error) {
140
- this.connectedDevices.delete(input.uuid);
141
70
  this.Log.debug('lowlelvel transport connect error: ', error);
142
71
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.LowlevelTrasnportConnectError, (_a = error.message) !== null && _a !== void 0 ? _a : error);
143
72
  }
144
- this.protocolV2Assemblers.set(input.uuid, new transport.ProtocolV2FrameAssembler());
145
- const protocolHint = input.expectedProtocol
146
- ? undefined
147
- : this.deviceProtocolHints.get(input.uuid);
148
- const protocolType = yield this.detectProtocol(input.uuid, input.expectedProtocol, protocolHint);
149
- return { uuid: input.uuid, protocolType };
150
73
  });
151
74
  }
152
75
  release(uuid) {
153
76
  return __awaiter(this, void 0, void 0, function* () {
154
77
  try {
155
- yield this.protocolV2Links.invalidateLink(uuid, 'Lowlevel transport released');
156
78
  yield this.plugin.disconnect(uuid);
157
- this.connectedDevices.delete(uuid);
158
- this.deviceProtocol.delete(uuid);
159
- this.protocolV2Assemblers.delete(uuid);
160
79
  return true;
161
80
  }
162
81
  catch (error) {
@@ -165,31 +84,22 @@ class LowlevelTransport {
165
84
  }
166
85
  });
167
86
  }
168
- call(uuid, name, data, options) {
87
+ call(uuid, name, data) {
169
88
  return __awaiter(this, void 0, void 0, function* () {
170
89
  if (this._messages === null || !this._messages) {
171
90
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
172
91
  }
173
- const protocol = this.getProtocolType(uuid);
174
- if (!protocol) {
175
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Device protocol has not been detected for ${uuid}`);
92
+ const messages = this._messages;
93
+ if (transport.LogBlockCommand.has(name)) {
94
+ this.Log.debug('lowlevel-transport', 'call-', ' name: ', name);
176
95
  }
177
- this.Log.debug('transport call', { name, protocol });
178
- if (protocol === 'V2') {
179
- return this.callProtocolV2(uuid, name, data, options);
96
+ else {
97
+ this.Log.debug('lowlevel-transport', 'call-', ' name: ', name, ' data: ', data);
180
98
  }
181
- return this.callProtocolV1(uuid, name, data, options);
182
- });
183
- }
184
- callProtocolV1(uuid, name, data, options) {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- if (!this._messages) {
187
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
188
- }
189
- const messages = this._messages;
190
- const buffers = ProtocolV1.encodeTransportPackets(messages, name, data);
99
+ const buffers = buildBuffers(messages, name, data);
191
100
  for (const o of buffers) {
192
101
  const outData = o.toString('hex');
102
+ this.Log.debug('send hex strting: ', outData);
193
103
  try {
194
104
  yield this.plugin.send(uuid, outData);
195
105
  }
@@ -199,261 +109,20 @@ class LowlevelTransport {
199
109
  }
200
110
  }
201
111
  try {
202
- const response = yield this.readProtocolV1Message(uuid, options === null || options === void 0 ? void 0 : options.timeoutMs);
203
- const jsonData = ProtocolV1.decodeMessage(messages, response);
204
- return check.call(jsonData);
205
- }
206
- catch (e) {
207
- if (name === 'Initialize' && (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS) {
208
- this.Log.debug('[LowlevelTransport] Protocol V1 Initialize probe call failed:', e);
209
- }
210
- else {
211
- this.Log.error('lowlevel call error: ', e);
212
- }
213
- throw e;
214
- }
215
- });
216
- }
217
- createProtocolTimeoutError(name, timeout) {
218
- return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleTimeoutError, `Lowlevel response timeout after ${timeout}ms for ${name}`);
219
- }
220
- createProtocolMismatchError(expected) {
221
- return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Device protocol mismatch: expected ${expected}, but device did not respond to expected protocol`);
222
- }
223
- createProtocolDetectionError() {
224
- return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleTimeoutError, 'Unable to detect BLE protocol: device did not respond to Protocol V1 Initialize or Protocol V2 Ping');
225
- }
226
- clearProbeProtocol(uuid, protocol) {
227
- if (this.deviceProtocol.get(uuid) === protocol) {
228
- this.deviceProtocol.delete(uuid);
229
- }
230
- }
231
- detectProtocol(uuid, expectedProtocol, protocolHint) {
232
- var _a, _b, _c, _d, _e, _f;
233
- return __awaiter(this, void 0, void 0, function* () {
234
- if (expectedProtocol === 'V2') {
235
- this.deviceProtocol.set(uuid, 'V2');
236
- (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2 (expected)`);
237
- return 'V2';
238
- }
239
- if (expectedProtocol === 'V1') {
240
- if (yield this.probeProtocolV1(uuid)) {
241
- this.deviceProtocol.set(uuid, 'V1');
242
- (_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V1 (expected)`);
243
- return 'V1';
244
- }
245
- throw this.createProtocolMismatchError(expectedProtocol);
246
- }
247
- if (protocolHint === 'V2' && (yield this.probeProtocolV2(uuid))) {
248
- this.deviceProtocol.set(uuid, 'V2');
249
- (_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2 (hint)`);
250
- return 'V2';
251
- }
252
- const cachedProtocol = this.deviceProtocol.get(uuid);
253
- if (cachedProtocol === 'V2' && (yield this.probeProtocolV2(uuid))) {
254
- this.deviceProtocol.set(uuid, 'V2');
255
- (_d = this.Log) === null || _d === void 0 ? void 0 : _d.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2 (cached)`);
256
- return 'V2';
257
- }
258
- const protocolV1Detected = yield this.probeProtocolV1(uuid);
259
- if (protocolV1Detected) {
260
- this.deviceProtocol.set(uuid, 'V1');
261
- (_e = this.Log) === null || _e === void 0 ? void 0 : _e.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V1`);
262
- return 'V1';
263
- }
264
- yield this.resetConnectionAfterProbe(uuid, 'V1');
265
- if (yield this.probeProtocolV2(uuid)) {
266
- this.deviceProtocol.set(uuid, 'V2');
267
- (_f = this.Log) === null || _f === void 0 ? void 0 : _f.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2`);
268
- return 'V2';
269
- }
270
- this.deviceProtocol.delete(uuid);
271
- throw this.createProtocolDetectionError();
272
- });
273
- }
274
- resetConnectionAfterProbe(uuid, protocol) {
275
- var _a, _b, _c, _d;
276
- return __awaiter(this, void 0, void 0, function* () {
277
- yield this.protocolV2Links.invalidateLink(uuid, `Reset connection after Protocol ${protocol} probe`);
278
- (_a = this.protocolV2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
279
- try {
280
- this.connectedDevices.delete(uuid);
281
- yield this.plugin.disconnect(uuid);
282
- }
283
- catch (error) {
284
- (_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug(`[LowlevelTransport] disconnect after Protocol ${protocol} probe failed:`, error);
285
- }
286
- try {
287
- yield this.plugin.connect(uuid);
288
- this.connectedDevices.add(uuid);
289
- this.advanceProtocolV2Generation(uuid);
290
- }
291
- catch (error) {
292
- (_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[LowlevelTransport] reconnect after Protocol ${protocol} probe failed:`, error);
293
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.LowlevelTrasnportConnectError, (_d = error.message) !== null && _d !== void 0 ? _d : error);
294
- }
295
- });
296
- }
297
- probeProtocolV1(uuid) {
298
- var _a;
299
- return __awaiter(this, void 0, void 0, function* () {
300
- if (!this._messages) {
301
- return false;
302
- }
303
- try {
304
- this.deviceProtocol.set(uuid, 'V1');
305
- yield this.callProtocolV1(uuid, 'Initialize', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS });
306
- return true;
307
- }
308
- catch (error) {
309
- this.clearProbeProtocol(uuid, 'V1');
310
- (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[LowlevelTransport] Protocol V1 Initialize probe failed:', error);
311
- return false;
312
- }
313
- });
314
- }
315
- probeProtocolV2(uuid) {
316
- var _a;
317
- return __awaiter(this, void 0, void 0, function* () {
318
- if (!this._messages || !this._messagesV2) {
319
- return false;
320
- }
321
- this.deviceProtocol.set(uuid, 'V2');
322
- (_a = this.protocolV2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
323
- try {
324
- const detected = yield transport.probeProtocolV2({
325
- call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
326
- timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
327
- logger: this.Log,
328
- logPrefix: 'ProtocolV2 Lowlevel-BLE',
329
- onProbeFailed: () => __awaiter(this, void 0, void 0, function* () {
330
- var _b;
331
- (_b = this.protocolV2Assemblers.get(uuid)) === null || _b === void 0 ? void 0 : _b.reset();
332
- yield this.resetConnectionAfterProbe(uuid, 'V2');
333
- }),
334
- });
335
- if (!detected) {
336
- this.clearProbeProtocol(uuid, 'V2');
112
+ const response = yield this.plugin.receive();
113
+ if (typeof response !== 'string') {
114
+ throw new Error('Returning data is not string');
337
115
  }
338
- return detected;
339
- }
340
- catch (error) {
341
- this.clearProbeProtocol(uuid, 'V2');
342
- throw error;
343
- }
344
- });
345
- }
346
- receiveHex(uuid, timeoutMs, commandName) {
347
- return __awaiter(this, void 0, void 0, function* () {
348
- const response = yield transport.withProtocolTimeout(this.plugin.receive(uuid), timeoutMs, () => this.createProtocolTimeoutError(commandName, timeoutMs !== null && timeoutMs !== void 0 ? timeoutMs : 0));
349
- if (typeof response !== 'string') {
350
- throw new Error('Returning data is not string');
351
- }
352
- return response;
353
- });
354
- }
355
- readProtocolV1Message(uuid, timeoutMs) {
356
- return __awaiter(this, void 0, void 0, function* () {
357
- const first = yield this.receiveHex(uuid, timeoutMs, 'ProtocolV1');
358
- const firstData = transport.hexToBytes(first);
359
- if (!isProtocolV1TransportChunk(firstData)) {
360
- return first;
361
- }
362
- const payloadLength = readProtocolV1PayloadLength(firstData);
363
- let buffer = firstData.slice(3);
364
- const expectedLength = transport.PROTOCOL_V1_MESSAGE_HEADER_SIZE + payloadLength;
365
- while (buffer.length < expectedLength) {
366
- const next = yield this.receiveHex(uuid, timeoutMs, 'ProtocolV1');
367
- buffer = transport.concatUint8Arrays([buffer, transport.hexToBytes(next)]);
368
- }
369
- return transport.bytesToHex(buffer.slice(0, expectedLength));
370
- });
371
- }
372
- readProtocolV2Frame(uuid, timeoutMs, commandName = 'ProtocolV2') {
373
- return __awaiter(this, void 0, void 0, function* () {
374
- let assembler = this.protocolV2Assemblers.get(uuid);
375
- if (!assembler) {
376
- assembler = new transport.ProtocolV2FrameAssembler();
377
- this.protocolV2Assemblers.set(uuid, assembler);
378
- }
379
- const queuedFrame = assembler.push(new Uint8Array(0));
380
- if (queuedFrame)
381
- return queuedFrame;
382
- let frame;
383
- while (!frame) {
384
- const response = yield this.receiveHex(uuid, timeoutMs, commandName);
385
- const chunk = transport.hexToBytes(response);
386
- if (chunk.length > 0) {
387
- frame = assembler.push(chunk);
388
- }
389
- }
390
- return frame;
391
- });
392
- }
393
- writeProtocolV2Frame(uuid, frame) {
394
- return __awaiter(this, void 0, void 0, function* () {
395
- for (let offset = 0; offset < frame.length; offset += LOWLEVEL_PROTOCOL_V2_PACKET_LENGTH) {
396
- const chunk = frame.slice(offset, offset + LOWLEVEL_PROTOCOL_V2_PACKET_LENGTH);
397
- yield this.plugin.send(uuid, transport.bytesToHex(chunk));
398
- }
399
- });
400
- }
401
- callProtocolV2(uuid, name, data, options) {
402
- var _a;
403
- return __awaiter(this, void 0, void 0, function* () {
404
- if (!this._messages || !this._messagesV2) {
405
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
406
- }
407
- const timeoutMs = (_a = options === null || options === void 0 ? void 0 : options.timeoutMs) !== null && _a !== void 0 ? _a : LOWLEVEL_PROTOCOL_TIMEOUT_MS;
408
- try {
409
- return yield this.protocolV2Links.call(uuid, () => this.createProtocolV2Adapter(uuid), name, data, Object.assign(Object.assign({}, options), { timeoutMs }));
116
+ this.Log.debug('receive data: ', response);
117
+ const jsonData = receiveOne(messages, response);
118
+ return check.call(jsonData);
410
119
  }
411
120
  catch (e) {
412
- this.Log.error('lowlevel Protocol V2 call error: ', e);
121
+ this.Log.error('lowlevel call error: ', e);
413
122
  throw e;
414
123
  }
415
124
  });
416
125
  }
417
- createProtocolV2Adapter(uuid) {
418
- var _a;
419
- const generation = (_a = this.protocolV2Generations.get(uuid)) !== null && _a !== void 0 ? _a : 0;
420
- const assertCurrentGeneration = () => {
421
- if (this.protocolV2Generations.get(uuid) !== generation) {
422
- throw new Error(`Protocol V2 connection generation changed for ${uuid}`);
423
- }
424
- };
425
- return {
426
- router: transport.PROTOCOL_V2_CHANNEL_BLE_UART,
427
- maxFrameBytes: transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES,
428
- generation,
429
- prepareCall: () => {
430
- var _a;
431
- assertCurrentGeneration();
432
- (_a = this.protocolV2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
433
- },
434
- writeFrame: (frame) => {
435
- assertCurrentGeneration();
436
- return this.writeProtocolV2Frame(uuid, frame);
437
- },
438
- readFrame: (context) => {
439
- assertCurrentGeneration();
440
- return this.readProtocolV2Frame(uuid, context.timeoutMs, context.messageName);
441
- },
442
- reset: () => {
443
- var _a;
444
- (_a = this.protocolV2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
445
- },
446
- logger: this.Log,
447
- logPrefix: 'ProtocolV2 Lowlevel-BLE',
448
- createTimeoutError: (messageName, timeout) => this.createProtocolTimeoutError(messageName, timeout),
449
- };
450
- }
451
- advanceProtocolV2Generation(uuid) {
452
- var _a;
453
- const nextGeneration = ((_a = this.protocolV2Generations.get(uuid)) !== null && _a !== void 0 ? _a : 0) + 1;
454
- this.protocolV2Generations.set(uuid, nextGeneration);
455
- return nextGeneration;
456
- }
457
126
  cancel() {
458
127
  this.Log.debug('lowlevel-transport', 'cancel');
459
128
  }
package/dist/types.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- import type { ProtocolType } from '@onekeyfe/hd-transport';
2
1
  export type LowLevelAcquireInput = {
3
2
  uuid: string;
4
- expectedProtocol?: ProtocolType;
5
3
  };
6
4
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,gBAAgB,CAAC,EAAE,YAAY,CAAC;CACjC,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-lowlevel",
3
- "version": "1.1.34-alpha.3",
3
+ "version": "1.1.34-alpha.4",
4
4
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -19,8 +19,8 @@
19
19
  "lint:fix": "eslint . --fix"
20
20
  },
21
21
  "dependencies": {
22
- "@onekeyfe/hd-shared": "1.1.34-alpha.3",
23
- "@onekeyfe/hd-transport": "1.1.34-alpha.3"
22
+ "@onekeyfe/hd-shared": "1.1.34-alpha.4",
23
+ "@onekeyfe/hd-transport": "1.1.34-alpha.4"
24
24
  },
25
- "gitHead": "3688bc8495eeb2ffd5616270d13fa29e8967623a"
25
+ "gitHead": "d546bc55410990351d34009230aefea77725d0d6"
26
26
  }