Skip to content

Commit

Permalink
Cleanup IM messagedef (#11811)
Browse files Browse the repository at this point in the history
Replace buidler/parser base with  structBuilder/Parser for rest struct message.
Replace ReturnLogErroronFailure and VerifyLogErrorOnFailure without log since CHIP_ERROR has recorded the initial error location.
  • Loading branch information
yunhanw-google authored Nov 16, 2021
1 parent bbd19d9 commit 33a869d
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 288 deletions.
2 changes: 1 addition & 1 deletion src/app/MessageDef/ArrayParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CHIP_ERROR ArrayParser::Init(const TLV::TLVReader & aReader)
{
mReader.Init(aReader);
VerifyOrReturnError(TLV::kTLVType_Array == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnLogErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}
} // namespace app
Expand Down
18 changes: 2 additions & 16 deletions src/app/MessageDef/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,11 @@ void Builder::ResetError(CHIP_ERROR aErr)
void Builder::EndOfContainer()
{
// skip if error has already been set
SuccessOrExit(mError);

mError = mpWriter->EndContainer(mOuterContainerType);
SuccessOrExit(mError);

ReturnOnFailure(mError);
ReturnOnFailure(mError = mpWriter->EndContainer(mOuterContainerType));
// we've just closed properly
// mark it so we do not panic when the build object destructor is called
mOuterContainerType = chip::TLV::kTLVType_NotSpecified;

exit:;
}

CHIP_ERROR Builder::InitAnonymousStructure(chip::TLV::TLVWriter * const apWriter)
{
mpWriter = apWriter;
mOuterContainerType = chip::TLV::kTLVType_NotSpecified;
mError = mpWriter->StartContainer(chip::TLV::AnonymousTag, chip::TLV::kTLVType_Structure, mOuterContainerType);

return mError;
}
} // namespace app
} // namespace chip
4 changes: 2 additions & 2 deletions src/app/MessageDef/EventFilterIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ CHIP_ERROR EventFilterIB::Parser::CheckSchemaValidity() const
#if CHIP_DETAIL_LOGGING
{
NodeId node;
ReturnLogErrorOnFailure(reader.Get(node));
ReturnErrorOnFailure(reader.Get(node));
PRETTY_PRINT("\tNode = 0x%" PRIx64 ",", node);
}
#endif // CHIP_DETAIL_LOGGING
Expand All @@ -69,7 +69,7 @@ CHIP_ERROR EventFilterIB::Parser::CheckSchemaValidity() const
#if CHIP_DETAIL_LOGGING
{
uint64_t eventMin;
ReturnLogErrorOnFailure(reader.Get(eventMin));
ReturnErrorOnFailure(reader.Get(eventMin));
PRETTY_PRINT("\tEventMin = 0x%" PRIx64 ",", eventMin);
}
#endif // CHIP_DETAIL_LOGGING
Expand Down
8 changes: 0 additions & 8 deletions src/app/MessageDef/InvokeRequestMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@

