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