From b7859846fc630606ce858d95fcfa43978ecb7460 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Tue, 29 Mar 2022 14:59:38 +0530 Subject: [PATCH 1/5] [ESP32] Notify-update-applied command and fixed initialization order --- examples/all-clusters-app/esp32/main/main.cpp | 26 ++++++++-------- examples/lighting-app/esp32/main/main.cpp | 1 - .../DefaultOTARequestorDriver.cpp | 15 ++++++++-- src/platform/ESP32/OTAImageProcessorImpl.cpp | 30 +++++++++++++++++++ src/platform/ESP32/OTAImageProcessorImpl.h | 4 +-- 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 55e08b595d94fa..1410ac504ddb22 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -106,6 +106,18 @@ constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; } // namespace +static void InitOTARequestor(void) +{ +#if CONFIG_ENABLE_OTA_REQUESTOR + SetRequestorInstance(&gRequestorCore); + gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); + gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); + gImageProcessor.SetOTADownloader(&gDownloader); + gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.Init(&gRequestorCore, &gImageProcessor); +#endif +} + static void InitServer(intptr_t context) { // Init ZCL Data Model and CHIP App Server @@ -124,18 +136,8 @@ static void InitServer(intptr_t context) #if CONFIG_DEVICE_TYPE_M5STACK SetupPretendDevices(); #endif -} -static void InitOTARequestor(void) -{ -#if CONFIG_ENABLE_OTA_REQUESTOR - SetRequestorInstance(&gRequestorCore); - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); -#endif + InitOTARequestor(); } extern "C" void app_main() @@ -174,8 +176,6 @@ extern "C" void app_main() return; } - InitOTARequestor(); - ESP_LOGI(TAG, "------------------------Starting App Task---------------------------"); error = GetAppTask().StartAppTask(); if (error != CHIP_NO_ERROR) diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 7004f6c576d7a6..5a0b4c12c9db9c 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -111,7 +111,6 @@ static void InitServer(intptr_t context) chip::app::DnssdServer::Instance().StartServer(); } #endif - InitOTARequestor(); } extern "C" void app_main() diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp index 7e710f7c2bd5cb..4b56e57b506021 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp @@ -48,6 +48,7 @@ using namespace app::Clusters::OtaSoftwareUpdateRequestor::Structs; constexpr uint32_t kDelayQueryUponCommissioningSec = 30; // Delay before sending the initial image query after commissioning constexpr uint32_t kImmediateStartDelaySec = 1; // Delay before sending a query in response to UrgentUpdateAvailable constexpr System::Clock::Seconds32 kDefaultDelayedActionTime = System::Clock::Seconds32(120); +constexpr uint32_t kDelayNotifyUpdateApplied = 10; // Delay before sending notify update applied command DefaultOTARequestorDriver * ToDriver(void * context) { @@ -56,7 +57,16 @@ DefaultOTARequestorDriver * ToDriver(void * context) } // namespace -void DefaultOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImageProcessorInterface * processor) +void ScheduleNotifyUpdateApplied(chip::System::Layer * systemLayer, void * appState) +{ + OTARequestorInterface * requestor = static_cast(appState); + if (requestor != nullptr) + { + requestor->NotifyUpdateApplied(); + } +} + +void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImageProcessorInterface * processor) { mRequestor = requestor; mImageProcessor = processor; @@ -74,7 +84,8 @@ void DefaultOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImage return; } - mRequestor->NotifyUpdateApplied(); + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kDelayNotifyUpdateApplied), + ScheduleNotifyUpdateApplied, mRequestor); }); } else if ((mRequestor->GetCurrentUpdateState() != OTAUpdateStateEnum::kIdle)) diff --git a/src/platform/ESP32/OTAImageProcessorImpl.cpp b/src/platform/ESP32/OTAImageProcessorImpl.cpp index 6f8d5d07dda0d3..62fbe55410bef2 100644 --- a/src/platform/ESP32/OTAImageProcessorImpl.cpp +++ b/src/platform/ESP32/OTAImageProcessorImpl.cpp @@ -16,6 +16,7 @@ */ #include +#include #include #include "OTAImageProcessorImpl.h" @@ -38,6 +39,35 @@ void HandleRestart(Layer * systemLayer, void * appState) } } // namespace +bool OTAImageProcessorImpl::IsFirstImageRun() +{ + OTARequestorInterface * requestor = chip::GetRequestorInstance(); + if (requestor == nullptr) + { + return false; + } + + return requestor->GetCurrentUpdateState() == OTARequestorInterface::OTAUpdateStateEnum::kApplying; +} + +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; +} + CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() { DeviceLayer::PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast(this)); diff --git a/src/platform/ESP32/OTAImageProcessorImpl.h b/src/platform/ESP32/OTAImageProcessorImpl.h index 64ba8e8f51f557..5d0775c4392f13 100644 --- a/src/platform/ESP32/OTAImageProcessorImpl.h +++ b/src/platform/ESP32/OTAImageProcessorImpl.h @@ -35,8 +35,8 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface CHIP_ERROR Abort() override; CHIP_ERROR ProcessBlock(ByteSpan & block) override; void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; }; - bool IsFirstImageRun() override { return false; } - CHIP_ERROR ConfirmCurrentImage() override { return CHIP_NO_ERROR; } + bool IsFirstImageRun() override; + CHIP_ERROR ConfirmCurrentImage() override; private: static void HandlePrepareDownload(intptr_t context); From 4158c4546a109ca3c6f645d729d9c4f1d91225d5 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Wed, 30 Mar 2022 19:23:48 +0530 Subject: [PATCH 2/5] Addressed review comments --- examples/all-clusters-app/esp32/main/main.cpp | 4 +- examples/lighting-app/esp32/main/main.cpp | 4 +- .../ota-requestor-app/esp32/main/main.cpp | 4 +- .../DefaultOTARequestorDriver.cpp | 38 +++++++++---------- .../ota-requestor/DefaultOTARequestorDriver.h | 1 + 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 1410ac504ddb22..83311f6ac2d0b0 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -70,7 +70,8 @@ using namespace ::chip::DeviceLayer; // Used to indicate that an IP address has been added to the QRCode #define EXAMPLE_VENDOR_TAG_IP 1 -const char * TAG = "all-clusters-app"; +const char * TAG = "all-clusters-app"; +const uint32_t delayNotifySeconds = 10; static DeviceCallbacks EchoCallbacks; @@ -114,6 +115,7 @@ static void InitOTARequestor(void) gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.DelayNotifyUpdateAppliedAction(delayNotifySeconds); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); #endif } diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 5a0b4c12c9db9c..c0b3feeae07459 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -58,7 +58,8 @@ OTAImageProcessorImpl gImageProcessor; LEDWidget AppLED; -static const char * TAG = "light-app"; +static const char * TAG = "light-app"; +const uint32_t delayNotifySeconds = 10; static DeviceCallbacks EchoCallbacks; namespace { @@ -80,6 +81,7 @@ static void InitOTARequestor(void) gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.DelayNotifyUpdateAppliedAction(delayNotifySeconds); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); #endif } diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index 5f962d712d8db1..18e22851aa78f2 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -50,7 +50,8 @@ using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; namespace { -const char * TAG = "ota-requester-app"; +const char * TAG = "ota-requester-app"; +const uint32_t delayNotifySeconds = 10; static DeviceCallbacks EchoCallbacks; DefaultOTARequestor gRequestorCore; @@ -78,6 +79,7 @@ static void InitServer(intptr_t context) gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.DelayNotifyUpdateAppliedAction(delayNotifySeconds); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); } diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp index 4b56e57b506021..fa33d09a30d0f1 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp @@ -48,7 +48,8 @@ using namespace app::Clusters::OtaSoftwareUpdateRequestor::Structs; constexpr uint32_t kDelayQueryUponCommissioningSec = 30; // Delay before sending the initial image query after commissioning constexpr uint32_t kImmediateStartDelaySec = 1; // Delay before sending a query in response to UrgentUpdateAvailable constexpr System::Clock::Seconds32 kDefaultDelayedActionTime = System::Clock::Seconds32(120); -constexpr uint32_t kDelayNotifyUpdateApplied = 10; // Delay before sending notify update applied command +System::Clock::Seconds32 mDelayNotifyUpdateAppliedSeconds = + System::Clock::Seconds32(0); // Delay before sending notify update applied command DefaultOTARequestorDriver * ToDriver(void * context) { @@ -57,13 +58,9 @@ DefaultOTARequestorDriver * ToDriver(void * context) } // namespace -void ScheduleNotifyUpdateApplied(chip::System::Layer * systemLayer, void * appState) +void GenericOTARequestorDriver::DelayNotifyUpdateAppliedAction(uint32_t seconds) { - OTARequestorInterface * requestor = static_cast(appState); - if (requestor != nullptr) - { - requestor->NotifyUpdateApplied(); - } + mDelayNotifyUpdateAppliedSeconds = System::Clock::Seconds32(seconds); } void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImageProcessorInterface * processor) @@ -74,19 +71,20 @@ void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImage if (mImageProcessor->IsFirstImageRun()) { - SystemLayer().ScheduleLambda([this] { - CHIP_ERROR error = mImageProcessor->ConfirmCurrentImage(); - - if (error != CHIP_NO_ERROR) - { - ChipLogError(SoftwareUpdate, "Failed to confirm image: %" CHIP_ERROR_FORMAT, error.Format()); - mRequestor->Reset(); - return; - } - - chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kDelayNotifyUpdateApplied), - ScheduleNotifyUpdateApplied, mRequestor); - }); + ScheduleDelayedAction( + mDelayNotifyUpdateAppliedSeconds, + [](System::Layer *, void * context) { + CHIP_ERROR error = ToDriver(context)->mImageProcessor->ConfirmCurrentImage(); + if (error != CHIP_NO_ERROR) + { + ChipLogError(SoftwareUpdate, "Failed to confirm image: %" CHIP_ERROR_FORMAT, error.Format()); + ToDriver(context)->mRequestor->Reset(); + return; + } + + ToDriver(context)->mRequestor->NotifyUpdateApplied(); + }, + this); } else if ((mRequestor->GetCurrentUpdateState() != OTAUpdateStateEnum::kIdle)) { diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h index 24df0922156433..d6c6038ef4205d 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h @@ -80,6 +80,7 @@ class DefaultOTARequestorDriver : public OTARequestorDriver app::Clusters::OtaSoftwareUpdateRequestor::OTAAnnouncementReason announcementReason) override; void SendQueryImage() override; bool GetNextProviderLocation(ProviderLocationType & providerLocation, bool & listExhausted) override; + void DelayNotifyUpdateAppliedAction(uint32_t seconds); protected: void StartPeriodicQueryTimer(); From 71f77954eb56310332cb7c0ae732a04729a027cf Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Fri, 1 Apr 2022 16:10:39 +0530 Subject: [PATCH 3/5] Addressed review comments --- examples/all-clusters-app/esp32/main/main.cpp | 6 +++--- examples/lighting-app/esp32/main/main.cpp | 6 +++--- examples/ota-requestor-app/esp32/main/main.cpp | 6 +++--- .../ota-requestor/DefaultOTARequestorDriver.cpp | 10 +++++----- .../clusters/ota-requestor/DefaultOTARequestorDriver.h | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 83311f6ac2d0b0..4d5d923a5ca2b3 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -70,8 +70,8 @@ using namespace ::chip::DeviceLayer; // Used to indicate that an IP address has been added to the QRCode #define EXAMPLE_VENDOR_TAG_IP 1 -const char * TAG = "all-clusters-app"; -const uint32_t delayNotifySeconds = 10; +const char * TAG = "all-clusters-app"; +const uint32_t delayConfirmImageSec = 10; static DeviceCallbacks EchoCallbacks; @@ -115,7 +115,7 @@ static void InitOTARequestor(void) gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.DelayNotifyUpdateAppliedAction(delayNotifySeconds); + gRequestorUser.SetDelayConfirmCurrentImageSec(delayConfirmImageSec); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); #endif } diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index c0b3feeae07459..533772c1c5400e 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -58,8 +58,8 @@ OTAImageProcessorImpl gImageProcessor; LEDWidget AppLED; -static const char * TAG = "light-app"; -const uint32_t delayNotifySeconds = 10; +static const char * TAG = "light-app"; +const uint32_t delayConfirmImageSec = 10; static DeviceCallbacks EchoCallbacks; namespace { @@ -81,7 +81,7 @@ static void InitOTARequestor(void) gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.DelayNotifyUpdateAppliedAction(delayNotifySeconds); + gRequestorUser.SetDelayConfirmCurrentImageSec(delayConfirmImageSec); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); #endif } diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index 18e22851aa78f2..dd129a00022d31 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -50,8 +50,8 @@ using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; namespace { -const char * TAG = "ota-requester-app"; -const uint32_t delayNotifySeconds = 10; +const char * TAG = "ota-requester-app"; +const uint32_t delayConfirmImageSec = 10; static DeviceCallbacks EchoCallbacks; DefaultOTARequestor gRequestorCore; @@ -79,7 +79,7 @@ static void InitServer(intptr_t context) gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.DelayNotifyUpdateAppliedAction(delayNotifySeconds); + gRequestorUser.SetDelayConfirmCurrentImageSec(delayConfirmImageSec); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); } diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp index fa33d09a30d0f1..ded8bceef2fe44 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp @@ -48,8 +48,8 @@ using namespace app::Clusters::OtaSoftwareUpdateRequestor::Structs; constexpr uint32_t kDelayQueryUponCommissioningSec = 30; // Delay before sending the initial image query after commissioning constexpr uint32_t kImmediateStartDelaySec = 1; // Delay before sending a query in response to UrgentUpdateAvailable constexpr System::Clock::Seconds32 kDefaultDelayedActionTime = System::Clock::Seconds32(120); -System::Clock::Seconds32 mDelayNotifyUpdateAppliedSeconds = - System::Clock::Seconds32(0); // Delay before sending notify update applied command +System::Clock::Seconds32 mDelayConfirmCurrentImageSec = + System::Clock::Seconds32(0); // Delay before confirming current image (in seconds) DefaultOTARequestorDriver * ToDriver(void * context) { @@ -58,9 +58,9 @@ DefaultOTARequestorDriver * ToDriver(void * context) } // namespace -void GenericOTARequestorDriver::DelayNotifyUpdateAppliedAction(uint32_t seconds) +void GenericOTARequestorDriver::SetDelayConfirmCurrentImageSec(uint32_t seconds) { - mDelayNotifyUpdateAppliedSeconds = System::Clock::Seconds32(seconds); + mDelayConfirmCurrentImageSec = System::Clock::Seconds32(seconds); } void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImageProcessorInterface * processor) @@ -72,7 +72,7 @@ void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImage if (mImageProcessor->IsFirstImageRun()) { ScheduleDelayedAction( - mDelayNotifyUpdateAppliedSeconds, + mDelayConfirmCurrentImageSec, [](System::Layer *, void * context) { CHIP_ERROR error = ToDriver(context)->mImageProcessor->ConfirmCurrentImage(); if (error != CHIP_NO_ERROR) diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h index d6c6038ef4205d..21f2ef8ac0a036 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h @@ -80,7 +80,7 @@ class DefaultOTARequestorDriver : public OTARequestorDriver app::Clusters::OtaSoftwareUpdateRequestor::OTAAnnouncementReason announcementReason) override; void SendQueryImage() override; bool GetNextProviderLocation(ProviderLocationType & providerLocation, bool & listExhausted) override; - void DelayNotifyUpdateAppliedAction(uint32_t seconds); + void SetDelayConfirmCurrentImageSec(uint32_t seconds); protected: void StartPeriodicQueryTimer(); From 6aaaeeefa6bf6d3d54b90d66812a22e5653dcff0 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Wed, 20 Apr 2022 17:46:34 +0530 Subject: [PATCH 4/5] Resolved merge conflicts and addressed review comment --- .../esp32/main/CMakeLists.txt | 2 + .../esp32/main/DeviceCallbacks.cpp | 17 ++++++- examples/all-clusters-app/esp32/main/main.cpp | 31 +----------- .../lighting-app/esp32/main/CMakeLists.txt | 2 + .../esp32/main/DeviceCallbacks.cpp | 17 ++++++- examples/lighting-app/esp32/main/main.cpp | 29 +---------- .../esp32/main/CMakeLists.txt | 6 ++- .../esp32/main/DeviceCallbacks.cpp | 16 ++++++- .../ota-requestor-app/esp32/main/main.cpp | 23 +-------- examples/platform/esp32/ota/InitOTAR.cpp | 31 ++++++++++++ examples/platform/esp32/ota/InitOTAR.h | 48 +++++++++++++++++++ .../DefaultOTARequestorDriver.cpp | 35 +++++--------- .../ota-requestor/DefaultOTARequestorDriver.h | 1 - 13 files changed, 150 insertions(+), 108 deletions(-) create mode 100644 examples/platform/esp32/ota/InitOTAR.cpp create mode 100644 examples/platform/esp32/ota/InitOTAR.h diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index 0d6e9b0007c1b9..99600c5d19fb60 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -21,6 +21,7 @@ set(PRIV_INCLUDE_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-app" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common/include" "${CMAKE_CURRENT_LIST_DIR}/include" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip" ) set(SRC_DIRS_LIST "${CMAKE_CURRENT_LIST_DIR}" @@ -28,6 +29,7 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/util" diff --git a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp index 44f242971a8a6b..499be4c2f0c6a7 100644 --- a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -55,7 +56,8 @@ using namespace ::chip::System; using namespace ::chip::DeviceLayer; using namespace chip::app; -constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t mInitOTARequestorDelaySec = 3; void OnIdentifyTriggerEffect(Identify * identify) { @@ -200,13 +202,26 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %zu\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } +void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ + InitOTA::Instance().InitOTARequestor(); +} + void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { + static bool IsOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); wifiLED.Set(true); chip::app::DnssdServer::Instance().StartServer(); + + if (!IsOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(mInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + IsOTAInitialized = true; + } } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) { diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 4d5d923a5ca2b3..0638b838d77e9d 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -37,17 +37,12 @@ #include "shell_extension/launch.h" #include -#include -#include -#include -#include #include #include #include #include #include #include -#include #if CONFIG_HAVE_DISPLAY #include "DeviceWithDisplay.h" @@ -70,21 +65,12 @@ using namespace ::chip::DeviceLayer; // Used to indicate that an IP address has been added to the QRCode #define EXAMPLE_VENDOR_TAG_IP 1 -const char * TAG = "all-clusters-app"; -const uint32_t delayConfirmImageSec = 10; +const char * TAG = "all-clusters-app"; static DeviceCallbacks EchoCallbacks; namespace { -#if CONFIG_ENABLE_OTA_REQUESTOR -DefaultOTARequestor gRequestorCore; -DefaultOTARequestorStorage gRequestorStorage; -DefaultOTARequestorDriver gRequestorUser; -BDXDownloader gDownloader; -OTAImageProcessorImpl gImageProcessor; -#endif - app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); @@ -107,19 +93,6 @@ constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; } // namespace -static void InitOTARequestor(void) -{ -#if CONFIG_ENABLE_OTA_REQUESTOR - SetRequestorInstance(&gRequestorCore); - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.SetDelayConfirmCurrentImageSec(delayConfirmImageSec); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); -#endif -} - static void InitServer(intptr_t context) { // Init ZCL Data Model and CHIP App Server @@ -138,8 +111,6 @@ static void InitServer(intptr_t context) #if CONFIG_DEVICE_TYPE_M5STACK SetupPretendDevices(); #endif - - InitOTARequestor(); } extern "C" void app_main() diff --git a/examples/lighting-app/esp32/main/CMakeLists.txt b/examples/lighting-app/esp32/main/CMakeLists.txt index 3a81faaf0f0394..ff6715bd8e8b29 100644 --- a/examples/lighting-app/esp32/main/CMakeLists.txt +++ b/examples/lighting-app/esp32/main/CMakeLists.txt @@ -20,12 +20,14 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lighting-app" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lighting-app/lighting-common/color_format" "${CMAKE_CURRENT_LIST_DIR}/include" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip" SRC_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lighting-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lighting-app/lighting-common/color_format" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server" diff --git a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp index 6a77f7d80342c9..75e04bf1a44c54 100644 --- a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp @@ -37,9 +37,11 @@ #include #include #include +#include #include -static const char * TAG = "light-app-callbacks"; +static const char * TAG = "light-app-callbacks"; +constexpr uint32_t mInitOTARequestorDelaySec = 3; extern LEDWidget AppLED; @@ -149,12 +151,25 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %zu\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } +void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ + InitOTA::Instance().InitOTARequestor(); +} + void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { + static bool IsOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); chip::app::DnssdServer::Instance().StartServer(); + + if (!IsOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(mInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + IsOTAInitialized = true; + } } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) { diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 533772c1c5400e..838b77a1234d09 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -27,17 +27,12 @@ #include "nvs_flash.h" #include "shell_extension/launch.h" #include -#include -#include -#include -#include #include #include #include #include #include #include -#include #if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER #include @@ -48,18 +43,9 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; -#if CONFIG_ENABLE_OTA_REQUESTOR -DefaultOTARequestor gRequestorCore; -DefaultOTARequestorStorage gRequestorStorage; -DefaultOTARequestorDriver gRequestorUser; -BDXDownloader gDownloader; -OTAImageProcessorImpl gImageProcessor; -#endif - LEDWidget AppLED; -static const char * TAG = "light-app"; -const uint32_t delayConfirmImageSec = 10; +static const char * TAG = "light-app"; static DeviceCallbacks EchoCallbacks; namespace { @@ -73,19 +59,6 @@ ESP32FactoryDataProvider sFactoryDataProvider; #endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER } // namespace -static void InitOTARequestor(void) -{ -#if CONFIG_ENABLE_OTA_REQUESTOR - SetRequestorInstance(&gRequestorCore); - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.SetDelayConfirmCurrentImageSec(delayConfirmImageSec); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); -#endif -} - static void InitServer(intptr_t context) { // Print QR Code URL diff --git a/examples/ota-requestor-app/esp32/main/CMakeLists.txt b/examples/ota-requestor-app/esp32/main/CMakeLists.txt index 08f878c56b683f..e9623612bb6a58 100644 --- a/examples/ota-requestor-app/esp32/main/CMakeLists.txt +++ b/examples/ota-requestor-app/esp32/main/CMakeLists.txt @@ -20,7 +20,8 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/ota-requestor-app/" - SRC_DIRS + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip" + SRC_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/ota-requestor-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" @@ -49,7 +50,8 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor" - PRIV_REQUIRES chip QRCode bt console app_update) + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" + PRIV_REQUIRES chip QRCode bt console app_update) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 14) target_compile_options(${COMPONENT_LIB} PRIVATE "-DLWIP_IPV6_SCOPES=0" "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp index 043d452011e7a4..c7fdb12b64b26f 100644 --- a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp @@ -29,9 +29,11 @@ #include #include #include +#include #include -static const char * TAG = "echo-devicecallbacks"; +static const char * TAG = "echo-devicecallbacks"; +constexpr uint32_t mInitOTARequestorDelaySec = 3; using namespace ::chip; using namespace ::chip::Inet; @@ -78,12 +80,24 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } +void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ + InitOTA::Instance().InitOTARequestor(); +} + void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { + static bool IsOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); chip::app::DnssdServer::Instance().StartServer(); + if (!IsOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(mInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + IsOTAInitialized = true; + } } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) { diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index dd129a00022d31..02adf0680655ae 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -29,10 +29,6 @@ #include "freertos/task.h" #include "nvs_flash.h" #include -#include -#include -#include -#include #include #include @@ -41,8 +37,6 @@ #include -#include "OTAImageProcessorImpl.h" - using namespace ::chip; using namespace ::chip::System; using namespace ::chip::Credentials; @@ -50,16 +44,9 @@ using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; namespace { -const char * TAG = "ota-requester-app"; -const uint32_t delayConfirmImageSec = 10; +const char * TAG = "ota-requester-app"; static DeviceCallbacks EchoCallbacks; -DefaultOTARequestor gRequestorCore; -DefaultOTARequestorStorage gRequestorStorage; -DefaultOTARequestorDriver gRequestorUser; -BDXDownloader gDownloader; -OTAImageProcessorImpl gImageProcessor; - app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); @@ -73,14 +60,6 @@ static void InitServer(intptr_t context) SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); sWiFiNetworkCommissioningInstance.Init(); - - SetRequestorInstance(&gRequestorCore); - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.SetDelayConfirmCurrentImageSec(delayConfirmImageSec); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); } } // namespace diff --git a/examples/platform/esp32/ota/InitOTAR.cpp b/examples/platform/esp32/ota/InitOTAR.cpp new file mode 100644 index 00000000000000..abc759a6b87fa8 --- /dev/null +++ b/examples/platform/esp32/ota/InitOTAR.cpp @@ -0,0 +1,31 @@ +/* + * + * Copyright (c) 2021 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. + */ + +#include "InitOTAR.h" + +void InitOTA::InitOTARequestor() +{ + +#if CONFIG_ENABLE_OTA_REQUESTOR + SetRequestorInstance(&gRequestorCore); + gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); + gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); + gImageProcessor.SetOTADownloader(&gDownloader); + gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.Init(&gRequestorCore, &gImageProcessor); +#endif +} diff --git a/examples/platform/esp32/ota/InitOTAR.h b/examples/platform/esp32/ota/InitOTAR.h new file mode 100644 index 00000000000000..682d526a6c641f --- /dev/null +++ b/examples/platform/esp32/ota/InitOTAR.h @@ -0,0 +1,48 @@ +/* + * + * Copyright (c) 2021 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 +#include +#include +#include + +using namespace chip::DeviceLayer; +using namespace chip; + +namespace { + +#if CONFIG_ENABLE_OTA_REQUESTOR +DefaultOTARequestor gRequestorCore; +DefaultOTARequestorStorage gRequestorStorage; +DefaultOTARequestorDriver gRequestorUser; +BDXDownloader gDownloader; +OTAImageProcessorImpl gImageProcessor; +#endif +} // namespace + +class InitOTA +{ +public: + static InitOTA & Instance(void) + { + static InitOTA sInitOTA; + return sInitOTA; + } + void InitOTARequestor(void); +}; diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp index ded8bceef2fe44..7e710f7c2bd5cb 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp @@ -48,8 +48,6 @@ using namespace app::Clusters::OtaSoftwareUpdateRequestor::Structs; constexpr uint32_t kDelayQueryUponCommissioningSec = 30; // Delay before sending the initial image query after commissioning constexpr uint32_t kImmediateStartDelaySec = 1; // Delay before sending a query in response to UrgentUpdateAvailable constexpr System::Clock::Seconds32 kDefaultDelayedActionTime = System::Clock::Seconds32(120); -System::Clock::Seconds32 mDelayConfirmCurrentImageSec = - System::Clock::Seconds32(0); // Delay before confirming current image (in seconds) DefaultOTARequestorDriver * ToDriver(void * context) { @@ -58,12 +56,7 @@ DefaultOTARequestorDriver * ToDriver(void * context) } // namespace -void GenericOTARequestorDriver::SetDelayConfirmCurrentImageSec(uint32_t seconds) -{ - mDelayConfirmCurrentImageSec = System::Clock::Seconds32(seconds); -} - -void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImageProcessorInterface * processor) +void DefaultOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImageProcessorInterface * processor) { mRequestor = requestor; mImageProcessor = processor; @@ -71,20 +64,18 @@ void GenericOTARequestorDriver::Init(OTARequestorInterface * requestor, OTAImage if (mImageProcessor->IsFirstImageRun()) { - ScheduleDelayedAction( - mDelayConfirmCurrentImageSec, - [](System::Layer *, void * context) { - CHIP_ERROR error = ToDriver(context)->mImageProcessor->ConfirmCurrentImage(); - if (error != CHIP_NO_ERROR) - { - ChipLogError(SoftwareUpdate, "Failed to confirm image: %" CHIP_ERROR_FORMAT, error.Format()); - ToDriver(context)->mRequestor->Reset(); - return; - } - - ToDriver(context)->mRequestor->NotifyUpdateApplied(); - }, - this); + SystemLayer().ScheduleLambda([this] { + CHIP_ERROR error = mImageProcessor->ConfirmCurrentImage(); + + if (error != CHIP_NO_ERROR) + { + ChipLogError(SoftwareUpdate, "Failed to confirm image: %" CHIP_ERROR_FORMAT, error.Format()); + mRequestor->Reset(); + return; + } + + mRequestor->NotifyUpdateApplied(); + }); } else if ((mRequestor->GetCurrentUpdateState() != OTAUpdateStateEnum::kIdle)) { diff --git a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h index 21f2ef8ac0a036..24df0922156433 100644 --- a/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h +++ b/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.h @@ -80,7 +80,6 @@ class DefaultOTARequestorDriver : public OTARequestorDriver app::Clusters::OtaSoftwareUpdateRequestor::OTAAnnouncementReason announcementReason) override; void SendQueryImage() override; bool GetNextProviderLocation(ProviderLocationType & providerLocation, bool & listExhausted) override; - void SetDelayConfirmCurrentImageSec(uint32_t seconds); protected: void StartPeriodicQueryTimer(); From 3c15be157ca6bb82a3e31f27d4ea44e23daeb257 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Fri, 22 Apr 2022 14:08:59 +0530 Subject: [PATCH 5/5] Some minor changes --- .../esp32/main/CMakeLists.txt | 2 +- .../esp32/main/DeviceCallbacks.cpp | 20 +++++++++----- .../lighting-app/esp32/main/CMakeLists.txt | 2 +- .../esp32/main/DeviceCallbacks.cpp | 21 ++++++++++----- .../esp32/main/CMakeLists.txt | 2 +- .../esp32/main/DeviceCallbacks.cpp | 20 +++++++++----- .../esp32/ota/{InitOTAR.h => OTAHelper.cpp} | 26 ++++++++++--------- .../esp32/ota/{InitOTAR.cpp => OTAHelper.h} | 24 +++++++---------- 8 files changed, 67 insertions(+), 50 deletions(-) rename examples/platform/esp32/ota/{InitOTAR.h => OTAHelper.cpp} (69%) rename examples/platform/esp32/ota/{InitOTAR.cpp => OTAHelper.h} (53%) diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index 99600c5d19fb60..94c5bd15476d60 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -21,7 +21,7 @@ set(PRIV_INCLUDE_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-app" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common/include" "${CMAKE_CURRENT_LIST_DIR}/include" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32" ) set(SRC_DIRS_LIST "${CMAKE_CURRENT_LIST_DIR}" diff --git a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp index 499be4c2f0c6a7..2fe0c6f10edca6 100644 --- a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp @@ -44,9 +44,9 @@ #include #include #include -#include #include #include +#include static const char * TAG = "app-devicecallbacks"; @@ -57,7 +57,7 @@ using namespace ::chip::DeviceLayer; using namespace chip::app; constexpr uint32_t kIdentifyTimerDelayMS = 250; -constexpr uint32_t mInitOTARequestorDelaySec = 3; +constexpr uint32_t kInitOTARequestorDelaySec = 3; void OnIdentifyTriggerEffect(Identify * identify) { @@ -204,23 +204,23 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) { - InitOTA::Instance().InitOTARequestor(); + OTAHelpers::Instance().InitOTARequestor(); } void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { - static bool IsOTAInitialized = false; + static bool isOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); wifiLED.Set(true); chip::app::DnssdServer::Instance().StartServer(); - if (!IsOTAInitialized) + if (!isOTAInitialized) { - chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(mInitOTARequestorDelaySec), + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler, nullptr); - IsOTAInitialized = true; + isOTAInitialized = true; } } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) @@ -232,6 +232,12 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { ESP_LOGI(TAG, "IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); + if (!isOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + isOTAInitialized = true; + } } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/lighting-app/esp32/main/CMakeLists.txt b/examples/lighting-app/esp32/main/CMakeLists.txt index ff6715bd8e8b29..efbabfd9b5f678 100644 --- a/examples/lighting-app/esp32/main/CMakeLists.txt +++ b/examples/lighting-app/esp32/main/CMakeLists.txt @@ -20,7 +20,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lighting-app" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lighting-app/lighting-common/color_format" "${CMAKE_CURRENT_LIST_DIR}/include" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32" SRC_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" diff --git a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp index 75e04bf1a44c54..fdcb2e11dc7230 100644 --- a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp @@ -37,11 +37,11 @@ #include #include #include -#include #include +#include static const char * TAG = "light-app-callbacks"; -constexpr uint32_t mInitOTARequestorDelaySec = 3; +constexpr uint32_t kInitOTARequestorDelaySec = 3; extern LEDWidget AppLED; @@ -153,22 +153,22 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) { - InitOTA::Instance().InitOTARequestor(); + OTAHelpers::Instance().InitOTARequestor(); } void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { - static bool IsOTAInitialized = false; + static bool isOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); chip::app::DnssdServer::Instance().StartServer(); - if (!IsOTAInitialized) + if (!isOTAInitialized) { - chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(mInitOTARequestorDelaySec), + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler, nullptr); - IsOTAInitialized = true; + isOTAInitialized = true; } } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) @@ -179,6 +179,13 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { ESP_LOGI(TAG, "IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); + + if (!isOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + isOTAInitialized = true; + } } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/ota-requestor-app/esp32/main/CMakeLists.txt b/examples/ota-requestor-app/esp32/main/CMakeLists.txt index e9623612bb6a58..f89b111d538656 100644 --- a/examples/ota-requestor-app/esp32/main/CMakeLists.txt +++ b/examples/ota-requestor-app/esp32/main/CMakeLists.txt @@ -20,7 +20,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/ota-requestor-app/" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32" SRC_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/ota-requestor-app/zap-generated" diff --git a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp index c7fdb12b64b26f..6807e16cde0bb9 100644 --- a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp @@ -29,11 +29,11 @@ #include #include #include -#include #include +#include static const char * TAG = "echo-devicecallbacks"; -constexpr uint32_t mInitOTARequestorDelaySec = 3; +constexpr uint32_t kInitOTARequestorDelaySec = 3; using namespace ::chip; using namespace ::chip::Inet; @@ -82,21 +82,21 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) { - InitOTA::Instance().InitOTARequestor(); + OTAHelpers::Instance().InitOTARequestor(); } void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { - static bool IsOTAInitialized = false; + static bool isOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); chip::app::DnssdServer::Instance().StartServer(); - if (!IsOTAInitialized) + if (!isOTAInitialized) { - chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(mInitOTARequestorDelaySec), + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler, nullptr); - IsOTAInitialized = true; + isOTAInitialized = true; } } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) @@ -107,6 +107,12 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { ESP_LOGI(TAG, "IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); + if (!isOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + isOTAInitialized = true; + } } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/platform/esp32/ota/InitOTAR.h b/examples/platform/esp32/ota/OTAHelper.cpp similarity index 69% rename from examples/platform/esp32/ota/InitOTAR.h rename to examples/platform/esp32/ota/OTAHelper.cpp index 682d526a6c641f..d561a09ad815e6 100644 --- a/examples/platform/esp32/ota/InitOTAR.h +++ b/examples/platform/esp32/ota/OTAHelper.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * 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. @@ -14,7 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once + +#include "OTAHelper.h" #include #include @@ -35,14 +36,15 @@ BDXDownloader gDownloader; OTAImageProcessorImpl gImageProcessor; #endif } // namespace - -class InitOTA +void OTAHelpers::InitOTARequestor() { -public: - static InitOTA & Instance(void) - { - static InitOTA sInitOTA; - return sInitOTA; - } - void InitOTARequestor(void); -}; + +#if CONFIG_ENABLE_OTA_REQUESTOR + SetRequestorInstance(&gRequestorCore); + gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); + gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); + gImageProcessor.SetOTADownloader(&gDownloader); + gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.Init(&gRequestorCore, &gImageProcessor); +#endif +} diff --git a/examples/platform/esp32/ota/InitOTAR.cpp b/examples/platform/esp32/ota/OTAHelper.h similarity index 53% rename from examples/platform/esp32/ota/InitOTAR.cpp rename to examples/platform/esp32/ota/OTAHelper.h index abc759a6b87fa8..521c5b39532b6d 100644 --- a/examples/platform/esp32/ota/InitOTAR.cpp +++ b/examples/platform/esp32/ota/OTAHelper.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * 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. @@ -15,17 +15,13 @@ * limitations under the License. */ -#include "InitOTAR.h" - -void InitOTA::InitOTARequestor() +class OTAHelpers { - -#if CONFIG_ENABLE_OTA_REQUESTOR - SetRequestorInstance(&gRequestorCore); - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); -#endif -} +public: + static OTAHelpers & Instance(void) + { + static OTAHelpers sInitOTA; + return sInitOTA; + } + void InitOTARequestor(void); +};