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 : #include <app/AppConfig.h>
20 : #include <app/AttributeAccessInterfaceRegistry.h>
21 : #include <app/AttributeValueDecoder.h>
22 : #include <app/ConcreteAttributePath.h>
23 : #include <app/GlobalAttributes.h>
24 : #include <app/InteractionModelEngine.h>
25 : #include <app/MessageDef/EventPathIB.h>
26 : #include <app/MessageDef/StatusIB.h>
27 : #include <app/RequiredPrivilege.h>
28 : #include <app/StatusResponse.h>
29 : #include <app/WriteHandler.h>
30 : #include <app/data-model-provider/ActionReturnStatus.h>
31 : #include <app/data-model-provider/MetadataLookup.h>
32 : #include <app/data-model-provider/MetadataTypes.h>
33 : #include <app/data-model-provider/OperationTypes.h>
34 : #include <app/reporting/Engine.h>
35 : #include <app/util/MatterCallbacks.h>
36 : #include <credentials/GroupDataProvider.h>
37 : #include <lib/core/CHIPError.h>
38 : #include <lib/support/CodeUtils.h>
39 : #include <lib/support/TypeTraits.h>
40 : #include <lib/support/logging/TextOnlyLogging.h>
41 : #include <messaging/ExchangeContext.h>
42 : #include <protocols/interaction_model/StatusCode.h>
43 :
44 : #include <optional>
45 :
46 : namespace chip {
47 : namespace app {
48 :
49 : namespace {
50 :
51 : using Protocols::InteractionModel::Status;
52 :
53 : /// Wraps a EndpointIterator and ensures that `::Release()` is called
54 : /// for the iterator (assuming it is non-null)
55 : class AutoReleaseGroupEndpointIterator
56 : {
57 : public:
58 0 : explicit AutoReleaseGroupEndpointIterator(Credentials::GroupDataProvider::EndpointIterator * iterator) : mIterator(iterator) {}
59 0 : ~AutoReleaseGroupEndpointIterator()
60 : {
61 0 : if (mIterator != nullptr)
62 : {
63 0 : mIterator->Release();
64 : }
65 0 : }
66 :
67 0 : bool IsNull() const { return mIterator == nullptr; }
68 0 : bool Next(Credentials::GroupDataProvider::GroupEndpoint & item) { return mIterator->Next(item); }
69 :
70 : private:
71 : Credentials::GroupDataProvider::EndpointIterator * mIterator;
72 : };
73 :
74 : } // namespace
75 :
76 : using namespace Protocols::InteractionModel;
77 : using Status = Protocols::InteractionModel::Status;
78 :
79 789 : CHIP_ERROR WriteHandler::Init(DataModel::Provider * apProvider, WriteHandlerDelegate * apWriteHandlerDelegate)
80 : {
81 789 : VerifyOrReturnError(!mExchangeCtx, CHIP_ERROR_INCORRECT_STATE);
82 789 : VerifyOrReturnError(apWriteHandlerDelegate, CHIP_ERROR_INVALID_ARGUMENT);
83 789 : VerifyOrReturnError(apProvider, CHIP_ERROR_INVALID_ARGUMENT);
84 788 : mDataModelProvider = apProvider;
85 :
86 788 : mDelegate = apWriteHandlerDelegate;
87 788 : MoveToState(State::Initialized);
88 :
89 788 : mACLCheckCache.ClearValue();
90 788 : mProcessingAttributePath.ClearValue();
91 :
92 788 : return CHIP_NO_ERROR;
93 : }
94 :
95 788 : void WriteHandler::Close()
96 : {
97 788 : VerifyOrReturn(mState != State::Uninitialized);
98 :
99 : // DeliverFinalListWriteEnd will be a no-op if we have called
100 : // DeliverFinalListWriteEnd in success conditions, so passing false for
101 : // wasSuccessful here is safe: if it does anything, we were in fact not
102 : // successful.
103 788 : DeliverFinalListWriteEnd(false /* wasSuccessful */);
104 788 : mExchangeCtx.Release();
105 788 : mStateFlags.Clear(StateBits::kSuppressResponse);
106 788 : mDataModelProvider = nullptr;
107 788 : MoveToState(State::Uninitialized);
108 : }
109 :
110 850 : std::optional<bool> WriteHandler::IsListAttributePath(const ConcreteAttributePath & path)
111 : {
112 850 : if (mDataModelProvider == nullptr)
113 : {
114 : #if CHIP_CONFIG_DATA_MODEL_EXTRA_LOGGING
115 0 : ChipLogError(DataManagement, "Null data model while checking attribute properties.");
116 : #endif
117 0 : return std::nullopt;
118 : }
119 :
120 850 : DataModel::AttributeFinder finder(mDataModelProvider);
121 850 : std::optional<DataModel::AttributeEntry> info = finder.Find(path);
122 :
123 850 : if (!info.has_value())
124 : {
125 0 : return std::nullopt;
126 : }
127 :
128 850 : return info->flags.Has(DataModel::AttributeQualityFlags::kListAttribute);
129 850 : }
130 :
131 3435 : Status WriteHandler::HandleWriteRequestMessage(Messaging::ExchangeContext * apExchangeContext,
132 : System::PacketBufferHandle && aPayload, bool aIsTimedWrite)
133 : {
134 3435 : System::PacketBufferHandle packet = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
135 3435 : VerifyOrReturnError(!packet.IsNull(), Status::Failure);
136 :
137 3435 : System::PacketBufferTLVWriter messageWriter;
138 3435 : messageWriter.Init(std::move(packet));
139 3435 : VerifyOrReturnError(mWriteResponseBuilder.Init(&messageWriter) == CHIP_NO_ERROR, Status::Failure);
140 :
141 3435 : mWriteResponseBuilder.CreateWriteResponses();
142 3435 : VerifyOrReturnError(mWriteResponseBuilder.GetError() == CHIP_NO_ERROR, Status::Failure);
143 :
144 3435 : Status status = ProcessWriteRequest(std::move(aPayload), aIsTimedWrite);
145 :
146 : // Do not send response on Group Write
147 3435 : if (status == Status::Success && !apExchangeContext->IsGroupExchangeContext())
148 : {
149 3432 : CHIP_ERROR err = SendWriteResponse(std::move(messageWriter));
150 3432 : if (err != CHIP_NO_ERROR)
151 : {
152 0 : status = Status::Failure;
153 : }
154 : }
155 :
156 3435 : return status;
157 3435 : }
158 :
159 788 : Status WriteHandler::OnWriteRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload,
160 : bool aIsTimedWrite)
161 : {
162 : //
163 : // Let's take over further message processing on this exchange from the IM.
164 : // This is only relevant during chunked requests.
165 : //
166 788 : mExchangeCtx.Grab(apExchangeContext);
167 :
168 788 : Status status = HandleWriteRequestMessage(apExchangeContext, std::move(aPayload), aIsTimedWrite);
169 :
170 : // The write transaction will be alive only when the message was handled successfully and there are more chunks.
171 788 : if (!(status == Status::Success && mStateFlags.Has(StateBits::kHasMoreChunks)))
172 : {
173 58 : Close();
174 : }
175 :
176 788 : return status;
177 : }
178 :
179 2648 : CHIP_ERROR WriteHandler::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
180 : System::PacketBufferHandle && aPayload)
181 : {
182 2648 : CHIP_ERROR err = CHIP_NO_ERROR;
183 :
184 2648 : VerifyOrDieWithMsg(apExchangeContext == mExchangeCtx.Get(), DataManagement,
185 : "Incoming exchange context should be same as the initial request.");
186 2648 : VerifyOrDieWithMsg(!apExchangeContext->IsGroupExchangeContext(), DataManagement,
187 : "OnMessageReceived should not be called on GroupExchangeContext");
188 2648 : if (!aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::WriteRequest))
189 : {
190 1 : if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
191 : {
192 0 : CHIP_ERROR statusError = CHIP_NO_ERROR;
193 : // Parse the status response so we can log it properly.
194 0 : StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError);
195 : }
196 1 : ChipLogDetail(DataManagement, "Unexpected message type %d", aPayloadHeader.GetMessageType());
197 1 : StatusResponse::Send(Status::InvalidAction, apExchangeContext, false /*aExpectResponse*/);
198 1 : Close();
199 1 : return CHIP_ERROR_INVALID_MESSAGE_TYPE;
200 : }
201 :
202 : Status status =
203 2647 : HandleWriteRequestMessage(apExchangeContext, std::move(aPayload), false /* chunked write should not be timed write */);
204 2647 : if (status == Status::Success)
205 : {
206 : // We have no more chunks, the write response has been sent in HandleWriteRequestMessage, so close directly.
207 2647 : if (!mStateFlags.Has(StateBits::kHasMoreChunks))
208 : {
209 724 : Close();
210 : }
211 : }
212 : else
213 : {
214 0 : err = StatusResponse::Send(status, apExchangeContext, false /*aExpectResponse*/);
215 0 : Close();
216 : }
217 2647 : return CHIP_NO_ERROR;
218 : }
219 :
220 4 : void WriteHandler::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext)
221 : {
222 4 : ChipLogError(DataManagement, "Time out! failed to receive status response from Exchange: " ChipLogFormatExchange,
223 : ChipLogValueExchange(apExchangeContext));
224 4 : Close();
225 4 : }
226 :
227 3432 : CHIP_ERROR WriteHandler::FinalizeMessage(System::PacketBufferTLVWriter && aMessageWriter, System::PacketBufferHandle & packet)
228 : {
229 3432 : VerifyOrReturnError(mState == State::AddStatus, CHIP_ERROR_INCORRECT_STATE);
230 3432 : ReturnErrorOnFailure(mWriteResponseBuilder.GetWriteResponses().EndOfAttributeStatuses());
231 3432 : ReturnErrorOnFailure(mWriteResponseBuilder.EndOfWriteResponseMessage());
232 3432 : ReturnErrorOnFailure(aMessageWriter.Finalize(&packet));
233 3432 : return CHIP_NO_ERROR;
234 : }
235 :
236 3432 : CHIP_ERROR WriteHandler::SendWriteResponse(System::PacketBufferTLVWriter && aMessageWriter)
237 : {
238 3432 : CHIP_ERROR err = CHIP_NO_ERROR;
239 3432 : System::PacketBufferHandle packet;
240 :
241 3432 : VerifyOrExit(mState == State::AddStatus, err = CHIP_ERROR_INCORRECT_STATE);
242 :
243 3432 : err = FinalizeMessage(std::move(aMessageWriter), packet);
244 3432 : SuccessOrExit(err);
245 :
246 3432 : VerifyOrExit(mExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
247 3432 : mExchangeCtx->UseSuggestedResponseTimeout(app::kExpectedIMProcessingTime);
248 6864 : err = mExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::WriteResponse, std::move(packet),
249 3432 : mStateFlags.Has(StateBits::kHasMoreChunks) ? Messaging::SendMessageFlags::kExpectResponse
250 : : Messaging::SendMessageFlags::kNone);
251 3432 : SuccessOrExit(err);
252 :
253 3432 : MoveToState(State::Sending);
254 :
255 3432 : exit:
256 3432 : return err;
257 3432 : }
258 :
259 765 : void WriteHandler::DeliverListWriteBegin(const ConcreteAttributePath & aPath)
260 : {
261 765 : if (mDataModelProvider != nullptr)
262 : {
263 765 : mDataModelProvider->ListAttributeWriteNotification(aPath, DataModel::ListWriteOperation::kListWriteBegin);
264 : }
265 765 : }
266 :
267 770 : void WriteHandler::DeliverListWriteEnd(const ConcreteAttributePath & aPath, bool writeWasSuccessful)
268 : {
269 770 : if (mDataModelProvider != nullptr)
270 : {
271 770 : mDataModelProvider->ListAttributeWriteNotification(aPath,
272 : writeWasSuccessful ? DataModel::ListWriteOperation::kListWriteSuccess
273 : : DataModel::ListWriteOperation::kListWriteFailure);
274 : }
275 770 : }
276 :
277 1567 : void WriteHandler::DeliverFinalListWriteEnd(bool writeWasSuccessful)
278 : {
279 1567 : if (mProcessingAttributePath.HasValue() && mStateFlags.Has(StateBits::kProcessingAttributeIsList))
280 : {
281 766 : DeliverListWriteEnd(mProcessingAttributePath.Value(), writeWasSuccessful);
282 : }
283 1567 : mProcessingAttributePath.ClearValue();
284 1567 : }
285 :
286 0 : CHIP_ERROR WriteHandler::DeliverFinalListWriteEndForGroupWrite(bool writeWasSuccessful)
287 : {
288 0 : VerifyOrReturnError(mProcessingAttributePath.HasValue() && mStateFlags.Has(StateBits::kProcessingAttributeIsList),
289 : CHIP_NO_ERROR);
290 :
291 0 : Credentials::GroupDataProvider::GroupEndpoint mapping;
292 0 : Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
293 : Credentials::GroupDataProvider::EndpointIterator * iterator;
294 :
295 0 : GroupId groupId = mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
296 0 : FabricIndex fabricIndex = GetAccessingFabricIndex();
297 :
298 0 : auto processingConcreteAttributePath = mProcessingAttributePath.Value();
299 0 : mProcessingAttributePath.ClearValue();
300 :
301 0 : iterator = groupDataProvider->IterateEndpoints(fabricIndex);
302 0 : VerifyOrReturnError(iterator != nullptr, CHIP_ERROR_NO_MEMORY);
303 :
304 0 : while (iterator->Next(mapping))
305 : {
306 0 : if (groupId != mapping.group_id)
307 : {
308 0 : continue;
309 : }
310 :
311 0 : processingConcreteAttributePath.mEndpointId = mapping.endpoint_id;
312 :
313 0 : VerifyOrReturnError(mDelegate, CHIP_ERROR_INCORRECT_STATE);
314 0 : if (!mDelegate->HasConflictWriteRequests(this, processingConcreteAttributePath))
315 : {
316 0 : DeliverListWriteEnd(processingConcreteAttributePath, writeWasSuccessful);
317 : }
318 : }
319 0 : iterator->Release();
320 0 : return CHIP_NO_ERROR;
321 : }
322 : namespace {
323 :
324 : // To reduce the various use of previousProcessed.HasValue() && previousProcessed.Value() == nextAttribute to save code size.
325 11873 : bool IsSameAttribute(const Optional<ConcreteAttributePath> & previousProcessed, const ConcreteDataAttributePath & nextAttribute)
326 : {
327 11873 : return previousProcessed.HasValue() && previousProcessed.Value() == nextAttribute;
328 : }
329 :
330 4246 : bool ShouldReportListWriteEnd(const Optional<ConcreteAttributePath> & previousProcessed, bool previousProcessedAttributeIsList,
331 : const ConcreteDataAttributePath & nextAttribute)
332 : {
333 4246 : return previousProcessedAttributeIsList && !IsSameAttribute(previousProcessed, nextAttribute) && previousProcessed.HasValue();
334 : }
335 :
336 4246 : bool ShouldReportListWriteBegin(const Optional<ConcreteAttributePath> & previousProcessed, bool previousProcessedAttributeIsList,
337 : const ConcreteDataAttributePath & nextAttribute)
338 : {
339 4246 : return !IsSameAttribute(previousProcessed, nextAttribute) && nextAttribute.IsListOperation();
340 : }
341 :
342 : } // namespace
343 :
344 3432 : CHIP_ERROR WriteHandler::ProcessAttributeDataIBs(TLV::TLVReader & aAttributeDataIBsReader)
345 : {
346 3432 : CHIP_ERROR err = CHIP_NO_ERROR;
347 :
348 3432 : VerifyOrReturnError(mExchangeCtx, CHIP_ERROR_INTERNAL);
349 3432 : const Access::SubjectDescriptor subjectDescriptor = mExchangeCtx->GetSessionHandle()->GetSubjectDescriptor();
350 :
351 7691 : while (CHIP_NO_ERROR == (err = aAttributeDataIBsReader.Next()))
352 : {
353 4259 : chip::TLV::TLVReader dataReader;
354 4259 : AttributeDataIB::Parser element;
355 4259 : AttributePathIB::Parser attributePath;
356 4259 : ConcreteDataAttributePath dataAttributePath;
357 4259 : TLV::TLVReader reader = aAttributeDataIBsReader;
358 :
359 4259 : err = element.Init(reader);
360 4259 : SuccessOrExit(err);
361 :
362 4259 : err = element.GetPath(&attributePath);
363 4259 : SuccessOrExit(err);
364 :
365 4259 : err = attributePath.GetConcreteAttributePath(dataAttributePath);
366 4259 : SuccessOrExit(err);
367 :
368 4259 : err = element.GetData(&dataReader);
369 4259 : SuccessOrExit(err);
370 :
371 4259 : if (!dataAttributePath.IsListOperation() && IsListAttributePath(dataAttributePath).value_or(false))
372 : {
373 830 : dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
374 : }
375 :
376 4259 : VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
377 11913 : if (mDelegate->HasConflictWriteRequests(this, dataAttributePath) ||
378 : // Per chunking protocol, we are processing the list entries, but the initial empty list is not processed, so we reject
379 : // it with Busy status code.
380 7654 : (dataAttributePath.IsListItemOperation() && !IsSameAttribute(mProcessingAttributePath, dataAttributePath)))
381 : {
382 13 : err = AddStatusInternal(dataAttributePath, StatusIB(Status::Busy));
383 13 : continue;
384 : }
385 :
386 4246 : if (ShouldReportListWriteEnd(mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList),
387 : dataAttributePath))
388 : {
389 4 : DeliverListWriteEnd(mProcessingAttributePath.Value(), mStateFlags.Has(StateBits::kAttributeWriteSuccessful));
390 : }
391 :
392 4246 : if (ShouldReportListWriteBegin(mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList),
393 : dataAttributePath))
394 : {
395 765 : DeliverListWriteBegin(dataAttributePath);
396 765 : mStateFlags.Set(StateBits::kAttributeWriteSuccessful);
397 : }
398 :
399 4246 : mStateFlags.Set(StateBits::kProcessingAttributeIsList, dataAttributePath.IsListOperation());
400 4246 : mProcessingAttributePath.SetValue(dataAttributePath);
401 :
402 4246 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
403 : DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
404 :
405 4246 : TLV::TLVWriter backup;
406 4246 : DataVersion version = 0;
407 4246 : mWriteResponseBuilder.GetWriteResponses().Checkpoint(backup);
408 4246 : err = element.GetDataVersion(&version);
409 4246 : if (CHIP_NO_ERROR == err)
410 : {
411 12 : dataAttributePath.mDataVersion.SetValue(version);
412 : }
413 4234 : else if (CHIP_END_OF_TLV == err)
414 : {
415 4234 : err = CHIP_NO_ERROR;
416 : }
417 4246 : SuccessOrExit(err);
418 4246 : err = WriteClusterData(subjectDescriptor, dataAttributePath, dataReader);
419 4246 : if (err != CHIP_NO_ERROR)
420 : {
421 0 : mWriteResponseBuilder.GetWriteResponses().Rollback(backup);
422 0 : err = AddStatusInternal(dataAttributePath, StatusIB(err));
423 : }
424 :
425 4246 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
426 : DataModelCallbacks::OperationOrder::Post, dataAttributePath);
427 4246 : SuccessOrExit(err);
428 : }
429 :
430 3432 : if (CHIP_END_OF_TLV == err)
431 : {
432 3432 : err = CHIP_NO_ERROR;
433 : }
434 :
435 3432 : SuccessOrExit(err);
436 :
437 3432 : if (!mStateFlags.Has(StateBits::kHasMoreChunks))
438 : {
439 779 : DeliverFinalListWriteEnd(mStateFlags.Has(StateBits::kAttributeWriteSuccessful));
440 : }
441 :
442 2653 : exit:
443 3432 : return err;
444 : }
445 :
446 0 : CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttributeDataIBsReader)
447 : {
448 0 : CHIP_ERROR err = CHIP_NO_ERROR;
449 :
450 0 : VerifyOrReturnError(mExchangeCtx, CHIP_ERROR_INTERNAL);
451 : const Access::SubjectDescriptor subjectDescriptor =
452 0 : mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetSubjectDescriptor();
453 :
454 0 : GroupId groupId = mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
455 0 : FabricIndex fabric = GetAccessingFabricIndex();
456 :
457 0 : while (CHIP_NO_ERROR == (err = aAttributeDataIBsReader.Next()))
458 : {
459 0 : chip::TLV::TLVReader dataReader;
460 0 : AttributeDataIB::Parser element;
461 0 : AttributePathIB::Parser attributePath;
462 0 : ConcreteDataAttributePath dataAttributePath;
463 0 : TLV::TLVReader reader = aAttributeDataIBsReader;
464 :
465 0 : err = element.Init(reader);
466 0 : SuccessOrExit(err);
467 :
468 0 : err = element.GetPath(&attributePath);
469 0 : SuccessOrExit(err);
470 :
471 0 : err = attributePath.GetGroupAttributePath(dataAttributePath);
472 0 : SuccessOrExit(err);
473 :
474 0 : err = element.GetData(&dataReader);
475 0 : SuccessOrExit(err);
476 :
477 0 : if (!dataAttributePath.IsListOperation() && dataReader.GetType() == TLV::TLVType::kTLVType_Array)
478 : {
479 0 : dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
480 : }
481 :
482 0 : ChipLogDetail(DataManagement,
483 : "Received group attribute write for Group=%u Cluster=" ChipLogFormatMEI " attribute=" ChipLogFormatMEI,
484 : groupId, ChipLogValueMEI(dataAttributePath.mClusterId), ChipLogValueMEI(dataAttributePath.mAttributeId));
485 :
486 0 : AutoReleaseGroupEndpointIterator iterator(Credentials::GetGroupDataProvider()->IterateEndpoints(fabric));
487 0 : VerifyOrExit(!iterator.IsNull(), err = CHIP_ERROR_NO_MEMORY);
488 :
489 0 : bool shouldReportListWriteEnd = ShouldReportListWriteEnd(
490 0 : mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList), dataAttributePath);
491 0 : bool shouldReportListWriteBegin = false; // This will be set below.
492 :
493 0 : std::optional<bool> isListAttribute = std::nullopt;
494 :
495 0 : Credentials::GroupDataProvider::GroupEndpoint mapping;
496 0 : while (iterator.Next(mapping))
497 : {
498 0 : if (groupId != mapping.group_id)
499 : {
500 0 : continue;
501 : }
502 :
503 0 : dataAttributePath.mEndpointId = mapping.endpoint_id;
504 :
505 : // Try to get the metadata from for the attribute from one of the expanded endpoints (it doesn't really matter which
506 : // endpoint we pick, as long as it's valid) and update the path info according to it and recheck if we need to report
507 : // list write begin.
508 0 : if (!isListAttribute.has_value())
509 : {
510 0 : isListAttribute = IsListAttributePath(dataAttributePath);
511 0 : bool currentAttributeIsList = isListAttribute.value_or(false);
512 :
513 0 : if (!dataAttributePath.IsListOperation() && currentAttributeIsList)
514 : {
515 0 : dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
516 : }
517 : ConcreteDataAttributePath pathForCheckingListWriteBegin(kInvalidEndpointId, dataAttributePath.mClusterId,
518 0 : dataAttributePath.mEndpointId, dataAttributePath.mListOp,
519 0 : dataAttributePath.mListIndex);
520 : shouldReportListWriteBegin =
521 0 : ShouldReportListWriteBegin(mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList),
522 : pathForCheckingListWriteBegin);
523 : }
524 :
525 0 : if (shouldReportListWriteEnd)
526 : {
527 0 : auto processingConcreteAttributePath = mProcessingAttributePath.Value();
528 0 : processingConcreteAttributePath.mEndpointId = mapping.endpoint_id;
529 0 : VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
530 0 : if (mDelegate->HasConflictWriteRequests(this, processingConcreteAttributePath))
531 : {
532 0 : DeliverListWriteEnd(processingConcreteAttributePath, true /* writeWasSuccessful */);
533 : }
534 : }
535 :
536 0 : VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
537 0 : if (mDelegate->HasConflictWriteRequests(this, dataAttributePath))
538 : {
539 0 : ChipLogDetail(DataManagement,
540 : "Writing attribute endpoint=%u Cluster=" ChipLogFormatMEI " attribute=" ChipLogFormatMEI
541 : " is conflict with other write transactions.",
542 : mapping.endpoint_id, ChipLogValueMEI(dataAttributePath.mClusterId),
543 : ChipLogValueMEI(dataAttributePath.mAttributeId));
544 0 : continue;
545 : }
546 :
547 0 : if (shouldReportListWriteBegin)
548 : {
549 0 : DeliverListWriteBegin(dataAttributePath);
550 : }
551 :
552 0 : ChipLogDetail(DataManagement,
553 : "Processing group attribute write for endpoint=%u Cluster=" ChipLogFormatMEI
554 : " attribute=" ChipLogFormatMEI,
555 : mapping.endpoint_id, ChipLogValueMEI(dataAttributePath.mClusterId),
556 : ChipLogValueMEI(dataAttributePath.mAttributeId));
557 :
558 0 : chip::TLV::TLVReader tmpDataReader(dataReader);
559 :
560 0 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
561 : DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
562 0 : err = WriteClusterData(subjectDescriptor, dataAttributePath, tmpDataReader);
563 0 : if (err != CHIP_NO_ERROR)
564 : {
565 0 : ChipLogError(DataManagement,
566 : "WriteClusterData Endpoint=%u Cluster=" ChipLogFormatMEI " Attribute =" ChipLogFormatMEI
567 : " failed: %" CHIP_ERROR_FORMAT,
568 : mapping.endpoint_id, ChipLogValueMEI(dataAttributePath.mClusterId),
569 : ChipLogValueMEI(dataAttributePath.mAttributeId), err.Format());
570 : }
571 0 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
572 : DataModelCallbacks::OperationOrder::Post, dataAttributePath);
573 : }
574 :
575 0 : dataAttributePath.mEndpointId = kInvalidEndpointId;
576 0 : mStateFlags.Set(StateBits::kProcessingAttributeIsList, dataAttributePath.IsListOperation());
577 0 : mProcessingAttributePath.SetValue(dataAttributePath);
578 0 : }
579 :
580 0 : if (CHIP_END_OF_TLV == err)
581 : {
582 0 : err = CHIP_NO_ERROR;
583 : }
584 :
585 0 : err = DeliverFinalListWriteEndForGroupWrite(true);
586 :
587 0 : exit:
588 : // The DeliverFinalListWriteEndForGroupWrite above will deliver the successful state of the list write and clear the
589 : // mProcessingAttributePath making the following call no-op. So we call it again after the exit label to deliver a failure state
590 : // to the clusters. Ignore the error code since we need to deliver other more important failures.
591 0 : DeliverFinalListWriteEndForGroupWrite(false);
592 0 : return err;
593 : }
594 :
595 3435 : Status WriteHandler::ProcessWriteRequest(System::PacketBufferHandle && aPayload, bool aIsTimedWrite)
596 : {
597 3435 : CHIP_ERROR err = CHIP_NO_ERROR;
598 3435 : System::PacketBufferTLVReader reader;
599 :
600 3435 : WriteRequestMessage::Parser writeRequestParser;
601 3435 : AttributeDataIBs::Parser AttributeDataIBsParser;
602 3435 : TLV::TLVReader AttributeDataIBsReader;
603 : // Default to InvalidAction for our status; that's what we want if any of
604 : // the parsing of our overall structure or paths fails. Once we have a
605 : // successfully parsed path, the only way we will get a failure return is if
606 : // our path handling fails to AddStatus on us.
607 : //
608 : // TODO: That's not technically InvalidAction, and we should probably make
609 : // our callees hand out Status as well.
610 3435 : Status status = Status::InvalidAction;
611 :
612 3435 : mLastSuccessfullyWrittenPath = std::nullopt;
613 :
614 3435 : reader.Init(std::move(aPayload));
615 :
616 3435 : err = writeRequestParser.Init(reader);
617 3435 : SuccessOrExit(err);
618 :
619 : #if CHIP_CONFIG_IM_PRETTY_PRINT
620 3434 : writeRequestParser.PrettyPrint();
621 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
622 : bool boolValue;
623 :
624 3434 : boolValue = mStateFlags.Has(StateBits::kSuppressResponse);
625 3434 : err = writeRequestParser.GetSuppressResponse(&boolValue);
626 3434 : if (err == CHIP_END_OF_TLV)
627 : {
628 4 : err = CHIP_NO_ERROR;
629 : }
630 3434 : SuccessOrExit(err);
631 3434 : mStateFlags.Set(StateBits::kSuppressResponse, boolValue);
632 :
633 3434 : boolValue = mStateFlags.Has(StateBits::kIsTimedRequest);
634 3434 : err = writeRequestParser.GetTimedRequest(&boolValue);
635 3434 : SuccessOrExit(err);
636 3434 : mStateFlags.Set(StateBits::kIsTimedRequest, boolValue);
637 :
638 3434 : boolValue = mStateFlags.Has(StateBits::kHasMoreChunks);
639 3434 : err = writeRequestParser.GetMoreChunkedMessages(&boolValue);
640 3434 : if (err == CHIP_ERROR_END_OF_TLV)
641 : {
642 4 : err = CHIP_NO_ERROR;
643 : }
644 3434 : SuccessOrExit(err);
645 3434 : mStateFlags.Set(StateBits::kHasMoreChunks, boolValue);
646 :
647 8740 : if (mStateFlags.Has(StateBits::kHasMoreChunks) &&
648 5306 : (mExchangeCtx->IsGroupExchangeContext() || mStateFlags.Has(StateBits::kIsTimedRequest)))
649 : {
650 : // Sanity check: group exchange context should only have one chunk.
651 : // Also, timed requests should not have more than one chunk.
652 0 : ExitNow(err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
653 : }
654 :
655 3434 : err = writeRequestParser.GetWriteRequests(&AttributeDataIBsParser);
656 3434 : SuccessOrExit(err);
657 :
658 3434 : if (mStateFlags.Has(StateBits::kIsTimedRequest) != aIsTimedWrite)
659 : {
660 : // The message thinks it should be part of a timed interaction but it's
661 : // not, or vice versa.
662 2 : status = Status::TimedRequestMismatch;
663 2 : goto exit;
664 : }
665 :
666 3432 : AttributeDataIBsParser.GetReader(&AttributeDataIBsReader);
667 :
668 3432 : if (mExchangeCtx->IsGroupExchangeContext())
669 : {
670 0 : err = ProcessGroupAttributeDataIBs(AttributeDataIBsReader);
671 : }
672 : else
673 : {
674 3432 : err = ProcessAttributeDataIBs(AttributeDataIBsReader);
675 : }
676 3432 : SuccessOrExit(err);
677 3432 : SuccessOrExit(err = writeRequestParser.ExitContainer());
678 :
679 3432 : if (err == CHIP_NO_ERROR)
680 : {
681 3432 : status = Status::Success;
682 : }
683 :
684 0 : exit:
685 3435 : if (err != CHIP_NO_ERROR)
686 : {
687 1 : ChipLogError(DataManagement, "Failed to process write request: %" CHIP_ERROR_FORMAT, err.Format());
688 : }
689 6870 : return status;
690 3435 : }
691 :
692 0 : CHIP_ERROR WriteHandler::AddStatus(const ConcreteDataAttributePath & aPath,
693 : const Protocols::InteractionModel::ClusterStatusCode & aStatus)
694 : {
695 0 : return AddStatusInternal(aPath, StatusIB{ aStatus });
696 : }
697 :
698 0 : CHIP_ERROR WriteHandler::AddClusterSpecificSuccess(const ConcreteDataAttributePath & aPath, ClusterStatus aClusterStatus)
699 : {
700 0 : return AddStatus(aPath, Protocols::InteractionModel::ClusterStatusCode::ClusterSpecificSuccess(aClusterStatus));
701 : }
702 :
703 0 : CHIP_ERROR WriteHandler::AddClusterSpecificFailure(const ConcreteDataAttributePath & aPath, ClusterStatus aClusterStatus)
704 : {
705 0 : return AddStatus(aPath, Protocols::InteractionModel::ClusterStatusCode::ClusterSpecificFailure(aClusterStatus));
706 : }
707 :
708 4259 : CHIP_ERROR WriteHandler::AddStatusInternal(const ConcreteDataAttributePath & aPath, const StatusIB & aStatus)
709 : {
710 4259 : AttributeStatusIBs::Builder & writeResponses = mWriteResponseBuilder.GetWriteResponses();
711 4259 : AttributeStatusIB::Builder & attributeStatusIB = writeResponses.CreateAttributeStatus();
712 :
713 4259 : if (!aStatus.IsSuccess())
714 : {
715 37 : mStateFlags.Clear(StateBits::kAttributeWriteSuccessful);
716 : }
717 :
718 4259 : ReturnErrorOnFailure(writeResponses.GetError());
719 :
720 4259 : AttributePathIB::Builder & path = attributeStatusIB.CreatePath();
721 4259 : ReturnErrorOnFailure(attributeStatusIB.GetError());
722 4259 : ReturnErrorOnFailure(path.Encode(aPath));
723 :
724 4259 : StatusIB::Builder & statusIBBuilder = attributeStatusIB.CreateErrorStatus();
725 4259 : ReturnErrorOnFailure(attributeStatusIB.GetError());
726 4259 : statusIBBuilder.EncodeStatusIB(aStatus);
727 4259 : ReturnErrorOnFailure(statusIBBuilder.GetError());
728 4259 : ReturnErrorOnFailure(attributeStatusIB.EndOfAttributeStatusIB());
729 :
730 4259 : MoveToState(State::AddStatus);
731 4259 : return CHIP_NO_ERROR;
732 : }
733 :
734 1 : FabricIndex WriteHandler::GetAccessingFabricIndex() const
735 : {
736 1 : return mExchangeCtx->GetSessionHandle()->GetFabricIndex();
737 : }
738 :
739 9267 : const char * WriteHandler::GetStateStr() const
740 : {
741 : #if CHIP_DETAIL_LOGGING
742 9267 : switch (mState)
743 : {
744 788 : case State::Uninitialized:
745 788 : return "Uninitialized";
746 :
747 788 : case State::Initialized:
748 788 : return "Initialized";
749 :
750 4259 : case State::AddStatus:
751 4259 : return "AddStatus";
752 3432 : case State::Sending:
753 3432 : return "Sending";
754 : }
755 : #endif // CHIP_DETAIL_LOGGING
756 0 : return "N/A";
757 : }
758 :
759 9267 : void WriteHandler::MoveToState(const State aTargetState)
760 : {
761 9267 : mState = aTargetState;
762 9267 : ChipLogDetail(DataManagement, "IM WH moving to [%s]", GetStateStr());
763 9267 : }
764 :
765 4246 : DataModel::ActionReturnStatus WriteHandler::CheckWriteAllowed(const Access::SubjectDescriptor & aSubject,
766 : const ConcreteAttributePath & aPath)
767 : {
768 : // TODO: ordering is to check writability/existence BEFORE ACL and this seems wrong, however
769 : // existing unit tests (TC_AcessChecker.py) validate that we get UnsupportedWrite instead of UnsupportedAccess
770 : //
771 : // This should likely be fixed in spec (probably already fixed by
772 : // https://github.com/CHIP-Specifications/connectedhomeip-spec/pull/9024)
773 : // and tests and implementation
774 : //
775 : // Open issue that needs fixing: https://github.com/project-chip/connectedhomeip/issues/33735
776 :
777 4246 : std::optional<DataModel::AttributeEntry> attributeEntry;
778 4246 : DataModel::AttributeFinder finder(mDataModelProvider);
779 :
780 4246 : attributeEntry = finder.Find(aPath);
781 :
782 : // if path is not valid, return a spec-compliant return code.
783 4246 : if (!attributeEntry.has_value())
784 : {
785 : // Global lists are not in metadata and not writable. Return the correct error code according to the spec
786 : Status attributeErrorStatus =
787 0 : IsSupportedGlobalAttributeNotInMetadata(aPath.mAttributeId) ? Status::UnsupportedWrite : Status::UnsupportedAttribute;
788 :
789 0 : return DataModel::ValidateClusterPath(mDataModelProvider, aPath, attributeErrorStatus);
790 : }
791 :
792 : // Allow writes on writable attributes only
793 4246 : VerifyOrReturnValue(attributeEntry->writePrivilege.has_value(), Status::UnsupportedWrite);
794 :
795 4246 : bool checkAcl = true;
796 4246 : if (mLastSuccessfullyWrittenPath.has_value())
797 : {
798 : // only validate ACL if path has changed
799 : //
800 : // Note that this is NOT operator==: we could do `checkAcl == (aPath != *mLastSuccessfullyWrittenPath)`
801 : // however that seems to use more flash.
802 808 : if ((aPath.mEndpointId == mLastSuccessfullyWrittenPath->mEndpointId) &&
803 1616 : (aPath.mClusterId == mLastSuccessfullyWrittenPath->mClusterId) &&
804 808 : (aPath.mAttributeId == mLastSuccessfullyWrittenPath->mAttributeId))
805 : {
806 804 : checkAcl = false;
807 : }
808 : }
809 :
810 4246 : if (checkAcl)
811 : {
812 3442 : Access::RequestPath requestPath{ .cluster = aPath.mClusterId,
813 3442 : .endpoint = aPath.mEndpointId,
814 : .requestType = Access::RequestType::kAttributeWriteRequest,
815 3442 : .entityId = aPath.mAttributeId };
816 3442 : CHIP_ERROR err = Access::GetAccessControl().Check(aSubject, requestPath, RequiredPrivilege::ForWriteAttribute(aPath));
817 :
818 3442 : if (err != CHIP_NO_ERROR)
819 : {
820 0 : VerifyOrReturnValue(err != CHIP_ERROR_ACCESS_DENIED, Status::UnsupportedAccess);
821 0 : VerifyOrReturnValue(err != CHIP_ERROR_ACCESS_RESTRICTED_BY_ARL, Status::AccessRestricted);
822 :
823 0 : return err;
824 : }
825 : }
826 :
827 : // validate that timed write is enforced
828 4246 : VerifyOrReturnValue(IsTimedWrite() || !attributeEntry->flags.Has(DataModel::AttributeQualityFlags::kTimed),
829 : Status::NeedsTimedInteraction);
830 :
831 4246 : return Status::Success;
832 4246 : }
833 :
834 4246 : CHIP_ERROR WriteHandler::WriteClusterData(const Access::SubjectDescriptor & aSubject, const ConcreteDataAttributePath & aPath,
835 : TLV::TLVReader & aData)
836 : {
837 : // Writes do not have a checked-path. If data model interface is enabled (both checked and only version)
838 : // the write is done via the DataModel interface
839 4246 : VerifyOrReturnError(mDataModelProvider != nullptr, CHIP_ERROR_INCORRECT_STATE);
840 :
841 4246 : ChipLogDetail(DataManagement, "Writing attribute: Cluster=" ChipLogFormatMEI " Endpoint=0x%x AttributeId=" ChipLogFormatMEI,
842 : ChipLogValueMEI(aPath.mClusterId), aPath.mEndpointId, ChipLogValueMEI(aPath.mAttributeId));
843 :
844 4246 : DataModel::ActionReturnStatus status = CheckWriteAllowed(aSubject, aPath);
845 4246 : if (status.IsSuccess())
846 : {
847 4246 : DataModel::WriteAttributeRequest request;
848 :
849 4246 : request.path = aPath;
850 4246 : request.subjectDescriptor = &aSubject;
851 4246 : request.writeFlags.Set(DataModel::WriteFlags::kTimed, IsTimedWrite());
852 :
853 4246 : AttributeValueDecoder decoder(aData, aSubject);
854 4246 : status = mDataModelProvider->WriteAttribute(request, decoder);
855 : }
856 :
857 4246 : mLastSuccessfullyWrittenPath = status.IsSuccess() ? std::make_optional(aPath) : std::nullopt;
858 :
859 4246 : return AddStatusInternal(aPath, StatusIB(status.GetStatusCode()));
860 : }
861 :
862 : } // namespace app
863 : } // namespace chip
|