[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.
diff --git a/examples/all-clusters-app/nrfconnect/CMakeLists.txt b/examples/all-clusters-app/nrfconnect/CMakeLists.txt
index a70f92a..34a1448 100644
--- a/examples/all-clusters-app/nrfconnect/CMakeLists.txt
+++ b/examples/all-clusters-app/nrfconnect/CMakeLists.txt
@@ -68,7 +68,7 @@
     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 d1bc309..cdff02e 100644
--- a/examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt
+++ b/examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt
@@ -67,7 +67,7 @@
     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 3a0084e..0304f21 100644
--- a/examples/light-switch-app/nrfconnect/CMakeLists.txt
+++ b/examples/light-switch-app/nrfconnect/CMakeLists.txt
@@ -63,7 +63,7 @@
                ${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 5c813e6..59c9cf2 100644
--- a/examples/lighting-app/nrfconnect/CMakeLists.txt
+++ b/examples/lighting-app/nrfconnect/CMakeLists.txt
@@ -69,7 +69,7 @@
     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 3581d05..d60d050 100644
--- a/examples/lock-app/nrfconnect/CMakeLists.txt
+++ b/examples/lock-app/nrfconnect/CMakeLists.txt
@@ -65,7 +65,7 @@
     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 e586383..629bb8b 100644
--- a/examples/platform/nrfconnect/util/DFUOverSMP.cpp
+++ b/examples/platform/nrfconnect/util/DFUOverSMP.cpp
@@ -55,10 +55,10 @@
         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 4addd5c..733a8eb 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 <app/clusters/ota-requestor/BDXDownloader.h>
 #include <app/clusters/ota-requestor/DefaultOTARequestor.h>
 #include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
 #include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
 #include <app/server/Server.h>
 #include <platform/nrfconnect/OTAImageProcessorImpl.h>
+#endif
 
 using namespace chip;
 using namespace chip::DeviceLayer;
 
+#if CONFIG_CHIP_OTA_REQUESTOR
+
 namespace {
 
 DefaultOTARequestorStorage sOTARequestorStorage;
@@ -33,12 +39,6 @@
 chip::DefaultOTARequestor sOTARequestor;
 } // namespace
 
-FlashHandler & GetFlashHandler()
-{
-    static FlashHandler sFlashHandler;
-    return sFlashHandler;
-}
-
 // compile-time factory method
 OTAImageProcessorImpl & GetOTAImageProcessor()
 {
@@ -61,5 +61,12 @@
     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 d952ef9..55b5112 100644
--- a/examples/platform/nrfconnect/util/include/OTAUtil.h
+++ b/examples/platform/nrfconnect/util/include/OTAUtil.h
@@ -17,6 +17,9 @@
 
 #pragma once
 
+#include <platform/nrfconnect/ExternalFlashManager.h>
+
+#if CONFIG_CHIP_OTA_REQUESTOR
 #include <platform/nrfconnect/OTAImageProcessorImpl.h>
 
 namespace chip {
@@ -26,14 +29,6 @@
 } // 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.
  *
  * If the application uses QSPI external flash and enables API for controlling
@@ -50,3 +45,13 @@
  * 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 1598815..f7eb257 100644
--- a/examples/pump-app/nrfconnect/CMakeLists.txt
+++ b/examples/pump-app/nrfconnect/CMakeLists.txt
@@ -65,7 +65,7 @@
     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 2d115d5..adf087a 100644
--- a/examples/pump-controller-app/nrfconnect/CMakeLists.txt
+++ b/examples/pump-controller-app/nrfconnect/CMakeLists.txt
@@ -65,7 +65,7 @@
     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 0dc64ec..8a609d5 100644
--- a/examples/window-app/nrfconnect/CMakeLists.txt
+++ b/examples/window-app/nrfconnect/CMakeLists.txt
@@ -68,7 +68,7 @@
     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 7d32f81..a841a2b 100644
--- a/src/platform/nrfconnect/BUILD.gn
+++ b/src/platform/nrfconnect/BUILD.gn
@@ -41,6 +41,7 @@
     "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 0000000..2a1b3f7
--- /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 <zephyr/device.h>
+#include <zephyr/pm/device.h>
+
+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 96c2fe2..0daf01c 100644
--- a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp
+++ b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp
@@ -53,7 +53,7 @@
 {
     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 error = System::MapErrorZephyr(dfu_multi_image_done(false));
 
-    TriggerFlashAction(FlashHandler::Action::SLEEP);
+    TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
 
     return error;
 }
@@ -114,7 +114,7 @@
     // 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 @@
     return CHIP_NO_ERROR;
 }
 
-void OTAImageProcessorImpl::TriggerFlashAction(FlashHandler::Action action)
+void OTAImageProcessorImpl::TriggerFlashAction(ExternalFlashManager::Action action)
 {
     if (mFlashHandler)
     {
@@ -208,19 +208,5 @@
     }
 }
 
-// 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 09c94bb..6012e20 100644
--- a/src/platform/nrfconnect/OTAImageProcessorImpl.h
+++ b/src/platform/nrfconnect/OTAImageProcessorImpl.h
@@ -19,6 +19,7 @@
 #include <lib/core/OTAImageHeader.h>
 #include <lib/support/Span.h>
 #include <platform/OTAImageProcessor.h>
+#include <platform/nrfconnect/ExternalFlashManager.h>
 
 namespace chip {
 
@@ -26,27 +27,15 @@
 
 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 @@
     OTADownloader * mDownloader = nullptr;
     OTAImageHeaderParser mHeaderParser;
     uint8_t mBuffer[kBufferSize];
-    FlashHandler * mFlashHandler;
+    ExternalFlashManager * mFlashHandler;
 };
 
 } // namespace DeviceLayer