Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAN-431] File-uploading-and-downloading-samsung-a8-workarounds #156

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,50 @@ public AndroidDeviceResetter(@NonNull final Context context, @NonNull final Blue
_transport = new McuMgrBleTransport(context, bluetoothDevice);
}

public void beginReset()
public EAndroidDeviceResetterInitializationVerdict beginReset()
{
try
{
_manager = new DefaultManager(_transport);
}
catch (final Exception ex)
{
setState(EAndroidDeviceResetterState.FAILED);
onError("Failed to create manager: '" + ex.getMessage() + "'", ex);
return;
}
if (!IsCold())
{ //keep first
onError("[ADR.BR.000] Another reset operation is already in progress (state='" + _currentState + "')");

setState(EAndroidDeviceResetterState.RESETTING);

AndroidDeviceResetter self = this;
return EAndroidDeviceResetterInitializationVerdict.FAILED__OTHER_RESET_ALREADY_IN_PROGRESS;
}

_manager.reset(new McuMgrCallback<McuMgrOsResponse>()
try
{
setState(EAndroidDeviceResetterState.IDLE); //order
_manager = new DefaultManager(_transport); //order
setState(EAndroidDeviceResetterState.RESETTING); //order

@Override
public void onResponse(@NotNull final McuMgrOsResponse response)
AndroidDeviceResetter self = this;
_manager.reset(new McuMgrCallback<McuMgrOsResponse>()
{
if (!response.isSuccess())
{ // check for an error return code
self.onError("Reset failed (error-code '" + response.getReturnCode().toString() + "')", response.getReturnCode(), response.getGroupReturnCode());

setState(EAndroidDeviceResetterState.FAILED);
return;
@Override
public void onResponse(@NotNull final McuMgrOsResponse response)
{
if (!response.isSuccess())
{ // check for an error return code
self.onError("[ADR.BR.002] Reset failed (error-code '" + response.getReturnCode().toString() + "')", response.getReturnCode(), response.getGroupReturnCode());
return;
}

setState(EAndroidDeviceResetterState.COMPLETE);
}

setState(EAndroidDeviceResetterState.COMPLETE);
}

@Override
public void onError(@NotNull final McuMgrException exception)
{
self.onError("Reset failed '" + exception.getMessage() + "'", exception);

setState(EAndroidDeviceResetterState.FAILED);
}
@Override
public void onError(@NotNull final McuMgrException exception)
{
self.onError("[ADR.BR.005] Reset failed '" + exception.getMessage() + "'", exception);
}
});
}
catch (final Exception ex)
{
onError("[ADR.BR.010] Failed to initialize reset operation: '" + ex.getMessage() + "'", ex);
return EAndroidDeviceResetterInitializationVerdict.FAILED__ERROR_UPON_COMMENCING;
}

});
return EAndroidDeviceResetterInitializationVerdict.SUCCESS;
}

public void disconnect()
Expand All @@ -96,6 +97,13 @@ public EAndroidDeviceResetterState getState()

private EAndroidDeviceResetterState _currentState = EAndroidDeviceResetterState.NONE;

@Contract(pure = true)
private boolean IsCold()
{
return _currentState == EAndroidDeviceResetterState.NONE
|| _currentState == EAndroidDeviceResetterState.COMPLETE;
}

private void setState(final EAndroidDeviceResetterState newState)
{
final EAndroidDeviceResetterState oldState = _currentState; //order
Expand All @@ -118,6 +126,11 @@ public String getLastFatalErrorMessage()
return _lastFatalErrorMessage;
}

private void onError(final String errorMessage)
{
onError(errorMessage, null);
}

