@onekeyfe/hd-core 1.2.0-alpha.48 → 1.2.0-alpha.49

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.
Files changed (55) hide show
  1. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +4 -4
  2. package/__tests__/device-lifecycle-events.test.ts +105 -3
  3. package/__tests__/method-protocol-support.test.ts +19 -0
  4. package/__tests__/protocol-binding.test.ts +89 -0
  5. package/__tests__/protocol-v2.test.ts +274 -6
  6. package/__tests__/search-devices.test.ts +12 -3
  7. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  8. package/dist/api/DetectDeviceConnectProtocol.d.ts +7 -0
  9. package/dist/api/DetectDeviceConnectProtocol.d.ts.map +1 -0
  10. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  11. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  12. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  13. package/dist/api/FirmwareUpdateV4.d.ts +1 -0
  14. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  15. package/dist/api/SearchDevices.d.ts.map +1 -1
  16. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  17. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  18. package/dist/api/index.d.ts +1 -0
  19. package/dist/api/index.d.ts.map +1 -1
  20. package/dist/core/index.d.ts.map +1 -1
  21. package/dist/device/Device.d.ts +5 -1
  22. package/dist/device/Device.d.ts.map +1 -1
  23. package/dist/device/DevicePool.d.ts.map +1 -1
  24. package/dist/index.d.ts +19 -2
  25. package/dist/index.js +238 -105
  26. package/dist/inject.d.ts +6 -1
  27. package/dist/inject.d.ts.map +1 -1
  28. package/dist/lowLevelInject.d.ts.map +1 -1
  29. package/dist/topLevelInject.d.ts.map +1 -1
  30. package/dist/types/api/detectDeviceConnectProtocol.d.ts +4 -0
  31. package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +1 -0
  32. package/dist/types/api/index.d.ts +4 -0
  33. package/dist/types/api/index.d.ts.map +1 -1
  34. package/dist/types/params.d.ts +1 -0
  35. package/dist/types/params.d.ts.map +1 -1
  36. package/package.json +4 -4
  37. package/src/api/CheckAllFirmwareRelease.ts +5 -3
  38. package/src/api/DetectDeviceConnectProtocol.ts +18 -0
  39. package/src/api/FirmwareUpdate.ts +6 -2
  40. package/src/api/FirmwareUpdateV2.ts +5 -2
  41. package/src/api/FirmwareUpdateV3.ts +6 -2
  42. package/src/api/FirmwareUpdateV4.ts +68 -40
  43. package/src/api/SearchDevices.ts +3 -1
  44. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +15 -4
  45. package/src/api/firmware/uploadFirmware.ts +17 -4
  46. package/src/api/index.ts +1 -0
  47. package/src/core/index.ts +11 -2
  48. package/src/device/Device.ts +53 -15
  49. package/src/device/DevicePool.ts +7 -2
  50. package/src/inject.ts +57 -2
  51. package/src/lowLevelInject.ts +6 -3
  52. package/src/topLevelInject.ts +6 -3
  53. package/src/types/api/detectDeviceConnectProtocol.ts +7 -0
  54. package/src/types/api/index.ts +11 -0
  55. package/src/types/params.ts +7 -1
package/dist/index.js CHANGED
@@ -101,26 +101,61 @@ const executeCallback = (id, ...args) => {
101
101
  const cleanupCallback = (id) => {
102
102
  callbackManager.delete(id);
103
103
  };
104
+ const normalizeConnectId = (connectId) => connectId.trim().toLowerCase();
105
+ const createProtocolAwareCall = (rawCall) => {
106
+ const protocolByConnectId = new Map();
107
+ const setDeviceConnectProtocol = (connectId, connectProtocol) => {
108
+ const normalizedConnectId = normalizeConnectId(connectId);
109
+ if (!normalizedConnectId)
110
+ return;
111
+ if (connectProtocol) {
112
+ protocolByConnectId.set(normalizedConnectId, connectProtocol);
113
+ }
114
+ else {
115
+ protocolByConnectId.delete(normalizedConnectId);
116
+ }
117
+ };
118
+ const call = params => {
119
+ if (!params || typeof params !== 'object') {
120
+ return rawCall(params);
121
+ }
122
+ const connectId = typeof params.connectId === 'string' ? params.connectId : undefined;
123
+ const boundProtocol = connectId
124
+ ? protocolByConnectId.get(normalizeConnectId(connectId))
125
+ : undefined;
126
+ if (boundProtocol &&
127
+ params.connectProtocol === undefined &&
128
+ params.forceProtocolDetection !== true) {
129
+ return rawCall(Object.assign(Object.assign({}, params), { connectProtocol: boundProtocol }));
130
+ }
131
+ return rawCall(params);
132
+ };
133
+ return { call, setDeviceConnectProtocol };
134
+ };
104
135
  const inject = ({ call, cancel, dispose, eventEmitter, init, updateSettings, switchTransport, uiResponse, }) => {
136
+ const protocolAwareCall = createProtocolAwareCall(call);
105
137
  const api = Object.assign({ on: (type, fn) => {
106
138
  eventEmitter.on(type, fn);
107
139
  }, emit: () => { }, off: (type, fn) => {
108
140
  eventEmitter.removeListener(type, fn);
109
141
  }, removeAllListeners: type => {
110
142
  eventEmitter.removeAllListeners(type);
111
- }, init,
112
- call,
113
- dispose,
143
+ }, init, call: protocolAwareCall.call, setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol, dispose,
114
144
  uiResponse,
115
145
  cancel,
116
146
  updateSettings,
117
- switchTransport }, createCoreApi(call));
147
+ switchTransport }, createCoreApi(protocolAwareCall.call));
118
148
  return api;
119
149
  };
120
150
  const createCoreApi = (call) => ({
121
151
  getLogs: () => call({ method: 'getLogs' }),
122
152
  clearSessionCache: params => call(Object.assign(Object.assign({}, params), { method: 'clearSessionCache' })),
123
153
  searchDevices: params => call(Object.assign(Object.assign({}, params), { method: 'searchDevices' })),
154
+ detectDeviceConnectProtocol: connectId => call({
155
+ connectId,
156
+ method: 'detectDeviceConnectProtocol',
157
+ forceProtocolDetection: true,
158
+ }),
124
159
  getFeatures: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'getFeatures' })),
