blob: 941f51ffc113d5d845ed298c8fb913a55496579c [file] [log] [blame]
/**
*
* 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 WriteResponse parser and builder in CHIP interaction model
*
*/
#include "WriteResponse.h"
#include "MessageDefHelper.h"
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
using namespace chip;
using namespace chip::TLV;
namespace chip {
namespace app {
CHIP_ERROR WriteResponse::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:
ChipLogFunctError(err);
return err;
}
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR WriteResponse::Parser::CheckSchemaValidity() const
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t TagPresenceMask = 0;
chip::TLV::TLVReader reader;
AttributeStatusList::Parser attributeStatusList;
PRETTY_PRINT("WriteResponse =");
PRETTY_PRINT("{");
// make a copy of the reader
reader.Init(mReader);
while (CHIP_NO_ERROR == (err = reader.Next()))
{
VerifyOrExit(chip::TLV::IsContextTag(reader.GetTag()), err = CHIP_ERROR_INVALID_TLV_TAG);
switch (chip::TLV::TagNumFromTag(reader.GetTag()))
{
case kCsTag_AttributeStatusList:
VerifyOrExit(!(TagPresenceMask & (1 << kCsTag_AttributeStatusList)), err = CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << kCsTag_AttributeStatusList);
VerifyOrExit(chip::TLV::kTLVType_Array == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
attributeStatusList.Init(reader);
PRETTY_PRINT_INCDEPTH();
err = attributeStatusList.CheckSchemaValidity();
SuccessOrExit(err);
PRETTY_PRINT_DECDEPTH();
break;
default:
ExitNow(err = CHIP_ERROR_INVALID_TLV_TAG);
}
}
PRETTY_PRINT("}");
PRETTY_PRINT("");
// if we have exhausted this container
if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
err = reader.ExitContainer(mOuterContainerType);
exit:
ChipLogFunctError(err);
return err;
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR WriteResponse::Parser::GetAttributeStatusList(AttributeStatusList::Parser * const apAttributeStatusList) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVReader reader;
err = mReader.FindElementWithTag(chip::TLV::ContextTag(kCsTag_AttributeStatusList), reader);
SuccessOrExit(err);
VerifyOrExit(chip::TLV::kTLVType_Array == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
err = apAttributeStatusList->Init(reader);
SuccessOrExit(err);
exit:
ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));
return err;
}
CHIP_ERROR WriteResponse::Builder::Init(chip::TLV::TLVWriter * const apWriter)
{
return InitAnonymousStructure(apWriter);
}
AttributeStatusList::Builder & WriteResponse::Builder::CreateAttributeStatusListBuilder()
{
// skip if error has already been set
if (mError == CHIP_NO_ERROR)
{
mError = mAttributeStatusListBuilder.Init(mpWriter, kCsTag_AttributeStatusList);
ChipLogFunctError(mError);
}
else
{
mAttributeStatusListBuilder.ResetError(mError);
}
return mAttributeStatusListBuilder;
}
AttributeStatusList::Builder & WriteResponse::Builder::GetAttributeStatusListBuilder()
{
return mAttributeStatusListBuilder;
}
WriteResponse::Builder & WriteResponse::Builder::EndOfWriteResponse()
{
EndOfContainer();
return *this;
}
}; // namespace app
}; // namespace chip