Skip to content

Commit b6d5847

Browse files
[clusters] Implemented ThreadDiagnosticDelegate
Added ThreadDiagnosticDelegate to enable generation of optional events from ThreadNetworkDiagnostics cluster. Additionally implemented generation of ConnectionStatus and NetworkFaultChanged events when Thread link state is changed.
1 parent 3c7c9b1 commit b6d5847

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

src/app/clusters/thread-network-diagnostics-server/thread-network-diagnostics-server.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222
#include <app/AttributeAccessInterface.h>
2323
#include <app/CommandHandler.h>
2424
#include <app/ConcreteCommandPath.h>
25+
#include <app/EventLogging.h>
2526
#include <app/clusters/thread-network-diagnostics-server/thread-network-diagnostics-provider.h>
2627
#include <app/util/attribute-storage.h>
2728
#include <lib/core/CHIPEncoding.h>
2829
#include <lib/core/Optional.h>
2930
#include <lib/core/TLVTypes.h>
3031
#include <lib/support/CHIPPlatformMemory.h>
3132
#include <platform/CHIPDeviceLayer.h>
33+
#include <platform/DiagnosticDataProvider.h>
34+
#include <tracing/macros.h>
35+
#include <tracing/metric_event.h>
3236

3337
using namespace chip;
3438
using namespace chip::app;
@@ -132,6 +136,62 @@ CHIP_ERROR ThreadDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & aP
132136
return CHIP_NO_ERROR;
133137
}
134138

139+
class ThreadDiagnosticsDelegate : public DeviceLayer::ThreadDiagnosticsDelegate
140+
{
141+
// Notified when the Node’s connection status to a Thread network has changed.
142+
void OnConnectionStatusChanged(ConnectionStatusEnum newConnectionStatus) override
143+
{
144+
MATTER_TRACE_SCOPE("OnConnectionStatusChanged", "ThreadDiagnosticsDelegate");
145+
ChipLogProgress(Zcl, "ThreadDiagnosticsDelegate: OnConnectionStatusChanged");
146+
147+
Events::ConnectionStatus::Type event{ newConnectionStatus };
148+
149+
// ThreadNetworkDiagnostics cluster should exist only for endpoint 0.
150+
if (emberAfContainsServer(0, ThreadNetworkDiagnostics::Id))
151+
{
152+
// If Thread Network Diagnostics cluster is implemented on this endpoint
153+
EventNumber eventNumber;
154+
155+
if (CHIP_NO_ERROR != LogEvent(event, 0, eventNumber))
156+
{
157+
ChipLogError(Zcl, "ThreadDiagnosticsDelegate: Failed to record ConnectionStatus event");
158+
}
159+
}
160+
}
161+
162+
// Notified when the Node’s faults related to a Thread network have changed.
163+
void OnNetworkFaultChanged(const GeneralFaults<kMaxNetworkFaults> & previous,
164+
const GeneralFaults<kMaxNetworkFaults> & current) override
165+
{
166+
MATTER_TRACE_SCOPE("OnNetworkFaultChanged", "ThreadDiagnosticsDelegate");
167+
ChipLogProgress(Zcl, "ThreadDiagnosticsDelegate: OnNetworkFaultChanged");
168+
169+
/* Verify that the data size matches the expected one. */
170+
static_assert(sizeof(*current.data()) == sizeof(NetworkFaultEnum));
171+
172+
DataModel::List<const NetworkFaultEnum> currentList(reinterpret_cast<const NetworkFaultEnum *>(current.data()),
173+
current.size());
174+
DataModel::List<const NetworkFaultEnum> previousList(reinterpret_cast<const NetworkFaultEnum *>(previous.data()),
175+
previous.size());
176+
177+
Events::NetworkFaultChange::Type event{ currentList, previousList };
178+
179+
// ThreadNetworkDiagnostics cluster should exist only for endpoint 0.
180+
if (emberAfContainsServer(0, ThreadNetworkDiagnostics::Id))
181+
{
182+
// If Thread Network Diagnostics cluster is implemented on this endpoint
183+
EventNumber eventNumber;
184+
185+
if (CHIP_NO_ERROR != LogEvent(event, 0, eventNumber))
186+
{
187+
ChipLogError(Zcl, "ThreadDiagnosticsDelegate: Failed to record NetworkFaultChange event");
188+
}
189+
}
190+
}
191+
};
192+
193+
ThreadDiagnosticsDelegate gDiagnosticDelegate;
194+
135195
} // anonymous namespace
136196

137197
bool emberAfThreadNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler * commandObj,
@@ -146,4 +206,5 @@ bool emberAfThreadNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandl
146206
void MatterThreadNetworkDiagnosticsPluginServerInitCallback()
147207
{
148208
registerAttributeAccessOverride(&gAttrAccess);
209+
GetDiagnosticDataProvider().SetThreadDiagnosticsDelegate(&gDiagnosticDelegate);
149210
}

