3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Google LLC.
5 * Copyright (c) 2016-2017 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * This file defines AttributeStatusElement parser and builder in CHIP interaction model
24 #include "AttributeStatusElement.h"
26 #include "MessageDefHelper.h"
33 using namespace chip::TLV;
37 CHIP_ERROR AttributeStatusElement::Builder::Init(chip::TLV::TLVWriter * const apWriter)
39 return InitAnonymousStructure(apWriter);
42 AttributePath::Builder & AttributeStatusElement::Builder::CreateAttributePathBuilder()
44 // skip if error has already been set
45 VerifyOrExit(CHIP_NO_ERROR == mError, mAttributePathBuilder.ResetError(mError));
47 mError = mAttributePathBuilder.Init(mpWriter, kCsTag_AttributePath);
50 ChipLogFunctError(mError);
51 return mAttributePathBuilder;
54 StatusElement::Builder & AttributeStatusElement::Builder::CreateStatusElementBuilder()
56 // skip if error has already been set
57 VerifyOrExit(CHIP_NO_ERROR == mError, mStatusElementBuilder.ResetError(mError));
59 mError = mStatusElementBuilder.Init(mpWriter, kCsTag_StatusElement);
62 ChipLogFunctError(mError);
63 return mStatusElementBuilder;
66 AttributeStatusElement::Builder & AttributeStatusElement::Builder::EndOfAttributeStatusElement()
72 CHIP_ERROR AttributeStatusElement::Parser::Init(const chip::TLV::TLVReader & aReader)
74 CHIP_ERROR err = CHIP_NO_ERROR;
76 // make a copy of the reader here
77 mReader.Init(aReader);
78 VerifyOrExit(chip::TLV::kTLVType_Structure == mReader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
80 // This is just a dummy, as we're not going to exit this container ever
81 chip::TLV::TLVType OuterContainerType;
82 err = mReader.EnterContainer(OuterContainerType);
85 ChipLogFunctError(err);
89 #if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
90 CHIP_ERROR AttributeStatusElement::Parser::CheckSchemaValidity() const
92 CHIP_ERROR err = CHIP_NO_ERROR;
93 uint16_t TagPresenceMask = 0;
94 chip::TLV::TLVReader reader;
96 PRETTY_PRINT("AttributeStatusElement =");
99 // make a copy of the reader
100 reader.Init(mReader);
102 while (CHIP_NO_ERROR == (err = reader.Next()))
104 VerifyOrExit(chip::TLV::IsContextTag(reader.GetTag()), err = CHIP_ERROR_INVALID_TLV_TAG);
105 switch (chip::TLV::TagNumFromTag(reader.GetTag()))
107 case kCsTag_AttributePath:
108 VerifyOrExit(!(TagPresenceMask & (1 << kCsTag_AttributePath)), err = CHIP_ERROR_INVALID_TLV_TAG);
109 TagPresenceMask |= (1 << kCsTag_AttributePath);
111 AttributePath::Parser path;
112 err = path.Init(reader);
115 PRETTY_PRINT_INCDEPTH();
116 err = path.CheckSchemaValidity();
118 PRETTY_PRINT_DECDEPTH();
121 case kCsTag_StatusElement:
122 VerifyOrExit(!(TagPresenceMask & (1 << kCsTag_StatusElement)), err = CHIP_ERROR_INVALID_TLV_TAG);
123 TagPresenceMask |= (1 << kCsTag_StatusElement);
125 StatusElement::Parser status;
126 err = status.Init(reader);
129 PRETTY_PRINT_INCDEPTH();
130 err = status.CheckSchemaValidity();
132 PRETTY_PRINT_DECDEPTH();
136 ExitNow(err = CHIP_ERROR_INVALID_TLV_TAG);
143 // if we have exhausted this container
144 if (CHIP_END_OF_TLV == err)
146 // check for required fields:
147 const uint16_t RequiredFields = (1 << kCsTag_AttributePath) | (1 << kCsTag_StatusElement);
149 if ((TagPresenceMask & RequiredFields) == RequiredFields)
155 err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_STATUS_ELEMENT;
160 ChipLogFunctError(err);
164 #endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
166 CHIP_ERROR AttributeStatusElement::Parser::GetAttributePath(AttributePath::Parser * const apAttributePath) const
168 CHIP_ERROR err = CHIP_NO_ERROR;
169 chip::TLV::TLVReader reader;
171 err = mReader.FindElementWithTag(chip::TLV::ContextTag(kCsTag_AttributePath), reader);
174 VerifyOrExit(chip::TLV::kTLVType_List == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
176 err = apAttributePath->Init(reader);
180 ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));
185 CHIP_ERROR AttributeStatusElement::Parser::GetStatusElement(StatusElement::Parser * const apStatusElement) const
187 CHIP_ERROR err = CHIP_NO_ERROR;
188 chip::TLV::TLVReader reader;
190 err = mReader.FindElementWithTag(chip::TLV::ContextTag(kCsTag_StatusElement), reader);
193 VerifyOrExit(chip::TLV::kTLVType_Array == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
195 err = apStatusElement->Init(reader);
199 ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));