Skip to content

Commit 158502f

Browse files
authored
Allow reserving space for MoreChunkedMessages in InvokeResponseMessage (#31301)
1 parent 62d38c5 commit 158502f

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

src/app/MessageDef/InvokeResponseMessage.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,32 @@ InvokeResponseIBs::Builder & InvokeResponseMessage::Builder::CreateInvokeRespons
147147

148148
InvokeResponseMessage::Builder & InvokeResponseMessage::Builder::MoreChunkedMessages(const bool aMoreChunkedMessages)
149149
{
150+
// If any changes are made to how we encoded MoreChunkedMessage that involves how many
151+
// bytes are needed, a corresponding change to GetSizeForMoreChunkResponses indicating
152+
// the new size that will be required.
153+
150154
// skip if error has already been set
151-
if (mError == CHIP_NO_ERROR)
155+
SuccessOrExit(mError);
156+
157+
if (mIsMoreChunkMessageBufferReserved)
152158
{
153-
mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kMoreChunkedMessages), aMoreChunkedMessages);
159+
mError = GetWriter()->UnreserveBuffer(GetSizeForMoreChunkResponses());
160+
SuccessOrExit(mError);
161+
mIsMoreChunkMessageBufferReserved = false;
154162
}
163+
164+
mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kMoreChunkedMessages), aMoreChunkedMessages);
165+
exit:
155166
return *this;
156167
}
157168

169+
CHIP_ERROR InvokeResponseMessage::Builder::ReserveSpaceForMoreChunkedMessages()
170+
{
171+
ReturnErrorOnFailure(GetWriter()->ReserveBuffer(GetSizeForMoreChunkResponses()));
172+
mIsMoreChunkMessageBufferReserved = true;
173+
return CHIP_NO_ERROR;
174+
}
175+
158176
CHIP_ERROR InvokeResponseMessage::Builder::EndOfInvokeResponseMessage()
159177
{
160178
// If any changes are made to how we end the invoke response message that involves how many
@@ -177,6 +195,15 @@ CHIP_ERROR InvokeResponseMessage::Builder::EndOfInvokeResponseMessage()
177195
return GetError();
178196
}
179197

198+
uint32_t InvokeResponseMessage::Builder::GetSizeForMoreChunkResponses()
199+
{
200+
// MoreChunkedMessages() encodes a uint8_t with context tag 0x02. This means 1 control byte,
201+
// 1 byte for the tag. For booleans the value is encoded in control byte.
202+
uint32_t kEncodeMoreChunkedMessages = 1 + 1;
203+
204+
return kEncodeMoreChunkedMessages;
205+
}
206+
180207
uint32_t InvokeResponseMessage::Builder::GetSizeToEndInvokeResponseMessage()
181208
{
182209
// EncodeInteractionModelRevision() encodes a uint8_t with context tag 0xFF. This means 1 control byte,

src/app/MessageDef/InvokeResponseMessage.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,27 @@ class Builder : public MessageBuilder
110110
*/
111111
InvokeResponseMessage::Builder & MoreChunkedMessages(const bool aMoreChunkedMessages);
112112

113+
/**
114+
* @brief Reserved space in TLVWriter for MoreChunkedMessages
115+
* @return CHIP_NO_ERROR upon successfully reserving space for MoreChunkedMessages
116+
* @return other CHIP error see TLVWriter::ReserveBuffer for more details.
117+
*/
118+
CHIP_ERROR ReserveSpaceForMoreChunkedMessages();
119+
113120
/**
114121
* @brief Mark the end of this InvokeResponseMessage
115122
*
116123
* @return The builder's final status.
117124
*/
118125
CHIP_ERROR EndOfInvokeResponseMessage();
119126

127+
/**
128+
* @brief Get number of bytes required in the buffer by MoreChunkedMessages
129+
*
130+
* @return Expected number of bytes required in the buffer by MoreChunkedMessages()
131+
*/
132+
uint32_t GetSizeForMoreChunkResponses();
133+
120134
/**
121135
* @brief Get number of bytes required in the buffer by EndOfInvokeResponseMessage()
122136
*
@@ -126,7 +140,8 @@ class Builder : public MessageBuilder
126140

127141
private:
128142
InvokeResponseIBs::Builder mInvokeResponses;
129-
bool mIsEndBufferReserved = false;
143+
bool mIsEndBufferReserved = false;
144+
bool mIsMoreChunkMessageBufferReserved = false;
130145
};
131146
} // namespace InvokeResponseMessage
132147
} // namespace app

src/app/tests/TestMessageDef.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,29 @@ void InvokeResponseMessageEndOfMessageReservationTest(nlTestSuite * apSuite, voi
21362136
NL_TEST_ASSERT(apSuite, remainingLengthAfterInitWithReservation == remainingLengthAfterEndingInvokeResponseMessage);
21372137
}
21382138

2139+
void InvokeResponseMessageReservationForEndandMoreChunkTest(nlTestSuite * apSuite, void * apContext)
2140+
{
2141+
CHIP_ERROR err = CHIP_NO_ERROR;
2142+
chip::System::PacketBufferTLVWriter writer;
2143+
InvokeResponseMessage::Builder invokeResponseMessageBuilder;
2144+
const uint32_t kSmallBufferSize = 100;
2145+
writer.Init(chip::System::PacketBufferHandle::New(kSmallBufferSize, /* aReservedSize = */ 0), /* useChainedBuffers = */ false);
2146+
err = invokeResponseMessageBuilder.InitWithEndBufferReserved(&writer);
2147+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
2148+
err = invokeResponseMessageBuilder.ReserveSpaceForMoreChunkedMessages();
2149+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
2150+
2151+
uint32_t remainingLengthAllReservations = writer.GetRemainingFreeLength();
2152+
2153+
invokeResponseMessageBuilder.MoreChunkedMessages(/* aMoreChunkedMessages = */ true);
2154+
NL_TEST_ASSERT(apSuite, invokeResponseMessageBuilder.GetError() == CHIP_NO_ERROR);
2155+
err = invokeResponseMessageBuilder.EndOfInvokeResponseMessage();
2156+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
2157+
2158+
uint32_t remainingLengthAfterEndingInvokeResponseMessage = writer.GetRemainingFreeLength();
2159+
NL_TEST_ASSERT(apSuite, remainingLengthAllReservations == remainingLengthAfterEndingInvokeResponseMessage);
2160+
}
2161+
21392162
void InvokeResponsesEndOfResponseReservationTest(nlTestSuite * apSuite, void * apContext)
21402163
{
21412164
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -2373,6 +2396,7 @@ const nlTest sTests[] =
23732396
NL_TEST_DEF("InvokeRequestsEndOfRequestReservationTest", InvokeRequestsEndOfRequestReservationTest),
23742397
NL_TEST_DEF("InvokeInvokeResponseMessageTest", InvokeInvokeResponseMessageTest),
23752398
NL_TEST_DEF("InvokeResponseMessageEndOfMessageReservationTest", InvokeResponseMessageEndOfMessageReservationTest),
2399+
NL_TEST_DEF("InvokeResponseMessageReservationForEndandMoreChunkTest", InvokeResponseMessageReservationForEndandMoreChunkTest),
23762400
NL_TEST_DEF("InvokeResponsesEndOfResponseReservationTest", InvokeResponsesEndOfResponseReservationTest),
23772401
NL_TEST_DEF("ReportDataMessageTest", ReportDataMessageTest),
23782402
NL_TEST_DEF("ReadRequestMessageTest", ReadRequestMessageTest),

0 commit comments

Comments
 (0)