yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 1 | /** |
| 2 | * |
| 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 |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 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. |
| 17 | */ |
| 18 | /** |
| 19 | * @file |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 20 | * This file defines AttributeDataIBs parser and builder in CHIP interaction model |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 24 | #include "AttributeDataIBs.h" |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 25 | |
| 26 | #include "MessageDefHelper.h" |
| 27 | |
| 28 | #include <inttypes.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | |
Daniel Nicoara | 641e3ad | 2022-07-25 15:32:03 -0400 | [diff] [blame] | 32 | #include <app/AppConfig.h> |
Michael Spang | e00eac3 | 2021-06-21 17:25:47 -0400 | [diff] [blame] | 33 | |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 34 | using namespace chip; |
| 35 | using namespace chip::TLV; |
| 36 | |
| 37 | namespace chip { |
| 38 | namespace app { |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 39 | #if CHIP_CONFIG_IM_PRETTY_PRINT |
| 40 | CHIP_ERROR AttributeDataIBs::Parser::PrettyPrint() const |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 41 | { |
| 42 | CHIP_ERROR err = CHIP_NO_ERROR; |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 43 | size_t numDataElement = 0; |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 44 | chip::TLV::TLVReader reader; |
| 45 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 46 | PRETTY_PRINT("AttributeDataIBs ="); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 47 | PRETTY_PRINT("["); |
| 48 | |
| 49 | // make a copy of the reader |
| 50 | reader.Init(mReader); |
| 51 | |
| 52 | while (CHIP_NO_ERROR == (err = reader.Next())) |
| 53 | { |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 54 | VerifyOrReturnError(TLV::AnonymousTag() == reader.GetTag(), CHIP_ERROR_INVALID_TLV_TAG); |
| 55 | VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 56 | |
| 57 | { |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 58 | AttributeDataIB::Parser data; |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 59 | ReturnErrorOnFailure(data.Init(reader)); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 60 | |
| 61 | PRETTY_PRINT_INCDEPTH(); |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 62 | ReturnErrorOnFailure(data.PrettyPrint()); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 63 | PRETTY_PRINT_DECDEPTH(); |
| 64 | } |
| 65 | |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 66 | ++numDataElement; |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | PRETTY_PRINT("],"); |
Boris Zbarsky | 6c4b54e | 2022-06-02 13:28:19 -0400 | [diff] [blame] | 70 | PRETTY_PRINT_BLANK_LINE(); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 71 | |
| 72 | // if we have exhausted this container |
| 73 | if (CHIP_END_OF_TLV == err) |
| 74 | { |
| 75 | // if we have at least one data element |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 76 | if (numDataElement > 0) |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 77 | { |
| 78 | err = CHIP_NO_ERROR; |
| 79 | } |
| 80 | } |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 81 | ReturnErrorOnFailure(err); |
| 82 | return reader.ExitContainer(mOuterContainerType); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 83 | } |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 84 | #endif // CHIP_CONFIG_IM_PRETTY_PRINT |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 85 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 86 | AttributeDataIB::Builder & AttributeDataIBs::Builder::CreateAttributeDataIBBuilder() |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 87 | { |
| 88 | // skip if error has already been set |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 89 | VerifyOrExit(CHIP_NO_ERROR == mError, mAttributeDataIBBuilder.ResetError(mError)); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 90 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 91 | mError = mAttributeDataIBBuilder.Init(mpWriter); |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 92 | |
| 93 | exit: |
| 94 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 95 | // on error, mAttributeDataIBBuilder would be un-/partial initialized and cannot be used to write anything |
| 96 | return mAttributeDataIBBuilder; |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 97 | } |
| 98 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 99 | AttributeDataIB::Builder & AttributeDataIBs::Builder::GetAttributeDataIBBuilder() |
yunhanw-google | 6c044e5 | 2021-06-18 13:01:52 -0700 | [diff] [blame] | 100 | { |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 101 | return mAttributeDataIBBuilder; |
yunhanw-google | 6c044e5 | 2021-06-18 13:01:52 -0700 | [diff] [blame] | 102 | } |
| 103 | |
yunhanw-google | b717b36 | 2021-11-08 13:34:19 -0800 | [diff] [blame] | 104 | AttributeDataIBs::Builder & AttributeDataIBs::Builder::EndOfAttributeDataIBs() |
yunhanw-google | 0c18355 | 2021-01-22 09:12:14 -0800 | [diff] [blame] | 105 | { |
| 106 | EndOfContainer(); |
| 107 | |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | }; // namespace app |
| 112 | }; // namespace chip |