Skip to content

Commit

Permalink
Reverted name changes for flag set and clear, reworded comments as su…
Browse files Browse the repository at this point in the history
…ggested
  • Loading branch information
lpbeliveau-silabs committed Jun 16, 2023
1 parent d9f617f commit e963a90
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 70 deletions.
36 changes: 18 additions & 18 deletions src/app/ReadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ReadHandler::ReadHandler(ManagementCallback & apCallback, Messaging::ExchangeCon
mLastWrittenEventsBytes = 0;
mTransactionStartGeneration = InteractionModelEngine::GetInstance()->GetReportingEngine().GetDirtySetGeneration();
mFlags.ClearAll();
SetStateFlagAndScheduleReport(ReadHandlerFlags::PrimingReports);
SetStateFlag(ReadHandlerFlags::PrimingReports);

mSessionHandle.Grab(mExchangeCtx->GetSessionHandle());
}
Expand All @@ -80,7 +80,7 @@ void ReadHandler::ResumeSubscription(CASESessionManager & caseSessionManager,
mSubscriptionId = subscriptionInfo.mSubscriptionId;
mMinIntervalFloorSeconds = subscriptionInfo.mMinInterval;
mMaxInterval = subscriptionInfo.mMaxInterval;
SetStateFlagAndScheduleReport(ReadHandlerFlags::FabricFiltered, subscriptionInfo.mFabricFiltered);
SetStateFlag(ReadHandlerFlags::FabricFiltered, subscriptionInfo.mFabricFiltered);

// Move dynamically allocated attributes and events from the SubscriptionInfo struct into
// the object pool managed by the IM engine
Expand Down Expand Up @@ -187,7 +187,7 @@ void ReadHandler::OnInitialRequest(System::PacketBufferHandle && aPayload)
else
{
// Force us to be in a dirty state so we get processed by the reporting
SetStateFlagAndScheduleReport(ReadHandlerFlags::ForceDirty);
SetStateFlag(ReadHandlerFlags::ForceDirty);
}
}

