Skip to content

Commit

Permalink
feat (FileDownloader.cs): add support for falling back to failsafe se…
Browse files Browse the repository at this point in the history
…ttings
  • Loading branch information
ksidirop-laerdal committed Oct 1, 2024
1 parent 15990eb commit 7a5f17e
Show file tree
Hide file tree
Showing 27 changed files with 602 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AndroidFileDownloader
private FsManager _fileSystemManager;
@SuppressWarnings("FieldCanBeLocal")
private final McuMgrBleTransport _transport;
private TransferController _controller;
private TransferController _downloadingController;

private int _initialBytes;
private long _downloadStartTimestamp;
Expand All @@ -30,7 +30,24 @@ public AndroidFileDownloader(@NonNull final Context context, @NonNull final Blue
_transport = new McuMgrBleTransport(context, bluetoothDevice);
}

public EAndroidFileDownloaderVerdict beginDownload(final String remoteFilePath)
/**
* Initiates a file download asynchronously. The progress is advertised through the callbacks provided by this class.
* Setup interceptors for them to get informed about the status of the firmware-installation.
*
* @param remoteFilePath the remote-file-path to the file on the remote device that you wish to download
* @param initialMtuSize sets the initial MTU for the connection that the McuMgr BLE-transport sets up for the firmware installation that will follow.
* Note that if less than 0 it gets ignored and if it doesn't fall within the range [23, 517] it will cause a hard error.
* @param windowCapacity specifies the windows-capacity for the data transfers of the BLE connection - if zero or negative the value provided gets ignored and will be set to 1 by default
* @param memoryAlignment specifies the memory-alignment to use for the data transfers of the BLE connection - if zero or negative the value provided gets ignored and will be set to 1 by default
*
* @return a verdict indicating whether the file uploading was started successfully or not
*/
public EAndroidFileDownloaderVerdict beginDownload(
final String remoteFilePath,
final int initialMtuSize,
final int windowCapacity, //todo should we keep this or remove it? it doesnt seem to be applicable to the file-downloader
final int memoryAlignment //todo should we keep this or remove it? it doesnt seem to be applicable to the file-downloader
)
{
if (_currentState != EAndroidFileDownloaderState.NONE //if the download is already in progress we bail out
&& _currentState != EAndroidFileDownloaderState.ERROR
Expand Down Expand Up @@ -68,7 +85,24 @@ public EAndroidFileDownloaderVerdict beginDownload(final String remoteFilePath)

try
{
if (initialMtuSize > 0)
{
_transport.setInitialMtu(initialMtuSize);
}

_fileSystemManager = new FsManager(_transport);

setLoggingEnabled(false);
requestHighConnectionPriority();

setState(EAndroidFileDownloaderState.IDLE);
busyStateChangedAdvertisement(true);
fileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);

_initialBytes = 0;

_remoteFilePathSanitized = remoteFilePathSanitized;
_downloadingController = _fileSystemManager.fileDownload(remoteFilePathSanitized, new FileDownloaderCallbackProxy());
}
catch (final Exception ex)
{
Expand All @@ -78,24 +112,12 @@ public EAndroidFileDownloaderVerdict beginDownload(final String remoteFilePath)
return EAndroidFileDownloaderVerdict.FAILED__INVALID_SETTINGS;
}

setLoggingEnabled(false);
requestHighConnectionPriority();

setState(EAndroidFileDownloaderState.IDLE);
busyStateChangedAdvertisement(true);
fileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);

_initialBytes = 0;

_remoteFilePathSanitized = remoteFilePathSanitized;
_controller = _fileSystemManager.fileDownload(remoteFilePathSanitized, new FileDownloaderCallbackProxy());

return EAndroidFileDownloaderVerdict.SUCCESS;
}

