Skip to content

Commit 5f31784

Browse files
committed
ReadHandler changes for large payloads
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.
1 parent 5ee4ef6 commit 5f31784

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/app/ReadHandler.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -919,5 +919,17 @@ void ReadHandler::ClearStateFlag(ReadHandlerFlags aFlag)
919919
SetStateFlag(aFlag, false);
920920
}
921921

922+
size_t ReadHandler::GetReportBufferMaxSize()
923+
{
924+
size_t maxBufSize = chip::app::kMaxSecureSduLengthBytes;
925+
Transport::SecureSession * session = GetSession();
926+
if (session && session->AllowsLargePayload())
927+
{
928+
maxBufSize = chip::app::kMaxLargeSecureSduLengthBytes;
929+
}
930+
931+
return maxBufSize;
932+
}
933+
922934
} // namespace app
923935
} // namespace chip

src/app/ReadHandler.h

+8
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ class ReadHandler : public Messaging::ExchangeDelegate
333333
*/
334334
CHIP_ERROR SendReportData(System::PacketBufferHandle && aPayload, bool aMoreChunks);
335335

336+
/*
337+
* Get the appropriate size of a packet buffer to allocate for encoding a Report message.
338+
* Depending on the underlying session, which may or may not support large
339+
* payloads, a buffer with the corresponding max size would be allocated.
340+
*
341+
*/
342+
size_t GetReportBufferMaxSize();
343+
336344
/**
337345
* Returns whether this ReadHandler represents a subscription that was created by the other side of the provided exchange.
338346
*/

src/app/StatusResponse.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
namespace chip {
2828
namespace app {
29-
static constexpr size_t kMaxSecureSduLengthBytes = kMaxAppMessageLen + kMaxTagLen;
29+
static constexpr size_t kMaxSecureSduLengthBytes = kMaxAppMessageLen + kMaxTagLen;
30+
static constexpr size_t kMaxLargeSecureSduLengthBytes = kMaxLargeAppMessageLen + kMaxTagLen;
3031

3132
class StatusResponse
3233
{

src/app/reporting/Engine.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,11 @@ CHIP_ERROR Engine::BuildAndSendSingleReportData(ReadHandler * apReadHandler)
492492
CHIP_ERROR err = CHIP_NO_ERROR;
493493
chip::System::PacketBufferTLVWriter reportDataWriter;
494494
ReportDataMessage::Builder reportDataBuilder;
495-
chip::System::PacketBufferHandle bufHandle = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
495+
chip::System::PacketBufferHandle bufHandle = nullptr;
496496
uint16_t reservedSize = 0;
497497
bool hasMoreChunks = false;
498498
bool needCloseReadHandler = false;
499+
size_t maxSduSize = 0;
499500

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

513514
VerifyOrExit(apReadHandler != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
514515
VerifyOrExit(apReadHandler->GetSession() != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
516+
517+
// Depending on whether the session supports large payload or not, the
518+
// appropriate max size would be returned for the Report buffer.
519+
maxSduSize = apReadHandler->GetReportBufferMaxSize();
520+
521+
bufHandle = System::PacketBufferHandle::New(maxSduSize);
515522
VerifyOrExit(!bufHandle.IsNull(), err = CHIP_ERROR_NO_MEMORY);
516523

517-
if (bufHandle->AvailableDataLength() > kMaxSecureSduLengthBytes)
524+
if (bufHandle->AvailableDataLength() > maxSduSize)
518525
{
519-
reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - kMaxSecureSduLengthBytes);
526+
reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - maxSduSize);
520527
}
521528

522529
reportDataWriter.Init(std::move(bufHandle));

0 commit comments

Comments
 (0)