Skip to content

Commit

Permalink
Merge branch 'master' into fix-opstate-phaselist
Browse files Browse the repository at this point in the history
  • Loading branch information
mideayanghui committed Jan 4, 2024
2 parents b4b7273 + 9279628 commit 5d1c066
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 93 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app-common/zap-generated/ids/Commands.h>
#include <commands/common/Commands.h>
#include <commands/clusters/ComplexArgument.h>
#include <commands/clusters/ClusterCommand.h>
#include <commands/clusters/ReportCommand.h>
Expand Down
4 changes: 3 additions & 1 deletion scripts/configure
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ function download_zap() {
case "$platform" in
Linux\ x86_64) flavor=linux-x64 ;;
Linux\ arm64) flavor=linux-arm64 ;;
Darwin\ *) flavor=mac-x64 ;; # there is no mac arm build of zap (can run x64 via Rosetta)
Linux\ aarch64) flavor=linux-arm64 ;;
Darwin\ x86_64) flavor=mac-x64 ;;
Darwin\ arm64) flavor=mac-arm64 ;;
*) fail "Unable to determine zap flavor for $platform" ;;
esac
local url="https://github.com/project-chip/zap/releases/download/${version}/zap-${flavor}.zip"
Expand Down
20 changes: 13 additions & 7 deletions src/app/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ CHIP_ERROR CommandHandler::AllocateBuffer()
VerifyOrReturnError(!commandPacket.IsNull(), CHIP_ERROR_NO_MEMORY);

mCommandMessageWriter.Init(std::move(commandPacket));
ReturnErrorOnFailure(mInvokeResponseBuilder.Init(&mCommandMessageWriter));
ReturnErrorOnFailure(mInvokeResponseBuilder.InitWithEndBufferReserved(&mCommandMessageWriter));

mInvokeResponseBuilder.SuppressResponse(mSuppressResponse);
ReturnErrorOnFailure(mInvokeResponseBuilder.GetError());

mInvokeResponseBuilder.CreateInvokeResponses();
mInvokeResponseBuilder.CreateInvokeResponses(/* aReserveEndBuffer = */ true);
ReturnErrorOnFailure(mInvokeResponseBuilder.GetError());

mBufferAllocated = true;
}

Expand Down Expand Up @@ -105,11 +106,16 @@ void CommandHandler::OnInvokeCommandRequest(Messaging::ExchangeContext * ec, con
mGoneAsync = true;
}

