From 3114537b3fdb1daf37b14bd54d78829e8aeda600 Mon Sep 17 00:00:00 2001 From: pankore <86098180+pankore@users.noreply.github.com> Date: Tue, 10 May 2022 11:31:03 +0800 Subject: [PATCH] [Ameba] NotifyUpdateApplied sent after rebooting into new OTA image (#18079) * [OTA] NotifyUpdateApplied to be sent after device first reboot into new image - Added IsFirstRun and ConfirmCurrentImage checks - Currently added delay of 15seconds in example_matter to give time for ameba to obtain ipv6 address - TODO: Need to move wlan fast connect into matter using DCT * Restyled by clang-format Co-authored-by: Restyled.io --- src/platform/Ameba/AmebaOTAImageProcessor.cpp | 47 +++++++++++++++---- src/platform/Ameba/AmebaOTAImageProcessor.h | 5 +- src/platform/Ameba/ConfigurationManagerImpl.h | 12 ++--- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/platform/Ameba/AmebaOTAImageProcessor.cpp b/src/platform/Ameba/AmebaOTAImageProcessor.cpp index 1f4df295bf8c12..ef9e51b8a6eccf 100644 --- a/src/platform/Ameba/AmebaOTAImageProcessor.cpp +++ b/src/platform/Ameba/AmebaOTAImageProcessor.cpp @@ -79,6 +79,38 @@ CHIP_ERROR AmebaOTAImageProcessor::ProcessBlock(ByteSpan & block) return CHIP_NO_ERROR; } +bool AmebaOTAImageProcessor::IsFirstImageRun() +{ + OTARequestorInterface * requestor = chip::GetRequestorInstance(); + if (requestor == nullptr) + { + return false; + } + + return requestor->GetCurrentUpdateState() == OTARequestorInterface::OTAUpdateStateEnum::kApplying; +} + +CHIP_ERROR AmebaOTAImageProcessor::ConfirmCurrentImage() +{ + OTARequestorInterface * requestor = chip::GetRequestorInstance(); + if (requestor == nullptr) + { + return CHIP_ERROR_INTERNAL; + } + + uint32_t currentVersion; + uint32_t targetVersion = requestor->GetTargetVersion(); + ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetSoftwareVersion(currentVersion)); + if (currentVersion != targetVersion) + { + ChipLogError(SoftwareUpdate, "Current software version = %" PRIu32 ", expected software version = %" PRIu32, currentVersion, + targetVersion); + return CHIP_ERROR_INCORRECT_STATE; + } + + return CHIP_NO_ERROR; +} + void AmebaOTAImageProcessor::HandlePrepareDownload(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); @@ -371,15 +403,14 @@ void AmebaOTAImageProcessor::HandleApply(intptr_t context) return; } - OTARequestorInterface * requestor = chip::GetRequestorInstance(); - if (requestor != nullptr) - { - // TODO: Implement restarting into new image instead of changing the version - DeviceLayer::ConfigurationMgr().StoreSoftwareVersion(imageProcessor->mSoftwareVersion); - requestor->NotifyUpdateApplied(); - } + ChipLogProgress(SoftwareUpdate, "Rebooting in 2 seconds..."); - // Reboot + // Delay action time before calling HandleRestart + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(2 * 1000), HandleRestart, nullptr); +} + +void AmebaOTAImageProcessor::HandleRestart(chip::System::Layer * systemLayer, void * appState) +{ ota_platform_reset(); } diff --git a/src/platform/Ameba/AmebaOTAImageProcessor.h b/src/platform/Ameba/AmebaOTAImageProcessor.h index 38d22e4eb1b96a..3d05b203bcabb5 100644 --- a/src/platform/Ameba/AmebaOTAImageProcessor.h +++ b/src/platform/Ameba/AmebaOTAImageProcessor.h @@ -44,8 +44,8 @@ class AmebaOTAImageProcessor : public OTAImageProcessorInterface CHIP_ERROR Apply() override; CHIP_ERROR Abort() override; CHIP_ERROR ProcessBlock(ByteSpan & block) override; - bool IsFirstImageRun() override { return false; } - CHIP_ERROR ConfirmCurrentImage() override { return CHIP_NO_ERROR; } + bool IsFirstImageRun() override; + CHIP_ERROR ConfirmCurrentImage() override; void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; } private: @@ -55,6 +55,7 @@ class AmebaOTAImageProcessor : public OTAImageProcessorInterface static void HandleAbort(intptr_t context); static void HandleProcessBlock(intptr_t context); static void HandleApply(intptr_t context); + static void HandleRestart(chip::System::Layer * systemLayer, void * appState); CHIP_ERROR ProcessHeader(ByteSpan & block); diff --git a/src/platform/Ameba/ConfigurationManagerImpl.h b/src/platform/Ameba/ConfigurationManagerImpl.h index c7cd2e2513ec97..78555c6f07b126 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.h +++ b/src/platform/Ameba/ConfigurationManagerImpl.h @@ -39,6 +39,12 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp public: // This returns an instance of this class. static ConfigurationManagerImpl & GetDefaultInstance(); + CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override; + CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; + CHIP_ERROR GetBootReason(uint32_t & bootReason) override; + CHIP_ERROR StoreBootReason(uint32_t bootReason) override; private: // ===== Members that implement the ConfigurationManager public interface. @@ -49,12 +55,6 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp void InitiateFactoryReset(void) override; CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) override; CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) override; - CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override; - CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; - CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; - CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; - CHIP_ERROR GetBootReason(uint32_t & bootReason) override; - CHIP_ERROR StoreBootReason(uint32_t bootReason) override; CHIP_ERROR GetUniqueId(char * buf, size_t bufSize) override; // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>.