Skip to content

Commit 3621660

Browse files
abdulrehmanee12restyled-commits
authored andcommitted
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 <commits@restyled.io>
1 parent ea85357 commit 3621660

File tree

9 files changed

+37
-45
lines changed

9 files changed

+37
-45
lines changed

examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include <binding-handler.h>
2829
#include <common/CHIPDeviceManager.h>
2930
#include <common/CommonDeviceCallbacks.h>
3031

@@ -50,4 +51,5 @@ class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate
5051
public:
5152
void OnIPv4ConnectivityEstablished(void) override;
5253
void OnIPv4ConnectivityLost(void) override;
54+
void OnDnssdInitialized(void) override { InitBindingHandlers(); }
5355
};

examples/all-clusters-app/esp32/main/main.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ static void InitServer(intptr_t context)
114114
// We only have network commissioning on endpoint 0.
115115
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
116116

117-
InitBindingHandlers();
118-
119117
CHIP_ERROR err = GetAppTask().LockInit();
120118
if (err != CHIP_NO_ERROR)
121119
{

examples/all-clusters-minimal-app/esp32/main/include/DeviceCallbacks.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include <binding-handler.h>
2829
#include <common/CHIPDeviceManager.h>
2930
#include <common/CommonDeviceCallbacks.h>
3031

@@ -50,4 +51,5 @@ class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate
5051
public:
5152
void OnIPv4ConnectivityEstablished(void) override;
5253
void OnIPv4ConnectivityLost(void) override;
54+
void OnDnssdInitialized(void) override { InitBindingHandlers(); }
5355
};

examples/all-clusters-minimal-app/esp32/main/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ static void InitServer(intptr_t context)
115115
// We only have network commissioning on endpoint 0.
116116
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
117117

118-
InitBindingHandlers();
119118
#if CONFIG_DEVICE_TYPE_M5STACK
120119
SetupPretendDevices();
121120
#endif

examples/light-switch-app/esp32/main/include/DeviceCallbacks.h

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include <BindingHandler.h>
2829
#include <common/CHIPDeviceManager.h>
2930
#include <common/CommonDeviceCallbacks.h>
3031

@@ -39,3 +40,11 @@ class AppDeviceCallbacks : public CommonDeviceCallbacks
3940
void OnOffSwitchConfigurationAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t type,
4041
uint8_t * value, uint16_t size);
4142
};
43+
44+
class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate
45+
{
46+
public:
47+
void OnIPv4ConnectivityEstablished(void) override {}
48+
void OnIPv4ConnectivityLost(void) override {}
49+
void OnDnssdInitialized(void) override { InitBindingHandler(); }
50+
};

examples/light-switch-app/esp32/main/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
using namespace ::chip;
5656
using namespace ::chip::Credentials;
5757
using namespace ::chip::DeviceManager;
58+
using namespace ::chip::DeviceLayer;
5859

5960
namespace {
6061
#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
@@ -71,15 +72,14 @@ DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
7172
static const char * TAG = "light-switch-app";
7273

7374
static AppDeviceCallbacks EchoCallbacks;
75+
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;
7476

7577
static void InitServer(intptr_t context)
7678
{
7779
// Print QR Code URL
7880
PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE));
7981

8082
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
81-
82-
InitBindingHandler();
8383
}
8484

8585
extern "C" void app_main()
@@ -118,6 +118,7 @@ extern "C" void app_main()
118118