private void onError(final String errorMessage, final Exception exception)
{
onErrorImpl(errorMessage, McuMgrExceptionHelpers.DeduceGlobalErrorCodeFromException(exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,24 @@ public EAndroidFileDownloaderVerdict beginDownload(
return EAndroidFileDownloaderVerdict.FAILED__INVALID_SETTINGS;
}

resetDownloadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order
try
{
resetDownloadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order
setLoggingEnabledOnConnection(false); //order

final EAndroidFileDownloaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileDownloaderVerdict.SUCCESS)
return verdict;
final EAndroidFileDownloaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileDownloaderVerdict.SUCCESS)
return verdict;

ensureFileDownloaderCallbackProxyIsInitializedExactlyOnce(); //order
tryEnsureConnectionPriorityOnTransport(); //order
ensureFileDownloaderCallbackProxyIsInitializedExactlyOnce(); //order

setLoggingEnabled(false);
try
{
_downloadingController = _fileSystemManager.fileDownload(_remoteFilePathSanitized, _fileDownloaderCallbackProxy);
}
catch (final Exception ex)
{
onError("[AFD.BD.060] Failed to initialize download: " + ex.getMessage(), ex);
onError("[AFD.BD.060] Failed to initialize the download operation: " + ex.getMessage(), ex);

return EAndroidFileDownloaderVerdict.FAILED__ERROR_UPON_COMMENCING;
}
Expand All @@ -178,7 +179,7 @@ public void pause()
return;

setState(EAndroidFileDownloaderState.PAUSED);
setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
transferController.pause();
busyStateChangedAdvertisement(false);
}
Expand All @@ -194,7 +195,7 @@ public void resume()
busyStateChangedAdvertisement(true);
_initialBytes = 0;

setLoggingEnabled(false);
setLoggingEnabledOnConnection(false);
transferController.resume();
}

Expand Down Expand Up @@ -233,10 +234,11 @@ private void resetDownloadState()

private void ensureTransportIsInitializedExactlyOnce(int initialMtuSize)
{
if (_transport != null)
return;
if (_transport == null)
{
_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
}

_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
if (initialMtuSize > 0)
{
_transport.setInitialMtu(initialMtuSize);
Expand All @@ -259,27 +261,20 @@ private EAndroidFileDownloaderVerdict ensureFilesystemManagerIsInitializedExactl
try
{
_fileSystemManager = new FsManager(_transport); //order

requestHighConnectionPriority(_fileSystemManager); //order
}
catch (final Exception ex)
{
onError("[AFD.EFMIIEO.010] Failed to initialize the filesystem manager: " + ex.getMessage(), ex);

return EAndroidFileDownloaderVerdict.FAILED__INVALID_SETTINGS;
return EAndroidFileDownloaderVerdict.FAILED__ERROR_UPON_COMMENCING;
}

return EAndroidFileDownloaderVerdict.SUCCESS;
}

private void requestHighConnectionPriority(FsManager fileSystemManager)
private void tryEnsureConnectionPriorityOnTransport()
{
final McuMgrTransport mcuMgrTransporter = fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

final McuMgrBleTransport bleTransporter = (McuMgrBleTransport) mcuMgrTransporter;
bleTransporter.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
_transport.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
}

private void disposeTransport()
Expand Down Expand Up @@ -321,13 +316,9 @@ private void disposeCallbackProxy()
_fileDownloaderCallbackProxy = null;
}

private void setLoggingEnabled(final boolean enabled)
private void setLoggingEnabledOnConnection(final boolean enabled)
{
final McuMgrTransport mcuMgrTransporter = _fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

((McuMgrBleTransport) mcuMgrTransporter).setLoggingEnabled(enabled);
_transport.setLoggingEnabled(enabled);
}

private void setState(final EAndroidFileDownloaderState newState)
Expand Down Expand Up @@ -488,7 +479,7 @@ public void onDownloadFailed(@NonNull final McuMgrException exception)
{
fileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
onError(exception.getMessage(), exception);
setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
busyStateChangedAdvertisement(false);

_downloadingController = null; //game over
Expand All @@ -500,7 +491,7 @@ public void onDownloadCanceled()
fileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
setState(EAndroidFileDownloaderState.CANCELLED);
cancelledAdvertisement();
setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
busyStateChangedAdvertisement(false);

_downloadingController = null; //game over
Expand All @@ -514,7 +505,7 @@ public void onDownloadCompleted(byte @NotNull [] data)
setState(EAndroidFileDownloaderState.COMPLETE); // order vital
downloadCompletedAdvertisement(_remoteFilePathSanitized, data); // order vital

setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
busyStateChangedAdvertisement(false);

_downloadingController = null; //game over
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ public EAndroidFileUploaderVerdict beginUpload(
return EAndroidFileUploaderVerdict.FAILED__INVALID_DATA;
}

resetUploadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order

final EAndroidFileUploaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileUploaderVerdict.SUCCESS)
return verdict;
try
{
resetUploadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order
setLoggingEnabledOnTransport(false); //order

ensureFileUploaderCallbackProxyIsInitializedExactlyOnce(); //order
final EAndroidFileUploaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileUploaderVerdict.SUCCESS)
return verdict;