CHIP_ERROR CommandHandler::ValidateInvokeRequestsAndBuildRegistry(TLV::TLVReader & invokeRequestsReader)
CHIP_ERROR CommandHandler::ValidateInvokeRequestMessageAndBuildRegistry(InvokeRequestMessage::Parser & invokeRequestMessage)
{
CHIP_ERROR err = CHIP_NO_ERROR;
size_t commandCount = 0;
bool commandRefExpected = false;
InvokeRequests::Parser invokeRequests;

ReturnErrorOnFailure(invokeRequestMessage.GetInvokeRequests(&invokeRequests));
TLV::TLVReader invokeRequestsReader;
invokeRequests.GetReader(&invokeRequestsReader);

ReturnErrorOnFailure(TLV::Utilities::Count(invokeRequestsReader, commandCount, false /* recurse */));

Expand Down Expand Up @@ -166,7 +172,8 @@ CHIP_ERROR CommandHandler::ValidateInvokeRequestsAndBuildRegistry(TLV::TLVReader
{
err = CHIP_NO_ERROR;
}
return err;
ReturnErrorOnFailure(err);
return invokeRequestMessage.ExitContainer();
}

Status CommandHandler::ProcessInvokeRequest(System::PacketBufferHandle && payload, bool isTimedInvoke)
Expand All @@ -191,9 +198,8 @@ Status CommandHandler::ProcessInvokeRequest(System::PacketBufferHandle && payloa
VerifyOrReturnError(mTimedRequest == isTimedInvoke, Status::UnsupportedAccess);

{
TLV::TLVReader validationInvokeRequestsReader;
invokeRequests.GetReader(&validationInvokeRequestsReader);
VerifyOrReturnError(ValidateInvokeRequestsAndBuildRegistry(validationInvokeRequestsReader) == CHIP_NO_ERROR,
InvokeRequestMessage::Parser validationInvokeRequestMessage = invokeRequestMessage;
VerifyOrReturnError(ValidateInvokeRequestMessageAndBuildRegistry(validationInvokeRequestMessage) == CHIP_NO_ERROR,
Status::InvalidAction);
}

Expand Down
5 changes: 3 additions & 2 deletions src/app/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ class CommandHandler : public Messaging::ExchangeDelegate

/**
* Checks that all CommandDataIB within InvokeRequests satisfy the spec's general
* constraints for CommandDataIB.
* constraints for CommandDataIB. Additionally checks that InvokeRequestMessage is
* properly formatted.
*
* This also builds a registry that to ensure that all commands can be responded
* to with the data required as per spec.
*/
CHIP_ERROR ValidateInvokeRequestsAndBuildRegistry(TLV::TLVReader & invokeRequestsReader);
CHIP_ERROR ValidateInvokeRequestMessageAndBuildRegistry(InvokeRequestMessage::Parser & invokeRequestMessage);

/**
* Adds the given command status and returns any failures in adding statuses (e.g. out
Expand Down
4 changes: 2 additions & 2 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ CHIP_ERROR CommandSender::AllocateBuffer()
VerifyOrReturnError(!commandPacket.IsNull(), CHIP_ERROR_NO_MEMORY);

mCommandMessageWriter.Init(std::move(commandPacket));
ReturnErrorOnFailure(mInvokeRequestBuilder.Init(&mCommandMessageWriter));
ReturnErrorOnFailure(mInvokeRequestBuilder.InitWithEndBufferReserved(&mCommandMessageWriter));

mInvokeRequestBuilder.SuppressResponse(mSuppressResponse).TimedRequest(mTimedRequest);
ReturnErrorOnFailure(mInvokeRequestBuilder.GetError());

mInvokeRequestBuilder.CreateInvokeRequests();
mInvokeRequestBuilder.CreateInvokeRequests(/* aReserveEndBuffer = */ true);
ReturnErrorOnFailure(mInvokeRequestBuilder.GetError());

mBufferAllocated = true;
Expand Down
38 changes: 36 additions & 2 deletions src/app/MessageDef/InvokeRequestMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ CHIP_ERROR InvokeRequestMessage::Parser::GetInvokeRequests(InvokeRequests::Parse
return apInvokeRequests->Init(reader);
}

CHIP_ERROR InvokeRequestMessage::Builder::InitWithEndBufferReserved(TLV::TLVWriter * const apWriter)
{
ReturnErrorOnFailure(Init(apWriter));
ReturnErrorOnFailure(GetWriter()->ReserveBuffer(GetSizeToEndInvokeRequestMessage()));
mIsEndBufferReserved = true;
return CHIP_NO_ERROR;
}

InvokeRequestMessage::Builder & InvokeRequestMessage::Builder::SuppressResponse(const bool aSuppressResponse)
{
if (mError == CHIP_NO_ERROR)
Expand All @@ -131,17 +139,33 @@ InvokeRequestMessage::Builder & InvokeRequestMessage::Builder::TimedRequest(cons
return *this;
}

InvokeRequests::Builder & InvokeRequestMessage::Builder::CreateInvokeRequests()
InvokeRequests::Builder & InvokeRequestMessage::Builder::CreateInvokeRequests(const bool aReserveEndBuffer)
{
if (mError == CHIP_NO_ERROR)
{
mError = mInvokeRequests.Init(mpWriter, to_underlying(Tag::kInvokeRequests));
if (aReserveEndBuffer)
{
mError = mInvokeRequests.InitWithEndBufferReserved(mpWriter, to_underlying(Tag::kInvokeRequests));
}
else
{
mError = mInvokeRequests.Init(mpWriter, to_underlying(Tag::kInvokeRequests));
}
}
return mInvokeRequests;
}

CHIP_ERROR InvokeRequestMessage::Builder::EndOfInvokeRequestMessage()
{
// If any changes are made to how we end the invoke request message that involves how many
// bytes are needed, a corresponding change to GetSizeToEndInvokeRequestMessage indicating
// the new size that will be required.
ReturnErrorOnFailure(mError);
if (mIsEndBufferReserved)
{
ReturnErrorOnFailure(GetWriter()->UnreserveBuffer(GetSizeToEndInvokeRequestMessage()));
mIsEndBufferReserved = false;
}
if (mError == CHIP_NO_ERROR)
{
mError = MessageBuilder::EncodeInteractionModelRevision();
Expand All @@ -152,5 +176,15 @@ CHIP_ERROR InvokeRequestMessage::Builder::EndOfInvokeRequestMessage()
}
return GetError();
}

uint32_t InvokeRequestMessage::Builder::GetSizeToEndInvokeRequestMessage()
{
// EncodeInteractionModelRevision() encodes a uint8_t with context tag 0xFF. This means 1 control byte,
// 1 byte for the tag, 1 byte for the value.
uint32_t kEncodeInteractionModelSize = 1 + 1 + 1;
uint32_t kEndOfContainerSize = 1;

return kEncodeInteractionModelSize + kEndOfContainerSize;
}
}; // namespace app
}; // namespace chip
16 changes: 15 additions & 1 deletion src/app/MessageDef/InvokeRequestMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class Parser : public MessageParser
class Builder : public MessageBuilder
{
public:
/**
* @brief Performs underlying StructBuilder::Init, but reserves memory need in
* EndOfInvokeRequestMessage() with underlying TLVWriter.
*/
CHIP_ERROR InitWithEndBufferReserved(TLV::TLVWriter * const apWriter);

/**
* @brief when sets to true, it means do not send a response to this action
*/
Expand All @@ -90,7 +96,7 @@ class Builder : public MessageBuilder
*
* @return A reference to InvokeRequests::Builder
*/
InvokeRequests::Builder & CreateInvokeRequests();
InvokeRequests::Builder & CreateInvokeRequests(const bool aReserveEndBuffer = false);

/**
* @brief Get reference to InvokeRequests::Builder
Expand All @@ -106,8 +112,16 @@ class Builder : public MessageBuilder
*/
CHIP_ERROR EndOfInvokeRequestMessage();

/**
* @brief Get number of bytes required in the buffer by EndOfInvokeRequestMessage()
*
* @return Expected number of bytes required in the buffer by EndOfInvokeRequestMessage()
*/
uint32_t GetSizeToEndInvokeRequestMessage();

private:
InvokeRequests::Builder mInvokeRequests;
bool mIsEndBufferReserved = false;
};
} // namespace InvokeRequestMessage
} // namespace app
Expand Down
22 changes: 22 additions & 0 deletions src/app/MessageDef/InvokeRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ CHIP_ERROR InvokeRequests::Parser::PrettyPrint() const
}
#endif // CHIP_CONFIG_IM_PRETTY_PRINT