public void pause()
{
final TransferController transferController = _controller;
final TransferController transferController = _downloadingController;
if (transferController == null)
return;

Expand All @@ -107,7 +129,7 @@ public void pause()

public void resume()
{
final TransferController transferController = _controller;
final TransferController transferController = _downloadingController;
if (transferController == null)
return;

Expand Down Expand Up @@ -135,7 +157,7 @@ public void cancel()
{
setState(EAndroidFileDownloaderState.CANCELLING); //order

final TransferController transferController = _controller;
final TransferController transferController = _downloadingController;
if (transferController == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,23 @@ public EAndroidFileUploaderVerdict beginUpload(
resetUploadState(); //order
setLoggingEnabled(false);

_uploadController = new FileUploader( //00
_fileSystemManager,
_remoteFilePathSanitized,
data,
Math.max(1, windowCapacity),
Math.max(1, memoryAlignment)
).uploadAsync(_fileUploaderCallbackProxy);
try
{
_uploadController = new FileUploader( //00
_fileSystemManager,
_remoteFilePathSanitized,
data,
Math.max(1, windowCapacity),
Math.max(1, memoryAlignment)
).uploadAsync(_fileUploaderCallbackProxy);
}
catch (final Exception ex)
{
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, "Failed to initialize the upload", ex);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
}

return EAndroidFileUploaderVerdict.SUCCESS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ public MockedGreenNativeFileDownloaderProxySpy1(INativeFileDownloaderCallbacksPr
_mockedFileData = mockedFileData;
}

public override EFileDownloaderVerdict BeginDownload(string remoteFilePath)
public override EFileDownloaderVerdict BeginDownload(string remoteFilePath, int? initialMtuSize = null, int? windowCapacity = null, int? memoryAlignment = null)
{
var verdict = base.BeginDownload(remoteFilePath);
var verdict = base.BeginDownload(
remoteFilePath,
initialMtuSize: initialMtuSize,
windowCapacity: windowCapacity,
memoryAlignment: memoryAlignment
);

Task.Run(async () => //00 vital
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ public MockedGreenNativeFileDownloaderProxySpy5(INativeFileDownloaderCallbacksPr
{
}

public override EFileDownloaderVerdict BeginDownload(string remoteFilePath)
public override EFileDownloaderVerdict BeginDownload(string remoteFilePath, int? initialMtuSize = null, int? windowCapacity = null, int? memoryAlignment = null)
{
var verdict = base.BeginDownload(remoteFilePath);
var verdict = base.BeginDownload(
remoteFilePath,
initialMtuSize: initialMtuSize,
windowCapacity: windowCapacity,
memoryAlignment: memoryAlignment
);

Task.Run(async () => //00 vital
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,14 @@ public MockedGreenNativeFileDownloaderProxySpy6(INativeFileDownloaderCallbacksPr
}

private int _retryCountForProblematicFile;
public override EFileDownloaderVerdict BeginDownload(string remoteFilePath)
public override EFileDownloaderVerdict BeginDownload(string remoteFilePath, int? initialMtuSize = null, int? windowCapacity = null, int? memoryAlignment = null)
{
var verdict = base.BeginDownload(remoteFilePath);
var verdict = base.BeginDownload(
remoteFilePath: remoteFilePath,
initialMtuSize: initialMtuSize,
windowCapacity: windowCapacity,
memoryAlignment: memoryAlignment
);

Task.Run(async () => //00 vital
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ public MockedGreenNativeFileDownloaderProxySpy11(INativeFileDownloaderCallbacksP
{
}

public override EFileDownloaderVerdict BeginDownload(string remoteFilePath)
public override EFileDownloaderVerdict BeginDownload(string remoteFilePath, int? initialMtuSize = null, int? windowCapacity = null, int? memoryAlignment = null)
{
var verdict = base.BeginDownload(remoteFilePath);
var verdict = base.BeginDownload(
remoteFilePath: remoteFilePath,
initialMtuSize: initialMtuSize,
windowCapacity: windowCapacity,
memoryAlignment: memoryAlignment
);

Task.Run(async () => //00 vital
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ public MockedGreenNativeFileDownloaderProxySpy10(INativeFileDownloaderCallbacksP
{
}

public override EFileDownloaderVerdict BeginDownload(string remoteFilePath)
public override EFileDownloaderVerdict BeginDownload(string remoteFilePath, int? initialMtuSize = null, int? windowCapacity = null, int? memoryAlignment = null)
{
var verdict = base.BeginDownload(remoteFilePath);
var verdict = base.BeginDownload(
remoteFilePath: remoteFilePath,
initialMtuSize: initialMtuSize,
windowCapacity: windowCapacity,
memoryAlignment: memoryAlignment
);

Task.Run(async () => //00 vital
{
Expand Down
Loading

0 comments on commit 7a5f17e

Please sign in to comment.