Skip to content

Commit d4c8cff

Browse files
kkasperczyk-noadbridge
authored andcommitted
[nrfconnect] Moved FlashHandler implementation to separate file (project-chip#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.
1 parent dc90f83 commit d4c8cff

File tree

15 files changed

+97
-58
lines changed

15 files changed

+97
-58
lines changed

examples/all-clusters-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ chip_configure_data_model(app
6868
ZAP_FILE ${ALL_CLUSTERS_COMMON_DIR}/all-clusters-app.zap
6969
)
7070

71-
if(CONFIG_CHIP_OTA_REQUESTOR)
71+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
7272
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7373
endif()
7474

examples/all-clusters-minimal-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ chip_configure_data_model(app
6767
ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../all-clusters-common/all-clusters-minimal-app.zap
6868
)
6969

70-
if(CONFIG_CHIP_OTA_REQUESTOR)
70+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
7171
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7272
endif()
7373

examples/light-switch-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ target_sources(app PRIVATE
6363
${NRFCONNECT_COMMON}/util/LEDWidget.cpp)
6464

6565

66-
if(CONFIG_CHIP_OTA_REQUESTOR)
66+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
6767
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
6868
endif()
6969

examples/lighting-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ chip_configure_data_model(app
6969
GEN_DIR ${GEN_DIR}/lighting-app/zap-generated
7070
)
7171

72-
if(CONFIG_CHIP_OTA_REQUESTOR)
72+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
7373
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7474
endif()
7575

examples/lock-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ chip_configure_data_model(app
6565
ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../lock-common/lock-app.zap
6666
)
6767

68-
if(CONFIG_CHIP_OTA_REQUESTOR)
68+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
6969
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7070
endif()
7171

examples/platform/nrfconnect/util/DFUOverSMP.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ void DFUOverSMP::Init(DFUOverSMPRestartAdvertisingHandler startAdvertisingCb)
5555
switch (opcode)
5656
{
5757
case MGMT_EVT_OP_CMD_RECV:
58-
GetFlashHandler().DoAction(FlashHandler::Action::WAKE_UP);
58+
GetFlashHandler().DoAction(ExternalFlashManager::Action::WAKE_UP);
5959
break;
6060
case MGMT_EVT_OP_CMD_DONE:
61-
GetFlashHandler().DoAction(FlashHandler::Action::SLEEP);
61+
GetFlashHandler().DoAction(ExternalFlashManager::Action::SLEEP);
6262
break;
6363
default:
6464
break;

examples/platform/nrfconnect/util/OTAUtil.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include "OTAUtil.h"
19+
20+
#if CONFIG_CHIP_OTA_REQUESTOR
1821
#include <app/clusters/ota-requestor/BDXDownloader.h>
1922
#include <app/clusters/ota-requestor/DefaultOTARequestor.h>
2023
#include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
2124
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
2225
#include <app/server/Server.h>
2326
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
27+
#endif
2428

2529
using namespace chip;
2630
using namespace chip::DeviceLayer;
2731

32+
#if CONFIG_CHIP_OTA_REQUESTOR
33+
2834
namespace {
2935

3036
DefaultOTARequestorStorage sOTARequestorStorage;
@@ -33,12 +39,6 @@ chip::BDXDownloader sBDXDownloader;
3339
chip::DefaultOTARequestor sOTARequestor;
3440
} // namespace
3541

36-
FlashHandler & GetFlashHandler()
37-
{
38-
static FlashHandler sFlashHandler;
39-
return sFlashHandler;
40-
}
41-
4242
// compile-time factory method
4343
OTAImageProcessorImpl & GetOTAImageProcessor()
4444
{
@@ -61,5 +61,12 @@ void InitBasicOTARequestor()
6161
sOTARequestor.Init(Server::GetInstance(), sOTARequestorStorage, sOTARequestorDriver, sBDXDownloader);
6262
chip::SetRequestorInstance(&sOTARequestor);
6363
sOTARequestorDriver.Init(&sOTARequestor, &imageProcessor);
64-
imageProcessor.TriggerFlashAction(FlashHandler::Action::SLEEP);
64+
imageProcessor.TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
65+
}
66+
#endif
67+
68+
ExternalFlashManager & GetFlashHandler()
69+
{
70+
static ExternalFlashManager sFlashHandler;
71+
return sFlashHandler;
6572
}

examples/platform/nrfconnect/util/include/OTAUtil.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#pragma once
1919

20+
#include <platform/nrfconnect/ExternalFlashManager.h>
21+
22+
#if CONFIG_CHIP_OTA_REQUESTOR
2023
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
2124

2225
namespace chip {
@@ -25,14 +28,6 @@ class OTAImageProcessorImpl;
2528
} // namespace DeviceLayer
2629
} // namespace chip
2730

28-
/**
29-
* Get FlashHandler static instance.
30-
*
31-
* Returned object can be used to control the QSPI external flash,
32-
* which can be introduced into sleep mode and woken up on demand.
33-
*/
34-
chip::DeviceLayer::FlashHandler & GetFlashHandler();
35-
3631
/**
3732
* Select recommended OTA image processor implementation.
3833
*
@@ -50,3 +45,13 @@ chip::DeviceLayer::OTAImageProcessorImpl & GetOTAImageProcessor();
5045
* an update so the confirmation must be done on the OTA provider side.
5146
*/
5247
void InitBasicOTARequestor();
48+
49+
#endif // CONFIG_CHIP_OTA_REQUESTOR
50+
51+
/**
52+
* Get ExternalFlashManager static instance.
53+
*
54+
* Returned object can be used to control the QSPI external flash,
55+
* which can be introduced into sleep mode and woken up on demand.
56+
*/
57+
chip::DeviceLayer::ExternalFlashManager & GetFlashHandler();

examples/pump-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ chip_configure_data_model(app
6565
ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../pump-common/pump-app.zap
6666
)
6767

68-
if(CONFIG_CHIP_OTA_REQUESTOR)
68+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
6969
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7070
endif()
7171

examples/pump-controller-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ chip_configure_data_model(app
6565
ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../pump-controller-common/pump-controller-app.zap
6666
)
6767

68-
if(CONFIG_CHIP_OTA_REQUESTOR)
68+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
6969
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7070
endif()
7171

examples/window-app/nrfconnect/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ chip_configure_data_model(app
6868
ZAP_FILE ${WIN_APP_COMMON_DIR}/window-app.zap
6969
)
7070

71-
if(CONFIG_CHIP_OTA_REQUESTOR)
71+
if(CONFIG_CHIP_OTA_REQUESTOR OR CONFIG_MCUMGR_SMP_BT)
7272
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/OTAUtil.cpp)
7373
endif()
7474

