Skip to content

Commit

Permalink
Add flag to enable/disable dataCache in ClusterStateCache (#24712)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Jun 30, 2023
1 parent edc7c27 commit 1192367
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 32 deletions.
75 changes: 45 additions & 30 deletions src/app/ClusterStateCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ CHIP_ERROR ClusterStateCache::UpdateCache(const ConcreteDataAttributePath & aPat

if (apData)
{
size_t elementSize = 0;
ReturnErrorOnFailure(GetElementTLVSize(apData, elementSize));
Platform::ScopedMemoryBufferWithSize<uint8_t> backingBuffer;
backingBuffer.Calloc(elementSize);
VerifyOrReturnError(backingBuffer.Get() != nullptr, CHIP_ERROR_NO_MEMORY);
TLV::ScopedBufferTLVWriter writer(std::move(backingBuffer), elementSize);
ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), *apData));
ReturnErrorOnFailure(writer.Finalize(backingBuffer));

state.Set<Platform::ScopedMemoryBufferWithSize<uint8_t>>(std::move(backingBuffer));
if (mCacheData)
{
size_t elementSize = 0;
ReturnErrorOnFailure(GetElementTLVSize(apData, elementSize));
Platform::ScopedMemoryBufferWithSize<uint8_t> backingBuffer;
backingBuffer.Calloc(elementSize);
VerifyOrReturnError(backingBuffer.Get() != nullptr, CHIP_ERROR_NO_MEMORY);
TLV::ScopedBufferTLVWriter writer(std::move(backingBuffer), elementSize);
ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), *apData));
ReturnErrorOnFailure(writer.Finalize(backingBuffer));

state.Set<Platform::ScopedMemoryBufferWithSize<uint8_t>>(std::move(backingBuffer));
}
//
// Clear out the committed data version and only set it again once we have received all data for this cluster.
// Otherwise, we may have incomplete data that looks like it's complete since it has a valid data version.
Expand Down Expand Up @@ -99,7 +102,10 @@ CHIP_ERROR ClusterStateCache::UpdateCache(const ConcreteDataAttributePath & aPat
}
else
{
state.Set<StatusIB>(aStatus);
if (mCacheData)
{
state.Set<StatusIB>(aStatus);
}
}

//
Expand All @@ -111,8 +117,12 @@ CHIP_ERROR ClusterStateCache::UpdateCache(const ConcreteDataAttributePath & aPat
mAddedEndpoints.push_back(aPath.mEndpointId);
}

mCache[aPath.mEndpointId][aPath.mClusterId].mAttributes[aPath.mAttributeId] = std::move(state);
mChangedAttributeSet.insert(aPath);
if (mCacheData)
{
mCache[aPath.mEndpointId][aPath.mClusterId].mAttributes[aPath.mAttributeId] = std::move(state);
mChangedAttributeSet.insert(aPath);
}

return CHIP_NO_ERROR;
}

Expand All @@ -127,32 +137,37 @@ CHIP_ERROR ClusterStateCache::UpdateEventCache(const EventHeader & aEventHeader,
{
return CHIP_NO_ERROR;
}
System::PacketBufferHandle handle = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
VerifyOrReturnError(!handle.IsNull(), CHIP_ERROR_NO_MEMORY);

System::PacketBufferTLVWriter writer;
writer.Init(std::move(handle), false);
if (mCacheData)
{
System::PacketBufferHandle handle = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
VerifyOrReturnError(!handle.IsNull(), CHIP_ERROR_NO_MEMORY);

ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), *apData));
ReturnErrorOnFailure(writer.Finalize(&handle));
System::PacketBufferTLVWriter writer;
writer.Init(std::move(handle), false);

//
// Compact the buffer down to a more reasonably sized packet buffer
// if we can.
//
handle.RightSize();
ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), *apData));
ReturnErrorOnFailure(writer.Finalize(&handle));

EventData eventData;
eventData.first = aEventHeader;
eventData.second = std::move(handle);
//
// Compact the buffer down to a more reasonably sized packet buffer
// if we can.
//
handle.RightSize();

mEventDataCache.insert(std::move(eventData));
EventData eventData;
eventData.first = aEventHeader;
eventData.second = std::move(handle);

mEventDataCache.insert(std::move(eventData));
}
mHighestReceivedEventNumber.SetValue(aEventHeader.mEventNumber);
}
else if (apStatus)
{
mEventStatusCache[aEventHeader.mPath] = *apStatus;
if (mCacheData)
{
mEventStatusCache[aEventHeader.mPath] = *apStatus;
}
}

return CHIP_NO_ERROR;
Expand Down
15 changes: 13 additions & 2 deletions src/app/ClusterStateCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,18 @@ class ClusterStateCache : protected ReadClient::Callback
virtual void OnEndpointAdded(ClusterStateCache * cache, EndpointId endpointId){};
};

ClusterStateCache(Callback & callback, Optional<EventNumber> highestReceivedEventNumber = Optional<EventNumber>::Missing()) :
mCallback(callback), mBufferedReader(*this)
/**
*
* @param [in] callback the derived callback which inherit from ReadClient::Callback
* @param [in] highestReceivedEventNumber optional highest received event number, if cache receive the events with the number
* less than or equal to this value, skip those events
* @param [in] cacheData boolean to decide whether this cache would store attribute/event data/status,
* the default is true.
*/
ClusterStateCache(Callback & callback, Optional<EventNumber> highestReceivedEventNumber = Optional<EventNumber>::Missing(),
bool cacheData = true) :
mCallback(callback),
mBufferedReader(*this), mCacheData(cacheData)
{
mHighestReceivedEventNumber = highestReceivedEventNumber;
}
Expand Down Expand Up @@ -622,6 +632,7 @@ class ClusterStateCache : protected ReadClient::Callback
std::map<ConcreteEventPath, StatusIB> mEventStatusCache;
BufferedReadCallback mBufferedReader;
ConcreteClusterPath mLastReportDataPath = ConcreteClusterPath(kInvalidEndpointId, kInvalidClusterId);
bool mCacheData = true;
};

}; // namespace app
Expand Down
1 change: 1 addition & 0 deletions src/controller/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ chip_test_suite("tests") {
test_sources += [ "TestEventCaching.cpp" ]
test_sources += [ "TestReadChunking.cpp" ]
test_sources += [ "TestWriteChunking.cpp" ]
test_sources += [ "TestEventNumberCaching.cpp" ]
}

cflags = [ "-Wconversion" ]
Expand Down
Loading

0 comments on commit 1192367

Please sign in to comment.