Skip to content

Commit

Permalink
Merge pull request #208 from NordicSemiconductor/bugfix/upload-cancel…
Browse files Browse the repository at this point in the history
…ation

Bugfix: Fixed crash when upload gets cancelled in the wrong moment, part 2
  • Loading branch information
philips77 authored Nov 14, 2024
2 parents de9f189 + b173d68 commit f25b2bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class UploadEnvelope extends SUITUpgradeTask {
private final static Logger LOG = LoggerFactory.getLogger(UploadEnvelope.class);
private final byte @NotNull [] envelope;
private final boolean deferInstall;
private boolean canceled = false;

/**
* Upload controller used to pause, resume, and cancel upload. Set when the upload is started.
*/
@Nullable
private TransferController mUploadController;

public UploadEnvelope(final byte @NotNull [] envelope, final boolean deferInstall) {
Expand Down Expand Up @@ -72,6 +74,12 @@ public void onUploadCompleted() {
}
};

// Check if the task was canceled before starting the upload.
if (canceled) {
callback.onUploadCanceled();
return;
}

LOG.info("Uploading SUIT envelope of size: {}", envelope.length);
final SUITUpgradePerformer.Settings settings = performer.getSettings();
final SUITManager manager = new SUITManager(performer.getTransport());
Expand All @@ -86,11 +94,17 @@ public void onUploadCompleted() {

@Override
public void pause() {
mUploadController.pause();
if (mUploadController != null) {
mUploadController.pause();
}
}

@Override
public void cancel() {
mUploadController.cancel();
if (mUploadController != null) {
mUploadController.cancel();
} else {
canceled = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class UploadResource extends SUITUpgradeTask {

private final byte @NotNull [] data;
private final int sessionId;
private boolean canceled = false;

/**
* Upload controller used to pause, resume, and cancel upload. Set when the upload is started.
*/
@Nullable
private TransferController mUploadController;

public UploadResource(
Expand Down Expand Up @@ -79,6 +81,12 @@ public void onUploadCompleted() {
}
};

// Check if the task was canceled before starting the upload.
if (canceled) {
callback.onUploadCanceled();
return;
}

LOG.info("Uploading resource with session ID: {} ({} bytes)", sessionId, data.length);
final SUITUpgradePerformer.Settings settings = performer.getSettings();
final SUITManager manager = new SUITManager(performer.getTransport());
Expand All @@ -93,11 +101,17 @@ public void onUploadCompleted() {

@Override
public void pause() {
mUploadController.pause();
if (mUploadController != null) {
mUploadController.pause();
}
}

@Override
public void cancel() {
mUploadController.cancel();
if (mUploadController != null) {
mUploadController.cancel();
} else {
canceled = true;
}
}
}

0 comments on commit f25b2bf

Please sign in to comment.