Skip to content

Commit

Permalink
Improve IM MessageDef
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Jun 1, 2022
1 parent 3956a9d commit 8ba39ae
Show file tree
Hide file tree
Showing 49 changed files with 236 additions and 303 deletions.
6 changes: 3 additions & 3 deletions src/app/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ CHIP_ERROR CommandHandler::ProcessCommandDataIB(CommandDataIB::Parser & aCommand
return AddStatus(concretePath, Protocols::InteractionModel::Status::NeedsTimedInteraction);
}

err = aCommandElement.GetData(&commandDataReader);
err = aCommandElement.GetFields(&commandDataReader);
if (CHIP_END_OF_TLV == err)
{
ChipLogDetail(DataManagement,
Expand Down Expand Up @@ -359,7 +359,7 @@ CHIP_ERROR CommandHandler::ProcessGroupCommandDataIB(CommandDataIB::Parser & aCo
ChipLogDetail(DataManagement, "Received group command for Group=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI,
groupId, ChipLogValueMEI(clusterId), ChipLogValueMEI(commandId));

err = aCommandElement.GetData(&commandDataReader);
err = aCommandElement.GetFields(&commandDataReader);
if (CHIP_END_OF_TLV == err)
{
ChipLogDetail(DataManagement,
Expand Down Expand Up @@ -488,7 +488,7 @@ CHIP_ERROR CommandHandler::PrepareCommand(const ConcreteCommandPath & aCommandPa
ReturnErrorOnFailure(path.Encode(aCommandPath));
if (aStartDataStruct)
{
ReturnErrorOnFailure(commandData.GetWriter()->StartContainer(TLV::ContextTag(to_underlying(CommandDataIB::Tag::kData)),
ReturnErrorOnFailure(commandData.GetWriter()->StartContainer(TLV::ContextTag(to_underlying(CommandDataIB::Tag::kFields)),
TLV::kTLVType_Structure, mDataElementContainerType));
}
MoveToState(State::AddingCommand);
Expand Down
2 changes: 1 addition & 1 deletion src/app/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class CommandHandler
ReturnErrorOnFailure(PrepareCommand(path, false));
TLV::TLVWriter * writer = GetCommandDataIBTLVWriter();
VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(DataModel::Encode(*writer, TLV::ContextTag(to_underlying(CommandDataIB::Tag::kData)), aData));
ReturnErrorOnFailure(DataModel::Encode(*writer, TLV::ContextTag(to_underlying(CommandDataIB::Tag::kFields)), aData));

return FinishCommand(/* aEndDataStruct = */ false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ CHIP_ERROR CommandSender::ProcessInvokeResponseIB(InvokeResponseIB::Parser & aIn
ReturnErrorOnFailure(commandPath.GetEndpointId(&endpointId));
ReturnErrorOnFailure(commandPath.GetClusterId(&clusterId));
ReturnErrorOnFailure(commandPath.GetCommandId(&commandId));
commandData.GetData(&commandDataReader);
commandData.GetFields(&commandDataReader);
err = CHIP_NO_ERROR;
hasDataResponse = true;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ CHIP_ERROR CommandSender::PrepareCommand(const CommandPathParams & aCommandPathP

if (aStartDataStruct)
{
ReturnErrorOnFailure(invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(to_underlying(CommandDataIB::Tag::kData)),
ReturnErrorOnFailure(invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(to_underlying(CommandDataIB::Tag::kFields)),
TLV::kTLVType_Structure, mDataElementContainerType));
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/CommandSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class CommandSender final : public Messaging::ExchangeDelegate
ReturnErrorOnFailure(PrepareCommand(aCommandPath, /* aStartDataStruct = */ false));
TLV::TLVWriter * writer = GetCommandDataIBTLVWriter();
VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(DataModel::Encode(*writer, TLV::ContextTag(to_underlying(CommandDataIB::Tag::kData)), aData));
ReturnErrorOnFailure(DataModel::Encode(*writer, TLV::ContextTag(to_underlying(CommandDataIB::Tag::kFields)), aData));
return FinishCommand(aTimedInvokeTimeoutMs);
}

Expand Down
3 changes: 1 addition & 2 deletions src/app/MessageDef/ArrayParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ CHIP_ERROR ArrayParser::Init(const TLV::TLVReader & aReader)
{
mReader.Init(aReader);
VerifyOrReturnError(TLV::kTLVType_Array == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return mReader.EnterContainer(mOuterContainerType);
}
} // namespace app
} // namespace chip
10 changes: 4 additions & 6 deletions src/app/MessageDef/AttributeDataIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ AttributeDataIB::Parser::ParseData(TLV::TLVReader & aReader, int aDepth) const

if (aDepth == 0)
{
PRETTY_PRINT("\tData = ");
PRETTY_PRINT("Data = ");
}
else
{
Expand Down Expand Up @@ -261,21 +261,19 @@ CHIP_ERROR AttributeDataIB::Parser::CheckSchemaValidity() const
}
else
{
err = CHIP_ERROR_IM_MALFORMED_EVENT_DATA_ELEMENT;
err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_DATA_ELEMENT;
}
}
ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

CHIP_ERROR AttributeDataIB::Parser::GetPath(AttributePathIB::Parser * const apPath) const
{
TLV::TLVReader reader;
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kPath)), reader));
ReturnErrorOnFailure(apPath->Init(reader));
return CHIP_NO_ERROR;
return apPath->Init(reader);
}

CHIP_ERROR AttributeDataIB::Parser::GetDataVersion(chip::DataVersion * const apVersion) const
Expand Down
24 changes: 9 additions & 15 deletions src/app/MessageDef/AttributeDataIBs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace app {
CHIP_ERROR AttributeDataIBs::Parser::CheckSchemaValidity() const
{
CHIP_ERROR err = CHIP_NO_ERROR;
size_t NumDataElement = 0;
size_t numDataElement = 0;
chip::TLV::TLVReader reader;

PRETTY_PRINT("AttributeDataIBs =");
Expand All @@ -51,21 +51,19 @@ CHIP_ERROR AttributeDataIBs::Parser::CheckSchemaValidity() const

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

{
AttributeDataIB::Parser data;
err = data.Init(reader);
SuccessOrExit(err);
ReturnErrorOnFailure(data.Init(reader));

PRETTY_PRINT_INCDEPTH();
err = data.CheckSchemaValidity();
SuccessOrExit(err);
ReturnErrorOnFailure(data.CheckSchemaValidity());
PRETTY_PRINT_DECDEPTH();
}

++NumDataElement;
++numDataElement;
}

PRETTY_PRINT("],");
Expand All @@ -75,17 +73,13 @@ CHIP_ERROR AttributeDataIBs::Parser::CheckSchemaValidity() const
if (CHIP_END_OF_TLV == err)
{
// if we have at least one data element
if (NumDataElement > 0)
if (numDataElement > 0)
{
err = CHIP_NO_ERROR;
}
}
SuccessOrExit(err);
err = reader.ExitContainer(mOuterContainerType);

exit:

return err;
ReturnErrorOnFailure(err);
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

Expand Down
3 changes: 1 addition & 2 deletions src/app/MessageDef/AttributePathIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ CHIP_ERROR AttributePathIB::Parser::CheckSchemaValidity() const
}

ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

Expand Down
3 changes: 1 addition & 2 deletions src/app/MessageDef/AttributePathIBs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ CHIP_ERROR AttributePathIBs::Parser::CheckSchemaValidity() const
}
}
ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

Expand Down
13 changes: 5 additions & 8 deletions src/app/MessageDef/AttributeReportIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ CHIP_ERROR AttributeReportIB::Parser::CheckSchemaValidity() const
if ((TagPresenceMask & CheckDataField) == CheckDataField && (TagPresenceMask & CheckStatusField) == CheckStatusField)
{
// kAttributeData and kAttributeStatus both exist
err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_REPORT_MESSAGE;
err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_REPORT_IB;
}
else if ((TagPresenceMask & CheckDataField) != CheckDataField && (TagPresenceMask & CheckStatusField) != CheckStatusField)
{
// kPath and kErrorStatus not exist
err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_REPORT_MESSAGE;
err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_REPORT_IB;
}
else
{
Expand All @@ -108,25 +108,22 @@ CHIP_ERROR AttributeReportIB::Parser::CheckSchemaValidity() const
}

ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

CHIP_ERROR AttributeReportIB::Parser::GetAttributeStatus(AttributeStatusIB::Parser * const apAttributeStatus) const
{
TLV::TLVReader reader;
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kAttributeStatus)), reader));
ReturnErrorOnFailure(apAttributeStatus->Init(reader));
return CHIP_NO_ERROR;
return apAttributeStatus->Init(reader);
}

