Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nrfconnect] Moved FlashHandler implementation to separate file #23570

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/all-clusters-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/light-switch-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions examples/platform/nrfconnect/util/DFUOverSMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 14 additions & 7 deletions examples/platform/nrfconnect/util/OTAUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,12 +39,6 @@ chip::BDXDownloader sBDXDownloader;
chip::DefaultOTARequestor sOTARequestor;
} // namespace

FlashHandler & GetFlashHandler()
{
static FlashHandler sFlashHandler;
return sFlashHandler;
}

// compile-time factory method
OTAImageProcessorImpl & GetOTAImageProcessor()
{
Expand All @@ -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;
}
21 changes: 13 additions & 8 deletions examples/platform/nrfconnect/util/include/OTAUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#pragma once

#include <platform/nrfconnect/ExternalFlashManager.h>

#if CONFIG_CHIP_OTA_REQUESTOR
#include <platform/nrfconnect/OTAImageProcessorImpl.h>

namespace chip {
Expand All @@ -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.
*
Expand All @@ -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();
2 changes: 1 addition & 1 deletion examples/pump-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/pump-controller-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/window-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions src/platform/nrfconnect/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static_library("nrfconnect") {
"ConfigurationManagerImpl.h",
"ConnectivityManagerImpl.cpp",
"ConnectivityManagerImpl.h",
"ExternalFlashManager.h",
"InetPlatformConfig.h",
"KeyValueStoreManagerImpl.h",
"PlatformManagerImpl.h",
Expand Down
51 changes: 51 additions & 0 deletions src/platform/nrfconnect/ExternalFlashManager.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 4 additions & 18 deletions src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()); });
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -200,27 +200,13 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & aBlock)
return CHIP_NO_ERROR;
}

void OTAImageProcessorImpl::TriggerFlashAction(FlashHandler::Action action)
void OTAImageProcessorImpl::TriggerFlashAction(ExternalFlashManager::Action action)
{
if (mFlashHandler)
{
mFlashHandler->DoAction(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
19 changes: 4 additions & 15 deletions src/platform/nrfconnect/OTAImageProcessorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,23 @@
#include <lib/core/OTAImageHeader.h>
#include <lib/support/Span.h>
#include <platform/OTAImageProcessor.h>
#include <platform/nrfconnect/ExternalFlashManager.h>

namespace chip {

class OTADownloader;

namespace DeviceLayer {

class FlashHandler
{
public:
enum class Action : uint8_t
{
WAKE_UP,
SLEEP
};
virtual ~FlashHandler() {}
virtual void DoAction(Action aAction);
kkasperczyk-no marked this conversation as resolved.
Show resolved Hide resolved
};

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;
Expand All @@ -63,7 +52,7 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
OTADownloader * mDownloader = nullptr;
OTAImageHeaderParser mHeaderParser;
uint8_t mBuffer[kBufferSize];
FlashHandler * mFlashHandler;
ExternalFlashManager * mFlashHandler;
};

} // namespace DeviceLayer
Expand Down