Skip to content

Commit

Permalink
ReadHandler changes for large payloads
Browse files Browse the repository at this point in the history
When sending reports, if the session established with the peer
supports large payloads, the ReadHandler will allocate a large
buffer to, potentially, fit more attribute and event data.
  • Loading branch information
pidarped committed Jun 11, 2024
1 parent 5ee4ef6 commit bcc7517
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/app/ReadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,5 +919,17 @@ void ReadHandler::ClearStateFlag(ReadHandlerFlags aFlag)
SetStateFlag(aFlag, false);
}

size_t ReadHandler::GetReportBufferMaxSize()
{
size_t maxBufSize = chip::app::kMaxSecureSduLengthBytes;
Transport::SecureSession *session = GetSession();
if (session && session->AllowsLargePayload())
{
maxBufSize = chip::app::kMaxLargeSecureSduLengthBytes;
}

return maxBufSize;
}

} // namespace app
} // namespace chip
9 changes: 9 additions & 0 deletions src/app/ReadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ class ReadHandler : public Messaging::ExchangeDelegate
*/
CHIP_ERROR SendReportData(System::PacketBufferHandle && aPayload, bool aMoreChunks);

/*
* Get the appropriate size of a packet buffer to allocate for encoding a Report message.
* Depending on the underlying session, which may or may not support large
* payloads, a buffer with the corresponding max size would be allocated.
*
*/
size_t GetReportBufferMaxSize();

/**
* Returns whether this ReadHandler represents a subscription that was created by the other side of the provided exchange.
*/
Expand Down Expand Up @@ -572,6 +580,7 @@ class ReadHandler : public Messaging::ExchangeDelegate

// TODO (#27675): Merge all observers into one and that one will dispatch the callbacks to the right place.
Observer * mObserver = nullptr;

};
} // namespace app
} // namespace chip
3 changes: 2 additions & 1 deletion src/app/StatusResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

namespace chip {
namespace app {
static constexpr size_t kMaxSecureSduLengthBytes = kMaxAppMessageLen + kMaxTagLen;
static constexpr size_t kMaxSecureSduLengthBytes = kMaxAppMessageLen + kMaxTagLen;
static constexpr size_t kMaxLargeSecureSduLengthBytes = kMaxLargeAppMessageLen + kMaxTagLen;

class StatusResponse
{
Expand Down
13 changes: 10 additions & 3 deletions src/app/reporting/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,11 @@ CHIP_ERROR Engine::BuildAndSendSingleReportData(ReadHandler * apReadHandler)
CHIP_ERROR err = CHIP_NO_ERROR;
chip::System::PacketBufferTLVWriter reportDataWriter;
ReportDataMessage::Builder reportDataBuilder;
chip::System::PacketBufferHandle bufHandle = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
chip::System::PacketBufferHandle bufHandle = nullptr;
uint16_t reservedSize = 0;
bool hasMoreChunks = false;
bool needCloseReadHandler = false;
size_t maxSduSize = 0;

// Reserved size for the MoreChunks boolean flag, which takes up 1 byte for the control tag and 1 byte for the context tag.
const uint32_t kReservedSizeForMoreChunksFlag = 1 + 1;
Expand All @@ -512,11 +513,17 @@ CHIP_ERROR Engine::BuildAndSendSingleReportData(ReadHandler * apReadHandler)

VerifyOrExit(apReadHandler != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(apReadHandler->GetSession() != nullptr, err = CHIP_ERROR_INCORRECT_STATE);

// Depending on whether the session supports large payload or not, the
// appropriate max size would be returned for the Report buffer.
maxSduSize = apReadHandler->GetReportBufferMaxSize();

bufHandle = System::PacketBufferHandle::New(maxSduSize);
VerifyOrExit(!bufHandle.IsNull(), err = CHIP_ERROR_NO_MEMORY);

if (bufHandle->AvailableDataLength() > kMaxSecureSduLengthBytes)
if (bufHandle->AvailableDataLength() > maxSduSize)
{
reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - kMaxSecureSduLengthBytes);
reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - maxSduSize);
}

reportDataWriter.Init(std::move(bufHandle));
Expand Down

0 comments on commit bcc7517

Please sign in to comment.