Skip to content

Commit

Permalink
Implement DiagnosticDataProvider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Nov 18, 2021
1 parent 4efa5de commit ea1da16
Show file tree
Hide file tree
Showing 75 changed files with 2,257 additions and 766 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
#include <platform/ConnectivityManager.h>
#include <platform/PlatformManager.h>
#include <platform/DiagnosticDataProvider.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::GeneralDiagnostics::Attributes;
using namespace chip::DeviceLayer;
using chip::DeviceLayer::ConnectivityMgr;
using chip::DeviceLayer::PlatformManager;
using chip::DeviceLayer::PlatformMgr;
using chip::DeviceLayer::DiagnosticDataMgr;
using chip::DeviceLayer::DiagnosticDataProvider;

namespace {

Expand All @@ -46,20 +46,20 @@ class GeneralDiagosticsAttrAccess : public AttributeAccessInterface

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

template <typename T>
CHIP_ERROR ReadListIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadListIfSupported(CHIP_ERROR (DiagnosticDataProvider::*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 (DiagnosticDataProvider::*getter)(T &),
AttributeValueEncoder & aEncoder)
{
T data;
CHIP_ERROR err = (PlatformMgr().*getter)(data);
CHIP_ERROR err = (DiagnosticDataMgr().*getter)(data);
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
{
data = 0;
Expand All @@ -73,13 +73,13 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (PlatformMana
}

template <typename T>
CHIP_ERROR GeneralDiagosticsAttrAccess::ReadListIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &),
CHIP_ERROR GeneralDiagosticsAttrAccess::ReadListIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &),
AttributeValueEncoder & aEncoder)
{
CHIP_ERROR err = CHIP_NO_ERROR;
T faultList;

if ((PlatformMgr().*getter)(faultList) == CHIP_NO_ERROR)
if ((DiagnosticDataMgr().*getter)(faultList) == CHIP_NO_ERROR)
{
err = aEncoder.EncodeList([&faultList](const TagBoundEncoder & encoder) -> CHIP_ERROR {
for (auto fault : faultList)
Expand Down Expand Up @@ -140,25 +140,25 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPath
return ReadNetworkInterfaces(aEncoder);
}
case ActiveHardwareFaults::Id: {
return ReadListIfSupported(&PlatformManager::GetActiveHardwareFaults, aEncoder);
return ReadListIfSupported(&DiagnosticDataProvider::GetActiveHardwareFaults, aEncoder);
}
case ActiveRadioFaults::Id: {
return ReadListIfSupported(&PlatformManager::GetActiveRadioFaults, aEncoder);
return ReadListIfSupported(&DiagnosticDataProvider::GetActiveRadioFaults, aEncoder);
}
case ActiveNetworkFaults::Id: {
return ReadListIfSupported(&PlatformManager::GetActiveNetworkFaults, aEncoder);
return ReadListIfSupported(&DiagnosticDataProvider::GetActiveNetworkFaults, aEncoder);
}
case RebootCount::Id: {
return ReadIfSupported(&PlatformManager::GetRebootCount, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetRebootCount, aEncoder);
}
case UpTime::Id: {
return ReadIfSupported(&PlatformManager::GetUpTime, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetUpTime, aEncoder);
}
case TotalOperationalHours::Id: {
return ReadIfSupported(&PlatformManager::GetTotalOperationalHours, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetTotalOperationalHours, aEncoder);
}
case BootReasons::Id: {
return ReadIfSupported(&PlatformManager::GetBootReasons, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetBootReasons, aEncoder);
}
default: {
break;
Expand All @@ -167,7 +167,7 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPath
return CHIP_NO_ERROR;
}

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

// Gets called when any network interface on the Node is updated.
Expand Down Expand Up @@ -223,6 +223,6 @@ void MatterGeneralDiagnosticsPluginServerInitCallback()
{
registerAttributeAccessOverride(&gAttrAccess);

PlatformMgr().SetDelegate(&gDiagnosticDelegate);
DiagnosticDataMgr().SetDelegate(&gDiagnosticDelegate);
ConnectivityMgr().SetDelegate(&gDiagnosticDelegate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/core/Optional.h>
#include <platform/PlatformManager.h>
#include <platform/DiagnosticDataProvider.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::SoftwareDiagnostics;
using namespace chip::app::Clusters::SoftwareDiagnostics::Attributes;
using chip::DeviceLayer::PlatformManager;
using chip::DeviceLayer::DiagnosticDataProvider;

namespace {

Expand All @@ -45,7 +45,7 @@ class SoftwareDiagosticsAttrAccess : public AttributeAccessInterface
CHIP_ERROR Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder) override;

private:
CHIP_ERROR ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(uint64_t &), AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(uint64_t &), AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadThreadMetrics(AttributeValueEncoder & aEncoder);
};

Expand All @@ -62,13 +62,13 @@ CHIP_ERROR SoftwareDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPat
switch (aPath.mAttributeId)
{
case CurrentHeapFree::Id: {
return ReadIfSupported(&PlatformManager::GetCurrentHeapFree, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetCurrentHeapFree, aEncoder);
}
case CurrentHeapUsed::Id: {
return ReadIfSupported(&PlatformManager::GetCurrentHeapUsed, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetCurrentHeapUsed, aEncoder);
}
case CurrentHeapHighWatermark::Id: {
return ReadIfSupported(&PlatformManager::GetCurrentHeapHighWatermark, aEncoder);
return ReadIfSupported(&DiagnosticDataProvider::GetCurrentHeapHighWatermark, aEncoder);
}
case ThreadMetrics::Id: {
return ReadThreadMetrics(aEncoder);
Expand All @@ -80,11 +80,11 @@ CHIP_ERROR SoftwareDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPat
return CHIP_NO_ERROR;
}

CHIP_ERROR SoftwareDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(uint64_t &),
CHIP_ERROR SoftwareDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(uint64_t &),
AttributeValueEncoder & aEncoder)
{
uint64_t data;
CHIP_ERROR err = (DeviceLayer::PlatformMgr().*getter)(data);
CHIP_ERROR err = (DeviceLayer::DiagnosticDataMgr().*getter)(data);
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
{
data = 0;
Expand All @@ -102,7 +102,7 @@ CHIP_ERROR SoftwareDiagosticsAttrAccess::ReadThreadMetrics(AttributeValueEncoder
CHIP_ERROR err = CHIP_NO_ERROR;
DeviceLayer::ThreadMetrics * threadMetrics;

if (DeviceLayer::PlatformMgr().GetThreadMetrics(&threadMetrics) == CHIP_NO_ERROR)
if (DeviceLayer::DiagnosticDataMgr().GetThreadMetrics(&threadMetrics) == CHIP_NO_ERROR)
{
err = aEncoder.EncodeList([&threadMetrics](const TagBoundEncoder & encoder) -> CHIP_ERROR {
for (DeviceLayer::ThreadMetrics * thread = threadMetrics; thread != nullptr; thread = thread->Next)
Expand All @@ -113,7 +113,7 @@ CHIP_ERROR SoftwareDiagosticsAttrAccess::ReadThreadMetrics(AttributeValueEncoder
return CHIP_NO_ERROR;
});

DeviceLayer::PlatformMgr().ReleaseThreadMetrics(threadMetrics);
DeviceLayer::DiagnosticDataMgr().ReleaseThreadMetrics(threadMetrics);
}
else
{
Expand Down
179 changes: 179 additions & 0 deletions src/include/platform/DiagnosticDataProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* Defines the public interface for the Device Layer DiagnosticDataProvider object.
*/

#pragma once

#include <app-common/zap-generated/cluster-objects.h>
#include <platform/CHIPDeviceBuildConfig.h>
#include <platform/GeneralFaults.h>

namespace chip {
namespace DeviceLayer {

// Maximum length of vendor defined name or prefix of the software thread that is
// static for the duration of the thread.
static constexpr size_t kMaxThreadNameLength = 32;

struct ThreadMetrics : public app::Clusters::SoftwareDiagnostics::Structs::ThreadMetrics::Type
{
char NameBuf[kMaxThreadNameLength + 1];
ThreadMetrics * Next; /* Pointer to the next structure. */
};

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

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

/**
* Provides access to runtime and build-time configuration information for a chip device.
*/
class DiagnosticDataProvider
{
public:
void SetDelegate(DiagnosticsDelegate * delegate) { mDelegate = delegate; }
DiagnosticsDelegate * GetDelegate() const { return mDelegate; }

/**
* General Diagnostics methods.
*/
virtual CHIP_ERROR GetRebootCount(uint16_t & rebootCount);
virtual CHIP_ERROR GetUpTime(uint64_t & upTime);
virtual CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours);
virtual CHIP_ERROR GetBootReasons(uint8_t & bootReasons);
virtual CHIP_ERROR GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults);
virtual CHIP_ERROR GetActiveRadioFaults(GeneralFaults<kMaxRadioFaults> & radioFaults);
virtual CHIP_ERROR GetActiveNetworkFaults(GeneralFaults<kMaxNetworkFaults> & networkFaults);

/**
* Software Diagnostics methods.
*/
virtual CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree);
virtual CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed);
virtual CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark);

/*
* Get the linked list of thread metrics of the current plaform. After usage, each caller of GetThreadMetrics
* needs to release the thread metrics list it gets via ReleaseThreadMetrics.
*
*/
virtual CHIP_ERROR GetThreadMetrics(ThreadMetrics ** threadMetricsOut);
virtual void ReleaseThreadMetrics(ThreadMetrics * threadMetrics);

protected:
// Construction/destruction limited to subclasses.
DiagnosticDataProvider() = default;
virtual ~DiagnosticDataProvider() = default;

private:
DiagnosticsDelegate * mDelegate = nullptr;

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

/**
* Returns a reference to a DiagnosticDataProvider object.
*
* Applications should use this to access the features of the DiagnosticDataProvider.
*/
extern DiagnosticDataProvider & DiagnosticDataMgr();

/**
* Sets a reference to a DiagnosticDataProvider object.
*
* This must be called before any calls to DiagnosticDataMgr. If a nullptr is passed in,
* no changes will be made.
*/
extern void SetDiagnosticDataProvider(DiagnosticDataProvider * diagnosticDataProvider);

inline CHIP_ERROR DiagnosticDataProvider::GetCurrentHeapFree(uint64_t & currentHeapFree)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetCurrentHeapUsed(uint64_t & currentHeapUsed)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetThreadMetrics(ThreadMetrics ** threadMetricsOut)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline void DiagnosticDataProvider::ReleaseThreadMetrics(ThreadMetrics * threadMetrics) {}

inline CHIP_ERROR DiagnosticDataProvider::GetRebootCount(uint16_t & rebootCount)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetUpTime(uint64_t & upTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetTotalOperationalHours(uint32_t & totalOperationalHours)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetBootReasons(uint8_t & bootReasons)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetActiveRadioFaults(GeneralFaults<kMaxRadioFaults> & radioFaults)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetActiveNetworkFaults(GeneralFaults<kMaxNetworkFaults> & networkFaults)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

} // namespace DeviceLayer
} // namespace chip
Loading

0 comments on commit ea1da16

Please sign in to comment.