Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <app/AppConfig.h>
21 : #include <app/util/basic-types.h>
22 : #include <lib/core/CHIPCore.h>
23 : #include <lib/core/TLV.h>
24 : #include <lib/support/CodeUtils.h>
25 : #include <lib/support/logging/CHIPLogging.h>
26 :
27 : #include "InvokeResponseIBs.h"
28 : #include "MessageBuilder.h"
29 : #include "MessageParser.h"
30 :
31 : namespace chip {
32 : namespace app {
33 : namespace InvokeResponseMessage {
34 : enum class Tag : uint8_t
35 : {
36 : kSuppressResponse = 0,
37 : kInvokeResponses = 1,
38 : kMoreChunkedMessages = 2,
39 : };
40 :
41 : class Parser : public MessageParser
42 : {
43 : public:
44 : #if CHIP_CONFIG_IM_PRETTY_PRINT
45 : CHIP_ERROR PrettyPrint() const;
46 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
47 :
48 : /**
49 : * @brief Get SuppressResponse. Next() must be called before accessing them.
50 : *
51 : * @return #CHIP_NO_ERROR on success
52 : * #CHIP_END_OF_TLV if there is no such element
53 : */
54 : CHIP_ERROR GetSuppressResponse(bool * const apSuppressResponse) const;
55 :
56 : /**
57 : * @brief Get a parser for a InvokeResponse.
58 : *
59 : * @param [in] apInvokeResponses A pointer to the invoke response list parser.
60 : *
61 : * @return #CHIP_NO_ERROR on success
62 : * #CHIP_END_OF_TLV if there is no such element
63 : */
64 : CHIP_ERROR GetInvokeResponses(InvokeResponseIBs::Parser * const apInvokeResponses) const;
65 :
66 : /**
67 : * @brief Get MoreChunkedMessages boolean
68 : *
69 : * @param [out] apMoreChunkedMessages A pointer to bool for storing more chunked messages value.
70 : *
71 : * @return #CHIP_NO_ERROR on success
72 : * #CHIP_END_OF_TLV if there is no such element
73 : */
74 : CHIP_ERROR GetMoreChunkedMessages(bool * const apMoreChunkedMessages) const;
75 : };
76 :
77 : class Builder : public MessageBuilder
78 : {
79 : public:
80 : /**
81 : * @brief Performs underlying StructBuilder::Init, but reserves memory need in
82 : * EndOfInvokeResponseMessage() with underlying TLVWriter.
83 : */
84 : CHIP_ERROR InitWithEndBufferReserved(TLV::TLVWriter * const apWriter);
85 :
86 : /**
87 : * @brief This is set to 'true' by the subscriber to indicate preservation of previous subscriptions. If omitted, it implies
88 : * 'false' as a value.
89 : */
90 : InvokeResponseMessage::Builder & SuppressResponse(const bool aSuppressResponse);
91 :
92 : /**
93 : * @brief Initialize a InvokeResponseIBs::Builder for writing into the TLV stream
94 : *
95 : * @return A reference to InvokeResponseIBs::Builder
96 : */
97 : InvokeResponseIBs::Builder & CreateInvokeResponses(const bool aReserveEndBuffer = false);
98 :
99 : /**
100 : * @brief Get reference to InvokeResponseIBs::Builder
101 : *
102 : * @return A reference to InvokeResponseIBs::Builder
103 : */
104 340 : InvokeResponseIBs::Builder & GetInvokeResponses() { return mInvokeResponses; }
105 :
106 : /**
107 : * @brief Set True if the set of InvokeResponseIB have to be sent across multiple packets in a single transaction
108 : * @param [in] aMoreChunkedMessages true if more chunked messages are needed
109 : * @return A reference to *this
110 : */
111 : InvokeResponseMessage::Builder & MoreChunkedMessages(const bool aMoreChunkedMessages);
112 :
113 : /**
114 : * @brief Reserved space in TLVWriter for MoreChunkedMessages
115 : * @return CHIP_NO_ERROR upon successfully reserving space for MoreChunkedMessages
116 : * @return other CHIP error see TLVWriter::ReserveBuffer for more details.
117 : */
118 : CHIP_ERROR ReserveSpaceForMoreChunkedMessages();
119 :
120 : /**
121 : * @brief Mark the end of this InvokeResponseMessage
122 : *
123 : * @return The builder's final status.
124 : */
125 : CHIP_ERROR EndOfInvokeResponseMessage();
126 :
127 : /**
128 : * @brief Get number of bytes required in the buffer by MoreChunkedMessages
129 : *
130 : * @return Expected number of bytes required in the buffer by MoreChunkedMessages()
131 : */
132 : uint32_t GetSizeForMoreChunkResponses();
133 :
134 : /**
135 : * @brief Get number of bytes required in the buffer by EndOfInvokeResponseMessage()
136 : *
137 : * @return Expected number of bytes required in the buffer by EndOfInvokeResponseMessage()
138 : */
139 : uint32_t GetSizeToEndInvokeResponseMessage();
140 :
141 : private:
142 : InvokeResponseIBs::Builder mInvokeResponses;
143 : bool mIsEndBufferReserved = false;
144 : bool mIsMoreChunkMessageBufferReserved = false;
145 : };
146 : } // namespace InvokeResponseMessage
147 : } // namespace app
148 : } // namespace chip
|