Skip to content

Commit

Permalink
refa (ConnectionSettingsHelpers.GetFailsafeConnectionSettingsIfConnec…
Browse files Browse the repository at this point in the history
…tionProvedToBeUnstable()): consolidated this helper method
  • Loading branch information
ksidirop-laerdal committed Oct 23, 2024
1 parent d8ce411 commit 963a8d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 72 deletions.
19 changes: 19 additions & 0 deletions Laerdal.McuMgr/Shared/Common/Helpers/ConnectionSettingsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ namespace Laerdal.McuMgr.Common.Helpers
{
static internal class ConnectionSettingsHelpers
{
static public (int? byteAlignment, int? pipelineDepth, int? initialMtuSize, int? windowCapacity, int? memoryAlignment)? GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable(
int triesCount,
int maxTriesCount,
int suspiciousTransportFailuresCount
)
{
var isConnectionTooUnstableForUploading = triesCount >= 2 && (triesCount == maxTriesCount || triesCount >= 3 && suspiciousTransportFailuresCount >= 2);
if (!isConnectionTooUnstableForUploading)
return null;

var byteAlignment = AppleTidbits.BleConnectionSettings.FailSafes.ByteAlignment; // ios + maccatalyst
var pipelineDepth = AppleTidbits.BleConnectionSettings.FailSafes.PipelineDepth; // ios + maccatalyst
var initialMtuSize = AndroidTidbits.BleConnectionSettings.FailSafes.InitialMtuSize; // android when noticing persistent failures when uploading we resort
var windowCapacity = AndroidTidbits.BleConnectionSettings.FailSafes.WindowCapacity; // android to forcing the most failsafe settings we know of just in case
var memoryAlignment = AndroidTidbits.BleConnectionSettings.FailSafes.MemoryAlignment; // android we manage to salvage this situation (works with SamsungA8 android tablets)

return (byteAlignment: byteAlignment, pipelineDepth: pipelineDepth, initialMtuSize: initialMtuSize, windowCapacity: windowCapacity, memoryAlignment: memoryAlignment);
}

static public (int? byteAlignment, int? pipelineDepth, int? initialMtuSize, int? windowCapacity, int? memoryAlignment) GetFailSafeConnectionSettingsIfHostDeviceIsProblematic(
string hostDeviceManufacturer,
string hostDeviceModel,
Expand Down
51 changes: 16 additions & 35 deletions Laerdal.McuMgr/Shared/FileDownloader/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,28 @@ public async Task<byte[]> DownloadAsync(
FatalErrorOccurred += FileDownloader_FatalErrorOccurred_;
FileDownloadProgressPercentageAndDataThroughputChanged += FileDownloader_FileDownloadProgressPercentageAndDataThroughputChanged_;

var failSafeSettingsToApply = GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable_(
triesCount_: triesCount,
maxTriesCount_: maxTriesCount,
suspiciousTransportFailuresCount_: suspiciousTransportFailuresCount,
emitWarningAboutUnstableConnection_: !didWarnOnceAboutUnstableConnection
var failSafeSettingsToApply = ConnectionSettingsHelpers.GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable(
triesCount: triesCount,
maxTriesCount: maxTriesCount,
suspiciousTransportFailuresCount: suspiciousTransportFailuresCount
);
if (failSafeSettingsToApply != null)
{
didWarnOnceAboutUnstableConnection = true;
initialMtuSize = failSafeSettingsToApply.Value.initialMtuSize;
windowCapacity = failSafeSettingsToApply.Value.windowCapacity;
memoryAlignment = failSafeSettingsToApply.Value.memoryAlignment;

if (!didWarnOnceAboutUnstableConnection)
{
didWarnOnceAboutUnstableConnection = true;
OnLogEmitted(new LogEmittedEventArgs(
level: ELogLevel.Warning,
message: $"[FD.DA.GFCSICPTBU.010] Attempt#{triesCount}: Connection is too unstable for uploading assets to the target device. Subsequent tries will use failsafe parameters on the connection " +
$"just in case it helps (initialMtuSize={failSafeSettingsToApply.Value.initialMtuSize}, windowCapacity={failSafeSettingsToApply.Value.windowCapacity}, memoryAlignment={failSafeSettingsToApply.Value.memoryAlignment})",
resource: "File",
category: "FileDownloader"
));
}
}

var verdict = BeginDownload( //00 dont use task.run here for now
Expand Down Expand Up @@ -415,35 +425,6 @@ void FileDownloader_FatalErrorOccurred_(object sender_, FatalErrorOccurredEventA
throw new DownloadCancelledException(); //20

return result;

(int? initialMtuSize, int? windowCapacity, int? memoryAlignment)? GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable_(
int triesCount_,
int maxTriesCount_,
int suspiciousTransportFailuresCount_,
bool emitWarningAboutUnstableConnection_
)
{
var isConnectionTooUnstableForDownloading_ = triesCount_ >= 2 && (triesCount_ == maxTriesCount_ || triesCount_ >= 3 && suspiciousTransportFailuresCount_ >= 2);
if (!isConnectionTooUnstableForDownloading_)
return null;

var initialMtuSize_ = AndroidTidbits.BleConnectionSettings.FailSafes.InitialMtuSize; // android when noticing persistent failures when uploading we resort
var windowCapacity_ = AndroidTidbits.BleConnectionSettings.FailSafes.WindowCapacity; // android to forcing the most failsafe settings we know of just in case
var memoryAlignment_ = AndroidTidbits.BleConnectionSettings.FailSafes.MemoryAlignment; // android we manage to salvage this situation (works with SamsungA8 android tablets)

if (emitWarningAboutUnstableConnection_)
{
OnLogEmitted(new LogEmittedEventArgs(
level: ELogLevel.Warning,
message: $"[FD.DA.GFCSICPTBU.010] Attempt#{triesCount_}: Connection is too unstable for uploading assets to the target device. Subsequent tries will use failsafe parameters on the connection " +
$"just in case it helps (initialMtuSize={initialMtuSize_}, windowCapacity={windowCapacity_}, memoryAlignment={memoryAlignment_})",
resource: "File",
category: "FileDownloader"
));
}

return (initialMtuSize: initialMtuSize_, windowCapacity: windowCapacity_, memoryAlignment: memoryAlignment_);
}

//00 we are aware that in order to be 100% accurate about timeouts we should use task.run() here without await and then await the
// taskcompletionsource right after but if we went down this path we would also have to account for exceptions thus complicating
Expand Down
53 changes: 16 additions & 37 deletions Laerdal.McuMgr/Shared/FileUploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,30 @@ public async Task UploadAsync<TData>(
FatalErrorOccurred += FileUploader_FatalErrorOccurred_;
FileUploadProgressPercentageAndDataThroughputChanged += FileUploader_FileUploadProgressPercentageAndDataThroughputChanged_;

var failSafeSettingsToApply = GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable_(
triesCount_: triesCount,
maxTriesCount_: maxTriesCount,
suspiciousTransportFailuresCount_: suspiciousTransportFailuresCount,
emitWarningAboutUnstableConnection_: !didWarnOnceAboutUnstableConnection
var failSafeSettingsToApply = ConnectionSettingsHelpers.GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable(
triesCount: triesCount,
maxTriesCount: maxTriesCount,
suspiciousTransportFailuresCount: suspiciousTransportFailuresCount
);
if (failSafeSettingsToApply != null)
{
didWarnOnceAboutUnstableConnection = true;
byteAlignment = failSafeSettingsToApply.Value.byteAlignment;
pipelineDepth = failSafeSettingsToApply.Value.pipelineDepth;
initialMtuSize = failSafeSettingsToApply.Value.initialMtuSize;
windowCapacity = failSafeSettingsToApply.Value.windowCapacity;
memoryAlignment = failSafeSettingsToApply.Value.memoryAlignment;

if (!didWarnOnceAboutUnstableConnection)
{
didWarnOnceAboutUnstableConnection = true;
OnLogEmitted(new LogEmittedEventArgs(
level: ELogLevel.Warning,
message: $"[FU.UA.GFCSICPTBU.010] Attempt#{triesCount}: Connection is too unstable for uploading assets to the target device. Subsequent tries will use failsafe parameters on the connection " +
$"just in case it helps (byteAlignment={failSafeSettingsToApply.Value.byteAlignment}, pipelineDepth={failSafeSettingsToApply.Value.pipelineDepth}, initialMtuSize={failSafeSettingsToApply.Value.initialMtuSize}, windowCapacity={failSafeSettingsToApply.Value.windowCapacity}, memoryAlignment={failSafeSettingsToApply.Value.memoryAlignment})",
resource: "File",
category: "FileUploader"
));
}
}

var verdict = BeginUpload( //00 dont use task.run here for now
Expand Down Expand Up @@ -478,37 +488,6 @@ void FileUploader_FatalErrorOccurred_(object sender, FatalErrorOccurredEventArgs
throw new UploadCancelledException(cancellationReason); //20

return;

(int? byteAlignment, int? pipelineDepth, int? initialMtuSize, int? windowCapacity, int? memoryAlignment)? GetFailsafeConnectionSettingsIfConnectionProvedToBeUnstable_(
int triesCount_,
int maxTriesCount_,
int suspiciousTransportFailuresCount_,
bool emitWarningAboutUnstableConnection_
)
{
var isConnectionTooUnstableForUploading_ = triesCount_ >= 2 && (triesCount_ == maxTriesCount_ || triesCount_ >= 3 && suspiciousTransportFailuresCount_ >= 2);
if (!isConnectionTooUnstableForUploading_)
return null;

var byteAlignment_ = AppleTidbits.BleConnectionSettings.FailSafes.ByteAlignment; // ios + maccatalyst
var pipelineDepth_ = AppleTidbits.BleConnectionSettings.FailSafes.PipelineDepth; // ios + maccatalyst
var initialMtuSize_ = AndroidTidbits.BleConnectionSettings.FailSafes.InitialMtuSize; // android when noticing persistent failures when uploading we resort
var windowCapacity_ = AndroidTidbits.BleConnectionSettings.FailSafes.WindowCapacity; // android to forcing the most failsafe settings we know of just in case
var memoryAlignment_ = AndroidTidbits.BleConnectionSettings.FailSafes.MemoryAlignment; // android we manage to salvage this situation (works with SamsungA8 android tablets)

if (emitWarningAboutUnstableConnection_)
{
OnLogEmitted(new LogEmittedEventArgs(
level: ELogLevel.Warning,
message: $"[FU.UA.GFCSICPTBU.010] Attempt#{triesCount_}: Connection is too unstable for uploading assets to the target device. Subsequent tries will use failsafe parameters on the connection " +
$"just in case it helps (byteAlignment={byteAlignment_}, pipelineDepth={pipelineDepth_}, initialMtuSize={initialMtuSize_}, windowCapacity={windowCapacity_}, memoryAlignment={memoryAlignment_})",
resource: "File",
category: "FileUploader"
));
}

return (byteAlignment: byteAlignment_, pipelineDepth: pipelineDepth_, initialMtuSize: initialMtuSize_, windowCapacity: windowCapacity_, memoryAlignment: memoryAlignment_);
}

//00 we are aware that in order to be 100% accurate about timeouts we should use task.run() here without await and then await the
// taskcompletionsource right after but if we went down this path we would also have to account for exceptions thus complicating
Expand Down

0 comments on commit 963a8d0

Please sign in to comment.