| /** |
| * |
| * Copyright (c) 2020-2021 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 "AttributeDataIB.h" |
| |
| #include "MessageDefHelper.h" |
| |
| #include <inttypes.h> |
| #include <stdarg.h> |
| #include <stdio.h> |
| |
| #include <app/AppBuildConfig.h> |
| |
| namespace chip { |
| namespace app { |
| CHIP_ERROR |
| AttributeDataIB::Parser::ParseData(TLV::TLVReader & aReader, int aDepth) const |
| { |
| CHIP_ERROR err = CHIP_NO_ERROR; |
| |
| if (aDepth == 0) |
| { |
| PRETTY_PRINT("\tData = "); |
| } |
| else |
| { |
| if (TLV::IsContextTag(aReader.GetTag())) |
| { |
| PRETTY_PRINT("\t0x%" PRIx32 " = ", TLV::TagNumFromTag(aReader.GetTag())); |
| } |
| else if (TLV::IsProfileTag(aReader.GetTag())) |
| { |
| PRETTY_PRINT("\t0x%" PRIx32 "::0x%" PRIx32 " = ", TLV::ProfileIdFromTag(aReader.GetTag()), |
| TLV::TagNumFromTag(aReader.GetTag())); |
| } |
| else |
| { |
| // Anonymous tag, don't print anything |
| } |
| } |
| |
| switch (aReader.GetType()) |
| { |
| case TLV::kTLVType_Structure: |
| PRETTY_PRINT("\t{"); |
| break; |
| |
| case TLV::kTLVType_Array: |
| PRETTY_PRINT_SAMELINE("["); |
| PRETTY_PRINT("\t\t"); |
| break; |
| |
| case TLV::kTLVType_SignedInteger: { |
| int64_t value_s64; |
| |
| ReturnErrorOnFailure(aReader.Get(value_s64)); |
| |
| PRETTY_PRINT_SAMELINE("%" PRId64 ", ", value_s64); |
| break; |
| } |
| |
| case TLV::kTLVType_UnsignedInteger: { |
| uint64_t value_u64; |
| |
| ReturnErrorOnFailure(aReader.Get(value_u64)); |
| |
| PRETTY_PRINT_SAMELINE("%" PRIu64 ", ", value_u64); |
| break; |
| } |
| |
| case TLV::kTLVType_Boolean: { |
| bool value_b; |
| |
| ReturnErrorOnFailure(aReader.Get(value_b)); |
| |
| PRETTY_PRINT_SAMELINE("%s, ", value_b ? "true" : "false"); |
| break; |
| } |
| |
| case TLV::kTLVType_UTF8String: { |
| char value_s[256]; |
| |
| err = aReader.GetString(value_s, sizeof(value_s)); |
| VerifyOrReturnError(err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL, err); |
| |
| 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 TLV::kTLVType_ByteString: { |
| uint8_t value_b[256]; |
| uint32_t len, readerLen; |
| |
| readerLen = aReader.GetLength(); |
| |
| err = aReader.GetBytes(value_b, sizeof(value_b)); |
| VerifyOrReturnError(err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL, err); |
| |
| 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("\t]"); |
| break; |
| } |
| |
| case TLV::kTLVType_Null: |
| PRETTY_PRINT_SAMELINE("NULL"); |
| break; |
| |
| default: |
| PRETTY_PRINT_SAMELINE("--"); |
| break; |
| } |
| |
| if (aReader.GetType() == TLV::kTLVType_Structure || aReader.GetType() == TLV::kTLVType_Array) |
| { |
| const char terminating_char = (aReader.GetType() == TLV::kTLVType_Structure) ? '}' : ']'; |
| TLV::TLVType type; |
| |
| IgnoreUnusedVariable(terminating_char); |
| |
| ReturnErrorOnFailure(aReader.EnterContainer(type)); |
| |
| while ((err = aReader.Next()) == CHIP_NO_ERROR) |
| { |
| PRETTY_PRINT_INCDEPTH(); |
| |
| ReturnErrorOnFailure(ParseData(aReader, aDepth + 1)); |
| |
| PRETTY_PRINT_DECDEPTH(); |
| } |
| |
| PRETTY_PRINT("\t%c,", terminating_char); |
| |
| ReturnErrorOnFailure(aReader.ExitContainer(type)); |
| } |
| |
| return CHIP_NO_ERROR; |
| } |
| |
| #if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK |
| CHIP_ERROR AttributeDataIB::Parser::CheckSchemaValidity() const |
| { |
| CHIP_ERROR err = CHIP_NO_ERROR; |
| int TagPresenceMask = 0; |
| TLV::TLVReader reader; |
| |
| PRETTY_PRINT("AttributeDataIB ="); |
| PRETTY_PRINT("{"); |
| |
| // make a copy of the Path reader |
| reader.Init(mReader); |
| |
| while (CHIP_NO_ERROR == (err = reader.Next())) |
| { |
| VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG); |
| uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag()); |
| switch (tagNum) |
| { |
| case to_underlying(Tag::kDataVersion): |
| // check if this tag has appeared before |
| VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kDataVersion))), CHIP_ERROR_INVALID_TLV_TAG); |
| TagPresenceMask |= (1 << to_underlying(Tag::kDataVersion)); |
| VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE); |
| |
| #if CHIP_DETAIL_LOGGING |
| { |
| chip::DataVersion version; |
| ReturnErrorOnFailure(reader.Get(version)); |
| PRETTY_PRINT("\tDataVersion = 0x%" PRIx32 ",", version); |
| } |
| #endif // CHIP_DETAIL_LOGGING |
| break; |
| case to_underlying(Tag::kPath): |
| // check if this tag has appeared before |
| VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kPath))), CHIP_ERROR_INVALID_TLV_TAG); |
| TagPresenceMask |= (1 << to_underlying(Tag::kPath)); |
| { |
| AttributePathIB::Parser path; |
| ReturnErrorOnFailure(path.Init(reader)); |
| |
| PRETTY_PRINT_INCDEPTH(); |
| ReturnErrorOnFailure(path.CheckSchemaValidity()); |
| PRETTY_PRINT_DECDEPTH(); |
| } |
| break; |
| case to_underlying(Tag::kData): |
| // check if this tag has appeared before |
| VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kData))), CHIP_ERROR_INVALID_TLV_TAG); |
| TagPresenceMask |= (1 << to_underlying(Tag::kData)); |
| |
| PRETTY_PRINT_INCDEPTH(); |
| ReturnErrorOnFailure(ParseData(reader, 0)); |
| 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 required fields: |
| const int RequiredFields = (1 << to_underlying(Tag::kPath)) | (1 << to_underlying(Tag::kData)); |
| |
| if ((TagPresenceMask & RequiredFields) == RequiredFields) |
| { |
| err = CHIP_NO_ERROR; |
| } |
| else |
| { |
| err = CHIP_ERROR_IM_MALFORMED_EVENT_DATA_ELEMENT; |
| } |
| } |
| ReturnErrorOnFailure(err); |
| ReturnErrorOnFailure(reader.ExitContainer(mOuterContainerType)); |
| return CHIP_NO_ERROR; |
| } |
| #endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK |
| |
| CHIP_ERROR AttributeDataIB::Parser::GetPath(AttributePathIB::Parser * const apPath) const |
| { |
| TLV::TLVReader reader; |
| ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kPath)), reader)); |
| ReturnErrorOnFailure(apPath->Init(reader)); |
| return CHIP_NO_ERROR; |
| } |
| |
| CHIP_ERROR AttributeDataIB::Parser::GetDataVersion(chip::DataVersion * const apVersion) const |
| { |
| return GetUnsignedInteger(to_underlying(Tag::kDataVersion), apVersion); |
| } |
| |
| CHIP_ERROR AttributeDataIB::Parser::GetData(TLV::TLVReader * const apReader) const |
| { |
| return mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kData)), *apReader); |
| } |
| |
| AttributePathIB::Builder & AttributeDataIB::Builder::CreatePath() |
| { |
| if (mError == CHIP_NO_ERROR) |
| { |
| mError = mPath.Init(mpWriter, to_underlying(Tag::kPath)); |
| } |
| return mPath; |
| } |
| |
| AttributeDataIB::Builder & AttributeDataIB::Builder::DataVersion(const chip::DataVersion aDataVersion) |
| { |
| // skip if error has already been set |
| if (mError == CHIP_NO_ERROR) |
| { |
| mError = mpWriter->Put(TLV::ContextTag(to_underlying(Tag::kDataVersion)), aDataVersion); |
| } |
| return *this; |
| } |
| |
| AttributeDataIB::Builder & AttributeDataIB::Builder::EndOfAttributeDataIB() |
| { |
| EndOfContainer(); |
| return *this; |
| } |
| } // namespace app |
| } // namespace chip |