Matter SDK Coverage Report
Current view: top level - app - WriteHandler.cpp (source / functions) Coverage Total Hit
Test: SHA:f84fe08d06f240e801b5d923f8a938a9938ca110 Lines: 68.7 % 390 268
Test Date: 2025-02-22 08:08:07 Functions: 71.9 % 32 23

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

Generated by: LCOV version 2.0-1