@onekeyfe/hd-core 1.2.0-alpha.30 → 1.2.0-alpha.32

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 (123) hide show
  1. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +251 -0
  2. package/__tests__/connectSettings.test.ts +5 -47
  3. package/__tests__/device-state-mapper.test.ts +10 -1
  4. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +31 -59
  5. package/__tests__/networkUtils.test.ts +20 -0
  6. package/__tests__/open-wallet-session.test.ts +19 -5
  7. package/__tests__/protocol-v2-firmware-targets.test.ts +18 -0
  8. package/__tests__/protocol-v2.test.ts +58 -240
  9. package/__tests__/public-device-state-api.test.ts +10 -1
  10. package/dist/api/CheckAllFirmwareRelease.d.ts +24 -1
  11. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  12. package/dist/api/FirmwareUpdateV2.d.ts +0 -11
  13. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  14. package/dist/api/FirmwareUpdateV3.d.ts +3 -7
  15. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  16. package/dist/api/FirmwareUpdateV4.d.ts +6 -22
  17. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  18. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  19. package/dist/api/PromptWebDeviceAccess.d.ts.map +1 -1
  20. package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
  21. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  22. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +0 -7
  23. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  24. package/dist/api/firmware/getBinary.d.ts +0 -7
  25. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  26. package/dist/api/firmware/updateBootloader.d.ts +0 -2
  27. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  28. package/dist/api/firmware/uploadFirmware.d.ts +1 -9
  29. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  30. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  31. package/dist/data-manager/DataManager.d.ts +0 -1
  32. package/dist/data-manager/DataManager.d.ts.map +1 -1
  33. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  34. package/dist/device/DeviceStateMapper.d.ts.map +1 -1
  35. package/dist/index.d.ts +43 -179
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +1048 -2606
  38. package/dist/inject.d.ts.map +1 -1
  39. package/dist/lowLevelInject.d.ts.map +1 -1
  40. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  41. package/dist/topLevelInject.d.ts.map +1 -1
  42. package/dist/types/api/checkAllFirmwareRelease.d.ts +25 -7
  43. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  44. package/dist/types/api/deviceUpdateBootloader.d.ts +0 -6
  45. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  46. package/dist/types/api/export.d.ts +2 -4
  47. package/dist/types/api/export.d.ts.map +1 -1
  48. package/dist/types/api/firmwareUpdate.d.ts +0 -69
  49. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  50. package/dist/types/api/index.d.ts +3 -10
  51. package/dist/types/api/index.d.ts.map +1 -1
  52. package/dist/types/api/openWalletSession.d.ts +9 -3
  53. package/dist/types/api/openWalletSession.d.ts.map +1 -1
  54. package/dist/types/device.d.ts +6 -0
  55. package/dist/types/device.d.ts.map +1 -1
  56. package/dist/types/settings.d.ts +1 -13
  57. package/dist/types/settings.d.ts.map +1 -1
  58. package/dist/utils/networkUtils.d.ts +1 -0
  59. package/dist/utils/networkUtils.d.ts.map +1 -1
  60. package/package.json +4 -4
  61. package/src/api/CheckAllFirmwareRelease.ts +368 -50
  62. package/src/api/FirmwareUpdateV2.ts +120 -298
  63. package/src/api/FirmwareUpdateV3.ts +56 -258
  64. package/src/api/FirmwareUpdateV4.ts +328 -765
  65. package/src/api/OpenWalletSession.ts +12 -8
  66. package/src/api/PromptWebDeviceAccess.ts +2 -6
  67. package/src/api/device/DeviceUpdateBootloader.ts +6 -122
  68. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +0 -50
  69. package/src/api/firmware/updateBootloader.ts +4 -19
  70. package/src/api/firmware/uploadFirmware.ts +22 -140
  71. package/src/data/messages/messages-protocol-v2.json +4 -0
  72. package/src/data-manager/DataManager.ts +33 -33
  73. package/src/data-manager/connectSettings.ts +0 -11
  74. package/src/device/DeviceStateMapper.ts +6 -0
  75. package/src/index.ts +0 -5
  76. package/src/inject.ts +2 -22
  77. package/src/lowLevelInject.ts +1 -5
  78. package/src/protocols/protocol-v2/walletSession.ts +2 -1
  79. package/src/topLevelInject.ts +1 -5
  80. package/src/types/api/checkAllFirmwareRelease.ts +46 -7
  81. package/src/types/api/deviceUpdateBootloader.ts +0 -6
  82. package/src/types/api/export.ts +5 -18
  83. package/src/types/api/firmwareUpdate.ts +0 -76
  84. package/src/types/api/index.ts +6 -15
  85. package/src/types/api/openWalletSession.ts +12 -3
  86. package/src/types/device.ts +6 -0
  87. package/src/types/settings.ts +3 -13
  88. package/src/utils/networkUtils.ts +5 -3
  89. package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -174
  90. package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
  91. package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
  92. package/__tests__/firmware-update/firmware-preparation-error.test.ts +0 -27
  93. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
  94. package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -593
  95. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
  96. package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
  97. package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
  98. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
  99. package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
  100. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
  101. package/dist/api/firmware/FirmwarePreparationError.d.ts +0 -3
  102. package/dist/api/firmware/FirmwarePreparationError.d.ts.map +0 -1
  103. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
  104. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
  105. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -25
  106. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
  107. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -30
  108. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
  109. package/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
  110. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
  111. package/dist/types/api/firmwareUpdatePlan.d.ts +0 -28
  112. package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
  113. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
  114. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
  115. package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
  116. package/src/api/firmware/FirmwareHostBinding.ts +0 -100
  117. package/src/api/firmware/FirmwarePreparationError.ts +0 -12
  118. package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
  119. package/src/api/firmware/FirmwareUpdatePlan.ts +0 -745
  120. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -392
  121. package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
  122. package/src/types/api/firmwareUpdatePlan.ts +0 -38
  123. package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
