blob: d3f1f8b44c74a2705033e386f10523da8345b422 [file] [log] [blame]
/**
*
* 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 CommandDataElement parser and builder in CHIP interaction model
*
*/
#include "CommandDataElement.h"
#include "MessageDefHelper.h"
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <app/AppBuildConfig.h>
using namespace chip;
using namespace chip::TLV;
namespace chip {
namespace app {
CHIP_ERROR CommandDataElement::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;
}
CHIP_ERROR
CommandDataElement::Parser::ParseData(chip::TLV::TLVReader & aReader, int aDepth) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
if (aDepth == 0)
{
PRETTY_PRINT("\tCommandData = ");
}
else
{
if (chip::TLV::IsContextTag(aReader.GetTag()))
{
PRETTY_PRINT("\t0x%" PRIx32 " = ", chip::TLV::TagNumFromTag(aReader.GetTag()));
}
else if (chip::TLV::IsProfileTag(aReader.GetTag()))
{
PRETTY_PRINT("\t0x%" PRIx32 "::0x%" PRIx32 " = ", chip::TLV::ProfileIdFromTag(aReader.GetTag()),
chip::TLV::TagNumFromTag(aReader.GetTag()));
}
else
{
// Anonymous tag, don't print anything
}
}
switch (aReader.GetType())
{
case chip::TLV::kTLVType_Structure:
PRETTY_PRINT("\t{");
break;
case chip::TLV::kTLVType_Array:
PRETTY_PRINT_SAMELINE("[");
PRETTY_PRINT("\t\t");
break;
case chip::TLV::kTLVType_SignedInteger: {
int64_t value_s64;
err = aReader.Get(value_s64);
SuccessOrExit(err);
PRETTY_PRINT_SAMELINE("%" PRId64 ", ", value_s64);
break;
}
case chip::TLV::kTLVType_UnsignedInteger: {
uint64_t value_u64;
err = aReader.Get(value_u64);
SuccessOrExit(err);
PRETTY_PRINT_SAMELINE("%" PRIu64 ", ", value_u64);
break;
}
case chip::TLV::kTLVType_FloatingPointNumber: {
double value_fp;
err = aReader.Get(value_fp);
SuccessOrExit(err);
PRETTY_PRINT_SAMELINE("%lf, ", value_fp);
break;
}
case chip::TLV::kTLVType_Boolean: {
bool value_b;
err = aReader.Get(value_b);
SuccessOrExit(err);
PRETTY_PRINT_SAMELINE("%s, ", value_b ? "true" : "false");
break;
}
case chip::TLV::kTLVType_UTF8String: {
char value_s[256];
err = aReader.GetString(value_s, sizeof(value_s));
VerifyOrExit(err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL, );
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
{
PRETTY_PRINT_SAMELINE("... (byte string too long) ...");
err = CHIP_NO_ERROR;
}
else
{
PRETTY_PRINT_SAMELINE("\"%s\", ", value_s);
}
break;
}
case chip::TLV::kTLVType_ByteString: {
uint8_t value_b[256];
uint32_t len, readerLen;
readerLen = aReader.GetLength();
err = aReader.GetBytes(value_b, sizeof(value_b));
VerifyOrExit(err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL, );
PRETTY_PRINT_SAMELINE("[");
PRETTY_PRINT("\t\t");
if (readerLen < sizeof(value_b))
{
len = readerLen;
}
else
{
len = sizeof(value_b);
}
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
{
PRETTY_PRINT_SAMELINE("... (byte string too long) ...");
}
else
{
for (size_t i = 0; i < len; i++)
{
PRETTY_PRINT_SAMELINE("0x%" PRIx8 ", ", value_b[i]);
}
}
err = CHIP_NO_ERROR;
PRETTY_PRINT("]");
break;
}
case chip::TLV::kTLVType_Null:
PRETTY_PRINT_SAMELINE("NULL");
break;
default:
PRETTY_PRINT_SAMELINE("--");
break;
}
if (aReader.GetType() == chip::TLV::kTLVType_Structure || aReader.GetType() == chip::TLV::kTLVType_Array)
{
const char terminating_char = (aReader.GetType() == chip::TLV::kTLVType_Structure) ? '}' : ']';
chip::TLV::TLVType type;
IgnoreUnusedVariable(terminating_char);
err = aReader.EnterContainer(type);
SuccessOrExit(err);
while ((err = aReader.Next()) == CHIP_NO_ERROR)
{
PRETTY_PRINT_INCDEPTH();
err = ParseData(aReader, aDepth + 1);
SuccessOrExit(err);
PRETTY_PRINT_DECDEPTH();
}
PRETTY_PRINT("\t%c,", terminating_char);
err = aReader.ExitContainer(type);
SuccessOrExit(err);
}
exit:
ChipLogFunctError(err);
return err;
}
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR CommandDataElement::Parser::CheckSchemaValidity() const
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t TagPresenceMask = 0;
chip::TLV::TLVReader reader;
uint32_t tagNum = 0;
PRETTY_PRINT("CommandDataElement =");
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);
tagNum = chip::TLV::TagNumFromTag(reader.GetTag());
switch (tagNum)
{
case kCsTag_CommandPath:
// check if this tag has appeared before
VerifyOrExit(!(TagPresenceMask & (1 << kCsTag_CommandPath)), err = CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << kCsTag_CommandPath);
VerifyOrExit(chip::TLV::kTLVType_List == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
{
CommandPath::Parser path;
err = path.Init(reader);
SuccessOrExit(err);
PRETTY_PRINT_INCDEPTH();
err = path.CheckSchemaValidity();
SuccessOrExit(err);
PRETTY_PRINT_DECDEPTH();
}
break;
case kCsTag_Data:
// check if this tag has appeared before
VerifyOrExit(!(TagPresenceMask & (1 << kCsTag_Data)), err = CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << kCsTag_Data);
err = ParseData(reader, 0);
SuccessOrExit(err);
break;
case kCsTag_StatusElement:
// check if this tag has appeared before
VerifyOrExit(!(TagPresenceMask & (1 << kCsTag_StatusElement)), err = CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << kCsTag_StatusElement);
VerifyOrExit(chip::TLV::kTLVType_Array == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
{
StatusElement::Parser status;
err = status.Init(reader);
SuccessOrExit(err);
PRETTY_PRINT_INCDEPTH();
err = status.CheckSchemaValidity();
SuccessOrExit(err);
PRETTY_PRINT_DECDEPTH();
}
break;
default:
PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
break;
}
}
PRETTY_PRINT("},");
PRETTY_PRINT("");
// if we have exhausted this container
if (CHIP_END_OF_TLV == err)
{
// check for at most field:
const uint16_t CheckDataField = 1 << kCsTag_Data;
const uint16_t CheckStatusElementField = 1 << kCsTag_StatusElement;
if ((TagPresenceMask & CheckDataField) == CheckDataField &&
(TagPresenceMask & CheckStatusElementField) == CheckStatusElementField)
{
// kCsTag_Data and kCsTag_StatusElement both exist
err = CHIP_ERROR_IM_MALFORMED_COMMAND_DATA_ELEMENT;
}
else
{
err = CHIP_NO_ERROR;
}
}
SuccessOrExit(err);
err = reader.ExitContainer(mOuterContainerType);
exit:
ChipLogFunctError(err);
return err;
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR CommandDataElement::Parser::GetCommandPath(CommandPath::Parser * const apCommandPath) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVReader reader;
err = mReader.FindElementWithTag(chip::TLV::ContextTag(kCsTag_CommandPath), reader);
SuccessOrExit(err);
VerifyOrExit(chip::TLV::kTLVType_List == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
err = apCommandPath->Init(reader);
SuccessOrExit(err);
exit:
ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));
return err;
}
CHIP_ERROR CommandDataElement::Parser::GetData(chip::TLV::TLVReader * const apReader) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
err = mReader.FindElementWithTag(chip::TLV::ContextTag(kCsTag_Data), *apReader);
SuccessOrExit(err);
exit:
ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));
return err;
}
CHIP_ERROR CommandDataElement::Parser::GetStatusElement(StatusElement::Parser * const apStatusElement) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVReader reader;
err = mReader.FindElementWithTag(chip::TLV::ContextTag(kCsTag_StatusElement), reader);
SuccessOrExit(err);
VerifyOrExit(chip::TLV::kTLVType_Array == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
err = apStatusElement->Init(reader);
SuccessOrExit(err);
exit:
ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));
return err;
}
CHIP_ERROR CommandDataElement::Builder::Init(chip::TLV::TLVWriter * const apWriter)
{
return InitAnonymousStructure(apWriter);
}
CommandPath::Builder & CommandDataElement::Builder::CreateCommandPathBuilder()
{
// skip if error has already been set
VerifyOrExit(CHIP_NO_ERROR == mError, mCommandPathBuilder.ResetError(mError));
mError = mCommandPathBuilder.Init(mpWriter, kCsTag_CommandPath);
ChipLogFunctError(mError);
exit:
// on error, mAttributePathBuilder would be un-/partial initialized and cannot be used to write anything
return mCommandPathBuilder;
}
StatusElement::Builder & CommandDataElement::Builder::CreateStatusElementBuilder()
{
// skip if error has already been set
VerifyOrExit(CHIP_NO_ERROR == mError, mStatusElementBuilder.ResetError(mError));
mError = mStatusElementBuilder.Init(mpWriter, kCsTag_StatusElement);
ChipLogFunctError(mError);
exit:
// on error, mStatusElementBuilder would be un-/partial initialized and cannot be used to write anything
return mStatusElementBuilder;
}
CommandDataElement::Builder & CommandDataElement::Builder::EndOfCommandDataElement()
{
EndOfContainer();
return *this;
}
}; // namespace app
}; // namespace chip