Search in sources :

Example 1 with McuMgrImageStateResponse

use of io.runtime.mcumgr.response.img.McuMgrImageStateResponse in project react-native-mcuboot by brandbrandnew.

the class McubootModule method test.

@ReactMethod
public void test(String id, String hash, Callback failureCallback, Callback successCallback) throws McuMgrException, IOException {
    byte[] bytesHash = Base64.decode(hash, Base64.DEFAULT);
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(id);
    transport = new McuMgrBleTransport(this.reactContext, device);
    imageManager = new ImageManager(transport);
    imageManager.test(bytesHash, new McuMgrCallback<McuMgrImageStateResponse>() {

        @Override
        public void onResponse(@NotNull McuMgrImageStateResponse response) {
            successCallback.invoke(response.toString());
        }

        @Override
        public void onError(@NotNull McuMgrException error) {
            failureCallback.invoke(error.toString());
        }
    });
}
Also used : McuMgrBleTransport(io.runtime.mcumgr.ble.McuMgrBleTransport) BluetoothDevice(android.bluetooth.BluetoothDevice) McuMgrImageStateResponse(io.runtime.mcumgr.response.img.McuMgrImageStateResponse) McuMgrException(io.runtime.mcumgr.exception.McuMgrException) ImageManager(io.runtime.mcumgr.managers.ImageManager) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 2 with McuMgrImageStateResponse

use of io.runtime.mcumgr.response.img.McuMgrImageStateResponse in project Android-nRF-Connect-Device-Manager by NordicSemiconductor.

the class Test method start.

@Override
public void start(@NotNull final TaskManager<Settings, State> performer) {
    final Settings settings = performer.getSettings();
    final ImageManager manager = new ImageManager(settings.transport);
    manager.test(hash, new McuMgrCallback<McuMgrImageStateResponse>() {

        @Override
        public void onResponse(@NotNull final McuMgrImageStateResponse response) {
            LOG.trace("Test response: {}", response.toString());
            // Check for an error return code.
            if (!response.isSuccess()) {
                performer.onTaskFailed(Test.this, new McuMgrErrorException(response.getReturnCode()));
                return;
            }
            // Search for tested slot and check its status.
            for (final McuMgrImageStateResponse.ImageSlot slot : response.images) {
                if (Arrays.equals(slot.hash, hash)) {
                    if (slot.pending) {
                        performer.onTaskCompleted(Test.this);
                    } else {
                        performer.onTaskFailed(Test.this, new McuMgrException("Tested image is not in a pending state."));
                    }
                    return;
                }
            }
            performer.onTaskFailed(Test.this, new McuMgrException("Tested image not found."));
        }

        @Override
        public void onError(@NotNull McuMgrException e) {
            performer.onTaskFailed(Test.this, e);
        }
    });
}
Also used : McuMgrErrorException(io.runtime.mcumgr.exception.McuMgrErrorException) McuMgrImageStateResponse(io.runtime.mcumgr.response.img.McuMgrImageStateResponse) McuMgrException(io.runtime.mcumgr.exception.McuMgrException) Settings(io.runtime.mcumgr.dfu.FirmwareUpgradeManager.Settings) ImageManager(io.runtime.mcumgr.managers.ImageManager)

Example 3 with McuMgrImageStateResponse

use of io.runtime.mcumgr.response.img.McuMgrImageStateResponse in project Android-nRF-Connect-Device-Manager by NordicSemiconductor.

the class Validate method start.