@@ -14,44 +14,18 @@ import { BaseMethod } from './BaseMethod';
14
14
  import { validateParams } from './helpers/paramsValidator';
15
15
  import { DevicePool } from '../device/DevicePool';
16
16
  import { getBinary, getInfo, getSysResourceBinary } from './firmware/getBinary';
17
- import { normalizeFirmwarePreparationError } from './firmware/FirmwarePreparationError';
18
- import {
19
- updateResources,
20
- updateResourcesFromSources,
21
- uploadFirmwareFromSource,
22
- } from './firmware/uploadFirmware';
23
- import { LoggerNames, getDeviceUUID, getLogger, wait } from '../utils';
17
+ import { updateResources, uploadFirmware } from './firmware/uploadFirmware';
18
+ import { LoggerNames, getLogger, wait } from '../utils';
24
19
  import { FirmwareUpdateTipMessage, createUiMessage } from '../events/ui-request';
25
20
  import { DeviceModelToTypes } from '../types';
26
21
  import { DataManager } from '../data-manager';
27
22
  import { DEVICE } from '../events';
28
- import { type FirmwareByteSource, openFirmwareByteSource } from './firmware/FirmwareArtifactSource';
29
- import { resolveFirmwareUpdateHostBinding } from './firmware/FirmwareHostBinding';
30
- import {
31
- assertFirmwareUpdatePreparedPlanBinding,
32
- assertFirmwareUpdatePreparedPlanDeviceIdentity,
33
- getFirmwareUpdateResourceName,
34
- } from './firmware/FirmwareUpdatePreparedPlan';
35
23
 
36
24
  import type { Features, KnownDevice } from '../types';
37
25
  import type { FirmwareBinary } from './firmware/getBinary';
38
- import type {
39
- FirmwareArtifactReader,
40
- FirmwareArtifactReference,
41
- } from '../types/api/firmwareUpdate';
42
- import type { FirmwareUpdatePreparedPlan } from '../types/api/firmwareUpdatePreparedPlan';
43
26
 
44
27
  type Params = {
45
28
  binary?: ArrayBuffer;
46
- artifact?: FirmwareArtifactReference;
47
- resourceArtifact?: FirmwareArtifactReference;
48
- resourceEntries?: Array<{
49
- entryName: string;
50
- artifact: FirmwareArtifactReference;
51
- }>;
52
- artifactReader?: FirmwareArtifactReader;
53
- preparedPlan?: FirmwareUpdatePreparedPlan;
54
- platform?: FirmwareUpdatePreparedPlan['platform'];
55
29
  version?: number[];
56
30
  updateType: 'firmware' | 'ble';
57
31
  forcedUpdateRes?: boolean;
@@ -132,13 +106,6 @@ const normalizeFirmwareBinary = (binary: unknown): FirmwareBinary | undefined =>
132
106
  return normalized.buffer;
133
107
  };
