Skip to content

Commit

Permalink
Reset -> Rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Mar 15, 2022
1 parent da77cb0 commit 3ac709c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/app/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ CHIP_ERROR CommandHandler::AddClusterSpecificFailure(const ConcreteCommandPath &
CHIP_ERROR CommandHandler::PrepareCommand(const ConcreteCommandPath & aCommandPath, bool aStartDataStruct)
{
ReturnErrorOnFailure(AllocateBuffer());

mInvokeResponseBuilder.Checkpoint(mBackupWriter);
//
// We must not be in the middle of preparing a command, or having prepared or sent one.
//
Expand Down Expand Up @@ -512,6 +514,7 @@ CHIP_ERROR CommandHandler::FinishCommand(bool aStartDataStruct)
CHIP_ERROR CommandHandler::PrepareStatus(const ConcreteCommandPath & aCommandPath)
{
ReturnErrorOnFailure(AllocateBuffer());
mInvokeResponseBuilder.Checkpoint(mBackupWriter);
//
// We must not be in the middle of preparing a command, or having prepared or sent one.
//
Expand Down Expand Up @@ -540,13 +543,14 @@ CHIP_ERROR CommandHandler::FinishStatus()
return CHIP_NO_ERROR;
}

CHIP_ERROR CommandHandler::ResetResponse()
CHIP_ERROR CommandHandler::RollbackResponse()
{
VerifyOrReturnError(mState == State::Idle || mState == State::AddedCommand || mState == State::AddingCommand,
CHIP_ERROR_INCORRECT_STATE);
// Calling mCommandMessageWriter will release its underlying buffer, thus we can allocate another one when encode something.
mCommandMessageWriter.Reset();
mBufferAllocated = false;
mInvokeResponseBuilder.Rollback(mBackupWriter);
mInvokeResponseBuilder.ResetError();
// Note: We only support one command per request, so we reset the state to Idle here, need to review the states when adding
// supports of having multiple requests in the same transaction.
MoveToState(State::Idle);
return CHIP_NO_ERROR;
}
Expand Down
11 changes: 4 additions & 7 deletions src/app/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,9 @@ class CommandHandler
FabricIndex GetAccessingFabricIndex() const;

/**
* API for resetting the internal response builder, useful when encoding the response manually.
* The TLVWriter got from GetCommandDataIBTLVWriter will be invalid after calling this.
*
* After calling this, users must call PrepareCommand or PrepareStatus before encoding something else. AddResponseData and
* AddStatus will handle this correctly.
* Rollback the state to before encoding the current ResponseData (before calling PrepareCommand / PrepareStatus)
*/
CHIP_ERROR ResetResponse();
CHIP_ERROR RollbackResponse();

/**
* API for adding a data response. The template parameter T is generally
Expand All @@ -192,7 +188,7 @@ class CommandHandler
{
// We have verified the state above, so this call must success since we must be one of the state required by
// ResetResponse.
ResetResponse();
RollbackResponse();
}
return err;
}
Expand Down Expand Up @@ -335,6 +331,7 @@ class CommandHandler

State mState = State::Idle;
chip::System::PacketBufferTLVWriter mCommandMessageWriter;
TLV::TLVWriter mBackupWriter;
bool mBufferAllocated = false;
};

Expand Down

0 comments on commit 3ac709c

Please sign in to comment.