From cf6903aea198917d0599655850b7a18ed672a97e Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Wed, 27 Oct 2021 17:51:43 -0700 Subject: [PATCH] [TE7]:Support read attribute with complex type for general diagnostic cluster (#10641) * Support read attribute with complex type for general diagnostic cluster * Update src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp Co-authored-by: Boris Zbarsky * Encode an empty list if GetNetworkInterfaces fails Co-authored-by: Boris Zbarsky --- .../general_diagnostics_server.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp b/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp index 2534d0649a0dd2..35495a16f5df82 100644 --- a/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp +++ b/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp @@ -16,15 +16,19 @@ */ #include +#include #include #include +#include #include +#include #include using namespace chip; using namespace chip::app; using namespace chip::app::Clusters; using namespace chip::app::Clusters::GeneralDiagnostics::Attributes; +using chip::DeviceLayer::ConnectivityMgr; using chip::DeviceLayer::PlatformManager; namespace { @@ -40,6 +44,7 @@ class GeneralDiagosticsAttrAccess : public AttributeAccessInterface private: template CHIP_ERROR ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder); + CHIP_ERROR ReadNetworkInterfaces(AttributeValueEncoder & aEncoder); }; template @@ -60,6 +65,32 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (PlatformMana return aEncoder.Encode(data); } +CHIP_ERROR GeneralDiagosticsAttrAccess::ReadNetworkInterfaces(AttributeValueEncoder & aEncoder) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + DeviceLayer::NetworkInterface * netifs; + + if (ConnectivityMgr().GetNetworkInterfaces(&netifs) == CHIP_NO_ERROR) + { + err = aEncoder.EncodeList([&netifs](const TagBoundEncoder & encoder) -> CHIP_ERROR { + for (DeviceLayer::NetworkInterface * ifp = netifs; ifp != nullptr; ifp = ifp->Next) + { + ReturnErrorOnFailure(encoder.Encode(*ifp)); + } + + return CHIP_NO_ERROR; + }); + + ConnectivityMgr().ReleaseNetworkInterfaces(netifs); + } + else + { + err = aEncoder.Encode(DataModel::List()); + } + + return err; +} + GeneralDiagosticsAttrAccess gAttrAccess; CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder) @@ -72,6 +103,9 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteAttributePath & aPath switch (aPath.mAttributeId) { + case NetworkInterfaces::Id: { + return ReadNetworkInterfaces(aEncoder); + } case RebootCount::Id: { return ReadIfSupported(&PlatformManager::GetRebootCount, aEncoder); }