Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : * All rights reserved.
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 : #pragma once
20 :
21 : #include <app-common/zap-generated/ids/Attributes.h>
22 : #include <app-common/zap-generated/ids/Clusters.h>
23 : #include <app/AttributePathParams.h>
24 : #include <app/ConcreteAttributePath.h>
25 : #include <app/InteractionModelTimeout.h>
26 : #include <app/MessageDef/AttributeDataIBs.h>
27 : #include <app/MessageDef/AttributeStatusIB.h>
28 : #include <app/MessageDef/StatusIB.h>
29 : #include <app/MessageDef/WriteRequestMessage.h>
30 : #include <app/data-model/Encode.h>
31 : #include <app/data-model/FabricScoped.h>
32 : #include <app/data-model/List.h>
33 : #include <lib/core/CHIPCore.h>
34 : #include <lib/core/TLVDebug.h>
35 : #include <lib/support/CodeUtils.h>
36 : #include <lib/support/DLLUtil.h>
37 : #include <lib/support/logging/CHIPLogging.h>
38 : #include <messaging/ExchangeHolder.h>
39 : #include <messaging/ExchangeMgr.h>
40 : #include <messaging/Flags.h>
41 : #include <platform/LockTracker.h>
42 : #include <protocols/Protocols.h>
43 : #include <system/SystemPacketBuffer.h>
44 : #include <system/TLVPacketBufferBackingStore.h>
45 :
46 : namespace chip {
47 : namespace app {
48 :
49 : class InteractionModelEngine;
50 :
51 : /**
52 : * @brief The write client represents the initiator side of a Write Interaction, and is responsible
53 : * for generating one Write Request for a particular set of attributes, and handling the Write response.
54 : * Consumer can allocate one write client, then call PrepareAttribute, insert attribute value, followed
55 : * by FinishAttribute for every attribute it wants to insert in write request, then call SendWriteRequest
56 : *
57 : * Note: When writing lists, you may receive multiple write status responses for a single list.
58 : * Please see ChunkedWriteCallback.h for a high level API which will merge status codes for
59 : * chunked write requests.
60 : *
61 : */
62 : class WriteClient : public Messaging::ExchangeDelegate
63 : {
64 : public:
65 : class Callback
66 : {
67 : public:
68 0 : virtual ~Callback() = default;
69 :
70 : /**
71 : * OnResponse will be called when a write response has been received
72 : * and processed for the given path.
73 : *
74 : * The WriteClient object MUST continue to exist after this call is completed. The application shall wait until it
75 : * receives an OnDone call before it shuts down the object.
76 : *
77 : * @param[in] apWriteClient The write client object that initiated the write transaction.
78 : * @param[in] aPath The attribute path field in write response.
79 : * @param[in] attributeStatus Attribute-specific status, containing an InteractionModel::Status code as well as
80 : * an optional cluster-specific status code.
81 : */
82 0 : virtual void OnResponse(const WriteClient * apWriteClient, const ConcreteDataAttributePath & aPath,
83 : StatusIB attributeStatus)
84 0 : {}
85 :
86 : /**
87 : * OnError will be called when an error occurs *after* a successful call to SendWriteRequest(). The following
88 : * errors will be delivered through this call in the aError field:
89 : *
90 : * - CHIP_ERROR_TIMEOUT: A response was not received within the expected response timeout.
91 : * - CHIP_ERROR_*TLV*: A malformed, non-compliant response was received from the server.
92 : * - CHIP_ERROR encapsulating a StatusIB: If we got a non-path-specific
93 : * status response from the server. In that case, constructing
94 : * a StatusIB from the error can be used to extract the status.
95 : * - CHIP_ERROR*: All other cases.
96 : *
97 : * The WriteClient object MUST continue to exist after this call is completed. The application shall wait until it
98 : * receives an OnDone call before it shuts down the object.
99 : *
100 : * @param[in] apWriteClient The write client object that initiated the attribute write transaction.
101 : * @param[in] aError A system error code that conveys the overall error code.
102 : */
103 0 : virtual void OnError(const WriteClient * apWriteClient, CHIP_ERROR aError) {}
104 :
105 : /**
106 : * OnDone will be called when WriteClient has finished all work and is reserved for future WriteClient ownership change.
107 : * (#10366) Users may use this function to release their own objects related to this write interaction.
108 : *
109 : * This function will:
110 : * - Always be called exactly *once* for a given WriteClient instance.
111 : * - Be called even in error circumstances.
112 : * - Only be called after a successful call to SendWriteRequest has been made.
113 : *
114 : * @param[in] apWriteClient The write client object of the terminated write transaction.
115 : */
116 : virtual void OnDone(WriteClient * apWriteClient) = 0;
117 : };
118 :
119 : /**
120 : * Construct the client object. Within the lifetime
121 : * of this instance.
122 : *
123 : * @param[in] apExchangeMgr A pointer to the ExchangeManager object.
124 : * @param[in] apCallback Callback set by application.
125 : * @param[in] aTimedWriteTimeoutMs If provided, do a timed write using this timeout.
126 : * @param[in] aSuppressResponse If provided, set SuppressResponse field to the provided value
127 : */
128 : WriteClient(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback, const Optional<uint16_t> & aTimedWriteTimeoutMs,
129 : bool aSuppressResponse = false) :
130 : mpExchangeMgr(apExchangeMgr),
131 : mExchangeCtx(*this), mpCallback(apCallback), mTimedWriteTimeoutMs(aTimedWriteTimeoutMs),
132 : mSuppressResponse(aSuppressResponse)
133 : {
134 : assertChipStackLockedByCurrentThread();
135 : }
136 :
137 : #if CONFIG_BUILD_FOR_HOST_UNIT_TEST
138 : WriteClient(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback, const Optional<uint16_t> & aTimedWriteTimeoutMs,
139 : uint16_t aReservedSize) :
140 : mpExchangeMgr(apExchangeMgr),
141 : mExchangeCtx(*this), mpCallback(apCallback), mTimedWriteTimeoutMs(aTimedWriteTimeoutMs), mReservedSize(aReservedSize)
142 : {
143 : assertChipStackLockedByCurrentThread();
144 : }
145 : #endif
146 :
147 0 : ~WriteClient() { assertChipStackLockedByCurrentThread(); }
148 :
149 : /**
150 : * Encode an attribute value that can be directly encoded using DataModel::Encode. Will create a new chunk when necessary.
151 : */
152 : template <class T>
153 : CHIP_ERROR EncodeAttribute(const AttributePathParams & attributePath, const T & value,
154 : const Optional<DataVersion> & aDataVersion = NullOptional)
155 : {
156 : ReturnErrorOnFailure(EnsureMessage());
157 :
158 : // Here, we are using kInvalidEndpointId for missing endpoint id, which is used when sending group write requests.
159 : return EncodeSingleAttributeDataIB(
160 : ConcreteDataAttributePath(attributePath.HasWildcardEndpointId() ? kInvalidEndpointId : attributePath.mEndpointId,
161 : attributePath.mClusterId, attributePath.mAttributeId, aDataVersion),
162 : value);
163 : }
164 :
165 : /**
166 : * Encode a possibly-chunked list attribute value. Will create a new chunk when necessary.
167 : *
168 : * Note: As an exception, for attributes in the Access Control cluster, this method will attempt to encode as many list items
169 : * as possible into a single AttributeDataIB with Change set to REPLACE.
170 : * If the list is too large, the WriteRequest will be chunked and remaining items will be encoded as individual AttributeDataIBs
171 : * with Change set to ADD, chunking them as needed.
172 : *
173 : */
174 : template <class T>
175 : CHIP_ERROR EncodeAttribute(const AttributePathParams & attributePath, const DataModel::List<T> & listValue,
176 : const Optional<DataVersion> & aDataVersion = NullOptional)
177 : {
178 : // Here, we are using kInvalidEndpointId for missing endpoint id, which is used when sending group write requests.
179 : ConcreteDataAttributePath path =
180 : ConcreteDataAttributePath(attributePath.HasWildcardEndpointId() ? kInvalidEndpointId : attributePath.mEndpointId,
181 : attributePath.mClusterId, attributePath.mAttributeId, aDataVersion);
182 :
183 : ListIndex firstItemToAppendIndex = 0;
184 : uint16_t encodedItemCount = 0;
185 : bool chunkingNeeded = false;
186 :
187 : // By convention, and as tested against all cluster servers, clients have historically encoded an empty list as a
188 : // ReplaceAll, (i.e. the entire attribute contents are cleared before appending the new list’s items). However, this
189 : // behavior can be problematic, especially for the ACL attribute; sending an empty ReplaceAll list can cause clients to be
190 : // locked out. This is because the empty list first deletes all existing ACL entries, and if the new (malformed) ACL is
191 : // rejected, the server is left without valid (or with incomplete) ACLs.
192 : // SOLUTION: we treat ACL as an exception and avoid encoding an empty ReplaceAll list. Instead, we pack as many ACL entries
193 : // as possible into the ReplaceAll list, and send any remaining entries in subsequent chunks are part of the AppendItem
194 : // list operation.
195 : // TODO (#38270): Generalize this behavior; send a non-empty ReplaceAll list for all clusters in a later Matter version and
196 : // enforce all clusters to support it in testing and in certification.
197 : bool encodeEmptyListAsReplaceAll = !(path.mClusterId == Clusters::AccessControl::Id);
198 :
199 : ReturnErrorOnFailure(EnsureMessage());
200 :
201 : if (encodeEmptyListAsReplaceAll)
202 : {
203 : ReturnErrorOnFailure(EncodeSingleAttributeDataIB(path, DataModel::List<uint8_t>()));
204 : }
205 : else
206 : {
207 : // Encode as many list-items as possible into a single AttributeDataIB, which will be included in a single
208 : // WriteRequestMessage chunk.
209 : ReturnErrorOnFailure(TryEncodeListIntoSingleAttributeDataIB(path, listValue, chunkingNeeded, encodedItemCount));
210 :
211 : // If all list items fit perfectly into a single AttributeDataIB, there is no need for any `append-item` or chunking,
212 : // and we can exit early.
213 : VerifyOrReturnError(chunkingNeeded, CHIP_NO_ERROR);
214 :
215 : // Start a new WriteRequest chunk, as there are still remaining list items to encode. These remaining items will be
216 : // appended one by one, each into its own AttributeDataIB. Unlike the first chunk (which contains only one
217 : // AttributeDataIB), subsequent chunks may contain multiple AttributeDataIBs if space allows it.
218 : ReturnErrorOnFailure(StartNewMessage());
219 : firstItemToAppendIndex = encodedItemCount;
220 : }
221 :
222 : path.mListOp = ConcreteDataAttributePath::ListOperation::AppendItem;
223 :
224 : for (ListIndex i = firstItemToAppendIndex; i < listValue.size(); i++)
225 : {
226 : ReturnErrorOnFailure(EncodeSingleAttributeDataIB(path, listValue[i]));
227 : }
228 :
229 : return CHIP_NO_ERROR;
230 : }
231 :
232 : /**
233 : * Encode a Nullable attribute value. This needs a separate overload so it can dispatch to the right
234 : * EncodeAttribute when writing a nullable list.
235 : */
236 : template <class T>
237 : CHIP_ERROR EncodeAttribute(const AttributePathParams & attributePath, const DataModel::Nullable<T> & value,
238 : const Optional<DataVersion> & aDataVersion = NullOptional)
239 : {
240 : ReturnErrorOnFailure(EnsureMessage());
241 :
242 : if (value.IsNull())
243 : {
244 : // Here, we are using kInvalidEndpointId to for missing endpoint id, which is used when sending group write requests.
245 : return EncodeSingleAttributeDataIB(
246 : ConcreteDataAttributePath(attributePath.HasWildcardEndpointId() ? kInvalidEndpointId : attributePath.mEndpointId,
247 : attributePath.mClusterId, attributePath.mAttributeId, aDataVersion),
248 : value);
249 : }
250 :
251 : return EncodeAttribute(attributePath, value.Value(), aDataVersion);
252 : }
253 :
254 : enum class TestListEncodingOverride
255 : {
256 : kNoOverride,
257 : kForceLegacyEncoding
258 : };
259 :
260 : /**
261 : * Encode an attribute value which is already encoded into a TLV. The TLVReader is expected to be initialized and the read head
262 : * is expected to point to the element to be encoded.
263 : *
264 : * Note: When encoding lists with this function, you may receive more than one write status for a single list. You can refer
265 : * to ChunkedWriteCallback.h for a high level API which will merge status codes for chunked write requests.
266 : *
267 : * Note: forceLegacyListEncoding is used by Test Harness and Python Tests to test backward compatibility and ensure end devices
268 : * support legacy WriteClients
269 : */
270 : CHIP_ERROR PutPreencodedAttribute(const ConcreteDataAttributePath & attributePath, const TLV::TLVReader & data,
271 : TestListEncodingOverride encodingBehavior = TestListEncodingOverride::kNoOverride);
272 :
273 : /**
274 : * Once SendWriteRequest returns successfully, the WriteClient will
275 : * handle calling Shutdown on itself once it decides it's done with waiting
276 : * for a response (i.e. times out or gets a response). Client can specify
277 : * the maximum time to wait for response (in milliseconds) via timeout parameter.
278 : * If the timeout is missing or is set to System::Clock::kZero, a value based on the MRP timeouts of the session will be used.
279 : * If SendWriteRequest is never called, or the call fails, the API
280 : * consumer is responsible for calling Shutdown on the WriteClient.
281 : */
282 : CHIP_ERROR SendWriteRequest(const SessionHandle & session, System::Clock::Timeout timeout = System::Clock::kZero);
283 :
284 : /**
285 : * Returns true if the WriteRequest is Chunked.
286 : * WARNING: This method is only used for UnitTests. It should only be called AFTER a call
287 : * EncodeAttribute/PutPreencodedAttribute AND BEFORE a call to SendWriteRequest(); only during this window does
288 : * "!mChunks.IsNull()" reliably indicate that the WriteRequest is chunked.
289 : */
290 : bool IsWriteRequestChunked() const { return !mChunks.IsNull(); };
291 :
292 : private:
293 : friend class TestWriteInteraction;
294 : friend class InteractionModelEngine;
295 : enum class State
296 : {
297 : Initialized = 0, // The client has been initialized
298 : AddAttribute, // The client has added attribute and ready for a SendWriteRequest
299 : AwaitingTimedStatus, // Sent a Tiemd Request, waiting for response.
300 : AwaitingResponse, // The client has sent out the write request message
301 : ResponseReceived, // We have gotten a response after sending write request
302 : AwaitingDestruction, // The object has completed its work and is awaiting destruction by the application.
303 : };
304 :
305 : CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
306 : System::PacketBufferHandle && aPayload) override;
307 : void OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) override;
308 :
309 : void MoveToState(const State aTargetState);
310 : CHIP_ERROR ProcessWriteResponseMessage(System::PacketBufferHandle && payload);
311 : CHIP_ERROR ProcessAttributeStatusIB(AttributeStatusIB::Parser & aAttributeStatusIB);
312 : const char * GetStateStr() const;
313 :
314 : // TODO (#38453) Clarify and fix the API contract of EnsureListStarted, TryToStartList and EnsureListEnded; in the case of
315 : // encoding failure, should we just undo buffer reservation? rollback to a checkpoint that we create within EnsureListStarted?
316 : // or just leave the WriteClient in a bad state.
317 : /**
318 : * A wrapper for TryToStartList which will start a new chunk when TryToStartList fails with CHIP_ERROR_NO_MEMORY or
319 : * CHIP_ERROR_BUFFER_TOO_SMALL.
320 : *
321 : * @note Must always be followed by a call to EnsureListEnded(), to undo buffer reservation that took place within
322 : * it, and properly close TLV Containers.
323 : */
324 : CHIP_ERROR EnsureListStarted(const ConcreteDataAttributePath & attributePath);
325 :
326 : /**
327 : * Prepare the Encoding of an Attribute with List DataType into an AttributeDataIB.
328 : *
329 : */
330 : CHIP_ERROR TryToStartList(const ConcreteDataAttributePath & attributePath);
331 :
332 : /**
333 : * Complete the Encoding of an Attribute with List DataType into an AttributeDataIB.
334 : *
335 : * @note Must always be called after EnsureListStarted(), even in cases of encoding failures; to undo buffer reservation that
336 : * took place in EnsureListStarted.
337 : */
338 : CHIP_ERROR EnsureListEnded();
339 :
340 : /**
341 : * Encode an attribute value that can be directly encoded using DataModel::Encode.
342 : */
343 : template <class T, std::enable_if_t<!DataModel::IsFabricScoped<T>::value, int> = 0>
344 0 : CHIP_ERROR TryEncodeSingleAttributeDataIB(const ConcreteDataAttributePath & attributePath, const T & value)
345 : {
346 0 : chip::TLV::TLVWriter * writer = nullptr;
347 :
348 0 : ReturnErrorOnFailure(PrepareAttributeIB(attributePath));
349 0 : VerifyOrReturnError((writer = GetAttributeDataIBTLVWriter()) != nullptr, CHIP_ERROR_INCORRECT_STATE);
350 0 : ReturnErrorOnFailure(DataModel::Encode(*writer, chip::TLV::ContextTag(chip::app::AttributeDataIB::Tag::kData), value));
351 0 : ReturnErrorOnFailure(FinishAttributeIB());
352 :
353 0 : return CHIP_NO_ERROR;
354 : }
355 :
356 : template <class T, std::enable_if_t<DataModel::IsFabricScoped<T>::value, int> = 0>
357 : CHIP_ERROR TryEncodeSingleAttributeDataIB(const ConcreteDataAttributePath & attributePath, const T & value)
358 : {
359 : chip::TLV::TLVWriter * writer = nullptr;
360 :
361 : ReturnErrorOnFailure(PrepareAttributeIB(attributePath));
362 : VerifyOrReturnError((writer = GetAttributeDataIBTLVWriter()) != nullptr, CHIP_ERROR_INCORRECT_STATE);
363 : ReturnErrorOnFailure(
364 : DataModel::EncodeForWrite(*writer, chip::TLV::ContextTag(chip::app::AttributeDataIB::Tag::kData), value));
365 : ReturnErrorOnFailure(FinishAttributeIB());
366 :
367 : return CHIP_NO_ERROR;
368 : }
369 :
370 : template <class T>
371 : CHIP_ERROR TryEncodeListIntoSingleAttributeDataIB(const ConcreteDataAttributePath & attributePath,
372 : const DataModel::List<T> & list, bool & outChunkingNeeded,
373 : uint16_t & outEncodedItemCount)
374 : {
375 : ReturnErrorOnFailure(EnsureListStarted(attributePath));
376 :
377 : AttributeDataIB::Builder & attributeDataIB = mWriteRequestBuilder.GetWriteRequests().GetAttributeDataIBBuilder();
378 : TLV::TLVWriter backupWriter;
379 : outEncodedItemCount = 0;
380 :
381 : for (auto & item : list)
382 : {
383 : // Try to put all the list items into the list we just started, until we either run out of items
384 : // or run out of space.
385 : // Make sure that if we run out of space we don't leave a partially-encoded list item around.
386 : attributeDataIB.Checkpoint(backupWriter);
387 : CHIP_ERROR err = CHIP_NO_ERROR;
388 :
389 : if constexpr (DataModel::IsFabricScoped<T>::value)
390 : {
391 : err = DataModel::EncodeForWrite(*attributeDataIB.GetWriter(), TLV::AnonymousTag(), item);
392 : }
393 : else
394 : {
395 : err = DataModel::Encode(*attributeDataIB.GetWriter(), TLV::AnonymousTag(), item);
396 : }
397 :
398 : if (err == CHIP_ERROR_NO_MEMORY || err == CHIP_ERROR_BUFFER_TOO_SMALL)
399 : {
400 : // Rollback through the attributeDataIB, which also resets the Builder's error state.
401 : // This returns the object to the state it was in before attempting to encode the list item.
402 : attributeDataIB.Rollback(backupWriter);
403 : outChunkingNeeded = true;
404 : break;
405 : }
406 : ReturnErrorOnFailure(err);
407 : outEncodedItemCount++;
408 : }
409 :
410 : return EnsureListEnded();
411 : }
412 :
413 : /**
414 : * A wrapper for TryEncodeSingleAttributeDataIB which will start a new chunk when failed with CHIP_ERROR_NO_MEMORY or
415 : * CHIP_ERROR_BUFFER_TOO_SMALL.
416 : *
417 : * NOTE: This method must not be used for encoding non-empty lists, even if the template accepts a list type.
418 : * For such cases, use TryEncodeListIntoSingleAttributeDataIB as part of a suitable encoding strategy,
419 : * since it has a different contract and has different usage expectations.
420 : */
421 : template <class T>
422 0 : CHIP_ERROR EncodeSingleAttributeDataIB(const ConcreteDataAttributePath & attributePath, const T & value)
423 : {
424 0 : TLV::TLVWriter backupWriter;
425 :
426 0 : mWriteRequestBuilder.GetWriteRequests().Checkpoint(backupWriter);
427 :
428 : // First attempt to write this attribute.
429 0 : CHIP_ERROR err = TryEncodeSingleAttributeDataIB(attributePath, value);
430 0 : if (err == CHIP_ERROR_NO_MEMORY || err == CHIP_ERROR_BUFFER_TOO_SMALL)
431 : {
432 : // If it failed with no memory, then we create a new chunk for it.
433 0 : mWriteRequestBuilder.GetWriteRequests().Rollback(backupWriter);
434 0 : ReturnErrorOnFailure(StartNewMessage());
435 0 : ReturnErrorOnFailure(TryEncodeSingleAttributeDataIB(attributePath, value));
436 : }
437 : else
438 : {
439 0 : ReturnErrorOnFailure(err);
440 : }
441 :
442 0 : return CHIP_NO_ERROR;
443 : }
444 :
445 : /**
446 : * Encode a preencoded attribute data, returns TLV encode error if the remaining space of current chunk is too small for the
447 : * AttributeDataIB.
448 : */
449 : CHIP_ERROR TryPutSinglePreencodedAttributeWritePayload(const ConcreteDataAttributePath & attributePath,
450 : const TLV::TLVReader & data);
451 :
452 : /**
453 : * Encode a preencoded attribute data, will try to create a new chunk when necessary.
454 : */
455 : CHIP_ERROR PutSinglePreencodedAttributeWritePayload(const ConcreteDataAttributePath & attributePath,
456 : const TLV::TLVReader & data);
457 :
458 : /**
459 : * Encodes preencoded attribute data into a list, that will be decoded by cluster servers as a REPLACE Change.
460 : * Returns outChunkingNeeded = true if it was not possible to fit all the data into a single list.
461 : */
462 : CHIP_ERROR TryPutPreencodedAttributeWritePayloadIntoList(const chip::app::ConcreteDataAttributePath & attributePath,
463 : TLV::TLVReader & valueReader, bool & outChunkingNeeded,
464 : uint16_t & outEncodedItemCount);
465 : CHIP_ERROR EnsureMessage();
466 :
467 : /**
468 : * Called internally to signal the completion of all work on this object, gracefully close the
469 : * exchange (by calling into the base class) and finally, signal to the application that it's
470 : * safe to release this object.
471 : */
472 : void Close();
473 :
474 : /**
475 : * This forcibly closes the exchange context if a valid one is pointed to. Such a situation does
476 : * not arise during normal message processing flows that all normally call Close() above. This can only
477 : * arise due to application-initiated destruction of the object when this object is handling receiving/sending
478 : * message payloads.
479 : */
480 : void Abort();
481 :
482 : // Send our queued-up Write Request message. Assumes the exchange is ready
483 : // and mPendingWriteData is populated.
484 : CHIP_ERROR SendWriteRequest();
485 :
486 : // Encodes the header of an AttributeDataIB, a special case for attributePath is its EndpointId can be kInvalidEndpointId, this
487 : // is used when sending group write requests.
488 : // TODO(#14935) Update AttributePathParams to support more list operations.
489 : CHIP_ERROR PrepareAttributeIB(const ConcreteDataAttributePath & attributePath);
490 : CHIP_ERROR FinishAttributeIB();
491 : TLV::TLVWriter * GetAttributeDataIBTLVWriter();
492 :
493 : /**
494 : * Create a new message (or a new chunk) for the write request.
495 : */
496 : CHIP_ERROR StartNewMessage();
497 :
498 : /**
499 : * Finalize Write Request Message TLV Builder and retrieve final data from tlv builder for later sending
500 : */
501 : CHIP_ERROR FinalizeMessage(bool aHasMoreChunks);
502 :
503 : Messaging::ExchangeManager * mpExchangeMgr = nullptr;
504 : Messaging::ExchangeHolder mExchangeCtx;
505 : Callback * mpCallback = nullptr;
506 : State mState = State::Initialized;
507 : System::PacketBufferTLVWriter mMessageWriter;
508 : WriteRequestMessage::Builder mWriteRequestBuilder;
509 : // TODO Maybe we should change PacketBufferTLVWriter so we can finalize it
510 : // but have it hold on to the buffer, and get the buffer from it later.
511 : // Then we could avoid this extra pointer-sized member.
512 : System::PacketBufferHandle mPendingWriteData;
513 : // If mTimedWriteTimeoutMs has a value, we are expected to do a timed
514 : // write.
515 : Optional<uint16_t> mTimedWriteTimeoutMs;
516 : bool mSuppressResponse = false;
517 :
518 : // A list of buffers, one buffer for each chunk.
519 : System::PacketBufferHandle mChunks;
520 :
521 : // TODO: This file might be compiled with different build flags on Darwin platform (when building WriteClient.cpp and
522 : // CHIPClustersObjc.mm), which will cause undefined behavior when building write requests. Uncomment the #if and #endif after
523 : // resolving it.
524 : // #if CONFIG_BUILD_FOR_HOST_UNIT_TEST
525 : uint16_t mReservedSize = 0;
526 : // #endif
527 :
528 : /**
529 : * Below we define several const variables for encoding overheads.
530 : * WriteRequestMessage =
531 : * {
532 : * timedRequest = false,
533 : * AttributeDataIBs =
534 : * [
535 : * AttributeDataIB = \
536 : * { |
537 : * DataVersion = 0x0, |
538 : * AttributePathIB = |
539 : * { |
540 : * Endpoint = 0x2, | "atomically" encoded via
541 : * Cluster = 0x50f, > EncodeAttribute or
542 : * Attribute = 0x0000_0006, | PutPreencodedAttribute
543 : * ListIndex = Null, |
544 : * } |
545 : * Data = ... |
546 : * }, /
547 : * (...)
548 : * ], <-- 1 byte "end of AttributeDataIB" (end of container)
549 : * moreChunkedMessages = false, <-- 2 bytes "kReservedSizeForMoreChunksFlag"
550 : * InteractionModelRevision = 1,<-- 3 bytes "kReservedSizeForIMRevision"
551 : * } <-- 1 byte "end of WriteRequestMessage" (end of container)
552 : */
553 :
554 : // Reserved size for the MoreChunks boolean flag, which takes up 1 byte for the control tag and 1 byte for the context tag.
555 : static constexpr uint16_t kReservedSizeForMoreChunksFlag = 1 + 1;
556 : // End Of Container (0x18) uses one byte.
557 : static constexpr uint16_t kReservedSizeForEndOfContainer = 1;
558 : // Reserved size for the uint8_t InteractionModelRevision flag, which takes up 1 byte for the control tag and 1 byte for the
559 : // context tag, 1 byte for value
560 : static constexpr uint16_t kReservedSizeForIMRevision = 1 + 1 + 1;
561 : // Reserved buffer for TLV level overhead (the overhead for end of AttributeDataIBs (end of container), more chunks flag, end
562 : // of WriteRequestMessage (another end of container)).
563 : static constexpr uint16_t kReservedSizeForTLVEncodingOverhead = kReservedSizeForIMRevision + kReservedSizeForMoreChunksFlag +
564 : kReservedSizeForEndOfContainer + kReservedSizeForEndOfContainer;
565 : bool mHasDataVersion = false;
566 :
567 : static constexpr uint16_t kReservedSizeForEndOfListAttributeIB =
568 : kReservedSizeForEndOfContainer + AttributeDataIB::Builder::GetSizeToEndAttributeDataIB();
569 :
570 : static constexpr TLV::TLVType kAttributeDataIBType = TLV::kTLVType_Structure;
571 : };
572 :
573 : } // namespace app
574 : } // namespace chip
|