Skip to content

Commit

Permalink
[GeneralDiagnostics]: Reporting change for the attributes not managed…
Browse files Browse the repository at this point in the history
… by the attribute store (#11302)
  • Loading branch information
yufengwangca authored Nov 2, 2021
1 parent 5532fa4 commit e24e76c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
#include <platform/ConnectivityManager.h>
#include <platform/PlatformManager.h>
Expand All @@ -29,7 +30,7 @@ using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::GeneralDiagnostics::Attributes;
using chip::DeviceLayer::ConnectivityMgr;
using chip::DeviceLayer::PlatformManager;
using chip::DeviceLayer::PlatformMgr;

namespace {

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

private:
template <typename T>
CHIP_ERROR ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadIfSupported(CHIP_ERROR (DeviceLayer::PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadNetworkInterfaces(AttributeValueEncoder & aEncoder);
};

template <typename T>
CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &),
CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (DeviceLayer::PlatformManager::*getter)(T &),
AttributeValueEncoder & aEncoder)
{
T data;
Expand Down Expand Up @@ -107,26 +108,82 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPath
return ReadNetworkInterfaces(aEncoder);
}
case RebootCount::Id: {
return ReadIfSupported(&PlatformManager::GetRebootCount, aEncoder);
return ReadIfSupported(&DeviceLayer::PlatformManager::GetRebootCount, aEncoder);
}
case UpTime::Id: {
return ReadIfSupported(&PlatformManager::GetUpTime, aEncoder);
return ReadIfSupported(&DeviceLayer::PlatformManager::GetUpTime, aEncoder);
}
case TotalOperationalHours::Id: {
return ReadIfSupported(&PlatformManager::GetTotalOperationalHours, aEncoder);
return ReadIfSupported(&DeviceLayer::PlatformManager::GetTotalOperationalHours, aEncoder);
}
case BootReasons::Id: {
return ReadIfSupported(&PlatformManager::GetBootReasons, aEncoder);
return ReadIfSupported(&DeviceLayer::PlatformManager::GetBootReasons, aEncoder);
}
default: {
break;
}
}
return CHIP_NO_ERROR;
}

class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegate, public DeviceLayer::PlatformManagerDelegate
{

// Gets called when any network interface on the Node is updated.
void OnNetworkInfoChanged() override
{
ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnNetworkInfoChanged");

for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
{
if (emberAfEndpointIndexIsEnabled(index))
{
EndpointId endpointId = emberAfEndpointFromIndex(index);
if (endpointId == 0)
continue;

if (emberAfContainsServer(endpointId, GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::NetworkInterfaces::Id);
}
}
}
}

// Gets called when the device has been rebooted.
void OnDeviceRebooted() override
{
ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnDeviceRebooted");

for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
{
if (emberAfEndpointIndexIsEnabled(index))
{
EndpointId endpointId = emberAfEndpointFromIndex(index);

if (emberAfContainsServer(endpointId, GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::RebootCount::Id);
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::BootReasons::Id);
}
}
}
}
};

GeneralDiagnosticDelegate gDiagnosticDelegate;

} // anonymous namespace

void MatterGeneralDiagnosticsPluginServerInitCallback()
{
registerAttributeAccessOverride(&gAttrAccess);

PlatformMgr().SetDelegate(&gDiagnosticDelegate);
ConnectivityMgr().SetDelegate(&gDiagnosticDelegate);
}
22 changes: 22 additions & 0 deletions src/include/platform/ConnectivityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,25 @@ struct NetworkInterface : public app::Clusters::GeneralDiagnostics::Structs::Net
NetworkInterface * Next; /* Pointer to the next structure. */
};

class ConnectivityManager;
class ConnectivityManagerImpl;

/**
* Defines the delegate class of Connectivity Manager to notify connectivity updates.
*/
class ConnectivityManagerDelegate
{
public:
virtual ~ConnectivityManagerDelegate() {}

/**
* @brief
* Called when any network interface on the Node is changed
*
*/
virtual void OnNetworkInfoChanged() {}
};

/**
* Provides control of network connectivity for a chip device.
*/
Expand Down Expand Up @@ -140,6 +157,9 @@ class ConnectivityManager

struct ThreadPollingConfig;

void SetDelegate(ConnectivityManagerDelegate * delegate) { mDelegate = delegate; }
ConnectivityManagerDelegate * GetDelegate() const { return mDelegate; }

// WiFi station methods
WiFiStationMode GetWiFiStationMode();
CHIP_ERROR SetWiFiStationMode(WiFiStationMode val);
Expand Down Expand Up @@ -241,6 +261,8 @@ class ConnectivityManager
static const char * CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode);

private:
ConnectivityManagerDelegate * mDelegate = nullptr;

// ===== Members for internal use by the following friends.

friend class PlatformManagerImpl;
Expand Down
23 changes: 22 additions & 1 deletion src/include/platform/PlatformManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ template <class>
class GenericThreadStackManagerImpl_OpenThread_LwIP;
} // namespace Internal

class PlatformManager;

/**
* Defines the delegate class of Platform Manager to notify platform updates.
*/
class PlatformManagerDelegate
{
public:
virtual ~PlatformManagerDelegate() {}

/**
* @brief
* Called after the current device is rebooted
*/
virtual void OnDeviceRebooted() {}
};

/**
* Provides features for initializing and interacting with the chip network
* stack on a chip-enabled device.
Expand All @@ -88,6 +105,8 @@ class PlatformManager
CHIP_ERROR InitChipStack();
CHIP_ERROR AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
void RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
void SetDelegate(PlatformManagerDelegate * delegate) { mDelegate = delegate; }
PlatformManagerDelegate * GetDelegate() const { return mDelegate; }

/**
* ScheduleWork can be called after InitChipStack has been called. Calls
Expand Down Expand Up @@ -171,7 +190,9 @@ class PlatformManager
#endif

private:
bool mInitialized = false;
bool mInitialized = false;
PlatformManagerDelegate * mDelegate = nullptr;

// ===== Members for internal use by the following friends.

friend class PlatformManagerImpl;
Expand Down
12 changes: 12 additions & 0 deletions src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()

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

ScheduleWork(HandleDeviceRebooted, 0);

exit:
return err;
}
Expand Down Expand Up @@ -343,6 +345,16 @@ CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons)
return err;
}

void PlatformManagerImpl::HandleDeviceRebooted(intptr_t arg)
{
PlatformManagerDelegate * delegate = PlatformMgr().GetDelegate();

if (delegate != nullptr)
{
delegate->OnDeviceRebooted();
}
}

#if CHIP_WITH_GIO
GDBusConnection * PlatformManagerImpl::GetGDBusConnection()
{
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
// The temporary hack for getting IP address change on linux for network provisioning in the rendezvous session.
// This should be removed or find a better place once we depercate the rendezvous session.
static void WiFIIPChangeListener();
static void HandleDeviceRebooted(intptr_t arg);

#if CHIP_WITH_GIO
struct GDBusConnectionDeleter
Expand Down

0 comments on commit e24e76c

Please sign in to comment.