Expand All @@ -214,7 +214,7 @@ CHIP_ERROR ReadHandler::OnStatusResponse(Messaging::ExchangeContext * apExchange
{
err = SendSubscribeResponse();

SetStateFlagAndScheduleReport(ReadHandlerFlags::ActiveSubscription);
SetStateFlag(ReadHandlerFlags::ActiveSubscription);

auto * appCallback = mManagementCallback.GetAppCallback();
if (appCallback)
Expand Down Expand Up @@ -297,7 +297,7 @@ CHIP_ERROR ReadHandler::SendReportData(System::PacketBufferHandle && aPayload, b
{
mCurrentReportsBeginGeneration = InteractionModelEngine::GetInstance()->GetReportingEngine().GetDirtySetGeneration();
}
SetStateFlagAndScheduleReport(ReadHandlerFlags::ChunkedReport, aMoreChunks);
SetStateFlag(ReadHandlerFlags::ChunkedReport, aMoreChunks);
bool responseExpected = IsType(InteractionType::Subscribe) || aMoreChunks;

mExchangeCtx->UseSuggestedResponseTimeout(app::kExpectedIMProcessingTime);
Expand Down Expand Up @@ -442,7 +442,7 @@ CHIP_ERROR ReadHandler::ProcessReadRequest(System::PacketBufferHandle && aPayloa

bool isFabricFiltered;
ReturnErrorOnFailure(readRequestParser.GetIsFabricFiltered(&isFabricFiltered));
SetStateFlagAndScheduleReport(ReadHandlerFlags::FabricFiltered, isFabricFiltered);
SetStateFlag(ReadHandlerFlags::FabricFiltered, isFabricFiltered);
ReturnErrorOnFailure(readRequestParser.ExitContainer());
MoveToState(HandlerState::GeneratingReports);

Expand Down Expand Up @@ -636,7 +636,7 @@ CHIP_ERROR ReadHandler::SendSubscribeResponse()

ReturnErrorOnFailure(UpdateReportTimer());

ClearStateFlagAndScheduleReport(ReadHandlerFlags::PrimingReports);
ClearStateFlag(ReadHandlerFlags::PrimingReports);
return mExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::SubscribeResponse, std::move(packet));
}

Expand Down Expand Up @@ -718,7 +718,7 @@ CHIP_ERROR ReadHandler::ProcessSubscribeRequest(System::PacketBufferHandle && aP

bool isFabricFiltered;
ReturnErrorOnFailure(subscribeRequestParser.GetIsFabricFiltered(&isFabricFiltered));
SetStateFlagAndScheduleReport(ReadHandlerFlags::FabricFiltered, isFabricFiltered);
SetStateFlag(ReadHandlerFlags::FabricFiltered, isFabricFiltered);
ReturnErrorOnFailure(Crypto::DRBG_get_bytes(reinterpret_cast<uint8_t *>(&mSubscriptionId), sizeof(mSubscriptionId)));
ReturnErrorOnFailure(subscribeRequestParser.ExitContainer());
MoveToState(HandlerState::GeneratingReports);
Expand Down Expand Up @@ -758,7 +758,7 @@ void ReadHandler::MinIntervalExpiredCallback(System::Layer * apSystemLayer, void
VerifyOrReturn(apAppState != nullptr);
ReadHandler * readHandler = static_cast<ReadHandler *>(apAppState);
ChipLogDetail(DataManagement, "Unblock report hold after min %d seconds", readHandler->mMinIntervalFloorSeconds);
readHandler->ClearStateFlagAndScheduleReport(ReadHandlerFlags::HoldMinInterval);
readHandler->ClearStateFlag(ReadHandlerFlags::WaitingUntilMinInterval);
InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->StartTimer(
System::Clock::Seconds16(readHandler->mMaxInterval - readHandler->mMinIntervalFloorSeconds), MaxIntervalExpiredCallback,
readHandler);
Expand All @@ -768,7 +768,7 @@ void ReadHandler::MaxIntervalExpiredCallback(System::Layer * apSystemLayer, void
{
VerifyOrReturn(apAppState != nullptr);
ReadHandler * readHandler = static_cast<ReadHandler *>(apAppState);
readHandler->ClearStateFlagAndScheduleReport(ReadHandlerFlags::HoldMaxInterval);
readHandler->ClearStateFlag(ReadHandlerFlags::WaitingUntilMaxInterval);
ChipLogProgress(DataManagement, "Refresh subscribe timer sync after %d seconds",
readHandler->mMaxInterval - readHandler->mMinIntervalFloorSeconds);
}
Expand All @@ -784,8 +784,8 @@ CHIP_ERROR ReadHandler::UpdateReportTimer()
{
ChipLogProgress(DataManagement, "Refresh Subscribe Sync Timer with min %d seconds and max %d seconds",
mMinIntervalFloorSeconds, mMaxInterval);
SetStateFlagAndScheduleReport(ReadHandlerFlags::HoldMinInterval);
SetStateFlagAndScheduleReport(ReadHandlerFlags::HoldMaxInterval);
SetStateFlag(ReadHandlerFlags::WaitingUntilMinInterval);
SetStateFlag(ReadHandlerFlags::WaitingUntilMaxInterval);
ReturnErrorOnFailure(
InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->StartTimer(
System::Clock::Seconds16(mMinIntervalFloorSeconds), MinIntervalExpiredCallback, this));
Expand All @@ -800,13 +800,13 @@ void ReadHandler::ResetPathIterator()
mAttributeEncoderState = AttributeValueEncoder::AttributeEncodeState();
}

void ReadHandler::UpdateDirtyGeneration(const AttributePathParams & aAttributeChanged)
void ReadHandler::AttributePathIsDirty(const AttributePathParams & aAttributeChanged)
{
ConcreteAttributePath path;

mDirtyGeneration = InteractionModelEngine::GetInstance()->GetReportingEngine().GetDirtySetGeneration();

// We won't reset the path iterator for every UpdateDirtyGeneration call to reduce the number of full data reports.
// We won't reset the path iterator for every AttributePathIsDirty call to reduce the number of full data reports.
// The iterator will be reset after finishing each report session.
//
// Here we just reset the iterator to the beginning of the current cluster, if the dirty path affects it.
Expand Down Expand Up @@ -846,10 +846,10 @@ Transport::SecureSession * ReadHandler::GetSession() const

void ReadHandler::ForceDirtyState()
{
SetStateFlagAndScheduleReport(ReadHandlerFlags::ForceDirty);
SetStateFlag(ReadHandlerFlags::ForceDirty);
}

void ReadHandler::SetStateFlagAndScheduleReport(ReadHandlerFlags aFlag, bool aValue)
void ReadHandler::SetStateFlag(ReadHandlerFlags aFlag, bool aValue)
{
bool oldReportable = IsReportableNow();
mFlags.Set(aFlag, aValue);
Expand All @@ -860,9 +860,9 @@ void ReadHandler::SetStateFlagAndScheduleReport(ReadHandlerFlags aFlag, bool aVa
}
}

void ReadHandler::ClearStateFlagAndScheduleReport(ReadHandlerFlags aFlag)
void ReadHandler::ClearStateFlag(ReadHandlerFlags aFlag)
{
SetStateFlagAndScheduleReport(aFlag, false);
SetStateFlag(aFlag, false);
}

void ReadHandler::HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr,
Expand Down
41 changes: 19 additions & 22 deletions src/app/ReadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ class ReadHandler : public Messaging::ExchangeDelegate

enum class ReadHandlerFlags : uint8_t
{
// mHoldMinInterval is used to prevent subscription data delivery while we are
// WaitingUntilMinInterval is used to prevent subscription data delivery while we are
// waiting for the min reporting interval to elapse.
WaitingUntilMinInterval = (1 << 0),

// mHoldMaxInterval is used to prevent subscription empty report delivery while we
// are waiting for the max reporting interval to elaps. When mHoldMaxInterval
// WaitingUntilMaxInterval is used to prevent subscription empty report delivery while we
// are waiting for the max reporting interval to elaps. When WaitingUntilMaxInterval
// becomes false, we are allowed to send an empty report to keep the
// subscription alive on the client.
WaitingUntilMaxInterval = (1 << 1),
Expand Down Expand Up @@ -291,18 +291,15 @@ class ReadHandler : public Messaging::ExchangeDelegate

bool IsIdle() const { return mState == HandlerState::Idle; }

/// @brief Returns whether the ReadHandler is in a state where it can immediately send a report. The main conditions for this
/// are that the handler is in a report generating state, meaning it is subscribed to or processing a read request, that the min
/// interval has expired and the max interval has expired, forcing an empty report, or that the handler is dirty. This function
/// @brief Returns whether the ReadHandler is in a state where it can immediately send a report. This function
/// is used to determine whether a report generation should be scheduled for the handler.
/// @return Whether the ReadHandler is in a state where it can immediately send a report.
bool IsReportableNow() const
{
// Important: Anything that changes the state IsReportableNow depends on in
// a way that causes IsReportableNow to become true must call ScheduleRun
// on the reporting engine.
return mState == HandlerState::GeneratingReports && !mFlags.Has(ReadHandlerFlags::HoldMinInterval) &&
(IsDirty() || !mFlags.Has(ReadHandlerFlags::HoldMaxInterval));
return mState == HandlerState::GeneratingReports && !mFlags.Has(ReadHandlerFlags::WaitingUntilMinInterval) &&
(IsDirty() || !mFlags.Has(ReadHandlerFlags::WaitingUntilMaxInterval));
}
bool IsGeneratingReports() const { return mState == HandlerState::GeneratingReports; }
bool IsAwaitingReportResponse() const { return mState == HandlerState::AwaitingReportResponse; }
Expand All @@ -329,15 +326,15 @@ class ReadHandler : public Messaging::ExchangeDelegate
void GetSubscriptionId(SubscriptionId & aSubscriptionId) const { aSubscriptionId = mSubscriptionId; }
AttributePathExpandIterator * GetAttributePathExpandIterator() { return &mAttributePathExpandIterator; }

/// @brief Update mDirtyGeneration to the latest DirtySetGeneration of the InteractionModelEngine. As this might make the
/// readhandler dirty, this function checks if the readhandler is now reportable and schedules a run if it is the case.
/// @brief Notifies the read handler that a set of attribute paths has been marked dirty. This will scehdule an engine run if
/// the change to the attribute path makes the ReadHandler reportable.
/// @param aAttributeChanged Path to the attribute that was changed.
void UpdateDirtyGeneration(const AttributePathParams & aAttributeChanged);
void AttributePathIsDirty(const AttributePathParams & aAttributeChanged);
bool IsDirty() const
{
return (mDirtyGeneration > mPreviousReportsBeginGeneration) || mFlags.Has(ReadHandlerFlags::ForceDirty);
}
void ClearForceDirtyFlag() { ClearStateFlagAndScheduleReport(ReadHandlerFlags::ForceDirty); }
void ClearForceDirtyFlag() { ClearStateFlag(ReadHandlerFlags::ForceDirty); }
NodeId GetInitiatorNodeId() const
{
auto session = GetSession();
Expand All @@ -356,7 +353,7 @@ class ReadHandler : public Messaging::ExchangeDelegate
auto GetTransactionStartGeneration() const { return mTransactionStartGeneration; }

/// @brief Forces the read handler into a dirty state, regardless of what's going on with attributes.
/// This can lead to scheduling of a reporting run immediately, if the min interval has been reached,
/// This can lead to scheduling of a reporting run immediately, if the min interval has been reached,
/// or after the min interval is reached if it has not yet been reached.
void ForceDirtyState();

Expand Down Expand Up @@ -428,16 +425,16 @@ class ReadHandler : public Messaging::ExchangeDelegate

void PersistSubscription();

/// @brief Modifies a state flag in the read handler and schedules an engine run if the read handler want from a
/// non-reportable state to a reportable state.
/// @brief Modifies a state flag in the read handler. If the flag went from a
/// non-reportable state to a reportable state, schedules an engine run.
/// @param aFlag Flag to set
/// @param aValue Flag new value
void SetStateFlagAndScheduleReport(ReadHandlerFlags aFlag, bool aValue = true);
void SetStateFlag(ReadHandlerFlags aFlag, bool aValue = true);

/// @brief This function calls the SetStateFlagAndScheduleReport with the flag value set to false, thus possibly emitting a report
/// @brief This function calls the SetStateFlag with the flag value set to false, thus possibly emitting a report
/// generation.
/// @param aFlag Flag to clear
void ClearStateFlagAndScheduleReport(ReadHandlerFlags aFlag);
void ClearStateFlag(ReadHandlerFlags aFlag);

// Helpers for continuing the subscription resumption
static void HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr,
Expand All @@ -455,7 +452,7 @@ class ReadHandler : public Messaging::ExchangeDelegate
// current generation when we started sending the last set reports that we completed.
//
// This allows us to reset the iterator to the beginning of the current
// cluster instead of the beginning of the whole report in UpdateDirtyGeneration, without
// cluster instead of the beginning of the whole report in AttributePathIsDirty, without
// permanently missing dirty any paths.
uint64_t mDirtyGeneration = 0;

Expand All @@ -469,14 +466,14 @@ class ReadHandler : public Messaging::ExchangeDelegate
/*
* (mDirtyGeneration = b > a, this is a dirty read handler)
* +- Start Report -> mCurrentReportsBeginGeneration = c
* | +- UpdateDirtyGeneration (Attribute Y) -> mDirtyGeneration = d
* | +- AttributePathIsDirty (Attribute Y) -> mDirtyGeneration = d
* | | +- Last Chunk -> mPreviousReportsBeginGeneration = mCurrentReportsBeginGeneration = c
* | | | +- (mDirtyGeneration = d) > (mPreviousReportsBeginGeneration = c), this is a dirty read handler
* | | | | Attribute X has a dirty generation less than c, Attribute Y has a dirty generation larger than c
* | | | | So Y will be included in the report but X will not be inclued in this report.
* -a--b--c------d-----e---f---> Generation
* | |
* | +- UpdateDirtyGeneration (Attribute X) (mDirtyGeneration = b)
* | +- AttributePathIsDirty (Attribute X) (mDirtyGeneration = b)
* +- mPreviousReportsBeginGeneration
* For read handler, if mDirtyGeneration > mPreviousReportsBeginGeneration, then we regard it as a dirty read handler, and it
* should generate report on timeout reached.
Expand Down
6 changes: 3 additions & 3 deletions src/app/reporting/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,16 +826,16 @@ CHIP_ERROR Engine::SetDirty(AttributePathParams & aAttributePath)
bool intersectsInterestPath = false;
InteractionModelEngine::GetInstance()->mReadHandlers.ForEachActiveObject(
[&aAttributePath, &intersectsInterestPath](ReadHandler * handler) {
// We call UpdateDirtyGeneration for both read interactions and subscribe interactions, since we may send inconsistent
// attribute data between two chunks. UpdateDirtyGeneration will not schedule a new run for read handlers which are
// We call AttributePathIsDirty for both read interactions and subscribe interactions, since we may send inconsistent
// attribute data between two chunks. AttributePathIsDirty will not schedule a new run for read handlers which are
// waiting for a response to the last message chunk for read interactions.
if (handler->IsGeneratingReports() || handler->IsAwaitingReportResponse())
{
for (auto object = handler->GetAttributePathList(); object != nullptr; object = object->mpNext)
{
if (object->mValue.Intersects(aAttributePath))
{
handler->UpdateDirtyGeneration(aAttributePath);
handler->AttributePathIsDirty(aAttributePath);
intersectsInterestPath = true;
break;
}
Expand Down
Loading

0 comments on commit e963a90

Please sign in to comment.