Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ListBuilder/Parser and StructBuilder/Parser #11231

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static_library("app") {
"CommandSender.cpp",
"EventManagement.cpp",
"InteractionModelEngine.cpp",
"MessageDef/ArrayBuilder.cpp",
"MessageDef/ArrayParser.cpp",
"MessageDef/AttributeDataElement.cpp",
"MessageDef/AttributeDataElement.h",
"MessageDef/AttributeDataList.cpp",
Expand Down Expand Up @@ -76,9 +78,7 @@ static_library("app") {
"MessageDef/InvokeCommand.cpp",
"MessageDef/InvokeCommand.h",
"MessageDef/ListBuilder.cpp",
"MessageDef/ListBuilder.h",
"MessageDef/ListParser.cpp",
"MessageDef/ListParser.h",
"MessageDef/MessageDefHelper.cpp",
"MessageDef/MessageDefHelper.h",
"MessageDef/Parser.cpp",
Expand All @@ -90,6 +90,8 @@ static_library("app") {
"MessageDef/StatusIB.cpp",
"MessageDef/StatusIB.h",
"MessageDef/StatusResponse.cpp",
"MessageDef/StructBuilder.cpp",
"MessageDef/StructParser.cpp",
"MessageDef/SubscribeRequest.cpp",
"MessageDef/SubscribeResponse.cpp",
"MessageDef/TimedRequestMessage.cpp",
Expand Down
41 changes: 41 additions & 0 deletions src/app/MessageDef/ArrayBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
* Copyright (c) 2021 Project CHIP Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ArrayBuilder.h"

#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>

namespace chip {
namespace app {
CHIP_ERROR ArrayBuilder::Init(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse)
{
mpWriter = apWriter;
mError = mpWriter->StartContainer(TLV::ContextTag(aContextTagToUse), chip::TLV::kTLVType_Array, mOuterContainerType);

return mError;
}

CHIP_ERROR ArrayBuilder::Init(TLV::TLVWriter * const apWriter)
{
mpWriter = apWriter;
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
mError = mpWriter->StartContainer(TLV::AnonymousTag, chip::TLV::kTLVType_Array, mOuterContainerType);

return mError;
}
}; // namespace app
}; // namespace chip
56 changes: 56 additions & 0 deletions src/app/MessageDef/ArrayBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Builder.h"
#include "Parser.h"
#include <app/util/basic-types.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPTLV.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

namespace chip {
namespace app {
class ArrayBuilder : public Builder
{
public:
/**
* Init the TLV array container with an particular context tag.
* Required to implement arrays of arrays, and to test ArrayBuilder.
*
* @param[in] apWriter Pointer to the TLVWriter that is encoding the message.
* @param[in] aContextTagToUse A contextTag to use.
*
* @return CHIP_ERROR codes returned by chip::TLV objects.
*/

CHIP_ERROR Init(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse);
/**
* Init the TLV array container with an anonymous tag.
* Required to implement arrays of arrays, and to test ArrayBuilder.
*
* @param[in] apWriter Pointer to the TLVWriter that is encoding the message.
*
* @return CHIP_ERROR codes returned by chip::TLV objects.
*/
CHIP_ERROR Init(TLV::TLVWriter * const apWriter);
};

}; // namespace app
yunhanw-google marked this conversation as resolved.
Show resolved Hide resolved
}; // namespace chip
31 changes: 31 additions & 0 deletions src/app/MessageDef/ArrayParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2018 Google LLC.
* Copyright (c) 2016-2017 Nest Labs, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ArrayParser.h"

namespace chip {
namespace app {
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));
return CHIP_NO_ERROR;
}
}; // namespace app
}; // namespace chip
38 changes: 38 additions & 0 deletions src/app/MessageDef/ArrayParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2016-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Parser.h"

namespace chip {
namespace app {
class ArrayParser : public Parser
{
public:
/**
* @brief Initialize the parser object with TLVReader
*
* @param [in] aReader A pointer to a TLVReader, which should be on the element of the array element
*
* @return #CHIP_NO_ERROR on success
*/
CHIP_ERROR Init(const TLV::TLVReader & aReader);
};
}; // namespace app
}; // namespace chip
8 changes: 4 additions & 4 deletions src/app/MessageDef/AttributeDataList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

#pragma once

