Skip to content

Commit

Permalink
Refresh IM Invoke encoding with latest spec (#11169)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Mar 2, 2022
1 parent f80723a commit 1213431
Show file tree
Hide file tree
Showing 32 changed files with 2,240 additions and 936 deletions.
12 changes: 6 additions & 6 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ static_library("app") {
"MessageDef/Builder.cpp",
"MessageDef/Builder.h",
"MessageDef/CommandDataIB.cpp",
"MessageDef/CommandDataIB.h",
"MessageDef/CommandList.cpp",
"MessageDef/CommandList.h",
"MessageDef/CommandPathIB.cpp",
"MessageDef/CommandPathIB.h",
"MessageDef/CommandStatusIB.cpp",
"MessageDef/EventDataElement.cpp",
"MessageDef/EventDataElement.h",
"MessageDef/EventFilterIB.cpp",
Expand All @@ -75,8 +72,11 @@ static_library("app") {
"MessageDef/EventPathIB.h",
"MessageDef/EventPaths.cpp",
"MessageDef/EventPaths.h",
"MessageDef/InvokeCommand.cpp",
"MessageDef/InvokeCommand.h",
"MessageDef/InvokeRequestMessage.cpp",
"MessageDef/InvokeRequests.cpp",
"MessageDef/InvokeResponseIB.cpp",
"MessageDef/InvokeResponseMessage.cpp",
"MessageDef/InvokeResponses.cpp",
"MessageDef/ListBuilder.cpp",
"MessageDef/ListParser.cpp",
"MessageDef/MessageDefHelper.cpp",
Expand Down
156 changes: 4 additions & 152 deletions src/app/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
*/

#include "Command.h"
#include "CommandHandler.h"
#include "CommandSender.h"
#include "InteractionModelEngine.h"

#include <app/AppBuildConfig.h>
#include <lib/core/CHIPTLVDebug.hpp>

Expand All @@ -35,165 +31,21 @@ namespace app {

Command::Command() {}

CHIP_ERROR Command::AllocateBuffer()
{
CHIP_ERROR err = CHIP_NO_ERROR;
CommandList::Builder commandListBuilder;

if (!mBufferAllocated)
{
mCommandMessageWriter.Reset();

System::PacketBufferHandle commandPacket = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
VerifyOrExit(!commandPacket.IsNull(), err = CHIP_ERROR_NO_MEMORY);

mCommandMessageWriter.Init(std::move(commandPacket));
err = mInvokeCommandBuilder.Init(&mCommandMessageWriter);
SuccessOrExit(err);

commandListBuilder = mInvokeCommandBuilder.CreateCommandListBuilder();
err = commandListBuilder.GetError();
SuccessOrExit(err);

mCommandIndex = 0;

mBufferAllocated = true;
}

exit:
return err;
}

CHIP_ERROR Command::ProcessCommandMessage(System::PacketBufferHandle && payload, CommandRoleId aCommandRoleId)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::System::PacketBufferTLVReader reader;
chip::TLV::TLVReader commandListReader;
InvokeCommand::Parser invokeCommandParser;
CommandList::Parser commandListParser;

reader.Init(std::move(payload));
err = reader.Next();
SuccessOrExit(err);

err = invokeCommandParser.Init(reader);
SuccessOrExit(err);

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
err = invokeCommandParser.CheckSchemaValidity();
SuccessOrExit(err);
#endif
err = invokeCommandParser.GetCommandList(&commandListParser);
SuccessOrExit(err);

commandListParser.GetReader(&commandListReader);

while (CHIP_NO_ERROR == (err = commandListReader.Next()))
{
VerifyOrExit(chip::TLV::AnonymousTag == commandListReader.GetTag(), err = CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrExit(chip::TLV::kTLVType_Structure == commandListReader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);

CommandDataIB::Parser commandElement;

err = commandElement.Init(commandListReader);
SuccessOrExit(err);

err = ProcessCommandDataIB(commandElement);
SuccessOrExit(err);
}

// if we have exhausted this container
if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}

exit:
return err;
}

CHIP_ERROR Command::PrepareCommand(const CommandPathParams & aCommandPathParams, bool aStartDataStruct)
{
CHIP_ERROR err = CHIP_NO_ERROR;
CommandDataIB::Builder commandDataIB;

err = AllocateBuffer();
SuccessOrExit(err);

//
// We must not be in the middle of preparing a command, or having prepared or sent one.
//
VerifyOrExit(mState == CommandState::Idle, err = CHIP_ERROR_INCORRECT_STATE);

commandDataIB = mInvokeCommandBuilder.GetCommandListBuilder().CreateCommandDataIBBuilder();
err = commandDataIB.GetError();
SuccessOrExit(err);

err = ConstructCommandPath(aCommandPathParams, commandDataIB);
SuccessOrExit(err);

if (aStartDataStruct)
{
err = commandDataIB.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::kCsTag_Data), TLV::kTLVType_Structure,
mDataElementContainerType);
}

MoveToState(CommandState::AddingCommand);

exit:
return err;
}

TLV::TLVWriter * Command::GetCommandDataIBTLVWriter()
{
if (mState != CommandState::AddingCommand)
{
return nullptr;
}
else
{
return mInvokeCommandBuilder.GetCommandListBuilder().GetCommandDataIBBuilder().GetWriter();
}
}

CHIP_ERROR Command::Finalize(System::PacketBufferHandle & commandPacket)
{
VerifyOrReturnError(mState == CommandState::AddedCommand, CHIP_ERROR_INCORRECT_STATE);
return mCommandMessageWriter.Finalize(&commandPacket);
}

CHIP_ERROR Command::FinishCommand(bool aEndDataStruct)
CHIP_ERROR Command::ConstructCommandPath(const CommandPathParams & aCommandPathParams, CommandPathIB::Builder & aCommandPath)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrReturnError(mState == CommandState::AddingCommand, err = CHIP_ERROR_INCORRECT_STATE);

CommandDataIB::Builder commandDataIB = mInvokeCommandBuilder.GetCommandListBuilder().GetCommandDataIBBuilder();
if (aEndDataStruct)
{
ReturnErrorOnFailure(commandDataIB.GetWriter()->EndContainer(mDataElementContainerType));
}

ReturnErrorOnFailure(commandDataIB.EndOfCommandDataIB().GetError());
ReturnErrorOnFailure(mInvokeCommandBuilder.GetCommandListBuilder().EndOfCommandList().GetError());
ReturnErrorOnFailure(mInvokeCommandBuilder.EndOfInvokeCommand().GetError());

MoveToState(CommandState::AddedCommand);

return CHIP_NO_ERROR;
}

