Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[ReadHandler] ReportScheduler Injection #28497

19 changes: 19 additions & 0 deletions examples/platform/silabs/MatterConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys
#include "SilabsDeviceDataProvider.h"
#include "SilabsTestEventTriggerDelegate.h"
#include <app/InteractionModelEngine.h>
#include <app/TimerDelegates.h>

#if CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED
#include <app/reporting/SynchronizedReportSchedulerImpl.h>
#else
#include <app/reporting/ReportSchedulerImpl.h>
#endif

#include <lib/support/BytesToHex.h>

#ifdef CHIP_CONFIG_USE_ICD_SUBSCRIPTION_CALLBACKS
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ static_library("app") {
"TimedHandler.h",
"TimedRequest.cpp",
"TimedRequest.h",
"TimerDelegates.cpp",
"TimerDelegates.h",
"WriteClient.cpp",
"WriteHandler.cpp",
"reporting/Engine.cpp",
Expand Down
55 changes: 55 additions & 0 deletions src/app/TimerDelegates.cpp
Original file line number Diff line number Diff line change
@@ -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 <app/InteractionModelEngine.h>
#include <app/TimerDelegates.h>
#include <system/SystemClock.h>

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<TimerContext *>(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
27 changes: 4 additions & 23 deletions src/app/TimerDelegates.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#pragma once

#include <app/InteractionModelEngine.h>
#include <app/reporting/ReportScheduler.h>
#include <system/SystemClock.h>

Expand All @@ -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<TimerContext *>(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
Expand Down
10 changes: 8 additions & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@
#include <transport/raw/BLE.h>
#endif
#include <app/TimerDelegates.h>
#include <transport/raw/UDP.h>
#if CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED
#include <app/reporting/SynchronizedReportSchedulerImpl.h>
#else
#include <app/reporting/ReportSchedulerImpl.h>
#endif
#include <transport/raw/UDP.h>

#if CHIP_CONFIG_ENABLE_ICD_SERVER
#include <app/icd/ICDEventManager.h> // nogncheck
Expand Down Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -360,7 +368,7 @@ class Server
static Server & GetInstance() { return sServer; }

private:
Server() : mTimerDelegate(), mReportScheduler(&mTimerDelegate) {}
Server() {}

static Server sServer;

Expand Down Expand Up @@ -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;
lpbeliveau-silabs marked this conversation as resolved.
Show resolved Hide resolved

Access::AccessControl mAccessControl;
app::AclStorage * mAclStorage;
Expand Down
6 changes: 6 additions & 0 deletions src/app/tests/TestCommissionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

#include <app/TimerDelegates.h>
#include <app/reporting/ReportSchedulerImpl.h>
#include <app/server/CommissioningWindowManager.h>
#include <app/server/Server.h>
#include <lib/dnssd/Advertiser.h>
Expand Down Expand Up @@ -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);

Expand Down