setLoggingEnabled(false);
requestHighConnectionPriorityOnTransport(); //order
ensureFileUploaderCallbackProxyIsInitializedExactlyOnce(); //order

try
{
FileUploader fileUploader = new FileUploader( //00
_fileSystemManager,
_remoteFilePathSanitized,
Expand Down Expand Up @@ -205,12 +205,10 @@ private void resetUploadState()

private void ensureTransportIsInitializedExactlyOnce(int initialMtuSize)
{
if (_transport != null)
return;

logMessageAdvertisement("[AFU.ETIIEO.010] (Re)Initializing transport: initial-mtu-size=" + initialMtuSize, "FileUploader", "TRACE", _remoteFilePathSanitized);

_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
if (_transport == null)
{
_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
}

if (initialMtuSize > 0)
{
Expand All @@ -236,8 +234,6 @@ private EAndroidFileUploaderVerdict ensureFilesystemManagerIsInitializedExactlyO
try
{
_fileSystemManager = new FsManager(_transport); //order

requestHighConnectionPriority(_fileSystemManager); //order
}
catch (final Exception ex)
{
Expand All @@ -256,7 +252,7 @@ public void pause()
return;

setState(EAndroidFileUploaderState.PAUSED);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
transferController.pause();
busyStateChangedAdvertisement(false);
}
Expand All @@ -272,7 +268,7 @@ public void resume()
busyStateChangedAdvertisement(true);
_initialBytes = 0;

setLoggingEnabled(false);
setLoggingEnabledOnTransport(false);
transferController.resume();
}

Expand Down Expand Up @@ -303,14 +299,9 @@ public void cancel(final String reason)
transferController.cancel(); //order
}

static private void requestHighConnectionPriority(final FsManager fileSystemManager)
private void requestHighConnectionPriorityOnTransport()
{
final McuMgrTransport mcuMgrTransporter = fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

final McuMgrBleTransport bleTransporter = (McuMgrBleTransport) mcuMgrTransporter;
bleTransporter.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
_transport.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
}

private void disposeTransport()
Expand Down Expand Up @@ -352,13 +343,9 @@ private void disposeCallbackProxy()
_fileUploaderCallbackProxy = null;
}

private void setLoggingEnabled(final boolean enabled)
private void setLoggingEnabledOnTransport(final boolean enabled)
{
final McuMgrTransport mcuMgrTransporter = _fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

((McuMgrBleTransport) mcuMgrTransporter).setLoggingEnabled(enabled);
_transport.setLoggingEnabled(enabled);
}

private void setState(final EAndroidFileUploaderState newState)
Expand Down Expand Up @@ -503,7 +490,7 @@ public void onUploadFailed(@NonNull final McuMgrException error)
{
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
onError(error.getMessage(), error);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
busyStateChangedAdvertisement(false);

_uploadController = null; //order
Expand All @@ -515,7 +502,7 @@ public void onUploadCanceled()
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
setState(EAndroidFileUploaderState.CANCELLED);
cancelledAdvertisement(_cancellationReason);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
busyStateChangedAdvertisement(false);

_uploadController = null; //order
Expand All @@ -527,7 +514,7 @@ public void onUploadCompleted()
//fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(100, 0); //no need this is taken care of inside setState()
setState(EAndroidFileUploaderState.COMPLETE);
fileUploadedAdvertisement(_remoteFilePathSanitized);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
busyStateChangedAdvertisement(false);

_uploadController = null; //order
Expand Down
Loading
Loading