diff --git a/examples/common/pigweed/rpc_services/Device.h b/examples/common/pigweed/rpc_services/Device.h index 6a92c0454df645..289fe7438fc55f 100644 --- a/examples/common/pigweed/rpc_services/Device.h +++ b/examples/common/pigweed/rpc_services/Device.h @@ -25,6 +25,7 @@ #include "device_service/device_service.rpc.pb.h" #include "platform/ConfigurationManager.h" #include "platform/PlatformManager.h" +#include namespace chip { namespace rpc { @@ -54,7 +55,7 @@ class Device : public generated::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()) 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 275750ec4fab31..dc35be5453494c 100644 --- a/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp +++ b/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include using namespace chip; using namespace chip::app; @@ -31,8 +31,8 @@ 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 { @@ -46,20 +46,20 @@ class GeneralDiagosticsAttrAccess : public AttributeAccessInterface private: template - CHIP_ERROR ReadIfSupported(CHIP_ERROR (PlatformManager::*getter)(T &), AttributeValueEncoder & aEncoder); + CHIP_ERROR ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), AttributeValueEncoder & aEncoder); template - 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 -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; @@ -73,13 +73,13 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (PlatformMana } template -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) @@ -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; @@ -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. @@ -223,6 +223,6 @@ void MatterGeneralDiagnosticsPluginServerInitCallback() { registerAttributeAccessOverride(&gAttrAccess); - PlatformMgr().SetDelegate(&gDiagnosticDelegate); + GetDiagnosticDataProvider().SetDelegate(&gDiagnosticDelegate); ConnectivityMgr().SetDelegate(&gDiagnosticDelegate); } diff --git a/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp b/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp index 3a29d8eafca146..667601f56a03cd 100644 --- a/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp +++ b/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp @@ -25,14 +25,14 @@ #include #include #include -#include +#include 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 { @@ -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); }; @@ -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); @@ -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; @@ -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) @@ -113,7 +113,7 @@ CHIP_ERROR SoftwareDiagosticsAttrAccess::ReadThreadMetrics(AttributeValueEncoder return CHIP_NO_ERROR; }); - DeviceLayer::PlatformMgr().ReleaseThreadMetrics(threadMetrics); + DeviceLayer::GetDiagnosticDataProvider().ReleaseThreadMetrics(threadMetrics); } else { diff --git a/src/include/platform/ConfigurationManager.h b/src/include/platform/ConfigurationManager.h index 9390fff692fada..f6f2a04df6e421 100644 --- a/src/include/platform/ConfigurationManager.h +++ b/src/include/platform/ConfigurationManager.h @@ -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; virtual CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) = 0; diff --git a/src/include/platform/DiagnosticDataProvider.h b/src/include/platform/DiagnosticDataProvider.h new file mode 100644 index 00000000000000..b64914688c1d4d --- /dev/null +++ b/src/include/platform/DiagnosticDataProvider.h @@ -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 +#include +#include + +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 & hardwareFaults); + virtual CHIP_ERROR GetActiveRadioFaults(GeneralFaults & radioFaults); + virtual CHIP_ERROR GetActiveNetworkFaults(GeneralFaults & 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. + */ +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 & hardwareFaults) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +inline CHIP_ERROR DiagnosticDataProvider::GetActiveRadioFaults(GeneralFaults & radioFaults) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +inline CHIP_ERROR DiagnosticDataProvider::GetActiveNetworkFaults(GeneralFaults & networkFaults) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h index 7c340ea6f8aa10..0030f1da8cbbad 100644 --- a/src/include/platform/PlatformManager.h +++ b/src/include/platform/PlatformManager.h @@ -23,10 +23,8 @@ #pragma once -#include #include #include -#include #include #include @@ -68,33 +66,6 @@ template class GenericThreadStackManagerImpl_OpenThread_LwIP; } // namespace Internal -// 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. */ -}; - -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. @@ -117,8 +88,6 @@ 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 @@ -182,40 +151,12 @@ class PlatformManager void UnlockChipStack(); CHIP_ERROR Shutdown(); - /** - * Software Diagnostics methods. - */ - CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed); - 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. - * - */ - CHIP_ERROR GetThreadMetrics(ThreadMetrics ** threadMetricsOut); - void ReleaseThreadMetrics(ThreadMetrics * threadMetrics); - - /** - * General Diagnostics methods. - */ - CHIP_ERROR GetRebootCount(uint16_t & rebootCount); - CHIP_ERROR GetUpTime(uint64_t & upTime); - CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR GetBootReasons(uint8_t & bootReasons); - - CHIP_ERROR GetActiveHardwareFaults(GeneralFaults & hardwareFaults); - CHIP_ERROR GetActiveRadioFaults(GeneralFaults & radioFaults); - CHIP_ERROR GetActiveNetworkFaults(GeneralFaults & networkFaults); - #if CHIP_STACK_LOCK_TRACKING_ENABLED bool IsChipStackLockedByCurrentThread() const; #endif private: - bool mInitialized = false; - PlatformManagerDelegate * mDelegate = nullptr; + bool mInitialized = false; // ===== Members for internal use by the following friends. @@ -455,65 +396,5 @@ inline CHIP_ERROR PlatformManager::StartChipTimer(System::Clock::Timeout duratio return static_cast(this)->_StartChipTimer(duration); } -inline CHIP_ERROR PlatformManager::GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - return static_cast(this)->_GetCurrentHeapFree(currentHeapFree); -} - -inline CHIP_ERROR PlatformManager::GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - return static_cast(this)->_GetCurrentHeapUsed(currentHeapUsed); -} - -inline CHIP_ERROR PlatformManager::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - return static_cast(this)->_GetCurrentHeapHighWatermark(currentHeapHighWatermark); -} - -inline CHIP_ERROR PlatformManager::GetThreadMetrics(ThreadMetrics ** threadMetricsOut) -{ - return static_cast(this)->_GetThreadMetrics(threadMetricsOut); -} - -inline void PlatformManager::ReleaseThreadMetrics(ThreadMetrics * threadMetrics) -{ - return static_cast(this)->_ReleaseThreadMetrics(threadMetrics); -} - -inline CHIP_ERROR PlatformManager::GetRebootCount(uint16_t & rebootCount) -{ - return static_cast(this)->_GetRebootCount(rebootCount); -} - -inline CHIP_ERROR PlatformManager::GetUpTime(uint64_t & upTime) -{ - return static_cast(this)->_GetUpTime(upTime); -} - -inline CHIP_ERROR PlatformManager::GetTotalOperationalHours(uint32_t & totalOperationalHours) -{ - return static_cast(this)->_GetTotalOperationalHours(totalOperationalHours); -} - -inline CHIP_ERROR PlatformManager::GetBootReasons(uint8_t & bootReasons) -{ - return static_cast(this)->_GetBootReasons(bootReasons); -} - -inline CHIP_ERROR PlatformManager::GetActiveHardwareFaults(GeneralFaults & hardwareFaults) -{ - return static_cast(this)->_GetActiveHardwareFaults(hardwareFaults); -} - -inline CHIP_ERROR PlatformManager::GetActiveRadioFaults(GeneralFaults & radioFaults) -{ - return static_cast(this)->_GetActiveRadioFaults(radioFaults); -} - -inline CHIP_ERROR PlatformManager::GetActiveNetworkFaults(GeneralFaults & networkFaults) -{ - return static_cast(this)->_GetActiveNetworkFaults(networkFaults); -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.cpp b/src/include/platform/internal/GenericConfigurationManagerImpl.cpp index 83aa127fab0a05..7817ce75f66698 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.cpp +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.cpp @@ -369,13 +369,13 @@ CHIP_ERROR GenericConfigurationManagerImpl::StoreTotalOperationalHour } template -CHIP_ERROR GenericConfigurationManagerImpl::GetBootReasons(uint32_t & bootReasons) +CHIP_ERROR GenericConfigurationManagerImpl::GetBootReason(uint32_t & bootReason) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; } template -CHIP_ERROR GenericConfigurationManagerImpl::StoreBootReasons(uint32_t bootReasons) +CHIP_ERROR GenericConfigurationManagerImpl::StoreBootReason(uint32_t bootReason) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; } diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.h b/src/include/platform/internal/GenericConfigurationManagerImpl.h index 7c707737edf142..79cdbacbaf7e04 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.h +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.h @@ -98,8 +98,8 @@ class GenericConfigurationManagerImpl : public ConfigurationManager CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; - CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override; - CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override; + CHIP_ERROR GetBootReason(uint32_t & bootReason) override; + CHIP_ERROR StoreBootReason(uint32_t bootReason) override; CHIP_ERROR RunUnitTests(void) override; bool IsFullyProvisioned() override; void InitiateFactoryReset() override; diff --git a/src/include/platform/internal/GenericPlatformManagerImpl.h b/src/include/platform/internal/GenericPlatformManagerImpl.h index 8a72d67a04ea9f..008a98335eccaf 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl.h +++ b/src/include/platform/internal/GenericPlatformManagerImpl.h @@ -58,21 +58,6 @@ class GenericPlatformManagerImpl void _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg); void _DispatchEvent(const ChipDeviceEvent * event); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); - CHIP_ERROR _GetThreadMetrics(ThreadMetrics ** threadMetricsOut); - void _ReleaseThreadMetrics(ThreadMetrics * threadMetrics); - - CHIP_ERROR _GetRebootCount(uint16_t & rebootCount); - CHIP_ERROR _GetUpTime(uint64_t & upTime); - CHIP_ERROR _GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR _GetBootReasons(uint8_t & bootReasons); - - CHIP_ERROR _GetActiveHardwareFaults(GeneralFaults & hardwareFaults); - CHIP_ERROR _GetActiveRadioFaults(GeneralFaults & radioFaults); - CHIP_ERROR _GetActiveNetworkFaults(GeneralFaults & networkFaults); - // ===== Support methods that can be overridden by the implementation subclass. void DispatchEventToDeviceLayer(const ChipDeviceEvent * event); @@ -88,77 +73,6 @@ class GenericPlatformManagerImpl // Instruct the compiler to instantiate the template only when explicitly told to do so. extern template class GenericPlatformManagerImpl; -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetThreadMetrics(ThreadMetrics ** threadMetricsOut) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline void GenericPlatformManagerImpl::_ReleaseThreadMetrics(ThreadMetrics * threadMetrics) -{} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetUpTime(uint64_t & upTime) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperationalHours) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR -GenericPlatformManagerImpl::_GetActiveHardwareFaults(GeneralFaults & hardwareFaults) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetActiveRadioFaults(GeneralFaults & radioFaults) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetActiveNetworkFaults(GeneralFaults & networkFaults) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Ameba/BUILD.gn b/src/platform/Ameba/BUILD.gn index 2626ec984d0b0b..0376c59b749733 100755 --- a/src/platform/Ameba/BUILD.gn +++ b/src/platform/Ameba/BUILD.gn @@ -34,6 +34,8 @@ static_library("Ameba") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", "Logging.cpp", diff --git a/src/platform/Ameba/ConfigurationManagerImpl.cpp b/src/platform/Ameba/ConfigurationManagerImpl.cpp index 8d67c147029300..a99924bbe56040 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.cpp +++ b/src/platform/Ameba/ConfigurationManagerImpl.cpp @@ -79,7 +79,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init() if (!AmebaConfig::ConfigValueExists(AmebaConfig::kCounterKey_BootReason)) { - err = StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED); + err = StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED); SuccessOrExit(err); } @@ -119,14 +119,14 @@ CHIP_ERROR ConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOp return WriteConfigValue(AmebaConfig::kCounterKey_TotalOperationalHours, totalOperationalHours); } -CHIP_ERROR ConfigurationManagerImpl::GetBootReasons(uint32_t & bootReasons) +CHIP_ERROR ConfigurationManagerImpl::GetBootReason(uint32_t & bootReason) { - return ReadConfigValue(AmebaConfig::kCounterKey_BootReason, bootReasons); + return ReadConfigValue(AmebaConfig::kCounterKey_BootReason, bootReason); } -CHIP_ERROR ConfigurationManagerImpl::StoreBootReasons(uint32_t bootReasons) +CHIP_ERROR ConfigurationManagerImpl::StoreBootReason(uint32_t bootReason) { - return WriteConfigValue(AmebaConfig::kCounterKey_BootReason, bootReasons); + return WriteConfigValue(AmebaConfig::kCounterKey_BootReason, bootReason); } CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) diff --git a/src/platform/Ameba/ConfigurationManagerImpl.h b/src/platform/Ameba/ConfigurationManagerImpl.h index 3470b312830c34..035f3313e7caae 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.h +++ b/src/platform/Ameba/ConfigurationManagerImpl.h @@ -53,8 +53,8 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; - CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override; - CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override; + CHIP_ERROR GetBootReason(uint32_t & bootReason) override; + CHIP_ERROR StoreBootReason(uint32_t bootReason) override; // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. diff --git a/src/platform/Ameba/DiagnosticDataProviderImpl.cpp b/src/platform/Ameba/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..7b983efc097a49 --- /dev/null +++ b/src/platform/Ameba/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,120 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for Ameba platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ + currentHeapFree = xPortGetFreeHeapSize(); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ + currentHeapUsed = xPortGetTotalHeapSize() - xPortGetFreeHeapSize(); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ + currentHeapHighWatermark = xPortGetTotalHeapSize() - xPortGetMinimumEverFreeHeapSize(); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetRebootCount(uint16_t & rebootCount) +{ + uint32_t count = 0; + + CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); + + if (err == CHIP_NO_ERROR) + { + VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + rebootCount = static_cast(count); + } + + return err; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime) +{ + System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); + System::Clock::Timestamp startTime = PlatformMgrImpl().GetStartTime(); + + if (currentTime >= startTime) + { + upTime = std::chrono::duration_cast(currentTime - startTime).count(); + return CHIP_NO_ERROR; + } + + return CHIP_ERROR_INVALID_TIME; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) +{ + uint64_t upTime = 0; + + if (GetUpTime(upTime) == CHIP_NO_ERROR) + { + uint32_t totalHours = 0; + if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) + { + VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + totalOperationalHours = totalHours + static_cast(upTime / 3600); + return CHIP_NO_ERROR; + } + } + + return CHIP_ERROR_INVALID_TIME; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason) +{ + uint32_t reason = 0; + + CHIP_ERROR err = ConfigurationMgr().GetBootReason(reason); + + if (err == CHIP_NO_ERROR) + { + VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + bootReason = static_cast(reason); + } + + return err; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Ameba/DiagnosticDataProviderImpl.h b/src/platform/Ameba/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..d96dbbb826538a --- /dev/null +++ b/src/platform/Ameba/DiagnosticDataProviderImpl.h @@ -0,0 +1,51 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; + + CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override; + CHIP_ERROR GetUpTime(uint64_t & upTime) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR GetBootReason(uint8_t & bootReason) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Ameba/PlatformManagerImpl.cpp b/src/platform/Ameba/PlatformManagerImpl.cpp index 000824271da1d2..5a4fbf631b12fa 100644 --- a/src/platform/Ameba/PlatformManagerImpl.cpp +++ b/src/platform/Ameba/PlatformManagerImpl.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -58,6 +59,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) CHIP_ERROR err; SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Make sure the LwIP core lock has been initialized err = Internal::InitLwIPCoreLock(); @@ -84,7 +86,7 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() { uint64_t upTime = 0; - if (_GetUpTime(upTime) == CHIP_NO_ERROR) + if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR) { uint32_t totalOperationalHours = 0; @@ -105,83 +107,5 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() return Internal::GenericPlatformManagerImpl_FreeRTOS::_Shutdown(); } -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - currentHeapFree = xPortGetFreeHeapSize(); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - currentHeapUsed = xPortGetTotalHeapSize() - xPortGetFreeHeapSize(); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - currentHeapHighWatermark = xPortGetTotalHeapSize() - xPortGetMinimumEverFreeHeapSize(); - return CHIP_NO_ERROR; -} -CHIP_ERROR PlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) -{ - uint32_t count = 0; - - CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); - - if (err == CHIP_NO_ERROR) - { - VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - rebootCount = static_cast(count); - } - - return err; -} - -CHIP_ERROR PlatformManagerImpl::_GetUpTime(uint64_t & upTime) -{ - System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); - - if (currentTime >= mStartTime) - { - upTime = std::chrono::duration_cast(currentTime - mStartTime).count(); - return CHIP_NO_ERROR; - } - - return CHIP_ERROR_INVALID_TIME; -} - -CHIP_ERROR PlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperationalHours) -{ - uint64_t upTime = 0; - - if (_GetUpTime(upTime) == CHIP_NO_ERROR) - { - uint32_t totalHours = 0; - if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) - { - VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - totalOperationalHours = totalHours + static_cast(upTime / 3600); - return CHIP_NO_ERROR; - } - } - - return CHIP_ERROR_INVALID_TIME; -} - -CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons) -{ - uint32_t reason = 0; - - CHIP_ERROR err = ConfigurationMgr().GetBootReasons(reason); - - if (err == CHIP_NO_ERROR) - { - VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - bootReasons = static_cast(reason); - } - - return err; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Ameba/PlatformManagerImpl.h b/src/platform/Ameba/PlatformManagerImpl.h index 86f46b170cb616..25263e05bce66c 100644 --- a/src/platform/Ameba/PlatformManagerImpl.h +++ b/src/platform/Ameba/PlatformManagerImpl.h @@ -47,28 +47,20 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener public: // ===== Platform-specific members that may be accessed directly by the application. - /* none so far */ + System::Clock::Timestamp GetStartTime() { return mStartTime; } private: // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(void); CHIP_ERROR _Shutdown(); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); - - CHIP_ERROR _GetRebootCount(uint16_t & rebootCount); - CHIP_ERROR _GetUpTime(uint64_t & upTime); - CHIP_ERROR _GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR _GetBootReasons(uint8_t & bootReasons); // ===== Members for internal use by the following friends. friend PlatformManager & PlatformMgr(void); friend PlatformManagerImpl & PlatformMgrImpl(void); - chip::System::Clock::Timestamp mStartTime = System::Clock::kZero; + System::Clock::Timestamp mStartTime = System::Clock::kZero; static PlatformManagerImpl sInstance; diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 12bc0db6b2aad2..c3ae225822d412 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -279,6 +279,7 @@ if (chip_device_platform != "none") { "../include/platform/internal/GenericPlatformManagerImpl_POSIX.h", "../include/platform/internal/testing/ConfigUnitTest.h", "DeviceControlServer.cpp", + "DiagnosticDataProvider.cpp", "Entropy.cpp", "GeneralUtils.cpp", "Globals.cpp", diff --git a/src/platform/Darwin/BUILD.gn b/src/platform/Darwin/BUILD.gn index 4e9f4f3220a3ad..49286449375002 100644 --- a/src/platform/Darwin/BUILD.gn +++ b/src/platform/Darwin/BUILD.gn @@ -47,6 +47,8 @@ static_library("Darwin") { "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "DnssdImpl.cpp", "DnssdImpl.h", "InetPlatformConfig.h", diff --git a/src/platform/Darwin/DiagnosticDataProviderImpl.cpp b/src/platform/Darwin/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..e62a7a397cc003 --- /dev/null +++ b/src/platform/Darwin/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for Darwin platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Darwin/DiagnosticDataProviderImpl.h b/src/platform/Darwin/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..731f445fec8671 --- /dev/null +++ b/src/platform/Darwin/DiagnosticDataProviderImpl.h @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Darwin/PlatformManagerImpl.cpp b/src/platform/Darwin/PlatformManagerImpl.cpp index ea11e5afc95b35..7fc0905158b733 100644 --- a/src/platform/Darwin/PlatformManagerImpl.cpp +++ b/src/platform/Darwin/PlatformManagerImpl.cpp @@ -24,6 +24,7 @@ #include +#include #include // Include the non-inline definitions for the GenericPlatformManagerImpl<> template, @@ -44,6 +45,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() err = Internal::PosixConfig::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); mRunLoopSem = dispatch_semaphore_create(0); diff --git a/src/platform/DiagnosticDataProvider.cpp b/src/platform/DiagnosticDataProvider.cpp new file mode 100644 index 00000000000000..2c58eb637e01c8 --- /dev/null +++ b/src/platform/DiagnosticDataProvider.cpp @@ -0,0 +1,53 @@ +/* + * + * 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 + * Implements a getter and setter for a singleton DiagnosticDataProvider object. + */ + +#include + +namespace chip { +namespace DeviceLayer { + +class DiagnosticDataProvider; + +namespace { + +/** Singleton pointer to the DiagnosticDataProvider implementation. + */ +DiagnosticDataProvider * gInstance = nullptr; + +} // namespace + +DiagnosticDataProvider & GetDiagnosticDataProvider() +{ + VerifyOrDie(gInstance != nullptr); + return *gInstance; +} + +void SetDiagnosticDataProvider(DiagnosticDataProvider * diagnosticDataProvider) +{ + if (diagnosticDataProvider != nullptr) + { + gInstance = diagnosticDataProvider; + } +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/EFR32/BUILD.gn b/src/platform/EFR32/BUILD.gn index 26652ffd3ee60e..b0ebb9bbf3ebef 100644 --- a/src/platform/EFR32/BUILD.gn +++ b/src/platform/EFR32/BUILD.gn @@ -40,6 +40,8 @@ static_library("EFR32") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "EFR32Config.cpp", "EFR32Config.h", "InetPlatformConfig.h", diff --git a/src/platform/EFR32/DiagnosticDataProviderImpl.cpp b/src/platform/EFR32/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..cdb3964a0b1c9c --- /dev/null +++ b/src/platform/EFR32/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,81 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for EFR32 platform. + */ + +#include + +#include +#include + +#include + +#include "AppConfig.h" +#include "FreeRTOS.h" + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +/* + * The following Heap stats are compiled values done by the FreeRTOS Heap4 implementation. + * See /examples/platform/efr32/heap_4_silabs.c + * It keeps track of the number of calls to allocate and free memory as well as the + * number of free bytes remaining, but says nothing about fragmentation. + */ +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ + size_t freeHeapSize = xPortGetFreeHeapSize(); + currentHeapFree = static_cast(freeHeapSize); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ + // Calculate the Heap used based on Total heap - Free heap + int64_t heapUsed = (configTOTAL_HEAP_SIZE - xPortGetFreeHeapSize()); + + // Something went wrong, this should not happen + VerifyOrReturnError(heapUsed >= 0, CHIP_ERROR_INVALID_INTEGER_VALUE); + currentHeapUsed = static_cast(heapUsed); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ + // FreeRTOS records the lowest amount of available heap during runtime + // currentHeapHighWatermark wants the highest heap usage point so we calculate it here + int64_t HighestHeapUsageRecorded = (configTOTAL_HEAP_SIZE - xPortGetMinimumEverFreeHeapSize()); + + // Something went wrong, this should not happen + VerifyOrReturnError(HighestHeapUsageRecorded >= 0, CHIP_ERROR_INVALID_INTEGER_VALUE); + currentHeapHighWatermark = static_cast(HighestHeapUsageRecorded); + + return CHIP_NO_ERROR; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/EFR32/DiagnosticDataProviderImpl.h b/src/platform/EFR32/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..6505cf5eedff17 --- /dev/null +++ b/src/platform/EFR32/DiagnosticDataProviderImpl.h @@ -0,0 +1,48 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/EFR32/PlatformManagerImpl.cpp b/src/platform/EFR32/PlatformManagerImpl.cpp index e1c3af86181520..764cf70075ffab 100644 --- a/src/platform/EFR32/PlatformManagerImpl.cpp +++ b/src/platform/EFR32/PlatformManagerImpl.cpp @@ -24,6 +24,7 @@ /* this file behaves like a config.h, comes first */ #include +#include #include #include @@ -45,6 +46,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::EFR32Config::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Initialize LwIP. tcpip_init(NULL, NULL); @@ -58,43 +60,5 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) return err; } -/* - * The following Heap stats are compiled values done by the FreeRTOS Heap4 implementation. - * See /examples/platform/efr32/heap_4_silabs.c - * It keeps track of the number of calls to allocate and free memory as well as the - * number of free bytes remaining, but says nothing about fragmentation. - */ - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - size_t freeHeapSize = xPortGetFreeHeapSize(); - currentHeapFree = static_cast(freeHeapSize); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - // Calculate the Heap used based on Total heap - Free heap - int64_t heapUsed = (configTOTAL_HEAP_SIZE - xPortGetFreeHeapSize()); - - // Something went wrong, this should not happen - VerifyOrReturnError(heapUsed >= 0, CHIP_ERROR_INVALID_INTEGER_VALUE); - currentHeapUsed = static_cast(heapUsed); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - // FreeRTOS records the lowest amount of available heap during runtime - // currentHeapHighWatermark wants the highest heap usage point so we calculate it here - int64_t HighestHeapUsageRecorded = (configTOTAL_HEAP_SIZE - xPortGetMinimumEverFreeHeapSize()); - - // Something went wrong, this should not happen - VerifyOrReturnError(HighestHeapUsageRecorded >= 0, CHIP_ERROR_INVALID_INTEGER_VALUE); - currentHeapHighWatermark = static_cast(HighestHeapUsageRecorded); - - return CHIP_NO_ERROR; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/EFR32/PlatformManagerImpl.h b/src/platform/EFR32/PlatformManagerImpl.h index db4ffe56179d6e..2f7de1d6e5825f 100644 --- a/src/platform/EFR32/PlatformManagerImpl.h +++ b/src/platform/EFR32/PlatformManagerImpl.h @@ -53,9 +53,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(void); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); // ===== Members for internal use by the following friends. diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 4c481560c2d84f..8173f9bddd80b6 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -31,6 +31,8 @@ static_library("ESP32") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "ESP32Config.cpp", "ESP32Config.h", "ESP32Utils.cpp", diff --git a/src/platform/ESP32/DiagnosticDataProviderImpl.cpp b/src/platform/ESP32/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..dd64d9b606d817 --- /dev/null +++ b/src/platform/ESP32/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,144 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for ESP32 platform. + */ + +#include + +#include +#include +#include +#include +#include + +#include "esp_event.h" +#include "esp_heap_caps_init.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_spi_flash.h" +#include "esp_system.h" +#include "esp_wifi.h" + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ + currentHeapFree = esp_get_free_heap_size(); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ + currentHeapUsed = heap_caps_get_total_size(MALLOC_CAP_DEFAULT) - esp_get_free_heap_size(); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ + currentHeapHighWatermark = heap_caps_get_total_size(MALLOC_CAP_DEFAULT) - esp_get_minimum_free_heap_size(); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetRebootCount(uint16_t & rebootCount) +{ + uint32_t count = 0; + + CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); + + if (err == CHIP_NO_ERROR) + { + VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + rebootCount = static_cast(count); + } + + return err; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime) +{ + System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); + System::Clock::Timestamp startTime = PlatformMgrImpl().GetStartTime(); + + if (currentTime >= startTime) + { + upTime = std::chrono::duration_cast(currentTime - startTime).count(); + return CHIP_NO_ERROR; + } + + return CHIP_ERROR_INVALID_TIME; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) +{ + uint64_t upTime = 0; + + if (GetUpTime(upTime) == CHIP_NO_ERROR) + { + uint32_t totalHours = 0; + if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) + { + VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + totalOperationalHours = totalHours + static_cast(upTime / 3600); + return CHIP_NO_ERROR; + } + } + + return CHIP_ERROR_INVALID_TIME; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason) +{ + bootReason = EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED; + uint8_t reason; + reason = static_cast(esp_reset_reason()); + if (reason == ESP_RST_UNKNOWN) + { + bootReason = EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED; + } + else if (reason == ESP_RST_POWERON) + { + bootReason = EMBER_ZCL_BOOT_REASON_TYPE_POWER_ON_REBOOT; + } + else if (reason == ESP_RST_BROWNOUT) + { + bootReason = EMBER_ZCL_BOOT_REASON_TYPE_BROWN_OUT_RESET; + } + else if (reason == ESP_RST_SW) + { + bootReason = EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET; + } + else if (reason == ESP_RST_INT_WDT) + { + bootReason = EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_WATCHDOG_RESET; + /* Reboot can be due to hardware or software watchdog*/ + } + return CHIP_NO_ERROR; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/ESP32/DiagnosticDataProviderImpl.h b/src/platform/ESP32/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..cd33af8885df15 --- /dev/null +++ b/src/platform/ESP32/DiagnosticDataProviderImpl.h @@ -0,0 +1,53 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; + + CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override; + CHIP_ERROR GetUpTime(uint64_t & upTime) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR GetBootReason(uint8_t & bootReason) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index 0d290acef5aecd..d059933e2a3668 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -58,6 +59,7 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); esp_err_t err; // Arrange for CHIP-encapsulated ESP32 errors to be translated to text @@ -129,7 +131,7 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() { uint64_t upTime = 0; - if (_GetUpTime(upTime) == CHIP_NO_ERROR) + if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR) { uint32_t totalOperationalHours = 0; @@ -150,104 +152,6 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() return Internal::GenericPlatformManagerImpl_FreeRTOS::_Shutdown(); } -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - currentHeapFree = esp_get_free_heap_size(); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - currentHeapUsed = heap_caps_get_total_size(MALLOC_CAP_DEFAULT) - esp_get_free_heap_size(); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - currentHeapHighWatermark = heap_caps_get_total_size(MALLOC_CAP_DEFAULT) - esp_get_minimum_free_heap_size(); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) -{ - uint32_t count = 0; - - CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); - - if (err == CHIP_NO_ERROR) - { - VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - rebootCount = static_cast(count); - } - - return err; -} - -CHIP_ERROR PlatformManagerImpl::_GetUpTime(uint64_t & upTime) -{ - System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); - - if (currentTime >= mStartTime) - { - upTime = std::chrono::duration_cast(currentTime - mStartTime).count(); - return CHIP_NO_ERROR; - } - - return CHIP_ERROR_INVALID_TIME; -} - -CHIP_ERROR PlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperationalHours) -{ - uint64_t upTime = 0; - - if (_GetUpTime(upTime) == CHIP_NO_ERROR) - { - uint32_t totalHours = 0; - if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) - { - VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - totalOperationalHours = totalHours + static_cast(upTime / 3600); - return CHIP_NO_ERROR; - } - } - - return CHIP_ERROR_INVALID_TIME; -} - -CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReason) -{ - bootReason = EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED; - uint8_t reason; - reason = static_cast(esp_reset_reason()); - if (reason == ESP_RST_UNKNOWN) - { - bootReason = EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED; - } - else if (reason == ESP_RST_POWERON) - { - bootReason = EMBER_ZCL_BOOT_REASON_TYPE_POWER_ON_REBOOT; - } - else if (reason == ESP_RST_BROWNOUT) - { - bootReason = EMBER_ZCL_BOOT_REASON_TYPE_BROWN_OUT_RESET; - } - else if (reason == ESP_RST_SW) - { - bootReason = EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET; - } - else if (reason == ESP_RST_INT_WDT) - { - bootReason = EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_WATCHDOG_RESET; - /* Reboot can be due to hardware or software watchdog*/ - } - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::InitLwIPCoreLock(void) -{ - return Internal::InitLwIPCoreLock(); -} - void PlatformManagerImpl::HandleESPSystemEvent(void * arg, esp_event_base_t eventBase, int32_t eventId, void * eventData) { ChipDeviceEvent event; diff --git a/src/platform/ESP32/PlatformManagerImpl.h b/src/platform/ESP32/PlatformManagerImpl.h index e29433190e37e4..f5672f992fc21b 100644 --- a/src/platform/ESP32/PlatformManagerImpl.h +++ b/src/platform/ESP32/PlatformManagerImpl.h @@ -51,26 +51,20 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener CHIP_ERROR InitLwIPCoreLock(void); static void HandleESPSystemEvent(void * arg, esp_event_base_t eventBase, int32_t eventId, void * eventData); + System::Clock::Timestamp GetStartTime() { return mStartTime; } private: // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(void); CHIP_ERROR _Shutdown(); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); - - CHIP_ERROR _GetRebootCount(uint16_t & rebootCount); - CHIP_ERROR _GetUpTime(uint64_t & upTime); - CHIP_ERROR _GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR _GetBootReasons(uint8_t & bootReasons); + // ===== Members for internal use by the following friends. friend PlatformManager & PlatformMgr(void); friend PlatformManagerImpl & PlatformMgrImpl(void); - chip::System::Clock::Timestamp mStartTime = System::Clock::kZero; + System::Clock::Timestamp mStartTime = System::Clock::kZero; static PlatformManagerImpl sInstance; }; diff --git a/src/platform/Linux/BUILD.gn b/src/platform/Linux/BUILD.gn index 2c39c68668afcf..6c9bcb122dc072 100644 --- a/src/platform/Linux/BUILD.gn +++ b/src/platform/Linux/BUILD.gn @@ -55,6 +55,8 @@ static_library("Linux") { "ConnectivityUtils.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "InetPlatformConfig.h", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", diff --git a/src/platform/Linux/ConfigurationManagerImpl.cpp b/src/platform/Linux/ConfigurationManagerImpl.cpp index 0e579de76fa61e..75722d2f3e5af0 100644 --- a/src/platform/Linux/ConfigurationManagerImpl.cpp +++ b/src/platform/Linux/ConfigurationManagerImpl.cpp @@ -87,7 +87,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init() if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_BootReason)) { - err = StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED); + err = StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED); SuccessOrExit(err); } @@ -310,14 +310,14 @@ CHIP_ERROR ConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOp return WriteConfigValue(PosixConfig::kCounterKey_TotalOperationalHours, totalOperationalHours); } -CHIP_ERROR ConfigurationManagerImpl::GetBootReasons(uint32_t & bootReasons) +CHIP_ERROR ConfigurationManagerImpl::GetBootReason(uint32_t & bootReason) { - return ReadConfigValue(PosixConfig::kCounterKey_BootReason, bootReasons); + return ReadConfigValue(PosixConfig::kCounterKey_BootReason, bootReason); } -CHIP_ERROR ConfigurationManagerImpl::StoreBootReasons(uint32_t bootReasons) +CHIP_ERROR ConfigurationManagerImpl::StoreBootReason(uint32_t bootReason) { - return WriteConfigValue(PosixConfig::kCounterKey_BootReason, bootReasons); + return WriteConfigValue(PosixConfig::kCounterKey_BootReason, bootReason); } } // namespace DeviceLayer diff --git a/src/platform/Linux/ConfigurationManagerImpl.h b/src/platform/Linux/ConfigurationManagerImpl.h index 80de9b5b1ad0fa..2fa670509ced4f 100644 --- a/src/platform/Linux/ConfigurationManagerImpl.h +++ b/src/platform/Linux/ConfigurationManagerImpl.h @@ -41,8 +41,8 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; - CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override; - CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override; + CHIP_ERROR GetBootReason(uint32_t & bootReason) override; + CHIP_ERROR StoreBootReason(uint32_t bootReason) override; static ConfigurationManagerImpl & GetDefaultInstance(); private: diff --git a/src/platform/Linux/ConnectivityManagerImpl.cpp b/src/platform/Linux/ConnectivityManagerImpl.cpp index 2a822b3d117897..2268e4ce19872b 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.cpp +++ b/src/platform/Linux/ConnectivityManagerImpl.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -1264,7 +1265,7 @@ CHIP_ERROR ConnectivityManagerImpl::_GetEthFullDuplex(bool & fullDuplex) CHIP_ERROR ConnectivityManagerImpl::_GetEthTimeSinceReset(uint64_t & timeSinceReset) { - return PlatformMgr().GetUpTime(timeSinceReset); + return GetDiagnosticDataProvider().GetUpTime(timeSinceReset); } CHIP_ERROR ConnectivityManagerImpl::_GetEthPacketRxCount(uint64_t & packetRxCount) diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.cpp b/src/platform/Linux/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..5d6e173bb3d0e4 --- /dev/null +++ b/src/platform/Linux/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,243 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for Linux platform. + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ + struct mallinfo mallocInfo = mallinfo(); + + // Get the current amount of heap memory, in bytes, that are not being utilized + // by the current running program. + currentHeapFree = mallocInfo.fordblks; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ + struct mallinfo mallocInfo = mallinfo(); + + // Get the current amount of heap memory, in bytes, that are being used by + // the current running program. + currentHeapUsed = mallocInfo.uordblks; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ + struct mallinfo mallocInfo = mallinfo(); + + // The usecase of this function is embedded devices,on which we would need to intercept + // malloc/calloc/free and then record the maximum amount of heap memory,in bytes, that + // has been used by the Node. + // On Linux, since it uses virtual memory, whereby a page of memory could be copied to + // the hard disk, called swap space, and free up that page of memory. So it is impossible + // to know accurately peak physical memory it use. We just return the current heap memory + // being used by the current running program. + currentHeapHighWatermark = mallocInfo.uordblks; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetThreadMetrics(ThreadMetrics ** threadMetricsOut) +{ + CHIP_ERROR err = CHIP_ERROR_READ_FAILED; + DIR * proc_dir = opendir("/proc/self/task"); + + if (proc_dir == nullptr) + { + ChipLogError(DeviceLayer, "Failed to open current process task directory"); + } + else + { + ThreadMetrics * head = nullptr; + struct dirent * entry; + + /* proc available, iterate through tasks... */ + while ((entry = readdir(proc_dir)) != NULL) + { + if (entry->d_name[0] == '.') + continue; + + ThreadMetrics * thread = new ThreadMetrics(); + + strncpy(thread->NameBuf, entry->d_name, kMaxThreadNameLength); + thread->NameBuf[kMaxThreadNameLength] = '\0'; + thread->name = CharSpan(thread->NameBuf, strlen(thread->NameBuf)); + thread->id = atoi(entry->d_name); + + // TODO: Get stack info of each thread + thread->stackFreeCurrent = 0; + thread->stackFreeMinimum = 0; + thread->stackSize = 0; + + thread->Next = head; + head = thread; + } + + closedir(proc_dir); + + *threadMetricsOut = head; + err = CHIP_NO_ERROR; + } + + return err; +} + +void DiagnosticDataProviderImpl::ReleaseThreadMetrics(ThreadMetrics * threadMetrics) +{ + while (threadMetrics) + { + ThreadMetrics * del = threadMetrics; + threadMetrics = threadMetrics->Next; + delete del; + } +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetRebootCount(uint16_t & rebootCount) +{ + uint32_t count = 0; + + CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); + + if (err == CHIP_NO_ERROR) + { + VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + rebootCount = static_cast(count); + } + + return err; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime) +{ + System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); + System::Clock::Timestamp startTime = PlatformMgrImpl().GetStartTime(); + + if (currentTime >= startTime) + { + upTime = std::chrono::duration_cast(currentTime - startTime).count(); + return CHIP_NO_ERROR; + } + + return CHIP_ERROR_INVALID_TIME; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) +{ + uint64_t upTime = 0; + + if (GetUpTime(upTime) == CHIP_NO_ERROR) + { + uint32_t totalHours = 0; + if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) + { + VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + totalOperationalHours = totalHours + static_cast(upTime / 3600); + } + } + + return CHIP_ERROR_INVALID_TIME; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason) +{ + uint32_t reason = 0; + + CHIP_ERROR err = ConfigurationMgr().GetBootReason(reason); + + if (err == CHIP_NO_ERROR) + { + VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); + bootReason = static_cast(reason); + } + + return err; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetActiveHardwareFaults(GeneralFaults & hardwareFaults) +{ +#if CHIP_CONFIG_TEST + // On Linux Simulation, set following hardware faults statically. + ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_RADIO)); + ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_SENSOR)); + ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_POWER_SOURCE)); + ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_USER_INTERFACE_FAULT)); +#endif + + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetActiveRadioFaults(GeneralFaults & radioFaults) +{ +#if CHIP_CONFIG_TEST + // On Linux Simulation, set following radio faults statically. + ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_WI_FI_FAULT)); + ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_CELLULAR_FAULT)); + ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_THREAD_FAULT)); + ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_NFC_FAULT)); +#endif + + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetActiveNetworkFaults(GeneralFaults & networkFaults) +{ +#if CHIP_CONFIG_TEST + // On Linux Simulation, set following radio faults statically. + ReturnErrorOnFailure(networkFaults.add(EMBER_ZCL_NETWORK_FAULT_TYPE_HARDWARE_FAILURE)); + ReturnErrorOnFailure(networkFaults.add(EMBER_ZCL_NETWORK_FAULT_TYPE_NETWORK_JAMMED)); + ReturnErrorOnFailure(networkFaults.add(EMBER_ZCL_NETWORK_FAULT_TYPE_CONNECTION_FAILED)); +#endif + + return CHIP_NO_ERROR; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.h b/src/platform/Linux/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..f58c6d712d81cf --- /dev/null +++ b/src/platform/Linux/DiagnosticDataProviderImpl.h @@ -0,0 +1,59 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; + CHIP_ERROR GetThreadMetrics(ThreadMetrics ** threadMetricsOut) override; + void ReleaseThreadMetrics(ThreadMetrics * threadMetrics) override; + + CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override; + CHIP_ERROR GetUpTime(uint64_t & upTime) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR GetBootReason(uint8_t & bootReason) override; + + CHIP_ERROR GetActiveHardwareFaults(GeneralFaults & hardwareFaults) override; + CHIP_ERROR GetActiveRadioFaults(GeneralFaults & radioFaults) override; + CHIP_ERROR GetActiveNetworkFaults(GeneralFaults & networkFaults) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index ad5f5c677089d3..cf0faa78ebe0bb 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -36,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -61,27 +61,27 @@ void SignalHandler(int signum) switch (signum) { case SIGINT: - ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET); + ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGHUP: - ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_BROWN_OUT_RESET); + ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_BROWN_OUT_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGTERM: - ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_POWER_ON_REBOOT); + ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_POWER_ON_REBOOT); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGUSR1: - ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_HARDWARE_WATCHDOG_RESET); + ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_HARDWARE_WATCHDOG_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGUSR2: - ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_WATCHDOG_RESET); + ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_WATCHDOG_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGTSTP: - ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_UPDATE_COMPLETED); + ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_UPDATE_COMPLETED); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; default: @@ -211,6 +211,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() err = Internal::PosixConfig::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); + // Call _InitChipStack() on the generic implementation base class // to finish the initialization process. err = Internal::GenericPlatformManagerImpl_POSIX::_InitChipStack(); @@ -228,7 +230,7 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() { uint64_t upTime = 0; - if (_GetUpTime(upTime) == CHIP_NO_ERROR) + if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR) { uint32_t totalOperationalHours = 0; @@ -249,200 +251,9 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() return Internal::GenericPlatformManagerImpl_POSIX::_Shutdown(); } -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - struct mallinfo mallocInfo = mallinfo(); - - // Get the current amount of heap memory, in bytes, that are not being utilized - // by the current running program. - currentHeapFree = mallocInfo.fordblks; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - struct mallinfo mallocInfo = mallinfo(); - - // Get the current amount of heap memory, in bytes, that are being used by - // the current running program. - currentHeapUsed = mallocInfo.uordblks; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - struct mallinfo mallocInfo = mallinfo(); - - // The usecase of this function is embedded devices,on which we would need to intercept - // malloc/calloc/free and then record the maximum amount of heap memory,in bytes, that - // has been used by the Node. - // On Linux, since it uses virtual memory, whereby a page of memory could be copied to - // the hard disk, called swap space, and free up that page of memory. So it is impossible - // to know accurately peak physical memory it use. We just return the current heap memory - // being used by the current running program. - currentHeapHighWatermark = mallocInfo.uordblks; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetThreadMetrics(ThreadMetrics ** threadMetricsOut) -{ - CHIP_ERROR err = CHIP_ERROR_READ_FAILED; - DIR * proc_dir = opendir("/proc/self/task"); - - if (proc_dir == nullptr) - { - ChipLogError(DeviceLayer, "Failed to open current process task directory"); - } - else - { - ThreadMetrics * head = nullptr; - struct dirent * entry; - - /* proc available, iterate through tasks... */ - while ((entry = readdir(proc_dir)) != NULL) - { - if (entry->d_name[0] == '.') - continue; - - ThreadMetrics * thread = new ThreadMetrics(); - - strncpy(thread->NameBuf, entry->d_name, kMaxThreadNameLength); - thread->NameBuf[kMaxThreadNameLength] = '\0'; - thread->name = CharSpan(thread->NameBuf, strlen(thread->NameBuf)); - thread->id = atoi(entry->d_name); - - // TODO: Get stack info of each thread - thread->stackFreeCurrent = 0; - thread->stackFreeMinimum = 0; - thread->stackSize = 0; - - thread->Next = head; - head = thread; - } - - closedir(proc_dir); - - *threadMetricsOut = head; - err = CHIP_NO_ERROR; - } - - return err; -} - -void PlatformManagerImpl::_ReleaseThreadMetrics(ThreadMetrics * threadMetrics) -{ - while (threadMetrics) - { - ThreadMetrics * del = threadMetrics; - threadMetrics = threadMetrics->Next; - delete del; - } -} - -CHIP_ERROR PlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) -{ - uint32_t count = 0; - - CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); - - if (err == CHIP_NO_ERROR) - { - VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - rebootCount = static_cast(count); - } - - return err; -} - -CHIP_ERROR PlatformManagerImpl::_GetUpTime(uint64_t & upTime) -{ - System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); - - if (currentTime >= mStartTime) - { - upTime = std::chrono::duration_cast(currentTime - mStartTime).count(); - return CHIP_NO_ERROR; - } - - return CHIP_ERROR_INVALID_TIME; -} - -CHIP_ERROR PlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperationalHours) -{ - uint64_t upTime = 0; - - if (_GetUpTime(upTime) == CHIP_NO_ERROR) - { - uint32_t totalHours = 0; - if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) - { - VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - totalOperationalHours = totalHours + static_cast(upTime / 3600); - } - } - - return CHIP_ERROR_INVALID_TIME; -} - -CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons) -{ - uint32_t reason = 0; - - CHIP_ERROR err = ConfigurationMgr().GetBootReasons(reason); - - if (err == CHIP_NO_ERROR) - { - VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); - bootReasons = static_cast(reason); - } - - return err; -} - -CHIP_ERROR PlatformManagerImpl::_GetActiveHardwareFaults(GeneralFaults & hardwareFaults) -{ -#if CHIP_CONFIG_TEST - // On Linux Simulation, set following hardware faults statically. - ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_RADIO)); - ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_SENSOR)); - ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_POWER_SOURCE)); - ReturnErrorOnFailure(hardwareFaults.add(EMBER_ZCL_HARDWARE_FAULT_TYPE_USER_INTERFACE_FAULT)); -#endif - - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetActiveRadioFaults(GeneralFaults & radioFaults) -{ -#if CHIP_CONFIG_TEST - // On Linux Simulation, set following radio faults statically. - ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_WI_FI_FAULT)); - ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_CELLULAR_FAULT)); - ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_THREAD_FAULT)); - ReturnErrorOnFailure(radioFaults.add(EMBER_ZCL_RADIO_FAULT_TYPE_NFC_FAULT)); -#endif - - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetActiveNetworkFaults(GeneralFaults & networkFaults) -{ -#if CHIP_CONFIG_TEST - // On Linux Simulation, set following radio faults statically. - ReturnErrorOnFailure(networkFaults.add(EMBER_ZCL_NETWORK_FAULT_TYPE_HARDWARE_FAILURE)); - ReturnErrorOnFailure(networkFaults.add(EMBER_ZCL_NETWORK_FAULT_TYPE_NETWORK_JAMMED)); - ReturnErrorOnFailure(networkFaults.add(EMBER_ZCL_NETWORK_FAULT_TYPE_CONNECTION_FAILED)); -#endif - - return CHIP_NO_ERROR; -} - void PlatformManagerImpl::HandleDeviceRebooted(intptr_t arg) { - PlatformManagerDelegate * delegate = PlatformMgr().GetDelegate(); + DiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetDelegate(); if (delegate != nullptr) { diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index a1a865e531d7aa..96f241fbb533ed 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -23,8 +23,6 @@ #pragma once -#include - #include #include @@ -56,25 +54,13 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener GDBusConnection * GetGDBusConnection(); #endif + System::Clock::Timestamp GetStartTime() { return mStartTime; } + private: // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(); CHIP_ERROR _Shutdown(); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); - CHIP_ERROR _GetThreadMetrics(ThreadMetrics ** threadMetricsOut); - void _ReleaseThreadMetrics(ThreadMetrics * threadMetrics); - - CHIP_ERROR _GetRebootCount(uint16_t & rebootCount); - CHIP_ERROR _GetUpTime(uint64_t & upTime); - CHIP_ERROR _GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR _GetBootReasons(uint8_t & bootReasons); - - CHIP_ERROR _GetActiveHardwareFaults(GeneralFaults & hardwareFaults); - CHIP_ERROR _GetActiveRadioFaults(GeneralFaults & radioFaults); - CHIP_ERROR _GetActiveNetworkFaults(GeneralFaults & networkFaults); // ===== Members for internal use by the following friends. @@ -116,7 +102,7 @@ inline PlatformManager & PlatformMgr() * Returns the platform-specific implementation of the PlatformManager singleton object. * * chip applications can use this to gain access to features of the PlatformManager - * that are specific to the ESP32 platform. + * that are specific to the platform. */ inline PlatformManagerImpl & PlatformMgrImpl() { diff --git a/src/platform/P6/BUILD.gn b/src/platform/P6/BUILD.gn index 4ee27012f2ac3e..f33ef8e443539c 100644 --- a/src/platform/P6/BUILD.gn +++ b/src/platform/P6/BUILD.gn @@ -39,6 +39,8 @@ static_library("P6") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "InetPlatformConfig.h", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", diff --git a/src/platform/P6/DiagnosticDataProviderImpl.cpp b/src/platform/P6/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..02593d420e2501 --- /dev/null +++ b/src/platform/P6/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for P6 platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/P6/DiagnosticDataProviderImpl.h b/src/platform/P6/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..731f445fec8671 --- /dev/null +++ b/src/platform/P6/DiagnosticDataProviderImpl.h @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/P6/PlatformManagerImpl.cpp b/src/platform/P6/PlatformManagerImpl.cpp index c9503f243e1da7..24272b1dae6b89 100644 --- a/src/platform/P6/PlatformManagerImpl.cpp +++ b/src/platform/P6/PlatformManagerImpl.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -43,6 +44,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) CHIP_ERROR err; SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Make sure the LwIP core lock has been initialized err = Internal::InitLwIPCoreLock(); diff --git a/src/platform/Tizen/BUILD.gn b/src/platform/Tizen/BUILD.gn index 64002e10607876..18048372c24321 100644 --- a/src/platform/Tizen/BUILD.gn +++ b/src/platform/Tizen/BUILD.gn @@ -43,6 +43,8 @@ static_library("Tizen") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "InetPlatformConfig.h", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", diff --git a/src/platform/Tizen/DiagnosticDataProviderImpl.cpp b/src/platform/Tizen/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..c1513741e5e925 --- /dev/null +++ b/src/platform/Tizen/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for Tizen platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Tizen/DiagnosticDataProviderImpl.h b/src/platform/Tizen/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..731f445fec8671 --- /dev/null +++ b/src/platform/Tizen/DiagnosticDataProviderImpl.h @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Tizen/PlatformManagerImpl.cpp b/src/platform/Tizen/PlatformManagerImpl.cpp index bd4abfa8c75b0d..d678f81fbc61ba 100644 --- a/src/platform/Tizen/PlatformManagerImpl.cpp +++ b/src/platform/Tizen/PlatformManagerImpl.cpp @@ -26,6 +26,7 @@ #include #include +#include #include namespace chip { @@ -37,6 +38,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { ReturnErrorOnFailure(Internal::PosixConfig::Init()); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); return Internal::GenericPlatformManagerImpl_POSIX::_InitChipStack(); } diff --git a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..7ac6665dcbdd26 --- /dev/null +++ b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,78 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for Zephy platform. + */ + +#include + +#include +#include +#include + +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ +#ifdef CONFIG_NEWLIB_LIBC + // This will return the amount of memory which has been allocated from the system, but is not + // used right now. Ideally, this value should be increased by the amount of memory which can + // be allocated from the system, but Zephyr does not expose that number. + currentHeapFree = mallinfo().fordblks; + return CHIP_NO_ERROR; +#else + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +#endif +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ +#ifdef CONFIG_NEWLIB_LIBC + currentHeapUsed = mallinfo().uordblks; + return CHIP_NO_ERROR; +#else + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +#endif +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ +#ifdef CONFIG_NEWLIB_LIBC + // ARM newlib does not provide a way to obtain the peak heap usage, so for now just return + // the amount of memory allocated from the system which should be an upper bound of the peak + // usage provided that the heap is not very fragmented. + currentHeapHighWatermark = mallinfo().arena; + return CHIP_NO_ERROR; +#else + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +#endif +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Zephyr/DiagnosticDataProviderImpl.h b/src/platform/Zephyr/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..6505cf5eedff17 --- /dev/null +++ b/src/platform/Zephyr/DiagnosticDataProviderImpl.h @@ -0,0 +1,48 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index 09f388058c0c03..1efda5025fa61c 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -73,6 +74,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::ZephyrConfig::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); #if !CONFIG_NORDIC_SECURITY_BACKEND // Add entropy source based on Zephyr entropy driver @@ -88,41 +90,5 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) return err; } -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ -#ifdef CONFIG_NEWLIB_LIBC - // This will return the amount of memory which has been allocated from the system, but is not - // used right now. Ideally, this value should be increased by the amount of memory which can - // be allocated from the system, but Zephyr does not expose that number. - currentHeapFree = mallinfo().fordblks; - return CHIP_NO_ERROR; -#else - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -#endif -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ -#ifdef CONFIG_NEWLIB_LIBC - currentHeapUsed = mallinfo().uordblks; - return CHIP_NO_ERROR; -#else - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -#endif -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ -#ifdef CONFIG_NEWLIB_LIBC - // ARM newlib does not provide a way to obtain the peak heap usage, so for now just return - // the amount of memory allocated from the system which should be an upper bound of the peak - // usage provided that the heap is not very fragmented. - currentHeapHighWatermark = mallinfo().arena; - return CHIP_NO_ERROR; -#else - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -#endif -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Zephyr/PlatformManagerImpl.h b/src/platform/Zephyr/PlatformManagerImpl.h index bf07fa1c0e1f35..cdc924029f3847 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.h +++ b/src/platform/Zephyr/PlatformManagerImpl.h @@ -52,9 +52,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(void); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); // ===== Members for internal use by the following friends. diff --git a/src/platform/android/BUILD.gn b/src/platform/android/BUILD.gn index 1b6cc5ea3313eb..dcdb3690ab048c 100644 --- a/src/platform/android/BUILD.gn +++ b/src/platform/android/BUILD.gn @@ -42,6 +42,8 @@ static_library("android") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "DnssdImpl.cpp", "DnssdImpl.h", "KeyValueStoreManagerImpl.cpp", diff --git a/src/platform/android/DiagnosticDataProviderImpl.cpp b/src/platform/android/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..a7015f00895253 --- /dev/null +++ b/src/platform/android/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,41 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for android platform. + */ + +#include + +#include +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/android/DiagnosticDataProviderImpl.h b/src/platform/android/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..731f445fec8671 --- /dev/null +++ b/src/platform/android/DiagnosticDataProviderImpl.h @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/android/PlatformManagerImpl.cpp b/src/platform/android/PlatformManagerImpl.cpp index 50f5d04be48420..2bc2597cfcc7e0 100644 --- a/src/platform/android/PlatformManagerImpl.cpp +++ b/src/platform/android/PlatformManagerImpl.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -44,6 +45,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() err = Internal::AndroidConfig::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Call _InitChipStack() on the generic implementation base class // to finish the initialization process. diff --git a/src/platform/cc13x2_26x2/BUILD.gn b/src/platform/cc13x2_26x2/BUILD.gn index db40cfb1918db3..5eb53855aa2244 100644 --- a/src/platform/cc13x2_26x2/BUILD.gn +++ b/src/platform/cc13x2_26x2/BUILD.gn @@ -37,6 +37,8 @@ static_library("cc13x2_26x2") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "InetPlatformConfig.h", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", diff --git a/src/platform/cc13x2_26x2/DiagnosticDataProviderImpl.cpp b/src/platform/cc13x2_26x2/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..33eb5db4ab1a28 --- /dev/null +++ b/src/platform/cc13x2_26x2/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for cc13x2 platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/cc13x2_26x2/DiagnosticDataProviderImpl.h b/src/platform/cc13x2_26x2/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..731f445fec8671 --- /dev/null +++ b/src/platform/cc13x2_26x2/DiagnosticDataProviderImpl.h @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp b/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp index 07178e46e3c866..fbc1d3fe3282af 100644 --- a/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp +++ b/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -107,6 +108,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::CC13X2_26X2Config::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // DMM Addition DMMPolicy_Params dmmPolicyParams; diff --git a/src/platform/fake/BUILD.gn b/src/platform/fake/BUILD.gn index 9c4d008f11300f..ad2a45b51ea2fb 100644 --- a/src/platform/fake/BUILD.gn +++ b/src/platform/fake/BUILD.gn @@ -25,6 +25,8 @@ static_library("fake") { "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "DnssdImpl.cpp", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", diff --git a/src/platform/fake/ConfigurationManagerImpl.h b/src/platform/fake/ConfigurationManagerImpl.h index 4d4cce0a0b9397..f4653a96bfbd42 100644 --- a/src/platform/fake/ConfigurationManagerImpl.h +++ b/src/platform/fake/ConfigurationManagerImpl.h @@ -87,8 +87,8 @@ class ConfigurationManagerImpl : public ConfigurationManager CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override { return CHIP_ERROR_NOT_IMPLEMENTED; } CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override { return CHIP_ERROR_NOT_IMPLEMENTED; } CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override { return CHIP_ERROR_NOT_IMPLEMENTED; } - CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override { return CHIP_ERROR_NOT_IMPLEMENTED; } - CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR GetBootReason(uint32_t & bootReason) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR StoreBootReason(uint32_t bootReason) override { return CHIP_ERROR_NOT_IMPLEMENTED; } #if !defined(NDEBUG) CHIP_ERROR RunUnitTests(void) override { return CHIP_ERROR_NOT_IMPLEMENTED; } #endif diff --git a/src/platform/fake/DiagnosticDataProviderImpl.cpp b/src/platform/fake/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..6158e9bca2483e --- /dev/null +++ b/src/platform/fake/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for Linux platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/fake/DiagnosticDataProviderImpl.h b/src/platform/fake/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..f58c6d712d81cf --- /dev/null +++ b/src/platform/fake/DiagnosticDataProviderImpl.h @@ -0,0 +1,59 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; + CHIP_ERROR GetThreadMetrics(ThreadMetrics ** threadMetricsOut) override; + void ReleaseThreadMetrics(ThreadMetrics * threadMetrics) override; + + CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override; + CHIP_ERROR GetUpTime(uint64_t & upTime) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR GetBootReason(uint8_t & bootReason) override; + + CHIP_ERROR GetActiveHardwareFaults(GeneralFaults & hardwareFaults) override; + CHIP_ERROR GetActiveRadioFaults(GeneralFaults & radioFaults) override; + CHIP_ERROR GetActiveNetworkFaults(GeneralFaults & networkFaults) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/fake/PlatformManagerImpl.h b/src/platform/fake/PlatformManagerImpl.h index fa69fb892052cc..5ab67dc50f0b54 100644 --- a/src/platform/fake/PlatformManagerImpl.h +++ b/src/platform/fake/PlatformManagerImpl.h @@ -102,21 +102,6 @@ class PlatformManagerImpl final : public PlatformManager bool _TryLockChipStack() { return true; } void _UnlockChipStack() {} - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); - CHIP_ERROR _GetThreadMetrics(ThreadMetrics ** threadMetricsOut); - void _ReleaseThreadMetrics(ThreadMetrics * threadMetrics); - - CHIP_ERROR _GetRebootCount(uint16_t & rebootCount); - CHIP_ERROR _GetUpTime(uint64_t & upTime); - CHIP_ERROR _GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR _GetBootReasons(uint8_t & bootReasons); - - CHIP_ERROR _GetActiveHardwareFaults(GeneralFaults & hardwareFaults); - CHIP_ERROR _GetActiveRadioFaults(GeneralFaults & radioFaults); - CHIP_ERROR _GetActiveNetworkFaults(GeneralFaults & networkFaults); - // ===== Members for internal use by the following friends. friend PlatformManager & PlatformMgr(); diff --git a/src/platform/mbed/BUILD.gn b/src/platform/mbed/BUILD.gn index beee849ddc7f41..d590d96410b5a1 100644 --- a/src/platform/mbed/BUILD.gn +++ b/src/platform/mbed/BUILD.gn @@ -28,6 +28,8 @@ static_library("mbed") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "KeyValueStoreManagerImpl.cpp", "KeyValueStoreManagerImpl.h", "Logging.cpp", diff --git a/src/platform/mbed/DiagnosticDataProviderImpl.cpp b/src/platform/mbed/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..f7b2dd0aa4bed5 --- /dev/null +++ b/src/platform/mbed/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for mbed platform. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/mbed/DiagnosticDataProviderImpl.h b/src/platform/mbed/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..731f445fec8671 --- /dev/null +++ b/src/platform/mbed/DiagnosticDataProviderImpl.h @@ -0,0 +1,40 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/mbed/PlatformManagerImpl.cpp b/src/platform/mbed/PlatformManagerImpl.cpp index a76ea42dda9138..740aeaf8826c88 100644 --- a/src/platform/mbed/PlatformManagerImpl.cpp +++ b/src/platform/mbed/PlatformManagerImpl.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "MbedEventTimeout.h" @@ -92,6 +93,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) #endif SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Call up to the base class _InitChipStack() to perform the bulk of the initialization. auto err = GenericPlatformManagerImpl::_InitChipStack(); diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 83f00cf1d82e30..d2296794185015 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -23,6 +23,8 @@ static_library("nrfconnect") { "../SingletonConfigurationManager.cpp", "../Zephyr/BLEManagerImpl.cpp", "../Zephyr/ConfigurationManagerImpl.cpp", + "../Zephyr/DiagnosticDataProviderImpl.cpp", + "../Zephyr/DiagnosticDataProviderImpl.h", "../Zephyr/KeyValueStoreManagerImpl.cpp", "../Zephyr/Logging.cpp", "../Zephyr/PlatformManagerImpl.cpp", diff --git a/src/platform/nxp/k32w/k32w0/BUILD.gn b/src/platform/nxp/k32w/k32w0/BUILD.gn index 0a37f9caf71408..3c44882b296b42 100644 --- a/src/platform/nxp/k32w/k32w0/BUILD.gn +++ b/src/platform/nxp/k32w/k32w0/BUILD.gn @@ -36,6 +36,8 @@ static_library("k32w0") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "K32W0Config.cpp", "K32W0Config.h", "KeyValueStoreManagerImpl.cpp", diff --git a/src/platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.cpp b/src/platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..9702e4109ad280 --- /dev/null +++ b/src/platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,74 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for k32w0 platform. + */ + +#include + +#include +#include +#include + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ + size_t freeHeapSize; + + freeHeapSize = xPortGetFreeHeapSize(); + currentHeapFree = static_cast(freeHeapSize); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ + size_t freeHeapSize; + size_t usedHeapSize; + + freeHeapSize = xPortGetFreeHeapSize(); + usedHeapSize = HEAP_SIZE - freeHeapSize; + + currentHeapUsed = static_cast(usedHeapSize); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ + size_t highWatermarkHeapSize; + + highWatermarkHeapSize = HEAP_SIZE - xPortGetMinimumEverFreeHeapSize(); + currentHeapHighWatermark = static_cast(highWatermarkHeapSize); + return CHIP_NO_ERROR; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.h b/src/platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..6505cf5eedff17 --- /dev/null +++ b/src/platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.h @@ -0,0 +1,48 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp index 567274e3052ca8..354550e805d144 100644 --- a/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -59,6 +60,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::K32WConfig::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Initialize LwIP. tcpip_init(NULL, NULL); @@ -75,35 +77,5 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) return err; } -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - size_t freeHeapSize; - - freeHeapSize = xPortGetFreeHeapSize(); - currentHeapFree = static_cast(freeHeapSize); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - size_t freeHeapSize; - size_t usedHeapSize; - - freeHeapSize = xPortGetFreeHeapSize(); - usedHeapSize = HEAP_SIZE - freeHeapSize; - - currentHeapUsed = static_cast(usedHeapSize); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - size_t highWatermarkHeapSize; - - highWatermarkHeapSize = HEAP_SIZE - xPortGetMinimumEverFreeHeapSize(); - currentHeapHighWatermark = static_cast(highWatermarkHeapSize); - return CHIP_NO_ERROR; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.h b/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.h index 29c17d6f9cdbcc..4e0e355b75bdc2 100644 --- a/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.h +++ b/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.h @@ -54,9 +54,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(void); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); // ===== Members for internal use by the following friends. diff --git a/src/platform/qpg/BUILD.gn b/src/platform/qpg/BUILD.gn index c3526b4756b838..e5d6d35f2e068c 100644 --- a/src/platform/qpg/BUILD.gn +++ b/src/platform/qpg/BUILD.gn @@ -38,6 +38,8 @@ static_library("qpg") { "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", "InetPlatformConfig.h", "Logging.cpp", "PlatformManagerImpl.cpp", diff --git a/src/platform/qpg/DiagnosticDataProviderImpl.cpp b/src/platform/qpg/DiagnosticDataProviderImpl.cpp new file mode 100644 index 00000000000000..eeaf719c946a4f --- /dev/null +++ b/src/platform/qpg/DiagnosticDataProviderImpl.cpp @@ -0,0 +1,75 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object + * for qpg platform. + */ + +#include + +#include +#include +#include + +#include + +namespace chip { +namespace DeviceLayer { + +DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() +{ + static DiagnosticDataProviderImpl sInstance; + return sInstance; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) +{ + size_t freeHeapSize; + size_t usedHeapSize; + size_t highWatermarkHeapSize; + + qvCHIP_GetHeapStats(&freeHeapSize, &usedHeapSize, &highWatermarkHeapSize); + currentHeapFree = static_cast(freeHeapSize); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) +{ + size_t freeHeapSize; + size_t usedHeapSize; + size_t highWatermarkHeapSize; + + qvCHIP_GetHeapStats(&freeHeapSize, &usedHeapSize, &highWatermarkHeapSize); + currentHeapUsed = static_cast(usedHeapSize); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) +{ + size_t freeHeapSize; + size_t usedHeapSize; + size_t highWatermarkHeapSize; + + qvCHIP_GetHeapStats(&freeHeapSize, &usedHeapSize, &highWatermarkHeapSize); + currentHeapHighWatermark = static_cast(highWatermarkHeapSize); + return CHIP_NO_ERROR; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/qpg/DiagnosticDataProviderImpl.h b/src/platform/qpg/DiagnosticDataProviderImpl.h new file mode 100644 index 00000000000000..6505cf5eedff17 --- /dev/null +++ b/src/platform/qpg/DiagnosticDataProviderImpl.h @@ -0,0 +1,48 @@ +/* + * + * 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 + * Provides an implementation of the DiagnosticDataProvider object. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the PlatformManager singleton object for Linux platforms. + */ +class DiagnosticDataProviderImpl : public DiagnosticDataProvider +{ +public: + static DiagnosticDataProviderImpl & GetDefaultInstance(); + + // ===== Methods that implement the PlatformManager abstract interface. + + CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override; + CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override; + CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/qpg/PlatformManagerImpl.cpp b/src/platform/qpg/PlatformManagerImpl.cpp index ff6e38aafc853d..00f38d4bd92068 100644 --- a/src/platform/qpg/PlatformManagerImpl.cpp +++ b/src/platform/qpg/PlatformManagerImpl.cpp @@ -25,6 +25,7 @@ #include #include +#include #include @@ -41,6 +42,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::QPGConfig::Init(); SuccessOrExit(err); SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance()); // Initialize LwIP. tcpip_init(NULL, NULL); @@ -54,38 +56,5 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) return err; } -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree) -{ - size_t freeHeapSize; - size_t usedHeapSize; - size_t highWatermarkHeapSize; - - qvCHIP_GetHeapStats(&freeHeapSize, &usedHeapSize, &highWatermarkHeapSize); - currentHeapFree = static_cast(freeHeapSize); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed) -{ - size_t freeHeapSize; - size_t usedHeapSize; - size_t highWatermarkHeapSize; - - qvCHIP_GetHeapStats(&freeHeapSize, &usedHeapSize, &highWatermarkHeapSize); - currentHeapUsed = static_cast(usedHeapSize); - return CHIP_NO_ERROR; -} - -CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) -{ - size_t freeHeapSize; - size_t usedHeapSize; - size_t highWatermarkHeapSize; - - qvCHIP_GetHeapStats(&freeHeapSize, &usedHeapSize, &highWatermarkHeapSize); - currentHeapHighWatermark = static_cast(highWatermarkHeapSize); - return CHIP_NO_ERROR; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/qpg/PlatformManagerImpl.h b/src/platform/qpg/PlatformManagerImpl.h index 968410f6bd1619..ba8403486c4af7 100644 --- a/src/platform/qpg/PlatformManagerImpl.h +++ b/src/platform/qpg/PlatformManagerImpl.h @@ -52,9 +52,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener // ===== Methods that implement the PlatformManager abstract interface. CHIP_ERROR _InitChipStack(void); - CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree); - CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed); - CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark); // ===== Members for internal use by the following friends. diff --git a/src/platform/telink/BUILD.gn b/src/platform/telink/BUILD.gn index 539a8d93775747..b4c5c22962ac3e 100644 --- a/src/platform/telink/BUILD.gn +++ b/src/platform/telink/BUILD.gn @@ -23,6 +23,8 @@ static_library("telink") { "../SingletonConfigurationManager.cpp", "../Zephyr/BLEManagerImpl.cpp", "../Zephyr/ConfigurationManagerImpl.cpp", + "../Zephyr/DiagnosticDataProviderImpl.cpp", + "../Zephyr/DiagnosticDataProviderImpl.h", "../Zephyr/KeyValueStoreManagerImpl.cpp", "../Zephyr/Logging.cpp", "../Zephyr/PlatformManagerImpl.cpp",