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);