134
108
 
135
- const toArrayBuffer = (binary: FirmwareBinary): ArrayBuffer => {
136
- if (binary instanceof ArrayBuffer) {
137
- return binary;
138
- }
139
- return new Uint8Array(binary.buffer, binary.byteOffset, binary.byteLength).slice().buffer;
140
- };
141
-
142
109
  export default class FirmwareUpdateV2 extends BaseMethod<Params> {
143
110
  checkPromise: Deferred<any> | null = null;
144
111
 
@@ -185,38 +152,6 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
185
152
  binary: payload.binary,
186
153
  };
187
154
  }
188
-
189
- if ('artifact' in payload) {
190
- const hostBinding = resolveFirmwareUpdateHostBinding(payload.hostBindingGeneration);
191
- const target = payload.isUpdateBootloader ? 'bootloader' : payload.updateType;
192
- const resourceBindings = [
193
- ...(payload.resourceArtifact
194
- ? [{ target: 'resource' as const, artifact: payload.resourceArtifact }]
195
- : []),
196
- ...(payload.resourceEntries ?? []).map(
197
- (entry: { entryName: string; artifact: FirmwareArtifactReference }) => ({
198
- target: 'resource' as const,
199
- entryName: entry.entryName,
200
- artifact: entry.artifact,
201
- })
202
- ),
203
- ];
204
- assertFirmwareUpdatePreparedPlanBinding({
205
- preparedPlan: payload.preparedPlan,
206
- executor: 'v2',
207
- platform: payload.platform,
208
- scopeTargets: [target, ...(target === 'firmware' ? (['resource'] as const) : [])],
209
- bindings: [{ target, artifact: payload.artifact }, ...resourceBindings],
210
- });
211
- this.params = {
212
- ...this.params,
213
- preparedPlan: payload.preparedPlan,
214
- artifact: payload.artifact,
215
- resourceArtifact: payload.resourceArtifact,
216
- resourceEntries: payload.resourceEntries,
217
- artifactReader: hostBinding.artifactReader,
218
- };
219
- }
220
155
  }
221
156
 