namespace chip {
namespace app {
CHIP_ERROR InvokeRequestMessage::Parser::Init(const TLV::TLVReader & aReader)
{
mReader.Init(aReader);
VerifyOrReturnError(TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR InvokeRequestMessage::Parser::CheckSchemaValidity() const
{
Expand Down
9 changes: 0 additions & 9 deletions src/app/MessageDef/InvokeRequestMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ enum class Tag : uint8_t
class Parser : public StructParser
{
public:
/**
* @brief Initialize the parser object with TLVReader
*
* @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this request
*
* @return #CHIP_NO_ERROR on success
*/
CHIP_ERROR Init(const TLV::TLVReader & aReader);

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
/**
* @brief Roughly verify the message is correctly formed
Expand Down
2 changes: 1 addition & 1 deletion src/app/MessageDef/ListParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CHIP_ERROR ListParser::Init(const TLV::TLVReader & aReader)
{
mReader.Init(aReader);
VerifyOrReturnError(TLV::kTLVType_List == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnLogErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}
} // namespace app
Expand Down
21 changes: 0 additions & 21 deletions src/app/MessageDef/ReadRequestMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ using namespace chip::TLV;

namespace chip {
namespace app {
CHIP_ERROR ReadRequestMessage::Parser::Init(const chip::TLV::TLVReader & aReader)
{
CHIP_ERROR err = CHIP_NO_ERROR;

// make a copy of the reader here
mReader.Init(aReader);

VerifyOrExit(chip::TLV::kTLVType_Structure == mReader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);

err = mReader.EnterContainer(mOuterContainerType);

exit:

return err;
}

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR ReadRequestMessage::Parser::CheckSchemaValidity() const
{
Expand Down Expand Up @@ -224,11 +208,6 @@ CHIP_ERROR ReadRequestMessage::Parser::GetEventNumber(uint64_t * const apEventNu
return GetUnsignedInteger(kCsTag_EventNumber, apEventNumber);
}

CHIP_ERROR ReadRequestMessage::Builder::Init(chip::TLV::TLVWriter * const apWriter)
{
return InitAnonymousStructure(apWriter);
}

AttributePathIBs::Builder & ReadRequestMessage::Builder::CreateAttributePathListBuilder()
{
// skip if error has already been set
Expand Down
21 changes: 2 additions & 19 deletions src/app/MessageDef/ReadRequestMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,9 @@ enum
kCsTag_EventNumber = 3,
};

class Parser : public chip::app::Parser
class Parser : public StructParser
{
public:
/**
* @brief Initialize the parser object with TLVReader
*
* @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this request
*
* @return #CHIP_NO_ERROR on success
*/
CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
/**
* @brief Roughly verify the message is correctly formed
Expand Down Expand Up @@ -117,18 +109,9 @@ class Parser : public chip::app::Parser
CHIP_ERROR GetEventNumber(uint64_t * const apEventNumber) const;
};

class Builder : public chip::app::Builder
class Builder : public StructBuilder
{
public:
/**
* @brief Initialize a ReadRequestMessage::Builder for writing into a TLV stream
*
* @param [in] apWriter A pointer to TLVWriter
*
* @return #CHIP_NO_ERROR on success
*/
CHIP_ERROR Init(chip::TLV::TLVWriter * const apWriter);

/**
* @brief Initialize a AttributePathIBs::Builder for writing into the TLV stream
*
Expand Down
9 changes: 0 additions & 9 deletions src/app/MessageDef/StatusIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ using namespace chip::TLV;

namespace chip {
namespace app {
CHIP_ERROR StatusIB::Parser::Init(const TLV::TLVReader & aReader)
{
// make a copy of the reader here
mReader.Init(aReader);
VerifyOrReturnError(TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}

CHIP_ERROR StatusIB::Parser::DecodeStatusIB(StatusIB & aStatusIB) const
{
TLV::TLVReader reader;
Expand Down
9 changes: 0 additions & 9 deletions src/app/MessageDef/StatusIB.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ struct StatusIB
class Parser : public StructParser
{
public:
/**
* @brief Initialize the parser object with TLVReader
*
* @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this StatusIB
*
* @return #CHIP_NO_ERROR on success
*/
CHIP_ERROR Init(const TLV::TLVReader & aReader);

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
/**
* @brief Roughly verify the message is correctly formed
Expand Down
26 changes: 6 additions & 20 deletions src/app/MessageDef/StatusResponseMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@

namespace chip {
namespace app {
CHIP_ERROR StatusResponseMessage::Parser::Init(const TLV::TLVReader & aReader)
{
// make a copy of the reader here
mReader.Init(aReader);
VerifyOrReturnLogError(TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnLogErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR StatusResponseMessage::Parser::CheckSchemaValidity() const
{
Expand All @@ -42,23 +33,23 @@ CHIP_ERROR StatusResponseMessage::Parser::CheckSchemaValidity() const

while (CHIP_NO_ERROR == (err = reader.Next()))
{
VerifyOrReturnLogError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
switch (TLV::TagNumFromTag(reader.GetTag()))
{
case kCsTag_Status:
VerifyOrReturnLogError(!statusTagPresence, CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrReturnError(!statusTagPresence, CHIP_ERROR_INVALID_TLV_TAG);
statusTagPresence = true;
VerifyOrReturnLogError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
#if CHIP_DETAIL_LOGGING
{
uint16_t status;
ReturnLogErrorOnFailure(reader.Get(status));
ReturnErrorOnFailure(reader.Get(status));
PRETTY_PRINT("\tStatus = 0x%" PRIx16 ",", status);
}
#endif // CHIP_DETAIL_LOGGING
break;
default:
ReturnLogErrorOnFailure(CHIP_ERROR_INVALID_TLV_TAG);
ReturnErrorOnFailure(CHIP_ERROR_INVALID_TLV_TAG);
}
}
PRETTY_PRINT("}");
Expand All @@ -68,7 +59,7 @@ CHIP_ERROR StatusResponseMessage::Parser::CheckSchemaValidity() const
{
err = CHIP_NO_ERROR;
}
ReturnLogErrorOnFailure(err);
ReturnErrorOnFailure(err);
return reader.ExitContainer(mOuterContainerType);
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
Expand All @@ -81,11 +72,6 @@ CHIP_ERROR StatusResponseMessage::Parser::GetStatus(Protocols::InteractionModel:
return err;
}

CHIP_ERROR StatusResponseMessage::Builder::Init(TLV::TLVWriter * const apWriter)
{
return InitAnonymousStructure(apWriter);
}

StatusResponseMessage::Builder & StatusResponseMessage::Builder::Status(const Protocols::InteractionModel::Status aStatus)
{
// skip if error has already been set
Expand Down
13 changes: 4 additions & 9 deletions src/app/MessageDef/StatusResponseMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/

#pragma once
#include "Builder.h"
#include "Parser.h"
#include "StructBuilder.h"
#include "StructParser.h"
#include <app/AppBuildConfig.h>
#include <app/util/basic-types.h>
#include <lib/core/CHIPCore.h>
Expand All @@ -34,13 +34,9 @@ enum
kCsTag_Status = 0,
};

class Parser : public app::Parser
class Parser : public StructParser
{
public:
/**
* @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this response
*/
CHIP_ERROR Init(const TLV::TLVReader & aReader);
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
/**
* @brief Roughly verify the message is correctly formed
Expand All @@ -67,10 +63,9 @@ class Parser : public app::Parser
CHIP_ERROR GetStatus(Protocols::InteractionModel::Status & aStatus) const;
};

class Builder : public app::Builder
class Builder : public StructBuilder
{
public:
CHIP_ERROR Init(TLV::TLVWriter * const apWriter);
StatusResponseMessage::Builder & Status(const Protocols::InteractionModel::Status aStatus);
};
} // namespace StatusResponseMessage
Expand Down
2 changes: 1 addition & 1 deletion src/app/MessageDef/StructParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CHIP_ERROR StructParser::Init(const TLV::TLVReader & aReader)
{
mReader.Init(aReader);
VerifyOrReturnError(TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnLogErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}
} // namespace app
Expand Down
Loading

0 comments on commit 33a869d

Please sign in to comment.