@Override
public void start(@NotNull final TaskManager<Settings, State> performer) {
    final Settings settings = performer.getSettings();
    final ImageManager manager = new ImageManager(settings.transport);
    manager.list(new McuMgrCallback<McuMgrImageStateResponse>() {

        @Override
        public void onResponse(@NotNull final McuMgrImageStateResponse response) {
            LOG.trace("Validation response: {}", response.toString());
            // Check for an error return code.
            if (!response.isSuccess()) {
                performer.onTaskFailed(Validate.this, new McuMgrErrorException(response.getReturnCode()));
                return;
            }
            // Initial validation.
            McuMgrImageStateResponse.ImageSlot[] slots = response.images;
            if (slots == null) {
                LOG.error("Missing images information: {}", response.toString());
                performer.onTaskFailed(Validate.this, new McuMgrException("Missing images information"));
                return;
            }
            // The following code adds Erase, Upload, Test, Reset and Confirm operations
            // to the task priority queue. The priorities of those tasks ensure they are executed
            // in the right (given 2 lines above) order.
            // The flag indicates whether a reset operation should be performed during the process.
            boolean resetRequired = false;
            boolean initialResetRequired = false;
            // For each image that is to be sent, check if the same image has already been sent.
            for (final Pair<Integer, McuMgrImage> pair : images) {
                final int image = pair.first;
                final McuMgrImage mcuMgrImage = pair.second;
                // The following flags will be updated based on the received slot information.
                boolean found = false;
                // TEST command was sent
                boolean pending = false;
                // CONFIRM command was sent
                boolean permanent = false;
                // Image has booted and confirmed itself
                boolean confirmed = false;
                // Image has booted
                boolean active = false;
                for (final McuMgrImageStateResponse.ImageSlot slot : slots) {
                    if (slot.image != image)
                        continue;
                    // required. The image may need testing or confirming, or may already be running.
                    if (Arrays.equals(slot.hash, mcuMgrImage.getHash())) {
                        found = true;
                        pending = slot.pending;
                        permanent = slot.permanent;
                        confirmed = slot.confirmed;
                        active = slot.active;
                        // primary slot.
                        if (confirmed && slot.slot == SLOT_SECONDARY) {
                            resetRequired = true;
                        }
                        break;
                    } else {
                        if (slot.slot == SLOT_SECONDARY) {
                            // The slot will be overridden automatically. Nothing needs to be done.
                            if (!slot.pending && !slot.confirmed) {
                                continue;
                            }
                            // risky.
                            if (slot.confirmed) {
                                initialResetRequired = true;
                            }
                            // slot cannot be overwritten (NO MEMORY error would be returned).
                            if (slot.pending || slot.permanent) {
                                initialResetRequired = true;
                            }
                        }
                    }
                }
                if (!found) {
                    performer.enqueue(new Upload(mcuMgrImage, image));
                }
                switch(mode) {
                    case TEST_AND_CONFIRM:
                        // running image, send test command and update the flag.
                        if (!pending && !confirmed && !active) {
                            performer.enqueue(new Test(mcuMgrImage.getHash()));
                            pending = true;
                        }
                        // If the image is pending, reset is required.
                        if (pending) {
                            resetRequired = true;
                        }
                        if (!permanent && !confirmed) {
                            performer.enqueue(new ConfirmAfterReset(mcuMgrImage.getHash()));
                        }
                        break;
                    case TEST_ONLY:
                        // running image, send test command and update the flag.
                        if (!pending && !confirmed && !active) {
                            performer.enqueue(new Test(mcuMgrImage.getHash()));
                            pending = true;
                        }
                        // If the image is pending, reset is required.
                        if (pending) {
                            resetRequired = true;
                        }
                        break;
                    case CONFIRM_ONLY:
                        // If the firmware is not confirmed yet, confirm t.
                        if (!permanent && !confirmed) {
                            performer.enqueue(new Confirm(mcuMgrImage.getHash()));
                            permanent = true;
                        }
                        if (permanent) {
                            resetRequired = true;
                        }
                        break;
                }
            }
            // To make sure the reset command are added just once, they're added based on flags.
            if (initialResetRequired) {
                performer.enqueue(new ResetBeforeUpload());
            }
            if (resetRequired) {
                if (eraseSettings)
                    performer.enqueue(new EraseStorage());
                performer.enqueue(new Reset());
            }
            performer.onTaskCompleted(Validate.this);
        }

        @Override
        public void onError(@NotNull final McuMgrException e) {
            performer.onTaskFailed(Validate.this, e);
        }
    });
}
Also used : McuMgrImageStateResponse(io.runtime.mcumgr.response.img.McuMgrImageStateResponse) ImageManager(io.runtime.mcumgr.managers.ImageManager) McuMgrImage(io.runtime.mcumgr.image.McuMgrImage) McuMgrErrorException(io.runtime.mcumgr.exception.McuMgrErrorException) McuMgrException(io.runtime.mcumgr.exception.McuMgrException) Settings(io.runtime.mcumgr.dfu.FirmwareUpgradeManager.Settings) Pair(android.util.Pair)

Example 4 with McuMgrImageStateResponse

use of io.runtime.mcumgr.response.img.McuMgrImageStateResponse in project Android-nRF-Connect-Device-Manager by NordicSemiconductor.

