From f7faa1f098acedf718526458f7aec4f855161849 Mon Sep 17 00:00:00 2001 From: maniglia Date: Tue, 7 Jan 2025 11:40:59 +0100 Subject: [PATCH] Async backup. --- .../CubeProgrammerApi.cs | 161 ++++++++++++++++++ .../Interface/ICubeProgrammerApiAsync.cs | 62 +++---- 2 files changed, 192 insertions(+), 31 deletions(-) diff --git a/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs index b867425..fbd600c 100644 --- a/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs @@ -7,6 +7,7 @@ namespace SharpCubeProgrammer using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; + using System.IO.Compression; using System.Net; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -1395,6 +1396,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command) return output; } + public async ValueTask SendOptionBytesCmdAsync(string command, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.SendOptionBytesCmd(command), cancellationToken).ConfigureAwait(false); + } + /// public DevicePeripheralC? InitOptionBytesInterface() { @@ -1408,6 +1414,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command) return null; } + public async ValueTask InitOptionBytesInterfaceAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.InitOptionBytesInterface(), cancellationToken).ConfigureAwait(false); + } + /// public DevicePeripheralC? FastRomInitOptionBytesInterface(ushort deviceId) { @@ -1421,6 +1432,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command) return null; } + public async ValueTask FastRomInitOptionBytesInterfaceAsync(ushort deviceId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.FastRomInitOptionBytesInterface(deviceId), cancellationToken).ConfigureAwait(false); + } + private DevicePeripheralC? DevicePeripheralCHandler(IntPtr pointer) { var pointerSize = Marshal.SizeOf(); @@ -1539,6 +1555,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command) return null; } + //private async ValueTask DevicePeripheralCHandlerAsync(IntPtr pointer, CancellationToken cancellationToken = default) + //{ + // return await Task.Run(() => this.DevicePeripheralCHandler(pointer), cancellationToken).ConfigureAwait(false); + //} + /// public CubeProgrammerError ObDisplay() { @@ -1560,6 +1581,11 @@ public CubeProgrammerError ObDisplay() return output; } + public async ValueTask ObDisplayAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.ObDisplay(), cancellationToken).ConfigureAwait(false); + } + #endregion #region [Loaders] @@ -1576,6 +1602,11 @@ public void SetLoadersPath(string path) } } + public async ValueTask SetLoadersPathAsync(string path, CancellationToken cancellationToken = default) + { + await Task.Run(() => this.SetLoadersPath(path), cancellationToken).ConfigureAwait(false); + } + /// public DeviceExternalLoader? SetExternalLoaderPath(string path) { @@ -1625,6 +1656,11 @@ public void SetLoadersPath(string path) return null; } + public async ValueTask SetExternalLoaderPathAsync(string path, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.SetExternalLoaderPath(path), cancellationToken).ConfigureAwait(false); + } + /// public DeviceExternalLoader? SetExternalLoaderOBL(string path) { @@ -1674,6 +1710,11 @@ public void SetLoadersPath(string path) return null; } + public async ValueTask SetExternalLoaderOBLAsync(string path, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.SetExternalLoaderOBL(path), cancellationToken).ConfigureAwait(false); + } + /// public DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer") { @@ -1742,6 +1783,11 @@ public void SetLoadersPath(string path) return null; } + public async ValueTask GetExternalLoadersAsync(string path = @".\st\Programmer", CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetExternalLoaders(path), cancellationToken).ConfigureAwait(false); + } + /// public void RemoveExternalLoader(string path) { @@ -1752,6 +1798,11 @@ public void RemoveExternalLoader(string path) } } + public async ValueTask RemoveExternalLoaderAsync(string path, CancellationToken cancellationToken = default) + { + await Task.Run(() => this.RemoveExternalLoader(path), cancellationToken).ConfigureAwait(false); + } + /// public void DeleteLoaders() { @@ -1761,6 +1812,11 @@ public void DeleteLoaders() } } + public async ValueTask DeleteLoadersAsync(CancellationToken cancellationToken = default) + { + await Task.Run(() => this.DeleteLoaders(), cancellationToken).ConfigureAwait(false); + } + #endregion #region [STM32WB specific] @@ -1790,6 +1846,11 @@ public void DeleteLoaders() return (result, buffer); } + public async ValueTask<(CubeProgrammerError, byte[])> GetUID64Async(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetUID64(), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError FirmwareDelete() { @@ -1804,6 +1865,11 @@ public CubeProgrammerError FirmwareDelete() return result; } + public async ValueTask FirmwareDeleteAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.FirmwareDelete(), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint firstInstall, uint startStack, uint verify) { @@ -1821,6 +1887,11 @@ public CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint return result; } + public async ValueTask FirmwareUpgradeAsync(string filePath, string address, uint firstInstall, uint startStack, uint verify, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.FirmwareUpgrade(filePath, address, firstInstall, startStack, verify), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError StartWirelessStack() { @@ -1835,6 +1906,11 @@ public CubeProgrammerError StartWirelessStack() return result; } + public async ValueTask StartWirelessStackAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.StartWirelessStack(), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError UpdateAuthKey(string filePath) { @@ -1849,6 +1925,11 @@ public CubeProgrammerError UpdateAuthKey(string filePath) return result; } + public async ValueTask UpdateAuthKeyAsync(string filePath, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.UpdateAuthKey(filePath), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError AuthKeyLock() { @@ -1863,6 +1944,11 @@ public CubeProgrammerError AuthKeyLock() return result; } + public async ValueTask AuthKeyLockAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.AuthKeyLock(), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError WriteUserKey(string filePath, byte keyType) { @@ -1879,6 +1965,11 @@ public CubeProgrammerError WriteUserKey(string filePath, byte keyType) return result; } + public async ValueTask WriteUserKeyAsync(string filePath, byte keyType, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.WriteUserKey(filePath, keyType), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError AntiRollBack() { @@ -1893,6 +1984,11 @@ public CubeProgrammerError AntiRollBack() return result; } + public async ValueTask AntiRollBackAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.AntiRollBack(), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError StartFus() { @@ -1907,6 +2003,11 @@ public CubeProgrammerError StartFus() return result; } + public async ValueTask StartFusAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.StartFus(), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError UnlockChip() { @@ -1921,6 +2022,11 @@ public CubeProgrammerError UnlockChip() return result; } + public async ValueTask UnlockChipAsync(CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.UnlockChip(), cancellationToken).ConfigureAwait(false); + } + #endregion #region [STM32MP specific functions] @@ -1942,6 +2048,11 @@ public CubeProgrammerError ProgramSsp(string sspFile, string licenseFile, string return result; } + public async ValueTask ProgramSspAsync(string sspFile, string licenseFile, string tfaFile, int hsmSlotId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.ProgramSsp(sspFile, licenseFile, tfaFile, hsmSlotId), cancellationToken).ConfigureAwait(false); + } + #endregion #region [STM32 HSM specific functions] @@ -1957,6 +2068,11 @@ public string GetHsmFirmwareID(int hsmSlotId) return String.Empty; } + public async ValueTask GetHsmFirmwareIDAsync(int hsmSlotId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetHsmFirmwareID(hsmSlotId), cancellationToken).ConfigureAwait(false); + } + /// public ulong GetHsmCounter(int hsmSlotId) { @@ -1967,6 +2083,11 @@ public ulong GetHsmCounter(int hsmSlotId) return 0UL; } + public async ValueTask GetHsmCounterAsync(int hsmSlotId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetHsmCounter(hsmSlotId), cancellationToken).ConfigureAwait(false); + } + /// public string GetHsmState(int hsmSlotId) { @@ -1977,6 +2098,11 @@ public string GetHsmState(int hsmSlotId) return String.Empty; } + public async ValueTask GetHsmStateAsync(int hsmSlotId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetHsmState(hsmSlotId), cancellationToken).ConfigureAwait(false); + } + /// public string GetHsmVersion(int hsmSlotId) { @@ -1987,6 +2113,11 @@ public string GetHsmVersion(int hsmSlotId) return String.Empty; } + public async ValueTask GetHsmVersionAsync(int hsmSlotId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetHsmVersion(hsmSlotId), cancellationToken).ConfigureAwait(false); + } + /// public string GetHsmType(int hsmSlotId) { @@ -1997,6 +2128,11 @@ public string GetHsmType(int hsmSlotId) return String.Empty; } + public async ValueTask GetHsmTypeAsync(int hsmSlotId, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetHsmType(hsmSlotId), cancellationToken).ConfigureAwait(false); + } + /// public CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath) { @@ -2012,6 +2148,11 @@ public CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath) return result; } + public async ValueTask GetHsmLicenseAsync(int hsmSlotId, string outLicensePath, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.GetHsmLicense(hsmSlotId, outLicensePath), cancellationToken).ConfigureAwait(false); + } + #endregion #region [Util] @@ -2025,6 +2166,11 @@ public uint HexConverterToUint(string hex) return parseResult ? result : 0; } + public async ValueTask HexConverterToUintAsync(string hex, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.HexConverterToUint(hex), cancellationToken).ConfigureAwait(false); + } + /// public int HexConverterToInt(string hex) { @@ -2034,6 +2180,11 @@ public int HexConverterToInt(string hex) return parseResult ? result : 0; } + public async ValueTask HexConverterToIntAsync(string hex, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.HexConverterToInt(hex), cancellationToken).ConfigureAwait(false); + } + /// public string HexConverterToString(uint hex) { @@ -2041,6 +2192,11 @@ public string HexConverterToString(uint hex) return output; } + public async ValueTask HexConverterToStringAsync(uint hex, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.HexConverterToString(hex), cancellationToken).ConfigureAwait(false); + } + /// public string HexConverterToString(int hex) { @@ -2048,6 +2204,11 @@ public string HexConverterToString(int hex) return output; } + public async ValueTask HexConverterToStringAsync(int hex, CancellationToken cancellationToken = default) + { + return await Task.Run(() => this.HexConverterToString(hex), cancellationToken).ConfigureAwait(false); + } + private static string StringFilter(string hex) { if (String.IsNullOrEmpty(hex)) diff --git a/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApiAsync.cs b/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApiAsync.cs index ae769b2..72e8d17 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApiAsync.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApiAsync.cs @@ -309,22 +309,22 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// This routine allows program the given Option Byte. /// The option bytes are configured by the end user depending on the application requirements. /// - CubeProgrammerError SendOptionBytesCmd(string command); + ValueTask SendOptionBytesCmdAsync(string command, CancellationToken cancellationToken = default); /// /// This routine allows to get option bytes values of the connected target. /// - DevicePeripheralC? InitOptionBytesInterface(); + ValueTask InitOptionBytesInterfaceAsync(CancellationToken cancellationToken = default); /// /// This routine allows to get option bytes values of the connected target. /// - DevicePeripheralC? FastRomInitOptionBytesInterface(ushort deviceId); + ValueTask FastRomInitOptionBytesInterfaceAsync(ushort deviceId, CancellationToken cancellationToken = default); /// /// This routine allows to display the Option bytes. /// - CubeProgrammerError ObDisplay(); + ValueTask ObDisplayAsync(CancellationToken cancellationToken = default); #endregion @@ -336,34 +336,34 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// This routine allows to specify the location of Flash Loader. /// /// Indicates the full path of the considered folder. - void SetLoadersPath(string path); + ValueTask SetLoadersPathAsync(string path, CancellationToken cancellationToken = default); /// /// This routine allows to specify the path of the external Loaders to be loaded. /// /// - DeviceExternalLoader? SetExternalLoaderPath(string path); + ValueTask SetExternalLoaderPathAsync(string path, CancellationToken cancellationToken = default); /// /// This routine allows to specify the path of the external Loaders to be loaded via OBL interfaces. /// /// Indicates the full path of the folder containing external Loaders. - DeviceExternalLoader? SetExternalLoaderOBL(string path); + ValueTask SetExternalLoaderOBLAsync(string path, CancellationToken cancellationToken = default); /// /// This routine allows to get available external Loaders in th mentioned path. /// - DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer"); + ValueTask GetExternalLoadersAsync(string path = @".\st\Programmer", CancellationToken cancellationToken = default); /// /// This routine allows to unload an external Loaders. /// - void RemoveExternalLoader(string path); + ValueTask RemoveExternalLoaderAsync(string path, CancellationToken cancellationToken = default); /// /// This routine allows to delete all target Flash Loaders. /// - void DeleteLoaders(); + ValueTask DeleteLoadersAsync(CancellationToken cancellationToken = default); #endregion @@ -376,53 +376,53 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// /// This routine allows to read the device unique identifier. /// - (CubeProgrammerError, byte[]) GetUID64(); + ValueTask<(CubeProgrammerError, byte[])> GetUID64Async(CancellationToken cancellationToken = default); /// /// This routine allows to erase the BLE stack firmware. /// - CubeProgrammerError FirmwareDelete(); + ValueTask FirmwareDeleteAsync(CancellationToken cancellationToken = default); /// /// This routine allows to make upgrade of BLE stack firmware or FUS firmware. /// - CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint firstInstall, uint startStack, uint verify); + ValueTask FirmwareUpgradeAsync(string filePath, string address, uint firstInstall, uint startStack, uint verify, CancellationToken cancellationToken = default); /// /// This routine allows to start the programmed Stack. /// - CubeProgrammerError StartWirelessStack(); + ValueTask StartWirelessStackAsync(CancellationToken cancellationToken = default); /// /// This routine allows to start the programmed Stack. /// - CubeProgrammerError UpdateAuthKey(string filePath); + ValueTask UpdateAuthKeyAsync(string filePath, CancellationToken cancellationToken = default); /// /// This routine allows to lock the authentication key and once locked, it is no longer possible to change it. /// - CubeProgrammerError AuthKeyLock(); + ValueTask AuthKeyLockAsync(CancellationToken cancellationToken = default); /// /// This routine allows to write a customized user key. /// - CubeProgrammerError WriteUserKey(string filePath, byte keyType); + ValueTask WriteUserKeyAsync(string filePath, byte keyType, CancellationToken cancellationToken = default); /// /// This routine allows to activate the AntiRollBack. /// - CubeProgrammerError AntiRollBack(); + ValueTask AntiRollBackAsync(CancellationToken cancellationToken = default); /// /// This routine allows to start and establish a communication with the FUS operator. /// - CubeProgrammerError StartFus(); + ValueTask StartFusAsync(CancellationToken cancellationToken = default); /// /// This routine allows to set default option Bytes. /// /// - CubeProgrammerError UnlockChip(); + ValueTask UnlockChipAsync(CancellationToken cancellationToken = default); #endregion @@ -439,7 +439,7 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// Indicates the full path of the tfa-ssp file. /// Indicates the HSM slot ID. /// 0 if the SSP was finished successfully, otherwise an error occurred. - CubeProgrammerError ProgramSsp(string sspFile, string licenseFile, string tfaFile, int hsmSlotId); + ValueTask ProgramSspAsync(string sspFile, string licenseFile, string tfaFile, int hsmSlotId, CancellationToken cancellationToken = default); #endregion @@ -452,35 +452,35 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// /// The slot index of the plugged-in HSM /// string that contains the HSM Firmware Identifier. - string GetHsmFirmwareID(int hsmSlotId); + ValueTask GetHsmFirmwareIDAsync(int hsmSlotId, CancellationToken cancellationToken = default); /// /// This routine aims to get the current HSM counter. /// /// The slot index of the plugged-in HSM /// Counter value - ulong GetHsmCounter(int hsmSlotId); + ValueTask GetHsmCounterAsync(int hsmSlotId, CancellationToken cancellationToken = default); /// /// This routine aims to get the HSM State. /// /// The slot index of the plugged-in HSM /// string with possible values: ST_STATE , OEM_STATE, OPERATIONAL_STATE , UNKNOWN_STATE - string GetHsmState(int hsmSlotId); + ValueTask GetHsmStateAsync(int hsmSlotId, CancellationToken cancellationToken = default); /// /// This routine aims to get the HSM version. /// /// The slot index of the plugged-in HSM /// string with possible values: 1 , 2 - string GetHsmVersion(int hsmSlotId); + ValueTask GetHsmVersionAsync(int hsmSlotId, CancellationToken cancellationToken = default); /// /// This routine aims to get the HSM type. /// /// The slot index of the plugged-in HSM /// string with possible values: SFI. SMU. SSP... - string GetHsmType(int hsmSlotId); + ValueTask GetHsmTypeAsync(int hsmSlotId, CancellationToken cancellationToken = default); /// /// This routine aims to get and save the HSM license into a binary file. @@ -489,7 +489,7 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// The slot index of the plugged-in HSM /// Path of the output binary file. /// 0 if the operation was finished successfully, otherwise an error occurred. - CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath); + ValueTask GetHsmLicenseAsync(int hsmSlotId, string outLicensePath, CancellationToken cancellationToken = default); #endregion @@ -500,28 +500,28 @@ public interface ICubeProgrammerApiAsync : IAsyncDisposable /// /// /// - uint HexConverterToUint(string hex); + ValueTask HexConverterToUintAsync(string hex, CancellationToken cancellationToken = default); /// /// HexConverterToInt /// /// /// - int HexConverterToInt(string hex); + ValueTask HexConverterToIntAsync(string hex, CancellationToken cancellationToken = default); /// /// HexConverterToString /// /// /// - string HexConverterToString(uint hex); + ValueTask HexConverterToStringAsync(uint hex, CancellationToken cancellationToken = default); /// /// HexConverterToString /// /// /// - string HexConverterToString(int hex); + ValueTask HexConverterToStringAsync(int hex, CancellationToken cancellationToken = default); #endregion