@onekeyfe/hd-core 1.2.0-alpha.22 → 1.2.0-alpha.24

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