From 1747436a1ff1f00f1e733101cc8f5622ccad2fe5 Mon Sep 17 00:00:00 2001 From: Abdul Rehman <44258486+abdulrehmanee12@users.noreply.github.com> Date: Fri, 10 Mar 2023 01:23:36 +0800 Subject: [PATCH] ESP32: Add kDnss initialized event to re-establish binding table (#25563) * ESP32: Add kDnss initialized event to re-establish binding table * Restyled by clang-format * ESP32: Add kDnss initialized event to re-establish binding table * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../esp32/main/include/DeviceCallbacks.h | 2 ++ examples/all-clusters-app/esp32/main/main.cpp | 2 -- .../esp32/main/include/DeviceCallbacks.h | 2 ++ .../esp32/main/main.cpp | 1 - .../esp32/main/include/DeviceCallbacks.h | 9 ++++++ examples/light-switch-app/esp32/main/main.cpp | 5 +-- .../esp32/common/CommonDeviceCallbacks.cpp | 29 ++++------------- .../esp32/common/CommonDeviceCallbacks.h | 1 + examples/platform/esp32/ota/OTAHelper.cpp | 31 +++++++++---------- 9 files changed, 37 insertions(+), 45 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h b/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h index db97219ed86c9e..8a9dec23c8b84b 100644 --- a/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h @@ -25,6 +25,7 @@ #pragma once +#include #include #include @@ -50,4 +51,5 @@ class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate public: void OnIPv4ConnectivityEstablished(void) override; void OnIPv4ConnectivityLost(void) override; + void OnDnssdInitialized(void) override { InitBindingHandlers(); } }; diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 2393488befa017..94bf4afcc2e5e2 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -114,8 +114,6 @@ static void InitServer(intptr_t context) // We only have network commissioning on endpoint 0. emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); - InitBindingHandlers(); - CHIP_ERROR err = GetAppTask().LockInit(); if (err != CHIP_NO_ERROR) { diff --git a/examples/all-clusters-minimal-app/esp32/main/include/DeviceCallbacks.h b/examples/all-clusters-minimal-app/esp32/main/include/DeviceCallbacks.h index db97219ed86c9e..8a9dec23c8b84b 100644 --- a/examples/all-clusters-minimal-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/all-clusters-minimal-app/esp32/main/include/DeviceCallbacks.h @@ -25,6 +25,7 @@ #pragma once +#include #include #include @@ -50,4 +51,5 @@ class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate public: void OnIPv4ConnectivityEstablished(void) override; void OnIPv4ConnectivityLost(void) override; + void OnDnssdInitialized(void) override { InitBindingHandlers(); } }; diff --git a/examples/all-clusters-minimal-app/esp32/main/main.cpp b/examples/all-clusters-minimal-app/esp32/main/main.cpp index ad4c3ee09d4374..989c221f6de5f9 100644 --- a/examples/all-clusters-minimal-app/esp32/main/main.cpp +++ b/examples/all-clusters-minimal-app/esp32/main/main.cpp @@ -115,7 +115,6 @@ static void InitServer(intptr_t context) // We only have network commissioning on endpoint 0. emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); - InitBindingHandlers(); #if CONFIG_DEVICE_TYPE_M5STACK SetupPretendDevices(); #endif diff --git a/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h b/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h index 0e226f574feeb5..93c53cbfd37e97 100644 --- a/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h @@ -25,6 +25,7 @@ #pragma once +#include #include #include @@ -39,3 +40,11 @@ class AppDeviceCallbacks : public CommonDeviceCallbacks void OnOffSwitchConfigurationAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t type, uint8_t * value, uint16_t size); }; + +class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate +{ +public: + void OnIPv4ConnectivityEstablished(void) override {} + void OnIPv4ConnectivityLost(void) override {} + void OnDnssdInitialized(void) override { InitBindingHandler(); } +}; diff --git a/examples/light-switch-app/esp32/main/main.cpp b/examples/light-switch-app/esp32/main/main.cpp index ed0225fcd62016..43c98425fe565c 100644 --- a/examples/light-switch-app/esp32/main/main.cpp +++ b/examples/light-switch-app/esp32/main/main.cpp @@ -55,6 +55,7 @@ using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; +using namespace ::chip::DeviceLayer; namespace { #if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER @@ -71,6 +72,7 @@ DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; static const char * TAG = "light-switch-app"; static AppDeviceCallbacks EchoCallbacks; +static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate; static void InitServer(intptr_t context) { @@ -78,8 +80,6 @@ static void InitServer(intptr_t context) PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE)); Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config - - InitBindingHandler(); } extern "C" void app_main() @@ -118,6 +118,7 @@ extern "C" void app_main() CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance(); CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks); + DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate); if (error != CHIP_NO_ERROR) { ESP_LOGE(TAG, "device.Init() failed: %" CHIP_ERROR_FORMAT, error.Format()); diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp index be0fbf1255cce6..4a112aef9ed7f0 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -46,9 +46,6 @@ using namespace chip::DeviceLayer; using namespace chip::System; DeviceCallbacksDelegate * appDelegate = nullptr; -#if CONFIG_ENABLE_OTA_REQUESTOR -static bool isOTAInitialized = false; -#endif void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { @@ -66,14 +63,15 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i ESP_LOGI(TAG, "CHIPoBLE disconnected"); break; - case DeviceEventType::kThreadConnectivityChange: + case DeviceEventType::kDnssdInitialized: #if CONFIG_ENABLE_OTA_REQUESTOR - if (event->ThreadConnectivityChange.Result == kConnectivity_Established && !isOTAInitialized) + OTAHelpers::Instance().InitOTARequestor(); +#endif + appDelegate = DeviceCallbacksDelegate::Instance().GetAppDelegate(); + if (appDelegate != nullptr) { - OTAHelpers::Instance().InitOTARequestor(); - isOTAInitialized = true; + appDelegate->OnDnssdInitialized(); } -#endif break; case DeviceEventType::kCommissioningComplete: { @@ -136,13 +134,6 @@ void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * appDelegate->OnIPv4ConnectivityEstablished(); } chip::app::DnssdServer::Instance().StartServer(); -#if CONFIG_ENABLE_OTA_REQUESTOR - if (!isOTAInitialized) - { - OTAHelpers::Instance().InitOTARequestor(); - isOTAInitialized = true; - } -#endif } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) { @@ -156,14 +147,6 @@ void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * { ESP_LOGI(TAG, "IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); - -#if CONFIG_ENABLE_OTA_REQUESTOR - if (!isOTAInitialized) - { - OTAHelpers::Instance().InitOTARequestor(); - isOTAInitialized = true; - } -#endif } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.h b/examples/platform/esp32/common/CommonDeviceCallbacks.h index a87014cbf00858..f76cdc405aa80e 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.h +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.h @@ -40,6 +40,7 @@ class DeviceCallbacksDelegate } virtual void OnIPv4ConnectivityEstablished() {} virtual void OnIPv4ConnectivityLost() {} + virtual void OnDnssdInitialized() {} DeviceCallbacksDelegate * mDelegate = nullptr; void SetAppDelegate(DeviceCallbacksDelegate * delegate) { mDelegate = delegate; } DeviceCallbacksDelegate * GetAppDelegate() { return mDelegate; } diff --git a/examples/platform/esp32/ota/OTAHelper.cpp b/examples/platform/esp32/ota/OTAHelper.cpp index b2be750db337c9..cd4cfe90dea17d 100644 --- a/examples/platform/esp32/ota/OTAHelper.cpp +++ b/examples/platform/esp32/ota/OTAHelper.cpp @@ -56,28 +56,25 @@ bool CustomOTARequestorDriver::CanConsent() return gRequestorCanConsent.ValueOr(DeviceLayer::ExtendedOTARequestorDriver::CanConsent()); } -static void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +void OTAHelpers::InitOTARequestor() { - SetRequestorInstance(&gRequestorCore); - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); - - if (gUserConsentState != chip::ota::UserConsentState::kUnknown) + if (!GetRequestorInstance()) { - gUserConsentProvider.SetUserConsentState(gUserConsentState); - gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider); + SetRequestorInstance(&gRequestorCore); + gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); + gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); + gImageProcessor.SetOTADownloader(&gDownloader); + gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.Init(&gRequestorCore, &gImageProcessor); + + if (gUserConsentState != chip::ota::UserConsentState::kUnknown) + { + gUserConsentProvider.SetUserConsentState(gUserConsentState); + gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider); + } } } -void OTAHelpers::InitOTARequestor() -{ - chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler, - nullptr); -} - namespace chip { namespace Shell { namespace {