Skip to content

Commit

Permalink
refa (DeviceResetter): the BeginReset() method now returns a verdict …
Browse files Browse the repository at this point in the history
…just like the FileUploader/Downloader classes do
  • Loading branch information
ksidirop-laerdal committed Oct 31, 2024
1 parent 95f26f5 commit 986d01c
Show file tree
Hide file tree
Showing 40 changed files with 423 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ public AndroidDeviceResetter(@NonNull final Context context, @NonNull final Blue
_transport = new McuMgrBleTransport(context, bluetoothDevice);
}

public void beginReset()
public EAndroidDeviceResetterInitializationVerdict beginReset()
{
if (!IsCold())
{ //keep first
onError("[ADR.BR.000] Another reset operation is already in progress (state='" + _currentState + "')");

return EAndroidDeviceResetterInitializationVerdict.FAILED__OTHER_RESET_ALREADY_IN_PROGRESS;
}

try
{
_manager = new DefaultManager(_transport);

setState(EAndroidDeviceResetterState.RESETTING);
setState(EAndroidDeviceResetterState.IDLE); //order
_manager = new DefaultManager(_transport); //order
setState(EAndroidDeviceResetterState.RESETTING); //order

AndroidDeviceResetter self = this;
_manager.reset(new McuMgrCallback<McuMgrOsResponse>()
Expand All @@ -65,7 +72,10 @@ public void onError(@NotNull final McuMgrException 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 @@ -87,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 @@ -109,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 @@ -32,38 +32,54 @@ public AndroidFirmwareEraser(@NonNull final Context context, @NonNull final Blue
_transport = new McuMgrBleTransport(context, bluetoothDevice);
}

public void beginErasure(final int imageIndex)
public EAndroidFirmwareEraserInitializationVerdict beginErasure(final int imageIndex)
{
busyStateChangedAdvertisement(true);

setState(EAndroidFirmwareEraserState.ERASING);
if (!IsCold())
{ //keep first
onError("[AFE.BE.000] Another reset operation is already in progress (state='" + _currentState + "')");

AndroidFirmwareEraser self = this;
return EAndroidFirmwareEraserInitializationVerdict.FAILED__OTHER_ERASURE_ALREADY_IN_PROGRESS;
}

_imageManager = new ImageManager(_transport);
_imageManager.erase(imageIndex, new McuMgrCallback<McuMgrImageResponse>()
try
{
@Override
public void onResponse(@NonNull final McuMgrImageResponse response)
setState(EAndroidFirmwareEraserState.IDLE); //order
_imageManager = new ImageManager(_transport); //order
busyStateChangedAdvertisement(true); //order
setState(EAndroidFirmwareEraserState.ERASING); //order

AndroidFirmwareEraser self = this;
_imageManager.erase(imageIndex, new McuMgrCallback<McuMgrImageResponse>()
{
if (!response.isSuccess())
{ // check for an error return code
self.onError("[AFE.BE.OR.010] Erasure failed (error-code '" + response.getReturnCode().toString() + "')", response.getReturnCode(), response.getGroupReturnCode());
return;
@Override
public void onResponse(@NonNull final McuMgrImageResponse response)
{
if (!response.isSuccess())
{ // check for an error return code
self.onError("[AFE.BE.010] Erasure failed (error-code '" + response.getReturnCode().toString() + "')", response.getReturnCode(), response.getGroupReturnCode());
return;
}

readImageErasure();
setState(EAndroidFirmwareEraserState.COMPLETE);
}

readImageErasure();
setState(EAndroidFirmwareEraserState.COMPLETE);
}
@Override
public void onError(@NonNull final McuMgrException exception)
{
self.onError("[AFE.BE.020] Erasure failed '" + exception.getMessage() + "'", exception);

@Override
public void onError(@NonNull final McuMgrException exception)
{
self.onError("[AFE.BE.OE.010] Erasure failed '" + exception.getMessage() + "'", exception);
busyStateChangedAdvertisement(false);
}
});
}
catch (final Exception ex)
{
onError("[AFE.BE.010] Failed to initialize erase operation: '" + ex.getMessage() + "'", ex);
return EAndroidFirmwareEraserInitializationVerdict.FAILED__ERROR_UPON_COMMENCING;
}

busyStateChangedAdvertisement(false);
}
});
return EAndroidFirmwareEraserInitializationVerdict.SUCCESS;
}

public void disconnect()
Expand All @@ -78,6 +94,13 @@ public void disconnect()
mcuMgrTransporter.release();
}

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

private EAndroidFirmwareEraserState _currentState = EAndroidFirmwareEraserState.NONE;

private void setState(EAndroidFirmwareEraserState newState)
Expand All @@ -103,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 @@ -80,24 +80,9 @@ public EAndroidFirmwareInstallationVerdict beginInstallation(
&& _currentState != EAndroidFirmwareInstallationState.COMPLETE
&& _currentState != EAndroidFirmwareInstallationState.CANCELLED)
{
return EAndroidFirmwareInstallationVerdict.FAILED__INSTALLATION_ALREADY_IN_PROGRESS;
}

_manager = new FirmwareUpgradeManager(_transport);
_manager.setFirmwareUpgradeCallback(new FirmwareInstallCallbackProxy());

_handlerThread = new HandlerThread("AndroidFirmwareInstaller.HandlerThread"); //todo peer review whether this is the best way to go maybe we should be getting this from the call environment?
_handlerThread.start();

_handler = new Handler(_handlerThread.getLooper());
onError(EAndroidFirmwareInstallerFatalErrorType.FAILED__INSTALLATION_ALREADY_IN_PROGRESS, "[AFI.BI.000] Another firmware installation is already in progress");

if (estimatedSwapTimeInMilliseconds >= 0 && estimatedSwapTimeInMilliseconds <= 1000)
{ //it is better to just warn the calling environment instead of erroring out
logMessageAdvertisement(
"Estimated swap-time of '" + estimatedSwapTimeInMilliseconds + "' milliseconds seems suspiciously low - did you mean to say '" + (estimatedSwapTimeInMilliseconds * 1000) + "' milliseconds?",
"FirmwareInstaller",
"WARN"
);
return EAndroidFirmwareInstallationVerdict.FAILED__INSTALLATION_ALREADY_IN_PROGRESS;
}

ImageSet images = new ImageSet();
Expand All @@ -119,6 +104,23 @@ public EAndroidFirmwareInstallationVerdict beginInstallation(
}
}

_manager = new FirmwareUpgradeManager(_transport);
_manager.setFirmwareUpgradeCallback(new FirmwareInstallCallbackProxy());

_handlerThread = new HandlerThread("AndroidFirmwareInstaller.HandlerThread"); //todo peer review whether this is the best way to go maybe we should be getting this from the call environment?
_handlerThread.start();

_handler = new Handler(_handlerThread.getLooper());

if (estimatedSwapTimeInMilliseconds >= 0 && estimatedSwapTimeInMilliseconds <= 1000)
{ //it is better to just warn the calling environment instead of erroring out
logMessageAdvertisement(
"Estimated swap-time of '" + estimatedSwapTimeInMilliseconds + "' milliseconds seems suspiciously low - did you mean to say '" + (estimatedSwapTimeInMilliseconds * 1000) + "' milliseconds?",
"FirmwareInstaller",
"WARN"
);
}

@NotNull Settings settings;
try
{
Expand Down Expand Up @@ -244,7 +246,12 @@ public String getLastFatalErrorMessage()
return _lastFatalErrorMessage;
}

public void onError(EAndroidFirmwareInstallerFatalErrorType fatalErrorType, final String errorMessage, Exception ex)
public void onError(final EAndroidFirmwareInstallerFatalErrorType fatalErrorType, final String errorMessage)
{
onError(fatalErrorType, errorMessage, null);
}

public void onError(final EAndroidFirmwareInstallerFatalErrorType fatalErrorType, final String errorMessage, final Exception ex)
{
EAndroidFirmwareInstallationState currentStateSnapshot = _currentState; //00 order
setState(EAndroidFirmwareInstallationState.ERROR); // order
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package no.laerdal.mcumgr_laerdal_wrapper;

public enum EAndroidDeviceResetterInitializationVerdict
{
SUCCESS(0),
FAILED__ERROR_UPON_COMMENCING(1), //connection problems
FAILED__OTHER_RESET_ALREADY_IN_PROGRESS(2);

@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final int _value;

EAndroidDeviceResetterInitializationVerdict(int value)
{
_value = value;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ public enum EAndroidFileUploaderVerdict //this must mirror the enum values of E[
_value = value;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package no.laerdal.mcumgr_laerdal_wrapper;

public enum EAndroidFirmwareEraserInitializationVerdict
{
SUCCESS(0),
FAILED__ERROR_UPON_COMMENCING(1), //connection problems
FAILED__OTHER_ERASURE_ALREADY_IN_PROGRESS(2);

@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final int _value;

EAndroidFirmwareEraserInitializationVerdict(int value)
{
_value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum EAndroidFirmwareInstallerFatalErrorType //this must mirror the enum
INVALID_FIRMWARE(2),
DEPLOYMENT_FAILED(3),
FIRMWARE_IMAGE_SWAP_TIMEOUT(4),
FIRMWARE_UPLOADING_ERRORED_OUT(5);
FIRMWARE_UPLOADING_ERRORED_OUT(5),
FAILED__INSTALLATION_ALREADY_IN_PROGRESS(6);

@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final int _value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
B028FB802940DF1400CB71EB /* iOSMcuManagerLibrary in Frameworks */ = {isa = PBXBuildFile; productRef = B028FB7F2940DF1400CB71EB /* iOSMcuManagerLibrary */; };
B04DC3652CD3C7810048B553 /* EIOSDeviceResetInitializationVerdict.swift in Sources */ = {isa = PBXBuildFile; fileRef = B04DC3642CD3C7810048B553 /* EIOSDeviceResetInitializationVerdict.swift */; };
B04DC3672CD3E1A20048B553 /* EIOSFirmwareErasureInitializationVerdict.swift in Sources */ = {isa = PBXBuildFile; fileRef = B04DC3662CD3E1A20048B553 /* EIOSFirmwareErasureInitializationVerdict.swift */; };
B0FF8AE92CCFF8E1004B39DE /* IOSListenerForFileUploader.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0FF8AE82CCFF8E1004B39DE /* IOSListenerForFileUploader.swift */; };
E769D0FFF3A39C575B982F6D /* InvalidFirmwareInstallationModeError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E769DB7090742CC7D6AFD01D /* InvalidFirmwareInstallationModeError.swift */; };
E769D1175ADD137DCA6B0207 /* EIOSFirmwareEraserState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E769D3130BA737A0C282D1DD /* EIOSFirmwareEraserState.swift */; };
Expand Down Expand Up @@ -36,6 +38,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
B04DC3642CD3C7810048B553 /* EIOSDeviceResetInitializationVerdict.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EIOSDeviceResetInitializationVerdict.swift; sourceTree = "<group>"; };
B04DC3662CD3E1A20048B553 /* EIOSFirmwareErasureInitializationVerdict.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EIOSFirmwareErasureInitializationVerdict.swift; sourceTree = "<group>"; };
B0C562262936567900B070BA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
B0C562282936568800B070BA /* CoreBluetooth.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBluetooth.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/CoreBluetooth.framework; sourceTree = DEVELOPER_DIR; };
B0FF8AE82CCFF8E1004B39DE /* IOSListenerForFileUploader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOSListenerForFileUploader.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,6 +104,8 @@
E769DC768A6D6F0D1CAC3C06 /* McuMgrBindingsiOS.h */,
E769D3E2D33EE281518619A7 /* IOSFirmwareInstaller.swift */,
E769D1DEDAD5F89C361DF28C /* EIOSFirmwareInstallationVerdict.swift */,
B04DC3642CD3C7810048B553 /* EIOSDeviceResetInitializationVerdict.swift */,
B04DC3662CD3E1A20048B553 /* EIOSFirmwareErasureInitializationVerdict.swift */,
E769DC8A37490B096716C6B8 /* EIOSFirmwareInstallationMode.swift */,
E769DCEDC8442691AF65E8F8 /* EIOSFirmwareInstallationState.swift */,
E769DB7090742CC7D6AFD01D /* InvalidFirmwareInstallationModeError.swift */,
Expand Down Expand Up @@ -237,13 +243,15 @@
E769D37F2EF53CC9C5858BED /* IOSListenerForFirmwareInstaller.swift in Sources */,
E769D36EAF2DD40E0A892DB2 /* DummyPlaceholder.swift in Sources */,
E769D8E49B6F52BD0065A849 /* EIOSFileUploaderState.swift in Sources */,
B04DC3652CD3C7810048B553 /* EIOSDeviceResetInitializationVerdict.swift in Sources */,
E769DA011D5897CB39E02602 /* IOSFileUploader.swift in Sources */,
E769DEB3EFF58D41D88197C4 /* EIOSFileUploadingInitializationVerdict.swift in Sources */,
E769D339FCECEE79EEDD55C5 /* McuMgrExceptionHelpers.swift in Sources */,
E769D7F106E9BC190CDFFD19 /* IOSFileDownloader.swift in Sources */,
B0FF8AE92CCFF8E1004B39DE /* IOSListenerForFileUploader.swift in Sources */,
E769D33171D78172F096C8A4 /* EIOSFileDownloaderState.swift in Sources */,
E769D3F56B12BF1FA2695F1E /* IOSListenerForFileDownloader.swift in Sources */,
B04DC3672CD3E1A20048B553 /* EIOSFirmwareErasureInitializationVerdict.swift in Sources */,
E769D54A54C788CCE8488B9B /* EIOSFileDownloadingInitializationVerdict.swift in Sources */,
E769D5AE4CFECFEA0B509626 /* EIOSFirmwareInstallationFatalError.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@objc
public enum EIOSDeviceResetInitializationVerdict: Int {
case success = 0
case failedErrorUponCommencing = 1
case failedOtherResetAlreadyInProgress = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@objc
public enum EIOSFirmwareErasureInitializationVerdict: Int {
case success = 0
case failedErrorUponCommencing = 1
case failedOtherErasureAlreadyInProgress = 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public enum EIOSFirmwareInstallerFatalErrorType : Int //this must mirror the enu
case deploymentFailed = 3
case firmwareImageSwapTimeout = 4
case firmwareUploadingErroredOut = 5
case failedInstallationAlreadyInProgress = 6
}
Loading

0 comments on commit 986d01c

Please sign in to comment.