From b2408b206540f7807224479c727e1639cc08bac0 Mon Sep 17 00:00:00 2001 From: Kamil Kasperczyk <66371704+kkasperczyk-no@users.noreply.github.com> Date: Tue, 15 Nov 2022 19:38:02 +0100 Subject: [PATCH] [nrfconnect] Moved FlashHandler implementation to separate file (#23570) The FlashHandler implementation was coupled with Matter OTAImageProcessorImpl, what resulted with the fact that it was not possible to use build other DFU mechanisms, like DFU over BT SMP without Matter OTA enabled. Summary of changes: * Moved FlashHandler to separate file called ExternalFlashManager * Added ifdefs to OTAUtil that prevents from building Matter OTA dependencies while Matter OTA is disabled. --- .../nrfconnect/CMakeLists.txt | 2 +- .../nrfconnect/CMakeLists.txt | 2 +- .../nrfconnect/CMakeLists.txt | 2 +- .../lighting-app/nrfconnect/CMakeLists.txt | 2 +- examples/lock-app/nrfconnect/CMakeLists.txt | 2 +- .../platform/nrfconnect/util/DFUOverSMP.cpp | 4 +- examples/platform/nrfconnect/util/OTAUtil.cpp | 21 +++++--- .../nrfconnect/util/include/OTAUtil.h | 21 +++++--- examples/pump-app/nrfconnect/CMakeLists.txt | 2 +- .../nrfconnect/CMakeLists.txt | 2 +- examples/window-app/nrfconnect/CMakeLists.txt | 2 +- src/platform/nrfconnect/BUILD.gn | 1 + .../nrfconnect/ExternalFlashManager.h | 51 +++++++++++++++++++ .../nrfconnect/OTAImageProcessorImpl.cpp | 22 ++------ .../nrfconnect/OTAImageProcessorImpl.h | 19 ++----- 15 files changed, 97 insertions(+), 58 deletions(-) create mode 100644 src/platform/nrfconnect/ExternalFlashManager.h diff --git a/examples/all-clusters-app/nrfconnect/CMakeLists.txt b/examples/all-clusters-app/nrfconnect/CMakeLists.txt index a70f92af2440c7..34a14486434256 100644 --- a/examples/all-clusters-app/nrfconnect/CMakeLists.txt +++ b/examples/all-clusters-app/nrfconnect/CMakeLists.txt @@ -68,7 +68,7 @@ chip_configure_data_model(app ZAP_FILE ${ALL_CLUSTERS_COMMON_DIR}/all-clusters-app.zap ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt b/examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt index d1bc3092977a05..cdff02ee1dedb8 100644 --- a/examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt +++ b/examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt @@ -67,7 +67,7 @@ chip_configure_data_model(app ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../all-clusters-common/all-clusters-minimal-app.zap ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/light-switch-app/nrfconnect/CMakeLists.txt b/examples/light-switch-app/nrfconnect/CMakeLists.txt index 3a0084efa79c42..0304f21a9ed3ad 100644 --- a/examples/light-switch-app/nrfconnect/CMakeLists.txt +++ b/examples/light-switch-app/nrfconnect/CMakeLists.txt @@ -63,7 +63,7 @@ target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/LEDWidget.cpp) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/lighting-app/nrfconnect/CMakeLists.txt b/examples/lighting-app/nrfconnect/CMakeLists.txt index 5c813e69c8768e..59c9cf219a62ac 100644 --- a/examples/lighting-app/nrfconnect/CMakeLists.txt +++ b/examples/lighting-app/nrfconnect/CMakeLists.txt @@ -69,7 +69,7 @@ chip_configure_data_model(app GEN_DIR ${GEN_DIR}/lighting-app/zap-generated ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/lock-app/nrfconnect/CMakeLists.txt b/examples/lock-app/nrfconnect/CMakeLists.txt index 3581d05438e9c2..d60d050d31c91e 100644 --- a/examples/lock-app/nrfconnect/CMakeLists.txt +++ b/examples/lock-app/nrfconnect/CMakeLists.txt @@ -65,7 +65,7 @@ chip_configure_data_model(app ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../lock-common/lock-app.zap ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/platform/nrfconnect/util/DFUOverSMP.cpp b/examples/platform/nrfconnect/util/DFUOverSMP.cpp index e58638342948a2..629bb8bc894db5 100644 --- a/examples/platform/nrfconnect/util/DFUOverSMP.cpp +++ b/examples/platform/nrfconnect/util/DFUOverSMP.cpp @@ -55,10 +55,10 @@ void DFUOverSMP::Init(DFUOverSMPRestartAdvertisingHandler startAdvertisingCb) switch (opcode) { case MGMT_EVT_OP_CMD_RECV: - GetFlashHandler().DoAction(FlashHandler::Action::WAKE_UP); + GetFlashHandler().DoAction(ExternalFlashManager::Action::WAKE_UP); break; case MGMT_EVT_OP_CMD_DONE: - GetFlashHandler().DoAction(FlashHandler::Action::SLEEP); + GetFlashHandler().DoAction(ExternalFlashManager::Action::SLEEP); break; default: break; diff --git a/examples/platform/nrfconnect/util/OTAUtil.cpp b/examples/platform/nrfconnect/util/OTAUtil.cpp index 4addd5c30a7d30..733a8ebd741d1f 100644 --- a/examples/platform/nrfconnect/util/OTAUtil.cpp +++ b/examples/platform/nrfconnect/util/OTAUtil.cpp @@ -15,16 +15,22 @@ * limitations under the License. */ +#include "OTAUtil.h" + +#if CONFIG_CHIP_OTA_REQUESTOR #include #include #include #include #include #include +#endif using namespace chip; using namespace chip::DeviceLayer; +#if CONFIG_CHIP_OTA_REQUESTOR + namespace { DefaultOTARequestorStorage sOTARequestorStorage; @@ -33,12 +39,6 @@ chip::BDXDownloader sBDXDownloader; chip::DefaultOTARequestor sOTARequestor; } // namespace -FlashHandler & GetFlashHandler() -{ - static FlashHandler sFlashHandler; - return sFlashHandler; -} - // compile-time factory method OTAImageProcessorImpl & GetOTAImageProcessor() { @@ -61,5 +61,12 @@ void InitBasicOTARequestor() sOTARequestor.Init(Server::GetInstance(), sOTARequestorStorage, sOTARequestorDriver, sBDXDownloader); chip::SetRequestorInstance(&sOTARequestor); sOTARequestorDriver.Init(&sOTARequestor, &imageProcessor); - imageProcessor.TriggerFlashAction(FlashHandler::Action::SLEEP); + imageProcessor.TriggerFlashAction(ExternalFlashManager::Action::SLEEP); +} +#endif + +ExternalFlashManager & GetFlashHandler() +{ + static ExternalFlashManager sFlashHandler; + return sFlashHandler; } diff --git a/examples/platform/nrfconnect/util/include/OTAUtil.h b/examples/platform/nrfconnect/util/include/OTAUtil.h index d952ef9fea9617..55b51129aa81a0 100644 --- a/examples/platform/nrfconnect/util/include/OTAUtil.h +++ b/examples/platform/nrfconnect/util/include/OTAUtil.h @@ -17,6 +17,9 @@ #pragma once +#include + +#if CONFIG_CHIP_OTA_REQUESTOR #include namespace chip { @@ -25,14 +28,6 @@ class OTAImageProcessorImpl; } // namespace DeviceLayer } // namespace chip -/** - * Get FlashHandler static instance. - * - * Returned object can be used to control the QSPI external flash, - * which can be introduced into sleep mode and woken up on demand. - */ -chip::DeviceLayer::FlashHandler & GetFlashHandler(); - /** * Select recommended OTA image processor implementation. * @@ -50,3 +45,13 @@ chip::DeviceLayer::OTAImageProcessorImpl & GetOTAImageProcessor(); * an update so the confirmation must be done on the OTA provider side. */ void InitBasicOTARequestor(); + +#endif // CONFIG_CHIP_OTA_REQUESTOR + +/** + * Get ExternalFlashManager static instance. + * + * Returned object can be used to control the QSPI external flash, + * which can be introduced into sleep mode and woken up on demand. + */ +chip::DeviceLayer::ExternalFlashManager & GetFlashHandler(); diff --git a/examples/pump-app/nrfconnect/CMakeLists.txt b/examples/pump-app/nrfconnect/CMakeLists.txt index 15988152d17351..f7eb2579398efb 100644 --- a/examples/pump-app/nrfconnect/CMakeLists.txt +++ b/examples/pump-app/nrfconnect/CMakeLists.txt @@ -65,7 +65,7 @@ chip_configure_data_model(app ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../pump-common/pump-app.zap ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/pump-controller-app/nrfconnect/CMakeLists.txt b/examples/pump-controller-app/nrfconnect/CMakeLists.txt index 2d115d5c308e83..adf087afe2dabf 100644 --- a/examples/pump-controller-app/nrfconnect/CMakeLists.txt +++ b/examples/pump-controller-app/nrfconnect/CMakeLists.txt @@ -65,7 +65,7 @@ chip_configure_data_model(app ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../pump-controller-common/pump-controller-app.zap ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/examples/window-app/nrfconnect/CMakeLists.txt b/examples/window-app/nrfconnect/CMakeLists.txt index 0dc64ec5ccd3e9..8a609d5a888257 100644 --- a/examples/window-app/nrfconnect/CMakeLists.txt +++ b/examples/window-app/nrfconnect/CMakeLists.txt @@ -68,7 +68,7 @@ chip_configure_data_model(app ZAP_FILE ${WIN_APP_COMMON_DIR}/window-app.zap ) -if(CONFIG_CHIP_OTA_REQUESTOR) +if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp) endif() diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 7d32f8102b2558..a841a2bb374020 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -41,6 +41,7 @@ static_library("nrfconnect") { "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", + "ExternalFlashManager.h", "InetPlatformConfig.h", "KeyValueStoreManagerImpl.h", "PlatformManagerImpl.h", diff --git a/src/platform/nrfconnect/ExternalFlashManager.h b/src/platform/nrfconnect/ExternalFlashManager.h new file mode 100644 index 00000000000000..2a1b3f7f19ff63 --- /dev/null +++ b/src/platform/nrfconnect/ExternalFlashManager.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace chip { +namespace DeviceLayer { + +class ExternalFlashManager +{ +public: + enum class Action : uint8_t + { + WAKE_UP, + SLEEP + }; + + virtual ~ExternalFlashManager() {} + + virtual void DoAction(Action aAction) + { +#if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR + // utilize the QSPI driver sleep power mode + const auto * qspi_dev = DEVICE_DT_GET(DT_INST(0, nordic_qspi_nor)); + if (device_is_ready(qspi_dev)) + { + const auto requestedAction = Action::WAKE_UP == aAction ? PM_DEVICE_ACTION_RESUME : PM_DEVICE_ACTION_SUSPEND; + (void) pm_device_action_run(qspi_dev, requestedAction); // not much can be done in case of a failure + } +#endif + } +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp index 96c2fe22a55b36..0daf01cb892d15 100644 --- a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp +++ b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp @@ -53,7 +53,7 @@ CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() { VerifyOrReturnError(mDownloader != nullptr, CHIP_ERROR_INCORRECT_STATE); - TriggerFlashAction(FlashHandler::Action::WAKE_UP); + TriggerFlashAction(ExternalFlashManager::Action::WAKE_UP); return DeviceLayer::SystemLayer().ScheduleLambda([this] { mDownloader->OnPreparedForDownload(PrepareDownloadImpl()); }); } @@ -104,7 +104,7 @@ CHIP_ERROR OTAImageProcessorImpl::Abort() { CHIP_ERROR error = System::MapErrorZephyr(dfu_multi_image_done(false)); - TriggerFlashAction(FlashHandler::Action::SLEEP); + TriggerFlashAction(ExternalFlashManager::Action::SLEEP); return error; } @@ -114,7 +114,7 @@ CHIP_ERROR OTAImageProcessorImpl::Apply() // Schedule update of all images int err = dfu_target_schedule_update(-1); - TriggerFlashAction(FlashHandler::Action::SLEEP); + TriggerFlashAction(ExternalFlashManager::Action::SLEEP); #ifdef CONFIG_CHIP_OTA_REQUESTOR_REBOOT_ON_APPLY if (!err) @@ -200,7 +200,7 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & aBlock) return CHIP_NO_ERROR; } -void OTAImageProcessorImpl::TriggerFlashAction(FlashHandler::Action action) +void OTAImageProcessorImpl::TriggerFlashAction(ExternalFlashManager::Action action) { if (mFlashHandler) { @@ -208,19 +208,5 @@ void OTAImageProcessorImpl::TriggerFlashAction(FlashHandler::Action action) } } -// external flash power consumption optimization -void FlashHandler::DoAction(Action aAction) -{ -#if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR - // utilize the QSPI driver sleep power mode - const auto * qspi_dev = DEVICE_DT_GET(DT_INST(0, nordic_qspi_nor)); - if (device_is_ready(qspi_dev)) - { - const auto requestedAction = Action::WAKE_UP == aAction ? PM_DEVICE_ACTION_RESUME : PM_DEVICE_ACTION_SUSPEND; - (void) pm_device_action_run(qspi_dev, requestedAction); // not much can be done in case of a failure - } -#endif -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/nrfconnect/OTAImageProcessorImpl.h b/src/platform/nrfconnect/OTAImageProcessorImpl.h index 09c94bb1969456..6012e20c29ac81 100644 --- a/src/platform/nrfconnect/OTAImageProcessorImpl.h +++ b/src/platform/nrfconnect/OTAImageProcessorImpl.h @@ -19,6 +19,7 @@ #include #include #include +#include namespace chip { @@ -26,27 +27,15 @@ class OTADownloader; namespace DeviceLayer { -class FlashHandler -{ -public: - enum class Action : uint8_t - { - WAKE_UP, - SLEEP - }; - virtual ~FlashHandler() {} - virtual void DoAction(Action aAction); -}; - class OTAImageProcessorImpl : public OTAImageProcessorInterface { public: static constexpr size_t kBufferSize = CONFIG_CHIP_OTA_REQUESTOR_BUFFER_SIZE; - explicit OTAImageProcessorImpl(FlashHandler * flashHandler = nullptr) : mFlashHandler(flashHandler) {} + explicit OTAImageProcessorImpl(ExternalFlashManager * flashHandler = nullptr) : mFlashHandler(flashHandler) {} void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; }; - void TriggerFlashAction(FlashHandler::Action action); + void TriggerFlashAction(ExternalFlashManager::Action action); CHIP_ERROR PrepareDownload() override; CHIP_ERROR Finalize() override; @@ -63,7 +52,7 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface OTADownloader * mDownloader = nullptr; OTAImageHeaderParser mHeaderParser; uint8_t mBuffer[kBufferSize]; - FlashHandler * mFlashHandler; + ExternalFlashManager * mFlashHandler; }; } // namespace DeviceLayer