Skip to content

Commit 1608052

Browse files
yufengwangcapull[bot]
authored andcommitted
[GeneralDiagnostics]: Reporting change for the attributes not managed by the attribute store (#11302)
1 parent 3beccd8 commit 1608052

File tree

5 files changed

+121
-8
lines changed

5 files changed

+121
-8
lines changed

src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp

+64-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <app-common/zap-generated/ids/Attributes.h>
2121
#include <app-common/zap-generated/ids/Clusters.h>
2222
#include <app/AttributeAccessInterface.h>
23+
#include <app/reporting/reporting.h>
2324
#include <app/util/attribute-storage.h>
2425
#include <platform/ConnectivityManager.h>
2526
#include <platform/PlatformManager.h>
@@ -29,7 +30,7 @@ using namespace chip::app;
2930
using namespace chip::app::Clusters;
3031
using namespace chip::app::Clusters::GeneralDiagnostics::Attributes;
3132
using chip::DeviceLayer::ConnectivityMgr;
32-
using chip::DeviceLayer::PlatformManager;
33+
using chip::DeviceLayer::PlatformMgr;
3334

3435
namespace {
3536

@@ -43,12 +44,12 @@ class GeneralDiagosticsAttrAccess : public AttributeAccessInterface
4344

4445
private:
4546
template <typename T>
46-
CHIP_ERROR ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder);
47+
CHIP_ERROR ReadIfSupported(CHIP_ERROR (DeviceLayer::PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder);
4748
CHIP_ERROR ReadNetworkInterfaces(AttributeValueEncoder & aEncoder);
4849
};
4950

5051
template <typename T>
51-
CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &),
52+
CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (DeviceLayer::PlatformManager::*getter)(T &),
5253
AttributeValueEncoder & aEncoder)
5354
{
5455
T data;
@@ -107,26 +108,82 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPath
107108
return ReadNetworkInterfaces(aEncoder);
108109
}
109110
case RebootCount::Id: {
110-
return ReadIfSupported(&PlatformManager::GetRebootCount, aEncoder);
111+
return ReadIfSupported(&DeviceLayer::PlatformManager::GetRebootCount, aEncoder);
111112
}
112113
case UpTime::Id: {
113-
return ReadIfSupported(&PlatformManager::GetUpTime, aEncoder);
114+
return ReadIfSupported(&DeviceLayer::PlatformManager::GetUpTime, aEncoder);
114115
}
115116
case TotalOperationalHours::Id: {
116-
return ReadIfSupported(&PlatformManager::GetTotalOperationalHours, aEncoder);
117+
return ReadIfSupported(&DeviceLayer::PlatformManager::GetTotalOperationalHours, aEncoder);
117118
}
118119
case BootReasons::Id: {
119-
return ReadIfSupported(&PlatformManager::GetBootReasons, aEncoder);
120+
return ReadIfSupported(&DeviceLayer::PlatformManager::GetBootReasons, aEncoder);
120121
}
121122
default: {
122123
break;
123124
}
124125
}
125126
return CHIP_NO_ERROR;
126127
}
128+
129+
class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegate, public DeviceLayer::PlatformManagerDelegate
130+
{
131+
132+
// Gets called when any network interface on the Node is updated.
133+
void OnNetworkInfoChanged() override
134+
{
135+
ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnNetworkInfoChanged");
136+
137+
for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
138+
{
139+
if (emberAfEndpointIndexIsEnabled(index))
140+
{
141+
EndpointId endpointId = emberAfEndpointFromIndex(index);
142+
if (endpointId == 0)
143+
continue;
144+
145+
if (emberAfContainsServer(endpointId, GeneralDiagnostics::Id))
146+
{
147+
// If General Diagnostics cluster is implemented on this endpoint
148+
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
149+
GeneralDiagnostics::Attributes::NetworkInterfaces::Id);
150+
}
151+
}
152+
}
153+
}
154+
155+
// Gets called when the device has been rebooted.
156+
void OnDeviceRebooted() override
157+
{
158+
ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnDeviceRebooted");
159+
160+
for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
161+
{
162+
if (emberAfEndpointIndexIsEnabled(index))
163+
{
164+
EndpointId endpointId = emberAfEndpointFromIndex(index);
165+
166+
if (emberAfContainsServer(endpointId, GeneralDiagnostics::Id))
167+
{
168+
// If General Diagnostics cluster is implemented on this endpoint
169+
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
170+
GeneralDiagnostics::Attributes::RebootCount::Id);
171+
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
172+
GeneralDiagnostics::Attributes::BootReasons::Id);
173+
}
174+
}
175+
}
176+
}
177+
};
178+
179+
GeneralDiagnosticDelegate gDiagnosticDelegate;
180+
127181
} // anonymous namespace
128182