CHIP_ERROR InvokeRequests::Builder::InitWithEndBufferReserved(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse)
{
ReturnErrorOnFailure(Init(apWriter, aContextTagToUse));
ReturnErrorOnFailure(GetWriter()->ReserveBuffer(GetSizeToEndInvokeRequests()));
mIsEndBufferReserved = true;
return CHIP_NO_ERROR;
}

CommandDataIB::Builder & InvokeRequests::Builder::CreateCommandData()
{
if (mError == CHIP_NO_ERROR)
Expand All @@ -81,8 +89,22 @@ CommandDataIB::Builder & InvokeRequests::Builder::CreateCommandData()

CHIP_ERROR InvokeRequests::Builder::EndOfInvokeRequests()
{
// If any changes are made to how we end the invoke requests that involves how many bytes are
// needed, a corresponding change to GetSizeToEndInvokeRequests indicating the new size that
// will be required.
if (mIsEndBufferReserved)
{
ReturnErrorOnFailure(GetWriter()->UnreserveBuffer(GetSizeToEndInvokeRequests()));
mIsEndBufferReserved = false;
}
EndOfContainer();
return GetError();
}

uint32_t InvokeRequests::Builder::GetSizeToEndInvokeRequests()
{
uint32_t kEndOfContainerSize = 1;
return kEndOfContainerSize;
}
} // namespace app
} // namespace chip
14 changes: 14 additions & 0 deletions src/app/MessageDef/InvokeRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Parser : public ArrayParser
class Builder : public ArrayBuilder
{
public:
/**
* @brief Performs underlying StructBuilder::Init, but reserves memory need in
* EndOfInvokeRequests() with underlying TLVWriter.
*/
CHIP_ERROR InitWithEndBufferReserved(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse);

/**
* @brief Initialize a CommandDataIB::Builder for writing into the TLV stream
*
Expand All @@ -61,8 +67,16 @@ class Builder : public ArrayBuilder
*/
CHIP_ERROR EndOfInvokeRequests();

/**
* @brief Get number of bytes required in the buffer by EndOfInvokeRequests()
*
* @return Expected number of bytes required in the buffer by EndOfInvokeRequests()
*/
uint32_t GetSizeToEndInvokeRequests();

private:
CommandDataIB::Builder mCommandData;
bool mIsEndBufferReserved = false;
};
} // namespace InvokeRequests
} // namespace app
Expand Down
22 changes: 22 additions & 0 deletions src/app/MessageDef/InvokeResponseIBs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ CHIP_ERROR InvokeResponseIBs::Parser::PrettyPrint() const
}
#endif // CHIP_CONFIG_IM_PRETTY_PRINT

