Skip to content

Commit

Permalink
Add checkout and rollback API in IM message builder (project-chip#5564)
Browse files Browse the repository at this point in the history
Problem:
Need checkpoint API to get the previous IM TLV buffer state, when writen
data is oversized, need rollback API to get last good IM TLV buffer
State.

Summary of Changes:
-- Add Checkpoint API to Checkpoint the current tlv state into a TLVWriter
-- Add Rollback the request state into the checkpointed TLVWriter
-- Add unit test
Note: this is one sliced PR from PR 4800
  • Loading branch information
yunhanw-google authored Mar 26, 2021
1 parent dd6468e commit 1a94b9e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/app/MessageDef/Builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ class Builder
*/
chip::TLV::TLVWriter * GetWriter() { return mpWriter; };

/**
* Checkpoint the current tlv state into a TLVWriter
*
* @param[out] aPoint A writer to checkpoint the state of the TLV writer into.
* This writer must not outlive the builder
*/
void Checkpoint(chip::TLV::TLVWriter & aPoint) { aPoint = *mpWriter; };

/**
* Rollback the request state to the checkpointed TLVWriter
*
* @param[in] aPoint A that captured the state via Checkpoint() at some point in the past
*/
void Rollback(const chip::TLV::TLVWriter & aPoint) { *mpWriter = aPoint; }

protected:
CHIP_ERROR mError;
chip::TLV::TLVWriter * mpWriter;
Expand Down
51 changes: 51 additions & 0 deletions src/app/tests/TestMessageDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,56 @@ void ReadRequestTest(nlTestSuite * apSuite, void * apContext)
ParseReadRequest(apSuite, reader);
}

void CheckPointRollbackTest(nlTestSuite * apSuite, void * apContext)
{
CHIP_ERROR err = CHIP_NO_ERROR;
size_t NumDataElement = 0;
chip::System::PacketBufferTLVWriter writer;
chip::System::PacketBufferTLVReader reader;
AttributeDataList::Parser attributeDataListParser;
chip::TLV::TLVWriter checkpoint;
writer.Init(chip::System::PacketBufferHandle::New(chip::System::PacketBuffer::kMaxSize));
AttributeDataList::Builder attributeDataListBuilder;
attributeDataListBuilder.Init(&writer);

// encode one attribute element
AttributeDataElement::Builder attributeDataElementBuilder1 = attributeDataListBuilder.CreateAttributeDataElementBuilder();
NL_TEST_ASSERT(apSuite, attributeDataListBuilder.GetError() == CHIP_NO_ERROR);
BuildAttributeDataElement(apSuite, attributeDataElementBuilder1);
// checkpoint
attributeDataListBuilder.Checkpoint(checkpoint);
// encode another attribute element
AttributeDataElement::Builder attributeDataElementBuilder2 = attributeDataListBuilder.CreateAttributeDataElementBuilder();
NL_TEST_ASSERT(apSuite, attributeDataListBuilder.GetError() == CHIP_NO_ERROR);
BuildAttributeDataElement(apSuite, attributeDataElementBuilder2);
// rollback to previous checkpoint
attributeDataListBuilder.Rollback(checkpoint);

attributeDataListBuilder.EndOfAttributeDataList();
NL_TEST_ASSERT(apSuite, attributeDataListBuilder.GetError() == CHIP_NO_ERROR);

chip::System::PacketBufferHandle buf;
err = writer.Finalize(&buf);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

DebugPrettyPrint(buf);

reader.Init(std::move(buf));
err = reader.Next();
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

err = attributeDataListParser.Init(reader);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
attributeDataListParser.CheckSchemaValidity();

while (CHIP_NO_ERROR == (err = attributeDataListParser.Next()))
{
++NumDataElement;
}

NL_TEST_ASSERT(apSuite, NumDataElement == 1);
}

/**
* Test Suite. It lists all the test functions.
*/
Expand All @@ -1176,6 +1226,7 @@ const nlTest sTests[] =
NL_TEST_DEF("ReportDataTest", ReportDataTest),
NL_TEST_DEF("InvokeCommandTest", InvokeCommandTest),
NL_TEST_DEF("ReadRequestTest", ReadRequestTest),
NL_TEST_DEF("CheckPointRollbackTest", CheckPointRollbackTest),
NL_TEST_SENTINEL()
};
// clang-format on
Expand Down

0 comments on commit 1a94b9e

Please sign in to comment.