blob: 5a736ad9d87cb1cd2d16064db4ecdef883b78596 [file] [log] [blame]
yunhanw-google0c183552021-01-22 09:12:14 -08001/**
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-googleb717b362021-11-08 13:34:19 -080020 * This file defines AttributeDataIBs parser and builder in CHIP interaction model
yunhanw-google0c183552021-01-22 09:12:14 -080021 *
22 */
23
yunhanw-googleb717b362021-11-08 13:34:19 -080024#include "AttributeDataIBs.h"
yunhanw-google0c183552021-01-22 09:12:14 -080025
26#include "MessageDefHelper.h"
27
28#include <inttypes.h>
29#include <stdarg.h>
30#include <stdio.h>
31
Daniel Nicoara641e3ad2022-07-25 15:32:03 -040032#include <app/AppConfig.h>
Michael Spange00eac32021-06-21 17:25:47 -040033
yunhanw-google0c183552021-01-22 09:12:14 -080034using namespace chip;
35using namespace chip::TLV;
36
37namespace chip {
38namespace app {
yunhanw-google751d0dd2022-11-16 19:09:58 -080039#if CHIP_CONFIG_IM_PRETTY_PRINT
40CHIP_ERROR AttributeDataIBs::Parser::PrettyPrint() const
yunhanw-google0c183552021-01-22 09:12:14 -080041{
42 CHIP_ERROR err = CHIP_NO_ERROR;
yunhanw-google731ae5a2022-06-01 23:02:05 -070043 size_t numDataElement = 0;
yunhanw-google0c183552021-01-22 09:12:14 -080044 chip::TLV::TLVReader reader;
45
yunhanw-googleb717b362021-11-08 13:34:19 -080046 PRETTY_PRINT("AttributeDataIBs =");
yunhanw-google0c183552021-01-22 09:12:14 -080047 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-google731ae5a2022-06-01 23:02:05 -070054 VerifyOrReturnError(TLV::AnonymousTag() == reader.GetTag(), CHIP_ERROR_INVALID_TLV_TAG);
55 VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
yunhanw-google0c183552021-01-22 09:12:14 -080056
57 {
yunhanw-googleb717b362021-11-08 13:34:19 -080058 AttributeDataIB::Parser data;
yunhanw-google731ae5a2022-06-01 23:02:05 -070059 ReturnErrorOnFailure(data.Init(reader));
yunhanw-google0c183552021-01-22 09:12:14 -080060
61 PRETTY_PRINT_INCDEPTH();
yunhanw-google751d0dd2022-11-16 19:09:58 -080062 ReturnErrorOnFailure(data.PrettyPrint());
yunhanw-google0c183552021-01-22 09:12:14 -080063 PRETTY_PRINT_DECDEPTH();
64 }
65
yunhanw-google731ae5a2022-06-01 23:02:05 -070066 ++numDataElement;
yunhanw-google0c183552021-01-22 09:12:14 -080067 }
68
69 PRETTY_PRINT("],");
Boris Zbarsky6c4b54e2022-06-02 13:28:19 -040070 PRETTY_PRINT_BLANK_LINE();
yunhanw-google0c183552021-01-22 09:12:14 -080071
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-google731ae5a2022-06-01 23:02:05 -070076 if (numDataElement > 0)
yunhanw-google0c183552021-01-22 09:12:14 -080077 {
78 err = CHIP_NO_ERROR;
79 }
80 }
yunhanw-google731ae5a2022-06-01 23:02:05 -070081 ReturnErrorOnFailure(err);
82 return reader.ExitContainer(mOuterContainerType);
yunhanw-google0c183552021-01-22 09:12:14 -080083}
yunhanw-google751d0dd2022-11-16 19:09:58 -080084#endif // CHIP_CONFIG_IM_PRETTY_PRINT
yunhanw-google0c183552021-01-22 09:12:14 -080085
yunhanw-googleb717b362021-11-08 13:34:19 -080086AttributeDataIB::Builder & AttributeDataIBs::Builder::CreateAttributeDataIBBuilder()
yunhanw-google0c183552021-01-22 09:12:14 -080087{
88 // skip if error has already been set
yunhanw-googleb717b362021-11-08 13:34:19 -080089 VerifyOrExit(CHIP_NO_ERROR == mError, mAttributeDataIBBuilder.ResetError(mError));
yunhanw-google0c183552021-01-22 09:12:14 -080090
yunhanw-googleb717b362021-11-08 13:34:19 -080091 mError = mAttributeDataIBBuilder.Init(mpWriter);
yunhanw-google0c183552021-01-22 09:12:14 -080092
93exit:
94
yunhanw-googleb717b362021-11-08 13:34:19 -080095 // on error, mAttributeDataIBBuilder would be un-/partial initialized and cannot be used to write anything
96 return mAttributeDataIBBuilder;
yunhanw-google0c183552021-01-22 09:12:14 -080097}
98
yunhanw-googleb717b362021-11-08 13:34:19 -080099AttributeDataIB::Builder & AttributeDataIBs::Builder::GetAttributeDataIBBuilder()
yunhanw-google6c044e52021-06-18 13:01:52 -0700100{
yunhanw-googleb717b362021-11-08 13:34:19 -0800101 return mAttributeDataIBBuilder;
yunhanw-google6c044e52021-06-18 13:01:52 -0700102}
103
yunhanw-googleb717b362021-11-08 13:34:19 -0800104AttributeDataIBs::Builder & AttributeDataIBs::Builder::EndOfAttributeDataIBs()
yunhanw-google0c183552021-01-22 09:12:14 -0800105{
106 EndOfContainer();
107
108 return *this;
109}
110
111}; // namespace app
112}; // namespace chip