222
157
  postTipMessage = (message: string) => {
@@ -406,264 +341,151 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
406
341
  const { features, commands } = device;
407
342
  const deviceType = device.getCurrentDeviceType();
408
343
 
409
- // Protocol V2 (Pro2) uses DeviceFirmwareUpdate and must not enter this legacy flow.
410
- if (device.isProtocolV2()) {
411
- throw ERRORS.TypedError(
412
- HardwareErrorCode.RuntimeError,
413
- 'Protocol V2 firmware update must use firmwareUpdateV4'
414
- );
415
- }
416
- if (params.preparedPlan) {
417
- assertFirmwareUpdatePreparedPlanDeviceIdentity({
418
- preparedPlan: params.preparedPlan,
419
- deviceIdentity: getDeviceUUID(features) || undefined,
420
- });
421
- }
422
-
423
344
  const deviceFirmwareType = device.getCurrentFirmwareType();
424
345
  const firmwareType = params.firmwareType ?? deviceFirmwareType;
425
346
 
426
347
  this.checkVersionForCopyTouchResource(features, firmwareType);
427
348
 
428
- let preparedSource: FirmwareByteSource | undefined;
429
- try {
430
- const acquireFirmwareSource = async (): Promise<FirmwareByteSource> => {
431
- try {
432
- if (preparedSource) {
433
- return preparedSource;
434
- }
349
+ let preparedBinary: FirmwareBinary | undefined;
350
+ const acquireFirmwareBinary = async (): Promise<FirmwareBinary> => {
351
+ try {
352
+ if (preparedBinary) {
353
+ return preparedBinary;
354
+ }
435
355
 
436
- if (params.binary !== undefined) {
437
- const binary = normalizeFirmwareBinary(params.binary);
438
- if (!binary) {
439
- throw new Error('firmware binary is empty or invalid');
440
- }
441
- const source = await openFirmwareByteSource({
442
- binary: toArrayBuffer(binary),
443
- });
444
- if (!source) {
445
- throw new Error('firmware binary is empty or invalid');
446
- }
447
- preparedSource = source;
448
- return source;
356
+ if (params.binary !== undefined) {
357
+ preparedBinary = normalizeFirmwareBinary(params.binary);
358
+ if (!preparedBinary) {
359
+ throw new Error('firmware binary is empty or invalid');
449
360
  }
361
+ return preparedBinary;
362
+ }
450
363
 
451
- if (params.artifact) {
452
- const source = await openFirmwareByteSource({
453
- artifact: params.artifact,
454
- reader: params.artifactReader,
455
- });
456
- if (!source) {
457
- throw new Error('firmware artifact is not prepared');
458
- }
459
- preparedSource = source;
460
- return source;
461
- }
364
+ if (!device.features) {
365
+ throw ERRORS.TypedError(
366
+ HardwareErrorCode.RuntimeError,
367
+ 'no features found for this device'
368
+ );
369
+ }
462
370
 
463
- if (!device.features) {
464
- throw ERRORS.TypedError(
465
- HardwareErrorCode.RuntimeError,
466
- 'no features found for this device'
467
- );
468
- }
371
+ this.postTipMessage('DownloadFirmware');
372
+ const firmware = await getBinary({
373
+ features: device.features,
374
+ version: params.version,
375
+ updateType: params.updateType,
376
+ isUpdateBootloader: params.isUpdateBootloader,
377
+ firmwareType,
378
+ requestOptions: FIRMWARE_DOWNLOAD_REQUEST_OPTIONS,
379
+ });
380
+ preparedBinary = normalizeFirmwareBinary(firmware.binary);
381
+ if (!preparedBinary) {
382
+ throw new Error('downloaded firmware binary is empty or invalid');
383
+ }
384
+ this.postTipMessage('DownloadFirmwareSuccess');
385
+ return preparedBinary;
386
+ } catch (err) {
387
+ throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateDownloadFailed, err.message ?? err);
388
+ }
389
+ };
469
390
 
470
- if (
471
- params.artifactReader ||
472
- DataManager.getSettings('firmwareManifestMode') === 'external-only'
473
- ) {
474
- throw ERRORS.TypedError(
475
- HardwareErrorCode.RuntimeError,
476
- 'Firmware must be prepared by the external firmware host',
477
- {
478
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
479
- artifactName: 'firmware',
480
- }
481
- );
482
- }
391
+ if (!device.isBootloader() && features) {
392
+ const serialNo = device.getCurrentSerialNo();
393
+ // should go to bootloader mode manually
394
+ if (this.isEnteredManuallyBoot()) {
395
+ return Promise.reject(ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateManuallyEnterBoot));
396
+ }
483
397
 
484
- this.postTipMessage('DownloadFirmware');
485
- const firmware = await getBinary({
486
- features: device.features,
487
- version: params.version,
488
- updateType: params.updateType,
489
- isUpdateBootloader: params.isUpdateBootloader,
490
- firmwareType,
491
- requestOptions: FIRMWARE_DOWNLOAD_REQUEST_OPTIONS,
492
- });
493
- const binary = normalizeFirmwareBinary(firmware.binary);
494
- if (!binary) {
495
- throw new Error('downloaded firmware binary is empty or invalid');
496
- }
497
- const source = await openFirmwareByteSource({
498
- binary: toArrayBuffer(binary),
499
- });
500
- if (!source) {
501
- throw new Error('downloaded firmware binary is empty or invalid');
398
+ // check & upgrade firmware resource
399
+ if (this.isSupportResourceUpdate(params.updateType)) {
400
+ this.postTipMessage('CheckLatestUiResource');
401
+ const resourceUrl = DataManager.getSysResourcesLatestRelease({
402
+ features,
403
+ forcedUpdateRes: params.forcedUpdateRes,
404
+ firmwareType,
405
+ });
406
+ if (resourceUrl) {
407
+ this.postTipMessage('DownloadLatestUiResource');
408
+ const resource = await getSysResourceBinary(resourceUrl);
409
+ this.postTipMessage('DownloadLatestUiResourceSuccess');
410
+ if (resource) {
411
+ await updateResources(
412
+ this.device.getCommands().typedCall.bind(this.device.getCommands()),
413
+ this.postMessage,
414
+ device,
415
+ resource.binary
416
+ );
502
417
  }
503
- preparedSource = source;
504
- this.postTipMessage('DownloadFirmwareSuccess');
505
- return source;
506
- } catch (err) {
507
- throw normalizeFirmwarePreparationError(err);
508
418
  }
509
- };
419
+ }
510
420
 
511
- if (!device.isBootloader() && features) {
512
- const serialNo = device.getCurrentSerialNo();
513
- // should go to bootloader mode manually
514
- if (this.isEnteredManuallyBoot()) {
515
- throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateManuallyEnterBoot);
421
+ // check if the device commands has been disposed
422
+ this.device?.commands?.checkDisposed();
423
+
424
+ // A failed firmware download must leave the device in normal mode.
425
+ await acquireFirmwareBinary();
426
+
427
+ // The request may outlive the current transport command instance.
428
+ this.device?.commands?.checkDisposed();
429
+
430
+ // auto go to bootloader mode
431
+ try {
432
+ this.postTipMessage('AutoRebootToBootloader');
433
+ const bootRes = await commands.typedCall('DeviceBackToBoot', 'Success');
434
+ // @ts-expect-error
435
+ if (bootRes.type === 'CallMethodError') {
436
+ throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure);
516
437
  }
438
+ this.postTipMessage('GoToBootloaderSuccess');
439
+ this.checkDeviceToBootloader(this.payload.connectId);
517
440
 
518
- // All network-backed input must be ready before the first device mutation.
519
- await acquireFirmwareSource();
520
-
521
- // check & upgrade firmware resource
522
- if (this.isSupportResourceUpdate(params.updateType)) {
523
- this.postTipMessage('CheckLatestUiResource');
524
- const resourceUrl = DataManager.getSysResourcesLatestRelease({
525
- features,
526
- forcedUpdateRes: params.forcedUpdateRes,
527
- firmwareType,
528
- });
529
- if (resourceUrl) {
530
- this.postTipMessage('DownloadLatestUiResource');
531
- if (params.resourceEntries?.length) {
532
- const sources: Array<{
533
- entryName: string;
534
- source: FirmwareByteSource;
535
- }> = [];
536
- try {
537
- for (const entry of params.resourceEntries) {
538
- const resourceName = getFirmwareUpdateResourceName(entry.entryName);
539
- const source = await openFirmwareByteSource({
540
- artifact: entry.artifact,
541
- reader: params.artifactReader,
542
- });
543
- if (!source) {
544
- throw new Error('Firmware resource entry is not prepared');
545
- }
546
- sources.push({ entryName: resourceName, source });
547
- }
548
- await updateResourcesFromSources(
549
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
550
- this.postMessage,
551
- device,
552
- sources.map(entry => ({
553
- entryName: entry.entryName,
554
- source: entry.source,
555
- }))
556
- );
557
- } finally {
558
- await Promise.all(
559
- sources.map(entry => entry.source.close().catch(() => undefined))
560
- );
561
- }
562
- this.postTipMessage('DownloadLatestUiResourceSuccess');
563
- } else {
564
- let resourceBinary: ArrayBuffer | Buffer;
565
- if (params.resourceArtifact) {
566
- throw ERRORS.TypedError(
567
- HardwareErrorCode.RuntimeError,
568
- 'Firmware resource archive must be materialized into prepared entries',
569
- {
570
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
571
- artifactName: 'resource',
572
- }
573
- );
574
- } else {
575
- if (
576
- params.artifactReader ||
577
- DataManager.getSettings('firmwareManifestMode') === 'external-only'
578
- ) {
579
- throw ERRORS.TypedError(
580
- HardwareErrorCode.RuntimeError,
581
- 'Firmware resource must be prepared by the external firmware host',
582
- {
583
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
584
- artifactName: 'resource',
585
- }
586
- );
587
- }
588
- resourceBinary = (await getSysResourceBinary(resourceUrl)).binary;
589
- }
590
- this.postTipMessage('DownloadLatestUiResourceSuccess');
591
- await updateResources(
592
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
593
- this.postMessage,
594
- device,
595
- resourceBinary
596
- );
597
- }
598
- }
441
+ // force clean classic device cache so that the device can initialize again
442
+ if (DeviceModelToTypes.model_classic.includes(deviceType)) {
443
+ DevicePool.clearDeviceCache(serialNo);
599
444
  }
445
+ delete DevicePool.devicesCache[''];
446
+ await this.checkPromise?.promise;
447
+ this.checkPromise = null;
600
448
 
601
- // The request may outlive the current transport command instance.
449
+ // check if the device commands has been disposed
602
450
  this.device?.commands?.checkDisposed();
603
451
 
604
- // auto go to bootloader mode
605
- try {
606
- this.postTipMessage('AutoRebootToBootloader');
607
- const bootRes = await commands.typedCall('DeviceBackToBoot', 'Success');
608
- // @ts-expect-error
609
- if (bootRes.type === 'CallMethodError') {
610
- throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure);
611
- }
612
- this.postTipMessage('GoToBootloaderSuccess');
613
- this.checkDeviceToBootloader(this.payload.connectId);
614
-
615
- // force clean classic device cache so that the device can initialize again
616
- if (DeviceModelToTypes.model_classic.includes(deviceType)) {
617
- DevicePool.clearDeviceCache(serialNo);
618
- }
619
- delete DevicePool.devicesCache[''];
620
- await this.checkPromise?.promise;
621
- this.checkPromise = null;
622
-
623
- // check if the device commands has been disposed
624
- this.device?.commands?.checkDisposed();
625
-
626
- /**
627
- * Touch 1 with bootloader v2.5.0 issue: BLE chip need more time for looking up name, here change the delay time to 3000ms after rebooting.
628
- */
629
- const isTouch = DeviceModelToTypes.model_touch.includes(deviceType);
630
- await wait(isTouch ? 3000 : 1500);
631
- } catch (e) {
632
- if (e instanceof HardwareError) {
633
- return Promise.reject(e);
634
- }
635
- console.log('auto go to bootloader mode failed: ', e);
636
- return Promise.reject(
637
- ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure)
638
- );
452
+ /**
453
+ * Touch 1 with bootloader v2.5.0 issue: BLE chip need more time for looking up name, here change the delay time to 3000ms after rebooting.
454
+ */
455
+ const isTouch = DeviceModelToTypes.model_touch.includes(deviceType);
456
+ await wait(isTouch ? 3000 : 1500);
457
+ } catch (e) {
458
+ if (e instanceof HardwareError) {
459
+ return Promise.reject(e);
639
460
  }
461
+ console.log('auto go to bootloader mode failed: ', e);
462
+ return Promise.reject(
463
+ ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure)
464
+ );
640
465
  }
466
+ }
641
467
 
642
- // Devices already in bootloader mode still acquire through the same helper.
643
- const source = await acquireFirmwareSource();
644
-
645
- // check if the device commands has been disposed
646
- this.device?.commands?.checkDisposed();
468
+ // Devices already in bootloader mode still acquire through the same helper.
469
+ const binary = await acquireFirmwareBinary();
647
470
 
648
- await this.device.acquire();
471
+ // check if the device commands has been disposed
472
+ this.device?.commands?.checkDisposed();
649
473
 
650
- const response = await uploadFirmwareFromSource(
651
- params.updateType,
652
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
653
- this.postMessage,
654
- device,
655
- source,
656
- true,
657
- params.isUpdateBootloader
658
- );
474
+ await this.device.acquire();
659
475
 
660
- if (this.connectId) {
661
- DevicePool.clearDeviceCache(this.connectId);
662
- }
476
+ const response = await uploadFirmware(
477
+ params.updateType,
478
+ this.device.getCommands().typedCall.bind(this.device.getCommands()),
479
+ this.postMessage,
480
+ device,
481
+ { payload: binary, rebootOnSuccess: true },
482
+ params.isUpdateBootloader
483
+ );
663
484
 
664
- return response;
665
- } finally {
666
- await preparedSource?.close().catch(() => undefined);
485
+ if (this.connectId) {
486
+ DevicePool.clearDeviceCache(this.connectId);
667
487
  }
488
+
489
+ return response;
668
490
  }
669
491
  }