From 50ada96f9b27658e016a2522def5cadc1b631fc6 Mon Sep 17 00:00:00 2001 From: Arkadiusz Balys Date: Thu, 31 Aug 2023 11:46:19 +0200 Subject: [PATCH] [nrfconnect][ota] Change image checking condition for OTA. If we want to confirm the new firmware update in the AppTask Init method we cannot rely on the imageProcessor and instead of that, we need to call the mcuboot_swap_type function to verify whether the swapping type is equal to REVERT. --- .../nrfconnect/main/AppTask.cpp | 6 +---- .../nrfconnect/main/AppTask.cpp | 6 +---- .../nrfconnect/main/AppTask.cpp | 6 +---- .../lighting-app/nrfconnect/main/AppTask.cpp | 14 ++++------- examples/lock-app/nrfconnect/main/AppTask.cpp | 6 +---- examples/platform/nrfconnect/util/OTAUtil.cpp | 23 +++++++++++-------- .../nrfconnect/util/include/OTAUtil.h | 2 +- examples/pump-app/nrfconnect/main/AppTask.cpp | 6 +---- .../nrfconnect/main/AppTask.cpp | 6 +---- .../window-app/nrfconnect/main/AppTask.cpp | 6 +---- 10 files changed, 26 insertions(+), 55 deletions(-) diff --git a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp index 7f9b4f87b74eea..217748c218519f 100644 --- a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp @@ -180,11 +180,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp index 783096c52d32b5..6ec81dfd5c363b 100644 --- a/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp @@ -139,11 +139,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/light-switch-app/nrfconnect/main/AppTask.cpp b/examples/light-switch-app/nrfconnect/main/AppTask.cpp index 6bc4138a9d635a..b64f70472558c4 100644 --- a/examples/light-switch-app/nrfconnect/main/AppTask.cpp +++ b/examples/light-switch-app/nrfconnect/main/AppTask.cpp @@ -181,11 +181,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize Timers diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index a2ad1d6e5996e5..8bbddd6b47774f 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -203,6 +203,11 @@ CHIP_ERROR AppTask::Init() GetDFUOverSMP().ConfirmNewImage(); #endif +#ifdef CONFIG_CHIP_OTA_REQUESTOR + /* OTA image confirmation must be done before the factory data init. */ + OtaConfirmNewImage(); +#endif + // Initialize lighting device (PWM) uint8_t minLightLevel = kDefaultMinLevel; Clusters::LevelControl::Attributes::MinLevel::Get(kLightEndpointId, &minLightLevel); @@ -217,15 +222,6 @@ CHIP_ERROR AppTask::Init() } mPWMDevice.SetCallbacks(ActionInitiated, ActionCompleted); -#ifdef CONFIG_CHIP_OTA_REQUESTOR - /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } -#endif - // Initialize CHIP server #if CONFIG_CHIP_FACTORY_DATA ReturnErrorOnFailure(mFactoryDataProvider.Init()); diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index ab5df56d36d9d4..17fa9e31412123 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -189,11 +189,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/platform/nrfconnect/util/OTAUtil.cpp b/examples/platform/nrfconnect/util/OTAUtil.cpp index f1c1c916aa0aa7..92e3f5c6d16546 100644 --- a/examples/platform/nrfconnect/util/OTAUtil.cpp +++ b/examples/platform/nrfconnect/util/OTAUtil.cpp @@ -66,20 +66,23 @@ void InitBasicOTARequestor() imageProcessor.TriggerFlashAction(ExternalFlashManager::Action::SLEEP); } -CHIP_ERROR OtaConfirmNewImage() +void OtaConfirmNewImage() { - CHIP_ERROR err = CHIP_NO_ERROR; + /* Check if the image is run in the REVERT mode and eventually */ + /* confirm it to prevent reverting on the next boot. */ + VerifyOrReturn(mcuboot_swap_type() == BOOT_SWAP_TYPE_REVERT); + OTAImageProcessorImpl & imageProcessor = GetOTAImageProcessor(); - if (imageProcessor.IsFirstImageRun()) + CHIP_ERROR err = System::MapErrorZephyr(boot_write_img_confirmed()); + if (CHIP_NO_ERROR == err) + { + imageProcessor.SetImageConfirmed(); + ChipLogProgress(SoftwareUpdate, "New firmware image confirmed"); + } + else { - CHIP_ERROR err = System::MapErrorZephyr(boot_write_img_confirmed()); - if (CHIP_NO_ERROR == err) - { - imageProcessor.SetImageConfirmed(); - } + ChipLogError(SoftwareUpdate, "Failed to confirm firmware image, it will be reverted on the next boot"); } - ChipLogError(SoftwareUpdate, "Failed to confirm firmware image, it will be reverted on the next boot"); - return err; } #endif diff --git a/examples/platform/nrfconnect/util/include/OTAUtil.h b/examples/platform/nrfconnect/util/include/OTAUtil.h index 2e120f6b7db096..9c4c6d8d410bc5 100644 --- a/examples/platform/nrfconnect/util/include/OTAUtil.h +++ b/examples/platform/nrfconnect/util/include/OTAUtil.h @@ -54,7 +54,7 @@ void InitBasicOTARequestor(); * boot after the OTA update. * Other CHIP_ERROR codes if the image could not be confirmed. */ -CHIP_ERROR OtaConfirmNewImage(); +void OtaConfirmNewImage(); #endif // CONFIG_CHIP_OTA_REQUESTOR diff --git a/examples/pump-app/nrfconnect/main/AppTask.cpp b/examples/pump-app/nrfconnect/main/AppTask.cpp index d21908bfa165b4..b2406ed56cf29d 100644 --- a/examples/pump-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-app/nrfconnect/main/AppTask.cpp @@ -162,11 +162,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp index f1b0fb19a3a19b..9b00a541cada3b 100644 --- a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp @@ -160,11 +160,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/window-app/nrfconnect/main/AppTask.cpp b/examples/window-app/nrfconnect/main/AppTask.cpp index 5b80507a375c71..ed16dc1311dff8 100644 --- a/examples/window-app/nrfconnect/main/AppTask.cpp +++ b/examples/window-app/nrfconnect/main/AppTask.cpp @@ -167,11 +167,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server