diff --git a/examples/platform/silabs/MatterConfig.cpp b/examples/platform/silabs/MatterConfig.cpp index 9c2253233f79e7..f5845b30cd12d4 100644 --- a/examples/platform/silabs/MatterConfig.cpp +++ b/examples/platform/silabs/MatterConfig.cpp @@ -56,6 +56,14 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys #include "SilabsDeviceDataProvider.h" #include "SilabsTestEventTriggerDelegate.h" #include +#include + +#if CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED +#include +#else +#include +#endif + #include #ifdef CHIP_CONFIG_USE_ICD_SUBSCRIPTION_CALLBACKS @@ -186,8 +194,19 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName) chip::DeviceLayer::PlatformMgr().LockChipStack(); // Create initParams with SDK example defaults here + // TODO: replace with our own init param to avoid double allocation in examples static chip::CommonCaseDeviceServerInitParams initParams; + // Report scheduler and timer delegate instance + static chip::app::DefaultTimerDelegate sTimerDelegate; +#if CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED + static chip::app::reporting::SynchronizedReportSchedulerImpl sReportScheduler(&sTimerDelegate); +#else + static chip::app::reporting::ReportSchedulerImpl sReportScheduler(&sTimerDelegate); +#endif + + initParams.reportScheduler = &sReportScheduler; + #if SILABS_TEST_EVENT_TRIGGER_ENABLED if (Encoding::HexToBytes(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY, strlen(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY), sTestEventTriggerEnableKey, diff --git a/src/app/BUILD.gn b/src/app/BUILD.gn index 312a53aa6ee1a7..cc380ddf680c71 100644 --- a/src/app/BUILD.gn +++ b/src/app/BUILD.gn @@ -191,6 +191,8 @@ static_library("app") { "TimedHandler.h", "TimedRequest.cpp", "TimedRequest.h", + "TimerDelegates.cpp", + "TimerDelegates.h", "WriteClient.cpp", "WriteHandler.cpp", "reporting/Engine.cpp", diff --git a/src/app/TimerDelegates.cpp b/src/app/TimerDelegates.cpp new file mode 100644 index 00000000000000..d41503d0947d04 --- /dev/null +++ b/src/app/TimerDelegates.cpp @@ -0,0 +1,55 @@ +/* + * + * Copyright (c) 2023 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. + */ + +#include +#include +#include + +using TimerContext = chip::app::reporting::TimerContext; +using Timeout = chip::System::Clock::Timeout; + +namespace chip { +namespace app { + +static void TimerCallbackInterface(System::Layer * aLayer, void * aAppState) +{ + TimerContext * context = static_cast(aAppState); + context->TimerFired(); +} +CHIP_ERROR DefaultTimerDelegate::StartTimer(TimerContext * context, Timeout aTimeout) +{ + return InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->StartTimer( + aTimeout, TimerCallbackInterface, context); +} +void DefaultTimerDelegate::CancelTimer(TimerContext * context) +{ + InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->CancelTimer( + TimerCallbackInterface, context); +} +bool DefaultTimerDelegate::IsTimerActive(TimerContext * context) +{ + return InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->IsTimerActive( + TimerCallbackInterface, context); +} + +System::Clock::Timestamp DefaultTimerDelegate::GetCurrentMonotonicTimestamp() +{ + return System::SystemClock().GetMonotonicTimestamp(); +} + +} // namespace app +} // namespace chip diff --git a/src/app/TimerDelegates.h b/src/app/TimerDelegates.h index 8e24fd4aef09bd..563ee138d9199f 100644 --- a/src/app/TimerDelegates.h +++ b/src/app/TimerDelegates.h @@ -17,7 +17,6 @@ #pragma once -#include #include #include @@ -29,28 +28,10 @@ class DefaultTimerDelegate : public reporting::ReportScheduler::TimerDelegate public: using TimerContext = reporting::TimerContext; using Timeout = System::Clock::Timeout; - static void TimerCallbackInterface(System::Layer * aLayer, void * aAppState) - { - TimerContext * context = static_cast(aAppState); - context->TimerFired(); - } - CHIP_ERROR StartTimer(TimerContext * context, Timeout aTimeout) override - { - return InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->StartTimer( - aTimeout, TimerCallbackInterface, context); - } - void CancelTimer(TimerContext * context) override - { - InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->CancelTimer( - TimerCallbackInterface, context); - } - bool IsTimerActive(TimerContext * context) override - { - return InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->IsTimerActive( - TimerCallbackInterface, context); - } - - System::Clock::Timestamp GetCurrentMonotonicTimestamp() override { return System::SystemClock().GetMonotonicTimestamp(); } + CHIP_ERROR StartTimer(TimerContext * context, Timeout aTimeout) override; + void CancelTimer(TimerContext * context) override; + bool IsTimerActive(TimerContext * context) override; + System::Clock::Timestamp GetCurrentMonotonicTimestamp() override; }; } // namespace app diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index b416d5086c3096..f7d8d24ab36d2f 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -117,6 +117,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams) VerifyOrExit(initParams.sessionKeystore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); VerifyOrExit(initParams.operationalKeystore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); VerifyOrExit(initParams.opCertStore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrExit(initParams.reportScheduler != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); // TODO(16969): Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code chip::Platform::MemoryInit(); @@ -171,6 +172,8 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams) mGroupsProvider = initParams.groupDataProvider; SetGroupDataProvider(mGroupsProvider); + mReportScheduler = initParams.reportScheduler; + mTestEventTriggerDelegate = initParams.testEventTriggerDelegate; deviceInfoprovider = DeviceLayer::GetDeviceInfoProvider(); @@ -250,7 +253,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams) #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT #if CHIP_CONFIG_ENABLE_ICD_SERVER - mICDManager.Init(mDeviceStorage, &GetFabricTable(), &mReportScheduler); + mICDManager.Init(mDeviceStorage, &GetFabricTable(), mReportScheduler); mICDEventManager.Init(&mICDManager); #endif // CHIP_CONFIG_ENABLE_ICD_SERVER @@ -317,7 +320,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams) &mCertificateValidityPolicy, mGroupsProvider); SuccessOrExit(err); - err = chip::app::InteractionModelEngine::GetInstance()->Init(&mExchangeMgr, &GetFabricTable(), &mReportScheduler, + err = chip::app::InteractionModelEngine::GetInstance()->Init(&mExchangeMgr, &GetFabricTable(), mReportScheduler, &mCASESessionManager, mSubscriptionResumptionStorage); SuccessOrExit(err); @@ -555,6 +558,9 @@ KvsPersistentStorageDelegate CommonCaseDeviceServerInitParams::sKvsPersistenStor PersistentStorageOperationalKeystore CommonCaseDeviceServerInitParams::sPersistentStorageOperationalKeystore; Credentials::PersistentStorageOpCertStore CommonCaseDeviceServerInitParams::sPersistentStorageOpCertStore; Credentials::GroupDataProviderImpl CommonCaseDeviceServerInitParams::sGroupDataProvider; +app::DefaultTimerDelegate CommonCaseDeviceServerInitParams::sTimerDelegate; +app::reporting::ReportSchedulerImpl + CommonCaseDeviceServerInitParams::sReportScheduler(&CommonCaseDeviceServerInitParams::sTimerDelegate); #if CHIP_CONFIG_ENABLE_SESSION_RESUMPTION SimpleSessionResumptionStorage CommonCaseDeviceServerInitParams::sSessionResumptionStorage; #endif diff --git a/src/app/server/Server.h b/src/app/server/Server.h index c41a9e3127e0d9..e5b5585be19f44 100644 --- a/src/app/server/Server.h +++ b/src/app/server/Server.h @@ -64,12 +64,8 @@ #include #endif #include -#include -#if CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED -#include -#else #include -#endif +#include #if CHIP_CONFIG_ENABLE_ICD_SERVER #include // nogncheck @@ -146,6 +142,8 @@ struct ServerInitParams // Operational certificate store with access to the operational certs in persisted storage: // must not be null at timne of Server::Init(). Credentials::OperationalCertificateStore * opCertStore = nullptr; + // Required, if not provided, the Server::Init() WILL fail. + app::reporting::ReportScheduler * reportScheduler = nullptr; }; /** @@ -222,6 +220,14 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams this->opCertStore = &sPersistentStorageOpCertStore; } + // Injection of report scheduler WILL lead to two schedulers being allocated. As recommended above, this should only be used + // for IN-TREE examples. If a default scheduler is desired, the basic ServerInitParams should be used by the application and + // CommonCaseDeviceServerInitParams should not be allocated. + if (this->reportScheduler == nullptr) + { + reportScheduler = &sReportScheduler; + } + // Session Keystore injection this->sessionKeystore = &sSessionKeystore; @@ -260,6 +266,8 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams static PersistentStorageOperationalKeystore sPersistentStorageOperationalKeystore; static Credentials::PersistentStorageOpCertStore sPersistentStorageOpCertStore; static Credentials::GroupDataProviderImpl sGroupDataProvider; + static chip::app::DefaultTimerDelegate sTimerDelegate; + static app::reporting::ReportSchedulerImpl sReportScheduler; #if CHIP_CONFIG_ENABLE_SESSION_RESUMPTION static SimpleSessionResumptionStorage sSessionResumptionStorage; @@ -340,7 +348,7 @@ class Server app::DefaultAttributePersistenceProvider & GetDefaultAttributePersister() { return mAttributePersister; } - app::reporting::ReportScheduler & GetReportScheduler() { return mReportScheduler; } + app::reporting::ReportScheduler * GetReportScheduler() { return mReportScheduler; } /** * This function causes the ShutDown event to be generated async on the @@ -360,7 +368,7 @@ class Server static Server & GetInstance() { return sServer; } private: - Server() : mTimerDelegate(), mReportScheduler(&mTimerDelegate) {} + Server() {} static Server sServer; @@ -594,12 +602,7 @@ class Server app::DefaultAttributePersistenceProvider mAttributePersister; GroupDataProviderListener mListener; ServerFabricDelegate mFabricDelegate; - app::DefaultTimerDelegate mTimerDelegate; -#if CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED - app::reporting::SynchronizedReportSchedulerImpl mReportScheduler; -#else - app::reporting::ReportSchedulerImpl mReportScheduler; -#endif + app::reporting::ReportScheduler * mReportScheduler; Access::AccessControl mAccessControl; app::AclStorage * mAclStorage; diff --git a/src/app/tests/TestCommissionManager.cpp b/src/app/tests/TestCommissionManager.cpp index e2886f16dbdeab..73cf9423ef0838 100644 --- a/src/app/tests/TestCommissionManager.cpp +++ b/src/app/tests/TestCommissionManager.cpp @@ -15,6 +15,8 @@ * limitations under the License. */ +#include +#include #include #include #include @@ -92,6 +94,10 @@ void InitializeChip(nlTestSuite * suite) chip::DeviceLayer::SetCommissionableDataProvider(&commissionableDataProvider); static chip::CommonCaseDeviceServerInitParams initParams; + // Report scheduler and timer delegate instance + static chip::app::DefaultTimerDelegate sTimerDelegate; + static chip::app::reporting::ReportSchedulerImpl sReportScheduler(&sTimerDelegate); + initParams.reportScheduler = &sReportScheduler; (void) initParams.InitializeStaticResourcesBeforeServerInit(); err = chip::Server::GetInstance().Init(initParams);