Skip to content

Commit

Permalink
Improve im encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed May 31, 2022
1 parent a242d80 commit 3303e8a
Show file tree
Hide file tree
Showing 38 changed files with 187 additions and 204 deletions.
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
5 changes: 2 additions & 3 deletions src/app/MessageDef/CommandDataIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,12 @@ 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

Expand Down
32 changes: 14 additions & 18 deletions src/app/MessageDef/CommandPathIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CHIP_ERROR CommandPathIB::Parser::CheckSchemaValidity() const
{
CHIP_ERROR err = CHIP_NO_ERROR;
int TagPresenceMask = 0;
chip::TLV::TLVReader reader;
TLV::TLVReader reader;
PRETTY_PRINT("CommandPathIB =");
PRETTY_PRINT("{");

Expand All @@ -54,40 +54,40 @@ CHIP_ERROR CommandPathIB::Parser::CheckSchemaValidity() const
{
case to_underlying(Tag::kEndpointId):
// check if this tag has appeared before
VerifyOrExit(!(TagPresenceMask & (1 << to_underlying(Tag::kEndpointId))), err = CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kEndpointId))), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << to_underlying(Tag::kEndpointId));
VerifyOrExit(chip::TLV::kTLVType_UnsignedInteger == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);

#if CHIP_DETAIL_LOGGING
{
uint16_t endpointId;
reader.Get(endpointId);
ReturnErrorOnFailure(reader.Get(endpointId));
PRETTY_PRINT("\tEndpointId = 0x%x,", endpointId);
}
#endif // CHIP_DETAIL_LOGGING
break;
case to_underlying(Tag::kClusterId):
// check if this tag has appeared before
VerifyOrExit(!(TagPresenceMask & (1 << to_underlying(Tag::kClusterId))), err = CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kClusterId))), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << to_underlying(Tag::kClusterId));
VerifyOrExit(chip::TLV::kTLVType_UnsignedInteger == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
#if CHIP_DETAIL_LOGGING
{
chip::ClusterId clusterId;
reader.Get(clusterId);
ReturnErrorOnFailure(reader.Get(clusterId));
PRETTY_PRINT("\tClusterId = 0x%" PRIx32 ",", clusterId);
}
#endif // CHIP_DETAIL_LOGGING
break;
case to_underlying(Tag::kCommandId):
// check if this tag has appeared before
VerifyOrExit(!(TagPresenceMask & (1 << to_underlying(Tag::kCommandId))), err = CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kCommandId))), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << to_underlying(Tag::kCommandId));
VerifyOrExit(chip::TLV::kTLVType_UnsignedInteger == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
VerifyOrReturnError(chip::TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
#if CHIP_DETAIL_LOGGING
{
chip::CommandId commandId;
reader.Get(commandId);
ReturnErrorOnFailure(reader.Get(commandId));
PRETTY_PRINT("\tCommandId = 0x%x,", commandId);
}
#endif // CHIP_DETAIL_LOGGING
Expand All @@ -103,23 +103,19 @@ CHIP_ERROR CommandPathIB::Parser::CheckSchemaValidity() const
if (CHIP_END_OF_TLV == err)
{
// check for required fields:
const uint16_t RequiredFields = (1 << to_underlying(Tag::kCommandId)) | (1 << to_underlying(Tag::kClusterId));
const uint16_t RequiredFields = (1 << to_underlying(Tag::kEndpointId)) | (1 << to_underlying(Tag::kCommandId)) | (1 << to_underlying(Tag::kClusterId));

if ((TagPresenceMask & RequiredFields) == RequiredFields)
{
err = CHIP_NO_ERROR;
}
else
{
err = CHIP_ERROR_IM_MALFORMED_COMMAND_PATH;
err = CHIP_ERROR_IM_MALFORMED_COMMAND_PATH_IB;
}
}
SuccessOrExit(err);
err = reader.ExitContainer(mOuterContainerType);

exit:

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

Expand Down
9 changes: 3 additions & 6 deletions src/app/MessageDef/CommandStatusIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,22 @@ CHIP_ERROR CommandStatusIB::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 CommandStatusIB::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 CommandStatusIB::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);
}

CommandPathIB::Builder & CommandStatusIB::Builder::CreatePath()
Expand Down
3 changes: 3 additions & 0 deletions src/app/MessageDef/DataVersionFilterIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ CHIP_ERROR DataVersionFilterIB::Parser::CheckSchemaValidity() const
}
#endif // CHIP_DETAIL_LOGGING
break;
default:
PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
break;
}
}
PRETTY_PRINT("},");
Expand Down
Loading

0 comments on commit 3303e8a

Please sign in to comment.