Skip to content

Commit

Permalink
Async backup.
Browse files Browse the repository at this point in the history
  • Loading branch information
maniglia committed Jan 7, 2025
1 parent 93d7cca commit f7faa1f
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 31 deletions.
161 changes: 161 additions & 0 deletions src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1395,6 +1396,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
return output;
}

public async ValueTask<CubeProgrammerError> SendOptionBytesCmdAsync(string command, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.SendOptionBytesCmd(command), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public DevicePeripheralC? InitOptionBytesInterface()
{
Expand All @@ -1408,6 +1414,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
return null;
}

public async ValueTask<DevicePeripheralC?> InitOptionBytesInterfaceAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.InitOptionBytesInterface(), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public DevicePeripheralC? FastRomInitOptionBytesInterface(ushort deviceId)
{
Expand All @@ -1421,6 +1432,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
return null;
}

public async ValueTask<DevicePeripheralC?> 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<IntPtr>();
Expand Down Expand Up @@ -1539,6 +1555,11 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
return null;
}

//private async ValueTask<DevicePeripheralC?> DevicePeripheralCHandlerAsync(IntPtr pointer, CancellationToken cancellationToken = default)
//{
// return await Task.Run(() => this.DevicePeripheralCHandler(pointer), cancellationToken).ConfigureAwait(false);
//}

/// <inheritdoc />
public CubeProgrammerError ObDisplay()
{
Expand All @@ -1560,6 +1581,11 @@ public CubeProgrammerError ObDisplay()
return output;
}

public async ValueTask<CubeProgrammerError> ObDisplayAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.ObDisplay(), cancellationToken).ConfigureAwait(false);
}

#endregion

#region [Loaders]
Expand All @@ -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);
}

/// <inheritdoc />
public DeviceExternalLoader? SetExternalLoaderPath(string path)
{
Expand Down Expand Up @@ -1625,6 +1656,11 @@ public void SetLoadersPath(string path)
return null;
}

public async ValueTask<DeviceExternalLoader?> SetExternalLoaderPathAsync(string path, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.SetExternalLoaderPath(path), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public DeviceExternalLoader? SetExternalLoaderOBL(string path)
{
Expand Down Expand Up @@ -1674,6 +1710,11 @@ public void SetLoadersPath(string path)
return null;
}

public async ValueTask<DeviceExternalLoader?> SetExternalLoaderOBLAsync(string path, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.SetExternalLoaderOBL(path), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer")
{
Expand Down Expand Up @@ -1742,6 +1783,11 @@ public void SetLoadersPath(string path)
return null;
}

public async ValueTask<DeviceExternalStorageInfo?> GetExternalLoadersAsync(string path = @".\st\Programmer", CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetExternalLoaders(path), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public void RemoveExternalLoader(string path)
{
Expand All @@ -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);
}

/// <inheritdoc />
public void DeleteLoaders()
{
Expand All @@ -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]
Expand Down Expand Up @@ -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);
}

/// <inheritdoc />
public CubeProgrammerError FirmwareDelete()
{
Expand All @@ -1804,6 +1865,11 @@ public CubeProgrammerError FirmwareDelete()
return result;
}

public async ValueTask<CubeProgrammerError> FirmwareDeleteAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.FirmwareDelete(), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint firstInstall, uint startStack, uint verify)
{
Expand All @@ -1821,6 +1887,11 @@ public CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint
return result;
}

public async ValueTask<CubeProgrammerError> 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);
}

/// <inheritdoc />
public CubeProgrammerError StartWirelessStack()
{
Expand All @@ -1835,6 +1906,11 @@ public CubeProgrammerError StartWirelessStack()
return result;
}

public async ValueTask<CubeProgrammerError> StartWirelessStackAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.StartWirelessStack(), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError UpdateAuthKey(string filePath)
{
Expand All @@ -1849,6 +1925,11 @@ public CubeProgrammerError UpdateAuthKey(string filePath)
return result;
}

