Skip to content

Commit

Permalink
ESP32: Add kDnss initialized event to re-establish binding table (#25563
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jan 11, 2024
1 parent 258d72e commit 1397300
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#pragma once

#include <binding-handler.h>
#include <common/CHIPDeviceManager.h>
#include <common/CommonDeviceCallbacks.h>

Expand All @@ -50,4 +51,5 @@ class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate
public:
void OnIPv4ConnectivityEstablished(void) override;
void OnIPv4ConnectivityLost(void) override;
void OnDnssdInitialized(void) override { InitBindingHandlers(); }
};
2 changes: 0 additions & 2 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#pragma once

#include <binding-handler.h>
#include <common/CHIPDeviceManager.h>
#include <common/CommonDeviceCallbacks.h>

Expand All @@ -50,4 +51,5 @@ class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate
public:
void OnIPv4ConnectivityEstablished(void) override;
void OnIPv4ConnectivityLost(void) override;
void OnDnssdInitialized(void) override { InitBindingHandlers(); }
};
1 change: 0 additions & 1 deletion examples/all-clusters-minimal-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#pragma once

#include <BindingHandler.h>
#include <common/CHIPDeviceManager.h>
#include <common/CommonDeviceCallbacks.h>

Expand All @@ -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(); }
};
5 changes: 3 additions & 2 deletions examples/light-switch-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,15 +72,14 @@ DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
static const char * TAG = "light-switch-app";

static AppDeviceCallbacks EchoCallbacks;
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;

static void InitServer(intptr_t context)
{
// Print QR Code URL
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()
Expand Down Expand Up @@ -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());
Expand Down
29 changes: 6 additions & 23 deletions examples/platform/esp32/common/CommonDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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: {
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions examples/platform/esp32/common/CommonDeviceCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
31 changes: 14 additions & 17 deletions examples/platform/esp32/ota/OTAHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1397300

Please sign in to comment.