diff --git a/examples/lock-app/p6/src/AppTask.cpp b/examples/lock-app/p6/src/AppTask.cpp index 6f62d3c3d852c8..7c4dd3d10385a8 100644 --- a/examples/lock-app/p6/src/AppTask.cpp +++ b/examples/lock-app/p6/src/AppTask.cpp @@ -66,7 +66,7 @@ using chip::FabricIndex; using chip::GetRequestorInstance; using chip::NodeId; using chip::OTADownloader; -using chip::OTAImageProcessorImpl; +using chip::DeviceLayer::OTAImageProcessorImpl; using chip::System::Layer; using namespace ::chip; @@ -662,34 +662,14 @@ void vApplicationStackOverflowHook(TaskHandle_t pxTask, char * pcTaskName) #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR void AppTask::InitOTARequestor() { - CHIP_ERROR err = CHIP_NO_ERROR; SetRequestorInstance(&gRequestorCore); gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage()); gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); + ConfigurationMgr().StoreSoftwareVersion(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); - uint32_t savedSoftwareVersion; - err = ConfigurationMgr().GetSoftwareVersion(savedSoftwareVersion); - if (err != CHIP_NO_ERROR) - { - P6_LOG("Can't get saved software version"); - appError(err); - } - - if (savedSoftwareVersion != CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION) - { - ConfigurationMgr().StoreSoftwareVersion(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION); - - P6_LOG("Confirming update to version: %u", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION); - chip::OTARequestorInterface * requestor = chip::GetRequestorInstance(); - if (requestor != nullptr) - { - requestor->NotifyUpdateApplied(); - } - } - P6_LOG("Current Software Version: %u", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION); P6_LOG("Current Software Version String: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); } diff --git a/src/platform/P6/OTAImageProcessorImpl.cpp b/src/platform/P6/OTAImageProcessorImpl.cpp index b6e3bc175fab4a..5ca1fa089e98ea 100644 --- a/src/platform/P6/OTAImageProcessorImpl.cpp +++ b/src/platform/P6/OTAImageProcessorImpl.cpp @@ -16,11 +16,16 @@ * limitations under the License. */ +#include "OTAImageProcessorImpl.h" #include +#include +#include +#include -#include "OTAImageProcessorImpl.h" +using namespace ::chip::DeviceLayer::Internal; namespace chip { +namespace DeviceLayer { #ifdef P6_OTA CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() @@ -99,6 +104,39 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block) return CHIP_NO_ERROR; } +bool OTAImageProcessorImpl::IsFirstImageRun() +{ + OTARequestorInterface * requestor = GetRequestorInstance(); + ReturnErrorCodeIf(requestor == nullptr, false); + + uint32_t currentVersion; + ReturnErrorCodeIf(ConfigurationMgr().GetSoftwareVersion(currentVersion) != CHIP_NO_ERROR, false); + + ChipLogProgress(SoftwareUpdate, "%ld", currentVersion); + ChipLogProgress(SoftwareUpdate, "%ld", requestor->GetTargetVersion()); + + return ((requestor->GetCurrentUpdateState() == OTARequestorInterface::OTAUpdateStateEnum::kApplying) && + (requestor->GetTargetVersion() == currentVersion)); +} + +CHIP_ERROR OTAImageProcessorImpl::ConfirmCurrentImage() +{ + OTARequestorInterface * requestor = chip::GetRequestorInstance(); + if (requestor == nullptr) + { + return CHIP_ERROR_INTERNAL; + } + + uint32_t currentVersion; + ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetSoftwareVersion(currentVersion)); + if (currentVersion != requestor->GetTargetVersion()) + { + return CHIP_ERROR_INCORRECT_STATE; + } + + return CHIP_NO_ERROR; +} + void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); @@ -264,4 +302,5 @@ CHIP_ERROR OTAImageProcessorImpl::ReleaseBlock() } #endif // P6_OTA +} // namespace DeviceLayer } // namespace chip diff --git a/src/platform/P6/OTAImageProcessorImpl.h b/src/platform/P6/OTAImageProcessorImpl.h index 0f6a53fc7bd52a..4150565f8867ec 100644 --- a/src/platform/P6/OTAImageProcessorImpl.h +++ b/src/platform/P6/OTAImageProcessorImpl.h @@ -33,6 +33,7 @@ extern "C" { } namespace chip { +namespace DeviceLayer { class OTAImageProcessorImpl : public OTAImageProcessorInterface { @@ -43,8 +44,8 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface CHIP_ERROR Apply() override; CHIP_ERROR Abort() override; CHIP_ERROR ProcessBlock(ByteSpan & block) override; - bool IsFirstImageRun() override { return false; } - CHIP_ERROR ConfirmCurrentImage() override { return CHIP_NO_ERROR; } + bool IsFirstImageRun() override; + CHIP_ERROR ConfirmCurrentImage() override; void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; } @@ -80,5 +81,6 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface OTAImageHeaderParser mHeaderParser; }; +} // namespace DeviceLayer } // namespace chip #endif