Skip to content

Commit

Permalink
Reworked the logic to force a report emission when min elapsed and in…
Browse files Browse the repository at this point in the history
…ject the scheduler in the ICD manager from Server::Init()
  • Loading branch information
lpbeliveau-silabs committed Jul 27, 2023
1 parent 270705f commit 6fdb8d5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/app/icd/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ source_set("manager-srcs") {
sources = [
"ICDManager.cpp",
"ICDManager.h",
"${chip_root}/src/app/reporting/ReportScheduler.h",
]

deps = [ ":cluster-srcs" ]
Expand Down Expand Up @@ -54,6 +55,7 @@ source_set("server-srcs") {
sources = [
"ICDEventManager.cpp",
"ICDEventManager.h",

]

public_deps = [ ":manager-srcs" ]
Expand Down
13 changes: 9 additions & 4 deletions src/app/icd/ICDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <app/icd/ICDManager.h>
#include <app/icd/IcdManagementServer.h>
#include <app/icd/IcdMonitoringTable.h>
#include <app/reporting/ReportScheduler.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ConnectivityManager.h>
Expand All @@ -39,13 +40,17 @@ namespace app {
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::IcdManagement;
using ReportScheduler = reporting::ReportScheduler;

void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricTable)
void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricTable, ReportScheduler * reportScheduler)
{
VerifyOrDie(storage != nullptr);
VerifyOrDie(fabricTable != nullptr);
mStorage = storage;
mFabricTable = fabricTable;
VerifyOrDie(reportScheduler != nullptr);

mStorage = storage;
mFabricTable = fabricTable;
mReportScheduler = reportScheduler;

uint32_t activeModeInterval = IcdManagementServer::GetInstance().GetActiveModeInterval();
VerifyOrDie(kFastPollingInterval.count() < activeModeInterval);
Expand Down Expand Up @@ -153,7 +158,7 @@ void ICDManager::UpdateOperationState(OperationalState state)
ChipLogError(AppServer, "Failed to set Polling Interval: err %" CHIP_ERROR_FORMAT, err.Format());
}

InteractionModelEngine::GetInstance()->GetReportScheduler()->OnActiveModeEntered();
mReportScheduler->TriggerReportEmission();
}
else
{
Expand Down
12 changes: 7 additions & 5 deletions src/app/icd/ICDManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#pragma once

#include <app/reporting/ReportScheduler.h>
#include <credentials/FabricTable.h>
#include <lib/support/BitFlags.h>
#include <platform/CHIPDeviceConfig.h>
Expand Down Expand Up @@ -51,7 +52,7 @@ class ICDManager
};

ICDManager() {}
void Init(PersistentStorageDelegate * storage, FabricTable * fabricTable);
void Init(PersistentStorageDelegate * storage, FabricTable * fabricTable, reporting::ReportScheduler * reportScheduler);
void Shutdown();
void UpdateIcdMode();
void UpdateOperationState(OperationalState state);
Expand Down Expand Up @@ -82,10 +83,11 @@ class ICDManager
bool SupportsCheckInProtocol();

BitFlags<KeepActiveFlags> mKeepActiveFlags{ 0 };
OperationalState mOperationalState = OperationalState::IdleMode;
ICDMode mICDMode = ICDMode::SIT;
PersistentStorageDelegate * mStorage = nullptr;
FabricTable * mFabricTable = nullptr;
OperationalState mOperationalState = OperationalState::IdleMode;
ICDMode mICDMode = ICDMode::SIT;
PersistentStorageDelegate * mStorage = nullptr;
FabricTable * mFabricTable = nullptr;
reporting::ReportScheduler * mReportScheduler = nullptr;
};

} // namespace app
Expand Down
68 changes: 51 additions & 17 deletions src/app/reporting/ReportScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ class ReportScheduler : public ReadHandler::Observer
MinIntervalElapsed = (1 << 0),
MaxIntervalElapsed = (1 << 1),
};
void SetTestFlags(TestFlags aFlag, bool aValue) { mFlags.Set(aFlag, aValue); }
bool GetTestFlags(TestFlags aFlag) const { return mFlags.Has(aFlag); }
void SetTestFlags(TestFlags aFlag, bool aValue)
{
mFlags.Set(aFlag, aValue);
}
bool GetTestFlags(TestFlags aFlag) const
{
return mFlags.Has(aFlag);
}
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST

