Skip to content

Commit

Permalink
clean (DfuInstallation.cs): trivial neutral cleanups for the sake of …
Browse files Browse the repository at this point in the history
…readability
  • Loading branch information
ksidirop-laerdal committed Nov 14, 2024
1 parent c28f81c commit d2c5ac1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 90 deletions.
1 change: 1 addition & 0 deletions Laerdal.Dfu/Laerdal.Dfu.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=ios/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platforms/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platforms_002Eandroid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platforms_002Edroid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platforms_002Eios/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platforms_002Eiosandmaccatalyst/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platforms_002Enetx/@EntryIndexedValue">True</s:Boolean>
Expand Down
101 changes: 42 additions & 59 deletions Laerdal.Dfu/Platforms.Droid/DfuInstallation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,101 +13,83 @@ namespace Laerdal.Dfu
{
public partial class DfuInstallation
{
public Func<Laerdal.Dfu.Bindings.Android.DfuServiceInitiator, Laerdal.Dfu.Bindings.Android.DfuServiceInitiator> CustomDfuServiceInitiatorConfiguration { get; set; } = (dfuInitiator) => dfuInitiator;
public Func<Laerdal.Dfu.Bindings.Android.DfuServiceInitiator, Laerdal.Dfu.Bindings.Android.DfuServiceInitiator> CustomDfuServiceInitiatorConfiguration { get; set; } = dfuInitiator => dfuInitiator;

private void SetInitiator()
{
DfuProgressListener = new DfuProgressListener(this);
DfuLogger = new DfuLogger(DeviceId);
DfuProgressListener = new DfuProgressListener(this);

Initiator = new Laerdal.Dfu.Bindings.Android.DfuServiceInitiator(DeviceId).SetZip(FileUrl);

// PacketsReceiptNotifications
Initiator = Initiator.SetPacketsReceiptNotificationsEnabled(PacketReceiptNotificationParameter.HasValue);
Initiator = Initiator.SetPacketsReceiptNotificationsValue(PacketReceiptNotificationParameter ?? Laerdal.Dfu.Bindings.Android.DfuServiceInitiator.DefaultPrnValue);

// DataObjectPreparationDelay
Initiator = Initiator.SetPrepareDataObjectDelay((long) (DataObjectPreparationDelay ?? 0));
Initiator = new Laerdal.Dfu.Bindings.Android.DfuServiceInitiator(DeviceId).SetZip(FileUrl)!;
Initiator = Initiator.SetPacketsReceiptNotificationsEnabled(PacketReceiptNotificationParameter.HasValue)!; // PacketsReceiptNotifications
Initiator = Initiator.SetPacketsReceiptNotificationsValue(PacketReceiptNotificationParameter ?? Laerdal.Dfu.Bindings.Android.DfuServiceInitiator.DefaultPrnValue)!;
Initiator = Initiator.SetPrepareDataObjectDelay((long) (DataObjectPreparationDelay ?? 0))!; // DataObjectPreparationDelay

// DisableResume
if (DisableResume ?? false)
{
Initiator = Initiator.DisableResume();
Initiator = Initiator.DisableResume()!;
}

// AlternativeAdvertisingName
if (!string.IsNullOrEmpty(AlternativeAdvertisingName))
{
Initiator = Initiator.SetDeviceName(AlternativeAdvertisingName);
Initiator = Initiator.SetDeviceName(AlternativeAdvertisingName)!;
}

// ForceScanningForNewAddressInLegacyDfu
if (ForceScanningForNewAddressInLegacyDfu.HasValue)
{
Initiator = Initiator.SetForceScanningForNewAddressInLegacyDfu(ForceScanningForNewAddressInLegacyDfu.Value);
Initiator = Initiator.SetForceScanningForNewAddressInLegacyDfu(ForceScanningForNewAddressInLegacyDfu.Value)!;
}

// EnableUnsafeExperimentalButtonlessServiceInSecureDfu
if (EnableUnsafeExperimentalButtonlessServiceInSecureDfu.HasValue)
{
Initiator = Initiator.SetUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(EnableUnsafeExperimentalButtonlessServiceInSecureDfu.Value);
Initiator = Initiator.SetUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(EnableUnsafeExperimentalButtonlessServiceInSecureDfu.Value)!;
}

// ForceDfu

if (ForceDfu.HasValue)
{
Initiator = Initiator.SetForceDfu(ForceDfu.Value);
Initiator = Initiator.SetForceDfu(ForceDfu.Value)!;
}

// DisableMtuRequest
if (DisableMtuRequest ?? false)
{
Initiator = Initiator.DisableMtuRequest();
Initiator = Initiator.DisableMtuRequest()!;
}

// DisableNotification
if (DisableNotification.HasValue)
{
Initiator = Initiator.SetDisableNotification(DisableNotification.Value);
Initiator = Initiator.SetDisableNotification(DisableNotification.Value)!;
}

// MbrSize
Initiator = Initiator.SetMbrSize(MbrSize ?? Laerdal.Dfu.Bindings.Android.DfuServiceInitiator.DefaultMbrSize);
Initiator = Initiator.SetMbrSize(MbrSize ?? Laerdal.Dfu.Bindings.Android.DfuServiceInitiator.DefaultMbrSize)!;

// Scope
if (Scope.HasValue)
{
Initiator = Initiator.SetScope((int) Scope.Value);
Initiator = Initiator.SetScope((int) Scope.Value)!;
}

// Foreground
if (Foreground.HasValue)
{
Initiator = Initiator.SetForeground(Foreground.Value);
Initiator = Initiator.SetForeground(Foreground.Value)!;
}

// KeepBond

if (KeepBond.HasValue)
{
Initiator = Initiator.SetKeepBond(KeepBond.Value);
Initiator = Initiator.SetKeepBond(KeepBond.Value)!;
}

// RestoreBond
if (RestoreBond.HasValue)
{
Initiator = Initiator.SetRestoreBond(RestoreBond.Value);
Initiator = Initiator.SetRestoreBond(RestoreBond.Value)!;
}

// Mtu
if (Mtu.HasValue)
{
Initiator = Initiator.SetMtu(Mtu.Value);
Initiator = Initiator.SetMtu(Mtu.Value)!;
}

// NumberOfRetries

if (NumberOfRetries.HasValue)
{
Initiator = Initiator.SetNumberOfRetries(NumberOfRetries.Value);
Initiator = Initiator.SetNumberOfRetries(NumberOfRetries.Value)!;
}

// For Oreo progress
Expand All @@ -116,7 +98,7 @@ private void SetInitiator()
Laerdal.Dfu.Bindings.Android.DfuServiceInitiator.CreateDfuNotificationChannel(Android.App.Application.Context);
}

Initiator = CustomDfuServiceInitiatorConfiguration?.Invoke(Initiator);
Initiator = CustomDfuServiceInitiatorConfiguration?.Invoke(Initiator)!;

// public DfuServiceInitiator SetCurrentMtu(int mtu)
// public DfuServiceInitiator SetCustomUuidsForButtonlessDfuWithBondSharing(UUID buttonlessDfuServiceUuid, UUID buttonlessDfuControlPointUuid)
Expand All @@ -126,28 +108,24 @@ private void SetInitiator()
// public DfuServiceInitiator SetCustomUuidsForSecureDfu(UUID dfuServiceUuid, UUID dfuControlPointUuid, UUID dfuPacketUuid)
}

private DfuLogger DfuLogger { get; set;}
private DfuProgressListener DfuProgressListener { get; set;}

public Laerdal.Dfu.Bindings.Android.DfuServiceInitiator Initiator { get; private set; }

public Laerdal.Dfu.Bindings.Android.DfuServiceController Controller { get; private set; }

private DfuProgressListener DfuProgressListener { get; set;}

private DfuLogger DfuLogger { get; set;}

public DfuInstallation(string deviceId, string fileUrl) : base(deviceId, fileUrl)
{
}

public DfuInstallation() : base()
public DfuInstallation()
{
}

public override void Start()
{
if (Controller != null)
{
throw new System.Exception("Controller is already set.");
}

SetInitiator();
Controller = Initiator.Start(Android.App.Application.Context, Class.FromType(typeof(DfuService)));
Expand All @@ -170,26 +148,31 @@ public override void Abort()

protected override void Dispose(bool disposing)
{
if (!disposing)
return;

DfuProgressListener?.Dispose();
if (disposing)
{
Initiator?.Dispose();
Controller?.Dispose();
DfuLogger?.Dispose();
}
DfuProgressListener = null;

Initiator?.Dispose();
Initiator = null;

Controller?.Dispose();
Controller = null;

DfuLogger?.Dispose();
DfuLogger = null;
}

public bool CheckDeviceAddress(string deviceAddress)
{
if (deviceAddress == DeviceId)
{
return true;
}

var parsed = DeviceId.Split(':').Select(p => int.Parse(p, NumberStyles.HexNumber)).ToList();
parsed[^1] += 1;

var deviceAddressPlusOne = string.Join(":", parsed.Select(p => p.ToString("X2")));

if (deviceAddress == deviceAddressPlusOne)
{
return true;
Expand Down
76 changes: 45 additions & 31 deletions Laerdal.Dfu/Platforms.iOSandMacCatalyst/DfuInstallation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ namespace Laerdal.Dfu
public partial class DfuInstallation
{
public static Func<DispatchQueue> Queue { get; set; } = () => DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default);
public static Func<DispatchQueue> LoggerQueue { get; set; } = () => DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default);
public static Func<DispatchQueue> DelegateQueue { get; set; } = () => DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default);
public static Func<DispatchQueue> ProgressQueue { get; set; } = () => DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default);
public static Func<DispatchQueue> LoggerQueue { get; set; } = () => DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default);