129183
void MatterGeneralDiagnosticsPluginServerInitCallback()
130184
{
131185
registerAttributeAccessOverride(&gAttrAccess);
186+
187+
PlatformMgr().SetDelegate(&gDiagnosticDelegate);
188+
ConnectivityMgr().SetDelegate(&gDiagnosticDelegate);
132189
}

src/include/platform/ConnectivityManager.h

+22
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,25 @@ struct NetworkInterface : public app::Clusters::GeneralDiagnostics::Structs::Net
6060
NetworkInterface * Next; /* Pointer to the next structure. */
6161
};
6262

63+
class ConnectivityManager;
6364
class ConnectivityManagerImpl;
6465

66+
/**
67+
* Defines the delegate class of Connectivity Manager to notify connectivity updates.
68+
*/
69+
class ConnectivityManagerDelegate
70+
{
71+
public:
72+
virtual ~ConnectivityManagerDelegate() {}
73+
74+
/**
75+
* @brief
76+
* Called when any network interface on the Node is changed
77+
*
78+
*/
79+
virtual void OnNetworkInfoChanged() {}
80+
};
81+
6582
/**
6683
* Provides control of network connectivity for a chip device.
6784
*/
@@ -140,6 +157,9 @@ class ConnectivityManager
140157

141158
struct ThreadPollingConfig;
142159

160+
void SetDelegate(ConnectivityManagerDelegate * delegate) { mDelegate = delegate; }
161+
ConnectivityManagerDelegate * GetDelegate() const { return mDelegate; }
162+
143163
// WiFi station methods
144164
WiFiStationMode GetWiFiStationMode();
145165
CHIP_ERROR SetWiFiStationMode(WiFiStationMode val);
@@ -241,6 +261,8 @@ class ConnectivityManager
241261
static const char * CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode);
242262

243263
private:
264+
ConnectivityManagerDelegate * mDelegate = nullptr;
265+
244266
// ===== Members for internal use by the following friends.
245267

246268
friend class PlatformManagerImpl;

src/include/platform/PlatformManager.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ template <class>
6666
class GenericThreadStackManagerImpl_OpenThread_LwIP;
6767
} // namespace Internal
6868

69+
class PlatformManager;
70+
71+
/**
72+
* Defines the delegate class of Platform Manager to notify platform updates.
73+
*/
74+
class PlatformManagerDelegate
75+
{
76+
public:
77+
virtual ~PlatformManagerDelegate() {}
78+
79+
/**
80+
* @brief
81+
* Called after the current device is rebooted
82+
*/
83+
virtual void OnDeviceRebooted() {}
84+
};
85+
6986
/**
7087
* Provides features for initializing and interacting with the chip network
7188
* stack on a chip-enabled device.
@@ -88,6 +105,8 @@ class PlatformManager
88105
CHIP_ERROR InitChipStack();
89106
CHIP_ERROR AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
90107
void RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
108+
void SetDelegate(PlatformManagerDelegate * delegate) { mDelegate = delegate; }
109+
PlatformManagerDelegate * GetDelegate() const { return mDelegate; }
91110

92111
/**
93112
* ScheduleWork can be called after InitChipStack has been called. Calls
@@ -171,7 +190,9 @@ class PlatformManager
171190
#endif
172191

173192
private:
174-
bool mInitialized = false;
193+
bool mInitialized = false;
194+
PlatformManagerDelegate * mDelegate = nullptr;
195+
175196
// ===== Members for internal use by the following friends.
176197

177198
friend class PlatformManagerImpl;

src/platform/Linux/PlatformManagerImpl.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
216216

217217
mStartTime = System::SystemClock().GetMonotonicTimestamp();
218218

219+
ScheduleWork(HandleDeviceRebooted, 0);
220+
219221
exit:
220222
return err;
221223
}
@@ -343,6 +345,16 @@ CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons)
343345
return err;
344346
}
345347

348+
void PlatformManagerImpl::HandleDeviceRebooted(intptr_t arg)
349+
{
350+
PlatformManagerDelegate * delegate = PlatformMgr().GetDelegate();
351+
352+
if (delegate != nullptr)
353+
{
354+
delegate->OnDeviceRebooted();
355+
}
356+
}
357+
346358
#if CHIP_WITH_GIO
347359
GDBusConnection * PlatformManagerImpl::GetGDBusConnection()
348360
{

src/platform/Linux/PlatformManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
8383
// The temporary hack for getting IP address change on linux for network provisioning in the rendezvous session.
8484
// This should be removed or find a better place once we depercate the rendezvous session.
8585
static void WiFIIPChangeListener();
86+
static void HandleDeviceRebooted(intptr_t arg);
8687

8788
#if CHIP_WITH_GIO
8889
struct GDBusConnectionDeleter

0 commit comments

Comments
 (0)