#include "ArrayBuilder.h"
#include "ArrayParser.h"
#include "AttributeDataElement.h"
#include "ListBuilder.h"
#include "ListParser.h"

#include <app/AppBuildConfig.h>
#include <app/util/basic-types.h>
Expand All @@ -37,7 +37,7 @@
namespace chip {
namespace app {
namespace AttributeDataList {
class Parser : public ListParser
class Parser : public ArrayParser
{
public:
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
Expand All @@ -58,7 +58,7 @@ class Parser : public ListParser
#endif
};

class Builder : public ListBuilder
class Builder : public ArrayBuilder
{
public:
/**
Expand Down
8 changes: 4 additions & 4 deletions src/app/MessageDef/AttributeDataVersionList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

#pragma once

#include "ArrayBuilder.h"
#include "ArrayParser.h"
#include "AttributeDataElement.h"
#include "ListBuilder.h"
#include "ListParser.h"

#include <app/AppBuildConfig.h>
#include <app/util/basic-types.h>
Expand All @@ -37,7 +37,7 @@
namespace chip {
namespace app {
namespace AttributeDataVersionList {
class Parser : public ListParser
class Parser : public ArrayParser
{
public:
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
Expand Down Expand Up @@ -83,7 +83,7 @@ class Parser : public ListParser
CHIP_ERROR GetVersion(chip::DataVersion * const apVersion);
};

class Builder : public ListBuilder
class Builder : public ArrayBuilder
{
public:
/**
Expand Down
8 changes: 4 additions & 4 deletions src/app/MessageDef/AttributePathList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

#pragma once

#include "ArrayBuilder.h"
#include "ArrayParser.h"
#include "AttributePath.h"
#include "ListBuilder.h"
#include "ListParser.h"

#include <app/AppBuildConfig.h>
#include <app/util/basic-types.h>
Expand All @@ -37,7 +37,7 @@
namespace chip {
namespace app {
namespace AttributePathList {
class Parser : public ListParser
class Parser : public ArrayParser
{
public:
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
Expand All @@ -58,7 +58,7 @@ class Parser : public ListParser
#endif
};

class Builder : public ListBuilder
class Builder : public ArrayBuilder
{
public:
/**
Expand Down
8 changes: 4 additions & 4 deletions src/app/MessageDef/AttributeStatusList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

#pragma once

#include "ArrayBuilder.h"
#include "ArrayParser.h"
#include "AttributeStatusIB.h"
#include "ListBuilder.h"
#include "ListParser.h"

#include <app/AppBuildConfig.h>
#include <app/util/basic-types.h>
Expand All @@ -37,7 +37,7 @@
namespace chip {
namespace app {
namespace AttributeStatusList {
class Builder : public ListBuilder
class Builder : public ArrayBuilder
{
public:
/**
Expand All @@ -58,7 +58,7 @@ class Builder : public ListBuilder
AttributeStatusIB::Builder mAttributeStatusBuilder;
};

class Parser : public ListParser
class Parser : public ArrayParser
{
public:
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
Expand Down
4 changes: 2 additions & 2 deletions src/app/MessageDef/Builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ class Builder
*/
void Rollback(const chip::TLV::TLVWriter & aPoint) { *mpWriter = aPoint; }

void EndOfContainer();

protected:
CHIP_ERROR mError;
chip::TLV::TLVWriter * mpWriter;
chip::TLV::TLVType mOuterContainerType;

Builder();
void EndOfContainer();

CHIP_ERROR InitAnonymousStructure(chip::TLV::TLVWriter * const apWriter);
};
}; // namespace app
Expand Down
20 changes: 0 additions & 20 deletions src/app/MessageDef/CommandDataIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ using namespace chip::TLV;

namespace chip {
namespace app {
CHIP_ERROR CommandDataIB::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;
}

CHIP_ERROR
CommandDataIB::Parser::ParseData(chip::TLV::TLVReader & aReader, int aDepth) const
{
Expand Down Expand Up @@ -370,11 +355,6 @@ CHIP_ERROR CommandDataIB::Parser::GetStatusIB(StatusIB::Parser * const apStatusI
return err;
}

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

CommandPathIB::Builder & CommandDataIB::Builder::CreateCommandPathBuilder()
{
// skip if error has already been set
Expand Down
Loading