CHIP_ERROR InvokeResponseIBs::Builder::InitWithEndBufferReserved(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse)
{
ReturnErrorOnFailure(Init(apWriter, aContextTagToUse));
ReturnErrorOnFailure(GetWriter()->ReserveBuffer(GetSizeToEndInvokeResponses()));
mIsEndBufferReserved = true;
return CHIP_NO_ERROR;
}

InvokeResponseIB::Builder & InvokeResponseIBs::Builder::CreateInvokeResponse()
{
if (mError == CHIP_NO_ERROR)
Expand All @@ -81,8 +89,22 @@ InvokeResponseIB::Builder & InvokeResponseIBs::Builder::CreateInvokeResponse()

CHIP_ERROR InvokeResponseIBs::Builder::EndOfInvokeResponses()
{
// If any changes are made to how we end the invoke responses that involves how many bytes are
// needed, a corresponding change to GetSizeToEndInvokeResponses indicating the new size that
// will be required.
if (mIsEndBufferReserved)
{
ReturnErrorOnFailure(GetWriter()->UnreserveBuffer(GetSizeToEndInvokeResponses()));
mIsEndBufferReserved = false;
}
EndOfContainer();
return GetError();
}

uint32_t InvokeResponseIBs::Builder::GetSizeToEndInvokeResponses()
{
uint32_t kEndOfContainerSize = 1;
return kEndOfContainerSize;
}
} // namespace app
} // namespace chip
14 changes: 14 additions & 0 deletions src/app/MessageDef/InvokeResponseIBs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Parser : public ArrayParser
class Builder : public ArrayBuilder
{
public:
/**
* @brief Performs underlying StructBuilder::Init, but reserves memory need in
* EndOfInvokeResponses() with underlying TLVWriter.
*/
CHIP_ERROR InitWithEndBufferReserved(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse);

/**
* @brief Initialize a InvokeResponseIB::Builder for writing into the TLV stream
*
Expand All @@ -61,8 +67,16 @@ class Builder : public ArrayBuilder
*/
CHIP_ERROR EndOfInvokeResponses();

/**
* @brief Get number of bytes required in the buffer by EndOfInvokeResponses()
*
* @return Expected number of bytes required in the buffer by EndOfInvokeResponses()
*/
uint32_t GetSizeToEndInvokeResponses();

private:
InvokeResponseIB::Builder mInvokeResponse;
bool mIsEndBufferReserved = false;
};
} // namespace InvokeResponseIBs
} // namespace app
Expand Down
Loading

0 comments on commit 5d1c066

Please sign in to comment.