Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2016-2017 Nest Labs, Inc.
5 : *
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
20 : * This file defines builder in CHIP interaction model
21 : *
22 : */
23 :
24 : #pragma once
25 :
26 : #include <app/util/basic-types.h>
27 : #include <lib/core/CHIPCore.h>
28 : #include <lib/core/TLV.h>
29 : #include <lib/support/CodeUtils.h>
30 : #include <lib/support/logging/CHIPLogging.h>
31 :
32 : namespace chip {
33 : namespace app {
34 : class Builder
35 : {
36 : public:
37 : /**
38 : * @brief Initialize the Builder object with TLVWriter and ContainerType
39 : *
40 : * @param [in] apWriter A pointer to a TLVWriter
41 : * @param [in] aOuterContainerType outer container type
42 : *
43 : */
44 : void Init(chip::TLV::TLVWriter * const apWriter, chip::TLV::TLVType aOuterContainerType);
45 :
46 : /**
47 : * @brief Reset the Error
48 : *
49 : */
50 : void ResetError();
51 :
52 : /**
53 : * @brief Reset the Error with particular aErr.
54 : * @param [in] aErr the Error it would be reset with
55 : *
56 : */
57 : void ResetError(CHIP_ERROR aErr);
58 :
59 : /**
60 : * @brief Get current error
61 : *
62 : * @return #CHIP_NO_ERROR on success
63 : */
64 75273 : CHIP_ERROR GetError() const { return mError; };
65 :
66 : /**
67 : * @brief Get TLV Writer
68 : *
69 : * @return #Pointer to the TLVWriter
70 : */
71 29383 : chip::TLV::TLVWriter * GetWriter() { return mpWriter; };
72 :
73 : /**
74 : * Checkpoint the current tlv state into a TLVWriter
75 : *
76 : * @param[out] aPoint A writer to checkpoint the state of the TLV writer into.
77 : * This writer must not outlive the builder
78 : */
79 23560 : void Checkpoint(chip::TLV::TLVWriter & aPoint) { aPoint = *mpWriter; };
80 :
81 : /**
82 : * Rollback the request state to the checkpointed TLVWriter
83 : *
84 : * @param[in] aPoint A writer that captured the state via Checkpoint() at some point in the past
85 : */
86 4135 : void Rollback(const chip::TLV::TLVWriter & aPoint)
87 : {
88 4135 : *mpWriter = aPoint;
89 4135 : ResetError();
90 4135 : }
91 :
92 : void EndOfContainer();
93 :
94 : Builder(Builder &) = delete;
95 :
96 : protected:
97 : CHIP_ERROR mError;
98 : chip::TLV::TLVWriter * mpWriter;
99 : chip::TLV::TLVType mOuterContainerType;
100 :
101 : Builder();
102 : CHIP_ERROR InitAnonymousStructure(chip::TLV::TLVWriter * const apWriter);
103 : };
104 : } // namespace app
105 : } // namespace chip
|