CHIP_ERROR AttributeReportIB::Parser::GetAttributeData(AttributeDataIB::Parser * const apAttributeData) const
{
TLV::TLVReader reader;
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kAttributeData)), reader));
ReturnErrorOnFailure(apAttributeData->Init(reader));
return CHIP_NO_ERROR;
return apAttributeData->Init(reader);
}

AttributeStatusIB::Builder & AttributeReportIB::Builder::CreateAttributeStatus()
Expand Down
3 changes: 1 addition & 2 deletions src/app/MessageDef/AttributeReportIBs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ CHIP_ERROR AttributeReportIBs::Parser::CheckSchemaValidity() const
err = CHIP_NO_ERROR;
}
ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

Expand Down
13 changes: 7 additions & 6 deletions src/app/MessageDef/AttributeStatusIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,29 @@ CHIP_ERROR AttributeStatusIB::Parser::CheckSchemaValidity() const
{
err = CHIP_NO_ERROR;
}
else
{
err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_STATUS_IB;
}
}

ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

CHIP_ERROR AttributeStatusIB::Parser::GetPath(AttributePathIB::Parser * const apPath) const
{
TLV::TLVReader reader;
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kPath)), reader));
ReturnErrorOnFailure(apPath->Init(reader));
return CHIP_NO_ERROR;
return apPath->Init(reader);
}