public Func<Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator, Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator> CustomDfuServiceInitiatorConfiguration { get; set; } = (dfuInitiator) => dfuInitiator;

public Func<Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator, Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator> CustomDfuServiceInitiatorConfiguration { get; set; } = dfuInitiator => dfuInitiator;
private void SetInitiator()
{
DfuProgressDelegate = new DfuProgressDelegate(this);
DfuServiceDelegate = new DfuServiceDelegate(this);
DfuProgressDelegate = new DfuProgressDelegate(this);
DfuPeripheralSelectorDelegate = new DfuPeripheralSelectorDelegate(this);

Firmware = new Laerdal.Dfu.Bindings.iOS.DFUFirmware(new NSUrl(FileUrl, false), out NSError error);
Firmware = new Laerdal.Dfu.Bindings.iOS.DFUFirmware(new NSUrl(FileUrl, isDir: false), out _);

Initiator = new Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator(
queue: Queue.Invoke(),
Expand All @@ -30,74 +29,75 @@ private void SetInitiator()
)
{
Logger = new DfuLogger(),
WeakProgressDelegate = DfuProgressDelegate,
WeakDelegate = DfuServiceDelegate,
WeakProgressDelegate = DfuProgressDelegate,
};

Initiator = Initiator.WithFirmware(Firmware);

// PacketsReceiptNotifications
if (PacketReceiptNotificationParameter.HasValue)
{
Initiator.PacketReceiptNotificationParameter = PacketReceiptNotificationParameter.Value;

// DataObjectPreparationDelay
}
if (DataObjectPreparationDelay.HasValue)
{
Initiator.DataObjectPreparationDelay = DataObjectPreparationDelay.Value;
}

// DisableResume
if (DisableResume.HasValue)
{
Initiator.DisableResume = DisableResume.Value;
}

// AlternativeAdvertisingName
Initiator.AlternativeAdvertisingNameEnabled = !string.IsNullOrEmpty(AlternativeAdvertisingName);
Initiator.AlternativeAdvertisingName = AlternativeAdvertisingName;

// ForceScanningForNewAddressInLegacyDfu
Initiator.AlternativeAdvertisingNameEnabled = !string.IsNullOrEmpty(AlternativeAdvertisingName);
if (ForceScanningForNewAddressInLegacyDfu.HasValue)
{
Initiator.ForceScanningForNewAddressInLegacyDfu = ForceScanningForNewAddressInLegacyDfu.Value;

// EnableUnsafeExperimentalButtonlessServiceInSecureDfu
}
if (EnableUnsafeExperimentalButtonlessServiceInSecureDfu.HasValue)
{
Initiator.ForceScanningForNewAddressInLegacyDfu = EnableUnsafeExperimentalButtonlessServiceInSecureDfu.Value;

// ForceDfu
}
if (ForceDfu.HasValue)
{
Initiator.ForceDfu = ForceDfu.Value;
}

// ConnectionTimeout
if (ConnectionTimeout.HasValue)
{
Initiator.ConnectionTimeout = ConnectionTimeout.Value;
}

Initiator = CustomDfuServiceInitiatorConfiguration?.Invoke(Initiator);

// public DFUUuidHelper UuidHelper {get; set;}
}

public Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator Initiator { get; private set; }

public Laerdal.Dfu.Bindings.iOS.DFUFirmware Firmware { get; private set; }public Laerdal.Dfu.Bindings.iOS.DFUServiceInitiator Initiator { get; private set; }
public Laerdal.Dfu.Bindings.iOS.DFUServiceController Controller { get; private set; }

public Laerdal.Dfu.Bindings.iOS.DFUFirmware Firmware { get; private set; }
private DfuServiceDelegate DfuServiceDelegate { get;set; }

private DfuProgressDelegate DfuProgressDelegate { get; set; }

private DfuServiceDelegate DfuServiceDelegate { get; set; }


private DfuPeripheralSelectorDelegate DfuPeripheralSelectorDelegate { get; set; }

public DfuInstallation(string deviceId, string fileUrl) : base(deviceId, fileUrl)
{
}

public DfuInstallation() : base()
public DfuInstallation()
{
}

public override void Start()
{
if (Controller != null)
{
throw new System.Exception("Controller is already set.");
}

SetInitiator();
Controller = Initiator.StartWithTargetWithIdentifier(new NSUuid(DeviceId));
Expand All @@ -120,12 +120,26 @@ public override void Abort()

protected override void Dispose(bool disposing)
{
if (disposing)
{
Initiator?.Dispose();
Controller?.Dispose();
Firmware?.Dispose();
}
if (!disposing)
return;

DfuServiceDelegate?.Dispose();
DfuServiceDelegate = null;

DfuProgressDelegate?.Dispose();
DfuProgressDelegate = null;

DfuPeripheralSelectorDelegate?.Dispose();
DfuPeripheralSelectorDelegate = null;

Firmware?.Dispose();
Firmware = null;

Initiator?.Dispose();
Initiator = null;

Controller?.Dispose();
Controller = null;
}
}
}

0 comments on commit d2c5ac1

Please sign in to comment.