From 2822850e64cf87ef071e45d0e63b892d86b47193 Mon Sep 17 00:00:00 2001 From: Sergei Lissianoi <54454955+selissia@users.noreply.github.com> Date: Fri, 8 Jul 2022 21:28:47 -0400 Subject: [PATCH] [EFR32] [sve-cherry-pick] Force key value store to save pending keys before reboot (#20506) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Test added march 8 (#15957) * Added new manual scripts * Added Auto generated File * [OTA] Fix OTARequestorDriverImpl inclusion (#15981) * Regen to fix CI failures (#15990) * [ota] Store Default OTA Providers in flash (#15970) * [ota] Store Default OTA Providers in flash Store Default OTA Providers in flash each time the attribute is modified and load it back on the application startup. * Restyled by clang-format * Fix build and reduce flash usage Co-authored-by: Restyled.io * Force EFR32 key value store to save pending keys before reboot * Remove merge artifacts * Restyled by clang-format Co-authored-by: kowsisoundhar12 <57476670+kowsisoundhar12@users.noreply.github.com> Co-authored-by: Carol Yang Co-authored-by: Boris Zbarsky Co-authored-by: Damian Królik <66667989+Damian-Nordic@users.noreply.github.com> Co-authored-by: Restyled.io --- src/platform/EFR32/KeyValueStoreManagerImpl.cpp | 5 +++++ src/platform/EFR32/KeyValueStoreManagerImpl.h | 3 +++ src/platform/EFR32/OTAImageProcessorImpl.cpp | 13 +++++++------ src/platform/EFR32/OTAImageProcessorImpl.h | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/platform/EFR32/KeyValueStoreManagerImpl.cpp b/src/platform/EFR32/KeyValueStoreManagerImpl.cpp index 3b577fce108eb4..31a5648f5f99b9 100644 --- a/src/platform/EFR32/KeyValueStoreManagerImpl.cpp +++ b/src/platform/EFR32/KeyValueStoreManagerImpl.cpp @@ -105,6 +105,11 @@ CHIP_ERROR KeyValueStoreManagerImpl::MapKvsKeyToNvm3(const char * key, uint32_t return err; } +void KeyValueStoreManagerImpl::ForceKeyMapSave() +{ + OnScheduledKeyMapSave(nullptr, nullptr); +} + void KeyValueStoreManagerImpl::OnScheduledKeyMapSave(System::Layer * systemLayer, void * appState) { EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_KvsStringKeyMap, diff --git a/src/platform/EFR32/KeyValueStoreManagerImpl.h b/src/platform/EFR32/KeyValueStoreManagerImpl.h index daf5ff8ce32c42..d8d288604af9da 100644 --- a/src/platform/EFR32/KeyValueStoreManagerImpl.h +++ b/src/platform/EFR32/KeyValueStoreManagerImpl.h @@ -46,8 +46,11 @@ class KeyValueStoreManagerImpl final : public KeyValueStoreManager static constexpr size_t kMaxEntries = KVS_MAX_ENTRIES; + static void ForceKeyMapSave(); + private: static void OnScheduledKeyMapSave(System::Layer * systemLayer, void * appState); + void ScheduleKeyMapSave(void); bool IsValidKvsNvm3Key(const uint32_t nvm3Key) const; CHIP_ERROR MapKvsKeyToNvm3(const char * key, uint32_t & nvm3Key, bool isSlotNeeded = false) const; diff --git a/src/platform/EFR32/OTAImageProcessorImpl.cpp b/src/platform/EFR32/OTAImageProcessorImpl.cpp index 6d11e219fa0534..c8c717e191837a 100644 --- a/src/platform/EFR32/OTAImageProcessorImpl.cpp +++ b/src/platform/EFR32/OTAImageProcessorImpl.cpp @@ -51,10 +51,7 @@ CHIP_ERROR OTAImageProcessorImpl::Finalize() } CHIP_ERROR OTAImageProcessorImpl::Apply() { - // Delay HandleApply() to give KVS time to store the data in StoreCurrentUpdateInfo() - ChipLogError(SoftwareUpdate, "Scheduling HandleApply"); - chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(EFR32_KVS_SAVE_DELAY_SECONDS + 1), HandleApply, - nullptr); + DeviceLayer::PlatformMgr().ScheduleWork(HandleApply, reinterpret_cast(this)); return CHIP_NO_ERROR; } @@ -180,17 +177,20 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context) ChipLogProgress(SoftwareUpdate, "OTA image downloaded successfully"); } -void OTAImageProcessorImpl::HandleApply(chip::System::Layer * systemLayer, void * context) +void OTAImageProcessorImpl::HandleApply(intptr_t context) { uint32_t err = SL_BOOTLOADER_OK; ChipLogProgress(SoftwareUpdate, "OTAImageProcessorImpl::HandleApply()"); - CORE_CRITICAL_SECTION(err = bootloader_verifyImage(mSlotId, NULL);) + // Force KVS to store pending keys such as data from StoreCurrentUpdateInfo() + chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().ForceKeyMapSave(); + CORE_CRITICAL_SECTION(err = bootloader_verifyImage(mSlotId, NULL);) if (err != SL_BOOTLOADER_OK) { ChipLogError(SoftwareUpdate, "ERROR: bootloader_verifyImage() error %ld", err); + return; } @@ -198,6 +198,7 @@ void OTAImageProcessorImpl::HandleApply(chip::System::Layer * systemLayer, void if (err != SL_BOOTLOADER_OK) { ChipLogError(SoftwareUpdate, "ERROR: bootloader_setImageToBootload() error %ld", err); + return; } diff --git a/src/platform/EFR32/OTAImageProcessorImpl.h b/src/platform/EFR32/OTAImageProcessorImpl.h index d2d8fc7a36c294..30709bd7f32f3a 100644 --- a/src/platform/EFR32/OTAImageProcessorImpl.h +++ b/src/platform/EFR32/OTAImageProcessorImpl.h @@ -46,7 +46,7 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface //////////// Actual handlers for the OTAImageProcessorInterface /////////////// static void HandlePrepareDownload(intptr_t context); static void HandleFinalize(intptr_t context); - static void HandleApply(chip::System::Layer * systemLayer, void *); + static void HandleApply(intptr_t context); static void HandleAbort(intptr_t context); static void HandleProcessBlock(intptr_t context); CHIP_ERROR ProcessHeader(ByteSpan & block);