Skip to content

Commit 1a8c6d2

Browse files
pidarpedbzbarsky-appleandy31415
authored
Enable ReadHandler to support large payloads (#33814)
* 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. * Address comments and apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/app/ReadHandler.cpp Co-authored-by: Andrei Litvin <andy314@gmail.com> --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent 3a21d25 commit 1a8c6d2

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/app/ReadHandler.cpp

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

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

src/app/ReadHandler.h

+9
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ 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+
* This size might depend on the underlying session used by the ReadHandler.
339+
*
340+
* The size returned here is the size not including the various prepended headers
341+
* (what System::PacketBuffer calls the "available size").
342+
*/
343+
size_t GetReportBufferMaxSize();
344+
336345
/**
337346
* Returns whether this ReadHandler represents a subscription that was created by the other side of the provided exchange.
338347
*/

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-5
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 reportBufferMaxSize = 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,15 @@ 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+
reportBufferMaxSize = apReadHandler->GetReportBufferMaxSize();
518+
519+
bufHandle = System::PacketBufferHandle::New(reportBufferMaxSize);
515520
VerifyOrExit(!bufHandle.IsNull(), err = CHIP_ERROR_NO_MEMORY);
516521

517-
if (bufHandle->AvailableDataLength() > kMaxSecureSduLengthBytes)
522+
if (bufHandle->AvailableDataLength() > reportBufferMaxSize)
518523
{
519-
reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - kMaxSecureSduLengthBytes);
524+
reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - reportBufferMaxSize);
520525
}
521526

522527
reportDataWriter.Init(std::move(bufHandle));
@@ -525,8 +530,8 @@ CHIP_ERROR Engine::BuildAndSendSingleReportData(ReadHandler * apReadHandler)
525530
reportDataWriter.ReserveBuffer(mReservedSize);
526531
#endif
527532

528-
// Always limit the size of the generated packet to fit within kMaxSecureSduLengthBytes regardless of the available buffer
529-
// capacity.
533+
// Always limit the size of the generated packet to fit within the max size returned by the ReadHandler regardless
534+
// of the available buffer capacity.
530535
// Also, we need to reserve some extra space for the MIC field.
531536
reportDataWriter.ReserveBuffer(static_cast<uint32_t>(reservedSize + chip::Crypto::CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES));
532537

0 commit comments

Comments
 (0)