CHIP_ERROR AttributeStatusIB::Parser::GetErrorStatus(StatusIB::Parser * const apErrorStatus) const
{
TLV::TLVReader reader;
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kErrorStatus)), reader));
ReturnErrorOnFailure(apErrorStatus->Init(reader));
return CHIP_NO_ERROR;
return apErrorStatus->Init(reader);
}

AttributePathIB::Builder & AttributeStatusIB::Builder::CreatePath()
Expand Down
15 changes: 2 additions & 13 deletions src/app/MessageDef/AttributeStatusIBs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,10 @@ CHIP_ERROR AttributeStatusIBs::Parser::CheckSchemaValidity() const
// if we have exhausted this container
if (CHIP_END_OF_TLV == err)
{
// if we have at least one data element
if (NumAttributeStatus > 0)
{
err = CHIP_NO_ERROR;
}
// NOTE: temporarily disable this check, to allow test to continue
else
{
ChipLogError(DataManagement, "PROTOCOL ERROR: Empty attribute statuses");
err = CHIP_NO_ERROR;
}
err = CHIP_NO_ERROR;
}
ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif
}; // namespace app
Expand Down
26 changes: 12 additions & 14 deletions src/app/MessageDef/CommandDataIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
namespace chip {
namespace app {
CHIP_ERROR
CommandDataIB::Parser::ParseData(TLV::TLVReader & aReader, int aDepth) const
CommandDataIB::Parser::ParseFields(TLV::TLVReader & aReader, int aDepth) const
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (aDepth == 0)
{
PRETTY_PRINT("\tCommandData = ");
PRETTY_PRINT("\tCommandFields = ");
}
else
{
Expand Down Expand Up @@ -183,7 +183,7 @@ CommandDataIB::Parser::ParseData(TLV::TLVReader & aReader, int aDepth) const
{
PRETTY_PRINT_INCDEPTH();

err = ParseData(aReader, aDepth + 1);
err = ParseFields(aReader, aDepth + 1);
SuccessOrExit(err);

PRETTY_PRINT_DECDEPTH();
Expand Down Expand Up @@ -235,11 +235,11 @@ CHIP_ERROR CommandDataIB::Parser::CheckSchemaValidity() const
}

break;
case to_underlying(Tag::kData):
case to_underlying(Tag::kFields):
// check if this tag has appeared before
VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kData))), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << to_underlying(Tag::kData));
ReturnErrorOnFailure(ParseData(reader, 0));
VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kFields))), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << to_underlying(Tag::kFields));
ReturnErrorOnFailure(ParseFields(reader, 0));
break;
default:
PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
Expand All @@ -260,27 +260,25 @@ CHIP_ERROR CommandDataIB::Parser::CheckSchemaValidity() const
}
else
{
err = CHIP_ERROR_IM_MALFORMED_INVOKE_RESPONSE_MESSAGE;
err = CHIP_ERROR_IM_MALFORMED_COMMAND_DATA_IB;
}
}

ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType));
return CHIP_NO_ERROR;
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

CHIP_ERROR CommandDataIB::Parser::GetPath(CommandPathIB::Parser * const apPath) const
{
TLV::TLVReader reader;
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kPath)), reader));
ReturnErrorOnFailure(apPath->Init(reader));
return CHIP_NO_ERROR;
return apPath->Init(reader);
}

CHIP_ERROR CommandDataIB::Parser::GetData(TLV::TLVReader * const apReader) const
CHIP_ERROR CommandDataIB::Parser::GetFields(TLV::TLVReader * const apReader) const
{
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kData)), *apReader));
ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kFields)), *apReader));
return CHIP_NO_ERROR;
}

Expand Down
Loading

0 comments on commit 8ba39ae

Please sign in to comment.