125
160
  getDeviceState: (connectId, params) => {
126
161
  const _a = (params !== null && params !== void 0 ? params : {}), commonParams = __rest(_a, ["refresh", "includeRaw"]);
@@ -277,15 +312,14 @@ const createCoreApi = (call) => ({
277
312
  });
278
313
 
279
314
  const lowLevelInject = ({ call, cancel, dispose, eventEmitter, init, uiResponse, updateSettings, switchTransport, addHardwareGlobalEventListener, }) => {
315
+ const protocolAwareCall = createProtocolAwareCall(call);
280
316
  const api = Object.assign({ addHardwareGlobalEventListener, removeAllListeners: type => {
281
317
  eventEmitter.removeAllListeners(type);
282
- }, init,
283
- call,
284
- dispose,
318
+ }, init, call: protocolAwareCall.call, setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol, dispose,
285
319
  uiResponse,
286
320
  cancel,
287
321
  updateSettings,
288
- switchTransport, emit: () => { } }, createCoreApi(call));
322
+ switchTransport, emit: () => { } }, createCoreApi(protocolAwareCall.call));
289
323
  return api;
290
324
  };
291
325
 
@@ -773,6 +807,7 @@ const topLevelInject = () => {
773
807
  return Promise.resolve(undefined);
774
808
  return lowLevelApi.call(params);
775
809
  };
810
+ const protocolAwareCall = createProtocolAwareCall(call);
776
811
  const api = Object.assign(Object.assign({ on: (type, fn) => {
777
812
  eventEmitter.on(type, fn);
778
813
  }, emit: (eventName, ...args) => {
@@ -783,7 +818,7 @@ const topLevelInject = () => {
783
818
  var _a;
784
819
  lowLevelApi = hardwareLowLeverApi;
785
820
  return (_a = lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.init(settings)) !== null && _a !== void 0 ? _a : Promise.resolve(false);
786
- }, call }, createCoreApi(call)), { removeAllListeners: type => {
821
+ }, call: protocolAwareCall.call, setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol }, createCoreApi(protocolAwareCall.call)), { removeAllListeners: type => {
787
822
  eventEmitter.removeAllListeners(type);
788
823
  lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.removeAllListeners(type);
789
824
  }, dispose: () => lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.dispose(), uiResponse: response => lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.uiResponse(response), cancel: (connectId) => lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.cancel(connectId), updateSettings: settings => { var _a; return (_a = lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.updateSettings(settings)) !== null && _a !== void 0 ? _a : Promise.resolve(false); }, switchTransport: (env) => { var _a; return (_a = lowLevelApi === null || lowLevelApi === void 0 ? void 0 : lowLevelApi.switchTransport(env)) !== null && _a !== void 0 ? _a : Promise.resolve({ success: false }); } });
@@ -41970,7 +42005,9 @@ class DevicePool extends events.exports {
41970
42005
  if (!device) {
41971
42006
  device = Device.fromDescriptor(descriptor);
41972
42007
  device.deviceConnector = this.connector;
41973
- yield device.connect(initOptions === null || initOptions === void 0 ? void 0 : initOptions.connectProtocol);
42008
+ yield device.connect(initOptions === null || initOptions === void 0 ? void 0 : initOptions.connectProtocol, {
42009
+ forceProtocolDetection: initOptions === null || initOptions === void 0 ? void 0 : initOptions.forceProtocolDetection,
42010
+ });
41974
42011
  try {
41975
42012
  yield device.initialize(initOptions);
41976
42013
  if ((initOptions === null || initOptions === void 0 ? void 0 : initOptions.refreshRuntimeState) && device.isProtocolV2()) {
@@ -41996,7 +42033,10 @@ class DevicePool extends events.exports {
41996
42033
  catch (error) {
41997
42034
  refreshError = error;
41998
42035
  }
41999
- }), { connectProtocol: initOptions.connectProtocol });
42036
+ }), {
42037
+ connectProtocol: initOptions.connectProtocol,
42038
+ forceProtocolDetection: initOptions.forceProtocolDetection,
42039
+ });
42000
42040
  if (refreshError instanceof Error)
42001
42041
  throw refreshError;
42002
42042
  if (refreshError)
@@ -43742,7 +43782,7 @@ class Device extends events.exports {
43742
43782
  unavailableCapabilities: this.unavailableCapabilities,
43743
43783
  };
43744
43784
  }