src/include/platform/DiagnosticDataProvider.h

+27-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ class WiFiDiagnosticsDelegate
9191
virtual void OnConnectionStatusChanged(uint8_t connectionStatus) {}
9292
};
9393

94+
/**
95+
* Defines the Thread Diagnostics Delegate class to notify Thread network events.
96+
*/
97+
class ThreadDiagnosticsDelegate
98+
{
99+
public:
100+
virtual ~ThreadDiagnosticsDelegate() {}
101+
102+
/**
103+
* @brief
104+
* Called when the Node’s connection status to a Thread network has changed.
105+
*/
106+
virtual void OnConnectionStatusChanged(app::Clusters::ThreadNetworkDiagnostics::ConnectionStatusEnum newConnectionStatus) {}
107+
108+
/**
109+
* @brief
110+
* Called when the Node detects change in the set of current Thread network faults.
111+
*/
112+
virtual void OnNetworkFaultChanged(const GeneralFaults<kMaxNetworkFaults> & previous,
113+
const GeneralFaults<kMaxNetworkFaults> & current)
114+
{}
115+
};
116+
94117
/**
95118
* Provides access to runtime and build-time configuration information for a chip device.
96119
*/
@@ -99,6 +122,8 @@ class DiagnosticDataProvider
99122
public:
100123
void SetWiFiDiagnosticsDelegate(WiFiDiagnosticsDelegate * delegate) { mWiFiDiagnosticsDelegate = delegate; }
101124
WiFiDiagnosticsDelegate * GetWiFiDiagnosticsDelegate() const { return mWiFiDiagnosticsDelegate; }
125+
void SetThreadDiagnosticsDelegate(ThreadDiagnosticsDelegate * delegate) { mThreadDiagnosticsDelegate = delegate; }
126+
ThreadDiagnosticsDelegate * GetThreadDiagnosticsDelegate() const { return mThreadDiagnosticsDelegate; }
102127

103128
/**
104129
* General Diagnostics methods.
@@ -238,7 +263,8 @@ class DiagnosticDataProvider
238263
virtual ~DiagnosticDataProvider() = default;
239264

240265
private:
241-
WiFiDiagnosticsDelegate * mWiFiDiagnosticsDelegate = nullptr;
266+
WiFiDiagnosticsDelegate * mWiFiDiagnosticsDelegate = nullptr;
267+
ThreadDiagnosticsDelegate * mThreadDiagnosticsDelegate = nullptr;
242268

243269
// No copy, move or assignment.
244270
DiagnosticDataProvider(const DiagnosticDataProvider &) = delete;

src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <app/icd/server/ICDServerConfig.h>
4141
#include <lib/dnssd/Advertiser.h>
4242
#include <lib/dnssd/platform/Dnssd.h>
43+
#include <platform/GeneralFaults.h>
4344
#include <platform/NetworkCommissioning.h>
4445

4546
namespace chip {
@@ -229,6 +230,7 @@ class GenericThreadStackManagerImpl_OpenThread
229230

230231
DnsBrowseCallback mDnsBrowseCallback;
231232
DnsResolveCallback mDnsResolveCallback;
233+
GeneralFaults<kMaxNetworkFaults> mNetworkFaults;
232234

233235
struct DnsServiceTxtEntries
234236
{

src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <lib/support/FixedBufferAllocator.h>
5353
#include <lib/support/ThreadOperationalDataset.h>
5454
#include <lib/support/logging/CHIPLogging.h>
55+
#include <platform/DiagnosticDataProvider.h>
5556
#include <platform/OpenThread/GenericNetworkCommissioningThreadDriver.h>
5657
#include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h>
5758
#include <platform/OpenThread/OpenThreadUtils.h>
@@ -223,6 +224,22 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnPlatformEvent(const
223224
{
224225
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format());
225226
}
227+
228+
ThreadDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetThreadDiagnosticsDelegate();
229+
230+
if (mIsAttached)
231+
{
232+
delegate->OnConnectionStatusChanged(app::Clusters::ThreadNetworkDiagnostics::ConnectionStatusEnum::kConnected);
233+
}
234+
else
235+
{
236+
delegate->OnConnectionStatusChanged(app::Clusters::ThreadNetworkDiagnostics::ConnectionStatusEnum::kNotConnected);
237+
238+
GeneralFaults<kMaxNetworkFaults> current;
239+
current.add(to_underlying(chip::app::Clusters::ThreadNetworkDiagnostics::NetworkFaultEnum::kLinkDown));
240+
delegate->OnNetworkFaultChanged(mNetworkFaults, current);
241+
mNetworkFaults = current;
242+
}
226243
}
227244

228245
#if CHIP_DETAIL_LOGGING

0 commit comments

Comments
 (0)