119119
CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();
120120
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
121+
DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate);
121122
if (error != CHIP_NO_ERROR)
122123
{
123124
ESP_LOGE(TAG, "device.Init() failed: %" CHIP_ERROR_FORMAT, error.Format());

examples/platform/esp32/common/CommonDeviceCallbacks.cpp

+6-23
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ using namespace chip::DeviceLayer;
4646
using namespace chip::System;
4747

4848
DeviceCallbacksDelegate * appDelegate = nullptr;
49-
#if CONFIG_ENABLE_OTA_REQUESTOR
50-
static bool isOTAInitialized = false;
51-
#endif
5249

5350
void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
5451
{
@@ -66,14 +63,15 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
6663
ESP_LOGI(TAG, "CHIPoBLE disconnected");
6764
break;
6865

69-
case DeviceEventType::kThreadConnectivityChange:
66+
case DeviceEventType::kDnssdInitialized:
7067
#if CONFIG_ENABLE_OTA_REQUESTOR
71-
if (event->ThreadConnectivityChange.Result == kConnectivity_Established && !isOTAInitialized)
68+
OTAHelpers::Instance().InitOTARequestor();
69+
#endif
70+
appDelegate = DeviceCallbacksDelegate::Instance().GetAppDelegate();
71+
if (appDelegate != nullptr)
7272
{
73-
OTAHelpers::Instance().InitOTARequestor();
74-
isOTAInitialized = true;
73+
appDelegate->OnDnssdInitialized();
7574
}
76-
#endif
7775
break;
7876

7977
case DeviceEventType::kCommissioningComplete: {
@@ -136,13 +134,6 @@ void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent *
136134
appDelegate->OnIPv4ConnectivityEstablished();
137135
}
138136
chip::app::DnssdServer::Instance().StartServer();
139-
#if CONFIG_ENABLE_OTA_REQUESTOR
140-
if (!isOTAInitialized)
141-
{
142-
OTAHelpers::Instance().InitOTARequestor();
143-
isOTAInitialized = true;
144-
}
145-
#endif
146137
}
147138
else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
148139
{
@@ -156,14 +147,6 @@ void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent *
156147
{
157148
ESP_LOGI(TAG, "IPv6 Server ready...");
158149
chip::app::DnssdServer::Instance().StartServer();
159-
160-
#if CONFIG_ENABLE_OTA_REQUESTOR
161-
if (!isOTAInitialized)
162-
{
163-
OTAHelpers::Instance().InitOTARequestor();
164-
isOTAInitialized = true;
165-
}
166-
#endif
167150
}
168151
else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
169152
{

examples/platform/esp32/common/CommonDeviceCallbacks.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DeviceCallbacksDelegate
4040
}
4141
virtual void OnIPv4ConnectivityEstablished() {}
4242
virtual void OnIPv4ConnectivityLost() {}
43+
virtual void OnDnssdInitialized() {}
4344
DeviceCallbacksDelegate * mDelegate = nullptr;
4445
void SetAppDelegate(DeviceCallbacksDelegate * delegate) { mDelegate = delegate; }
4546
DeviceCallbacksDelegate * GetAppDelegate() { return mDelegate; }

examples/platform/esp32/ota/OTAHelper.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,25 @@ bool CustomOTARequestorDriver::CanConsent()
5656
return gRequestorCanConsent.ValueOr(DeviceLayer::ExtendedOTARequestorDriver::CanConsent());
5757
}
5858

59-
static void InitOTARequestorHandler(System::Layer * systemLayer, void * appState)
59+
void OTAHelpers::InitOTARequestor()
6060
{
61-
SetRequestorInstance(&gRequestorCore);
62-
gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage());
63-
gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader);
64-
gImageProcessor.SetOTADownloader(&gDownloader);
65-
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
66-
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
67-
68-
if (gUserConsentState != chip::ota::UserConsentState::kUnknown)
61+
if (!GetRequestorInstance())
6962
{
70-
gUserConsentProvider.SetUserConsentState(gUserConsentState);
71-
gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider);
63+
SetRequestorInstance(&gRequestorCore);
64+
gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage());
65+
gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader);
66+
gImageProcessor.SetOTADownloader(&gDownloader);
67+
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
68+
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
69+
70+
if (gUserConsentState != chip::ota::UserConsentState::kUnknown)
71+
{
72+
gUserConsentProvider.SetUserConsentState(gUserConsentState);
73+
gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider);
74+
}
7275
}
7376
}
7477

75-
void OTAHelpers::InitOTARequestor()
76-
{
77-
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler,
78-
nullptr);
79-
}
80-
8178
namespace chip {
8279
namespace Shell {
8380
namespace {

0 commit comments

Comments
 (0)