CHIP_ERROR Command::ConstructCommandPath(const CommandPathParams & aCommandPathParams, CommandDataIB::Builder aCommandDataIB)
{
CommandPathIB::Builder commandPath = aCommandDataIB.CreateCommandPathBuilder();
if (aCommandPathParams.mFlags.Has(CommandPathFlags::kEndpointIdValid))
{
commandPath.EndpointId(aCommandPathParams.mEndpointId);
aCommandPath.EndpointId(aCommandPathParams.mEndpointId);
}

commandPath.ClusterId(aCommandPathParams.mClusterId).CommandId(aCommandPathParams.mCommandId).EndOfCommandPath();

return commandPath.GetError();
aCommandPath.ClusterId(aCommandPathParams.mClusterId).CommandId(aCommandPathParams.mCommandId).EndOfCommandPathIB();
return aCommandPath.GetError();
}

void Command::Abort()
Expand Down
31 changes: 2 additions & 29 deletions src/app/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <app/ConcreteCommandPath.h>
#include <app/InteractionModelDelegate.h>
#include <app/MessageDef/CommandDataIB.h>
#include <app/MessageDef/CommandList.h>
#include <app/MessageDef/InvokeCommand.h>
#include <lib/core/CHIPCore.h>
#include <lib/support/BitFlags.h>
#include <lib/support/CodeUtils.h>
Expand All @@ -47,12 +45,6 @@ namespace app {
class Command
{
public:
enum class CommandRoleId
{
SenderId = 0,
HandlerId = 1,
};

enum class CommandState
{
Idle, ///< Default state that the object starts out in, where no work has commenced
Expand All @@ -70,12 +62,6 @@ class Command
*/
virtual ~Command() { Abort(); }

/*
* A set of methods to construct command request or response payloads
*/
CHIP_ERROR PrepareCommand(const CommandPathParams & aCommandPathParams, bool aStartDataStruct = true);
TLV::TLVWriter * GetCommandDataIBTLVWriter();
CHIP_ERROR FinishCommand(bool aEndDataStruct = true);
CHIP_ERROR Finalize(System::PacketBufferHandle & commandPacket);

virtual CHIP_ERROR AddStatus(const ConcreteCommandPath & aCommandPath, const Protocols::InteractionModel::Status aStatus)
Expand All @@ -102,19 +88,9 @@ class Command
*/
Messaging::ExchangeContext * GetExchangeContext() const { return mpExchangeCtx; }

virtual CHIP_ERROR ProcessCommandDataIB(CommandDataIB::Parser & aCommandElement) = 0;

protected:
Command();

/*
* Allocates a packet buffer used for encoding an invoke request/response payload.
*
* This can be called multiple times safely, as it will only allocate the buffer once for the lifetime
* of this object.
*/
CHIP_ERROR AllocateBuffer();

/*
* The actual closure of the exchange happens automatically in the exchange layer.
* This function just sets the internally tracked exchange pointer to null to align
Expand All @@ -123,15 +99,14 @@ class Command
void Close();

void MoveToState(const CommandState aTargetState);
CHIP_ERROR ProcessCommandMessage(System::PacketBufferHandle && payload, CommandRoleId aCommandRoleId);
CHIP_ERROR ConstructCommandPath(const CommandPathParams & aCommandPathParams, CommandDataIB::Builder aCommandDataIB);
CHIP_ERROR ConstructCommandPath(const CommandPathParams & aCommandPathParams, CommandPathIB::Builder & aCommandPath);
const char * GetStateStr() const;

InvokeCommand::Builder mInvokeCommandBuilder;
Messaging::ExchangeContext * mpExchangeCtx = nullptr;
uint8_t mCommandIndex = 0;
CommandState mState = CommandState::Idle;
chip::System::PacketBufferTLVWriter mCommandMessageWriter;
bool mBufferAllocated = false;

private:
/*
Expand All @@ -143,8 +118,6 @@ class Command
void Abort();

friend class TestCommandInteraction;
TLV::TLVType mDataElementContainerType = TLV::kTLVType_NotSpecified;
bool mBufferAllocated = false;
};
} // namespace app
} // namespace chip
Loading

0 comments on commit 1213431

Please sign in to comment.