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 2 : explicit AutoReleaseGroupEndpointIterator(Credentials::GroupDataProvider::EndpointIterator * iterator) : mIterator(iterator) {}
60 2 : ~AutoReleaseGroupEndpointIterator()
61 : {
62 2 : if (mIterator != nullptr)
63 : {
64 2 : mIterator->Release();
65 : }
66 2 : }
67 :
68 2 : bool IsNull() const { return mIterator == nullptr; }
69 8 : 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 969 : CHIP_ERROR WriteHandler::Init(DataModel::Provider * apProvider, WriteHandlerDelegate * apWriteHandlerDelegate)
81 : {
82 969 : VerifyOrReturnError(!mExchangeCtx, CHIP_ERROR_INCORRECT_STATE);
83 969 : VerifyOrReturnError(apWriteHandlerDelegate, CHIP_ERROR_INVALID_ARGUMENT);
84 969 : VerifyOrReturnError(apProvider, CHIP_ERROR_INVALID_ARGUMENT);
85 968 : mDataModelProvider = apProvider;
86 :
87 968 : mDelegate = apWriteHandlerDelegate;
88 968 : MoveToState(State::Initialized);
89 :
90 968 : mProcessingAttributePath.ClearValue();
91 :
92 968 : return CHIP_NO_ERROR;
93 : }
94 :
95 968 : void WriteHandler::Close()
96 : {
97 968 : 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 968 : DeliverFinalListWriteEnd(false /* wasSuccessful */);
104 968 : mExchangeCtx.Release();
105 968 : mStateFlags.Clear(StateBits::kSuppressResponse);
106 968 : mDataModelProvider = nullptr;
107 968 : MoveToState(State::Uninitialized);
108 : }
109 :
110 1097 : std::optional<bool> WriteHandler::IsListAttributePath(const ConcreteAttributePath & path)
111 : {
112 1097 : 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 1097 : DataModel::AttributeFinder finder(mDataModelProvider);
121 1097 : std::optional<DataModel::AttributeEntry> info = finder.Find(path);
122 :
123 1097 : if (!info.has_value())
124 : {
125 6 : return std::nullopt;
126 : }
127 :
128 1091 : return info->HasFlags(DataModel::AttributeQualityFlags::kListAttribute);
129 1097 : }
130 :
131 3880 : Status WriteHandler::HandleWriteRequestMessage(Messaging::ExchangeContext * apExchangeContext,
132 : System::PacketBufferHandle && aPayload, bool aIsTimedWrite)
133 : {
134 3880 : System::PacketBufferHandle packet = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
135 3880 : VerifyOrReturnError(!packet.IsNull(), Status::Failure);
136 :
137 3880 : System::PacketBufferTLVWriter messageWriter;
138 3880 : messageWriter.Init(std::move(packet));
139 7760 : VerifyOrReturnError(mWriteResponseBuilder.Init(&messageWriter) == CHIP_NO_ERROR, Status::Failure);
140 :
141 3880 : mWriteResponseBuilder.CreateWriteResponses();
142 7760 : VerifyOrReturnError(mWriteResponseBuilder.GetError() == CHIP_NO_ERROR, Status::Failure);
143 :
144 3880 : Status status = ProcessWriteRequest(std::move(aPayload), aIsTimedWrite);
145 :
146 : // Do not send response on Group Write
147 3880 : 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 3880 : return status;
157 3880 : }
158 :
159 968 : 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 968 : mExchangeCtx.Grab(apExchangeContext);
167 :
168 968 : 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 968 : if (!(status == Status::Success && mStateFlags.Has(StateBits::kHasMoreChunks)))
172 : {
173 105 : Close();
174 : }
175 :
176 968 : 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 3876 : CHIP_ERROR WriteHandler::FinalizeMessage(System::PacketBufferTLVWriter && aMessageWriter, System::PacketBufferHandle & packet)
228 : {
229 3876 : VerifyOrReturnError(mState == State::AddStatus, CHIP_ERROR_INCORRECT_STATE);
230 3876 : ReturnErrorOnFailure(mWriteResponseBuilder.GetWriteResponses().EndOfAttributeStatuses());
231 3876 : ReturnErrorOnFailure(mWriteResponseBuilder.EndOfWriteResponseMessage());
232 3876 : ReturnErrorOnFailure(aMessageWriter.Finalize(&packet));
233 3876 : return CHIP_NO_ERROR;
234 : }
235 :
236 3876 : CHIP_ERROR WriteHandler::SendWriteResponse(System::PacketBufferTLVWriter && aMessageWriter)
237 : {
238 3876 : CHIP_ERROR err = CHIP_NO_ERROR;
239 3876 : System::PacketBufferHandle packet;
240 :
241 3876 : VerifyOrExit(mState == State::AddStatus, err = CHIP_ERROR_INCORRECT_STATE);
242 :
243 3876 : err = FinalizeMessage(std::move(aMessageWriter), packet);
244 3876 : SuccessOrExit(err);
245 :
246 3876 : VerifyOrExit(mExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
247 3876 : err = mExchangeCtx->UseSuggestedResponseTimeout(app::kExpectedIMProcessingTime);
248 3876 : SuccessOrExit(err);
249 7750 : err = mExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::WriteResponse, std::move(packet),
250 3875 : mStateFlags.Has(StateBits::kHasMoreChunks) ? Messaging::SendMessageFlags::kExpectResponse
251 : : Messaging::SendMessageFlags::kNone);
252 3875 : SuccessOrExit(err);
253 :
254 3875 : MoveToState(State::Sending);
255 :
256 3876 : exit:
257 7752 : return err;
258 3876 : }
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 943 : void WriteHandler::DeliverListWriteEnd(const ConcreteAttributePath & aPath, bool writeWasSuccessful)
270 : {
271 943 : if (mDataModelProvider != nullptr)
272 : {
273 943 : mDataModelProvider->ListAttributeWriteNotification(aPath,
274 : writeWasSuccessful ? DataModel::ListWriteOperation::kListWriteSuccess
275 : : DataModel::ListWriteOperation::kListWriteFailure,
276 943 : GetAccessingFabricIndex());
277 : }
278 943 : }
279 :
280 1921 : void WriteHandler::DeliverFinalListWriteEnd(bool writeWasSuccessful)
281 : {
282 1921 : if (mProcessingAttributePath.HasValue() && mStateFlags.Has(StateBits::kProcessingAttributeIsList))
283 : {
284 935 : DeliverListWriteEnd(mProcessingAttributePath.Value(), writeWasSuccessful);
285 : }
286 1921 : mProcessingAttributePath.ClearValue();
287 1921 : }
288 :
289 4 : CHIP_ERROR WriteHandler::DeliverFinalListWriteEndForGroupWrite(bool writeWasSuccessful)
290 : {
291 4 : 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 14508 : bool IsSameAttribute(const Optional<ConcreteAttributePath> & previousProcessed, const ConcreteDataAttributePath & nextAttribute)
329 : {
330 14508 : return previousProcessed.HasValue() && previousProcessed.Value() == nextAttribute;
331 : }
332 :
333 5211 : bool ShouldReportListWriteEnd(const Optional<ConcreteAttributePath> & previousProcessed, bool previousProcessedAttributeIsList,
334 : const ConcreteDataAttributePath & nextAttribute)
335 : {
336 5211 : return previousProcessedAttributeIsList && !IsSameAttribute(previousProcessed, nextAttribute) && previousProcessed.HasValue();
337 : }
338 :
339 5211 : bool ShouldReportListWriteBegin(const Optional<ConcreteAttributePath> & previousProcessed, bool previousProcessedAttributeIsList,
340 : const ConcreteDataAttributePath & nextAttribute)
341 : {
342 5211 : return !IsSameAttribute(previousProcessed, nextAttribute) && nextAttribute.IsListOperation();
343 : }
344 :
345 : } // namespace
346 :
347 3875 : CHIP_ERROR WriteHandler::ProcessAttributeDataIBs(TLV::TLVReader & aAttributeDataIBsReader)
348 : {
349 3875 : CHIP_ERROR err = CHIP_NO_ERROR;
350 :
351 3875 : VerifyOrReturnError(mExchangeCtx, CHIP_ERROR_INTERNAL);
352 3875 : const Access::SubjectDescriptor subjectDescriptor = mExchangeCtx->GetSessionHandle()->GetSubjectDescriptor();
353 :
354 18194 : while (CHIP_NO_ERROR == (err = aAttributeDataIBsReader.Next()))
355 : {
356 5222 : chip::TLV::TLVReader dataReader;
357 5222 : AttributeDataIB::Parser element;
358 5222 : AttributePathIB::Parser attributePath;
359 5222 : ConcreteDataAttributePath dataAttributePath;
360 5222 : TLV::TLVReader reader = aAttributeDataIBsReader;
361 :
362 5222 : err = element.Init(reader);
363 5222 : SuccessOrExit(err);
364 :
365 5222 : err = element.GetPath(&attributePath);
366 5222 : SuccessOrExit(err);
367 :
368 5222 : err = attributePath.GetConcreteAttributePath(dataAttributePath);
369 5222 : SuccessOrExit(err);
370 :
371 5222 : err = element.GetData(&dataReader);
372 5222 : SuccessOrExit(err);
373 :
374 5222 : if (!dataAttributePath.IsListOperation() && IsListAttributePath(dataAttributePath).value_or(false))
375 : {
376 1065 : dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
377 : }
378 :
379 5222 : VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
380 14557 : 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 9335 : (dataAttributePath.IsListItemOperation() && !IsSameAttribute(mProcessingAttributePath, dataAttributePath)))
384 : {
385 13 : err = AddStatusInternal(dataAttributePath, StatusIB(Status::Busy));
386 13 : continue;
387 : }
388 :
389 5209 : if (ShouldReportListWriteEnd(mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList),
390 : dataAttributePath))
391 : {
392 8 : DeliverListWriteEnd(mProcessingAttributePath.Value(), mStateFlags.Has(StateBits::kAttributeWriteSuccessful));
393 : }
394 :
395 5209 : if (ShouldReportListWriteBegin(mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList),
396 : dataAttributePath))
397 : {
398 937 : DeliverListWriteBegin(dataAttributePath);
399 937 : mStateFlags.Set(StateBits::kAttributeWriteSuccessful);
400 : }
401 :
402 5209 : mStateFlags.Set(StateBits::kProcessingAttributeIsList, dataAttributePath.IsListOperation());
403 5209 : mProcessingAttributePath.SetValue(dataAttributePath);
404 :
405 5209 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
406 : DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
407 :
408 5209 : TLV::TLVWriter backup;
409 5209 : DataVersion version = 0;
410 5209 : mWriteResponseBuilder.GetWriteResponses().Checkpoint(backup);
411 5209 : err = element.GetDataVersion(&version);
412 10418 : if (CHIP_NO_ERROR == err)
413 : {
414 12 : dataAttributePath.mDataVersion.SetValue(version);
415 : }
416 10394 : else if (CHIP_END_OF_TLV == err)
417 : {
418 5197 : err = CHIP_NO_ERROR;
419 : }
420 5209 : SuccessOrExit(err);
421 5209 : err = WriteClusterData(subjectDescriptor, dataAttributePath, dataReader);
422 10418 : if (err != CHIP_NO_ERROR)
423 : {
424 0 : mWriteResponseBuilder.GetWriteResponses().Rollback(backup);
425 0 : err = AddStatusInternal(dataAttributePath, StatusIB(err));
426 : }
427 :
428 5209 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
429 : DataModelCallbacks::OperationOrder::Post, dataAttributePath);
430 5209 : SuccessOrExit(err);
431 : }
432 :
433 7750 : if (CHIP_END_OF_TLV == err)
434 : {
435 3875 : err = CHIP_NO_ERROR;
436 : }
437 :
438 3875 : SuccessOrExit(err);
439 :
440 3875 : if (!mStateFlags.Has(StateBits::kHasMoreChunks))
441 : {
442 953 : DeliverFinalListWriteEnd(mStateFlags.Has(StateBits::kAttributeWriteSuccessful));
443 : }
444 :
445 2922 : exit:
446 3875 : return err;
447 : }
448 :
449 2 : CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttributeDataIBsReader)
450 : {
451 2 : CHIP_ERROR err = CHIP_NO_ERROR;
452 :
453 2 : VerifyOrReturnError(mExchangeCtx, CHIP_ERROR_INTERNAL);
454 : const Access::SubjectDescriptor subjectDescriptor =
455 2 : mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetSubjectDescriptor();
456 :
457 2 : GroupId groupId = mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
458 2 : FabricIndex fabric = GetAccessingFabricIndex();
459 :
460 8 : while (CHIP_NO_ERROR == (err = aAttributeDataIBsReader.Next()))
461 : {
462 2 : chip::TLV::TLVReader dataReader;
463 2 : AttributeDataIB::Parser element;
464 2 : AttributePathIB::Parser attributePath;
465 2 : ConcreteDataAttributePath dataAttributePath;
466 2 : TLV::TLVReader reader = aAttributeDataIBsReader;
467 :
468 2 : err = element.Init(reader);
469 2 : SuccessOrExit(err);
470 :
471 2 : err = element.GetPath(&attributePath);
472 2 : SuccessOrExit(err);
473 :
474 2 : err = attributePath.GetGroupAttributePath(dataAttributePath);
475 2 : SuccessOrExit(err);
476 :
477 2 : err = element.GetData(&dataReader);
478 2 : SuccessOrExit(err);
479 :
480 2 : if (!dataAttributePath.IsListOperation() && dataReader.GetType() == TLV::TLVType::kTLVType_Array)
481 : {
482 0 : dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
483 : }
484 :
485 2 : ChipLogDetail(DataManagement,
486 : "Received group attribute write for Group=%u Cluster=" ChipLogFormatMEI " attribute=" ChipLogFormatMEI,
487 : groupId, ChipLogValueMEI(dataAttributePath.mClusterId), ChipLogValueMEI(dataAttributePath.mAttributeId));
488 :
489 2 : AutoReleaseGroupEndpointIterator iterator(Credentials::GetGroupDataProvider()->IterateEndpoints(fabric));
490 2 : VerifyOrExit(!iterator.IsNull(), err = CHIP_ERROR_NO_MEMORY);
491 :
492 2 : bool shouldReportListWriteEnd = ShouldReportListWriteEnd(
493 2 : mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList), dataAttributePath);
494 2 : bool shouldReportListWriteBegin = false; // This will be set below.
495 :
496 2 : std::optional<bool> isListAttribute = std::nullopt;
497 :
498 2 : Credentials::GroupDataProvider::GroupEndpoint mapping;
499 8 : while (iterator.Next(mapping))
500 : {
501 6 : if (groupId != mapping.group_id)
502 : {
503 4 : continue;
504 : }
505 :
506 2 : dataAttributePath.mEndpointId = mapping.endpoint_id;
507 : // Groupcast Testing
508 2 : auto & testing = Groupcast::GetTesting();
509 2 : 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 2 : if (!isListAttribute.has_value())
521 : {
522 2 : isListAttribute = IsListAttributePath(dataAttributePath);
523 2 : bool currentAttributeIsList = isListAttribute.value_or(false);
524 :
525 2 : if (!dataAttributePath.IsListOperation() && currentAttributeIsList)
526 : {
527 0 : dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
528 : }
529 : ConcreteDataAttributePath pathForCheckingListWriteBegin(kInvalidEndpointId, dataAttributePath.mClusterId,
530 2 : dataAttributePath.mEndpointId, dataAttributePath.mListOp,
531 2 : dataAttributePath.mListIndex);
532 : shouldReportListWriteBegin =
533 2 : ShouldReportListWriteBegin(mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList),
534 : pathForCheckingListWriteBegin);
535 : }
536 :
537 2 : 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 2 : VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
549 2 : 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 2 : if (shouldReportListWriteBegin)
560 : {
561 0 : DeliverListWriteBegin(dataAttributePath);
562 : }
563 :
564 2 : 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 2 : chip::TLV::TLVReader tmpDataReader(dataReader);
571 :
572 2 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
573 : DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
574 2 : err = WriteClusterData(subjectDescriptor, dataAttributePath, tmpDataReader);
575 4 : 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 2 : DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
584 : DataModelCallbacks::OperationOrder::Post, dataAttributePath);
585 : }
586 :
587 2 : dataAttributePath.mEndpointId = kInvalidEndpointId;
588 2 : mStateFlags.Set(StateBits::kProcessingAttributeIsList, dataAttributePath.IsListOperation());
589 2 : mProcessingAttributePath.SetValue(dataAttributePath);
590 2 : }
591 :
592 4 : if (CHIP_END_OF_TLV == err)
593 : {
594 2 : err = CHIP_NO_ERROR;
595 : }
596 :
597 2 : err = DeliverFinalListWriteEndForGroupWrite(true);
598 :
599 2 : 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 2 : TEMPORARY_RETURN_IGNORED DeliverFinalListWriteEndForGroupWrite(false);
604 2 : return err;
605 : }
606 :
607 3880 : Status WriteHandler::ProcessWriteRequest(System::PacketBufferHandle && aPayload, bool aIsTimedWrite)
608 : {
609 3880 : CHIP_ERROR err = CHIP_NO_ERROR;
610 3880 : System::PacketBufferTLVReader reader;
611 :
612 3880 : WriteRequestMessage::Parser writeRequestParser;
613 3880 : AttributeDataIBs::Parser AttributeDataIBsParser;
614 3880 : 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 3880 : Status status = Status::InvalidAction;
623 :
624 3880 : mLastSuccessfullyWrittenPath = std::nullopt;
625 :
626 3880 : reader.Init(std::move(aPayload));
627 :
628 3880 : err = writeRequestParser.Init(reader);
629 3880 : SuccessOrExit(err);
630 :
631 : #if CHIP_CONFIG_IM_PRETTY_PRINT
632 3879 : TEMPORARY_RETURN_IGNORED writeRequestParser.PrettyPrint();
633 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
634 : bool boolValue;
635 :
636 3879 : boolValue = mStateFlags.Has(StateBits::kSuppressResponse);
637 3879 : err = writeRequestParser.GetSuppressResponse(&boolValue);
638 7758 : if (err == CHIP_END_OF_TLV)
639 : {
640 4 : err = CHIP_NO_ERROR;
641 : }
642 3879 : SuccessOrExit(err);
643 3879 : mStateFlags.Set(StateBits::kSuppressResponse, boolValue);
644 :
645 3879 : boolValue = mStateFlags.Has(StateBits::kIsTimedRequest);
646 3879 : err = writeRequestParser.GetTimedRequest(&boolValue);
647 3879 : SuccessOrExit(err);
648 3879 : mStateFlags.Set(StateBits::kIsTimedRequest, boolValue);
649 :
650 3879 : boolValue = mStateFlags.Has(StateBits::kHasMoreChunks);
651 3879 : err = writeRequestParser.GetMoreChunkedMessages(&boolValue);
652 7758 : if (err == CHIP_ERROR_END_OF_TLV)
653 : {
654 4 : err = CHIP_NO_ERROR;
655 : }
656 3879 : SuccessOrExit(err);
657 3879 : mStateFlags.Set(StateBits::kHasMoreChunks, boolValue);
658 :
659 9723 : 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 3879 : err = writeRequestParser.GetWriteRequests(&AttributeDataIBsParser);
668 3879 : SuccessOrExit(err);
669 :
670 3879 : 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 3877 : AttributeDataIBsParser.GetReader(&AttributeDataIBsReader);
679 :
680 3877 : if (mExchangeCtx->IsGroupExchangeContext())
681 : {
682 2 : err = ProcessGroupAttributeDataIBs(AttributeDataIBsReader);
683 : }
684 : else
685 : {
686 3875 : err = ProcessAttributeDataIBs(AttributeDataIBsReader);
687 : }
688 3877 : SuccessOrExit(err);
689 3877 : SuccessOrExit(err = writeRequestParser.ExitContainer());
690 :
691 7754 : if (err == CHIP_NO_ERROR)
692 : {
693 3877 : status = Status::Success;
694 : }
695 :
696 0 : exit:
697 7760 : if (err != CHIP_NO_ERROR)
698 : {
699 1 : ChipLogError(DataManagement, "Failed to process write request: %" CHIP_ERROR_FORMAT, err.Format());
700 : }
701 7760 : return status;
702 3880 : }
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 5224 : CHIP_ERROR WriteHandler::AddStatusInternal(const ConcreteDataAttributePath & aPath, const StatusIB & aStatus)
721 : {
722 5224 : AttributeStatusIBs::Builder & writeResponses = mWriteResponseBuilder.GetWriteResponses();
723 5224 : AttributeStatusIB::Builder & attributeStatusIB = writeResponses.CreateAttributeStatus();
724 :
725 5224 : if (!aStatus.IsSuccess())
726 : {
727 49 : mStateFlags.Clear(StateBits::kAttributeWriteSuccessful);
728 : }
729 :
730 5224 : ReturnErrorOnFailure(writeResponses.GetError());
731 :
732 5224 : AttributePathIB::Builder & path = attributeStatusIB.CreatePath();
733 5224 : ReturnErrorOnFailure(attributeStatusIB.GetError());
734 5224 : ReturnErrorOnFailure(path.Encode(aPath));
735 :
736 5224 : StatusIB::Builder & statusIBBuilder = attributeStatusIB.CreateErrorStatus();
737 5224 : ReturnErrorOnFailure(attributeStatusIB.GetError());
738 5224 : statusIBBuilder.EncodeStatusIB(aStatus);
739 5224 : ReturnErrorOnFailure(statusIBBuilder.GetError());
740 5224 : ReturnErrorOnFailure(attributeStatusIB.EndOfAttributeStatusIB());
741 :
742 5224 : MoveToState(State::AddStatus);
743 5224 : return CHIP_NO_ERROR;
744 : }
745 :
746 1883 : FabricIndex WriteHandler::GetAccessingFabricIndex() const
747 : {
748 1883 : 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 11035 : void WriteHandler::MoveToState(const State aTargetState)
772 : {
773 11035 : mState = aTargetState;
774 11035 : ChipLogDetail(DataManagement, "IM WH moving to [%s]", GetStateStr());
775 11035 : }
776 :
777 5211 : 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 5211 : Status writeAccessStatus = CheckWriteAccess(aSubject, aPath, Access::Privilege::kView);
789 5211 : VerifyOrReturnValue(writeAccessStatus == Status::Success, writeAccessStatus);
790 :
791 5208 : DataModel::AttributeFinder finder(mDataModelProvider);
792 :
793 5208 : std::optional<DataModel::AttributeEntry> attributeEntry = finder.Find(aPath);
794 :
795 : // if path is not valid, return a spec-compliant return code.
796 5208 : if (!attributeEntry.has_value())
797 : {
798 3 : return DataModel::ValidateClusterPath(mDataModelProvider, aPath, Status::UnsupportedAttribute);
799 : }
800 :
801 : // Allow writes on writable attributes only
802 5205 : 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 5205 : writeAccessStatus = CheckWriteAccess(aSubject, aPath, *attributeEntry->GetWritePrivilege());
806 5205 : 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 5205 : 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 10363 : 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 5205 : 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 5199 : return Status::Success;
840 5208 : }
841 :
842 10416 : Status WriteHandler::CheckWriteAccess(const Access::SubjectDescriptor & aSubject, const ConcreteAttributePath & aPath,
843 : const Access::Privilege aRequiredPrivilege)
844 : {
845 :
846 10416 : bool checkAcl = true;
847 10416 : 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 2650 : if ((aPath.mEndpointId == mLastSuccessfullyWrittenPath->mEndpointId) &&
854 5300 : (aPath.mClusterId == mLastSuccessfullyWrittenPath->mClusterId) &&
855 2650 : (aPath.mAttributeId == mLastSuccessfullyWrittenPath->mAttributeId))
856 : {
857 2634 : checkAcl = false;
858 : }
859 : }
860 :
861 10416 : if (checkAcl)
862 : {
863 7782 : Access::RequestPath requestPath{ .cluster = aPath.mClusterId,
864 7782 : .endpoint = aPath.mEndpointId,
865 : .requestType = Access::RequestType::kAttributeWriteRequest,
866 7782 : .entityId = aPath.mAttributeId };
867 :
868 7782 : CHIP_ERROR err = Access::GetAccessControl().Check(aSubject, requestPath, aRequiredPrivilege);
869 :
870 15564 : if (err == CHIP_NO_ERROR)
871 : {
872 7779 : 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 2634 : return Status::Success;
889 : }
890 :
891 5211 : 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 5211 : VerifyOrReturnError(mDataModelProvider != nullptr, CHIP_ERROR_INCORRECT_STATE);
897 :
898 5211 : ChipLogDetail(DataManagement, "Writing attribute: Cluster=" ChipLogFormatMEI " Endpoint=0x%x AttributeId=" ChipLogFormatMEI,
899 : ChipLogValueMEI(aPath.mClusterId), aPath.mEndpointId, ChipLogValueMEI(aPath.mAttributeId));
900 :
901 5211 : DataModel::ActionReturnStatus status = CheckWriteAllowed(aSubject, aPath);
902 5211 : if (status.IsSuccess())
903 : {
904 5199 : DataModel::WriteAttributeRequest request(aPath, aSubject);
905 :
906 5199 : request.writeFlags.Set(DataModel::WriteFlags::kTimed, IsTimedWrite());
907 :
908 5199 : AttributeValueDecoder decoder(aData, aSubject);
909 5199 : status = mDataModelProvider->WriteAttribute(request, decoder);
910 : }
911 :
912 5211 : mLastSuccessfullyWrittenPath = status.IsSuccess() ? std::make_optional(aPath) : std::nullopt;
913 :
914 5211 : return AddStatusInternal(aPath, StatusIB(status.GetStatusCode()));
915 : }
916 :
917 : } // namespace app
918 : } // namespace chip
|