From bb87c8e9534324d56a411a19e3fa9007919749e5 Mon Sep 17 00:00:00 2001 From: Yunhan Wang Date: Fri, 29 Oct 2021 09:36:10 -0700 Subject: [PATCH] Add ListBuilder/Parser and StructBuilder/Parser --Fix ListBuilder naming, which should be ArrayBuilder --Add ListBuilder/Parser and StructBuilder/Praser, the enablement for them would be follow-up PR --- src/app/BUILD.gn | 6 +- src/app/MessageDef/ArrayBuilder.cpp | 48 ++++++++++++++ src/app/MessageDef/ArrayBuilder.h | 64 +++++++++++++++++++ src/app/MessageDef/ArrayParser.cpp | 38 +++++++++++ src/app/MessageDef/ArrayParser.h | 46 +++++++++++++ src/app/MessageDef/AttributeDataList.h | 8 +-- src/app/MessageDef/AttributeDataVersionList.h | 8 +-- src/app/MessageDef/AttributePathList.h | 8 +-- src/app/MessageDef/AttributeStatusList.h | 8 +-- src/app/MessageDef/CommandList.h | 8 +-- src/app/MessageDef/EventList.h | 8 +-- src/app/MessageDef/EventPathList.h | 8 +-- src/app/MessageDef/ListBuilder.cpp | 11 ++-- src/app/MessageDef/ListBuilder.h | 2 +- src/app/MessageDef/ListParser.cpp | 49 ++------------ src/app/MessageDef/ListParser.h | 30 +-------- src/app/MessageDef/Parser.cpp | 12 ++-- src/app/MessageDef/Parser.h | 7 ++ src/app/MessageDef/StatusIB.h | 4 +- src/app/MessageDef/StructBuilder.cpp | 48 ++++++++++++++ src/app/MessageDef/StructBuilder.h | 64 +++++++++++++++++++ src/app/MessageDef/StructParser.cpp | 35 ++++++++++ src/app/MessageDef/StructParser.h | 46 +++++++++++++ 23 files changed, 450 insertions(+), 116 deletions(-) create mode 100644 src/app/MessageDef/ArrayBuilder.cpp create mode 100644 src/app/MessageDef/ArrayBuilder.h create mode 100644 src/app/MessageDef/ArrayParser.cpp create mode 100644 src/app/MessageDef/ArrayParser.h create mode 100644 src/app/MessageDef/StructBuilder.cpp create mode 100644 src/app/MessageDef/StructBuilder.h create mode 100644 src/app/MessageDef/StructParser.cpp create mode 100644 src/app/MessageDef/StructParser.h diff --git a/src/app/BUILD.gn b/src/app/BUILD.gn index 27a18093ad83d4..397e4d3c0e2a18 100644 --- a/src/app/BUILD.gn +++ b/src/app/BUILD.gn @@ -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", @@ -74,9 +76,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", @@ -88,6 +88,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", diff --git a/src/app/MessageDef/ArrayBuilder.cpp b/src/app/MessageDef/ArrayBuilder.cpp new file mode 100644 index 00000000000000..5a03a1f526fdab --- /dev/null +++ b/src/app/MessageDef/ArrayBuilder.cpp @@ -0,0 +1,48 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines Array builder in CHIP interaction model + * + */ + +#include "ArrayBuilder.h" + +#include +#include +#include + +namespace chip { +namespace app { +ArrayBuilder::ArrayBuilder() {} + +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; + mError = mpWriter->StartContainer(TLV::AnonymousTag, chip::TLV::kTLVType_Array, mOuterContainerType); + + return mError; +} +}; // namespace app +}; // namespace chip diff --git a/src/app/MessageDef/ArrayBuilder.h b/src/app/MessageDef/ArrayBuilder.h new file mode 100644 index 00000000000000..4c9fb3b01b129c --- /dev/null +++ b/src/app/MessageDef/ArrayBuilder.h @@ -0,0 +1,64 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines Array builder in CHIP interaction model + * + */ + +#pragma once + +#include "Builder.h" +#include "Parser.h" +#include +#include +#include +#include +#include + +namespace chip { +namespace app { +class ArrayBuilder : public Builder +{ +protected: + ArrayBuilder(); + +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 +}; // namespace chip diff --git a/src/app/MessageDef/ArrayParser.cpp b/src/app/MessageDef/ArrayParser.cpp new file mode 100644 index 00000000000000..12fe4585925abb --- /dev/null +++ b/src/app/MessageDef/ArrayParser.cpp @@ -0,0 +1,38 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines Array parser in CHIP interaction model + * + */ + +#include "ArrayParser.h" + +namespace chip { +namespace app { +ArrayParser::ArrayParser() {} + +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 diff --git a/src/app/MessageDef/ArrayParser.h b/src/app/MessageDef/ArrayParser.h new file mode 100644 index 00000000000000..ac312114375e53 --- /dev/null +++ b/src/app/MessageDef/ArrayParser.h @@ -0,0 +1,46 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines Array parser in CHIP interaction model + * + */ + +#pragma once + +#include "Parser.h" + +namespace chip { +namespace app { +class ArrayParser : public Parser +{ +protected: + ArrayParser(); + +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 diff --git a/src/app/MessageDef/AttributeDataList.h b/src/app/MessageDef/AttributeDataList.h index 9f38ab3c46c397..20949a064fd45d 100644 --- a/src/app/MessageDef/AttributeDataList.h +++ b/src/app/MessageDef/AttributeDataList.h @@ -23,9 +23,9 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "AttributeDataElement.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -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 @@ -58,7 +58,7 @@ class Parser : public ListParser #endif }; -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** diff --git a/src/app/MessageDef/AttributeDataVersionList.h b/src/app/MessageDef/AttributeDataVersionList.h index 29b7095def62b7..cb079e18e470e6 100644 --- a/src/app/MessageDef/AttributeDataVersionList.h +++ b/src/app/MessageDef/AttributeDataVersionList.h @@ -23,9 +23,9 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "AttributeDataElement.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -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 @@ -83,7 +83,7 @@ class Parser : public ListParser CHIP_ERROR GetVersion(chip::DataVersion * const apVersion); }; -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** diff --git a/src/app/MessageDef/AttributePathList.h b/src/app/MessageDef/AttributePathList.h index f28d378465f269..11b475596a228b 100644 --- a/src/app/MessageDef/AttributePathList.h +++ b/src/app/MessageDef/AttributePathList.h @@ -23,9 +23,9 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "AttributePath.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -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 @@ -58,7 +58,7 @@ class Parser : public ListParser #endif }; -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** diff --git a/src/app/MessageDef/AttributeStatusList.h b/src/app/MessageDef/AttributeStatusList.h index 6ee48d9851a00a..78774a7fea11ee 100644 --- a/src/app/MessageDef/AttributeStatusList.h +++ b/src/app/MessageDef/AttributeStatusList.h @@ -23,9 +23,9 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "AttributeStatusIB.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -37,7 +37,7 @@ namespace chip { namespace app { namespace AttributeStatusList { -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** @@ -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 diff --git a/src/app/MessageDef/CommandList.h b/src/app/MessageDef/CommandList.h index 3422ffe43eccde..ebf193a1da85d6 100644 --- a/src/app/MessageDef/CommandList.h +++ b/src/app/MessageDef/CommandList.h @@ -23,9 +23,9 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "CommandDataIB.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -37,7 +37,7 @@ namespace chip { namespace app { namespace CommandList { -class Parser : public ListParser +class Parser : public ArrayParser { public: #if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK @@ -58,7 +58,7 @@ class Parser : public ListParser #endif }; -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** diff --git a/src/app/MessageDef/EventList.h b/src/app/MessageDef/EventList.h index b90db97c122f32..c3550a24d5a276 100644 --- a/src/app/MessageDef/EventList.h +++ b/src/app/MessageDef/EventList.h @@ -23,9 +23,9 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "EventDataElement.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -37,7 +37,7 @@ namespace chip { namespace app { namespace EventList { -class Parser : public ListParser +class Parser : public ArrayParser { public: #if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK @@ -58,7 +58,7 @@ class Parser : public ListParser #endif }; -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** diff --git a/src/app/MessageDef/EventPathList.h b/src/app/MessageDef/EventPathList.h index 1ef4987f61f182..f236a04ceb3c8f 100644 --- a/src/app/MessageDef/EventPathList.h +++ b/src/app/MessageDef/EventPathList.h @@ -23,10 +23,10 @@ #pragma once +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include "EventPath.h" #include "EventPathList.h" -#include "ListBuilder.h" -#include "ListParser.h" #include #include @@ -38,7 +38,7 @@ namespace chip { namespace app { namespace EventPathList { -class Parser : public ListParser +class Parser : public ArrayParser { public: #if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK @@ -59,7 +59,7 @@ class Parser : public ListParser #endif }; -class Builder : public ListBuilder +class Builder : public ArrayBuilder { public: /** diff --git a/src/app/MessageDef/ListBuilder.cpp b/src/app/MessageDef/ListBuilder.cpp index 5ba483951d879f..5e659f3204e740 100644 --- a/src/app/MessageDef/ListBuilder.cpp +++ b/src/app/MessageDef/ListBuilder.cpp @@ -27,25 +27,22 @@ #include #include -using namespace chip; -using namespace chip::TLV; - namespace chip { namespace app { ListBuilder::ListBuilder() {} -CHIP_ERROR ListBuilder::Init(chip::TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse) +CHIP_ERROR ListBuilder::Init(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse) { mpWriter = apWriter; - mError = mpWriter->StartContainer(chip::TLV::ContextTag(aContextTagToUse), chip::TLV::kTLVType_Array, mOuterContainerType); + mError = mpWriter->StartContainer(chip::TLV::ContextTag(aContextTagToUse), TLV::kTLVType_Array, mOuterContainerType); return mError; } -CHIP_ERROR ListBuilder::Init(chip::TLV::TLVWriter * const apWriter) +CHIP_ERROR ListBuilder::Init(TLV::TLVWriter * const apWriter) { mpWriter = apWriter; - mError = mpWriter->StartContainer(chip::TLV::AnonymousTag, chip::TLV::kTLVType_Array, mOuterContainerType); + mError = mpWriter->StartContainer(TLV::AnonymousTag, TLV::kTLVType_Array, mOuterContainerType); return mError; } diff --git a/src/app/MessageDef/ListBuilder.h b/src/app/MessageDef/ListBuilder.h index b0a27380d3dfab..e8bc98a6cac120 100644 --- a/src/app/MessageDef/ListBuilder.h +++ b/src/app/MessageDef/ListBuilder.h @@ -33,7 +33,7 @@ namespace chip { namespace app { -class ListBuilder : public chip::app::Builder +class ListBuilder : public Builder { protected: ListBuilder(); diff --git a/src/app/MessageDef/ListParser.cpp b/src/app/MessageDef/ListParser.cpp index cc3811e2aa3371..799dbca7a0e7f4 100644 --- a/src/app/MessageDef/ListParser.cpp +++ b/src/app/MessageDef/ListParser.cpp @@ -23,57 +23,16 @@ #include "ListParser.h" -#include -#include -#include - -using namespace chip; -using namespace chip::TLV; - namespace chip { namespace app { ListParser::ListParser() {} -CHIP_ERROR ListParser::Init(const chip::TLV::TLVReader & aReader) +CHIP_ERROR ListParser::Init(const TLV::TLVReader & aReader) { - CHIP_ERROR err = CHIP_NO_ERROR; - - // make a copy of the reader here mReader.Init(aReader); - - VerifyOrExit(chip::TLV::kTLVType_Array == mReader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE); - - err = mReader.EnterContainer(mOuterContainerType); - -exit: - - return err; -} - -CHIP_ERROR ListParser::InitIfPresent(const chip::TLV::TLVReader & aReader, const uint8_t aContextTagToFind) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - chip::TLV::TLVReader reader; - - err = mReader.FindElementWithTag(chip::TLV::ContextTag(aContextTagToFind), reader); - SuccessOrExit(err); - - err = Init(reader); - SuccessOrExit(err); - -exit: - ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err)); - - return err; -} - -CHIP_ERROR ListParser::Next() -{ - CHIP_ERROR err = mReader.Next(); - - ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err)); - - return err; + VerifyOrReturnError(TLV::kTLVType_List == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE); + ReturnLogErrorOnFailure(mReader.EnterContainer(mOuterContainerType)); + return CHIP_NO_ERROR; } }; // namespace app }; // namespace chip diff --git a/src/app/MessageDef/ListParser.h b/src/app/MessageDef/ListParser.h index dc33ba271b0a4c..b09819eec20830 100644 --- a/src/app/MessageDef/ListParser.h +++ b/src/app/MessageDef/ListParser.h @@ -23,17 +23,11 @@ #pragma once -#include "Builder.h" #include "Parser.h" -#include -#include -#include -#include -#include namespace chip { namespace app { -class ListParser : public chip::app::Parser +class ListParser : public Parser { protected: ListParser(); @@ -42,29 +36,11 @@ class ListParser : public chip::app::Parser /** * @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 + * @param [in] aReader A pointer to a TLVReader, which should be on the element of the list element * * @return #CHIP_NO_ERROR on success */ - CHIP_ERROR Init(const chip::TLV::TLVReader & aReader); - - /** - * @brief Initialize the parser object with TLVReader if context tag exists - * - * @param [in] aReader A pointer to a TLVReader, which should be on the element of the array element - * @param [in] aContextTagToFind A context tag it tries to find - * - * @return #CHIP_NO_ERROR on success - */ - CHIP_ERROR InitIfPresent(const chip::TLV::TLVReader & aReader, const uint8_t aContextTagToFind); - - /** - * @brief Iterate to next element - * - * @return #CHIP_NO_ERROR on success - */ - CHIP_ERROR Next(); + CHIP_ERROR Init(const TLV::TLVReader & aReader); }; - }; // namespace app }; // namespace chip diff --git a/src/app/MessageDef/Parser.cpp b/src/app/MessageDef/Parser.cpp index 98b30f6ed38660..1303a6ac440603 100644 --- a/src/app/MessageDef/Parser.cpp +++ b/src/app/MessageDef/Parser.cpp @@ -27,9 +27,6 @@ #include #include -using namespace chip; -using namespace chip::TLV; - namespace chip { namespace app { @@ -41,7 +38,7 @@ void Parser::Init(const chip::TLV::TLVReader & aReader, chip::TLV::TLVType aOute mOuterContainerType = aOuterContainerType; } -CHIP_ERROR Parser::GetReaderOnTag(const Tag aTagToFind, chip::TLV::TLVReader * const apReader) const +CHIP_ERROR Parser::GetReaderOnTag(const TLV::Tag aTagToFind, chip::TLV::TLVReader * const apReader) const { return mReader.FindElementWithTag(aTagToFind, *apReader); } @@ -50,5 +47,12 @@ void Parser::GetReader(chip::TLV::TLVReader * const apReader) { apReader->Init(mReader); } + +CHIP_ERROR Parser::Next() +{ + CHIP_ERROR err = mReader.Next(); + ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err)); + return err; +} }; // namespace app }; // namespace chip diff --git a/src/app/MessageDef/Parser.h b/src/app/MessageDef/Parser.h index 0b027a93002474..7030a73e0b36a0 100644 --- a/src/app/MessageDef/Parser.h +++ b/src/app/MessageDef/Parser.h @@ -62,6 +62,13 @@ class Parser */ void GetReader(chip::TLV::TLVReader * const apReader); + /** + * @brief Iterate to next element + * + * @return #CHIP_NO_ERROR on success + */ + CHIP_ERROR Next(); + protected: chip::TLV::TLVReader mReader; chip::TLV::TLVType mOuterContainerType; diff --git a/src/app/MessageDef/StatusIB.h b/src/app/MessageDef/StatusIB.h index 80b7bfff9256c2..5d5e352a8b32a3 100644 --- a/src/app/MessageDef/StatusIB.h +++ b/src/app/MessageDef/StatusIB.h @@ -23,8 +23,8 @@ #pragma once -#include "ListBuilder.h" -#include "ListParser.h" +#include "ArrayBuilder.h" +#include "ArrayParser.h" #include #include diff --git a/src/app/MessageDef/StructBuilder.cpp b/src/app/MessageDef/StructBuilder.cpp new file mode 100644 index 00000000000000..136402418b26b4 --- /dev/null +++ b/src/app/MessageDef/StructBuilder.cpp @@ -0,0 +1,48 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines List builder in CHIP interaction model + * + */ + +#include "StructBuilder.h" + +#include +#include +#include + +namespace chip { +namespace app { +StructBuilder::StructBuilder() {} + +CHIP_ERROR StructBuilder::Init(TLV::TLVWriter * const apWriter, const uint8_t aContextTagToUse) +{ + mpWriter = apWriter; + mError = mpWriter->StartContainer(chip::TLV::ContextTag(aContextTagToUse), chip::TLV::kTLVType_Array, mOuterContainerType); + + return mError; +} + +CHIP_ERROR StructBuilder::Init(TLV::TLVWriter * const apWriter) +{ + mpWriter = apWriter; + mError = mpWriter->StartContainer(chip::TLV::AnonymousTag, TLV::kTLVType_Array, mOuterContainerType); + + return mError; +} +}; // namespace app +}; // namespace chip diff --git a/src/app/MessageDef/StructBuilder.h b/src/app/MessageDef/StructBuilder.h new file mode 100644 index 00000000000000..c9c398be9678ee --- /dev/null +++ b/src/app/MessageDef/StructBuilder.h @@ -0,0 +1,64 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines List builder in CHIP interaction model + * + */ + +#pragma once + +#include "Builder.h" +#include "Parser.h" +#include +#include +#include +#include +#include + +namespace chip { +namespace app { +class StructBuilder : public Builder +{ +protected: + StructBuilder(); + +public: + /** + * Init the TLV array container with an particular context tag. + * Required to implement arrays of arrays, and to test StructBuilder. + * + * @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 StructBuilder. + * + * @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 +}; // namespace chip diff --git a/src/app/MessageDef/StructParser.cpp b/src/app/MessageDef/StructParser.cpp new file mode 100644 index 00000000000000..ead814ac38c893 --- /dev/null +++ b/src/app/MessageDef/StructParser.cpp @@ -0,0 +1,35 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines Struct parser in CHIP interaction model + * + */ + +#include "StructParser.h" + +namespace chip { +namespace app { +StructParser::StructParser() {} +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)); + return CHIP_NO_ERROR; +} +}; // namespace app +}; // namespace chip diff --git a/src/app/MessageDef/StructParser.h b/src/app/MessageDef/StructParser.h new file mode 100644 index 00000000000000..6bc8f76fe2433d --- /dev/null +++ b/src/app/MessageDef/StructParser.h @@ -0,0 +1,46 @@ +/** + * + * 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. + */ +/** + * @file + * This file defines Struct parser in CHIP interaction model + * + */ + +#pragma once + +#include "Parser.h" + +namespace chip { +namespace app { +class StructParser : public Parser +{ +protected: + StructParser(); + +public: + /** + * @brief Initialize the parser object with TLVReader + * + * @param [in] aReader A pointer to a TLVReader, which should be on the element of the struct element + * + * @return #CHIP_NO_ERROR on success + */ + CHIP_ERROR Init(const TLV::TLVReader & aReader); +}; +}; // namespace app +}; // namespace chip