src/platform/nrfconnect/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static_library("nrfconnect") {
4141
"ConfigurationManagerImpl.h",
4242
"ConnectivityManagerImpl.cpp",
4343
"ConnectivityManagerImpl.h",
44+
"ExternalFlashManager.h",
4445
"InetPlatformConfig.h",
4546
"KeyValueStoreManagerImpl.h",
4647
"PlatformManagerImpl.h",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2022 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <zephyr/device.h>
20+
#include <zephyr/pm/device.h>
21+
22+
namespace chip {
23+
namespace DeviceLayer {
24+
25+
class ExternalFlashManager
26+
{
27+
public:
28+
enum class Action : uint8_t
29+
{
30+
WAKE_UP,
31+
SLEEP
32+
};
33+
34+
virtual ~ExternalFlashManager() {}
35+
36+
virtual void DoAction(Action aAction)
37+
{
38+
#if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR
39+
// utilize the QSPI driver sleep power mode
40+
const auto * qspi_dev = DEVICE_DT_GET(DT_INST(0, nordic_qspi_nor));
41+
if (device_is_ready(qspi_dev))
42+
{
43+
const auto requestedAction = Action::WAKE_UP == aAction ? PM_DEVICE_ACTION_RESUME : PM_DEVICE_ACTION_SUSPEND;
44+
(void) pm_device_action_run(qspi_dev, requestedAction); // not much can be done in case of a failure
45+
}
46+
#endif
47+
}
48+
};
49+
50+
} // namespace DeviceLayer
51+
} // namespace chip

src/platform/nrfconnect/OTAImageProcessorImpl.cpp

+4-18
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CHIP_ERROR OTAImageProcessorImpl::PrepareDownload()
5353
{
5454
VerifyOrReturnError(mDownloader != nullptr, CHIP_ERROR_INCORRECT_STATE);
5555

56-
TriggerFlashAction(FlashHandler::Action::WAKE_UP);
56+
TriggerFlashAction(ExternalFlashManager::Action::WAKE_UP);
5757

5858
return DeviceLayer::SystemLayer().ScheduleLambda([this] { mDownloader->OnPreparedForDownload(PrepareDownloadImpl()); });
5959
}
@@ -104,7 +104,7 @@ CHIP_ERROR OTAImageProcessorImpl::Abort()
104104
{
105105
CHIP_ERROR error = System::MapErrorZephyr(dfu_multi_image_done(false));
106106

107-
TriggerFlashAction(FlashHandler::Action::SLEEP);
107+
TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
108108

109109
return error;
110110
}
@@ -114,7 +114,7 @@ CHIP_ERROR OTAImageProcessorImpl::Apply()
114114
// Schedule update of all images
115115
int err = dfu_target_schedule_update(-1);
116116

117-
TriggerFlashAction(FlashHandler::Action::SLEEP);
117+
TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
118118

119119
#ifdef CONFIG_CHIP_OTA_REQUESTOR_REBOOT_ON_APPLY
120120
if (!err)
@@ -200,27 +200,13 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & aBlock)
200200
return CHIP_NO_ERROR;
201201
}
202202

