Skip to content

Commit 05c9cb2

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 05c9cb2

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

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

+54
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,55 @@ CHIP_ERROR ThreadDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & aP
132136
return CHIP_NO_ERROR;
133137
}
134138

139+
class ThreadDiagnosticsDelegate : public DeviceLayer::ThreadDiagnosticsDelegate
140+
{
141+
// Gets when the Node’s connection status to a Thread network has changed.
142+
void OnConnectionStatusChanged(uint8_t connectionStatus) override
143+
{
144+
MATTER_TRACE_SCOPE("OnConnectionStatusChanged", "ThreadDiagnosticsDelegate");
145+
ChipLogProgress(Zcl, "ThreadDiagnosticsDelegate: OnConnectionStatusChanged");
146+
147+
Events::ConnectionStatus::Type event{ static_cast<ConnectionStatusEnum>(connectionStatus) };
148+
for (auto endpoint : EnabledEndpointsWithServerCluster(ThreadNetworkDiagnostics::Id))
149+
{
150+
// If Thread Network Diagnostics cluster is implemented on this endpoint
151+
EventNumber eventNumber;
152+
153+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
154+
{
155+
ChipLogError(Zcl, "ThreadDiagnosticsDelegate: Failed to record ConnectionStatus event");
156+
}
157+
}
158+
}
159+
160+
// Gets when the Node’s default in a Thread network has occurred.
161+
void OnNetworkFaultChanged(const GeneralFaults<kMaxNetworkFaults> & previous,
162+
const GeneralFaults<kMaxNetworkFaults> & current) override
163+
{
164+
MATTER_TRACE_SCOPE("OnNetworkFaultChanged", "ThreadDiagnosticsDelegate");
165+
ChipLogProgress(Zcl, "ThreadDiagnosticsDelegate: OnNetworkFaultChanged");
166+
167+
DataModel::List<const NetworkFaultEnum> currentList(reinterpret_cast<const NetworkFaultEnum *>(current.data()),
168+
current.size());
169+
DataModel::List<const NetworkFaultEnum> previousList(reinterpret_cast<const NetworkFaultEnum *>(previous.data()),
170+
previous.size());
171+
172+
Events::NetworkFaultChange::Type event{ currentList, previousList };
173+
for (auto endpoint : EnabledEndpointsWithServerCluster(ThreadNetworkDiagnostics::Id))
174+
{
175+
// If Thread Network Diagnostics cluster is implemented on this endpoint
176+
EventNumber eventNumber;
177+
178+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
179+
{
180+
ChipLogError(Zcl, "ThreadDiagnosticsDelegate: Failed to record NetworkFaultChange event");
181+
}
182+
}
183+
}
184+
};
185+
186+
ThreadDiagnosticsDelegate gDiagnosticDelegate;
187+
135188
} // anonymous namespace
136189

137190
bool emberAfThreadNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler * commandObj,
@@ -146,4 +199,5 @@ bool emberAfThreadNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandl
146199
void MatterThreadNetworkDiagnosticsPluginServerInitCallback()
147200
{
148201
registerAttributeAccessOverride(&gAttrAccess);
202+
GetDiagnosticDataProvider().SetThreadDiagnosticsDelegate(&gDiagnosticDelegate);
149203
}

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(uint8_t connectionStatus) {}
107+
108+
/**
109+
* @brief
110+
* Called when the Node’s fault of a Thread network has occurred.
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

+19
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,24 @@ 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(
233+
chip::to_underlying(chip::app::Clusters::ThreadNetworkDiagnostics::ConnectionStatusEnum::kConnected));
234+
}
235+
else
236+
{
237+
delegate->OnConnectionStatusChanged(
238+
chip::to_underlying(chip::app::Clusters::ThreadNetworkDiagnostics::ConnectionStatusEnum::kNotConnected));
239+
240+
GeneralFaults<kMaxNetworkFaults> current;
241+
current.add(to_underlying(chip::app::Clusters::ThreadNetworkDiagnostics::NetworkFaultEnum::kLinkDown));
242+
delegate->OnNetworkFaultChanged(current, mNetworkFaults);
243+
mNetworkFaults = current;
244+
}
226245
}
227246

228247
#if CHIP_DETAIL_LOGGING

0 commit comments

Comments
 (0)