the class McuMgrResponseTest method buildResponse_missing_splitStatus.

@Test
public void buildResponse_missing_splitStatus() throws IOException {
    final byte[] data = { (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x79, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xBF, (byte) 0x66, (byte) 0x69, (byte) 0x6D, (byte) 0x61, (byte) 0x67, (byte) 0x65, (byte) 0x73, (byte) 0x9F, (byte) 0xBF, (byte) 0x64, (byte) 0x73, (byte) 0x6C, (byte) 0x6F, (byte) 0x74, (byte) 0x00, (byte) 0x67, (byte) 0x76, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x33, (byte) 0x2E, (byte) 0x30, (byte) 0x2E, (byte) 0x30, (byte) 0x64, (byte) 0x68, (byte) 0x61, (byte) 0x73, (byte) 0x68, (byte) 0x58, (byte) 0x20, (byte) 0x0D, (byte) 0x4F, (byte) 0x2C, (byte) 0x73, (byte) 0xD8, (byte) 0x58, (byte) 0x1C, (byte) 0xE9, (byte) 0x30, (byte) 0x15, (byte) 0xA2, (byte) 0xDA, (byte) 0xE6, (byte) 0x38, (byte) 0x73, (byte) 0x99, (byte) 0xF0, (byte) 0x20, (byte) 0x1C, (byte) 0x3C, (byte) 0x0C, (byte) 0x56, (byte) 0x15, (byte) 0xF0, (byte) 0xDD, (byte) 0x7A, (byte) 0x2D, (byte) 0x49, (byte) 0xCD, (byte) 0xF3, (byte) 0x46, (byte) 0xB2, (byte) 0x68, (byte) 0x62, (byte) 0x6F, (byte) 0x6F, (byte) 0x74, (byte) 0x61, (byte) 0x62, (byte) 0x6C, (byte) 0x65, (byte) 0xF5, (byte) 0x67, (byte) 0x70, (byte) 0x65, (byte) 0x6E, (byte) 0x64, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0xF4, (byte) 0x69, (byte) 0x63, (byte) 0x6F, (byte) 0x6E, (byte) 0x66, (byte) 0x69, (byte) 0x72, (byte) 0x6D, (byte) 0x65, (byte) 0x64, (byte) 0xF5, (byte) 0x66, (byte) 0x61, (byte) 0x63, (byte) 0x74, (byte) 0x69, (byte) 0x76, (byte) 0x65, (byte) 0xF5, (byte) 0x69, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x6D, (byte) 0x61, (byte) 0x6E, (byte) 0x65, (byte) 0x6E, (byte) 0x74, // no slot 2
    (byte) 0xF4, // no slot 2
    (byte) 0xFF, // no slot 2
    (byte) 0xFF, (byte) 0xFF };
    McuMgrImageStateResponse response = McuMgrResponse.buildResponse(McuMgrScheme.BLE, data, McuMgrImageStateResponse.class);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    assertNotNull(response.images);
    assertEquals(1, response.images.length);
    assertEquals(0, response.images[0].slot);
    assertEquals("3.0.0", response.images[0].version);
    assertTrue(response.images[0].bootable);
    assertFalse(response.images[0].pending);
    assertTrue(response.images[0].confirmed);
    assertTrue(response.images[0].active);
    assertFalse(response.images[0].permanent);
    assertEquals(0, response.splitStatus);
}
Also used : McuMgrImageStateResponse(io.runtime.mcumgr.response.img.McuMgrImageStateResponse) Test(org.junit.Test)

Example 5 with McuMgrImageStateResponse

use of io.runtime.mcumgr.response.img.McuMgrImageStateResponse in project Android-nRF-Connect-Device-Manager by NordicSemiconductor.

the class McuMgrResponseTest method buildResponse_typo.

@Test
public void buildResponse_typo() throws IOException {
    final byte[] data = { (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x79, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xBF, (byte) 0x66, (byte) 0x69, (byte) 0x6D, (byte) 0x61, (byte) 0x67, (byte) 0x65, (byte) 0x73, (byte) 0x9F, (byte) 0xBF, (byte) 0x64, (byte) 0x73, (byte) 0x6C, (byte) 0x6F, (byte) 0x74, (byte) 0x00, (byte) 0x67, (byte) 0x76, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x33, (byte) 0x2E, (byte) 0x30, (byte) 0x2E, (byte) 0x30, (byte) 0x64, (byte) 0x68, (byte) 0x61, (byte) 0x73, (byte) 0x68, (byte) 0x58, (byte) 0x20, (byte) 0x0D, (byte) 0x4F, (byte) 0x2C, (byte) 0x73, (byte) 0xD8, (byte) 0x58, (byte) 0x1C, (byte) 0xE9, (byte) 0x30, (byte) 0x15, (byte) 0xA2, (byte) 0xDA, (byte) 0xE6, (byte) 0x38, (byte) 0x73, (byte) 0x99, (byte) 0xF0, (byte) 0x20, (byte) 0x1C, (byte) 0x3C, (byte) 0x0C, (byte) 0x56, (byte) 0x15, (byte) 0xF0, (byte) 0xDD, (byte) 0x7A, (byte) 0x2D, (byte) 0x49, (byte) 0xCD, (byte) 0xF3, (byte) 0x46, (byte) 0xB2, (byte) 0x68, (byte) 0x62, (byte) 0x6F, (byte) 0x6F, (byte) 0x74, (byte) 0x61, (byte) 0x62, (byte) 0x6C, (byte) 0x65, (byte) 0xF5, (byte) 0x67, (byte) 0x70, (byte) 0x65, (byte) 0x6E, (byte) 0x64, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0xF4, (byte) 0x69, (byte) 0x63, (byte) 0x6F, (byte) 0x6E, (byte) 0x66, (byte) 0x69, (byte) 0x72, (byte) 0x6D, (byte) 0x65, (byte) 0x64, (byte) 0xF5, (byte) 0x66, (byte) 0x61, (byte) 0x63, (byte) 0x74, (byte) 0x69, (byte) 0x76, (byte) 0x65, (byte) 0xF5, (byte) 0x69, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x6D, (byte) 0x61, (byte) 0x6E, (byte) 0x65, (byte) 0x6E, (byte) 0x74, // no slot 2
    (byte) 0xF4, // no slot 2
    (byte) 0xFF, // no slot 2
    (byte) 0xFF, // 6C -> 6D
    (byte) 0x6B, // 6C -> 6D
    (byte) 0x73, // 6C -> 6D
    (byte) 0x70, // 6C -> 6D
    (byte) 0x6D, (byte) 0x69, (byte) 0x74, (byte) 0x53, (byte) 0x74, (byte) 0x61, (byte) 0x74, (byte) 0x75, (byte) 0x73, (byte) 0x00, (byte) 0xFF };
    McuMgrImageStateResponse response = McuMgrResponse.buildResponse(McuMgrScheme.BLE, data, McuMgrImageStateResponse.class);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    assertNotNull(response.images);
    assertEquals(1, response.images.length);
    assertEquals(0, response.images[0].slot);
    assertEquals("3.0.0", response.images[0].version);
    assertTrue(response.images[0].bootable);
    assertFalse(response.images[0].pending);
    assertTrue(response.images[0].confirmed);
    assertTrue(response.images[0].active);
    assertFalse(response.images[0].permanent);
    assertEquals(0, response.splitStatus);
}
Also used : McuMgrImageStateResponse(io.runtime.mcumgr.response.img.McuMgrImageStateResponse) Test(org.junit.Test)

Aggregations

McuMgrImageStateResponse (io.runtime.mcumgr.response.img.McuMgrImageStateResponse)11 McuMgrException (io.runtime.mcumgr.exception.McuMgrException)6 ImageManager (io.runtime.mcumgr.managers.ImageManager)6 Test (org.junit.Test)4 BluetoothDevice (android.bluetooth.BluetoothDevice)3 ReactMethod (com.facebook.react.bridge.ReactMethod)3 McuMgrBleTransport (io.runtime.mcumgr.ble.McuMgrBleTransport)3 Settings (io.runtime.mcumgr.dfu.FirmwareUpgradeManager.Settings)3 McuMgrErrorException (io.runtime.mcumgr.exception.McuMgrErrorException)3 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 StyleSpan (android.text.style.StyleSpan)1 Pair (android.util.Pair)1 WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 WritableNativeArray (com.facebook.react.bridge.WritableNativeArray)1 WritableNativeMap (com.facebook.react.bridge.WritableNativeMap)1 McuMgrImage (io.runtime.mcumgr.image.McuMgrImage)1