203-
void OTAImageProcessorImpl::TriggerFlashAction(FlashHandler::Action action)
203+
void OTAImageProcessorImpl::TriggerFlashAction(ExternalFlashManager::Action action)
204204
{
205205
if (mFlashHandler)
206206
{
207207
mFlashHandler->DoAction(action);
208208
}
209209
}
210210

211-
// external flash power consumption optimization
212-
void FlashHandler::DoAction(Action aAction)
213-
{
214-
#if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR
215-
// utilize the QSPI driver sleep power mode
216-
const auto * qspi_dev = DEVICE_DT_GET(DT_INST(0, nordic_qspi_nor));
217-
if (device_is_ready(qspi_dev))
218-
{
219-
const auto requestedAction = Action::WAKE_UP == aAction ? PM_DEVICE_ACTION_RESUME : PM_DEVICE_ACTION_SUSPEND;
220-
(void) pm_device_action_run(qspi_dev, requestedAction); // not much can be done in case of a failure
221-
}
222-
#endif
223-
}
224-
225211
} // namespace DeviceLayer
226212
} // namespace chip

src/platform/nrfconnect/OTAImageProcessorImpl.h

+4-15
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,23 @@
1919
#include <lib/core/OTAImageHeader.h>
2020
#include <lib/support/Span.h>
2121
#include <platform/OTAImageProcessor.h>
22+
#include <platform/nrfconnect/ExternalFlashManager.h>
2223

2324
namespace chip {
2425

2526
class OTADownloader;
2627

2728
namespace DeviceLayer {
2829

29-
class FlashHandler
30-
{
31-
public:
32-
enum class Action : uint8_t
33-
{
34-
WAKE_UP,
35-
SLEEP
36-
};
37-
virtual ~FlashHandler() {}
38-
virtual void DoAction(Action aAction);
39-
};
40-
4130
class OTAImageProcessorImpl : public OTAImageProcessorInterface
4231
{
4332
public:
4433
static constexpr size_t kBufferSize = CONFIG_CHIP_OTA_REQUESTOR_BUFFER_SIZE;
4534

46-
explicit OTAImageProcessorImpl(FlashHandler * flashHandler = nullptr) : mFlashHandler(flashHandler) {}
35+
explicit OTAImageProcessorImpl(ExternalFlashManager * flashHandler = nullptr) : mFlashHandler(flashHandler) {}
4736

4837
void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; };
49-
void TriggerFlashAction(FlashHandler::Action action);
38+
void TriggerFlashAction(ExternalFlashManager::Action action);
5039

5140
CHIP_ERROR PrepareDownload() override;
5241
CHIP_ERROR Finalize() override;
@@ -63,7 +52,7 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
6352
OTADownloader * mDownloader = nullptr;
6453
OTAImageHeaderParser mHeaderParser;
6554
uint8_t mBuffer[kBufferSize];
66-
FlashHandler * mFlashHandler;
55+
ExternalFlashManager * mFlashHandler;
6756
};
6857

6958
} // namespace DeviceLayer

0 commit comments

Comments
 (0)