Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DiagnosticDataProvider interface #11981

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/common/pigweed/rpc_services/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "device_service/device_service.rpc.pb.h"
#include "platform/ConfigurationManager.h"
#include "platform/PlatformManager.h"
#include <platform/DiagnosticDataProvider.h>

namespace chip {
namespace rpc {
Expand Down Expand Up @@ -54,7 +55,7 @@ class Device : public generated::Device<Device>
virtual pw::Status GetDeviceState(ServerContext &, const pw_protobuf_Empty & request, chip_rpc_DeviceState & response)
{
uint64_t time_since_boot_sec;
DeviceLayer::PlatformMgr().GetUpTime(time_since_boot_sec);
DeviceLayer::GetDiagnosticDataProvider().GetUpTime(time_since_boot_sec);
response.time_since_boot_millis = time_since_boot_sec * 1000;
size_t count = 0;
for (const FabricInfo & fabricInfo : Server::GetInstance().GetFabricTable())
Expand Down
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::DiagnosticDataProvider;
using chip::DeviceLayer::GetDiagnosticDataProvider;

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 = (GetDiagnosticDataProvider().*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 ((GetDiagnosticDataProvider().*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 ConcreteReadAttributePath & a
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::GetBootReason, aEncoder);
}
default: {
break;
Expand All @@ -167,7 +167,7 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & a
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);
GetDiagnosticDataProvider().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 ConcreteReadAttributePath & 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 ConcreteReadAttributePath &
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 ConcreteReadAttributePath &
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::GetDiagnosticDataProvider().*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::GetDiagnosticDataProvider().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::GetDiagnosticDataProvider().ReleaseThreadMetrics(threadMetrics);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/include/platform/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class ConfigurationManager
virtual CHIP_ERROR StoreRebootCount(uint32_t rebootCount) = 0;
virtual CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) = 0;
virtual CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) = 0;
virtual CHIP_ERROR GetBootReasons(uint32_t & bootReasons) = 0;
virtual CHIP_ERROR StoreBootReasons(uint32_t bootReasons) = 0;
virtual CHIP_ERROR GetBootReason(uint32_t & bootReason) = 0;
virtual CHIP_ERROR StoreBootReason(uint32_t bootReason) = 0;
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved

virtual CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) = 0;

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 GetBootReason(uint8_t & bootReason);
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);
Damian-Nordic marked this conversation as resolved.
Show resolved Hide resolved
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.
*/
DiagnosticDataProvider & GetDiagnosticDataProvider();

/**
* Sets a reference to a DiagnosticDataProvider object.
*
* This must be called before any calls to GetDiagnosticDataProvider. If a nullptr is passed in,
* no changes will be made.
*/
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::GetBootReason(uint8_t & bootReason)
{
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