ReadHandlerNode(ReadHandler * aReadHandler, TimerDelegate * aTimerDelegate, ReportScheduler * aScheduler) :
Expand All @@ -83,7 +89,10 @@ class ReportScheduler : public ReadHandler::Observer
mReadHandler = aReadHandler;
SetIntervalTimeStamps(aReadHandler);
}
ReadHandler * GetReadHandler() const { return mReadHandler; }
ReadHandler * GetReadHandler() const
{
return mReadHandler;
}

/// @brief Check if the Node is reportable now, meaning its readhandler was made reportable by attribute dirtying and
/// handler state, and minimal time interval since last report has elapsed, or the maximal time interval since last
Expand All @@ -102,7 +111,10 @@ class ReportScheduler : public ReadHandler::Observer
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
}

bool IsEngineRunScheduled() const { return mEngineRunScheduled; }
bool IsEngineRunScheduled() const
{
return mEngineRunScheduled;
}
void SetEngineRunScheduled(bool aEngineRunScheduled)
{
mEngineRunScheduled = aEngineRunScheduled;
Expand Down Expand Up @@ -140,9 +152,18 @@ class ReportScheduler : public ReadHandler::Observer
mSyncTimestamp = aSyncTimestamp;
}

System::Clock::Timestamp GetMinTimestamp() const { return mMinTimestamp; }
System::Clock::Timestamp GetMaxTimestamp() const { return mMaxTimestamp; }
System::Clock::Timestamp GetSyncTimestamp() const { return mSyncTimestamp; }
System::Clock::Timestamp GetMinTimestamp() const
{
return mMinTimestamp;
}
System::Clock::Timestamp GetMaxTimestamp() const
{
return mMaxTimestamp;
}
System::Clock::Timestamp GetSyncTimestamp() const
{
return mSyncTimestamp;
}

private:
#ifdef CONFIG_BUILD_FOR_HOST_UNIT_TEST
Expand All @@ -167,26 +188,39 @@ class ReportScheduler : public ReadHandler::Observer

virtual void ReportTimerCallback() = 0;

void OnActiveModeEntered()
/// @brief Method that triggers a report emission on each ReadHandler that are above their min interval
/// It checks if all read handlers are above their min interval and if so forces them in a dirty state to trigger a
/// report emission
void TriggerReportEmission()
{
for (auto & iter : mReadHandlerList)
{
if (iter.IsReportableNow())
Timestamp now = mTimerDelegate->GetCurrentMonotonicTimestamp();
mNodesPool.ForEachActiveObject([now](ReadHandlerNode * node) {
if (now >= node->GetMinTimestamp())
{
OnBecameReportable(iter.GetReadHandler());
break;
node->GetReadHandler()->ForceDirtyState();
}
}

return Loop::Continue;
});
}

/// @brief Check whether a ReadHandler is reportable right now, taking into account its minimum and maximum intervals.
/// @param aReadHandler read handler to check
bool IsReportableNow(ReadHandler * aReadHandler) { return FindReadHandlerNode(aReadHandler)->IsReportableNow(); }
bool IsReportableNow(ReadHandler * aReadHandler)
{
return FindReadHandlerNode(aReadHandler)->IsReportableNow();
}
/// @brief Check if a ReadHandler is reportable without considering the timing
bool IsReadHandlerReportable(ReadHandler * aReadHandler) const { return aReadHandler->IsReportable(); }
bool IsReadHandlerReportable(ReadHandler * aReadHandler) const
{
return aReadHandler->IsReportable();
}

/// @brief Get the number of ReadHandlers registered in the scheduler's node pool
size_t GetNumReadHandlers() const { return mNodesPool.Allocated(); }
size_t GetNumReadHandlers() const
{
return mNodesPool.Allocated();
}

#ifdef CONFIG_BUILD_FOR_HOST_UNIT_TEST
void RunNodeCallbackForHandler(const ReadHandler * aReadHandler)
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,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());
mICDManager.Init(mDeviceStorage, &GetFabricTable(), &mReportScheduler);
mICDEventManager.Init(&mICDManager);
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

Expand Down

0 comments on commit 6fdb8d5

Please sign in to comment.