public async ValueTask<CubeProgrammerError> UpdateAuthKeyAsync(string filePath, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.UpdateAuthKey(filePath), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError AuthKeyLock()
{
Expand All @@ -1863,6 +1944,11 @@ public CubeProgrammerError AuthKeyLock()
return result;
}

public async ValueTask<CubeProgrammerError> AuthKeyLockAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.AuthKeyLock(), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError WriteUserKey(string filePath, byte keyType)
{
Expand All @@ -1879,6 +1965,11 @@ public CubeProgrammerError WriteUserKey(string filePath, byte keyType)
return result;
}

public async ValueTask<CubeProgrammerError> WriteUserKeyAsync(string filePath, byte keyType, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.WriteUserKey(filePath, keyType), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError AntiRollBack()
{
Expand All @@ -1893,6 +1984,11 @@ public CubeProgrammerError AntiRollBack()
return result;
}

public async ValueTask<CubeProgrammerError> AntiRollBackAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.AntiRollBack(), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError StartFus()
{
Expand All @@ -1907,6 +2003,11 @@ public CubeProgrammerError StartFus()
return result;
}

public async ValueTask<CubeProgrammerError> StartFusAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.StartFus(), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError UnlockChip()
{
Expand All @@ -1921,6 +2022,11 @@ public CubeProgrammerError UnlockChip()
return result;
}

public async ValueTask<CubeProgrammerError> UnlockChipAsync(CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.UnlockChip(), cancellationToken).ConfigureAwait(false);
}

#endregion

#region [STM32MP specific functions]
Expand All @@ -1942,6 +2048,11 @@ public CubeProgrammerError ProgramSsp(string sspFile, string licenseFile, string
return result;
}

public async ValueTask<CubeProgrammerError> 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]
Expand All @@ -1957,6 +2068,11 @@ public string GetHsmFirmwareID(int hsmSlotId)
return String.Empty;
}

public async ValueTask<string> GetHsmFirmwareIDAsync(int hsmSlotId, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetHsmFirmwareID(hsmSlotId), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public ulong GetHsmCounter(int hsmSlotId)
{
Expand All @@ -1967,6 +2083,11 @@ public ulong GetHsmCounter(int hsmSlotId)
return 0UL;
}

public async ValueTask<ulong> GetHsmCounterAsync(int hsmSlotId, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetHsmCounter(hsmSlotId), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public string GetHsmState(int hsmSlotId)
{
Expand All @@ -1977,6 +2098,11 @@ public string GetHsmState(int hsmSlotId)
return String.Empty;
}

public async ValueTask<string> GetHsmStateAsync(int hsmSlotId, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetHsmState(hsmSlotId), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public string GetHsmVersion(int hsmSlotId)
{
Expand All @@ -1987,6 +2113,11 @@ public string GetHsmVersion(int hsmSlotId)
return String.Empty;
}

public async ValueTask<string> GetHsmVersionAsync(int hsmSlotId, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetHsmVersion(hsmSlotId), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public string GetHsmType(int hsmSlotId)
{
Expand All @@ -1997,6 +2128,11 @@ public string GetHsmType(int hsmSlotId)
return String.Empty;
}

public async ValueTask<string> GetHsmTypeAsync(int hsmSlotId, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetHsmType(hsmSlotId), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath)
{
Expand All @@ -2012,6 +2148,11 @@ public CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath)
return result;
}

public async ValueTask<CubeProgrammerError> GetHsmLicenseAsync(int hsmSlotId, string outLicensePath, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.GetHsmLicense(hsmSlotId, outLicensePath), cancellationToken).ConfigureAwait(false);
}

#endregion

#region [Util]
Expand All @@ -2025,6 +2166,11 @@ public uint HexConverterToUint(string hex)
return parseResult ? result : 0;
}

public async ValueTask<uint> HexConverterToUintAsync(string hex, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.HexConverterToUint(hex), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public int HexConverterToInt(string hex)
{
Expand All @@ -2034,20 +2180,35 @@ public int HexConverterToInt(string hex)
return parseResult ? result : 0;
}

public async ValueTask<int> HexConverterToIntAsync(string hex, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.HexConverterToInt(hex), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public string HexConverterToString(uint hex)
{
var output = "0x" + hex.ToString("X");
return output;
}

public async ValueTask<string> HexConverterToStringAsync(uint hex, CancellationToken cancellationToken = default)
{
return await Task.Run(() => this.HexConverterToString(hex), cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public string HexConverterToString(int hex)
{
var output = "0x" + hex.ToString("X");
return output;
}

public async ValueTask<string> 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))
Expand Down
Loading

0 comments on commit f7faa1f

Please sign in to comment.