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