Skip to content

Commit

Permalink
Restyled and ReadHandler include
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Jan 20, 2023
1 parent c813234 commit 4108561
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 46 deletions.
13 changes: 7 additions & 6 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,14 +1598,15 @@ CHIP_ERROR InteractionModelEngine::ResumeSubscriptions()
err = mpSubscriptionResumptionStorage->FindByScopedNodeId(subscriberIndex.mNodes[currentNodeIndex], subscriptions);
ReturnLogErrorOnFailure(err);

ChipLogProgress(
InteractionModel, "\tNode " ChipLogFormatScopedNodeId ": Loaded %u subscriptions..",
ChipLogValueScopedNodeId(subscriberIndex.mNodes[currentNodeIndex]), static_cast<unsigned>(subscriptions.mSize));
ChipLogProgress(InteractionModel, "\tNode " ChipLogFormatScopedNodeId ": Loaded %u subscriptions..",
ChipLogValueScopedNodeId(subscriberIndex.mNodes[currentNodeIndex]),
static_cast<unsigned>(subscriptions.mSize));
for (size_t currentSubscriptionIndex = 0; currentSubscriptionIndex < subscriptions.mSize; currentSubscriptionIndex++)
{
SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo = subscriptions.mSubscriptions[currentSubscriptionIndex];
auto requestedAttributePathCount = subscriptionInfo.mAttributePaths.AllocatedCount();
auto requestedEventPathCount = subscriptionInfo.mEventPaths.AllocatedCount();
SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo =
subscriptions.mSubscriptions[currentSubscriptionIndex];
auto requestedAttributePathCount = subscriptionInfo.mAttributePaths.AllocatedCount();
auto requestedEventPathCount = subscriptionInfo.mEventPaths.AllocatedCount();
if (!EnsureResourceForSubscription(subscriptionInfo.mFabricIndex, requestedAttributePathCount, requestedEventPathCount))
{
ChipLogProgress(InteractionModel, "no resource for Subscription resumption");
Expand Down
8 changes: 5 additions & 3 deletions src/app/ReadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ ReadHandler::ReadHandler(ManagementCallback & apCallback) :
mExchangeCtx(*this), mManagementCallback(apCallback), mOnConnectedCallback(HandleDeviceConnected, this),
mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this)
{
mInteractionType = InteractionType::Subscribe;
mInteractionType = InteractionType::Subscribe;
mFlags.ClearAll();
}

void ReadHandler::ResumeSubscription(CASESessionManager *caseSessionManager, SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo)
void ReadHandler::ResumeSubscription(CASESessionManager * caseSessionManager,
SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo)
{
mSubscriptionId = subscriptionInfo.mSubscriptionId;
mMinIntervalFloorSeconds = subscriptionInfo.mMinInterval;
mMaxInterval = subscriptionInfo.mMaxInterval;
SetStateFlag(ReadHandlerFlags::FabricFiltered, subscriptionInfo.mFabricFiltered);

// Move dynamically allocated attributes and events from the SubscriptionInfo struct into the object pool managed by the IM engine
// Move dynamically allocated attributes and events from the SubscriptionInfo struct into the object pool managed by the IM
// engine
CHIP_ERROR err;
for (size_t i = 0; i < subscriptionInfo.mAttributePaths.AllocatedCount(); i++)
{
Expand Down
4 changes: 3 additions & 1 deletion src/app/ReadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <app/MessageDef/EventFilterIBs.h>
#include <app/MessageDef/EventPathIBs.h>
#include <app/ObjectList.h>
#include <app/OperationalSessionSetup.h>
#include <app/SubscriptionResumptionStorage.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/TLVDebug.h>
Expand Down Expand Up @@ -262,7 +263,8 @@ class ReadHandler : public Messaging::ExchangeDelegate
* Resume a persisted subscription
*
*/
void ResumeSubscription(CASESessionManager *caseSessionManager, SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo);
void ResumeSubscription(CASESessionManager * caseSessionManager,
SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo);
#endif

/**
Expand Down
77 changes: 47 additions & 30 deletions src/app/SimpleSubscriptionResumptionStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::SaveIndex(const SubscriptionInde
return CHIP_NO_ERROR;
}

#define DeleteIndexAndReturnLogErrorOnFailure(expr) \
#define DeleteIndexAndReturnLogErrorOnFailure(expr) \
do \
{ \
auto __err = (expr); \
if (!::chip::ChipError::IsSuccess(__err)) \
{ \
mStorage->SyncDeleteKeyValue(DefaultStorageKeyAllocator::SubscriptionResumptionIndex().KeyName()); \
ChipLogError(DataManagement, "%s at %s:%d", ErrorStr(__err), __FILE__, __LINE__); \
mStorage->SyncDeleteKeyValue(DefaultStorageKeyAllocator::SubscriptionResumptionIndex().KeyName()); \
ChipLogError(DataManagement, "%s at %s:%d", ErrorStr(__err), __FILE__, __LINE__); \
return __err; \
} \
} while (false)
Expand Down Expand Up @@ -132,7 +132,7 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::LoadIndex(SubscriptionIndex & in
return CHIP_ERROR_NO_MEMORY;
}

index.mNodes = std::unique_ptr<ScopedNodeId[]>(new(std::nothrow) ScopedNodeId[nodeCount + allocateExtraSpace]);
index.mNodes = std::unique_ptr<ScopedNodeId[]>(new (std::nothrow) ScopedNodeId[nodeCount + allocateExtraSpace]);
if (!index.mNodes)
{
mStorage->SyncDeleteKeyValue(DefaultStorageKeyAllocator::SubscriptionResumptionIndex().KeyName());
Expand Down Expand Up @@ -166,14 +166,14 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::LoadIndex(SubscriptionIndex & in
return CHIP_NO_ERROR;
}

#define DeleteSubscriptionsAndReturnLogErrorOnFailure(expr) \
#define DeleteSubscriptionsAndReturnLogErrorOnFailure(expr) \
do \
{ \
auto __err = (expr); \
if (!::chip::ChipError::IsSuccess(__err)) \
{ \
mStorage->SyncDeleteKeyValue(GetStorageKey(node).KeyName()); \
ChipLogError(DataManagement, "%s at %s:%d", ErrorStr(__err), __FILE__, __LINE__); \
mStorage->SyncDeleteKeyValue(GetStorageKey(node).KeyName()); \
ChipLogError(DataManagement, "%s at %s:%d", ErrorStr(__err), __FILE__, __LINE__); \
return __err; \
} \
} while (false)
Expand All @@ -182,7 +182,8 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::FindByScopedNodeId(ScopedNodeId
{
return FindByScopedNodeId(node, subscriptions, 0);
}
CHIP_ERROR SimpleSubscriptionResumptionStorage::FindByScopedNodeId(ScopedNodeId node, SubscriptionList & subscriptions, size_t allocateExtraSpace)
CHIP_ERROR SimpleSubscriptionResumptionStorage::FindByScopedNodeId(ScopedNodeId node, SubscriptionList & subscriptions,
size_t allocateExtraSpace)
{
Platform::ScopedMemoryBuffer<uint8_t> backingBuffer;
backingBuffer.Calloc(MaxStateSize());
Expand Down Expand Up @@ -215,7 +216,8 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::FindByScopedNodeId(ScopedNodeId
return CHIP_ERROR_NO_MEMORY;
}

subscriptions.mSubscriptions = std::unique_ptr<SubscriptionInfo[]>(new(std::nothrow) SubscriptionInfo[subscriptionCount + allocateExtraSpace]);
subscriptions.mSubscriptions =
std::unique_ptr<SubscriptionInfo[]>(new (std::nothrow) SubscriptionInfo[subscriptionCount + allocateExtraSpace]);
if (!subscriptions.mSubscriptions)
{
mStorage->SyncDeleteKeyValue(GetStorageKey(node).KeyName());
Expand Down Expand Up @@ -261,13 +263,16 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::FindByScopedNodeId(ScopedNodeId
for (uint16_t currentPathIndex = 0; currentPathIndex < pathCount; currentPathIndex++)
{
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kEndpointIdTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mEndpointId));
DeleteSubscriptionsAndReturnLogErrorOnFailure(
reader.Get(subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mEndpointId));

DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kClusterIdTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mClusterId));
DeleteSubscriptionsAndReturnLogErrorOnFailure(
reader.Get(subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mClusterId));

DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kAttributeIdTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mAttributeId));
DeleteSubscriptionsAndReturnLogErrorOnFailure(
reader.Get(subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mAttributeId));
}
}

Expand All @@ -286,16 +291,20 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::FindByScopedNodeId(ScopedNodeId
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kPathTypeTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(eventPathType));

subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mIsUrgentEvent = (eventPathType == EventPathType::kUrgent);
subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mIsUrgentEvent =
(eventPathType == EventPathType::kUrgent);

DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kEndpointIdTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEndpointId));
DeleteSubscriptionsAndReturnLogErrorOnFailure(
reader.Get(subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEndpointId));

DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kClusterIdTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mClusterId));
DeleteSubscriptionsAndReturnLogErrorOnFailure(
reader.Get(subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mClusterId));

DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Next(kEventIdTag));
DeleteSubscriptionsAndReturnLogErrorOnFailure(reader.Get(subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEventId));
DeleteSubscriptionsAndReturnLogErrorOnFailure(
reader.Get(subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEventId));
}
}

Expand Down Expand Up @@ -328,18 +337,23 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::SaveSubscriptions(const ScopedNo
ReturnErrorOnFailure(writer.Put(kMaxIntervalTag, subscriptions[currentSubscriptionIndex].mMaxInterval));
ReturnErrorOnFailure(writer.Put(kFabricFilteredTag, subscriptions[currentSubscriptionIndex].mFabricFiltered));

ReturnErrorOnFailure(
writer.Put(kPathCountTag, static_cast<uint16_t>(subscriptions[currentSubscriptionIndex].mAttributePaths.AllocatedCount())));
for (size_t currentPathIndex = 0; currentPathIndex < subscriptions[currentSubscriptionIndex].mAttributePaths.AllocatedCount(); currentPathIndex++)
ReturnErrorOnFailure(writer.Put(
kPathCountTag, static_cast<uint16_t>(subscriptions[currentSubscriptionIndex].mAttributePaths.AllocatedCount())));
for (size_t currentPathIndex = 0;
currentPathIndex < subscriptions[currentSubscriptionIndex].mAttributePaths.AllocatedCount(); currentPathIndex++)
{
ReturnErrorOnFailure(writer.Put(kEndpointIdTag, subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mEndpointId));
ReturnErrorOnFailure(writer.Put(kClusterIdTag, subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mClusterId));
ReturnErrorOnFailure(writer.Put(kAttributeIdTag, subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mAttributeId));
ReturnErrorOnFailure(
writer.Put(kEndpointIdTag, subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mEndpointId));
ReturnErrorOnFailure(
writer.Put(kClusterIdTag, subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mClusterId));
ReturnErrorOnFailure(writer.Put(
kAttributeIdTag, subscriptions[currentSubscriptionIndex].mAttributePaths[currentPathIndex].mAttributeId));
}

ReturnErrorOnFailure(
writer.Put(kPathCountTag, static_cast<uint16_t>(subscriptions[currentSubscriptionIndex].mEventPaths.AllocatedCount())));
for (size_t currentPathIndex = 0; currentPathIndex < subscriptions[currentSubscriptionIndex].mEventPaths.AllocatedCount(); currentPathIndex++)
for (size_t currentPathIndex = 0; currentPathIndex < subscriptions[currentSubscriptionIndex].mEventPaths.AllocatedCount();
currentPathIndex++)
{
if (subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mIsUrgentEvent)
{
Expand All @@ -349,9 +363,12 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::SaveSubscriptions(const ScopedNo
{
ReturnErrorOnFailure(writer.Put(kPathTypeTag, EventPathType::kNonUrgent));
}
ReturnErrorOnFailure(writer.Put(kEndpointIdTag, subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEndpointId));
ReturnErrorOnFailure(writer.Put(kClusterIdTag, subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mClusterId));
ReturnErrorOnFailure(writer.Put(kAttributeIdTag, subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEventId));
ReturnErrorOnFailure(
writer.Put(kEndpointIdTag, subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEndpointId));
ReturnErrorOnFailure(
writer.Put(kClusterIdTag, subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mClusterId));
ReturnErrorOnFailure(
writer.Put(kAttributeIdTag, subscriptions[currentSubscriptionIndex].mEventPaths[currentPathIndex].mEventId));
}

ReturnErrorOnFailure(writer.EndContainer(subscriptionContainerType));
Expand Down Expand Up @@ -388,7 +405,7 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::Save(SubscriptionInfo & subscrip
{
if (!subscriptionIndex.mSize)
{
subscriptionIndex.mNodes = std::unique_ptr<ScopedNodeId[]>(new(std::nothrow) ScopedNodeId[1]);
subscriptionIndex.mNodes = std::unique_ptr<ScopedNodeId[]>(new (std::nothrow) ScopedNodeId[1]);
if (!subscriptionIndex.mNodes)
{
return CHIP_ERROR_NO_MEMORY;
Expand All @@ -405,7 +422,7 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::Save(SubscriptionInfo & subscrip

// Load existing subscriptions for node, then combine and save state
SubscriptionList subscriptions;
CHIP_ERROR err = FindByScopedNodeId(subscriptionNode, subscriptions, 1); // ask to allocate 1 extra space for new subscription
CHIP_ERROR err = FindByScopedNodeId(subscriptionNode, subscriptions, 1); // ask to allocate 1 extra space for new subscription
if (err != CHIP_NO_ERROR)
{
return err;
Expand All @@ -414,7 +431,7 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::Save(SubscriptionInfo & subscrip
// if this is the first subscription, allocate space for 1
if (!subscriptions.mSize)
{
subscriptions.mSubscriptions = std::unique_ptr<SubscriptionInfo[]>(new(std::nothrow) SubscriptionInfo[1]);
subscriptions.mSubscriptions = std::unique_ptr<SubscriptionInfo[]>(new (std::nothrow) SubscriptionInfo[1]);
if (!subscriptions.mSubscriptions)
{
return CHIP_ERROR_NO_MEMORY;
Expand Down Expand Up @@ -547,7 +564,7 @@ CHIP_ERROR SimpleSubscriptionResumptionStorage::DeleteAll(FabricIndex fabricInde

// Move the last element into this hole and keep looping
subscriptionIndex[i] = subscriptionIndex[subscriptionIndex.mSize];
indexChanged = true;
indexChanged = true;
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/app/SubscriptionResumptionStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class SubscriptionResumptionStorage
{
size_t mSize;
std::unique_ptr<SubscriptionInfo[]> mSubscriptions;
SubscriptionInfo &operator[](size_t index) { return mSubscriptions[index]; }
const SubscriptionInfo &operator[](size_t index) const { return mSubscriptions[index]; }
SubscriptionInfo & operator[](size_t index) { return mSubscriptions[index]; }
const SubscriptionInfo & operator[](size_t index) const { return mSubscriptions[index]; }
};

/**
Expand All @@ -104,8 +104,8 @@ class SubscriptionResumptionStorage
{
size_t mSize;
std::unique_ptr<ScopedNodeId[]> mNodes;
ScopedNodeId &operator[](size_t index) { return mNodes[index]; }
const ScopedNodeId &operator[](size_t index) const { return mNodes[index]; }
ScopedNodeId & operator[](size_t index) { return mNodes[index]; }
const ScopedNodeId & operator[](size_t index) const { return mNodes[index]; }
};

virtual ~SubscriptionResumptionStorage(){};
Expand Down
3 changes: 1 addition & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,7 @@ void Server::ResumeSubscriptions()
CHIP_ERROR err = chip::app::InteractionModelEngine::GetInstance()->ResumeSubscriptions();
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Error when trying to resume subscriptions : %" CHIP_ERROR_FORMAT,
err.Format());
ChipLogError(AppServer, "Error when trying to resume subscriptions : %" CHIP_ERROR_FORMAT, err.Format());
}
#endif
}
Expand Down

0 comments on commit 4108561

Please sign in to comment.