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