43745
- connect(connectProtocol) {
43785
+ connect(connectProtocol, options) {
43746
43786
  const env = DataManager.getSettings('env');
43747
43787
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
43748
43788
  if (DataManager.isBleConnect(env)) {
@@ -43751,7 +43791,7 @@ class Device extends events.exports {
43751
43791
  return;
43752
43792
  }
43753
43793
  try {
43754
- yield this.acquire(connectProtocol);
43794
+ yield this.acquire(connectProtocol, options);
43755
43795
  resolve(true);
43756
43796
  }
43757
43797
  catch (error) {
@@ -43761,7 +43801,7 @@ class Device extends events.exports {
43761
43801
  }
43762
43802
  if (!this.mainId || (!this.isUsedHere() && this.originalDescriptor)) {
43763
43803
  try {
43764
- yield this.acquire(connectProtocol);
43804
+ yield this.acquire(connectProtocol, options);
43765
43805
  resolve(true);
43766
43806
  }
43767
43807
  catch (error) {
@@ -43777,35 +43817,58 @@ class Device extends events.exports {
43777
43817
  }));
43778
43818
  }
43779
43819
  acquire(expectedProtocol, options) {
43780
- var _a, _b, _c, _d, _e, _f, _g;
43820
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
43781
43821
  return __awaiter(this, void 0, void 0, function* () {
43782
43822
  const env = DataManager.getSettings('env');
43783
43823
  const mainIdKey = DataManager.isBleConnect(env) ? 'id' : 'session';
43784
- const protocolHint = expectedProtocol ? undefined : this.originalDescriptor.protocolType;
43824
+ const previousProtocol = this.originalDescriptor.protocolType;
43825
+ const strictProtocol = (options === null || options === void 0 ? void 0 : options.forceProtocolDetection)
43826
+ ? undefined
43827
+ : expectedProtocol !== null && expectedProtocol !== void 0 ? expectedProtocol : this.originalDescriptor.protocolType;
43785
43828
  try {
43786
43829
  let acquireResult;
43787
43830
  if (DataManager.isBleConnect(env)) {
43788
- acquireResult = yield ((_a = this.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.originalDescriptor.id, undefined, true, expectedProtocol, protocolHint));
43831
+ acquireResult = yield ((_a = this.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.originalDescriptor.id, undefined, true, strictProtocol, undefined));
43789
43832
  this.mainId = (_b = acquireResult === null || acquireResult === void 0 ? void 0 : acquireResult.uuid) !== null && _b !== void 0 ? _b : '';
43790
43833
  Log$e.debug('Expected uuid:', this.mainId);
43791
43834
  }
43792
43835
  else {
43793
- acquireResult = yield ((_c = this.deviceConnector) === null || _c === void 0 ? void 0 : _c.acquire(this.originalDescriptor.path, this.originalDescriptor.session, undefined, expectedProtocol, protocolHint));
43836
+ acquireResult = yield ((_c = this.deviceConnector) === null || _c === void 0 ? void 0 : _c.acquire(this.originalDescriptor.path, this.originalDescriptor.session, undefined, strictProtocol, undefined));
43794
43837
  this.mainId = acquireResult;
43795
43838
  Log$e.debug('Expected session id:', this.mainId);
43796
43839
  }
43797
- this.deviceAcquired = true;
43798
- this.updateDescriptor({ [mainIdKey]: this.mainId });
43799
43840
  const detectedProtocol = (_d = acquireResult === null || acquireResult === void 0 ? void 0 : acquireResult.protocolType) !== null && _d !== void 0 ? _d : (_f = (_e = TransportManager.transport) === null || _e === void 0 ? void 0 : _e.getProtocolType) === null || _f === void 0 ? void 0 : _f.call(_e, DataManager.isBleConnect(env) ? this.originalDescriptor.id : this.originalDescriptor.path);
43841
+ if ((options === null || options === void 0 ? void 0 : options.forceProtocolDetection) && !detectedProtocol) {
43842
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Active protocol detection returned no protocol for ${this.originalDescriptor.path || this.originalDescriptor.id}`);
43843
+ }
43800
43844
  if (detectedProtocol) {
43801
43845
  this.originalDescriptor.protocolType = detectedProtocol;
43802
43846
  }
43847
+ this.deviceAcquired = true;
43848
+ this.updateDescriptor({ [mainIdKey]: this.mainId });
43803
43849
  if (this.commands) {
43804
43850
  yield this.commands.dispose(false);
43805
43851
  }
43806
43852
  this.commands = new DeviceCommands(this, (_g = this.mainId) !== null && _g !== void 0 ? _g : '');
43807
43853
  }
43808
43854
  catch (error) {
43855
+ if (options === null || options === void 0 ? void 0 : options.forceProtocolDetection) {
43856
+ this.originalDescriptor.protocolType = previousProtocol;
43857
+ const failedSession = this.mainId;
43858
+ this.deviceAcquired = false;
43859
+ if (failedSession) {
43860
+ try {
43861
+ yield ((_j = (_h = this.deviceConnector) === null || _h === void 0 ? void 0 : _h.release) === null || _j === void 0 ? void 0 : _j.call(_h, failedSession, false));
43862
+ }
43863
+ catch (releaseError) {
43864
+ Log$e.debug('Failed to release an unsuccessful protocol probe', releaseError);
43865
+ }
43866
+ }
43867
+ if (!DataManager.isBleConnect(env)) {
43868
+ this.mainId = null;
43869
+ this.updateDescriptor({ session: null });
43870
+ }
43871
+ }
43809
43872
  if (options === null || options === void 0 ? void 0 : options.throwOnRunPromiseError) {
43810
43873
  throw error;
43811
43874
  }
@@ -44527,11 +44590,16 @@ class Device extends events.exports {
44527
44590
  this.runPromise = null;
44528
44591
  }
44529
44592
  };
44530
- if (!this.isUsedHere() || this.commands.disposed) {
44531
- const env = DataManager.getSettings('env');
44593
+ const env = DataManager.getSettings('env');
44594
+ if (options.forceProtocolDetection && env !== 'react-native' && this.isUsedHere()) {
44595
+ yield this.release();
44596
+ }
44597
+ if (options.forceProtocolDetection || !this.isUsedHere() || this.commands.disposed) {
44532
44598
  if (env !== 'react-native') {
44533
44599
  try {
44534
- yield this.acquire(options.connectProtocol);
44600
+ yield this.acquire(options.connectProtocol, {
44601
+ forceProtocolDetection: options.forceProtocolDetection,
44602
+ });
44535
44603
  }
44536
44604
  catch (error) {
44537
44605
  clearRunPromise();
@@ -45355,7 +45423,8 @@ class SearchDevices extends BaseMethod {
45355
45423
  for (const descriptor of devicesDescriptor) {
45356
45424
  try {
45357
45425
  const result = yield DevicePool.getDevices([descriptor], descriptor.path, {
45358
- connectProtocol: this.payload.connectProtocol,
45426
+ connectProtocol: undefined,
45427
+ forceProtocolDetection: true,
45359
45428
  refreshRuntimeState: true,
45360
45429
  });
45361
45430
  deviceList.push(...result.deviceList);
@@ -45372,6 +45441,21 @@ class SearchDevices extends BaseMethod {
45372
45441
  }
45373
45442
  }
45374
45443
 
45444
+ class DetectDeviceConnectProtocol extends BaseMethod {
45445
+ init() {
45446
+ this.payload.forceProtocolDetection = true;
45447
+ this.useDevicePassphraseState = false;
45448
+ this.skipForceUpdateCheck = true;
45449
+ this.unlockPolicy = 'none';
45450
+ }
45451
+ getSupportedProtocols() {
45452
+ return ['V1', 'V2'];
45453
+ }
45454
+ run() {
45455
+ return Promise.resolve(this.device.getProtocol());
45456
+ }
45457
+ }
45458
+
45375
45459
  class GetFeatures extends BaseMethod {
45376
45460
  init() {
45377
45461
  this.unlockPolicy = 'none';
@@ -46349,7 +46433,7 @@ class CheckAllFirmwareRelease extends BaseMethod {
46349
46433
  let resourceStatus = 'unknown';
46350
46434
  if (resources === null || resources === void 0 ? void 0 : resources.length) {
46351
46435
  const loaderMode = state.status.mode === 'bootloader' || state.status.mode === 'romloader';
46352
- if (loaderMode || state.status.mode === 'normal') {
46436
+ if (loaderMode) {
46353
46437
  try {
46354
46438
  const inventory = yield readProtocolV2ResourceInventory({
46355
46439
  commands: this.device.getCommands(),
@@ -46358,13 +46442,13 @@ class CheckAllFirmwareRelease extends BaseMethod {
46358
46442
  resourceStatus = buildProtocolV2ResourceUpdatePlan({
46359
46443
  resources,
46360
46444
  inventory,
46361
- mode: loaderMode ? 'bootloader-recovery' : 'application',
46445
+ mode: 'bootloader-recovery',
46362
46446
  }).status;
46363
46447
  }
46364
46448
  catch (_b) {
46365
46449
  resourceStatus = buildProtocolV2ResourceUpdatePlan({
46366
46450
  resources,
46367
- mode: loaderMode ? 'bootloader-recovery' : 'application',
46451
+ mode: 'bootloader-recovery',
46368
46452
  }).status;
46369
46453
  }
46370
46454
  }
@@ -47370,7 +47454,7 @@ const newTouchUpdateProcess = (updateType, postMessage, device, { payload }, reb
47370
47454
  try {
47371
47455
  if (isBleReconnect) {
47372
47456
  try {
47373
- yield ((_d = device.deviceConnector) === null || _d === void 0 ? void 0 : _d.acquire(device.originalDescriptor.id, null, true));
47457
+ yield ((_d = device.deviceConnector) === null || _d === void 0 ? void 0 : _d.acquire(device.originalDescriptor.id, null, true, device.originalDescriptor.protocolType));
47374
47458
  const typedCall = device.getCommands().typedCall.bind(device.getCommands());
47375
47459
  yield Promise.race([
47376
47460
  typedCall('Initialize', 'Features', {}),
@@ -47388,7 +47472,7 @@ const newTouchUpdateProcess = (updateType, postMessage, device, { payload }, reb
47388
47472
  else {
47389
47473
  const deviceDiff = yield ((_e = device.deviceConnector) === null || _e === void 0 ? void 0 : _e.enumerate());
47390
47474
  const devicesDescriptor = (_f = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _f !== void 0 ? _f : [];
47391
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, device.originalDescriptor.id);
47475
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, device.originalDescriptor.id, { connectProtocol: device.originalDescriptor.protocolType });
47392
47476
  if (deviceList.length === 1) {
47393
47477
  device.updateFromCache(deviceList[0]);
47394
47478
  yield device.acquire();
@@ -47451,14 +47535,16 @@ const emmcFileWriteWithRetry = (device, filePath, chunkLength, offset, chunk, ov
47451
47535
  const env = DataManager.getSettings('env');
47452
47536
  if (DataManager.isBleConnect(env)) {
47453
47537
  yield wait(3000);
47454
- yield ((_h = device.deviceConnector) === null || _h === void 0 ? void 0 : _h.acquire(device.originalDescriptor.id, null, true));
47538
+ yield ((_h = device.deviceConnector) === null || _h === void 0 ? void 0 : _h.acquire(device.originalDescriptor.id, null, true, device.originalDescriptor.protocolType));
47455
47539
  yield device.initialize();
47456
47540
  }
47457
47541
  else if (((_j = error === null || error === void 0 ? void 0 : error.message) === null || _j === void 0 ? void 0 : _j.indexOf(SESSION_ERROR$2)) > -1 ||
47458
47542
  ((_l = (_k = error === null || error === void 0 ? void 0 : error.response) === null || _k === void 0 ? void 0 : _k.data) === null || _l === void 0 ? void 0 : _l.indexOf(SESSION_ERROR$2)) > -1) {
47459
47543
  const deviceDiff = yield ((_m = device.deviceConnector) === null || _m === void 0 ? void 0 : _m.enumerate());
47460
47544
  const devicesDescriptor = (_o = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _o !== void 0 ? _o : [];
47461
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined);
47545
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined, {
47546
+ connectProtocol: device.originalDescriptor.protocolType,
47547
+ });
47462
47548
  if (deviceList.length === 1 && ((_p = deviceList[0]) === null || _p === void 0 ? void 0 : _p.isBootloader())) {
47463
47549
  device.updateFromCache(deviceList[0]);
47464
47550
  yield device.acquire();
@@ -47674,7 +47760,7 @@ class FirmwareUpdateBaseMethod extends BaseMethod {
47674
47760
  const isTouchOrProDevice = ((_a = this === null || this === void 0 ? void 0 : this.device) === null || _a === void 0 ? void 0 : _a.getCurrentDeviceType()) === hdShared.EDeviceType.Touch ||
47675
47761
  ((_b = this === null || this === void 0 ? void 0 : this.device) === null || _b === void 0 ? void 0 : _b.getCurrentDeviceType()) === hdShared.EDeviceType.Pro;
47676
47762
  const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
47677
- var _c, _d, _e;
47763
+ var _c, _d, _e, _f;
47678
47764
  checkCount += 1;
47679
47765
  Log$9.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] isFirstCheck: ', isFirstCheck);
47680
47766
  if (isTouchOrProDevice && isFirstCheck) {
@@ -47709,11 +47795,11 @@ class FirmwareUpdateBaseMethod extends BaseMethod {
47709
47795
  }
47710
47796
  if (isBleReconnect) {
47711
47797
  try {
47712
- yield ((_d = this.device.deviceConnector) === null || _d === void 0 ? void 0 : _d.acquire(this.device.originalDescriptor.id, null, true));
47798
+ yield ((_d = this.device.deviceConnector) === null || _d === void 0 ? void 0 : _d.acquire(this.device.originalDescriptor.id, null, true, (_e = this.payload.connectProtocol) !== null && _e !== void 0 ? _e : this.device.originalDescriptor.protocolType));
47713
47799
  yield this.device.initialize();
47714
47800
  if (this.device.isBootloader()) {
47715
47801
  clearInterval(intervalTimer);
47716
- (_e = this.checkPromise) === null || _e === void 0 ? void 0 : _e.resolve(true);
47802
+ (_f = this.checkPromise) === null || _f === void 0 ? void 0 : _f.resolve(true);
47717
47803
  }
47718
47804
  }
47719
47805
  catch (e) {
@@ -47732,19 +47818,21 @@ class FirmwareUpdateBaseMethod extends BaseMethod {
47732
47818
  }, 30000);
47733
47819
  }
47734
47820
  _checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer) {
47735
- var _a, _b, _c, _d;
47821
+ var _a, _b, _c, _d, _e;
47736
47822
  return __awaiter(this, void 0, void 0, function* () {
47737
47823
  const deviceDiff = yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.enumerate());
47738
47824
  const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
47739
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId);
47740
- if (deviceList.length === 1 && ((_c = deviceList[0]) === null || _c === void 0 ? void 0 : _c.isBootloader())) {
47825
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId, {
47826
+ connectProtocol: (_c = this.payload.connectProtocol) !== null && _c !== void 0 ? _c : this.device.originalDescriptor.protocolType,
47827
+ });
47828
+ if (deviceList.length === 1 && ((_d = deviceList[0]) === null || _d === void 0 ? void 0 : _d.isBootloader())) {
47741
47829
  this.device.updateFromCache(deviceList[0]);
47742
47830
  this.device.commands.disposed = false;
47743
47831
  if (intervalTimer)
47744
47832
  clearInterval(intervalTimer);
47745
47833
  if (timeoutTimer)
47746
47834
  clearTimeout(timeoutTimer);
47747
- (_d = this.checkPromise) === null || _d === void 0 ? void 0 : _d.resolve(true);
47835
+ (_e = this.checkPromise) === null || _e === void 0 ? void 0 : _e.resolve(true);
47748
47836
  return true;
47749
47837
  }
47750
47838
  return false;
@@ -47855,10 +47943,10 @@ class FirmwareUpdateBaseMethod extends BaseMethod {
47855
47943
  });
47856
47944
  }
47857
47945
  emmcFileWriteWithRetry(filePath, chunkLength, offset, chunk, overwrite, progress) {
47858
- var _a, _b, _c, _d, _e, _f, _g, _h;
47946
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
47859
47947
  return __awaiter(this, void 0, void 0, function* () {
47860
47948
  const writeFunc = () => __awaiter(this, void 0, void 0, function* () {
47861
- var _j;
47949
+ var _l;
47862
47950
  const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
47863
47951
  const writeRes = yield typedCall('EmmcFileWrite', 'EmmcFile', {
47864
47952
  file: {
@@ -47873,7 +47961,7 @@ class FirmwareUpdateBaseMethod extends BaseMethod {
47873
47961
  });
47874
47962
  if (writeRes.type !== 'EmmcFile') {
47875
47963
  if (writeRes.type === 'CallMethodError') {
47876
- if (((_j = writeRes.message.error) !== null && _j !== void 0 ? _j : '').indexOf(SESSION_ERROR$1) > -1) {
47964
+ if (((_l = writeRes.message.error) !== null && _l !== void 0 ? _l : '').indexOf(SESSION_ERROR$1) > -1) {
47877
47965
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, SESSION_ERROR$1);
47878
47966
  }
47879
47967
  }
@@ -47896,18 +47984,20 @@ class FirmwareUpdateBaseMethod extends BaseMethod {
47896
47984
  const env = DataManager.getSettings('env');
47897
47985
  if (DataManager.isBleConnect(env)) {
47898
47986
  yield wait(3000);
47899
- yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true));
47987
+ yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true, (_b = this.payload.connectProtocol) !== null && _b !== void 0 ? _b : this.device.originalDescriptor.protocolType));
47900
47988
  yield this.device.initialize();
47901
47989
  }
47902
- else if (((_b = error === null || error === void 0 ? void 0 : error.message) === null || _b === void 0 ? void 0 : _b.indexOf(SESSION_ERROR$1)) > -1 ||
47903
- ((_d = (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.indexOf(SESSION_ERROR$1)) > -1) {
47904
- const deviceDiff = yield ((_e = this.device.deviceConnector) === null || _e === void 0 ? void 0 : _e.enumerate());
47905
- const devicesDescriptor = (_f = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _f !== void 0 ? _f : [];
47906
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined);
47907
- if (deviceList.length === 1 && ((_g = deviceList[0]) === null || _g === void 0 ? void 0 : _g.isBootloader())) {
47990
+ else if (((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.indexOf(SESSION_ERROR$1)) > -1 ||
47991
+ ((_e = (_d = error === null || error === void 0 ? void 0 : error.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.indexOf(SESSION_ERROR$1)) > -1) {
47992
+ const deviceDiff = yield ((_f = this.device.deviceConnector) === null || _f === void 0 ? void 0 : _f.enumerate());
47993
+ const devicesDescriptor = (_g = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _g !== void 0 ? _g : [];
47994
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined, {
47995
+ connectProtocol: (_h = this.payload.connectProtocol) !== null && _h !== void 0 ? _h : this.device.originalDescriptor.protocolType,
47996
+ });
47997
+ if (deviceList.length === 1 && ((_j = deviceList[0]) === null || _j === void 0 ? void 0 : _j.isBootloader())) {
47908
47998
  this.device.updateFromCache(deviceList[0]);
47909
47999
  yield this.device.acquire();
47910
- this.device.getCommands().mainId = (_h = this.device.mainId) !== null && _h !== void 0 ? _h : '';
48000
+ this.device.getCommands().mainId = (_k = this.device.mainId) !== null && _k !== void 0 ? _k : '';
47911
48001
  }
47912
48002
  }
47913
48003
  yield wait(2000);
@@ -48126,14 +48216,14 @@ class FirmwareUpdate extends BaseMethod {
48126
48216
  const isBleReconnect = connectId && DataManager.isBleConnect(env);
48127
48217
  Log$8.log('FirmwareUpdate [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
48128
48218
  const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
48129
- var _a, _b, _c, _d, _e, _f;
48219
+ var _a, _b, _c, _d, _e, _f, _g, _h;
48130
48220
  if (isBleReconnect) {
48131
48221
  try {
48132
- yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true));
48222
+ yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true, (_b = this.payload.connectProtocol) !== null && _b !== void 0 ? _b : this.device.originalDescriptor.protocolType));
48133
48223
  yield this.device.initialize();
48134
48224
  if (this.device.isBootloader()) {
48135
48225
  clearInterval(intervalTimer);
48136
- (_b = this.checkPromise) === null || _b === void 0 ? void 0 : _b.resolve(true);
48226
+ (_c = this.checkPromise) === null || _c === void 0 ? void 0 : _c.resolve(true);
48137
48227
  }
48138
48228
  }
48139
48229
  catch (e) {
@@ -48141,14 +48231,16 @@ class FirmwareUpdate extends BaseMethod {
48141
48231
  }
48142
48232
  }
48143
48233
  else {
48144
- const deviceDiff = yield ((_c = this.device.deviceConnector) === null || _c === void 0 ? void 0 : _c.enumerate());
48145
- const devicesDescriptor = (_d = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _d !== void 0 ? _d : [];
48146
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId);
48147
- if (deviceList.length === 1 && ((_e = deviceList[0]) === null || _e === void 0 ? void 0 : _e.isBootloader())) {
48234
+ const deviceDiff = yield ((_d = this.device.deviceConnector) === null || _d === void 0 ? void 0 : _d.enumerate());
48235
+ const devicesDescriptor = (_e = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _e !== void 0 ? _e : [];
48236
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId, {
48237
+ connectProtocol: (_f = this.payload.connectProtocol) !== null && _f !== void 0 ? _f : this.device.originalDescriptor.protocolType,
48238
+ });
48239
+ if (deviceList.length === 1 && ((_g = deviceList[0]) === null || _g === void 0 ? void 0 : _g.isBootloader())) {
48148
48240
  this.device.updateFromCache(deviceList[0]);
48149
48241
  this.device.commands.disposed = false;
48150
48242
  clearInterval(intervalTimer);
48151
- (_f = this.checkPromise) === null || _f === void 0 ? void 0 : _f.resolve(true);
48243
+ (_h = this.checkPromise) === null || _h === void 0 ? void 0 : _h.resolve(true);
48152
48244
  }
48153
48245
  }
48154
48246
  }), isBleReconnect ? 3000 : 2000);
@@ -48354,7 +48446,7 @@ class FirmwareUpdateV2 extends BaseMethod {
48354
48446
  const deviceType = (_a = this.device) === null || _a === void 0 ? void 0 : _a.getCurrentDeviceType();
48355
48447
  const isTouchOrProDevice = deviceType === hdShared.EDeviceType.Touch || deviceType === hdShared.EDeviceType.Pro;
48356
48448
  const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
48357
- var _b, _c, _d;
48449
+ var _b, _c, _d, _e;
48358
48450
  checkCount += 1;
48359
48451
  Log$7.log('FirmwareUpdateV2 [checkDeviceToBootloader] isFirstCheck: ', isFirstCheck);
48360
48452
  if (isTouchOrProDevice && isFirstCheck) {
@@ -48382,11 +48474,11 @@ class FirmwareUpdateV2 extends BaseMethod {
48382
48474
  }
48383
48475
  if (isBleReconnect) {
48384
48476
  try {
48385
- yield ((_c = this.device.deviceConnector) === null || _c === void 0 ? void 0 : _c.acquire(this.device.originalDescriptor.id, null, true));
48477
+ yield ((_c = this.device.deviceConnector) === null || _c === void 0 ? void 0 : _c.acquire(this.device.originalDescriptor.id, null, true, (_d = this.payload.connectProtocol) !== null && _d !== void 0 ? _d : this.device.originalDescriptor.protocolType));
48386
48478
  yield this.device.initialize();
48387
48479
  if (this.device.isBootloader()) {
48388
48480
  clearInterval(intervalTimer);
48389
- (_d = this.checkPromise) === null || _d === void 0 ? void 0 : _d.resolve(true);
48481
+ (_e = this.checkPromise) === null || _e === void 0 ? void 0 : _e.resolve(true);
48390
48482
  }
48391
48483
  }
48392
48484
  catch (e) {
@@ -48405,19 +48497,21 @@ class FirmwareUpdateV2 extends BaseMethod {
48405
48497
  }, 30000);
48406
48498
  }
48407
48499
  _checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer) {
48408
- var _a, _b, _c, _d;
48500
+ var _a, _b, _c, _d, _e;
48409
48501
  return __awaiter(this, void 0, void 0, function* () {
48410
48502
  const deviceDiff = yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.enumerate());
48411
48503
  const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
48412
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId);
48413
- if (deviceList.length === 1 && ((_c = deviceList[0]) === null || _c === void 0 ? void 0 : _c.isBootloader())) {
48504
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId, {
48505
+ connectProtocol: (_c = this.payload.connectProtocol) !== null && _c !== void 0 ? _c : this.device.originalDescriptor.protocolType,
48506
+ });
48507
+ if (deviceList.length === 1 && ((_d = deviceList[0]) === null || _d === void 0 ? void 0 : _d.isBootloader())) {
48414
48508
  this.device.updateFromCache(deviceList[0]);
48415
48509
  this.device.commands.disposed = false;
48416
48510
  if (intervalTimer)
48417
48511
  clearInterval(intervalTimer);
48418
48512
  if (timeoutTimer)
48419
48513
  clearTimeout(timeoutTimer);
48420
- (_d = this.checkPromise) === null || _d === void 0 ? void 0 : _d.resolve(true);
48514
+ (_e = this.checkPromise) === null || _e === void 0 ? void 0 : _e.resolve(true);
48421
48515
  return true;
48422
48516
  }
48423
48517
  return false;
@@ -48945,7 +49039,7 @@ class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod {
48945
49039
  this.device.listenerCount(DEVICE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE) > 0);
48946
49040
  }
48947
49041
  waitForDeviceReconnect(timeout) {
48948
- var _a, _b, _c, _d;
49042
+ var _a, _b, _c, _d, _e, _f;
48949
49043
  return __awaiter(this, void 0, void 0, function* () {
48950
49044
  const startTime = Date.now();
48951
49045
  const isBleReconnect = this.isBleReconnect();
@@ -48954,7 +49048,7 @@ class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod {
48954
49048
  try {
48955
49049
  if (isBleReconnect) {
48956
49050
  try {
48957
- yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true));
49051
+ yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true, (_b = this.payload.connectProtocol) !== null && _b !== void 0 ? _b : this.device.originalDescriptor.protocolType));
48958
49052
  const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
48959
49053
  yield Promise.race([
48960
49054
  typedCall('Initialize', 'Features', {}),
@@ -48971,8 +49065,8 @@ class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod {
48971
49065
  }
48972
49066
  }
48973
49067
  else {
48974
- const deviceDiff = yield ((_b = this.device.deviceConnector) === null || _b === void 0 ? void 0 : _b.enumerate());
48975
- const devicesDescriptor = (_c = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _c !== void 0 ? _c : [];
49068
+ const deviceDiff = yield ((_c = this.device.deviceConnector) === null || _c === void 0 ? void 0 : _c.enumerate());
49069
+ const devicesDescriptor = (_d = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _d !== void 0 ? _d : [];
48976
49070
  const canPromptSwitchFirmwareReconnect = this.canPromptWebUsbSwitchFirmwareReconnect();
48977
49071
  if (canPromptSwitchFirmwareReconnect) {
48978
49072
  webUsbCheckCount += 1;
@@ -48990,12 +49084,14 @@ class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod {
48990
49084
  else {
48991
49085
  webUsbCheckCount = 0;
48992
49086
  }
48993
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, this.connectId);
49087
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, this.connectId, {
49088
+ connectProtocol: (_e = this.payload.connectProtocol) !== null && _e !== void 0 ? _e : this.device.originalDescriptor.protocolType,
49089
+ });
48994
49090
  if (deviceList.length === 1) {
48995
49091
  this.device.updateFromCache(deviceList[0]);
48996
49092
  yield this.device.acquire();
48997
49093
  this.device.commands.disposed = false;
48998
- this.device.getCommands().mainId = (_d = this.device.mainId) !== null && _d !== void 0 ? _d : '';
49094
+ this.device.getCommands().mainId = (_f = this.device.mainId) !== null && _f !== void 0 ? _f : '';
48999
49095
  return;
49000
49096
  }
49001
49097
  }
@@ -49409,13 +49505,14 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
49409
49505
  });
49410
49506
  }
49411
49507
  runProtocolV2() {
49412
- var _a, _b, _c, _d, _e;
49508
+ var _a, _b, _c, _d, _e, _f;
49413
49509
  return __awaiter(this, void 0, void 0, function* () {
49414
49510
  yield this.captureProtocolV2PhysicalIdentity();
49415
49511
  const deviceFeatures = yield this.getProtocolV2DeviceFeatures();
49416
49512
  const deviceFirmwareType = getFirmwareType(deviceFeatures);
49417
49513
  const firmwareType = (_a = this.params.firmwareType) !== null && _a !== void 0 ? _a : deviceFirmwareType;
49418
- const resourceRecoveryMode = Boolean(this.isProtocolV2BootloaderMode() || this.isProtocolV2RomloaderMode());
49514
+ const needsRemoteResources = !((_b = this.params.resourceBundleFiles) === null || _b === void 0 ? void 0 : _b.length) &&
49515
+ !!((_c = this.params.targetsToUpdate) === null || _c === void 0 ? void 0 : _c.includes('resource'));
49419
49516
  let fwBinaryMap = [];
49420
49517
  let bootloaderBinary = null;
49421
49518
  let bootResourcesInstallItem;
@@ -49423,11 +49520,10 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
49423
49520
  let resourceBundles;
49424
49521
  try {
49425
49522
  this.postTipMessage(exports.FirmwareUpdateTipMessage.StartDownloadFirmware);
49523
+ resourceBundles = this.prepareExplicitProtocolV2ResourceBundles();
49426
49524
  fwBinaryMap = this.collectExplicitTargetBinaries();
49427
49525
  bootloaderBinary = this.prepareBootloaderBinary();
49428
49526
  const needsRemoteFirmware = !this.hasExplicitProtocolV2Payload(fwBinaryMap);
49429
- const needsRemoteResources = !((_b = this.params.resourceBundleFiles) === null || _b === void 0 ? void 0 : _b.length) &&
49430
- !!((_c = this.params.targetsToUpdate) === null || _c === void 0 ? void 0 : _c.includes('resource'));
49431
49527
  const needsRemoteBootResources = !this.params.bootResourcesBinary &&
49432
49528
  !!((_d = this.params.targetsToUpdate) === null || _d === void 0 ? void 0 : _d.includes('boot_resources'));
49433
49529
  if (needsRemoteFirmware || needsRemoteResources || needsRemoteBootResources) {
@@ -49446,8 +49542,9 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
49446
49542
  ...(installItems !== null && installItems !== void 0 ? installItems : this.buildProtocolV2InstallItems({ bootloaderBinary, fwBinaryMap })),
49447
49543
  ];
49448
49544
  }
49449
- resourceBundles = yield this.prepareProtocolV2ResourceBundles(resourceRecoveryMode);
49450
- this.postTipMessage(exports.FirmwareUpdateTipMessage.FinishDownloadFirmware);
49545
+ if (!needsRemoteResources) {
49546
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.FinishDownloadFirmware);
49547
+ }
49451
49548
  }
49452
49549
  catch (err) {
49453
49550
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateDownloadFailed, (_e = err.message) !== null && _e !== void 0 ? _e : err);
@@ -49455,14 +49552,28 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
49455
49552
  if (!bootloaderBinary &&
49456
49553
  fwBinaryMap.length === 0 &&
49457
49554
  !(installItems === null || installItems === void 0 ? void 0 : installItems.length) &&
49458
- !(resourceBundles === null || resourceBundles === void 0 ? void 0 : resourceBundles.length)) {
49459
- if (resourceBundles !== undefined) {
49460
- this.postTipMessage(exports.FirmwareUpdateTipMessage.FirmwareUpdateCompleted);
49461
- return this.getProtocolV2VersionResult(deviceFeatures);
49462
- }
49555
+ !(resourceBundles === null || resourceBundles === void 0 ? void 0 : resourceBundles.length) &&
49556
+ !needsRemoteResources) {
49463
49557
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateDownloadFailed, 'No firmware to update');
49464
49558
  }
49465
- yield this.enterProtocolV2BootloaderMode();
49559
+ const enteredBootloader = yield this.enterProtocolV2BootloaderMode();
49560
+ if (needsRemoteResources) {
49561
+ try {
49562
+ resourceBundles = yield this.prepareProtocolV2ResourceBundles();
49563
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.FinishDownloadFirmware);
49564
+ }
49565
+ catch (err) {
49566
+ if (enteredBootloader) {
49567
+ try {
49568
+ yield this.exitProtocolV2BootloaderToNormal();
49569
+ }
49570
+ catch (restoreError) {
49571
+ Log$5.warn('[FirmwareUpdateV4] failed to restore App mode after resource preparation error:', restoreError);
49572
+ }
49573
+ }
49574
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateDownloadFailed, (_f = err.message) !== null && _f !== void 0 ? _f : err);
49575
+ }
49576
+ }
49466
49577
  yield this.executeProtocolV2Update(Object.assign(Object.assign({ fwBinaryMap,
49467
49578
  bootloaderBinary }, (installItems ? { installItems } : undefined)), ((resourceBundles === null || resourceBundles === void 0 ? void 0 : resourceBundles.length) ? { resourceBundles } : undefined)));
49468
49579
  yield this.exitProtocolV2BootloaderToNormal();
@@ -49683,21 +49794,24 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
49683
49794
  };
49684
49795
  });
49685
49796
  }
49686
- prepareProtocolV2ResourceBundles(recoveryMode) {
49687
- var _a, _b;
49797
+ prepareExplicitProtocolV2ResourceBundles() {
49798
+ var _a;
49799
+ if (!((_a = this.params.resourceBundleFiles) === null || _a === void 0 ? void 0 : _a.length))
49800
+ return undefined;
49801
+ return this.params.resourceBundleFiles.map((file, index) => {
49802
+ var _a;
49803
+ const devicePath = validateProtocolV2FilesystemPath(file.devicePath, `resourceBundleFiles[${index}].devicePath`);
49804
+ return {
49805
+ name: (_a = devicePath.split('/').pop()) !== null && _a !== void 0 ? _a : devicePath,
49806
+ binary: file.binary,
49807
+ devicePath,
49808
+ };
49809
+ });
49810
+ }
49811
+ prepareProtocolV2ResourceBundles() {
49812
+ var _a;
49688
49813
  return __awaiter(this, void 0, void 0, function* () {
49689
- if ((_a = this.params.resourceBundleFiles) === null || _a === void 0 ? void 0 : _a.length) {
49690
- return this.params.resourceBundleFiles.map((file, index) => {
49691
- var _a;
49692
- const devicePath = validateProtocolV2FilesystemPath(file.devicePath, `resourceBundleFiles[${index}].devicePath`);
49693
- return {
49694
- name: (_a = devicePath.split('/').pop()) !== null && _a !== void 0 ? _a : devicePath,
49695
- binary: file.binary,
49696
- devicePath,
49697
- };
49698
- });
49699
- }
49700
- if (!((_b = this.params.targetsToUpdate) === null || _b === void 0 ? void 0 : _b.includes('resource'))) {
49814
+ if (!((_a = this.params.targetsToUpdate) === null || _a === void 0 ? void 0 : _a.includes('resource'))) {
49701
49815
  return undefined;
49702
49816
  }
49703
49817
  const resources = DataManager.getProtocolV2Resources();
@@ -49715,10 +49829,10 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
49715
49829
  const plan = buildProtocolV2ResourceUpdatePlan({
49716
49830
  resources,
49717
49831
  inventory,
49718
- mode: recoveryMode ? 'bootloader-recovery' : 'application',
49832
+ mode: 'bootloader-recovery',
49719
49833
  forced: this.params.forcedUpdateRes,
49720
49834
  });
49721
- Log$5.log(`[FirmwareUpdateV4] Pro2 resource plan mode=${recoveryMode ? 'bootloader-recovery' : 'application'} status=${plan.status} count=${plan.resources.length}`);
49835
+ Log$5.log(`[FirmwareUpdateV4] Pro2 resource plan mode=bootloader-recovery status=${plan.status} count=${plan.resources.length}`);
49722
49836
  const bundles = [];
49723
49837
  for (const resource of plan.resources) {
49724
49838
  bundles.push(yield this.downloadProtocolV2Resource(resource));
@@ -50099,7 +50213,7 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
50099
50213
  });
50100
50214
  }
50101
50215
  reconnectProtocolV2Device() {
50102
- var _a, _b, _c, _d;
50216
+ var _a, _b, _c, _d, _e, _f;
50103
50217
  return __awaiter(this, void 0, void 0, function* () {
50104
50218
  if (this.isBleReconnect()) {
50105
50219
  yield this.acquireProtocolV2BleDevice();
@@ -50118,14 +50232,23 @@ class FirmwareUpdateV4 extends FirmwareUpdateBaseMethod {
50118
50232
  const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined, {
50119
50233
  connectProtocol: PROTOCOL_V2_CONNECT_PROTOCOL,
50120
50234
  });
50121
- if (deviceList.length !== 1) {
50235
+ const expectedSerialNumber = (_d = this.protocolV2ExpectedSerialNumber) === null || _d === void 0 ? void 0 : _d.trim();
50236
+ const identityMatch = expectedSerialNumber
50237
+ ? deviceList.find(candidate => { var _a; return ((_a = candidate.getCurrentSerialNo) === null || _a === void 0 ? void 0 : _a.call(candidate).trim()) === expectedSerialNumber; })
50238
+ : undefined;
50239
+ const singleCandidate = deviceList.length === 1 ? deviceList.at(0) : undefined;
50240
+ const singleCandidateSerialNumber = (_e = singleCandidate === null || singleCandidate === void 0 ? void 0 : singleCandidate.getCurrentSerialNo) === null || _e === void 0 ? void 0 : _e.call(singleCandidate).trim();
50241
+ const reconnectDevice = identityMatch !== null && identityMatch !== void 0 ? identityMatch : (singleCandidate && (!expectedSerialNumber || !singleCandidateSerialNumber)
50242
+ ? singleCandidate
50243
+ : undefined);
50244
+ if (!reconnectDevice) {
50122
50245
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotFound);
50123
50246
  }
50124
- Log$5.debug('Protocol V2 firmware reconnect using single enumerated device:', deviceList[0].getConnectId());
50125
- this.device.updateFromCache(deviceList[0]);
50247
+ Log$5.debug('Protocol V2 firmware reconnect using matched device:', reconnectDevice.getConnectId());
50248
+ this.device.updateFromCache(reconnectDevice);
50126
50249
  yield this.ensureProtocolV2DeviceAcquired();
50127
50250
  this.device.commands.disposed = false;
50128
- this.device.getCommands().mainId = (_d = this.device.mainId) !== null && _d !== void 0 ? _d : '';
50251
+ this.device.getCommands().mainId = (_f = this.device.mainId) !== null && _f !== void 0 ? _f : '';
50129
50252
  });
50130
50253
  }
50131
50254
  ensureProtocolV2DeviceAcquired() {
@@ -60548,6 +60671,7 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
60548
60671
  testProtocolV2Ping: Ping,
60549
60672
  preInitialize: PreInitialize,
60550
60673
  searchDevices: SearchDevices,
60674
+ detectDeviceConnectProtocol: DetectDeviceConnectProtocol,
60551
60675
  getFeatures: GetFeatures,
60552
60676
  getDeviceState: GetDeviceState,
60553
60677
  getOnekeyFeatures: GetOnekeyFeatures,
@@ -61054,6 +61178,7 @@ const parseInitOptions = (method) => ({
61054
61178
  deviceId: method === null || method === void 0 ? void 0 : method.payload.deviceId,
61055
61179
  deriveCardano: method && hasDeriveCardano(method),
61056
61180
  connectProtocol: method === null || method === void 0 ? void 0 : method.payload.connectProtocol,
61181
+ forceProtocolDetection: method === null || method === void 0 ? void 0 : method.payload.forceProtocolDetection,
61057
61182
  protocolV2DeviceInfoTimeoutMs: method === null || method === void 0 ? void 0 : method.payload.protocolV2DeviceInfoTimeoutMs,
61058
61183
  });
61059
61184
  let _core;
@@ -61608,9 +61733,17 @@ function connectDeviceForBle(method, device, retryCount = 0) {
61608
61733
  var _a;
61609
61734
  return __awaiter(this, void 0, void 0, function* () {
61610
61735
  try {
61611
- const shouldAcquire = !device.hasDeviceAcquire() || !device.commands || device.commands.disposed;
61736
+ if (method.payload.forceProtocolDetection && device.hasDeviceAcquire()) {
61737
+ yield device.release();
61738
+ }
61739
+ const shouldAcquire = method.payload.forceProtocolDetection ||
61740
+ !device.hasDeviceAcquire() ||
61741
+ !device.commands ||
61742
+ device.commands.disposed;
61612
61743
  if (shouldAcquire) {
61613
- yield device.acquire(method.payload.connectProtocol);
61744
+ yield device.acquire(method.payload.connectProtocol, {
61745
+ forceProtocolDetection: method.payload.forceProtocolDetection,
61746
+ });
61614
61747
  }
61615
61748
  if ((_a = method.payload) === null || _a === void 0 ? void 0 : _a.onlyConnectBleDevice) {
61616
61749
  if (shouldAcquire) {